blob: ae7e597020b5e638c3f550b7c8c788876c67cdf4 [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
26 #include <bcc/BCCContext.h>
Stephen Hines82e0a672014-05-05 15:40:56 -070027 #include <bcc/Config/Config.h>
Jason Sams110f1812013-03-14 16:02:18 -070028 #include <bcc/Renderscript/RSCompilerDriver.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070029 #include <bcinfo/MetadataExtractor.h>
Stephen Hinesba17ae42013-06-05 17:18:04 -070030 #include <cutils/properties.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070031
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -080032 #include <zlib.h>
33 #include <sys/file.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070034 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080037
38 #include <string>
39 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070040#endif
Jason Sams709a0972012-11-15 18:18:04 -080041
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080042#include <set>
43#include <string>
44#include <dlfcn.h>
45#include <stdlib.h>
46#include <string.h>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080047#include <iostream>
48
49#ifdef __LP64__
50#define SYSLIBPATH "/system/lib64"
51#else
52#define SYSLIBPATH "/system/lib"
53#endif
54
Stephen Hinesba17ae42013-06-05 17:18:04 -070055namespace {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080056#ifndef RS_COMPATIBILITY_LIB
57
Stephen Hinesba17ae42013-06-05 17:18:04 -070058static bool is_force_recompile() {
59#ifdef RS_SERVER
60 return false;
61#else
62 char buf[PROPERTY_VALUE_MAX];
63
64 // Re-compile if floating point precision has been overridden.
65 property_get("debug.rs.precision", buf, "");
66 if (buf[0] != '\0') {
67 return true;
68 }
69
70 // Re-compile if debug.rs.forcerecompile is set.
71 property_get("debug.rs.forcerecompile", buf, "0");
72 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
73 return true;
74 } else {
75 return false;
76 }
77#endif // RS_SERVER
78}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070079
Chris Wailes6847e732014-08-11 17:30:51 -070080static void setCompileArguments(std::vector<const char*>* args,
81 const std::string& bcFileName,
82 const char* cacheDir, const char* resName,
83 const char* core_lib, bool useRSDebugContext,
84 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070085 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080086 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080087 args->push_back("-unroll-runtime");
88 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070089 args->push_back("-o");
90 args->push_back(resName);
91 args->push_back("-output_path");
92 args->push_back(cacheDir);
93 args->push_back("-bclib");
94 args->push_back(core_lib);
95 args->push_back("-mtriple");
96 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
97
Tim Murray358ffb82014-12-09 11:53:06 -080098 // Enable workaround for A53 codegen by default.
99#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
100 args->push_back("-aarch64-fix-cortex-a53-835769");
101#endif
102
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700103 // Execute the bcc compiler.
104 if (useRSDebugContext) {
105 args->push_back("-rs-debug-ctx");
106 } else {
107 // Only load additional libraries for compiles that don't use
108 // the debug context.
109 if (bccPluginName && strlen(bccPluginName) > 0) {
110 args->push_back("-load");
111 args->push_back(bccPluginName);
112 }
113 }
114
Stephen Hines45e753a2015-01-19 20:58:44 -0800115 args->push_back("-fPIC");
116 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800117
Chris Wailes6847e732014-08-11 17:30:51 -0700118 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700119 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700120}
121
Chris Wailes6847e732014-08-11 17:30:51 -0700122static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700123 const char *bitcode,
124 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700125 const char **compileArguments,
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800126 const char *compileCommandLine) {
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
142 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800143
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700144 switch (pid) {
145 case -1: { // Error occurred (we attempt no recovery)
146 ALOGE("Couldn't fork for bcc compiler execution");
147 return false;
148 }
149 case 0: { // Child process
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800150 ALOGV("Invoking BCC with: %s", compileCommandLine);
Yang Nida0f0692015-01-12 13:03:40 -0800151 execv(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
152 (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800153
Stephen Hines00511322014-01-31 11:20:23 -0800154 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700155 abort();
156 return false;
157 }
158 default: { // Parent process (actual driver)
159 // Wait on child process to finish compiling the source.
160 int status = 0;
161 pid_t w = waitpid(pid, &status, 0);
162 if (w == -1) {
163 ALOGE("Could not wait for bcc compiler");
164 return false;
165 }
166
167 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
168 return true;
169 }
170
171 ALOGE("bcc compiler terminated unexpectedly");
172 return false;
173 }
174 }
175}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700176
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800177bool isChecksumNeeded() {
178 char buf[PROPERTY_VALUE_MAX];
179 property_get("ro.debuggable", buf, "");
180 return (buf[0] == '1');
181}
182
183bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
184 int FD = open(fileName, O_RDONLY);
185 if (FD == -1) {
186 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
187 return false;
188 }
189
190 char buf[256];
191 while (true) {
192 ssize_t nread = read(FD, buf, sizeof(buf));
193 if (nread < 0) { // bail out on failed read
194 ALOGE("Error while computing checksum for file \'%s\'", fileName);
195 return false;
196 }
197
198 checksum = adler32(checksum, (const unsigned char *) buf, nread);
199 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
200 break;
201 }
202
203 if (close(FD) != 0) {
204 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
205 return false;
206 }
207 return true;
208}
209
210char *constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
211 const char *commandLine,
212 const std::vector<const char *> &bccFiles) {
213 uint32_t checksum = adler32(0L, Z_NULL, 0);
214
215 // include checksum of bitcode
216 checksum = adler32(checksum, bitcode, bitcodeSize);
217
218 // include checksum of command line arguments
219 checksum = adler32(checksum, (const unsigned char *) commandLine,
220 strlen(commandLine));
221
222 // include checksum of bccFiles
223 for (auto bccFile : bccFiles) {
224 if (!addFileToChecksum(bccFile, checksum)) {
225 // return empty checksum instead of something partial/corrupt
226 return nullptr;
227 }
228 }
229
230 char *checksumStr = new char[9]();
231 sprintf(checksumStr, "%08x", checksum);
232 return checksumStr;
233}
234
Yang Ni1c44cb62015-01-22 12:02:27 -0800235#endif // !defined(RS_COMPATIBILITY_LIB)
236} // namespace
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800237
Yang Ni1c44cb62015-01-22 12:02:27 -0800238namespace android {
239namespace renderscript {
240
Jason Sams709a0972012-11-15 18:18:04 -0800241RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
242 mCtx = ctx;
243 mScript = s;
244
Chris Wailes44bef6f2014-08-12 13:51:10 -0700245 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800246
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800247#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700248 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700249#endif
250
Tim Murraye195a3f2014-03-13 15:04:58 -0700251
Chris Wailes44bef6f2014-08-12 13:51:10 -0700252 mRoot = nullptr;
253 mRootExpand = nullptr;
254 mInit = nullptr;
255 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800256 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800257
Chris Wailes44bef6f2014-08-12 13:51:10 -0700258 mBoundAllocs = nullptr;
259 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800260 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800261
262 mBuildChecksum = nullptr;
263 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800264}
265
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800266bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800267 // The shared object may have an invalid build checksum.
268 // Validate and fail early.
269 mScriptExec = ScriptExecutable::createFromSharedObject(
270 mCtx->getContext(), mScriptSO);
271
272 if (mScriptExec == nullptr) {
273 return false;
274 }
275
276 if (mChecksumNeeded && !mScriptExec->isChecksumValid(mBuildChecksum)) {
277 ALOGE("Found invalid checksum. Expected %s, got %s\n",
278 mBuildChecksum, mScriptExec->getBuildChecksum());
279 delete mScriptExec;
280 mScriptExec = nullptr;
281 return false;
282 }
283
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800284 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
285 if (mRoot) {
286 //ALOGE("Found root(): %p", mRoot);
287 }
288 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
289 if (mRootExpand) {
290 //ALOGE("Found root.expand(): %p", mRootExpand);
291 }
292 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
293 if (mInit) {
294 //ALOGE("Found init(): %p", mInit);
295 }
296 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
297 if (mFreeChildren) {
298 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
299 }
300
Yang Nid9bae682015-01-20 15:31:15 -0800301 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800302 if (varCount > 0) {
303 mBoundAllocs = new Allocation *[varCount];
304 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
305 }
306
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800307 mIsThreadable = mScriptExec->getThreadable();
308 //ALOGE("Script isThreadable? %d", mIsThreadable);
309
Yang Nid9bae682015-01-20 15:31:15 -0800310 return true;
311}
312
Jason Sams709a0972012-11-15 18:18:04 -0800313bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
314 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800315 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800316 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
317 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800318 //ALOGE("rsdScriptInit %p %p", rsc, script);
319
320 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700321#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800322 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800323
Chris Wailes44bef6f2014-08-12 13:51:10 -0700324 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800325
Jason Sams709a0972012-11-15 18:18:04 -0800326 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700327 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800328 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
329 mCtx->unlockMutex();
330 return false;
331 }
332
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700333 // Run any compiler setup functions we have been provided with.
334 RSSetupCompilerCallback setupCompilerCallback =
335 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700336 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700337 setupCompilerCallback(mCompilerDriver);
338 }
339
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700340 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
341 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700342 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700343 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700344 return false;
345 }
346
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700347 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700348
349 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700350 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800351 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700352 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700353
Chris Wailes6847e732014-08-11 17:30:51 -0700354 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700355 bcFileName.append("/");
356 bcFileName.append(resName);
357 bcFileName.append(".bc");
358
359 std::vector<const char*> compileArguments;
360 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
361 useRSDebugContext, bccPluginName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800362
363 // The last argument of compileArguments is a nullptr, so remove 1 from the
364 // size.
365 std::unique_ptr<const char> compileCommandLine(
Yang Ni2abfcc62015-02-17 16:05:19 -0800366 rsuJoinStrings(compileArguments.size() - 1, compileArguments.data()));
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800367
368 mChecksumNeeded = isChecksumNeeded();
369 if (mChecksumNeeded) {
370 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
371 core_lib,
372 };
373 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
374 compileCommandLine.get(),
375 bccFiles);
376
377 if (mBuildChecksum == nullptr) {
378 // cannot compute checksum but verification is enabled
379 mCtx->unlockMutex();
380 return false;
381 }
382 }
383 else {
384 // add a dummy/constant as a checksum if verification is disabled
385 mBuildChecksum = new char[9]();
386 strcpy(const_cast<char *>(mBuildChecksum), "abadcafe");
387 }
388
389 // Append build checksum to commandline
390 // Handle the terminal nullptr in compileArguments
391 compileArguments.pop_back();
392 compileArguments.push_back("-build-checksum");
393 compileArguments.push_back(mBuildChecksum);
394 compileArguments.push_back(nullptr);
395
396 // recompute compileCommandLine with the extra arguments
397 compileCommandLine.reset(
398 rsuJoinStrings(compileArguments.size() - 1, compileArguments.data()));
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700399
Tim Murraybf96a522015-01-23 15:37:03 -0800400 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800401 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800402
403 // Read RS info from the shared object to detect checksum mismatch
404 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
405 dlclose(mScriptSO);
406 mScriptSO = nullptr;
407 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700408 }
409
410 // If we can't, it's either not there or out of date. We compile the bit code and try loading
411 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800412 if (mScriptSO == nullptr) {
413 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800414 compileArguments.data(), compileCommandLine.get()))
Stephen Hines45e753a2015-01-19 20:58:44 -0800415 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700416 ALOGE("bcc: FAILS to compile '%s'", resName);
417 mCtx->unlockMutex();
418 return false;
419 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800420
Yang Ni1c44cb62015-01-22 12:02:27 -0800421 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800422 ALOGE("Linker: Failed to link object file '%s'", resName);
423 mCtx->unlockMutex();
424 return false;
425 }
426
Yang Ni1c44cb62015-01-22 12:02:27 -0800427 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800428 if (mScriptSO == nullptr) {
429 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700430 mCtx->unlockMutex();
431 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700432 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800433
434 // Read RS symbol information from the .so.
435 if (!storeRSInfoFromSO()) {
436 goto error;
437 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700438 }
Jason Sams709a0972012-11-15 18:18:04 -0800439
Yang Nic31585b2015-02-15 11:43:50 -0800440 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800441
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700442#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800443 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
444 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700445
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800446 if (!mScriptSO) {
447 goto error;
448 }
Jason Sams110f1812013-03-14 16:02:18 -0700449
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800450 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700451 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700452 }
453#endif
Jason Sams709a0972012-11-15 18:18:04 -0800454 mCtx->unlockMutex();
455 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700456
Jason Sams110f1812013-03-14 16:02:18 -0700457error:
458
459 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700460 if (mScriptSO) {
461 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800462 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700463 }
464 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800465}
466
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700467#ifndef RS_COMPATIBILITY_LIB
468
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700469const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
470 size_t bitcodeSize) {
471 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
472
473 // If we're debugging, use the debug library.
474 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
475 return SYSLIBPATH"/libclcore_debug.bc";
476 }
477
478 // If a callback has been registered to specify a library, use that.
479 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700480 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700481 return selectRTCallback((const char*)bitcode, bitcodeSize);
482 }
483
484 // Check for a platform specific library
485#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
486 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700487 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700488 // NEON-capable ARMv7a devices can use an accelerated math library
489 // for all reduced precision scripts.
490 // ARMv8 does not use NEON, as ASIMD can be used with all precision
491 // levels.
492 return SYSLIBPATH"/libclcore_neon.bc";
493 } else {
494 return defaultLib;
495 }
496#elif defined(__i386__) || defined(__x86_64__)
497 // x86 devices will use an optimized library.
498 return SYSLIBPATH"/libclcore_x86.bc";
499#else
500 return defaultLib;
501#endif
502}
503
504#endif
505
Jason Sams709a0972012-11-15 18:18:04 -0800506void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700507 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800508 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
509 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800510 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800511 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
512 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700513
514 // Bug, need to stash in metadata
515 if (mRootExpand) {
516 script->mHal.info.root = mRootExpand;
517 } else {
518 script->mHal.info.root = mRoot;
519 }
Jason Sams709a0972012-11-15 18:18:04 -0800520}
521
Jason Sams709a0972012-11-15 18:18:04 -0800522
523typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
524
Jason Samsbf2111d2015-01-26 18:13:41 -0800525bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700526 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700527 Allocation * aout,
528 const void * usr, uint32_t usrLen,
529 const RsScriptCall *sc,
530 MTLaunchStruct *mtls) {
531
532 memset(mtls, 0, sizeof(MTLaunchStruct));
533
Chris Wailesf3712132014-07-16 15:18:30 -0700534 for (int index = inLen; --index >= 0;) {
535 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700536
Chris Wailesf3712132014-07-16 15:18:30 -0700537 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700538 if (ain != nullptr &&
539 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
540
Chris Wailesf3712132014-07-16 15:18:30 -0700541 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
542 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800543 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700544 }
545 }
546
Chris Wailes44bef6f2014-08-12 13:51:10 -0700547 if (aout &&
548 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
549
Chris Wailesf3712132014-07-16 15:18:30 -0700550 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
551 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800552 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700553 }
554
Chris Wailesf3712132014-07-16 15:18:30 -0700555 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700556 const Allocation *ain0 = ains[0];
557 const Type *inType = ain0->getType();
558
Jason Samsc0d68472015-01-20 14:29:52 -0800559 mtls->fep.dim.x = inType->getDimX();
560 mtls->fep.dim.y = inType->getDimY();
561 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700562
563 for (int Index = inLen; --Index >= 1;) {
564 if (!ain0->hasSameDims(ains[Index])) {
565 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800566 "Failed to launch kernel; dimensions of input and output"
567 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700568
Jason Samsbf2111d2015-01-26 18:13:41 -0800569 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700570 }
571 }
572
Chris Wailes44bef6f2014-08-12 13:51:10 -0700573 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700574 const Type *outType = aout->getType();
575
Jason Samsc0d68472015-01-20 14:29:52 -0800576 mtls->fep.dim.x = outType->getDimX();
577 mtls->fep.dim.y = outType->getDimY();
578 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700579
580 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700581 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
582 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800583 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700584 }
585
Chris Wailes44bef6f2014-08-12 13:51:10 -0700586 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700587 if (!ains[0]->hasSameDims(aout)) {
588 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
589 "Failed to launch kernel; dimensions of input and output allocations do not match.");
590
Jason Samsbf2111d2015-01-26 18:13:41 -0800591 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700592 }
593 }
594
595 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800596 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700597 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800598 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
599 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
600 if (mtls->start.x >= mtls->end.x) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700601 }
602
603 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800604 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700605 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800606 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
607 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
608 if (mtls->start.y >= mtls->end.y) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700609 }
610
611 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800612 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700613 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800614 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
615 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
616 if (mtls->start.z >= mtls->end.z) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700617 }
618
Jason Samsbf2111d2015-01-26 18:13:41 -0800619 if (!sc || (sc->arrayEnd == 0)) {
620 mtls->end.array[0] = mtls->fep.dim.array[0];
621 } else {
622 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
623 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
624 if (mtls->start.array[0] >= mtls->end.array[0]) return false;
625 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700626
Jason Samsbf2111d2015-01-26 18:13:41 -0800627 if (!sc || (sc->array2End == 0)) {
628 mtls->end.array[1] = mtls->fep.dim.array[1];
629 } else {
630 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
631 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
632 if (mtls->start.array[1] >= mtls->end.array[1]) return false;
633 }
634
635 if (!sc || (sc->array3End == 0)) {
636 mtls->end.array[2] = mtls->fep.dim.array[2];
637 } else {
638 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
639 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
640 if (mtls->start.array[2] >= mtls->end.array[2]) return false;
641 }
642
643 if (!sc || (sc->array4End == 0)) {
644 mtls->end.array[3] = mtls->fep.dim.array[3];
645 } else {
646 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
647 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
648 if (mtls->start.array[3] >= mtls->end.array[3]) return false;
649 }
650
651
652 // The X & Y walkers always want 0-1 min even if dim is not present
653 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
654 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700655
656 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800657 if (ains) {
658 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
659 }
660 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700661 mtls->fep.usr = usr;
662 mtls->fep.usrLen = usrLen;
663 mtls->mSliceSize = 1;
664 mtls->mSliceNum = 0;
665
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700666 mtls->isThreadable = mIsThreadable;
667
Chris Wailesf3712132014-07-16 15:18:30 -0700668 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700669 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700670 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800671 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
672 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700673 }
674 }
675
Chris Wailes44bef6f2014-08-12 13:51:10 -0700676 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800677 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
678 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700679 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800680
681 // All validation passed, ok to launch threads
682 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700683}
684
Jason Sams709a0972012-11-15 18:18:04 -0800685
686void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700687 const Allocation ** ains,
688 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800689 Allocation * aout,
690 const void * usr,
691 uint32_t usrLen,
692 const RsScriptCall *sc) {
693
694 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700695
Jason Samsbf2111d2015-01-26 18:13:41 -0800696 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
697 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700698
Jason Samsbf2111d2015-01-26 18:13:41 -0800699 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
700 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
701 mCtx->setTLS(oldTLS);
702 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700703}
704
Jason Sams709a0972012-11-15 18:18:04 -0800705void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800706 mtls->script = this;
707 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800708 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700709 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800710 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800711}
712
713int RsdCpuScriptImpl::invokeRoot() {
714 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
715 int ret = mRoot();
716 mCtx->setTLS(oldTLS);
717 return ret;
718}
719
720void RsdCpuScriptImpl::invokeInit() {
721 if (mInit) {
722 mInit();
723 }
724}
725
726void RsdCpuScriptImpl::invokeFreeChildren() {
727 if (mFreeChildren) {
728 mFreeChildren();
729 }
730}
731
732void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
733 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800734 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800735 void * ap = nullptr;
736
737#if defined(__x86_64__)
738 // The invoked function could have input parameter of vector type for example float4 which
739 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
740 // So try to align void* params before passing them into RS exported function.
741
742 if ((uint8_t)(uint64_t)params & 0x0F) {
743 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
744 memcpy(ap, params, paramLength);
745 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800746 ALOGE("x86_64: invokeFunction memalign error, still use params which"
747 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800748 }
749 }
750#endif
Jason Sams709a0972012-11-15 18:18:04 -0800751
752 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800753 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800754 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800755
Jason Sams709a0972012-11-15 18:18:04 -0800756 mCtx->setTLS(oldTLS);
757}
758
759void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
760 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800761 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800762
763 //if (mIntrinsicID) {
764 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
765 //return;
766 //}
767
Yang Nid9bae682015-01-20 15:31:15 -0800768 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800769 if (!destPtr) {
770 //ALOGV("Calling setVar on slot = %i which is null", slot);
771 return;
772 }
773
774 memcpy(destPtr, data, dataLength);
775}
776
Tim Murray9c642392013-04-11 13:29:59 -0700777void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
778 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800779 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700780
Yang Nid9bae682015-01-20 15:31:15 -0800781 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700782 if (!srcPtr) {
783 //ALOGV("Calling setVar on slot = %i which is null", slot);
784 return;
785 }
786 memcpy(data, srcPtr, dataLength);
787}
788
789
Jason Sams709a0972012-11-15 18:18:04 -0800790void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
791 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700792 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800793 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800794 if (!destPtr) {
795 //ALOGV("Calling setVar on slot = %i which is null", slot);
796 return;
797 }
798
799 // We want to look at dimension in terms of integer components,
800 // but dimLength is given in terms of bytes.
801 dimLength /= sizeof(int);
802
803 // Only a single dimension is currently supported.
804 rsAssert(dimLength == 1);
805 if (dimLength == 1) {
806 // First do the increment loop.
807 size_t stride = elem->getSizeBytes();
808 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700809 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800810 elem->incRefs(cVal);
811 cVal += stride;
812 }
813
814 // Decrement loop comes after (to prevent race conditions).
815 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700816 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800817 elem->decRefs(oldVal);
818 oldVal += stride;
819 }
820 }
821
822 memcpy(destPtr, data, dataLength);
823}
824
825void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
826
827 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800828 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800829
Yang Nid9bae682015-01-20 15:31:15 -0800830 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800831 if (!destPtr) {
832 //ALOGV("Calling setVar on slot = %i which is null", slot);
833 return;
834 }
835
Chris Wailes44bef6f2014-08-12 13:51:10 -0700836 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800837 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800838 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800839 ptr = data->mHal.drvState.lod[0].mallocPtr;
840 }
841 memcpy(destPtr, &ptr, sizeof(void *));
842}
843
844void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
845
846 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800847 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800848
Yang Nid9bae682015-01-20 15:31:15 -0800849 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800850 if (!destPtr) {
851 //ALOGV("Calling setVar on slot = %i which is null", slot);
852 return;
853 }
854
Jason Sams05ef73f2014-08-05 14:59:22 -0700855 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800856}
857
858RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -0700859#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -0800860 if (mCompilerDriver) {
861 delete mCompilerDriver;
862 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800863#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800864
Yang Nid9bae682015-01-20 15:31:15 -0800865 if (mScriptExec != nullptr) {
866 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800867 }
Jason Sams110f1812013-03-14 16:02:18 -0700868 if (mBoundAllocs) delete[] mBoundAllocs;
869 if (mScriptSO) {
870 dlclose(mScriptSO);
871 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800872
873 delete[] mBuildChecksum;
Jason Sams709a0972012-11-15 18:18:04 -0800874}
875
876Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
877 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700878 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800879 }
880
881 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
882 Allocation *a = mBoundAllocs[ct];
883 if (!a) continue;
884 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
885 return a;
886 }
887 }
888 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700889 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800890}
891
Chris Wailesf3712132014-07-16 15:18:30 -0700892void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
893 uint32_t inLen, Allocation * aout,
894 const void * usr, uint32_t usrLen,
895 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700896
Chris Wailesf3712132014-07-16 15:18:30 -0700897void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
898 uint32_t inLen, Allocation * aout,
899 const void * usr, uint32_t usrLen,
900 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700901
Jason Sams709a0972012-11-15 18:18:04 -0800902
903}
904}