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