RSInfo.cpp revision 01f05d4b45cbde1e07d4707152908c1d843f1328
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright 2012, The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#define LOG_NDEBUG 0
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Renderscript/RSInfo.h"
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <dlfcn.h>
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <cstring>
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <new>
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <string>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "bcc/Support/FileBase.h"
27#include "bcc/Support/Log.h"
28
29#ifdef HAVE_ANDROID_OS
30#include <cutils/properties.h>
31#endif
32
33using namespace bcc;
34
35const char RSInfo::LibBCCPath[] = "/system/lib/libbcc.so";
36const char RSInfo::LibCompilerRTPath[] = "/system/lib/libcompiler_rt.so";
37const char RSInfo::LibRSPath[] = "/system/lib/libRS.so";
38const char RSInfo::LibCLCorePath[] = "/system/lib/libclcore.bc";
39const char RSInfo::LibCLCoreDebugPath[] = "/system/lib/libclcore_debug.bc";
40#if defined(ARCH_X86_HAVE_SSE2)
41const char RSInfo::LibCLCoreX86Path[] = "/system/lib/libclcore_x86.bc";
42#endif
43#if defined(ARCH_ARM_HAVE_NEON)
44const char RSInfo::LibCLCoreNEONPath[] = "/system/lib/libclcore_neon.bc";
45#endif
46
47const uint8_t *RSInfo::LibBCCSHA1 = NULL;
48const uint8_t *RSInfo::LibCompilerRTSHA1 = NULL;
49const uint8_t *RSInfo::LibRSSHA1 = NULL;
50const uint8_t *RSInfo::LibCLCoreSHA1 = NULL;
51const uint8_t *RSInfo::LibCLCoreDebugSHA1 = NULL;
52#if defined(ARCH_ARM_HAVE_NEON)
53const uint8_t *RSInfo::LibCLCoreNEONSHA1 = NULL;
54#endif
55
56bool RSInfo::LoadBuiltInSHA1Information() {
57#ifdef TARGET_BUILD
58  if (LibBCCSHA1 != NULL) {
59    // Loaded before.
60    return true;
61  }
62
63  void *h = ::dlopen("/system/lib/libbcc.sha1.so", RTLD_LAZY | RTLD_NOW);
64  if (h == NULL) {
65    ALOGE("Failed to load SHA-1 information from shared library '"
66          "/system/lib/libbcc.sha1.so'! (%s)", ::dlerror());
67    return false;
68  }
69
70  LibBCCSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libbcc_so_SHA1"));
71  LibCompilerRTSHA1 =
72      reinterpret_cast<const uint8_t *>(::dlsym(h, "libcompiler_rt_so_SHA1"));
73  LibRSSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libRS_so_SHA1"));
74  LibCLCoreSHA1 =
75      reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_bc_SHA1"));
76  LibCLCoreDebugSHA1 =
77      reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_debug_bc_SHA1"));
78#if defined(ARCH_ARM_HAVE_NEON)
79  LibCLCoreNEONSHA1 =
80      reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_neon_bc_SHA1"));
81#endif
82
83  return true;
84#else  // TARGET_BUILD
85  return false;
86#endif  // TARGET_BUILD
87}
88
89android::String8 RSInfo::GetPath(const char *pFilename) {
90  android::String8 result(pFilename);
91  result.append(".info");
92  return result;
93}
94
95#define PRINT_DEPENDENCY(PREFIX, N, X) \
96        ALOGV("\t" PREFIX "Source name: %s, "                                 \
97                          "SHA-1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"   \
98                                 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",  \
99              (N), (X)[ 0], (X)[ 1], (X)[ 2], (X)[ 3], (X)[ 4], (X)[ 5],      \
100                   (X)[ 6], (X)[ 7], (X)[ 8], (X)[ 9], (X)[10], (X)[11],      \
101                   (X)[12], (X)[13], (X)[14], (X)[15], (X)[16], (X)[17],      \
102                   (X)[18], (X)[19]);
103
104bool RSInfo::CheckDependency(const RSInfo &pInfo,
105                             const char *pInputFilename,
106                             const DependencyTableTy &pDeps) {
107  // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc plus
108  // libclcore_neon.bc if NEON is available on the target device.
109#if !defined(ARCH_ARM_HAVE_NEON)
110  static const unsigned NumBuiltInDependencies = 5;
111#else
112  static const unsigned NumBuiltInDependencies = 6;
113#endif
114
115  LoadBuiltInSHA1Information();
116
117  if (pInfo.mDependencyTable.size() != (pDeps.size() + NumBuiltInDependencies)) {
118    ALOGD("Number of dependencies recorded mismatch (%lu v.s. %lu) in %s!",
119          static_cast<unsigned long>(pInfo.mDependencyTable.size()),
120          static_cast<unsigned long>(pDeps.size()), pInputFilename);
121    return false;
122  } else {
123    // Built-in dependencies always go first.
124    const std::pair<const char *, const uint8_t *> &cache_libbcc_dep =
125        pInfo.mDependencyTable[0];
126    const std::pair<const char *, const uint8_t *> &cache_libcompiler_rt_dep =
127        pInfo.mDependencyTable[1];
128    const std::pair<const char *, const uint8_t *> &cache_libRS_dep =
129        pInfo.mDependencyTable[2];
130    const std::pair<const char *, const uint8_t *> &cache_libclcore_dep =
131        pInfo.mDependencyTable[3];
132    const std::pair<const char *, const uint8_t *> &cache_libclcore_debug_dep =
133        pInfo.mDependencyTable[4];
134#if defined(ARCH_ARM_HAVE_NEON)
135    const std::pair<const char *, const uint8_t *> &cache_libclcore_neon_dep =
136        pInfo.mDependencyTable[5];
137#endif
138
139    // Check libbcc.so.
140    if (::memcmp(cache_libbcc_dep.second, LibBCCSHA1, SHA1_DIGEST_LENGTH) != 0) {
141        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
142              LibBCCPath);
143        PRINT_DEPENDENCY("current - ", LibBCCPath, LibBCCSHA1);
144        PRINT_DEPENDENCY("cache - ", cache_libbcc_dep.first,
145                                     cache_libbcc_dep.second);
146        return false;
147    }
148
149    // Check libcompiler_rt.so.
150    if (::memcmp(cache_libcompiler_rt_dep.second, LibCompilerRTSHA1,
151                 SHA1_DIGEST_LENGTH) != 0) {
152        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
153              LibCompilerRTPath);
154        PRINT_DEPENDENCY("current - ", LibCompilerRTPath, LibCompilerRTSHA1);
155        PRINT_DEPENDENCY("cache - ", cache_libcompiler_rt_dep.first,
156                                     cache_libcompiler_rt_dep.second);
157        return false;
158    }
159
160    // Check libRS.so.
161    if (::memcmp(cache_libRS_dep.second, LibRSSHA1, SHA1_DIGEST_LENGTH) != 0) {
162        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
163              LibRSPath);
164        PRINT_DEPENDENCY("current - ", LibRSPath, LibRSSHA1);
165        PRINT_DEPENDENCY("cache - ", cache_libRS_dep.first,
166                                     cache_libRS_dep.second);
167        return false;
168    }
169
170    // Check libclcore.bc.
171    if (::memcmp(cache_libclcore_dep.second, LibCLCoreSHA1,
172                 SHA1_DIGEST_LENGTH) != 0) {
173        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
174              LibCLCorePath);
175        PRINT_DEPENDENCY("current - ", LibCLCorePath, LibCLCoreSHA1);
176        PRINT_DEPENDENCY("cache - ", cache_libclcore_dep.first,
177                                     cache_libclcore_dep.second);
178        return false;
179    }
180
181    // Check libclcore_debug.bc.
182    if (::memcmp(cache_libclcore_debug_dep.second, LibCLCoreDebugSHA1,
183                 SHA1_DIGEST_LENGTH) != 0) {
184        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
185              LibCLCoreDebugPath);
186        PRINT_DEPENDENCY("current - ", LibCLCoreDebugPath, LibCLCoreDebugSHA1);
187        PRINT_DEPENDENCY("cache - ", cache_libclcore_debug_dep.first,
188                                     cache_libclcore_debug_dep.second);
189        return false;
190    }
191
192#if defined(ARCH_ARM_HAVE_NEON)
193    // Check libclcore_neon.bc if NEON is available.
194    if (::memcmp(cache_libclcore_neon_dep.second, LibCLCoreNEONSHA1,
195                 SHA1_DIGEST_LENGTH) != 0) {
196        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
197              LibCLCoreNEONPath);
198        PRINT_DEPENDENCY("current - ", LibCLCoreNEONPath, LibCLCoreNEONSHA1);
199        PRINT_DEPENDENCY("cache - ", cache_libclcore_neon_dep.first,
200                                     cache_libclcore_neon_dep.second);
201        return false;
202    }
203#endif
204
205    for (unsigned i = 0; i < pDeps.size(); i++) {
206      const std::pair<const char *, const uint8_t *> &cache_dep =
207          pInfo.mDependencyTable[i + NumBuiltInDependencies];
208
209      if ((::strcmp(pDeps[i].first, cache_dep.first) != 0) ||
210          (::memcmp(pDeps[i].second, cache_dep.second,
211                    SHA1_DIGEST_LENGTH) != 0)) {
212        ALOGD("Cache %s is dirty due to the source it dependends on has been "
213              "changed:", pInputFilename);
214        PRINT_DEPENDENCY("given - ", pDeps[i].first, pDeps[i].second);
215        PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
216        return false;
217      }
218    }
219  }
220
221  return true;
222}
223
224RSInfo::RSInfo(size_t pStringPoolSize) : mStringPool(NULL) {
225  ::memset(&mHeader, 0, sizeof(mHeader));
226
227  ::memcpy(mHeader.magic, RSINFO_MAGIC, sizeof(mHeader.magic));
228  ::memcpy(mHeader.version, RSINFO_VERSION, sizeof(mHeader.version));
229
230  mHeader.headerSize = sizeof(mHeader);
231
232  mHeader.dependencyTable.itemSize = sizeof(rsinfo::DependencyTableItem);
233  mHeader.pragmaList.itemSize = sizeof(rsinfo::PragmaItem);
234  mHeader.objectSlotList.itemSize = sizeof(rsinfo::ObjectSlotItem);
235  mHeader.exportVarNameList.itemSize = sizeof(rsinfo::ExportVarNameItem);
236  mHeader.exportFuncNameList.itemSize = sizeof(rsinfo::ExportFuncNameItem);
237  mHeader.exportForeachFuncList.itemSize = sizeof(rsinfo::ExportForeachFuncItem);
238
239  if (pStringPoolSize > 0) {
240    mHeader.strPoolSize = pStringPoolSize;
241    mStringPool = new (std::nothrow) char [ mHeader.strPoolSize ];
242    if (mStringPool == NULL) {
243      ALOGE("Out of memory when allocate memory for string pool in RSInfo "
244            "constructor (size: %u)!", mHeader.strPoolSize);
245    }
246  }
247}
248
249RSInfo::~RSInfo() {
250  delete [] mStringPool;
251}
252
253bool RSInfo::layout(off_t initial_offset) {
254  mHeader.dependencyTable.offset = initial_offset +
255                                   mHeader.headerSize +
256                                   mHeader.strPoolSize;
257  mHeader.dependencyTable.count = mDependencyTable.size();
258
259#define AFTER(_list) ((_list).offset + (_list).itemSize * (_list).count)
260  mHeader.pragmaList.offset = AFTER(mHeader.dependencyTable);
261  mHeader.pragmaList.count = mPragmas.size();
262
263  mHeader.objectSlotList.offset = AFTER(mHeader.pragmaList);
264  mHeader.objectSlotList.count = mObjectSlots.size();
265
266  mHeader.exportVarNameList.offset = AFTER(mHeader.objectSlotList);
267  mHeader.exportVarNameList.count = mExportVarNames.size();
268
269  mHeader.exportFuncNameList.offset = AFTER(mHeader.exportVarNameList);
270  mHeader.exportFuncNameList.count = mExportFuncNames.size();
271
272  mHeader.exportForeachFuncList.offset = AFTER(mHeader.exportFuncNameList);
273  mHeader.exportForeachFuncList.count = mExportForeachFuncs.size();
274#undef AFTER
275
276  return true;
277}
278
279void RSInfo::dump() const {
280  // Hide the codes to save the code size when debugging is disabled.
281#if !LOG_NDEBUG
282
283  // Dump header
284  ALOGV("RSInfo Header:");
285  ALOGV("\tIs threadable: %s", ((mHeader.isThreadable) ? "true" : "false"));
286  ALOGV("\tHeader size: %u", mHeader.headerSize);
287  ALOGV("\tString pool size: %u", mHeader.strPoolSize);
288
289#define DUMP_LIST_HEADER(_name, _header) do { \
290  ALOGV(_name ":"); \
291  ALOGV("\toffset: %u", (_header).offset);  \
292  ALOGV("\t# of item: %u", (_header).count);  \
293  ALOGV("\tsize of each item: %u", (_header).itemSize); \
294} while (false)
295  DUMP_LIST_HEADER("Dependency table", mHeader.dependencyTable);
296  for (DependencyTableTy::const_iterator dep_iter = mDependencyTable.begin(),
297          dep_end = mDependencyTable.end(); dep_iter != dep_end; dep_iter++) {
298    PRINT_DEPENDENCY("", dep_iter->first, dep_iter->second);
299  }
300
301  DUMP_LIST_HEADER("Pragma list", mHeader.pragmaList);
302  for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
303        pragma_end = mPragmas.end(); pragma_iter != pragma_end; pragma_iter++) {
304    ALOGV("\tkey: %s, value: %s", pragma_iter->first, pragma_iter->second);
305  }
306
307  DUMP_LIST_HEADER("RS object slots", mHeader.objectSlotList);
308  for (ObjectSlotListTy::const_iterator slot_iter = mObjectSlots.begin(),
309          slot_end = mObjectSlots.end(); slot_iter != slot_end; slot_iter++) {
310    ALOGV("slot: %u", *slot_iter);
311  }
312
313  DUMP_LIST_HEADER("RS export variables", mHeader.exportVarNameList);
314  for (ExportVarNameListTy::const_iterator var_iter = mExportVarNames.begin(),
315          var_end = mExportVarNames.end(); var_iter != var_end; var_iter++) {
316    ALOGV("name: %s", *var_iter);
317  }
318
319  DUMP_LIST_HEADER("RS export functions", mHeader.exportFuncNameList);
320  for (ExportFuncNameListTy::const_iterator func_iter = mExportFuncNames.begin(),
321        func_end = mExportFuncNames.end(); func_iter != func_end; func_iter++) {
322    ALOGV("name: %s", *func_iter);
323  }
324
325  DUMP_LIST_HEADER("RS foreach list", mHeader.exportForeachFuncList);
326  for (ExportForeachFuncListTy::const_iterator
327          foreach_iter = mExportForeachFuncs.begin(),
328          foreach_end = mExportForeachFuncs.end(); foreach_iter != foreach_end;
329          foreach_iter++) {
330    ALOGV("name: %s, signature: %05x", foreach_iter->first,
331                                       foreach_iter->second);
332  }
333#undef DUMP_LIST_HEADER
334
335#endif // LOG_NDEBUG
336  return;
337}
338
339const char *RSInfo::getStringFromPool(rsinfo::StringIndexTy pStrIdx) const {
340  // String pool uses direct indexing. Ensure that the pStrIdx is within the
341  // range.
342  if (pStrIdx >= mHeader.strPoolSize) {
343    ALOGE("String index #%u is out of range in string pool (size: %u)!",
344          pStrIdx, mHeader.strPoolSize);
345    return NULL;
346  }
347  return &mStringPool[ pStrIdx ];
348}
349
350rsinfo::StringIndexTy RSInfo::getStringIdxInPool(const char *pStr) const {
351  // Assume we are on the flat memory architecture (i.e., the memory space is
352  // continuous.)
353  if ((mStringPool + mHeader.strPoolSize) < pStr) {
354    ALOGE("String %s does not in the string pool!", pStr);
355    return rsinfo::gInvalidStringIndex;
356  }
357  return (pStr - mStringPool);
358}
359
360RSInfo::FloatPrecision RSInfo::getFloatPrecisionRequirement() const {
361  // Check to see if we have any FP precision-related pragmas.
362  std::string relaxed_pragma("rs_fp_relaxed");
363  std::string imprecise_pragma("rs_fp_imprecise");
364  std::string full_pragma("rs_fp_full");
365  bool relaxed_pragma_seen = false;
366  bool imprecise_pragma_seen = false;
367  RSInfo::FloatPrecision result = FP_Full;
368
369  for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
370           pragma_end = mPragmas.end(); pragma_iter != pragma_end;
371       pragma_iter++) {
372    const char *pragma_key = pragma_iter->first;
373    if (!relaxed_pragma.compare(pragma_key)) {
374      if (relaxed_pragma_seen || imprecise_pragma_seen) {
375        ALOGE("Multiple float precision pragmas specified!");
376      }
377      relaxed_pragma_seen = true;
378    } else if (!imprecise_pragma.compare(pragma_key)) {
379      if (relaxed_pragma_seen || imprecise_pragma_seen) {
380        ALOGE("Multiple float precision pragmas specified!");
381      }
382      imprecise_pragma_seen = true;
383    }
384  }
385
386  // Imprecise is selected over Relaxed precision.
387  // In the absence of both, we stick to the default Full precision.
388  if (imprecise_pragma_seen) {
389    result = FP_Imprecise;
390  } else if (relaxed_pragma_seen) {
391    result = FP_Relaxed;
392  }
393
394#ifdef HAVE_ANDROID_OS
395  // Provide an override for precsion via adb shell setprop
396  // adb shell setprop debug.rs.precision rs_fp_full
397  // adb shell setprop debug.rs.precision rs_fp_relaxed
398  // adb shell setprop debug.rs.precision rs_fp_imprecise
399  char precision_prop_buf[PROPERTY_VALUE_MAX];
400  property_get("debug.rs.precision", precision_prop_buf, "");
401
402  if (precision_prop_buf[0]) {
403    if (!relaxed_pragma.compare(precision_prop_buf)) {
404      ALOGI("Switching to RS FP relaxed mode via setprop");
405      result = FP_Relaxed;
406    } else if (!imprecise_pragma.compare(precision_prop_buf)) {
407      ALOGI("Switching to RS FP imprecise mode via setprop");
408      result = FP_Imprecise;
409    } else if (!full_pragma.compare(precision_prop_buf)) {
410      ALOGI("Switching to RS FP full mode via setprop");
411      result = FP_Full;
412    }
413  }
414#endif
415
416  return result;
417}
418