RSInfo.h revision e198abec6c5e3eab380ccf6897b0a0b9c2dd92dd
1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BCC_RS_INFO_H
18#define BCC_RS_INFO_H
19
20#include <stdint.h>
21
22#include <utility>
23
24#include "bcc/Renderscript/RSScript.h"
25#include "bcc/Support/Log.h"
26#include "bcc/Support/Sha1Util.h"
27
28#include <utils/String8.h>
29#include <utils/Vector.h>
30
31namespace bcc {
32
33// Forward declarations
34class FileBase;
35class InputFile;
36class OutputFile;
37class Source;
38
39namespace rsinfo {
40
41/* RS info file magic */
42#define RSINFO_MAGIC      "\0rsinfo\n"
43
44/* RS info file version, encoded in 4 bytes of ASCII */
45#define RSINFO_VERSION    "003\0"
46
47struct __attribute__((packed)) ListHeader {
48  // The offset from the beginning of the file of data
49  uint32_t offset;
50  // Number of item in the list
51  uint32_t count;
52  // Size of each item
53  uint8_t itemSize;
54};
55
56/* RS info file header */
57struct __attribute__((packed)) Header {
58  // Magic versus version
59  uint8_t magic[8];
60  uint8_t version[4];
61
62  uint8_t isThreadable;
63  uint8_t hasDebugInformation;
64
65  uint16_t headerSize;
66
67  uint32_t strPoolSize;
68
69  struct ListHeader dependencyTable;
70  struct ListHeader pragmaList;
71  struct ListHeader objectSlotList;
72  struct ListHeader exportVarNameList;
73  struct ListHeader exportFuncNameList;
74  struct ListHeader exportForeachFuncList;
75};
76
77typedef uint32_t StringIndexTy;
78// Use value -1 as an invalid string index marker. No need to declare with
79// 'static' modifier since 'const' variable has internal linkage by default.
80const StringIndexTy gInvalidStringIndex = static_cast<StringIndexTy>(-1);
81
82struct __attribute__((packed)) DependencyTableItem {
83  StringIndexTy id;
84  // SHA-1 checksum is stored as a string in string pool (and has fixed-length
85  // SHA1_DIGEST_LENGTH (=20) bytes)
86  StringIndexTy sha1;
87};
88
89struct __attribute__((packed)) PragmaItem {
90  // Pragma is a key-value pair.
91  StringIndexTy key;
92  StringIndexTy value;
93};
94
95struct __attribute__((packed)) ObjectSlotItem {
96  uint32_t slot;
97};
98
99struct __attribute__((packed)) ExportVarNameItem {
100  StringIndexTy name;
101};
102
103struct __attribute__((packed)) ExportFuncNameItem {
104  StringIndexTy name;
105};
106
107struct __attribute__((packed)) ExportForeachFuncItem {
108  StringIndexTy name;
109  uint32_t signature;
110};
111
112// Return the human-readable name of the given rsinfo::*Item in the template
113// parameter. This is for debugging and error message.
114template<typename Item>
115inline const char *GetItemTypeName();
116
117template<>
118inline const char *GetItemTypeName<DependencyTableItem>()
119{ return "rs dependency info"; }
120
121template<>
122inline const char *GetItemTypeName<PragmaItem>()
123{  return "rs pragma"; }
124
125template<>
126inline const char *GetItemTypeName<ObjectSlotItem>()
127{  return "rs object slot"; }
128
129template<>
130inline const char *GetItemTypeName<ExportVarNameItem>()
131{ return "rs export var"; }
132
133template<>
134inline const char *GetItemTypeName<ExportFuncNameItem>()
135{  return "rs export func"; }
136
137template<>
138inline const char *GetItemTypeName<ExportForeachFuncItem>()
139{ return "rs export foreach"; }
140
141} // end namespace rsinfo
142
143class RSInfo {
144public:
145  typedef android::Vector<std::pair<const char *,
146                                    const uint8_t *> > DependencyTableTy;
147  typedef android::Vector<std::pair<const char*, const char*> > PragmaListTy;
148  typedef android::Vector<uint32_t> ObjectSlotListTy;
149  typedef android::Vector<const char *> ExportVarNameListTy;
150  typedef android::Vector<const char *> ExportFuncNameListTy;
151  typedef android::Vector<std::pair<const char *,
152                                    uint32_t> > ExportForeachFuncListTy;
153
154public:
155  // Calculate or load the SHA-1 information of the built-in dependencies.
156  static void LoadBuiltInSHA1Information();
157
158  // Return the path of the RS info file corresponded to the given output
159  // executable file.
160  static android::String8 GetPath(const FileBase &pFile);
161
162  static const char LibBCCPath[];
163  static const char LibRSPath[];
164  static const char LibCLCorePath[];
165#if defined(ARCH_ARM_HAVE_NEON)
166  static const char LibCLCoreNEONPath[];
167#endif
168
169private:
170  // SHA-1 of the built-in dependencies. Will be initialized in
171  // LoadBuiltInSHA1Information().
172  static const uint8_t *LibBCCSHA1;
173  static const uint8_t *LibRSSHA1;
174  static const uint8_t *LibCLCoreSHA1;
175#if defined(ARCH_ARM_HAVE_NEON)
176  static const uint8_t *LibCLCoreNEONSHA1;
177#endif
178
179  static bool CheckDependency(const RSInfo &pInfo,
180                              const char *pInputFilename,
181                              const DependencyTableTy &pDeps);
182  static bool AddBuiltInDependencies(RSInfo &pInfo);
183
184  rsinfo::Header mHeader;
185
186  char *mStringPool;
187
188  // In most of the time, there're 4 source dependencies stored (libbcc.so,
189  // libRS.so, libclcore and the input bitcode itself.)
190  DependencyTableTy mDependencyTable;
191  PragmaListTy mPragmas;
192  ObjectSlotListTy mObjectSlots;
193  ExportVarNameListTy mExportVarNames;
194  ExportFuncNameListTy mExportFuncNames;
195  ExportForeachFuncListTy mExportForeachFuncs;
196
197  // Initialize an empty RSInfo with its size of string pool is pStringPoolSize.
198  RSInfo(size_t pStringPoolSize);
199
200  // layout() assigns value of offset in each ListHeader (i.e., it decides where
201  // data should go in the file.) It also updates fields other than offset to
202  // reflect the current RSInfo object states to mHeader.
203  bool layout(off_t initial_offset);
204
205public:
206  ~RSInfo();
207
208  // Implemented in RSInfoExtractor.cpp.
209  static RSInfo *ExtractFromSource(const Source &pSource,
210                                   const DependencyTableTy &pDeps);
211
212  // Implemented in RSInfoReader.cpp.
213  static RSInfo *ReadFromFile(InputFile &pInput,
214                              const DependencyTableTy &pDeps);
215
216  // Implemneted in RSInfoWriter.cpp
217  bool write(OutputFile &pOutput);
218
219  void dump() const;
220
221  // const getter
222  inline bool isThreadable() const
223  { return mHeader.isThreadable; }
224  inline bool hasDebugInformation() const
225  { return mHeader.hasDebugInformation; }
226  inline const DependencyTableTy &getDependencyTable() const
227  { return mDependencyTable; }
228  inline const PragmaListTy &getPragmas() const
229  { return mPragmas; }
230  inline const ObjectSlotListTy &getObjectSlots() const
231  { return mObjectSlots; }
232  inline const ExportVarNameListTy &getExportVarNames() const
233  { return mExportVarNames; }
234  inline const ExportFuncNameListTy &getExportFuncNames() const
235  { return mExportFuncNames; }
236  inline const ExportForeachFuncListTy &getExportForeachFuncs() const
237  { return mExportForeachFuncs; }
238
239  const char *getStringFromPool(rsinfo::StringIndexTy pStrIdx) const;
240  rsinfo::StringIndexTy getStringIdxInPool(const char *pStr) const;
241
242  // setter
243  inline void setThreadable(bool pThreadable = true)
244  { mHeader.isThreadable = pThreadable; }
245
246public:
247  enum FloatPrecision {
248    FP_Full,
249    FP_Relaxed,
250    FP_Imprecise,
251  };
252
253  // Return the minimal floating point precision required for the associated
254  // script.
255  FloatPrecision getFloatPrecisionRequirement() const;
256};
257
258} // end namespace bcc
259
260#endif  // BCC_RS_INFO_H
261