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