blob: 9d924ca14ab43b13c970963bba8fbbbf36025179 [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
Stephen Hines48cd7452013-07-30 22:33:44 -070020#if !defined(_WIN32) /* TODO create a HAVE_DLFCN_H */
Zonr Changf2907932012-04-13 11:56:21 +080021#include <dlfcn.h>
Stephen Hines48cd7452013-07-30 22:33:44 -070022#endif
Zonr Changf2907932012-04-13 11:56:21 +080023
Zonr Chang1e2adce2012-04-12 13:29:43 +080024#include <cstring>
25#include <new>
Stephen Hines688e4c02012-12-12 18:04:18 -080026#include <string>
Zonr Chang1e2adce2012-04-12 13:29:43 +080027
Zonr Changc72c4dd2012-04-12 15:38:53 +080028#include "bcc/Support/FileBase.h"
Zonr Changef73a242012-04-12 16:44:01 +080029#include "bcc/Support/Log.h"
Zonr Chang1e2adce2012-04-12 13:29:43 +080030
Nick Kralevichb81d6972013-05-21 16:52:35 -070031#ifdef HAVE_ANDROID_OS
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -070032#include <cutils/properties.h>
Nick Kralevichb81d6972013-05-21 16:52:35 -070033#endif
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -070034
Zonr Chang1e2adce2012-04-12 13:29:43 +080035using namespace bcc;
36
WeiTang36e642c2014-04-04 23:36:34 +080037#ifdef __LP64__
38#define SYSLIBPATH "/system/lib64"
39#else
40#define SYSLIBPATH "/system/lib"
41#endif
42
43const char RSInfo::LibBCCPath[] = SYSLIBPATH"/libbcc.so";
44const char RSInfo::LibCompilerRTPath[] = SYSLIBPATH"/libcompiler_rt.so";
45const char RSInfo::LibRSPath[] = SYSLIBPATH"/libRS.so";
46const char RSInfo::LibCLCorePath[] = SYSLIBPATH"/libclcore.bc";
47const char RSInfo::LibCLCoreDebugPath[] = SYSLIBPATH"/libclcore_debug.bc";
48#if defined(__i386__) || defined(__x86_64__)
49const char RSInfo::LibCLCoreX86Path[] = SYSLIBPATH"/libclcore_x86.bc";
Michael Liaocdcce322012-09-25 21:59:39 -070050#endif
Stephen Hines1c4d30c2014-05-09 19:33:07 -070051#if defined(ARCH_ARM_HAVE_NEON)
WeiTang36e642c2014-04-04 23:36:34 +080052const char RSInfo::LibCLCoreNEONPath[] = SYSLIBPATH"/libclcore_neon.bc";
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070053#endif
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070054
Zonr Changf2907932012-04-13 11:56:21 +080055const uint8_t *RSInfo::LibBCCSHA1 = NULL;
Shih-wei Liaof7401ab2013-01-25 16:29:44 -080056const uint8_t *RSInfo::LibCompilerRTSHA1 = NULL;
Zonr Changf2907932012-04-13 11:56:21 +080057const uint8_t *RSInfo::LibRSSHA1 = NULL;
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070058const uint8_t *RSInfo::LibCLCoreSHA1 = NULL;
Stephen Hines8ee82d42013-04-16 19:57:34 -070059const uint8_t *RSInfo::LibCLCoreDebugSHA1 = NULL;
Stephen Hines1c4d30c2014-05-09 19:33:07 -070060#if defined(ARCH_ARM_HAVE_NEON)
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070061const uint8_t *RSInfo::LibCLCoreNEONSHA1 = NULL;
62#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +080063
Stephen Hines331310e2012-10-26 19:27:55 -070064bool RSInfo::LoadBuiltInSHA1Information() {
65#ifdef TARGET_BUILD
Zonr Changf2907932012-04-13 11:56:21 +080066 if (LibBCCSHA1 != NULL) {
67 // Loaded before.
Stephen Hines331310e2012-10-26 19:27:55 -070068 return true;
Zonr Chang1e2adce2012-04-12 13:29:43 +080069 }
70
WeiTang36e642c2014-04-04 23:36:34 +080071 void *h = ::dlopen(SYSLIBPATH"/libbcc.sha1.so", RTLD_LAZY | RTLD_NOW);
Zonr Changf2907932012-04-13 11:56:21 +080072 if (h == NULL) {
73 ALOGE("Failed to load SHA-1 information from shared library '"
Tim Murrayc2074ca2014-04-08 15:39:08 -070074 "/system/lib64/libbcc.sha1.so'! (%s)", ::dlerror());
Stephen Hines331310e2012-10-26 19:27:55 -070075 return false;
Zonr Changf2907932012-04-13 11:56:21 +080076 }
Zonr Chang1e2adce2012-04-12 13:29:43 +080077
Zonr Changf2907932012-04-13 11:56:21 +080078 LibBCCSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libbcc_so_SHA1"));
Shih-wei Liaof7401ab2013-01-25 16:29:44 -080079 LibCompilerRTSHA1 =
80 reinterpret_cast<const uint8_t *>(::dlsym(h, "libcompiler_rt_so_SHA1"));
Zonr Changf2907932012-04-13 11:56:21 +080081 LibRSSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libRS_so_SHA1"));
Shih-wei Liao2665c2f2012-04-25 04:06:52 -070082 LibCLCoreSHA1 =
83 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_bc_SHA1"));
Stephen Hines8ee82d42013-04-16 19:57:34 -070084 LibCLCoreDebugSHA1 =
85 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_debug_bc_SHA1"));
Stephen Hines1c4d30c2014-05-09 19:33:07 -070086#if defined(ARCH_ARM_HAVE_NEON)
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -070087 LibCLCoreNEONSHA1 =
88 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_neon_bc_SHA1"));
89#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +080090
Stephen Hines331310e2012-10-26 19:27:55 -070091 return true;
92#else // TARGET_BUILD
93 return false;
94#endif // TARGET_BUILD
Zonr Chang1e2adce2012-04-12 13:29:43 +080095}
96
Stephen Hines01f05d42013-05-31 20:51:27 -070097android::String8 RSInfo::GetPath(const char *pFilename) {
98 android::String8 result(pFilename);
Zonr Chang1e2adce2012-04-12 13:29:43 +080099 result.append(".info");
100 return result;
101}
102
103#define PRINT_DEPENDENCY(PREFIX, N, X) \
104 ALOGV("\t" PREFIX "Source name: %s, " \
105 "SHA-1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
106 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", \
107 (N), (X)[ 0], (X)[ 1], (X)[ 2], (X)[ 3], (X)[ 4], (X)[ 5], \
108 (X)[ 6], (X)[ 7], (X)[ 8], (X)[ 9], (X)[10], (X)[11], \
109 (X)[12], (X)[13], (X)[14], (X)[15], (X)[16], (X)[17], \
110 (X)[18], (X)[19]);
111
112bool RSInfo::CheckDependency(const RSInfo &pInfo,
113 const char *pInputFilename,
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700114 const DependencyTableTy &pDeps) {
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700115 // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc plus
116 // libclcore_neon.bc if NEON is available on the target device.
Stephen Hines1c4d30c2014-05-09 19:33:07 -0700117#if !defined(ARCH_ARM_HAVE_NEON)
Shih-wei Liaof7401ab2013-01-25 16:29:44 -0800118 static const unsigned NumBuiltInDependencies = 5;
Stephen Hines8ee82d42013-04-16 19:57:34 -0700119#else
120 static const unsigned NumBuiltInDependencies = 6;
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700121#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +0800122
123 LoadBuiltInSHA1Information();
124
125 if (pInfo.mDependencyTable.size() != (pDeps.size() + NumBuiltInDependencies)) {
126 ALOGD("Number of dependencies recorded mismatch (%lu v.s. %lu) in %s!",
127 static_cast<unsigned long>(pInfo.mDependencyTable.size()),
Stephen Hines1c4d30c2014-05-09 19:33:07 -0700128 static_cast<unsigned long>(pDeps.size() + NumBuiltInDependencies), pInputFilename);
Zonr Chang1e2adce2012-04-12 13:29:43 +0800129 return false;
130 } else {
131 // Built-in dependencies always go first.
132 const std::pair<const char *, const uint8_t *> &cache_libbcc_dep =
133 pInfo.mDependencyTable[0];
Shih-wei Liaof7401ab2013-01-25 16:29:44 -0800134 const std::pair<const char *, const uint8_t *> &cache_libcompiler_rt_dep =
Zonr Chang1e2adce2012-04-12 13:29:43 +0800135 pInfo.mDependencyTable[1];
Shih-wei Liaof7401ab2013-01-25 16:29:44 -0800136 const std::pair<const char *, const uint8_t *> &cache_libRS_dep =
Shih-wei Liao2665c2f2012-04-25 04:06:52 -0700137 pInfo.mDependencyTable[2];
Shih-wei Liaof7401ab2013-01-25 16:29:44 -0800138 const std::pair<const char *, const uint8_t *> &cache_libclcore_dep =
139 pInfo.mDependencyTable[3];
Stephen Hines8ee82d42013-04-16 19:57:34 -0700140 const std::pair<const char *, const uint8_t *> &cache_libclcore_debug_dep =
141 pInfo.mDependencyTable[4];
Stephen Hines1c4d30c2014-05-09 19:33:07 -0700142#if defined(ARCH_ARM_HAVE_NEON)
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700143 const std::pair<const char *, const uint8_t *> &cache_libclcore_neon_dep =
Stephen Hines8ee82d42013-04-16 19:57:34 -0700144 pInfo.mDependencyTable[5];
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700145#endif
Zonr Chang1e2adce2012-04-12 13:29:43 +0800146
147 // Check libbcc.so.
Zonr Changf2907932012-04-13 11:56:21 +0800148 if (::memcmp(cache_libbcc_dep.second, LibBCCSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800149 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
150 LibBCCPath);
151 PRINT_DEPENDENCY("current - ", LibBCCPath, LibBCCSHA1);
152 PRINT_DEPENDENCY("cache - ", cache_libbcc_dep.first,
153 cache_libbcc_dep.second);
154 return false;
155 }
156
Shih-wei Liaof7401ab2013-01-25 16:29:44 -0800157 // Check libcompiler_rt.so.
158 if (::memcmp(cache_libcompiler_rt_dep.second, LibCompilerRTSHA1,
159 SHA1_DIGEST_LENGTH) != 0) {
160 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
161 LibCompilerRTPath);
162 PRINT_DEPENDENCY("current - ", LibCompilerRTPath, LibCompilerRTSHA1);
163 PRINT_DEPENDENCY("cache - ", cache_libcompiler_rt_dep.first,
164 cache_libcompiler_rt_dep.second);
165 return false;
166 }
167
Zonr Chang1e2adce2012-04-12 13:29:43 +0800168 // Check libRS.so.
Zonr Changf2907932012-04-13 11:56:21 +0800169 if (::memcmp(cache_libRS_dep.second, LibRSSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800170 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
171 LibRSPath);
172 PRINT_DEPENDENCY("current - ", LibRSPath, LibRSSHA1);
173 PRINT_DEPENDENCY("cache - ", cache_libRS_dep.first,
174 cache_libRS_dep.second);
175 return false;
176 }
177
Shih-wei Liao2665c2f2012-04-25 04:06:52 -0700178 // Check libclcore.bc.
179 if (::memcmp(cache_libclcore_dep.second, LibCLCoreSHA1,
180 SHA1_DIGEST_LENGTH) != 0) {
181 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
Stephen Hines8ee82d42013-04-16 19:57:34 -0700182 LibCLCorePath);
Shih-wei Liao2665c2f2012-04-25 04:06:52 -0700183 PRINT_DEPENDENCY("current - ", LibCLCorePath, LibCLCoreSHA1);
184 PRINT_DEPENDENCY("cache - ", cache_libclcore_dep.first,
185 cache_libclcore_dep.second);
186 return false;
187 }
188
Stephen Hines8ee82d42013-04-16 19:57:34 -0700189 // Check libclcore_debug.bc.
190 if (::memcmp(cache_libclcore_debug_dep.second, LibCLCoreDebugSHA1,
191 SHA1_DIGEST_LENGTH) != 0) {
192 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
193 LibCLCoreDebugPath);
194 PRINT_DEPENDENCY("current - ", LibCLCoreDebugPath, LibCLCoreDebugSHA1);
195 PRINT_DEPENDENCY("cache - ", cache_libclcore_debug_dep.first,
196 cache_libclcore_debug_dep.second);
197 return false;
198 }
199
Stephen Hines1c4d30c2014-05-09 19:33:07 -0700200#if defined(ARCH_ARM_HAVE_NEON)
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700201 // Check libclcore_neon.bc if NEON is available.
202 if (::memcmp(cache_libclcore_neon_dep.second, LibCLCoreNEONSHA1,
203 SHA1_DIGEST_LENGTH) != 0) {
204 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
Stephen Hines8ee82d42013-04-16 19:57:34 -0700205 LibCLCoreNEONPath);
Shih-wei Liaob1cc74f2012-06-30 11:27:01 -0700206 PRINT_DEPENDENCY("current - ", LibCLCoreNEONPath, LibCLCoreNEONSHA1);
207 PRINT_DEPENDENCY("cache - ", cache_libclcore_neon_dep.first,
208 cache_libclcore_neon_dep.second);
209 return false;
210 }
211#endif
212
Zonr Chang1e2adce2012-04-12 13:29:43 +0800213 for (unsigned i = 0; i < pDeps.size(); i++) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800214 const std::pair<const char *, const uint8_t *> &cache_dep =
215 pInfo.mDependencyTable[i + NumBuiltInDependencies];
216
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700217 if ((::strcmp(pDeps[i].first, cache_dep.first) != 0) ||
218 (::memcmp(pDeps[i].second, cache_dep.second,
Zonr Changf2907932012-04-13 11:56:21 +0800219 SHA1_DIGEST_LENGTH) != 0)) {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800220 ALOGD("Cache %s is dirty due to the source it dependends on has been "
221 "changed:", pInputFilename);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700222 PRINT_DEPENDENCY("given - ", pDeps[i].first, pDeps[i].second);
Zonr Chang1e2adce2012-04-12 13:29:43 +0800223 PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
224 return false;
225 }
226 }
227 }
228
229 return true;
230}
231
232RSInfo::RSInfo(size_t pStringPoolSize) : mStringPool(NULL) {
233 ::memset(&mHeader, 0, sizeof(mHeader));
234
235 ::memcpy(mHeader.magic, RSINFO_MAGIC, sizeof(mHeader.magic));
236 ::memcpy(mHeader.version, RSINFO_VERSION, sizeof(mHeader.version));
237
238 mHeader.headerSize = sizeof(mHeader);
239
240 mHeader.dependencyTable.itemSize = sizeof(rsinfo::DependencyTableItem);
241 mHeader.pragmaList.itemSize = sizeof(rsinfo::PragmaItem);
242 mHeader.objectSlotList.itemSize = sizeof(rsinfo::ObjectSlotItem);
243 mHeader.exportVarNameList.itemSize = sizeof(rsinfo::ExportVarNameItem);
244 mHeader.exportFuncNameList.itemSize = sizeof(rsinfo::ExportFuncNameItem);
245 mHeader.exportForeachFuncList.itemSize = sizeof(rsinfo::ExportForeachFuncItem);
246
247 if (pStringPoolSize > 0) {
248 mHeader.strPoolSize = pStringPoolSize;
249 mStringPool = new (std::nothrow) char [ mHeader.strPoolSize ];
250 if (mStringPool == NULL) {
251 ALOGE("Out of memory when allocate memory for string pool in RSInfo "
252 "constructor (size: %u)!", mHeader.strPoolSize);
253 }
254 }
255}
256
257RSInfo::~RSInfo() {
258 delete [] mStringPool;
259}
260
261bool RSInfo::layout(off_t initial_offset) {
262 mHeader.dependencyTable.offset = initial_offset +
263 mHeader.headerSize +
264 mHeader.strPoolSize;
265 mHeader.dependencyTable.count = mDependencyTable.size();
266
267#define AFTER(_list) ((_list).offset + (_list).itemSize * (_list).count)
268 mHeader.pragmaList.offset = AFTER(mHeader.dependencyTable);
269 mHeader.pragmaList.count = mPragmas.size();
270
271 mHeader.objectSlotList.offset = AFTER(mHeader.pragmaList);
272 mHeader.objectSlotList.count = mObjectSlots.size();
273
274 mHeader.exportVarNameList.offset = AFTER(mHeader.objectSlotList);
275 mHeader.exportVarNameList.count = mExportVarNames.size();
276
277 mHeader.exportFuncNameList.offset = AFTER(mHeader.exportVarNameList);
278 mHeader.exportFuncNameList.count = mExportFuncNames.size();
279
280 mHeader.exportForeachFuncList.offset = AFTER(mHeader.exportFuncNameList);
281 mHeader.exportForeachFuncList.count = mExportForeachFuncs.size();
282#undef AFTER
283
284 return true;
285}
286
287void RSInfo::dump() const {
288 // Hide the codes to save the code size when debugging is disabled.
289#if !LOG_NDEBUG
290
291 // Dump header
292 ALOGV("RSInfo Header:");
293 ALOGV("\tIs threadable: %s", ((mHeader.isThreadable) ? "true" : "false"));
294 ALOGV("\tHeader size: %u", mHeader.headerSize);
295 ALOGV("\tString pool size: %u", mHeader.strPoolSize);
296
297#define DUMP_LIST_HEADER(_name, _header) do { \
298 ALOGV(_name ":"); \
299 ALOGV("\toffset: %u", (_header).offset); \
300 ALOGV("\t# of item: %u", (_header).count); \
301 ALOGV("\tsize of each item: %u", (_header).itemSize); \
302} while (false)
303 DUMP_LIST_HEADER("Dependency table", mHeader.dependencyTable);
304 for (DependencyTableTy::const_iterator dep_iter = mDependencyTable.begin(),
305 dep_end = mDependencyTable.end(); dep_iter != dep_end; dep_iter++) {
306 PRINT_DEPENDENCY("", dep_iter->first, dep_iter->second);
307 }
308
309 DUMP_LIST_HEADER("Pragma list", mHeader.pragmaList);
310 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
311 pragma_end = mPragmas.end(); pragma_iter != pragma_end; pragma_iter++) {
312 ALOGV("\tkey: %s, value: %s", pragma_iter->first, pragma_iter->second);
313 }
314
315 DUMP_LIST_HEADER("RS object slots", mHeader.objectSlotList);
316 for (ObjectSlotListTy::const_iterator slot_iter = mObjectSlots.begin(),
317 slot_end = mObjectSlots.end(); slot_iter != slot_end; slot_iter++) {
318 ALOGV("slot: %u", *slot_iter);
319 }
320
321 DUMP_LIST_HEADER("RS export variables", mHeader.exportVarNameList);
322 for (ExportVarNameListTy::const_iterator var_iter = mExportVarNames.begin(),
323 var_end = mExportVarNames.end(); var_iter != var_end; var_iter++) {
324 ALOGV("name: %s", *var_iter);
325 }
326
327 DUMP_LIST_HEADER("RS export functions", mHeader.exportFuncNameList);
328 for (ExportFuncNameListTy::const_iterator func_iter = mExportFuncNames.begin(),
329 func_end = mExportFuncNames.end(); func_iter != func_end; func_iter++) {
330 ALOGV("name: %s", *func_iter);
331 }
332
333 DUMP_LIST_HEADER("RS foreach list", mHeader.exportForeachFuncList);
334 for (ExportForeachFuncListTy::const_iterator
335 foreach_iter = mExportForeachFuncs.begin(),
336 foreach_end = mExportForeachFuncs.end(); foreach_iter != foreach_end;
337 foreach_iter++) {
338 ALOGV("name: %s, signature: %05x", foreach_iter->first,
339 foreach_iter->second);
340 }
341#undef DUMP_LIST_HEADER
342
343#endif // LOG_NDEBUG
344 return;
345}
346
347const char *RSInfo::getStringFromPool(rsinfo::StringIndexTy pStrIdx) const {
348 // String pool uses direct indexing. Ensure that the pStrIdx is within the
349 // range.
350 if (pStrIdx >= mHeader.strPoolSize) {
351 ALOGE("String index #%u is out of range in string pool (size: %u)!",
352 pStrIdx, mHeader.strPoolSize);
353 return NULL;
354 }
355 return &mStringPool[ pStrIdx ];
356}
357
358rsinfo::StringIndexTy RSInfo::getStringIdxInPool(const char *pStr) const {
359 // Assume we are on the flat memory architecture (i.e., the memory space is
360 // continuous.)
361 if ((mStringPool + mHeader.strPoolSize) < pStr) {
362 ALOGE("String %s does not in the string pool!", pStr);
363 return rsinfo::gInvalidStringIndex;
364 }
365 return (pStr - mStringPool);
366}
367
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700368RSInfo::FloatPrecision RSInfo::getFloatPrecisionRequirement() const {
Zonr Chang1e2adce2012-04-12 13:29:43 +0800369 // Check to see if we have any FP precision-related pragmas.
Stephen Hines688e4c02012-12-12 18:04:18 -0800370 std::string relaxed_pragma("rs_fp_relaxed");
371 std::string imprecise_pragma("rs_fp_imprecise");
372 std::string full_pragma("rs_fp_full");
Zonr Chang1e2adce2012-04-12 13:29:43 +0800373 bool relaxed_pragma_seen = false;
Stephen Hines688e4c02012-12-12 18:04:18 -0800374 bool imprecise_pragma_seen = false;
375 RSInfo::FloatPrecision result = FP_Full;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800376
377 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
378 pragma_end = mPragmas.end(); pragma_iter != pragma_end;
379 pragma_iter++) {
380 const char *pragma_key = pragma_iter->first;
Stephen Hines688e4c02012-12-12 18:04:18 -0800381 if (!relaxed_pragma.compare(pragma_key)) {
382 if (relaxed_pragma_seen || imprecise_pragma_seen) {
383 ALOGE("Multiple float precision pragmas specified!");
Zonr Chang1e2adce2012-04-12 13:29:43 +0800384 }
Stephen Hines688e4c02012-12-12 18:04:18 -0800385 relaxed_pragma_seen = true;
386 } else if (!imprecise_pragma.compare(pragma_key)) {
387 if (relaxed_pragma_seen || imprecise_pragma_seen) {
388 ALOGE("Multiple float precision pragmas specified!");
389 }
390 imprecise_pragma_seen = true;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800391 }
392 }
393
394 // Imprecise is selected over Relaxed precision.
395 // In the absence of both, we stick to the default Full precision.
Stephen Hines688e4c02012-12-12 18:04:18 -0800396 if (imprecise_pragma_seen) {
397 result = FP_Imprecise;
398 } else if (relaxed_pragma_seen) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700399 result = FP_Relaxed;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800400 }
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700401
Nick Kralevichb81d6972013-05-21 16:52:35 -0700402#ifdef HAVE_ANDROID_OS
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700403 // Provide an override for precsion via adb shell setprop
404 // adb shell setprop debug.rs.precision rs_fp_full
405 // adb shell setprop debug.rs.precision rs_fp_relaxed
406 // adb shell setprop debug.rs.precision rs_fp_imprecise
407 char precision_prop_buf[PROPERTY_VALUE_MAX];
408 property_get("debug.rs.precision", precision_prop_buf, "");
409
410 if (precision_prop_buf[0]) {
Stephen Hines688e4c02012-12-12 18:04:18 -0800411 if (!relaxed_pragma.compare(precision_prop_buf)) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700412 ALOGI("Switching to RS FP relaxed mode via setprop");
413 result = FP_Relaxed;
Stephen Hines688e4c02012-12-12 18:04:18 -0800414 } else if (!imprecise_pragma.compare(precision_prop_buf)) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700415 ALOGI("Switching to RS FP imprecise mode via setprop");
416 result = FP_Imprecise;
Stephen Hines688e4c02012-12-12 18:04:18 -0800417 } else if (!full_pragma.compare(precision_prop_buf)) {
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700418 ALOGI("Switching to RS FP full mode via setprop");
419 result = FP_Full;
420 }
421 }
Nick Kralevichb81d6972013-05-21 16:52:35 -0700422#endif
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700423
424 return result;
Zonr Chang1e2adce2012-04-12 13:29:43 +0800425}