blob: 3939ee2b87d8038bda1dd9a851833eb0918d689b [file] [log] [blame]
Zonr Chang1e2adce2012-04-12 13:29:43 +08001/*
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 Changc72c4dd2012-04-12 15:38:53 +080018#include "bcc/RenderScript/RSInfo.h"
Zonr Chang1e2adce2012-04-12 13:29:43 +080019
Zonr Changf2907932012-04-13 11:56:21 +080020#include <dlfcn.h>
21
Zonr Chang1e2adce2012-04-12 13:29:43 +080022#include <cstring>
23#include <new>
24
Zonr Changc72c4dd2012-04-12 15:38:53 +080025#include "bcc/Support/FileBase.h"
Zonr Changef73a242012-04-12 16:44:01 +080026#include "bcc/Support/Log.h"
Zonr Chang1e2adce2012-04-12 13:29:43 +080027
28using namespace bcc;
29
30const char RSInfo::LibBCCPath[] = "/system/lib/libbcc.so";
31const char RSInfo::LibRSPath[] = "/system/lib/libRS.so";
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070032const char RSInfo::LibCLCorePath[] = "/system/lib/libclcore.bc";
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070033#if defined(ARCH_ARM_HAVE_NEON)
34const char RSInfo::LibCLCoreNEONPath[] = "/system/lib/libclcore_neon.bc";
35#endif
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070036
Zonr Changf2907932012-04-13 11:56:21 +080037const uint8_t *RSInfo::LibBCCSHA1 = NULL;
38const uint8_t *RSInfo::LibRSSHA1 = NULL;
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070039const uint8_t *RSInfo::LibCLCoreSHA1 = NULL;
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070040#if defined(ARCH_ARM_HAVE_NEON)
41const uint8_t *RSInfo::LibCLCoreNEONSHA1 = NULL;
42#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +080043
44void RSInfo::LoadBuiltInSHA1Information() {
Zonr Changf2907932012-04-13 11:56:21 +080045 if (LibBCCSHA1 != NULL) {
46 // Loaded before.
Zonr Chang1e2adce2012-04-12 13:29:43 +080047 return;
48 }
49
Zonr Changf2907932012-04-13 11:56:21 +080050 void *h = ::dlopen("/system/lib/libbcc.sha1.so", RTLD_LAZY | RTLD_NOW);
51 if (h == NULL) {
52 ALOGE("Failed to load SHA-1 information from shared library '"
53 "/system/lib/libbcc.sha1.so'! (%s)", ::dlerror());
54 return;
55 }
Zonr Chang1e2adce2012-04-12 13:29:43 +080056
Zonr Changf2907932012-04-13 11:56:21 +080057 LibBCCSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libbcc_so_SHA1"));
58 LibRSSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libRS_so_SHA1"));
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070059 LibCLCoreSHA1 =
60 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_bc_SHA1"));
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070061#if defined(ARCH_ARM_HAVE_NEON)
62 LibCLCoreNEONSHA1 =
63 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_neon_bc_SHA1"));
64#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +080065
66 return;
67}
68
69android::String8 RSInfo::GetPath(const FileBase &pFile) {
70 android::String8 result(pFile.getName().c_str());
71 result.append(".info");
72 return result;
73}
74
75#define PRINT_DEPENDENCY(PREFIX, N, X) \
76 ALOGV("\t" PREFIX "Source name: %s, " \
77 "SHA-1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
78 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", \
79 (N), (X)[ 0], (X)[ 1], (X)[ 2], (X)[ 3], (X)[ 4], (X)[ 5], \
80 (X)[ 6], (X)[ 7], (X)[ 8], (X)[ 9], (X)[10], (X)[11], \
81 (X)[12], (X)[13], (X)[14], (X)[15], (X)[16], (X)[17], \
82 (X)[18], (X)[19]);
83
84bool RSInfo::CheckDependency(const RSInfo &pInfo,
85 const char *pInputFilename,
Shih-wei Liao7bcec852012-04-25 04:07:09 -070086 const DependencyTableTy &pDeps) {
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070087 // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc plus
88 // libclcore_neon.bc if NEON is available on the target device.
89#if !defined(ARCH_ARM_HAVE_NEON)
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070090 static const unsigned NumBuiltInDependencies = 3;
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070091#else
92 static const unsigned NumBuiltInDependencies = 4;
93#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +080094
95 LoadBuiltInSHA1Information();
96
97 if (pInfo.mDependencyTable.size() != (pDeps.size() + NumBuiltInDependencies)) {
98 ALOGD("Number of dependencies recorded mismatch (%lu v.s. %lu) in %s!",
99 static_cast<unsigned long>(pInfo.mDependencyTable.size()),
100 static_cast<unsigned long>(pDeps.size()), pInputFilename);
101 return false;
102 } else {
103 // Built-in dependencies always go first.
104 const std::pair<const char *, const uint8_t *> &cache_libbcc_dep =
105 pInfo.mDependencyTable[0];
106 const std::pair<const char *, const uint8_t *> &cache_libRS_dep =
107 pInfo.mDependencyTable[1];
Shih-wei Liao2665c2f2012-04-25 04:06:52 -0700108 const std::pair<const char *, const uint8_t *> &cache_libclcore_dep =
109 pInfo.mDependencyTable[2];
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700110#if defined(ARCH_ARM_HAVE_NEON)
111 const std::pair<const char *, const uint8_t *> &cache_libclcore_neon_dep =
112 pInfo.mDependencyTable[3];
113#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +0800114
115 // Check libbcc.so.
Zonr Changf2907932012-04-13 11:56:21 +0800116 if (::memcmp(cache_libbcc_dep.second, LibBCCSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800117 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
118 LibBCCPath);
119 PRINT_DEPENDENCY("current - ", LibBCCPath, LibBCCSHA1);
120 PRINT_DEPENDENCY("cache - ", cache_libbcc_dep.first,
121 cache_libbcc_dep.second);
122 return false;
123 }
124
125 // Check libRS.so.
Zonr Changf2907932012-04-13 11:56:21 +0800126 if (::memcmp(cache_libRS_dep.second, LibRSSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800127 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
128 LibRSPath);
129 PRINT_DEPENDENCY("current - ", LibRSPath, LibRSSHA1);
130 PRINT_DEPENDENCY("cache - ", cache_libRS_dep.first,
131 cache_libRS_dep.second);
132 return false;
133 }
134
Shih-wei Liao2665c2f2012-04-25 04:06:52 -0700135 // Check libclcore.bc.
136 if (::memcmp(cache_libclcore_dep.second, LibCLCoreSHA1,
137 SHA1_DIGEST_LENGTH) != 0) {
138 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
139 LibRSPath);
140 PRINT_DEPENDENCY("current - ", LibCLCorePath, LibCLCoreSHA1);
141 PRINT_DEPENDENCY("cache - ", cache_libclcore_dep.first,
142 cache_libclcore_dep.second);
143 return false;
144 }
145
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700146#if defined(ARCH_ARM_HAVE_NEON)
147 // Check libclcore_neon.bc if NEON is available.
148 if (::memcmp(cache_libclcore_neon_dep.second, LibCLCoreNEONSHA1,
149 SHA1_DIGEST_LENGTH) != 0) {
150 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
151 LibRSPath);
152 PRINT_DEPENDENCY("current - ", LibCLCoreNEONPath, LibCLCoreNEONSHA1);
153 PRINT_DEPENDENCY("cache - ", cache_libclcore_neon_dep.first,
154 cache_libclcore_neon_dep.second);
155 return false;
156 }
157#endif
158
Zonr Chang1e2adce2012-04-12 13:29:43 +0800159 for (unsigned i = 0; i < pDeps.size(); i++) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800160 const std::pair<const char *, const uint8_t *> &cache_dep =
161 pInfo.mDependencyTable[i + NumBuiltInDependencies];
162
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700163 if ((::strcmp(pDeps[i].first, cache_dep.first) != 0) ||
164 (::memcmp(pDeps[i].second, cache_dep.second,
Zonr Changf2907932012-04-13 11:56:21 +0800165 SHA1_DIGEST_LENGTH) != 0)) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800166 ALOGD("Cache %s is dirty due to the source it dependends on has been "
167 "changed:", pInputFilename);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700168 PRINT_DEPENDENCY("given - ", pDeps[i].first, pDeps[i].second);
Zonr Chang1e2adce2012-04-12 13:29:43 +0800169 PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
170 return false;
171 }
172 }
173 }
174
175 return true;
176}
177
178RSInfo::RSInfo(size_t pStringPoolSize) : mStringPool(NULL) {
179 ::memset(&mHeader, 0, sizeof(mHeader));
180
181 ::memcpy(mHeader.magic, RSINFO_MAGIC, sizeof(mHeader.magic));
182 ::memcpy(mHeader.version, RSINFO_VERSION, sizeof(mHeader.version));
183
184 mHeader.headerSize = sizeof(mHeader);
185
186 mHeader.dependencyTable.itemSize = sizeof(rsinfo::DependencyTableItem);
187 mHeader.pragmaList.itemSize = sizeof(rsinfo::PragmaItem);
188 mHeader.objectSlotList.itemSize = sizeof(rsinfo::ObjectSlotItem);
189 mHeader.exportVarNameList.itemSize = sizeof(rsinfo::ExportVarNameItem);
190 mHeader.exportFuncNameList.itemSize = sizeof(rsinfo::ExportFuncNameItem);
191 mHeader.exportForeachFuncList.itemSize = sizeof(rsinfo::ExportForeachFuncItem);
192
193 if (pStringPoolSize > 0) {
194 mHeader.strPoolSize = pStringPoolSize;
195 mStringPool = new (std::nothrow) char [ mHeader.strPoolSize ];
196 if (mStringPool == NULL) {
197 ALOGE("Out of memory when allocate memory for string pool in RSInfo "
198 "constructor (size: %u)!", mHeader.strPoolSize);
199 }
200 }
201}
202
203RSInfo::~RSInfo() {
204 delete [] mStringPool;
205}
206
207bool RSInfo::layout(off_t initial_offset) {
208 mHeader.dependencyTable.offset = initial_offset +
209 mHeader.headerSize +
210 mHeader.strPoolSize;
211 mHeader.dependencyTable.count = mDependencyTable.size();
212
213#define AFTER(_list) ((_list).offset + (_list).itemSize * (_list).count)
214 mHeader.pragmaList.offset = AFTER(mHeader.dependencyTable);
215 mHeader.pragmaList.count = mPragmas.size();
216
217 mHeader.objectSlotList.offset = AFTER(mHeader.pragmaList);
218 mHeader.objectSlotList.count = mObjectSlots.size();
219
220 mHeader.exportVarNameList.offset = AFTER(mHeader.objectSlotList);
221 mHeader.exportVarNameList.count = mExportVarNames.size();
222
223 mHeader.exportFuncNameList.offset = AFTER(mHeader.exportVarNameList);
224 mHeader.exportFuncNameList.count = mExportFuncNames.size();
225
226 mHeader.exportForeachFuncList.offset = AFTER(mHeader.exportFuncNameList);
227 mHeader.exportForeachFuncList.count = mExportForeachFuncs.size();
228#undef AFTER
229
230 return true;
231}
232
233void RSInfo::dump() const {
234 // Hide the codes to save the code size when debugging is disabled.
235#if !LOG_NDEBUG
236
237 // Dump header
238 ALOGV("RSInfo Header:");
239 ALOGV("\tIs threadable: %s", ((mHeader.isThreadable) ? "true" : "false"));
240 ALOGV("\tHeader size: %u", mHeader.headerSize);
241 ALOGV("\tString pool size: %u", mHeader.strPoolSize);
242
243#define DUMP_LIST_HEADER(_name, _header) do { \
244 ALOGV(_name ":"); \
245 ALOGV("\toffset: %u", (_header).offset); \
246 ALOGV("\t# of item: %u", (_header).count); \
247 ALOGV("\tsize of each item: %u", (_header).itemSize); \
248} while (false)
249 DUMP_LIST_HEADER("Dependency table", mHeader.dependencyTable);
250 for (DependencyTableTy::const_iterator dep_iter = mDependencyTable.begin(),
251 dep_end = mDependencyTable.end(); dep_iter != dep_end; dep_iter++) {
252 PRINT_DEPENDENCY("", dep_iter->first, dep_iter->second);
253 }
254
255 DUMP_LIST_HEADER("Pragma list", mHeader.pragmaList);
256 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
257 pragma_end = mPragmas.end(); pragma_iter != pragma_end; pragma_iter++) {
258 ALOGV("\tkey: %s, value: %s", pragma_iter->first, pragma_iter->second);
259 }
260
261 DUMP_LIST_HEADER("RS object slots", mHeader.objectSlotList);
262 for (ObjectSlotListTy::const_iterator slot_iter = mObjectSlots.begin(),
263 slot_end = mObjectSlots.end(); slot_iter != slot_end; slot_iter++) {
264 ALOGV("slot: %u", *slot_iter);
265 }
266
267 DUMP_LIST_HEADER("RS export variables", mHeader.exportVarNameList);
268 for (ExportVarNameListTy::const_iterator var_iter = mExportVarNames.begin(),
269 var_end = mExportVarNames.end(); var_iter != var_end; var_iter++) {
270 ALOGV("name: %s", *var_iter);
271 }
272
273 DUMP_LIST_HEADER("RS export functions", mHeader.exportFuncNameList);
274 for (ExportFuncNameListTy::const_iterator func_iter = mExportFuncNames.begin(),
275 func_end = mExportFuncNames.end(); func_iter != func_end; func_iter++) {
276 ALOGV("name: %s", *func_iter);
277 }
278
279 DUMP_LIST_HEADER("RS foreach list", mHeader.exportForeachFuncList);
280 for (ExportForeachFuncListTy::const_iterator
281 foreach_iter = mExportForeachFuncs.begin(),
282 foreach_end = mExportForeachFuncs.end(); foreach_iter != foreach_end;
283 foreach_iter++) {
284 ALOGV("name: %s, signature: %05x", foreach_iter->first,
285 foreach_iter->second);
286 }
287#undef DUMP_LIST_HEADER
288
289#endif // LOG_NDEBUG
290 return;
291}
292
293const char *RSInfo::getStringFromPool(rsinfo::StringIndexTy pStrIdx) const {
294 // String pool uses direct indexing. Ensure that the pStrIdx is within the
295 // range.
296 if (pStrIdx >= mHeader.strPoolSize) {
297 ALOGE("String index #%u is out of range in string pool (size: %u)!",
298 pStrIdx, mHeader.strPoolSize);
299 return NULL;
300 }
301 return &mStringPool[ pStrIdx ];
302}
303
304rsinfo::StringIndexTy RSInfo::getStringIdxInPool(const char *pStr) const {
305 // Assume we are on the flat memory architecture (i.e., the memory space is
306 // continuous.)
307 if ((mStringPool + mHeader.strPoolSize) < pStr) {
308 ALOGE("String %s does not in the string pool!", pStr);
309 return rsinfo::gInvalidStringIndex;
310 }
311 return (pStr - mStringPool);
312}
313
314enum RSInfo::FloatPrecision RSInfo::getFloatPrecisionRequirement() const {
315 // Check to see if we have any FP precision-related pragmas.
316 static const char relaxed_pragma[] = "rs_fp_relaxed";
317 static const char imprecise_pragma[] = "rs_fp_imprecise";
318 bool relaxed_pragma_seen = false;
319
320 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
321 pragma_end = mPragmas.end(); pragma_iter != pragma_end;
322 pragma_iter++) {
323 const char *pragma_key = pragma_iter->first;
324 if (::strcmp(pragma_key, relaxed_pragma) == 0) {
325 relaxed_pragma_seen = true;
326 } else if (::strcmp(pragma_key, imprecise_pragma) == 0) {
327 if (relaxed_pragma_seen) {
328 ALOGW("Multiple float precision pragmas specified!");
329 }
330 // Fast return when there's rs_fp_imprecise specified.
331 return Imprecise;
332 }
333 }
334
335 // Imprecise is selected over Relaxed precision.
336 // In the absence of both, we stick to the default Full precision.
337 if (relaxed_pragma_seen) {
338 return Relaxed;
339 } else {
340 return Full;
341 }
342 // unreachable
343}