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