blob: ff6a0a28e70ff3dfce987b07f87b089d65a00915 [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
Stephen Hinese198abe2012-07-27 18:05:41 -070018#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>
Stephen Hines688e4c02012-12-12 18:04:18 -080024#include <string>
Zonr Chang1e2adce2012-04-12 13:29:43 +080025
Zonr Changc72c4dd2012-04-12 15:38:53 +080026#include "bcc/Support/FileBase.h"
Zonr Changef73a242012-04-12 16:44:01 +080027#include "bcc/Support/Log.h"
Zonr Chang1e2adce2012-04-12 13:29:43 +080028
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -070029#include <cutils/properties.h>
30
Zonr Chang1e2adce2012-04-12 13:29:43 +080031using namespace bcc;
32
33const char RSInfo::LibBCCPath[] = "/system/lib/libbcc.so";
34const char RSInfo::LibRSPath[] = "/system/lib/libRS.so";
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070035const char RSInfo::LibCLCorePath[] = "/system/lib/libclcore.bc";
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070036#if defined(ARCH_ARM_HAVE_NEON)
37const char RSInfo::LibCLCoreNEONPath[] = "/system/lib/libclcore_neon.bc";
38#endif
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070039
Zonr Changf2907932012-04-13 11:56:21 +080040const uint8_t *RSInfo::LibBCCSHA1 = NULL;
41const uint8_t *RSInfo::LibRSSHA1 = NULL;
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070042const uint8_t *RSInfo::LibCLCoreSHA1 = NULL;
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070043#if defined(ARCH_ARM_HAVE_NEON)
44const uint8_t *RSInfo::LibCLCoreNEONSHA1 = NULL;
45#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +080046
Stephen Hines331310e2012-10-26 19:27:55 -070047bool RSInfo::LoadBuiltInSHA1Information() {
48#ifdef TARGET_BUILD
Zonr Changf2907932012-04-13 11:56:21 +080049 if (LibBCCSHA1 != NULL) {
50 // Loaded before.
Stephen Hines331310e2012-10-26 19:27:55 -070051 return true;
Zonr Chang1e2adce2012-04-12 13:29:43 +080052 }
53
Zonr Changf2907932012-04-13 11:56:21 +080054 void *h = ::dlopen("/system/lib/libbcc.sha1.so", RTLD_LAZY | RTLD_NOW);
55 if (h == NULL) {
56 ALOGE("Failed to load SHA-1 information from shared library '"
57 "/system/lib/libbcc.sha1.so'! (%s)", ::dlerror());
Stephen Hines331310e2012-10-26 19:27:55 -070058 return false;
Zonr Changf2907932012-04-13 11:56:21 +080059 }
Zonr Chang1e2adce2012-04-12 13:29:43 +080060
Zonr Changf2907932012-04-13 11:56:21 +080061 LibBCCSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libbcc_so_SHA1"));
62 LibRSSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libRS_so_SHA1"));
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070063 LibCLCoreSHA1 =
64 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_bc_SHA1"));
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070065#if defined(ARCH_ARM_HAVE_NEON)
66 LibCLCoreNEONSHA1 =
67 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_neon_bc_SHA1"));
68#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +080069
Stephen Hines331310e2012-10-26 19:27:55 -070070 return true;
71#else // TARGET_BUILD
72 return false;
73#endif // TARGET_BUILD
Zonr Chang1e2adce2012-04-12 13:29:43 +080074}
75
76android::String8 RSInfo::GetPath(const FileBase &pFile) {
77 android::String8 result(pFile.getName().c_str());
78 result.append(".info");
79 return result;
80}
81
82#define PRINT_DEPENDENCY(PREFIX, N, X) \
83 ALOGV("\t" PREFIX "Source name: %s, " \
84 "SHA-1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
85 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", \
86 (N), (X)[ 0], (X)[ 1], (X)[ 2], (X)[ 3], (X)[ 4], (X)[ 5], \
87 (X)[ 6], (X)[ 7], (X)[ 8], (X)[ 9], (X)[10], (X)[11], \
88 (X)[12], (X)[13], (X)[14], (X)[15], (X)[16], (X)[17], \
89 (X)[18], (X)[19]);
90
91bool RSInfo::CheckDependency(const RSInfo &pInfo,
92 const char *pInputFilename,
Shih-wei Liao7bcec852012-04-25 04:07:09 -070093 const DependencyTableTy &pDeps) {
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070094 // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc plus
95 // libclcore_neon.bc if NEON is available on the target device.
96#if !defined(ARCH_ARM_HAVE_NEON)
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070097 static const unsigned NumBuiltInDependencies = 3;
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070098#else
99 static const unsigned NumBuiltInDependencies = 4;
100#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +0800101
102 LoadBuiltInSHA1Information();
103
104 if (pInfo.mDependencyTable.size() != (pDeps.size() + NumBuiltInDependencies)) {
105 ALOGD("Number of dependencies recorded mismatch (%lu v.s. %lu) in %s!",
106 static_cast<unsigned long>(pInfo.mDependencyTable.size()),
107 static_cast<unsigned long>(pDeps.size()), pInputFilename);
108 return false;
109 } else {
110 // Built-in dependencies always go first.
111 const std::pair<const char *, const uint8_t *> &cache_libbcc_dep =
112 pInfo.mDependencyTable[0];
113 const std::pair<const char *, const uint8_t *> &cache_libRS_dep =
114 pInfo.mDependencyTable[1];
Shih-wei Liao2665c2f2012-04-25 04:06:52 -0700115 const std::pair<const char *, const uint8_t *> &cache_libclcore_dep =
116 pInfo.mDependencyTable[2];
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700117#if defined(ARCH_ARM_HAVE_NEON)
118 const std::pair<const char *, const uint8_t *> &cache_libclcore_neon_dep =
119 pInfo.mDependencyTable[3];
120#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +0800121
122 // Check libbcc.so.
Zonr Changf2907932012-04-13 11:56:21 +0800123 if (::memcmp(cache_libbcc_dep.second, LibBCCSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800124 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
125 LibBCCPath);
126 PRINT_DEPENDENCY("current - ", LibBCCPath, LibBCCSHA1);
127 PRINT_DEPENDENCY("cache - ", cache_libbcc_dep.first,
128 cache_libbcc_dep.second);
129 return false;
130 }
131
132 // Check libRS.so.
Zonr Changf2907932012-04-13 11:56:21 +0800133 if (::memcmp(cache_libRS_dep.second, LibRSSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800134 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
135 LibRSPath);
136 PRINT_DEPENDENCY("current - ", LibRSPath, LibRSSHA1);
137 PRINT_DEPENDENCY("cache - ", cache_libRS_dep.first,
138 cache_libRS_dep.second);
139 return false;
140 }
141
Shih-wei Liao2665c2f2012-04-25 04:06:52 -0700142 // Check libclcore.bc.
143 if (::memcmp(cache_libclcore_dep.second, LibCLCoreSHA1,
144 SHA1_DIGEST_LENGTH) != 0) {
145 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
146 LibRSPath);
147 PRINT_DEPENDENCY("current - ", LibCLCorePath, LibCLCoreSHA1);
148 PRINT_DEPENDENCY("cache - ", cache_libclcore_dep.first,
149 cache_libclcore_dep.second);
150 return false;
151 }
152
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700153#if defined(ARCH_ARM_HAVE_NEON)
154 // Check libclcore_neon.bc if NEON is available.
155 if (::memcmp(cache_libclcore_neon_dep.second, LibCLCoreNEONSHA1,
156 SHA1_DIGEST_LENGTH) != 0) {
157 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
158 LibRSPath);
159 PRINT_DEPENDENCY("current - ", LibCLCoreNEONPath, LibCLCoreNEONSHA1);
160 PRINT_DEPENDENCY("cache - ", cache_libclcore_neon_dep.first,
161 cache_libclcore_neon_dep.second);
162 return false;
163 }
164#endif
165
Zonr Chang1e2adce2012-04-12 13:29:43 +0800166 for (unsigned i = 0; i < pDeps.size(); i++) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800167 const std::pair<const char *, const uint8_t *> &cache_dep =
168 pInfo.mDependencyTable[i + NumBuiltInDependencies];
169
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700170 if ((::strcmp(pDeps[i].first, cache_dep.first) != 0) ||
171 (::memcmp(pDeps[i].second, cache_dep.second,
Zonr Changf2907932012-04-13 11:56:21 +0800172 SHA1_DIGEST_LENGTH) != 0)) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800173 ALOGD("Cache %s is dirty due to the source it dependends on has been "
174 "changed:", pInputFilename);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700175 PRINT_DEPENDENCY("given - ", pDeps[i].first, pDeps[i].second);
Zonr Chang1e2adce2012-04-12 13:29:43 +0800176 PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
177 return false;
178 }
179 }
180 }
181
182 return true;
183}
184
185RSInfo::RSInfo(size_t pStringPoolSize) : mStringPool(NULL) {
186 ::memset(&mHeader, 0, sizeof(mHeader));
187
188 ::memcpy(mHeader.magic, RSINFO_MAGIC, sizeof(mHeader.magic));
189 ::memcpy(mHeader.version, RSINFO_VERSION, sizeof(mHeader.version));
190
191 mHeader.headerSize = sizeof(mHeader);
192
193 mHeader.dependencyTable.itemSize = sizeof(rsinfo::DependencyTableItem);
194 mHeader.pragmaList.itemSize = sizeof(rsinfo::PragmaItem);
195 mHeader.objectSlotList.itemSize = sizeof(rsinfo::ObjectSlotItem);
196 mHeader.exportVarNameList.itemSize = sizeof(rsinfo::ExportVarNameItem);
197 mHeader.exportFuncNameList.itemSize = sizeof(rsinfo::ExportFuncNameItem);
198 mHeader.exportForeachFuncList.itemSize = sizeof(rsinfo::ExportForeachFuncItem);
199
200 if (pStringPoolSize > 0) {
201 mHeader.strPoolSize = pStringPoolSize;
202 mStringPool = new (std::nothrow) char [ mHeader.strPoolSize ];
203 if (mStringPool == NULL) {
204 ALOGE("Out of memory when allocate memory for string pool in RSInfo "
205 "constructor (size: %u)!", mHeader.strPoolSize);
206 }
207 }
208}
209
210RSInfo::~RSInfo() {
211 delete [] mStringPool;
212}
213
214bool RSInfo::layout(off_t initial_offset) {
215 mHeader.dependencyTable.offset = initial_offset +
216 mHeader.headerSize +
217 mHeader.strPoolSize;
218 mHeader.dependencyTable.count = mDependencyTable.size();
219
220#define AFTER(_list) ((_list).offset + (_list).itemSize * (_list).count)
221 mHeader.pragmaList.offset = AFTER(mHeader.dependencyTable);
222 mHeader.pragmaList.count = mPragmas.size();
223
224 mHeader.objectSlotList.offset = AFTER(mHeader.pragmaList);
225 mHeader.objectSlotList.count = mObjectSlots.size();
226
227 mHeader.exportVarNameList.offset = AFTER(mHeader.objectSlotList);
228 mHeader.exportVarNameList.count = mExportVarNames.size();
229
230 mHeader.exportFuncNameList.offset = AFTER(mHeader.exportVarNameList);
231 mHeader.exportFuncNameList.count = mExportFuncNames.size();
232
233 mHeader.exportForeachFuncList.offset = AFTER(mHeader.exportFuncNameList);
234 mHeader.exportForeachFuncList.count = mExportForeachFuncs.size();
235#undef AFTER
236
237 return true;
238}
239
240void RSInfo::dump() const {
241 // Hide the codes to save the code size when debugging is disabled.
242#if !LOG_NDEBUG
243
244 // Dump header
245 ALOGV("RSInfo Header:");
246 ALOGV("\tIs threadable: %s", ((mHeader.isThreadable) ? "true" : "false"));
247 ALOGV("\tHeader size: %u", mHeader.headerSize);
248 ALOGV("\tString pool size: %u", mHeader.strPoolSize);
249
250#define DUMP_LIST_HEADER(_name, _header) do { \
251 ALOGV(_name ":"); \
252 ALOGV("\toffset: %u", (_header).offset); \
253 ALOGV("\t# of item: %u", (_header).count); \
254 ALOGV("\tsize of each item: %u", (_header).itemSize); \
255} while (false)
256 DUMP_LIST_HEADER("Dependency table", mHeader.dependencyTable);
257 for (DependencyTableTy::const_iterator dep_iter = mDependencyTable.begin(),
258 dep_end = mDependencyTable.end(); dep_iter != dep_end; dep_iter++) {
259 PRINT_DEPENDENCY("", dep_iter->first, dep_iter->second);
260 }
261
262 DUMP_LIST_HEADER("Pragma list", mHeader.pragmaList);
263 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
264 pragma_end = mPragmas.end(); pragma_iter != pragma_end; pragma_iter++) {
265 ALOGV("\tkey: %s, value: %s", pragma_iter->first, pragma_iter->second);
266 }
267
268 DUMP_LIST_HEADER("RS object slots", mHeader.objectSlotList);
269 for (ObjectSlotListTy::const_iterator slot_iter = mObjectSlots.begin(),
270 slot_end = mObjectSlots.end(); slot_iter != slot_end; slot_iter++) {
271 ALOGV("slot: %u", *slot_iter);
272 }
273
274 DUMP_LIST_HEADER("RS export variables", mHeader.exportVarNameList);
275 for (ExportVarNameListTy::const_iterator var_iter = mExportVarNames.begin(),
276 var_end = mExportVarNames.end(); var_iter != var_end; var_iter++) {
277 ALOGV("name: %s", *var_iter);
278 }
279
280 DUMP_LIST_HEADER("RS export functions", mHeader.exportFuncNameList);
281 for (ExportFuncNameListTy::const_iterator func_iter = mExportFuncNames.begin(),
282 func_end = mExportFuncNames.end(); func_iter != func_end; func_iter++) {
283 ALOGV("name: %s", *func_iter);
284 }
285
286 DUMP_LIST_HEADER("RS foreach list", mHeader.exportForeachFuncList);
287 for (ExportForeachFuncListTy::const_iterator
288 foreach_iter = mExportForeachFuncs.begin(),
289 foreach_end = mExportForeachFuncs.end(); foreach_iter != foreach_end;
290 foreach_iter++) {
291 ALOGV("name: %s, signature: %05x", foreach_iter->first,
292 foreach_iter->second);
293 }
294#undef DUMP_LIST_HEADER
295
296#endif // LOG_NDEBUG
297 return;
298}
299
300const char *RSInfo::getStringFromPool(rsinfo::StringIndexTy pStrIdx) const {
301 // String pool uses direct indexing. Ensure that the pStrIdx is within the
302 // range.
303 if (pStrIdx >= mHeader.strPoolSize) {
304 ALOGE("String index #%u is out of range in string pool (size: %u)!",
305 pStrIdx, mHeader.strPoolSize);
306 return NULL;
307 }
308 return &mStringPool[ pStrIdx ];
309}
310
311rsinfo::StringIndexTy RSInfo::getStringIdxInPool(const char *pStr) const {
312 // Assume we are on the flat memory architecture (i.e., the memory space is
313 // continuous.)
314 if ((mStringPool + mHeader.strPoolSize) < pStr) {
315 ALOGE("String %s does not in the string pool!", pStr);
316 return rsinfo::gInvalidStringIndex;
317 }
318 return (pStr - mStringPool);
319}
320
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700321RSInfo::FloatPrecision RSInfo::getFloatPrecisionRequirement() const {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800322 // Check to see if we have any FP precision-related pragmas.
Stephen Hines688e4c02012-12-12 18:04:18 -0800323 std::string relaxed_pragma("rs_fp_relaxed");
324 std::string imprecise_pragma("rs_fp_imprecise");
325 std::string full_pragma("rs_fp_full");
Zonr Chang1e2adce2012-04-12 13:29:43 +0800326 bool relaxed_pragma_seen = false;
Stephen Hines688e4c02012-12-12 18:04:18 -0800327 bool imprecise_pragma_seen = false;
328 RSInfo::FloatPrecision result = FP_Full;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800329
330 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
331 pragma_end = mPragmas.end(); pragma_iter != pragma_end;
332 pragma_iter++) {
333 const char *pragma_key = pragma_iter->first;
Stephen Hines688e4c02012-12-12 18:04:18 -0800334 if (!relaxed_pragma.compare(pragma_key)) {
335 if (relaxed_pragma_seen || imprecise_pragma_seen) {
336 ALOGE("Multiple float precision pragmas specified!");
Zonr Chang1e2adce2012-04-12 13:29:43 +0800337 }
Stephen Hines688e4c02012-12-12 18:04:18 -0800338 relaxed_pragma_seen = true;
339 } else if (!imprecise_pragma.compare(pragma_key)) {
340 if (relaxed_pragma_seen || imprecise_pragma_seen) {
341 ALOGE("Multiple float precision pragmas specified!");
342 }
343 imprecise_pragma_seen = true;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800344 }
345 }
346
347 // Imprecise is selected over Relaxed precision.
348 // In the absence of both, we stick to the default Full precision.
Stephen Hines688e4c02012-12-12 18:04:18 -0800349 if (imprecise_pragma_seen) {
350 result = FP_Imprecise;
351 } else if (relaxed_pragma_seen) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700352 result = FP_Relaxed;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800353 }
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700354
355 // Provide an override for precsion via adb shell setprop
356 // adb shell setprop debug.rs.precision rs_fp_full
357 // adb shell setprop debug.rs.precision rs_fp_relaxed
358 // adb shell setprop debug.rs.precision rs_fp_imprecise
359 char precision_prop_buf[PROPERTY_VALUE_MAX];
360 property_get("debug.rs.precision", precision_prop_buf, "");
361
362 if (precision_prop_buf[0]) {
Stephen Hines688e4c02012-12-12 18:04:18 -0800363 if (!relaxed_pragma.compare(precision_prop_buf)) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700364 ALOGI("Switching to RS FP relaxed mode via setprop");
365 result = FP_Relaxed;
Stephen Hines688e4c02012-12-12 18:04:18 -0800366 } else if (!imprecise_pragma.compare(precision_prop_buf)) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700367 ALOGI("Switching to RS FP imprecise mode via setprop");
368 result = FP_Imprecise;
Stephen Hines688e4c02012-12-12 18:04:18 -0800369 } else if (!full_pragma.compare(precision_prop_buf)) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700370 ALOGI("Switching to RS FP full mode via setprop");
371 result = FP_Full;
372 }
373 }
374
375 return result;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800376}