blob: 328237471f6515bab3f5d8593cb7e9f92b5e759b [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>
Yang Nicb170152015-04-16 10:27:02 -070049#include <sstream>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080050
51#ifdef __LP64__
52#define SYSLIBPATH "/system/lib64"
53#else
54#define SYSLIBPATH "/system/lib"
55#endif
56
Stephen Hinesba17ae42013-06-05 17:18:04 -070057namespace {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080058#ifndef RS_COMPATIBILITY_LIB
59
Stephen Hinesba17ae42013-06-05 17:18:04 -070060static bool is_force_recompile() {
61#ifdef RS_SERVER
62 return false;
63#else
64 char buf[PROPERTY_VALUE_MAX];
65
66 // Re-compile if floating point precision has been overridden.
67 property_get("debug.rs.precision", buf, "");
68 if (buf[0] != '\0') {
69 return true;
70 }
71
72 // Re-compile if debug.rs.forcerecompile is set.
73 property_get("debug.rs.forcerecompile", buf, "0");
74 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
75 return true;
76 } else {
77 return false;
78 }
79#endif // RS_SERVER
80}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070081
Chris Wailes6847e732014-08-11 17:30:51 -070082static void setCompileArguments(std::vector<const char*>* args,
83 const std::string& bcFileName,
84 const char* cacheDir, const char* resName,
85 const char* core_lib, bool useRSDebugContext,
86 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070087 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080088 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080089 args->push_back("-unroll-runtime");
90 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070091 args->push_back("-o");
92 args->push_back(resName);
93 args->push_back("-output_path");
94 args->push_back(cacheDir);
95 args->push_back("-bclib");
96 args->push_back(core_lib);
97 args->push_back("-mtriple");
98 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
99
Tim Murray358ffb82014-12-09 11:53:06 -0800100 // Enable workaround for A53 codegen by default.
101#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
102 args->push_back("-aarch64-fix-cortex-a53-835769");
103#endif
104
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700105 // Execute the bcc compiler.
106 if (useRSDebugContext) {
107 args->push_back("-rs-debug-ctx");
108 } else {
109 // Only load additional libraries for compiles that don't use
110 // the debug context.
111 if (bccPluginName && strlen(bccPluginName) > 0) {
112 args->push_back("-load");
113 args->push_back(bccPluginName);
114 }
115 }
116
Stephen Hines45e753a2015-01-19 20:58:44 -0800117 args->push_back("-fPIC");
118 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800119
Chris Wailes6847e732014-08-11 17:30:51 -0700120 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700121 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700122}
123
Chris Wailes6847e732014-08-11 17:30:51 -0700124static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700125 const char *bitcode,
126 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700127 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700128 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700129
Chris Wailes6847e732014-08-11 17:30:51 -0700130 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700131 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700132 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700133 return false;
134 }
135 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
136 fclose(bcfile);
137 if (nwritten != bitcodeSize) {
138 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700139 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700140 return false;
141 }
142
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700143 return android::renderscript::rsuExecuteCommand(
144 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
145 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700146}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700147
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800148bool isChecksumNeeded() {
149 char buf[PROPERTY_VALUE_MAX];
150 property_get("ro.debuggable", buf, "");
151 return (buf[0] == '1');
152}
153
154bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
155 int FD = open(fileName, O_RDONLY);
156 if (FD == -1) {
157 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
158 return false;
159 }
160
161 char buf[256];
162 while (true) {
163 ssize_t nread = read(FD, buf, sizeof(buf));
164 if (nread < 0) { // bail out on failed read
165 ALOGE("Error while computing checksum for file \'%s\'", fileName);
166 return false;
167 }
168
169 checksum = adler32(checksum, (const unsigned char *) buf, nread);
170 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
171 break;
172 }
173
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700174 if (TEMP_FAILURE_RETRY(close(FD)) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800175 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
176 return false;
177 }
178 return true;
179}
Yang Nicb170152015-04-16 10:27:02 -0700180
181#endif // !defined(RS_COMPATIBILITY_LIB)
182} // namespace
183
184namespace android {
185namespace renderscript {
186
187#ifndef RS_COMPATIBILITY_LIB
188
189uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
190 const char *commandLine,
191 const char** bccFiles, size_t numFiles) {
192 uint32_t checksum = adler32(0L, Z_NULL, 0);
193
194 // include checksum of bitcode
195 if (bitcode != nullptr && bitcodeSize > 0) {
196 checksum = adler32(checksum, bitcode, bitcodeSize);
197 }
198
199 // include checksum of command line arguments
200 checksum = adler32(checksum, (const unsigned char *) commandLine,
201 strlen(commandLine));
202
203 // include checksum of bccFiles
204 for (size_t i = 0; i < numFiles; i++) {
205 const char* bccFile = bccFiles[i];
206 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
207 // return empty checksum instead of something partial/corrupt
208 return 0;
209 }
210 }
211
212 return checksum;
213}
214
Yang Nif02a2b02015-04-07 16:00:31 -0700215#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800216
Jason Sams709a0972012-11-15 18:18:04 -0800217RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
218 mCtx = ctx;
219 mScript = s;
220
Chris Wailes44bef6f2014-08-12 13:51:10 -0700221 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800222
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800223#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700224 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700225#endif
226
Tim Murraye195a3f2014-03-13 15:04:58 -0700227
Chris Wailes44bef6f2014-08-12 13:51:10 -0700228 mRoot = nullptr;
229 mRootExpand = nullptr;
230 mInit = nullptr;
231 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800232 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800233
Chris Wailes44bef6f2014-08-12 13:51:10 -0700234 mBoundAllocs = nullptr;
235 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800236 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800237
Yang Nicb170152015-04-16 10:27:02 -0700238 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800239 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800240}
241
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800242bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800243 // The shared object may have an invalid build checksum.
244 // Validate and fail early.
245 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Nicb170152015-04-16 10:27:02 -0700246 mCtx->getContext(), mScriptSO,
247 mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800248
249 if (mScriptExec == nullptr) {
250 return false;
251 }
252
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800253 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
254 if (mRoot) {
255 //ALOGE("Found root(): %p", mRoot);
256 }
257 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
258 if (mRootExpand) {
259 //ALOGE("Found root.expand(): %p", mRootExpand);
260 }
261 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
262 if (mInit) {
263 //ALOGE("Found init(): %p", mInit);
264 }
265 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
266 if (mFreeChildren) {
267 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
268 }
269
Yang Nid9bae682015-01-20 15:31:15 -0800270 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800271 if (varCount > 0) {
272 mBoundAllocs = new Allocation *[varCount];
273 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
274 }
275
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800276 mIsThreadable = mScriptExec->getThreadable();
277 //ALOGE("Script isThreadable? %d", mIsThreadable);
278
Yang Nid9bae682015-01-20 15:31:15 -0800279 return true;
280}
281
Jason Sams709a0972012-11-15 18:18:04 -0800282bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
283 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800284 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800285 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
286 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800287 //ALOGE("rsdScriptInit %p %p", rsc, script);
288
289 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700290#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800291 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800292
Chris Wailes44bef6f2014-08-12 13:51:10 -0700293 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800294
Jason Sams709a0972012-11-15 18:18:04 -0800295 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700296 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800297 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
298 mCtx->unlockMutex();
299 return false;
300 }
301
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700302 // Run any compiler setup functions we have been provided with.
303 RSSetupCompilerCallback setupCompilerCallback =
304 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700305 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700306 setupCompilerCallback(mCompilerDriver);
307 }
308
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700309 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
310 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700311 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700312 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700313 return false;
314 }
315
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700316 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700317
318 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700319 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800320 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700321 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700322
Chris Wailes6847e732014-08-11 17:30:51 -0700323 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700324 bcFileName.append("/");
325 bcFileName.append(resName);
326 bcFileName.append(".bc");
327
328 std::vector<const char*> compileArguments;
329 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
330 useRSDebugContext, bccPluginName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800331
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800332 mChecksumNeeded = isChecksumNeeded();
333 if (mChecksumNeeded) {
334 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
335 core_lib,
336 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700337
338 // The last argument of compileArguments is a nullptr, so remove 1 from
339 // the size.
340 std::unique_ptr<const char> compileCommandLine(
341 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
342
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800343 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
344 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700345 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800346
Yang Nicb170152015-04-16 10:27:02 -0700347 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800348 // cannot compute checksum but verification is enabled
349 mCtx->unlockMutex();
350 return false;
351 }
352 }
353 else {
354 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700355 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800356 }
357
358 // Append build checksum to commandline
359 // Handle the terminal nullptr in compileArguments
360 compileArguments.pop_back();
361 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700362 std::stringstream ss;
363 ss << std::hex << mBuildChecksum;
364 compileArguments.push_back(ss.str().c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800365 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
Stephen Hines4c368af2015-05-06 00:43:02 -0700388 if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
389 cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800390 ALOGE("Linker: Failed to link object file '%s'", resName);
391 mCtx->unlockMutex();
392 return false;
393 }
394
Yang Ni1c44cb62015-01-22 12:02:27 -0800395 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800396 if (mScriptSO == nullptr) {
397 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700398 mCtx->unlockMutex();
399 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700400 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800401
402 // Read RS symbol information from the .so.
403 if (!storeRSInfoFromSO()) {
404 goto error;
405 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700406 }
Jason Sams709a0972012-11-15 18:18:04 -0800407
Yang Nic31585b2015-02-15 11:43:50 -0800408 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800409
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700410#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800411 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
412 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700413
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800414 if (!mScriptSO) {
415 goto error;
416 }
Jason Sams110f1812013-03-14 16:02:18 -0700417
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800418 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700419 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700420 }
421#endif
Jason Sams709a0972012-11-15 18:18:04 -0800422 mCtx->unlockMutex();
423 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700424
Jason Sams110f1812013-03-14 16:02:18 -0700425error:
426
427 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700428 if (mScriptSO) {
429 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800430 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700431 }
432 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800433}
434
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700435#ifndef RS_COMPATIBILITY_LIB
436
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700437const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
438 size_t bitcodeSize) {
439 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
440
441 // If we're debugging, use the debug library.
442 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
443 return SYSLIBPATH"/libclcore_debug.bc";
444 }
445
446 // If a callback has been registered to specify a library, use that.
447 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700448 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700449 return selectRTCallback((const char*)bitcode, bitcodeSize);
450 }
451
452 // Check for a platform specific library
453#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
454 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700455 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700456 // NEON-capable ARMv7a devices can use an accelerated math library
457 // for all reduced precision scripts.
458 // ARMv8 does not use NEON, as ASIMD can be used with all precision
459 // levels.
460 return SYSLIBPATH"/libclcore_neon.bc";
461 } else {
462 return defaultLib;
463 }
464#elif defined(__i386__) || defined(__x86_64__)
465 // x86 devices will use an optimized library.
466 return SYSLIBPATH"/libclcore_x86.bc";
467#else
468 return defaultLib;
469#endif
470}
471
472#endif
473
Jason Sams709a0972012-11-15 18:18:04 -0800474void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700475 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800476 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
477 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800478 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800479 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
480 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700481
482 // Bug, need to stash in metadata
483 if (mRootExpand) {
484 script->mHal.info.root = mRootExpand;
485 } else {
486 script->mHal.info.root = mRoot;
487 }
Jason Sams709a0972012-11-15 18:18:04 -0800488}
489
Jason Sams709a0972012-11-15 18:18:04 -0800490
Jason Samsbf2111d2015-01-26 18:13:41 -0800491bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700492 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700493 Allocation * aout,
494 const void * usr, uint32_t usrLen,
495 const RsScriptCall *sc,
496 MTLaunchStruct *mtls) {
497
498 memset(mtls, 0, sizeof(MTLaunchStruct));
499
Chris Wailesf3712132014-07-16 15:18:30 -0700500 for (int index = inLen; --index >= 0;) {
501 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700502
Chris Wailesf3712132014-07-16 15:18:30 -0700503 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700504 if (ain != nullptr &&
505 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
506
Chris Wailesf3712132014-07-16 15:18:30 -0700507 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
508 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800509 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700510 }
511 }
512
Chris Wailes44bef6f2014-08-12 13:51:10 -0700513 if (aout &&
514 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
515
Chris Wailesf3712132014-07-16 15:18:30 -0700516 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
517 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800518 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700519 }
520
Chris Wailesf3712132014-07-16 15:18:30 -0700521 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700522 const Allocation *ain0 = ains[0];
523 const Type *inType = ain0->getType();
524
Jason Samsc0d68472015-01-20 14:29:52 -0800525 mtls->fep.dim.x = inType->getDimX();
526 mtls->fep.dim.y = inType->getDimY();
527 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700528
529 for (int Index = inLen; --Index >= 1;) {
530 if (!ain0->hasSameDims(ains[Index])) {
531 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800532 "Failed to launch kernel; dimensions of input and output"
533 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700534
Jason Samsbf2111d2015-01-26 18:13:41 -0800535 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700536 }
537 }
538
Chris Wailes44bef6f2014-08-12 13:51:10 -0700539 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700540 const Type *outType = aout->getType();
541
Jason Samsc0d68472015-01-20 14:29:52 -0800542 mtls->fep.dim.x = outType->getDimX();
543 mtls->fep.dim.y = outType->getDimY();
544 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700545
Jason Samsa9139c72015-04-16 15:11:04 -0700546 } else if (sc != nullptr) {
547 mtls->fep.dim.x = sc->xEnd;
548 mtls->fep.dim.y = sc->yEnd;
549 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700550 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700551 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
552 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800553 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700554 }
555
Chris Wailes44bef6f2014-08-12 13:51:10 -0700556 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700557 if (!ains[0]->hasSameDims(aout)) {
558 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
559 "Failed to launch kernel; dimensions of input and output allocations do not match.");
560
Jason Samsbf2111d2015-01-26 18:13:41 -0800561 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700562 }
563 }
564
565 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800566 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700567 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800568 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
569 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700570 if (mtls->start.x >= mtls->end.x) {
571 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
572 "Failed to launch kernel; Invalid xStart or xEnd.");
573 return false;
574 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700575 }
576
577 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800578 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700579 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800580 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
581 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700582 if (mtls->start.y >= mtls->end.y) {
583 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
584 "Failed to launch kernel; Invalid yStart or yEnd.");
585 return false;
586 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700587 }
588
589 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800590 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700591 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800592 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
593 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700594 if (mtls->start.z >= mtls->end.z) {
595 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
596 "Failed to launch kernel; Invalid zStart or zEnd.");
597 return false;
598 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700599 }
600
Jason Samsbf2111d2015-01-26 18:13:41 -0800601 if (!sc || (sc->arrayEnd == 0)) {
602 mtls->end.array[0] = mtls->fep.dim.array[0];
603 } else {
604 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
605 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700606 if (mtls->start.array[0] >= mtls->end.array[0]) {
607 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
608 "Failed to launch kernel; Invalid arrayStart or arrayEnd.");
609 return false;
610 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800611 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700612
Jason Samsbf2111d2015-01-26 18:13:41 -0800613 if (!sc || (sc->array2End == 0)) {
614 mtls->end.array[1] = mtls->fep.dim.array[1];
615 } else {
616 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
617 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700618 if (mtls->start.array[1] >= mtls->end.array[1]) {
619 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
620 "Failed to launch kernel; Invalid array2Start or array2End.");
621 return false;
622 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800623 }
624
625 if (!sc || (sc->array3End == 0)) {
626 mtls->end.array[2] = mtls->fep.dim.array[2];
627 } else {
628 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
629 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700630 if (mtls->start.array[2] >= mtls->end.array[2]) {
631 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
632 "Failed to launch kernel; Invalid array3Start or array3End.");
633 return false;
634 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800635 }
636
637 if (!sc || (sc->array4End == 0)) {
638 mtls->end.array[3] = mtls->fep.dim.array[3];
639 } else {
640 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
641 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700642 if (mtls->start.array[3] >= mtls->end.array[3]) {
643 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
644 "Failed to launch kernel; Invalid array4Start or array4End.");
645 return false;
646 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800647 }
648
649
650 // The X & Y walkers always want 0-1 min even if dim is not present
651 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
652 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700653
654 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800655 if (ains) {
656 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
657 }
658 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700659 mtls->fep.usr = usr;
660 mtls->fep.usrLen = usrLen;
661 mtls->mSliceSize = 1;
662 mtls->mSliceNum = 0;
663
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700664 mtls->isThreadable = mIsThreadable;
665
Chris Wailesf3712132014-07-16 15:18:30 -0700666 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700667 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700668 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800669 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
670 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700671 }
672 }
673
Chris Wailes44bef6f2014-08-12 13:51:10 -0700674 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800675 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
676 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700677 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800678
679 // All validation passed, ok to launch threads
680 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700681}
682
Jason Sams709a0972012-11-15 18:18:04 -0800683
684void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700685 const Allocation ** ains,
686 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800687 Allocation * aout,
688 const void * usr,
689 uint32_t usrLen,
690 const RsScriptCall *sc) {
691
692 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700693
Jason Samsbf2111d2015-01-26 18:13:41 -0800694 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
695 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700696
Jason Samsbf2111d2015-01-26 18:13:41 -0800697 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
698 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
699 mCtx->setTLS(oldTLS);
700 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700701}
702
Jason Sams709a0972012-11-15 18:18:04 -0800703void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800704 mtls->script = this;
705 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800706 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700707 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800708 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800709}
710
711int RsdCpuScriptImpl::invokeRoot() {
712 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
713 int ret = mRoot();
714 mCtx->setTLS(oldTLS);
715 return ret;
716}
717
718void RsdCpuScriptImpl::invokeInit() {
719 if (mInit) {
720 mInit();
721 }
722}
723
724void RsdCpuScriptImpl::invokeFreeChildren() {
725 if (mFreeChildren) {
726 mFreeChildren();
727 }
728}
729
730void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
731 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800732 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800733 void * ap = nullptr;
734
735#if defined(__x86_64__)
736 // The invoked function could have input parameter of vector type for example float4 which
737 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
738 // So try to align void* params before passing them into RS exported function.
739
740 if ((uint8_t)(uint64_t)params & 0x0F) {
741 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
742 memcpy(ap, params, paramLength);
743 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800744 ALOGE("x86_64: invokeFunction memalign error, still use params which"
745 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800746 }
747 }
748#endif
Jason Sams709a0972012-11-15 18:18:04 -0800749
750 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800751 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800752 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800753
Jason Sams709a0972012-11-15 18:18:04 -0800754 mCtx->setTLS(oldTLS);
755}
756
757void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
758 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800759 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800760
761 //if (mIntrinsicID) {
762 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
763 //return;
764 //}
765
Yang Nid9bae682015-01-20 15:31:15 -0800766 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800767 if (!destPtr) {
768 //ALOGV("Calling setVar on slot = %i which is null", slot);
769 return;
770 }
771
772 memcpy(destPtr, data, dataLength);
773}
774
Tim Murray9c642392013-04-11 13:29:59 -0700775void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
776 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800777 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700778
Yang Nid9bae682015-01-20 15:31:15 -0800779 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700780 if (!srcPtr) {
781 //ALOGV("Calling setVar on slot = %i which is null", slot);
782 return;
783 }
784 memcpy(data, srcPtr, dataLength);
785}
786
787
Jason Sams709a0972012-11-15 18:18:04 -0800788void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
789 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700790 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800791 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800792 if (!destPtr) {
793 //ALOGV("Calling setVar on slot = %i which is null", slot);
794 return;
795 }
796
797 // We want to look at dimension in terms of integer components,
798 // but dimLength is given in terms of bytes.
799 dimLength /= sizeof(int);
800
801 // Only a single dimension is currently supported.
802 rsAssert(dimLength == 1);
803 if (dimLength == 1) {
804 // First do the increment loop.
805 size_t stride = elem->getSizeBytes();
806 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700807 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800808 elem->incRefs(cVal);
809 cVal += stride;
810 }
811
812 // Decrement loop comes after (to prevent race conditions).
813 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700814 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800815 elem->decRefs(oldVal);
816 oldVal += stride;
817 }
818 }
819
820 memcpy(destPtr, data, dataLength);
821}
822
823void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
824
825 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800826 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800827
Yang Nid9bae682015-01-20 15:31:15 -0800828 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800829 if (!destPtr) {
830 //ALOGV("Calling setVar on slot = %i which is null", slot);
831 return;
832 }
833
Chris Wailes44bef6f2014-08-12 13:51:10 -0700834 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800835 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800836 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800837 ptr = data->mHal.drvState.lod[0].mallocPtr;
838 }
839 memcpy(destPtr, &ptr, sizeof(void *));
840}
841
842void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
843
844 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800845 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800846
Yang Nid9bae682015-01-20 15:31:15 -0800847 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800848 if (!destPtr) {
849 //ALOGV("Calling setVar on slot = %i which is null", slot);
850 return;
851 }
852
Jason Sams05ef73f2014-08-05 14:59:22 -0700853 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800854}
855
Yang Ni062c2872015-02-20 15:20:00 -0800856const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
857 return mScriptExec->getFieldName(slot);
858}
859
Jason Sams709a0972012-11-15 18:18:04 -0800860RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -0700861#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -0800862 if (mCompilerDriver) {
863 delete mCompilerDriver;
864 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800865#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800866
Yang Nid9bae682015-01-20 15:31:15 -0800867 if (mScriptExec != nullptr) {
868 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800869 }
Jason Sams110f1812013-03-14 16:02:18 -0700870 if (mBoundAllocs) delete[] mBoundAllocs;
871 if (mScriptSO) {
872 dlclose(mScriptSO);
873 }
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}