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