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