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