blob: 02f6f6a7e057876a27d2d7073c5b209144092ca9 [file] [log] [blame]
Zonr Chang255cbc82012-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 Chang80232dd2012-04-12 15:38:53 +080018#include "bcc/RenderScript/RSInfo.h"
Zonr Chang255cbc82012-04-12 13:29:43 +080019
Zonr Chang6378d8f2012-04-13 11:56:21 +080020#include <dlfcn.h>
21
Zonr Chang255cbc82012-04-12 13:29:43 +080022#include <cstring>
23#include <new>
24
Zonr Chang80232dd2012-04-12 15:38:53 +080025#include "bcc/Support/FileBase.h"
Zonr Changb519fe32012-04-12 16:44:01 +080026#include "bcc/Support/Log.h"
Zonr Chang255cbc82012-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 Liao0467d9a2012-04-25 04:06:52 -070032const char RSInfo::LibCLCorePath[] = "/system/lib/libclcore.bc";
33
Zonr Chang6378d8f2012-04-13 11:56:21 +080034const uint8_t *RSInfo::LibBCCSHA1 = NULL;
35const uint8_t *RSInfo::LibRSSHA1 = NULL;
Shih-wei Liao0467d9a2012-04-25 04:06:52 -070036const uint8_t *RSInfo::LibCLCoreSHA1 = NULL;
Zonr Chang255cbc82012-04-12 13:29:43 +080037
38void RSInfo::LoadBuiltInSHA1Information() {
Zonr Chang6378d8f2012-04-13 11:56:21 +080039 if (LibBCCSHA1 != NULL) {
40 // Loaded before.
Zonr Chang255cbc82012-04-12 13:29:43 +080041 return;
42 }
43
Zonr Chang6378d8f2012-04-13 11:56:21 +080044 void *h = ::dlopen("/system/lib/libbcc.sha1.so", RTLD_LAZY | RTLD_NOW);
45 if (h == NULL) {
46 ALOGE("Failed to load SHA-1 information from shared library '"
47 "/system/lib/libbcc.sha1.so'! (%s)", ::dlerror());
48 return;
49 }
Zonr Chang255cbc82012-04-12 13:29:43 +080050
Zonr Chang6378d8f2012-04-13 11:56:21 +080051 LibBCCSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libbcc_so_SHA1"));
52 LibRSSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libRS_so_SHA1"));
Shih-wei Liao0467d9a2012-04-25 04:06:52 -070053 LibCLCoreSHA1 =
54 reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_bc_SHA1"));
Zonr Chang255cbc82012-04-12 13:29:43 +080055
56 return;
57}
58
59android::String8 RSInfo::GetPath(const FileBase &pFile) {
60 android::String8 result(pFile.getName().c_str());
61 result.append(".info");
62 return result;
63}
64
65#define PRINT_DEPENDENCY(PREFIX, N, X) \
66 ALOGV("\t" PREFIX "Source name: %s, " \
67 "SHA-1: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
68 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", \
69 (N), (X)[ 0], (X)[ 1], (X)[ 2], (X)[ 3], (X)[ 4], (X)[ 5], \
70 (X)[ 6], (X)[ 7], (X)[ 8], (X)[ 9], (X)[10], (X)[11], \
71 (X)[12], (X)[13], (X)[14], (X)[15], (X)[16], (X)[17], \
72 (X)[18], (X)[19]);
73
74bool RSInfo::CheckDependency(const RSInfo &pInfo,
75 const char *pInputFilename,
Shih-wei Liao170d4202012-04-25 04:07:09 -070076 const DependencyTableTy &pDeps) {
Shih-wei Liao0467d9a2012-04-25 04:06:52 -070077 // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc.
78 static const unsigned NumBuiltInDependencies = 3;
Zonr Chang255cbc82012-04-12 13:29:43 +080079
80 LoadBuiltInSHA1Information();
81
82 if (pInfo.mDependencyTable.size() != (pDeps.size() + NumBuiltInDependencies)) {
83 ALOGD("Number of dependencies recorded mismatch (%lu v.s. %lu) in %s!",
84 static_cast<unsigned long>(pInfo.mDependencyTable.size()),
85 static_cast<unsigned long>(pDeps.size()), pInputFilename);
86 return false;
87 } else {
88 // Built-in dependencies always go first.
89 const std::pair<const char *, const uint8_t *> &cache_libbcc_dep =
90 pInfo.mDependencyTable[0];
91 const std::pair<const char *, const uint8_t *> &cache_libRS_dep =
92 pInfo.mDependencyTable[1];
Shih-wei Liao0467d9a2012-04-25 04:06:52 -070093 const std::pair<const char *, const uint8_t *> &cache_libclcore_dep =
94 pInfo.mDependencyTable[2];
Zonr Chang255cbc82012-04-12 13:29:43 +080095
96 // Check libbcc.so.
Zonr Chang6378d8f2012-04-13 11:56:21 +080097 if (::memcmp(cache_libbcc_dep.second, LibBCCSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang255cbc82012-04-12 13:29:43 +080098 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
99 LibBCCPath);
100 PRINT_DEPENDENCY("current - ", LibBCCPath, LibBCCSHA1);
101 PRINT_DEPENDENCY("cache - ", cache_libbcc_dep.first,
102 cache_libbcc_dep.second);
103 return false;
104 }
105
106 // Check libRS.so.
Zonr Chang6378d8f2012-04-13 11:56:21 +0800107 if (::memcmp(cache_libRS_dep.second, LibRSSHA1, SHA1_DIGEST_LENGTH) != 0) {
Zonr Chang255cbc82012-04-12 13:29:43 +0800108 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
109 LibRSPath);
110 PRINT_DEPENDENCY("current - ", LibRSPath, LibRSSHA1);
111 PRINT_DEPENDENCY("cache - ", cache_libRS_dep.first,
112 cache_libRS_dep.second);
113 return false;
114 }
115
Shih-wei Liao0467d9a2012-04-25 04:06:52 -0700116 // Check libclcore.bc.
117 if (::memcmp(cache_libclcore_dep.second, LibCLCoreSHA1,
118 SHA1_DIGEST_LENGTH) != 0) {
119 ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
120 LibRSPath);
121 PRINT_DEPENDENCY("current - ", LibCLCorePath, LibCLCoreSHA1);
122 PRINT_DEPENDENCY("cache - ", cache_libclcore_dep.first,
123 cache_libclcore_dep.second);
124 return false;
125 }
126
Zonr Chang255cbc82012-04-12 13:29:43 +0800127 for (unsigned i = 0; i < pDeps.size(); i++) {
Zonr Chang255cbc82012-04-12 13:29:43 +0800128 const std::pair<const char *, const uint8_t *> &cache_dep =
129 pInfo.mDependencyTable[i + NumBuiltInDependencies];
130
Shih-wei Liao170d4202012-04-25 04:07:09 -0700131 if ((::strcmp(pDeps[i].first, cache_dep.first) != 0) ||
132 (::memcmp(pDeps[i].second, cache_dep.second,
Zonr Chang6378d8f2012-04-13 11:56:21 +0800133 SHA1_DIGEST_LENGTH) != 0)) {
Zonr Chang255cbc82012-04-12 13:29:43 +0800134 ALOGD("Cache %s is dirty due to the source it dependends on has been "
135 "changed:", pInputFilename);
Shih-wei Liao170d4202012-04-25 04:07:09 -0700136 PRINT_DEPENDENCY("given - ", pDeps[i].first, pDeps[i].second);
Zonr Chang255cbc82012-04-12 13:29:43 +0800137 PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
138 return false;
139 }
140 }
141 }
142
143 return true;
144}
145
146RSInfo::RSInfo(size_t pStringPoolSize) : mStringPool(NULL) {
147 ::memset(&mHeader, 0, sizeof(mHeader));
148
149 ::memcpy(mHeader.magic, RSINFO_MAGIC, sizeof(mHeader.magic));
150 ::memcpy(mHeader.version, RSINFO_VERSION, sizeof(mHeader.version));
151
152 mHeader.headerSize = sizeof(mHeader);
153
154 mHeader.dependencyTable.itemSize = sizeof(rsinfo::DependencyTableItem);
155 mHeader.pragmaList.itemSize = sizeof(rsinfo::PragmaItem);
156 mHeader.objectSlotList.itemSize = sizeof(rsinfo::ObjectSlotItem);
157 mHeader.exportVarNameList.itemSize = sizeof(rsinfo::ExportVarNameItem);
158 mHeader.exportFuncNameList.itemSize = sizeof(rsinfo::ExportFuncNameItem);
159 mHeader.exportForeachFuncList.itemSize = sizeof(rsinfo::ExportForeachFuncItem);
160
161 if (pStringPoolSize > 0) {
162 mHeader.strPoolSize = pStringPoolSize;
163 mStringPool = new (std::nothrow) char [ mHeader.strPoolSize ];
164 if (mStringPool == NULL) {
165 ALOGE("Out of memory when allocate memory for string pool in RSInfo "
166 "constructor (size: %u)!", mHeader.strPoolSize);
167 }
168 }
169}
170
171RSInfo::~RSInfo() {
172 delete [] mStringPool;
173}
174
175bool RSInfo::layout(off_t initial_offset) {
176 mHeader.dependencyTable.offset = initial_offset +
177 mHeader.headerSize +
178 mHeader.strPoolSize;
179 mHeader.dependencyTable.count = mDependencyTable.size();
180
181#define AFTER(_list) ((_list).offset + (_list).itemSize * (_list).count)
182 mHeader.pragmaList.offset = AFTER(mHeader.dependencyTable);
183 mHeader.pragmaList.count = mPragmas.size();
184
185 mHeader.objectSlotList.offset = AFTER(mHeader.pragmaList);
186 mHeader.objectSlotList.count = mObjectSlots.size();
187
188 mHeader.exportVarNameList.offset = AFTER(mHeader.objectSlotList);
189 mHeader.exportVarNameList.count = mExportVarNames.size();
190
191 mHeader.exportFuncNameList.offset = AFTER(mHeader.exportVarNameList);
192 mHeader.exportFuncNameList.count = mExportFuncNames.size();
193
194 mHeader.exportForeachFuncList.offset = AFTER(mHeader.exportFuncNameList);
195 mHeader.exportForeachFuncList.count = mExportForeachFuncs.size();
196#undef AFTER
197
198 return true;
199}
200
201void RSInfo::dump() const {
202 // Hide the codes to save the code size when debugging is disabled.
203#if !LOG_NDEBUG
204
205 // Dump header
206 ALOGV("RSInfo Header:");
207 ALOGV("\tIs threadable: %s", ((mHeader.isThreadable) ? "true" : "false"));
208 ALOGV("\tHeader size: %u", mHeader.headerSize);
209 ALOGV("\tString pool size: %u", mHeader.strPoolSize);
210
211#define DUMP_LIST_HEADER(_name, _header) do { \
212 ALOGV(_name ":"); \
213 ALOGV("\toffset: %u", (_header).offset); \
214 ALOGV("\t# of item: %u", (_header).count); \
215 ALOGV("\tsize of each item: %u", (_header).itemSize); \
216} while (false)
217 DUMP_LIST_HEADER("Dependency table", mHeader.dependencyTable);
218 for (DependencyTableTy::const_iterator dep_iter = mDependencyTable.begin(),
219 dep_end = mDependencyTable.end(); dep_iter != dep_end; dep_iter++) {
220 PRINT_DEPENDENCY("", dep_iter->first, dep_iter->second);
221 }
222
223 DUMP_LIST_HEADER("Pragma list", mHeader.pragmaList);
224 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
225 pragma_end = mPragmas.end(); pragma_iter != pragma_end; pragma_iter++) {
226 ALOGV("\tkey: %s, value: %s", pragma_iter->first, pragma_iter->second);
227 }
228
229 DUMP_LIST_HEADER("RS object slots", mHeader.objectSlotList);
230 for (ObjectSlotListTy::const_iterator slot_iter = mObjectSlots.begin(),
231 slot_end = mObjectSlots.end(); slot_iter != slot_end; slot_iter++) {
232 ALOGV("slot: %u", *slot_iter);
233 }
234
235 DUMP_LIST_HEADER("RS export variables", mHeader.exportVarNameList);
236 for (ExportVarNameListTy::const_iterator var_iter = mExportVarNames.begin(),
237 var_end = mExportVarNames.end(); var_iter != var_end; var_iter++) {
238 ALOGV("name: %s", *var_iter);
239 }
240
241 DUMP_LIST_HEADER("RS export functions", mHeader.exportFuncNameList);
242 for (ExportFuncNameListTy::const_iterator func_iter = mExportFuncNames.begin(),
243 func_end = mExportFuncNames.end(); func_iter != func_end; func_iter++) {
244 ALOGV("name: %s", *func_iter);
245 }
246
247 DUMP_LIST_HEADER("RS foreach list", mHeader.exportForeachFuncList);
248 for (ExportForeachFuncListTy::const_iterator
249 foreach_iter = mExportForeachFuncs.begin(),
250 foreach_end = mExportForeachFuncs.end(); foreach_iter != foreach_end;
251 foreach_iter++) {
252 ALOGV("name: %s, signature: %05x", foreach_iter->first,
253 foreach_iter->second);
254 }
255#undef DUMP_LIST_HEADER
256
257#endif // LOG_NDEBUG
258 return;
259}
260
261const char *RSInfo::getStringFromPool(rsinfo::StringIndexTy pStrIdx) const {
262 // String pool uses direct indexing. Ensure that the pStrIdx is within the
263 // range.
264 if (pStrIdx >= mHeader.strPoolSize) {
265 ALOGE("String index #%u is out of range in string pool (size: %u)!",
266 pStrIdx, mHeader.strPoolSize);
267 return NULL;
268 }
269 return &mStringPool[ pStrIdx ];
270}
271
272rsinfo::StringIndexTy RSInfo::getStringIdxInPool(const char *pStr) const {
273 // Assume we are on the flat memory architecture (i.e., the memory space is
274 // continuous.)
275 if ((mStringPool + mHeader.strPoolSize) < pStr) {
276 ALOGE("String %s does not in the string pool!", pStr);
277 return rsinfo::gInvalidStringIndex;
278 }
279 return (pStr - mStringPool);
280}
281
282enum RSInfo::FloatPrecision RSInfo::getFloatPrecisionRequirement() const {
283 // Check to see if we have any FP precision-related pragmas.
284 static const char relaxed_pragma[] = "rs_fp_relaxed";
285 static const char imprecise_pragma[] = "rs_fp_imprecise";
286 bool relaxed_pragma_seen = false;
287
288 for (PragmaListTy::const_iterator pragma_iter = mPragmas.begin(),
289 pragma_end = mPragmas.end(); pragma_iter != pragma_end;
290 pragma_iter++) {
291 const char *pragma_key = pragma_iter->first;
292 if (::strcmp(pragma_key, relaxed_pragma) == 0) {
293 relaxed_pragma_seen = true;
294 } else if (::strcmp(pragma_key, imprecise_pragma) == 0) {
295 if (relaxed_pragma_seen) {
296 ALOGW("Multiple float precision pragmas specified!");
297 }
298 // Fast return when there's rs_fp_imprecise specified.
299 return Imprecise;
300 }
301 }
302
303 // Imprecise is selected over Relaxed precision.
304 // In the absence of both, we stick to the default Full precision.
305 if (relaxed_pragma_seen) {
306 return Relaxed;
307 } else {
308 return Full;
309 }
310 // unreachable
311}