RSInfo.h revision 7233dcad720ea7e4f6b40b0448244bd4f0c5307e
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    "004\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 bool 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 LibCompilerRTPath[];
164  static const char LibRSPath[];
165  static const char LibCLCorePath[];
166#if defined(ARCH_ARM_HAVE_NEON)
167  static const char LibCLCoreNEONPath[];
168#endif
169
170private:
171  // SHA-1 of the built-in dependencies. Will be initialized in
172  // LoadBuiltInSHA1Information().
173  static const uint8_t *LibBCCSHA1;
174  static const uint8_t *LibCompilerRTSHA1;
175  static const uint8_t *LibRSSHA1;
176  static const uint8_t *LibCLCoreSHA1;
177#if defined(ARCH_ARM_HAVE_NEON)
178  static const uint8_t *LibCLCoreNEONSHA1;
179#endif
180
181  static bool CheckDependency(const RSInfo &pInfo,
182                              const char *pInputFilename,
183                              const DependencyTableTy &pDeps);
184  static bool AddBuiltInDependencies(RSInfo &pInfo);
185
186  rsinfo::Header mHeader;
187
188  char *mStringPool;
189
190  // In most of the time, there're 4 source dependencies stored (libbcc.so,
191  // libRS.so, libclcore and the input bitcode itself.)
192  DependencyTableTy mDependencyTable;
193  PragmaListTy mPragmas;
194  ObjectSlotListTy mObjectSlots;
195  ExportVarNameListTy mExportVarNames;
196  ExportFuncNameListTy mExportFuncNames;
197  ExportForeachFuncListTy mExportForeachFuncs;
198
199  // Initialize an empty RSInfo with its size of string pool is pStringPoolSize.
200  RSInfo(size_t pStringPoolSize);
201
202  // layout() assigns value of offset in each ListHeader (i.e., it decides where
203  // data should go in the file.) It also updates fields other than offset to
204  // reflect the current RSInfo object states to mHeader.
205  bool layout(off_t initial_offset);
206
207public:
208  ~RSInfo();
209
210  // Implemented in RSInfoExtractor.cpp.
211  static RSInfo *ExtractFromSource(const Source &pSource,
212                                   const DependencyTableTy &pDeps);
213
214  // Implemented in RSInfoReader.cpp.
215  static RSInfo *ReadFromFile(InputFile &pInput,
216                              const DependencyTableTy &pDeps);
217
218  // Implemneted in RSInfoWriter.cpp
219  bool write(OutputFile &pOutput);
220
221  void dump() const;
222
223  // const getter
224  inline bool isThreadable() const
225  { return mHeader.isThreadable; }
226  inline bool hasDebugInformation() const
227  { return mHeader.hasDebugInformation; }
228  inline const DependencyTableTy &getDependencyTable() const
229  { return mDependencyTable; }
230  inline const PragmaListTy &getPragmas() const
231  { return mPragmas; }
232  inline const ObjectSlotListTy &getObjectSlots() const
233  { return mObjectSlots; }
234  inline const ExportVarNameListTy &getExportVarNames() const
235  { return mExportVarNames; }
236  inline const ExportFuncNameListTy &getExportFuncNames() const
237  { return mExportFuncNames; }
238  inline const ExportForeachFuncListTy &getExportForeachFuncs() const
239  { return mExportForeachFuncs; }
240
241  const char *getStringFromPool(rsinfo::StringIndexTy pStrIdx) const;
242  rsinfo::StringIndexTy getStringIdxInPool(const char *pStr) const;
243
244  // setter
245  inline void setThreadable(bool pThreadable = true)
246  { mHeader.isThreadable = pThreadable; }
247
248public:
249  enum FloatPrecision {
250    FP_Full,
251    FP_Relaxed,
252    FP_Imprecise,
253  };
254
255  // Return the minimal floating point precision required for the associated
256  // script.
257  FloatPrecision getFloatPrecisionRequirement() const;
258};
259
260} // end namespace bcc
261
262#endif  // BCC_RS_INFO_H
263