blob: fb87dd1a3023a2a66a91616893ab22d127a42128 [file] [log] [blame]
Jason Sams709a0972012-11-15 18:18:04 -08001/*
2 * Copyright (C) 2011-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
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -07007 *
Jason Sams709a0972012-11-15 18:18:04 -08008 * 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
Jason Sams709a0972012-11-15 18:18:04 -080017#include "rsCpuCore.h"
Jason Sams709a0972012-11-15 18:18:04 -080018#include "rsCpuScript.h"
Yang Ni2abfcc62015-02-17 16:05:19 -080019#include "rsCpuExecutable.h"
Jason Sams709a0972012-11-15 18:18:04 -080020
Jason Sams110f1812013-03-14 16:02:18 -070021#ifdef RS_COMPATIBILITY_LIB
Jason Sams110f1812013-03-14 16:02:18 -070022 #include <stdio.h>
Stephen Hinesee48c0b2013-10-30 17:48:30 -070023 #include <sys/stat.h>
Stephen Hinesc2c11cc2013-07-19 01:07:42 -070024 #include <unistd.h>
Jason Sams110f1812013-03-14 16:02:18 -070025#else
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -070026 #include "rsCppUtils.h"
27
Jason Sams110f1812013-03-14 16:02:18 -070028 #include <bcc/BCCContext.h>
Stephen Hines82e0a672014-05-05 15:40:56 -070029 #include <bcc/Config/Config.h>
Jason Sams110f1812013-03-14 16:02:18 -070030 #include <bcc/Renderscript/RSCompilerDriver.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070031 #include <bcinfo/MetadataExtractor.h>
Stephen Hinesba17ae42013-06-05 17:18:04 -070032 #include <cutils/properties.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070033
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -080034 #include <zlib.h>
35 #include <sys/file.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070036 #include <sys/types.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070037 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080038
39 #include <string>
40 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070041#endif
Jason Sams709a0972012-11-15 18:18:04 -080042
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080043#include <set>
44#include <string>
45#include <dlfcn.h>
46#include <stdlib.h>
47#include <string.h>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080048#include <iostream>
49
50#ifdef __LP64__
51#define SYSLIBPATH "/system/lib64"
52#else
53#define SYSLIBPATH "/system/lib"
54#endif
55
Stephen Hinesba17ae42013-06-05 17:18:04 -070056namespace {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080057#ifndef RS_COMPATIBILITY_LIB
58
Stephen Hinesba17ae42013-06-05 17:18:04 -070059static bool is_force_recompile() {
60#ifdef RS_SERVER
61 return false;
62#else
63 char buf[PROPERTY_VALUE_MAX];
64
65 // Re-compile if floating point precision has been overridden.
66 property_get("debug.rs.precision", buf, "");
67 if (buf[0] != '\0') {
68 return true;
69 }
70
71 // Re-compile if debug.rs.forcerecompile is set.
72 property_get("debug.rs.forcerecompile", buf, "0");
73 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
74 return true;
75 } else {
76 return false;
77 }
78#endif // RS_SERVER
79}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070080
Chris Wailes6847e732014-08-11 17:30:51 -070081static void setCompileArguments(std::vector<const char*>* args,
82 const std::string& bcFileName,
83 const char* cacheDir, const char* resName,
84 const char* core_lib, bool useRSDebugContext,
85 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070086 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080087 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080088 args->push_back("-unroll-runtime");
89 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070090 args->push_back("-o");
91 args->push_back(resName);
92 args->push_back("-output_path");
93 args->push_back(cacheDir);
94 args->push_back("-bclib");
95 args->push_back(core_lib);
96 args->push_back("-mtriple");
97 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
98
Tim Murray358ffb82014-12-09 11:53:06 -080099 // Enable workaround for A53 codegen by default.
100#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
101 args->push_back("-aarch64-fix-cortex-a53-835769");
102#endif
103
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700104 // Execute the bcc compiler.
105 if (useRSDebugContext) {
106 args->push_back("-rs-debug-ctx");
107 } else {
108 // Only load additional libraries for compiles that don't use
109 // the debug context.
110 if (bccPluginName && strlen(bccPluginName) > 0) {
111 args->push_back("-load");
112 args->push_back(bccPluginName);
113 }
114 }
115
Stephen Hines45e753a2015-01-19 20:58:44 -0800116 args->push_back("-fPIC");
117 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800118
Chris Wailes6847e732014-08-11 17:30:51 -0700119 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700120 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700121}
122
Chris Wailes6847e732014-08-11 17:30:51 -0700123static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700124 const char *bitcode,
125 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700126 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700127 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700128
Chris Wailes6847e732014-08-11 17:30:51 -0700129 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700130 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700131 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700132 return false;
133 }
134 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
135 fclose(bcfile);
136 if (nwritten != bitcodeSize) {
137 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700138 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700139 return false;
140 }
141
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700142 return android::renderscript::rsuExecuteCommand(
143 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
144 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700145}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700146
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800147bool isChecksumNeeded() {
148 char buf[PROPERTY_VALUE_MAX];
149 property_get("ro.debuggable", buf, "");
150 return (buf[0] == '1');
151}
152
Yang Nif02a2b02015-04-07 16:00:31 -0700153char *constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
154 const char *commandLine,
155 const std::vector<const char *> &bccFiles) {
156 uint32_t checksum = adler32(0L, Z_NULL, 0);
157
158 // include checksum of bitcode
159 checksum = adler32(checksum, bitcode, bitcodeSize);
160
161 // include checksum of command line arguments
162 checksum = adler32(checksum, (const unsigned char *) commandLine,
163 strlen(commandLine));
164
165 // include checksum of bccFiles
166 for (auto bccFile : bccFiles) {
167 if (!android::renderscript::addFileToChecksum(bccFile, checksum)) {
168 // return empty checksum instead of something partial/corrupt
169 return nullptr;
170 }
171 }
172
173 char *checksumStr = new char[9]();
174 sprintf(checksumStr, "%08x", checksum);
175 return checksumStr;
176}
177
178#endif // !defined(RS_COMPATIBILITY_LIB)
179} // namespace
180
181namespace android {
182namespace renderscript {
183
184#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800185bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
186 int FD = open(fileName, O_RDONLY);
187 if (FD == -1) {
188 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
189 return false;
190 }
191
192 char buf[256];
193 while (true) {
194 ssize_t nread = read(FD, buf, sizeof(buf));
195 if (nread < 0) { // bail out on failed read
196 ALOGE("Error while computing checksum for file \'%s\'", fileName);
197 return false;
198 }
199
200 checksum = adler32(checksum, (const unsigned char *) buf, nread);
201 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
202 break;
203 }
204
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700205 if (TEMP_FAILURE_RETRY(close(FD)) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800206 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
207 return false;
208 }
209 return true;
210}
Yang Nif02a2b02015-04-07 16:00:31 -0700211#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800212
Jason Sams709a0972012-11-15 18:18:04 -0800213RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
214 mCtx = ctx;
215 mScript = s;
216
Chris Wailes44bef6f2014-08-12 13:51:10 -0700217 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800218
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800219#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700220 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700221#endif
222
Tim Murraye195a3f2014-03-13 15:04:58 -0700223
Chris Wailes44bef6f2014-08-12 13:51:10 -0700224 mRoot = nullptr;
225 mRootExpand = nullptr;
226 mInit = nullptr;
227 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800228 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800229
Chris Wailes44bef6f2014-08-12 13:51:10 -0700230 mBoundAllocs = nullptr;
231 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800232 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800233
234 mBuildChecksum = nullptr;
235 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800236}
237
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800238bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800239 // The shared object may have an invalid build checksum.
240 // Validate and fail early.
241 mScriptExec = ScriptExecutable::createFromSharedObject(
242 mCtx->getContext(), mScriptSO);
243
244 if (mScriptExec == nullptr) {
245 return false;
246 }
247
248 if (mChecksumNeeded && !mScriptExec->isChecksumValid(mBuildChecksum)) {
249 ALOGE("Found invalid checksum. Expected %s, got %s\n",
250 mBuildChecksum, mScriptExec->getBuildChecksum());
251 delete mScriptExec;
252 mScriptExec = nullptr;
253 return false;
254 }
255
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800256 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
257 if (mRoot) {
258 //ALOGE("Found root(): %p", mRoot);
259 }
260 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
261 if (mRootExpand) {
262 //ALOGE("Found root.expand(): %p", mRootExpand);
263 }
264 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
265 if (mInit) {
266 //ALOGE("Found init(): %p", mInit);
267 }
268 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
269 if (mFreeChildren) {
270 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
271 }
272
Yang Nid9bae682015-01-20 15:31:15 -0800273 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800274 if (varCount > 0) {
275 mBoundAllocs = new Allocation *[varCount];
276 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
277 }
278
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800279 mIsThreadable = mScriptExec->getThreadable();
280 //ALOGE("Script isThreadable? %d", mIsThreadable);
281
Yang Nid9bae682015-01-20 15:31:15 -0800282 return true;
283}
284
Jason Sams709a0972012-11-15 18:18:04 -0800285bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
286 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800287 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800288 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
289 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800290 //ALOGE("rsdScriptInit %p %p", rsc, script);
291
292 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700293#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800294 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800295
Chris Wailes44bef6f2014-08-12 13:51:10 -0700296 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800297
Jason Sams709a0972012-11-15 18:18:04 -0800298 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700299 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800300 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
301 mCtx->unlockMutex();
302 return false;
303 }
304
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700305 // Run any compiler setup functions we have been provided with.
306 RSSetupCompilerCallback setupCompilerCallback =
307 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700308 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700309 setupCompilerCallback(mCompilerDriver);
310 }
311
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700312 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
313 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700314 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700315 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700316 return false;
317 }
318
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700319 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700320
321 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700322 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800323 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700324 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700325
Chris Wailes6847e732014-08-11 17:30:51 -0700326 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700327 bcFileName.append("/");
328 bcFileName.append(resName);
329 bcFileName.append(".bc");
330
331 std::vector<const char*> compileArguments;
332 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
333 useRSDebugContext, bccPluginName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800334
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800335 mChecksumNeeded = isChecksumNeeded();
336 if (mChecksumNeeded) {
337 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
338 core_lib,
339 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700340
341 // The last argument of compileArguments is a nullptr, so remove 1 from
342 // the size.
343 std::unique_ptr<const char> compileCommandLine(
344 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
345
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800346 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
347 compileCommandLine.get(),
348 bccFiles);
349
350 if (mBuildChecksum == nullptr) {
351 // cannot compute checksum but verification is enabled
352 mCtx->unlockMutex();
353 return false;
354 }
355 }
356 else {
357 // add a dummy/constant as a checksum if verification is disabled
358 mBuildChecksum = new char[9]();
359 strcpy(const_cast<char *>(mBuildChecksum), "abadcafe");
360 }
361
362 // Append build checksum to commandline
363 // Handle the terminal nullptr in compileArguments
364 compileArguments.pop_back();
365 compileArguments.push_back("-build-checksum");
366 compileArguments.push_back(mBuildChecksum);
367 compileArguments.push_back(nullptr);
368
Tim Murraybf96a522015-01-23 15:37:03 -0800369 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800370 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800371
372 // Read RS info from the shared object to detect checksum mismatch
373 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
374 dlclose(mScriptSO);
375 mScriptSO = nullptr;
376 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700377 }
378
379 // If we can't, it's either not there or out of date. We compile the bit code and try loading
380 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800381 if (mScriptSO == nullptr) {
382 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700383 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800384 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700385 ALOGE("bcc: FAILS to compile '%s'", resName);
386 mCtx->unlockMutex();
387 return false;
388 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800389
Yang Ni1c44cb62015-01-22 12:02:27 -0800390 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800391 ALOGE("Linker: Failed to link object file '%s'", resName);
392 mCtx->unlockMutex();
393 return false;
394 }
395
Yang Ni1c44cb62015-01-22 12:02:27 -0800396 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800397 if (mScriptSO == nullptr) {
398 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700399 mCtx->unlockMutex();
400 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700401 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800402
403 // Read RS symbol information from the .so.
404 if (!storeRSInfoFromSO()) {
405 goto error;
406 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700407 }
Jason Sams709a0972012-11-15 18:18:04 -0800408
Yang Nic31585b2015-02-15 11:43:50 -0800409 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800410
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700411#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800412 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
413 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700414
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800415 if (!mScriptSO) {
416 goto error;
417 }
Jason Sams110f1812013-03-14 16:02:18 -0700418
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800419 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700420 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700421 }
422#endif
Jason Sams709a0972012-11-15 18:18:04 -0800423 mCtx->unlockMutex();
424 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700425
Jason Sams110f1812013-03-14 16:02:18 -0700426error:
427
428 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700429 if (mScriptSO) {
430 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800431 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700432 }
433 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800434}
435
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700436#ifndef RS_COMPATIBILITY_LIB
437
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700438const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
439 size_t bitcodeSize) {
440 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
441
442 // If we're debugging, use the debug library.
443 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
444 return SYSLIBPATH"/libclcore_debug.bc";
445 }
446
447 // If a callback has been registered to specify a library, use that.
448 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700449 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700450 return selectRTCallback((const char*)bitcode, bitcodeSize);
451 }
452
453 // Check for a platform specific library
454#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
455 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700456 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700457 // NEON-capable ARMv7a devices can use an accelerated math library
458 // for all reduced precision scripts.
459 // ARMv8 does not use NEON, as ASIMD can be used with all precision
460 // levels.
461 return SYSLIBPATH"/libclcore_neon.bc";
462 } else {
463 return defaultLib;
464 }
465#elif defined(__i386__) || defined(__x86_64__)
466 // x86 devices will use an optimized library.
467 return SYSLIBPATH"/libclcore_x86.bc";
468#else
469 return defaultLib;
470#endif
471}
472
473#endif
474
Jason Sams709a0972012-11-15 18:18:04 -0800475void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700476 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800477 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
478 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800479 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800480 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
481 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700482
483 // Bug, need to stash in metadata
484 if (mRootExpand) {
485 script->mHal.info.root = mRootExpand;
486 } else {
487 script->mHal.info.root = mRoot;
488 }
Jason Sams709a0972012-11-15 18:18:04 -0800489}
490
Jason Sams709a0972012-11-15 18:18:04 -0800491
492typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
493
Jason Samsbf2111d2015-01-26 18:13:41 -0800494bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700495 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700496 Allocation * aout,
497 const void * usr, uint32_t usrLen,
498 const RsScriptCall *sc,
499 MTLaunchStruct *mtls) {
500
501 memset(mtls, 0, sizeof(MTLaunchStruct));
502
Chris Wailesf3712132014-07-16 15:18:30 -0700503 for (int index = inLen; --index >= 0;) {
504 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700505
Chris Wailesf3712132014-07-16 15:18:30 -0700506 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700507 if (ain != nullptr &&
508 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
509
Chris Wailesf3712132014-07-16 15:18:30 -0700510 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
511 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800512 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700513 }
514 }
515
Chris Wailes44bef6f2014-08-12 13:51:10 -0700516 if (aout &&
517 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
518
Chris Wailesf3712132014-07-16 15:18:30 -0700519 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
520 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800521 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700522 }
523
Chris Wailesf3712132014-07-16 15:18:30 -0700524 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700525 const Allocation *ain0 = ains[0];
526 const Type *inType = ain0->getType();
527
Jason Samsc0d68472015-01-20 14:29:52 -0800528 mtls->fep.dim.x = inType->getDimX();
529 mtls->fep.dim.y = inType->getDimY();
530 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700531
532 for (int Index = inLen; --Index >= 1;) {
533 if (!ain0->hasSameDims(ains[Index])) {
534 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800535 "Failed to launch kernel; dimensions of input and output"
536 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700537
Jason Samsbf2111d2015-01-26 18:13:41 -0800538 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700539 }
540 }
541
Chris Wailes44bef6f2014-08-12 13:51:10 -0700542 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700543 const Type *outType = aout->getType();
544
Jason Samsc0d68472015-01-20 14:29:52 -0800545 mtls->fep.dim.x = outType->getDimX();
546 mtls->fep.dim.y = outType->getDimY();
547 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700548
549 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700550 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
551 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800552 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700553 }
554
Chris Wailes44bef6f2014-08-12 13:51:10 -0700555 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700556 if (!ains[0]->hasSameDims(aout)) {
557 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
558 "Failed to launch kernel; dimensions of input and output allocations do not match.");
559
Jason Samsbf2111d2015-01-26 18:13:41 -0800560 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700561 }
562 }
563
564 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800565 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700566 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800567 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
568 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
569 if (mtls->start.x >= mtls->end.x) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700570 }
571
572 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800573 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700574 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800575 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
576 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
577 if (mtls->start.y >= mtls->end.y) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700578 }
579
580 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800581 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700582 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800583 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
584 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
585 if (mtls->start.z >= mtls->end.z) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700586 }
587
Jason Samsbf2111d2015-01-26 18:13:41 -0800588 if (!sc || (sc->arrayEnd == 0)) {
589 mtls->end.array[0] = mtls->fep.dim.array[0];
590 } else {
591 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
592 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
593 if (mtls->start.array[0] >= mtls->end.array[0]) return false;
594 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700595
Jason Samsbf2111d2015-01-26 18:13:41 -0800596 if (!sc || (sc->array2End == 0)) {
597 mtls->end.array[1] = mtls->fep.dim.array[1];
598 } else {
599 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
600 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
601 if (mtls->start.array[1] >= mtls->end.array[1]) return false;
602 }
603
604 if (!sc || (sc->array3End == 0)) {
605 mtls->end.array[2] = mtls->fep.dim.array[2];
606 } else {
607 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
608 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
609 if (mtls->start.array[2] >= mtls->end.array[2]) return false;
610 }
611
612 if (!sc || (sc->array4End == 0)) {
613 mtls->end.array[3] = mtls->fep.dim.array[3];
614 } else {
615 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
616 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
617 if (mtls->start.array[3] >= mtls->end.array[3]) return false;
618 }
619
620
621 // The X & Y walkers always want 0-1 min even if dim is not present
622 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
623 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700624
625 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800626 if (ains) {
627 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
628 }
629 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700630 mtls->fep.usr = usr;
631 mtls->fep.usrLen = usrLen;
632 mtls->mSliceSize = 1;
633 mtls->mSliceNum = 0;
634
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700635 mtls->isThreadable = mIsThreadable;
636
Chris Wailesf3712132014-07-16 15:18:30 -0700637 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700638 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700639 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800640 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
641 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700642 }
643 }
644
Chris Wailes44bef6f2014-08-12 13:51:10 -0700645 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800646 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
647 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700648 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800649
650 // All validation passed, ok to launch threads
651 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700652}
653
Jason Sams709a0972012-11-15 18:18:04 -0800654
655void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700656 const Allocation ** ains,
657 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800658 Allocation * aout,
659 const void * usr,
660 uint32_t usrLen,
661 const RsScriptCall *sc) {
662
663 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700664
Jason Samsbf2111d2015-01-26 18:13:41 -0800665 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
666 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700667
Jason Samsbf2111d2015-01-26 18:13:41 -0800668 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
669 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
670 mCtx->setTLS(oldTLS);
671 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700672}
673
Jason Sams709a0972012-11-15 18:18:04 -0800674void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800675 mtls->script = this;
676 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800677 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700678 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800679 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800680}
681
682int RsdCpuScriptImpl::invokeRoot() {
683 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
684 int ret = mRoot();
685 mCtx->setTLS(oldTLS);
686 return ret;
687}
688
689void RsdCpuScriptImpl::invokeInit() {
690 if (mInit) {
691 mInit();
692 }
693}
694
695void RsdCpuScriptImpl::invokeFreeChildren() {
696 if (mFreeChildren) {
697 mFreeChildren();
698 }
699}
700
701void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
702 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800703 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800704 void * ap = nullptr;
705
706#if defined(__x86_64__)
707 // The invoked function could have input parameter of vector type for example float4 which
708 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
709 // So try to align void* params before passing them into RS exported function.
710
711 if ((uint8_t)(uint64_t)params & 0x0F) {
712 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
713 memcpy(ap, params, paramLength);
714 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800715 ALOGE("x86_64: invokeFunction memalign error, still use params which"
716 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800717 }
718 }
719#endif
Jason Sams709a0972012-11-15 18:18:04 -0800720
721 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800722 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800723 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800724
Jason Sams709a0972012-11-15 18:18:04 -0800725 mCtx->setTLS(oldTLS);
726}
727
728void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
729 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800730 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800731
732 //if (mIntrinsicID) {
733 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
734 //return;
735 //}
736
Yang Nid9bae682015-01-20 15:31:15 -0800737 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800738 if (!destPtr) {
739 //ALOGV("Calling setVar on slot = %i which is null", slot);
740 return;
741 }
742
743 memcpy(destPtr, data, dataLength);
744}
745
Tim Murray9c642392013-04-11 13:29:59 -0700746void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
747 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800748 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700749
Yang Nid9bae682015-01-20 15:31:15 -0800750 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700751 if (!srcPtr) {
752 //ALOGV("Calling setVar on slot = %i which is null", slot);
753 return;
754 }
755 memcpy(data, srcPtr, dataLength);
756}
757
758
Jason Sams709a0972012-11-15 18:18:04 -0800759void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
760 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700761 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800762 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800763 if (!destPtr) {
764 //ALOGV("Calling setVar on slot = %i which is null", slot);
765 return;
766 }
767
768 // We want to look at dimension in terms of integer components,
769 // but dimLength is given in terms of bytes.
770 dimLength /= sizeof(int);
771
772 // Only a single dimension is currently supported.
773 rsAssert(dimLength == 1);
774 if (dimLength == 1) {
775 // First do the increment loop.
776 size_t stride = elem->getSizeBytes();
777 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700778 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800779 elem->incRefs(cVal);
780 cVal += stride;
781 }
782
783 // Decrement loop comes after (to prevent race conditions).
784 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700785 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800786 elem->decRefs(oldVal);
787 oldVal += stride;
788 }
789 }
790
791 memcpy(destPtr, data, dataLength);
792}
793
794void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
795
796 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800797 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800798
Yang Nid9bae682015-01-20 15:31:15 -0800799 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800800 if (!destPtr) {
801 //ALOGV("Calling setVar on slot = %i which is null", slot);
802 return;
803 }
804
Chris Wailes44bef6f2014-08-12 13:51:10 -0700805 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800806 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800807 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800808 ptr = data->mHal.drvState.lod[0].mallocPtr;
809 }
810 memcpy(destPtr, &ptr, sizeof(void *));
811}
812
813void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
814
815 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800816 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800817
Yang Nid9bae682015-01-20 15:31:15 -0800818 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800819 if (!destPtr) {
820 //ALOGV("Calling setVar on slot = %i which is null", slot);
821 return;
822 }
823
Jason Sams05ef73f2014-08-05 14:59:22 -0700824 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800825}
826
Yang Ni062c2872015-02-20 15:20:00 -0800827const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
828 return mScriptExec->getFieldName(slot);
829}
830
Jason Sams709a0972012-11-15 18:18:04 -0800831RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -0700832#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -0800833 if (mCompilerDriver) {
834 delete mCompilerDriver;
835 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800836#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800837
Yang Nid9bae682015-01-20 15:31:15 -0800838 if (mScriptExec != nullptr) {
839 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800840 }
Jason Sams110f1812013-03-14 16:02:18 -0700841 if (mBoundAllocs) delete[] mBoundAllocs;
842 if (mScriptSO) {
843 dlclose(mScriptSO);
844 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800845
846 delete[] mBuildChecksum;
Jason Sams709a0972012-11-15 18:18:04 -0800847}
848
849Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
850 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700851 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800852 }
853
854 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
855 Allocation *a = mBoundAllocs[ct];
856 if (!a) continue;
857 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
858 return a;
859 }
860 }
861 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700862 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800863}
864
Chris Wailesf3712132014-07-16 15:18:30 -0700865void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
866 uint32_t inLen, Allocation * aout,
867 const void * usr, uint32_t usrLen,
868 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700869
Chris Wailesf3712132014-07-16 15:18:30 -0700870void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
871 uint32_t inLen, Allocation * aout,
872 const void * usr, uint32_t usrLen,
873 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700874
Jason Sams709a0972012-11-15 18:18:04 -0800875
876}
877}