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