blob: 6099cf4dbe4e8c30f5d9221b1acf28be3752949b [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
153bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
154 int FD = open(fileName, O_RDONLY);
155 if (FD == -1) {
156 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
157 return false;
158 }
159
160 char buf[256];
161 while (true) {
162 ssize_t nread = read(FD, buf, sizeof(buf));
163 if (nread < 0) { // bail out on failed read
164 ALOGE("Error while computing checksum for file \'%s\'", fileName);
165 return false;
166 }
167
168 checksum = adler32(checksum, (const unsigned char *) buf, nread);
169 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
170 break;
171 }
172
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700173 if (TEMP_FAILURE_RETRY(close(FD)) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800174 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
175 return false;
176 }
177 return true;
178}
179
180char *constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
181 const char *commandLine,
182 const std::vector<const char *> &bccFiles) {
183 uint32_t checksum = adler32(0L, Z_NULL, 0);
184
185 // include checksum of bitcode
186 checksum = adler32(checksum, bitcode, bitcodeSize);
187
188 // include checksum of command line arguments
189 checksum = adler32(checksum, (const unsigned char *) commandLine,
190 strlen(commandLine));
191
192 // include checksum of bccFiles
193 for (auto bccFile : bccFiles) {
194 if (!addFileToChecksum(bccFile, checksum)) {
195 // return empty checksum instead of something partial/corrupt
196 return nullptr;
197 }
198 }
199
200 char *checksumStr = new char[9]();
201 sprintf(checksumStr, "%08x", checksum);
202 return checksumStr;
203}
204
Yang Ni1c44cb62015-01-22 12:02:27 -0800205#endif // !defined(RS_COMPATIBILITY_LIB)
206} // namespace
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800207
Yang Ni1c44cb62015-01-22 12:02:27 -0800208namespace android {
209namespace renderscript {
210
Jason Sams709a0972012-11-15 18:18:04 -0800211RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
212 mCtx = ctx;
213 mScript = s;
214
Chris Wailes44bef6f2014-08-12 13:51:10 -0700215 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800216
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800217#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700218 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700219#endif
220
Tim Murraye195a3f2014-03-13 15:04:58 -0700221
Chris Wailes44bef6f2014-08-12 13:51:10 -0700222 mRoot = nullptr;
223 mRootExpand = nullptr;
224 mInit = nullptr;
225 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800226 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800227
Chris Wailes44bef6f2014-08-12 13:51:10 -0700228 mBoundAllocs = nullptr;
229 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800230 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800231
232 mBuildChecksum = nullptr;
233 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800234}
235
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800236bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800237 // The shared object may have an invalid build checksum.
238 // Validate and fail early.
239 mScriptExec = ScriptExecutable::createFromSharedObject(
240 mCtx->getContext(), mScriptSO);
241
242 if (mScriptExec == nullptr) {
243 return false;
244 }
245
246 if (mChecksumNeeded && !mScriptExec->isChecksumValid(mBuildChecksum)) {
247 ALOGE("Found invalid checksum. Expected %s, got %s\n",
248 mBuildChecksum, mScriptExec->getBuildChecksum());
249 delete mScriptExec;
250 mScriptExec = nullptr;
251 return false;
252 }
253
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800254 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
255 if (mRoot) {
256 //ALOGE("Found root(): %p", mRoot);
257 }
258 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
259 if (mRootExpand) {
260 //ALOGE("Found root.expand(): %p", mRootExpand);
261 }
262 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
263 if (mInit) {
264 //ALOGE("Found init(): %p", mInit);
265 }
266 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
267 if (mFreeChildren) {
268 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
269 }
270
Yang Nid9bae682015-01-20 15:31:15 -0800271 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800272 if (varCount > 0) {
273 mBoundAllocs = new Allocation *[varCount];
274 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
275 }
276
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800277 mIsThreadable = mScriptExec->getThreadable();
278 //ALOGE("Script isThreadable? %d", mIsThreadable);
279
Yang Nid9bae682015-01-20 15:31:15 -0800280 return true;
281}
282
Jason Sams709a0972012-11-15 18:18:04 -0800283bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
284 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800285 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800286 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
287 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800288 //ALOGE("rsdScriptInit %p %p", rsc, script);
289
290 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700291#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800292 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800293
Chris Wailes44bef6f2014-08-12 13:51:10 -0700294 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800295
Jason Sams709a0972012-11-15 18:18:04 -0800296 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700297 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800298 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
299 mCtx->unlockMutex();
300 return false;
301 }
302
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700303 // Run any compiler setup functions we have been provided with.
304 RSSetupCompilerCallback setupCompilerCallback =
305 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700306 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700307 setupCompilerCallback(mCompilerDriver);
308 }
309
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700310 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
311 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700312 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700313 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700314 return false;
315 }
316
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700317 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700318
319 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700320 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800321 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700322 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700323
Chris Wailes6847e732014-08-11 17:30:51 -0700324 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700325 bcFileName.append("/");
326 bcFileName.append(resName);
327 bcFileName.append(".bc");
328
329 std::vector<const char*> compileArguments;
330 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
331 useRSDebugContext, bccPluginName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800332
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800333 mChecksumNeeded = isChecksumNeeded();
334 if (mChecksumNeeded) {
335 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
336 core_lib,
337 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700338
339 // The last argument of compileArguments is a nullptr, so remove 1 from
340 // the size.
341 std::unique_ptr<const char> compileCommandLine(
342 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
343
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800344 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
345 compileCommandLine.get(),
346 bccFiles);
347
348 if (mBuildChecksum == nullptr) {
349 // cannot compute checksum but verification is enabled
350 mCtx->unlockMutex();
351 return false;
352 }
353 }
354 else {
355 // add a dummy/constant as a checksum if verification is disabled
356 mBuildChecksum = new char[9]();
357 strcpy(const_cast<char *>(mBuildChecksum), "abadcafe");
358 }
359
360 // Append build checksum to commandline
361 // Handle the terminal nullptr in compileArguments
362 compileArguments.pop_back();
363 compileArguments.push_back("-build-checksum");
364 compileArguments.push_back(mBuildChecksum);
365 compileArguments.push_back(nullptr);
366
Tim Murraybf96a522015-01-23 15:37:03 -0800367 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800368 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800369
370 // Read RS info from the shared object to detect checksum mismatch
371 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
372 dlclose(mScriptSO);
373 mScriptSO = nullptr;
374 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700375 }
376
377 // If we can't, it's either not there or out of date. We compile the bit code and try loading
378 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800379 if (mScriptSO == nullptr) {
380 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700381 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800382 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700383 ALOGE("bcc: FAILS to compile '%s'", resName);
384 mCtx->unlockMutex();
385 return false;
386 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800387
Yang Ni1c44cb62015-01-22 12:02:27 -0800388 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800389 ALOGE("Linker: Failed to link object file '%s'", resName);
390 mCtx->unlockMutex();
391 return false;
392 }
393
Yang Ni1c44cb62015-01-22 12:02:27 -0800394 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800395 if (mScriptSO == nullptr) {
396 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700397 mCtx->unlockMutex();
398 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700399 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800400
401 // Read RS symbol information from the .so.
402 if (!storeRSInfoFromSO()) {
403 goto error;
404 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700405 }
Jason Sams709a0972012-11-15 18:18:04 -0800406
Yang Nic31585b2015-02-15 11:43:50 -0800407 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800408
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700409#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800410 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
411 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700412
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800413 if (!mScriptSO) {
414 goto error;
415 }
Jason Sams110f1812013-03-14 16:02:18 -0700416
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800417 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700418 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700419 }
420#endif
Jason Sams709a0972012-11-15 18:18:04 -0800421 mCtx->unlockMutex();
422 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700423
Jason Sams110f1812013-03-14 16:02:18 -0700424error:
425
426 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700427 if (mScriptSO) {
428 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800429 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700430 }
431 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800432}
433
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700434#ifndef RS_COMPATIBILITY_LIB
435
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700436const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
437 size_t bitcodeSize) {
438 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
439
440 // If we're debugging, use the debug library.
441 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
442 return SYSLIBPATH"/libclcore_debug.bc";
443 }
444
445 // If a callback has been registered to specify a library, use that.
446 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700447 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700448 return selectRTCallback((const char*)bitcode, bitcodeSize);
449 }
450
451 // Check for a platform specific library
452#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
453 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700454 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700455 // NEON-capable ARMv7a devices can use an accelerated math library
456 // for all reduced precision scripts.
457 // ARMv8 does not use NEON, as ASIMD can be used with all precision
458 // levels.
459 return SYSLIBPATH"/libclcore_neon.bc";
460 } else {
461 return defaultLib;
462 }
463#elif defined(__i386__) || defined(__x86_64__)
464 // x86 devices will use an optimized library.
465 return SYSLIBPATH"/libclcore_x86.bc";
466#else
467 return defaultLib;
468#endif
469}
470
471#endif
472
Jason Sams709a0972012-11-15 18:18:04 -0800473void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700474 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800475 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
476 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800477 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800478 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
479 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700480
481 // Bug, need to stash in metadata
482 if (mRootExpand) {
483 script->mHal.info.root = mRootExpand;
484 } else {
485 script->mHal.info.root = mRoot;
486 }
Jason Sams709a0972012-11-15 18:18:04 -0800487}
488
Jason Sams709a0972012-11-15 18:18:04 -0800489
490typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
491
Jason Samsbf2111d2015-01-26 18:13:41 -0800492bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700493 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700494 Allocation * aout,
495 const void * usr, uint32_t usrLen,
496 const RsScriptCall *sc,
497 MTLaunchStruct *mtls) {
498
499 memset(mtls, 0, sizeof(MTLaunchStruct));
500
Chris Wailesf3712132014-07-16 15:18:30 -0700501 for (int index = inLen; --index >= 0;) {
502 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700503
Chris Wailesf3712132014-07-16 15:18:30 -0700504 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700505 if (ain != nullptr &&
506 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
507
Chris Wailesf3712132014-07-16 15:18:30 -0700508 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
509 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800510 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700511 }
512 }
513
Chris Wailes44bef6f2014-08-12 13:51:10 -0700514 if (aout &&
515 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
516
Chris Wailesf3712132014-07-16 15:18:30 -0700517 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
518 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800519 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700520 }
521
Chris Wailesf3712132014-07-16 15:18:30 -0700522 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700523 const Allocation *ain0 = ains[0];
524 const Type *inType = ain0->getType();
525
Jason Samsc0d68472015-01-20 14:29:52 -0800526 mtls->fep.dim.x = inType->getDimX();
527 mtls->fep.dim.y = inType->getDimY();
528 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700529
530 for (int Index = inLen; --Index >= 1;) {
531 if (!ain0->hasSameDims(ains[Index])) {
532 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800533 "Failed to launch kernel; dimensions of input and output"
534 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700535
Jason Samsbf2111d2015-01-26 18:13:41 -0800536 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700537 }
538 }
539
Chris Wailes44bef6f2014-08-12 13:51:10 -0700540 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700541 const Type *outType = aout->getType();
542
Jason Samsc0d68472015-01-20 14:29:52 -0800543 mtls->fep.dim.x = outType->getDimX();
544 mtls->fep.dim.y = outType->getDimY();
545 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700546
547 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700548 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
549 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800550 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700551 }
552
Chris Wailes44bef6f2014-08-12 13:51:10 -0700553 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700554 if (!ains[0]->hasSameDims(aout)) {
555 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
556 "Failed to launch kernel; dimensions of input and output allocations do not match.");
557
Jason Samsbf2111d2015-01-26 18:13:41 -0800558 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700559 }
560 }
561
562 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800563 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700564 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800565 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
566 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
567 if (mtls->start.x >= mtls->end.x) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700568 }
569
570 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800571 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700572 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800573 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
574 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
575 if (mtls->start.y >= mtls->end.y) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700576 }
577
578 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800579 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700580 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800581 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
582 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
583 if (mtls->start.z >= mtls->end.z) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700584 }
585
Jason Samsbf2111d2015-01-26 18:13:41 -0800586 if (!sc || (sc->arrayEnd == 0)) {
587 mtls->end.array[0] = mtls->fep.dim.array[0];
588 } else {
589 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
590 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
591 if (mtls->start.array[0] >= mtls->end.array[0]) return false;
592 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700593
Jason Samsbf2111d2015-01-26 18:13:41 -0800594 if (!sc || (sc->array2End == 0)) {
595 mtls->end.array[1] = mtls->fep.dim.array[1];
596 } else {
597 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
598 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
599 if (mtls->start.array[1] >= mtls->end.array[1]) return false;
600 }
601
602 if (!sc || (sc->array3End == 0)) {
603 mtls->end.array[2] = mtls->fep.dim.array[2];
604 } else {
605 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
606 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
607 if (mtls->start.array[2] >= mtls->end.array[2]) return false;
608 }
609
610 if (!sc || (sc->array4End == 0)) {
611 mtls->end.array[3] = mtls->fep.dim.array[3];
612 } else {
613 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
614 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
615 if (mtls->start.array[3] >= mtls->end.array[3]) return false;
616 }
617
618
619 // The X & Y walkers always want 0-1 min even if dim is not present
620 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
621 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700622
623 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800624 if (ains) {
625 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
626 }
627 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700628 mtls->fep.usr = usr;
629 mtls->fep.usrLen = usrLen;
630 mtls->mSliceSize = 1;
631 mtls->mSliceNum = 0;
632
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700633 mtls->isThreadable = mIsThreadable;
634
Chris Wailesf3712132014-07-16 15:18:30 -0700635 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700636 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700637 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800638 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
639 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700640 }
641 }
642
Chris Wailes44bef6f2014-08-12 13:51:10 -0700643 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800644 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
645 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700646 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800647
648 // All validation passed, ok to launch threads
649 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700650}
651
Jason Sams709a0972012-11-15 18:18:04 -0800652
653void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700654 const Allocation ** ains,
655 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800656 Allocation * aout,
657 const void * usr,
658 uint32_t usrLen,
659 const RsScriptCall *sc) {
660
661 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700662
Jason Samsbf2111d2015-01-26 18:13:41 -0800663 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
664 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700665
Jason Samsbf2111d2015-01-26 18:13:41 -0800666 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
667 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
668 mCtx->setTLS(oldTLS);
669 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700670}
671
Jason Sams709a0972012-11-15 18:18:04 -0800672void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800673 mtls->script = this;
674 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800675 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700676 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800677 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800678}
679
680int RsdCpuScriptImpl::invokeRoot() {
681 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
682 int ret = mRoot();
683 mCtx->setTLS(oldTLS);
684 return ret;
685}
686
687void RsdCpuScriptImpl::invokeInit() {
688 if (mInit) {
689 mInit();
690 }
691}
692
693void RsdCpuScriptImpl::invokeFreeChildren() {
694 if (mFreeChildren) {
695 mFreeChildren();
696 }
697}
698
699void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
700 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800701 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800702 void * ap = nullptr;
703
704#if defined(__x86_64__)
705 // The invoked function could have input parameter of vector type for example float4 which
706 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
707 // So try to align void* params before passing them into RS exported function.
708
709 if ((uint8_t)(uint64_t)params & 0x0F) {
710 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
711 memcpy(ap, params, paramLength);
712 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800713 ALOGE("x86_64: invokeFunction memalign error, still use params which"
714 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800715 }
716 }
717#endif
Jason Sams709a0972012-11-15 18:18:04 -0800718
719 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800720 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800721 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800722
Jason Sams709a0972012-11-15 18:18:04 -0800723 mCtx->setTLS(oldTLS);
724}
725
726void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
727 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800728 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800729
730 //if (mIntrinsicID) {
731 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
732 //return;
733 //}
734
Yang Nid9bae682015-01-20 15:31:15 -0800735 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800736 if (!destPtr) {
737 //ALOGV("Calling setVar on slot = %i which is null", slot);
738 return;
739 }
740
741 memcpy(destPtr, data, dataLength);
742}
743
Tim Murray9c642392013-04-11 13:29:59 -0700744void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
745 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800746 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700747
Yang Nid9bae682015-01-20 15:31:15 -0800748 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700749 if (!srcPtr) {
750 //ALOGV("Calling setVar on slot = %i which is null", slot);
751 return;
752 }
753 memcpy(data, srcPtr, dataLength);
754}
755
756
Jason Sams709a0972012-11-15 18:18:04 -0800757void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
758 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700759 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800760 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800761 if (!destPtr) {
762 //ALOGV("Calling setVar on slot = %i which is null", slot);
763 return;
764 }
765
766 // We want to look at dimension in terms of integer components,
767 // but dimLength is given in terms of bytes.
768 dimLength /= sizeof(int);
769
770 // Only a single dimension is currently supported.
771 rsAssert(dimLength == 1);
772 if (dimLength == 1) {
773 // First do the increment loop.
774 size_t stride = elem->getSizeBytes();
775 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700776 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800777 elem->incRefs(cVal);
778 cVal += stride;
779 }
780
781 // Decrement loop comes after (to prevent race conditions).
782 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700783 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800784 elem->decRefs(oldVal);
785 oldVal += stride;
786 }
787 }
788
789 memcpy(destPtr, data, dataLength);
790}
791
792void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
793
794 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800795 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800796
Yang Nid9bae682015-01-20 15:31:15 -0800797 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800798 if (!destPtr) {
799 //ALOGV("Calling setVar on slot = %i which is null", slot);
800 return;
801 }
802
Chris Wailes44bef6f2014-08-12 13:51:10 -0700803 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800804 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800805 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800806 ptr = data->mHal.drvState.lod[0].mallocPtr;
807 }
808 memcpy(destPtr, &ptr, sizeof(void *));
809}
810
811void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
812
813 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800814 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800815
Yang Nid9bae682015-01-20 15:31:15 -0800816 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800817 if (!destPtr) {
818 //ALOGV("Calling setVar on slot = %i which is null", slot);
819 return;
820 }
821
Jason Sams05ef73f2014-08-05 14:59:22 -0700822 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800823}
824
Yang Ni062c2872015-02-20 15:20:00 -0800825const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
826 return mScriptExec->getFieldName(slot);
827}
828
Jason Sams709a0972012-11-15 18:18:04 -0800829RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -0700830#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -0800831 if (mCompilerDriver) {
832 delete mCompilerDriver;
833 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800834#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800835
Yang Nid9bae682015-01-20 15:31:15 -0800836 if (mScriptExec != nullptr) {
837 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800838 }
Jason Sams110f1812013-03-14 16:02:18 -0700839 if (mBoundAllocs) delete[] mBoundAllocs;
840 if (mScriptSO) {
841 dlclose(mScriptSO);
842 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800843
844 delete[] mBuildChecksum;
Jason Sams709a0972012-11-15 18:18:04 -0800845}
846
847Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
848 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700849 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800850 }
851
852 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
853 Allocation *a = mBoundAllocs[ct];
854 if (!a) continue;
855 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
856 return a;
857 }
858 }
859 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700860 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800861}
862
Chris Wailesf3712132014-07-16 15:18:30 -0700863void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
864 uint32_t inLen, Allocation * aout,
865 const void * usr, uint32_t usrLen,
866 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700867
Chris Wailesf3712132014-07-16 15:18:30 -0700868void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
869 uint32_t inLen, Allocation * aout,
870 const void * usr, uint32_t usrLen,
871 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700872
Jason Sams709a0972012-11-15 18:18:04 -0800873
874}
875}