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