blob: 8c6e584333ecc4fb5fe0f02d7278d9d3e4c8ebf9 [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
Stephen Hines82e0a672014-05-05 15:40:56 -070028 #include <bcc/Config/Config.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>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070035 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080036
37 #include <string>
38 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070039#endif
Jason Sams709a0972012-11-15 18:18:04 -080040
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080041#include <set>
42#include <string>
43#include <dlfcn.h>
44#include <stdlib.h>
45#include <string.h>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080046#include <iostream>
Yang Nicb170152015-04-16 10:27:02 -070047#include <sstream>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080048
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 {
Stephen Hines8409d642015-04-28 18:49:56 -070056
57static const bool kDebugGlobalVariables = false;
58
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080059#ifndef RS_COMPATIBILITY_LIB
60
Stephen Hinesba17ae42013-06-05 17:18:04 -070061static bool is_force_recompile() {
62#ifdef RS_SERVER
63 return false;
64#else
65 char buf[PROPERTY_VALUE_MAX];
66
67 // Re-compile if floating point precision has been overridden.
68 property_get("debug.rs.precision", buf, "");
69 if (buf[0] != '\0') {
70 return true;
71 }
72
73 // Re-compile if debug.rs.forcerecompile is set.
74 property_get("debug.rs.forcerecompile", buf, "0");
75 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
76 return true;
77 } else {
78 return false;
79 }
80#endif // RS_SERVER
81}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070082
Chris Wailes6847e732014-08-11 17:30:51 -070083static void setCompileArguments(std::vector<const char*>* args,
84 const std::string& bcFileName,
85 const char* cacheDir, const char* resName,
86 const char* core_lib, bool useRSDebugContext,
Stephen Hines8409d642015-04-28 18:49:56 -070087 const char* bccPluginName, bool emitGlobalInfo,
88 bool emitGlobalInfoSkipConstant) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070089 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080090 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080091 args->push_back("-unroll-runtime");
92 args->push_back("-scalarize-load-store");
Stephen Hines8409d642015-04-28 18:49:56 -070093 if (emitGlobalInfo) {
94 args->push_back("-rs-global-info");
95 if (emitGlobalInfoSkipConstant) {
96 args->push_back("-rs-global-info-skip-constant");
97 }
98 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070099 args->push_back("-o");
100 args->push_back(resName);
101 args->push_back("-output_path");
102 args->push_back(cacheDir);
103 args->push_back("-bclib");
104 args->push_back(core_lib);
105 args->push_back("-mtriple");
106 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
107
Tim Murray358ffb82014-12-09 11:53:06 -0800108 // Enable workaround for A53 codegen by default.
109#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
110 args->push_back("-aarch64-fix-cortex-a53-835769");
111#endif
112
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700113 // Execute the bcc compiler.
114 if (useRSDebugContext) {
115 args->push_back("-rs-debug-ctx");
116 } else {
117 // Only load additional libraries for compiles that don't use
118 // the debug context.
119 if (bccPluginName && strlen(bccPluginName) > 0) {
120 args->push_back("-load");
121 args->push_back(bccPluginName);
122 }
123 }
124
Stephen Hines45e753a2015-01-19 20:58:44 -0800125 args->push_back("-fPIC");
126 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800127
Chris Wailes6847e732014-08-11 17:30:51 -0700128 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700129 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700130}
131
Chris Wailes6847e732014-08-11 17:30:51 -0700132static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700133 const char *bitcode,
134 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700135 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700136 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700137
Chris Wailes6847e732014-08-11 17:30:51 -0700138 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700139 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700140 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700141 return false;
142 }
143 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
144 fclose(bcfile);
145 if (nwritten != bitcodeSize) {
146 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700147 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700148 return false;
149 }
150
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700151 return android::renderscript::rsuExecuteCommand(
152 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
153 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700154}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700155
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800156bool isChecksumNeeded() {
157 char buf[PROPERTY_VALUE_MAX];
158 property_get("ro.debuggable", buf, "");
159 return (buf[0] == '1');
160}
161
162bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
163 int FD = open(fileName, O_RDONLY);
164 if (FD == -1) {
165 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
166 return false;
167 }
168
169 char buf[256];
170 while (true) {
171 ssize_t nread = read(FD, buf, sizeof(buf));
172 if (nread < 0) { // bail out on failed read
173 ALOGE("Error while computing checksum for file \'%s\'", fileName);
174 return false;
175 }
176
177 checksum = adler32(checksum, (const unsigned char *) buf, nread);
178 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
179 break;
180 }
181
Elliott Hughes2df57672015-05-15 17:06:47 -0700182 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800183 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
184 return false;
185 }
186 return true;
187}
Yang Nicb170152015-04-16 10:27:02 -0700188
189#endif // !defined(RS_COMPATIBILITY_LIB)
190} // namespace
191
192namespace android {
193namespace renderscript {
194
195#ifndef RS_COMPATIBILITY_LIB
196
197uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
198 const char *commandLine,
199 const char** bccFiles, size_t numFiles) {
200 uint32_t checksum = adler32(0L, Z_NULL, 0);
201
202 // include checksum of bitcode
203 if (bitcode != nullptr && bitcodeSize > 0) {
204 checksum = adler32(checksum, bitcode, bitcodeSize);
205 }
206
207 // include checksum of command line arguments
208 checksum = adler32(checksum, (const unsigned char *) commandLine,
209 strlen(commandLine));
210
211 // include checksum of bccFiles
212 for (size_t i = 0; i < numFiles; i++) {
213 const char* bccFile = bccFiles[i];
214 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
215 // return empty checksum instead of something partial/corrupt
216 return 0;
217 }
218 }
219
220 return checksum;
221}
222
Yang Nif02a2b02015-04-07 16:00:31 -0700223#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800224
Jason Sams709a0972012-11-15 18:18:04 -0800225RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
226 mCtx = ctx;
227 mScript = s;
228
Chris Wailes44bef6f2014-08-12 13:51:10 -0700229 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800230
Chris Wailes44bef6f2014-08-12 13:51:10 -0700231 mRoot = nullptr;
232 mRootExpand = nullptr;
233 mInit = nullptr;
234 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800235 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800236
Chris Wailes44bef6f2014-08-12 13:51:10 -0700237 mBoundAllocs = nullptr;
238 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800239 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800240
Yang Nicb170152015-04-16 10:27:02 -0700241 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800242 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800243}
244
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800245bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800246 // The shared object may have an invalid build checksum.
247 // Validate and fail early.
248 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Nicb170152015-04-16 10:27:02 -0700249 mCtx->getContext(), mScriptSO,
250 mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800251
252 if (mScriptExec == nullptr) {
253 return false;
254 }
255
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800256 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
257 if (mRoot) {
258 //ALOGE("Found root(): %p", mRoot);
259 }
260 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
261 if (mRootExpand) {
262 //ALOGE("Found root.expand(): %p", mRootExpand);
263 }
264 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
265 if (mInit) {
266 //ALOGE("Found init(): %p", mInit);
267 }
268 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
269 if (mFreeChildren) {
270 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
271 }
272
Yang Nid9bae682015-01-20 15:31:15 -0800273 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800274 if (varCount > 0) {
275 mBoundAllocs = new Allocation *[varCount];
276 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
277 }
278
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800279 mIsThreadable = mScriptExec->getThreadable();
280 //ALOGE("Script isThreadable? %d", mIsThreadable);
281
Stephen Hines8409d642015-04-28 18:49:56 -0700282 if (kDebugGlobalVariables) {
283 mScriptExec->dumpGlobalInfo();
284 }
285
Yang Nid9bae682015-01-20 15:31:15 -0800286 return true;
287}
288
Jason Sams709a0972012-11-15 18:18:04 -0800289bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
290 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800291 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800292 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
293 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800294 //ALOGE("rsdScriptInit %p %p", rsc, script);
295
296 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700297#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800298 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800299
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700300 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
301 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700302 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700303 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700304 return false;
305 }
306
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700307 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700308
309 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800310 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700311 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700312
Chris Wailes6847e732014-08-11 17:30:51 -0700313 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700314 bcFileName.append("/");
315 bcFileName.append(resName);
316 bcFileName.append(".bc");
317
318 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700319 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
320 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700321 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700322 useRSDebugContext, bccPluginName, emitGlobalInfo,
323 emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800324
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800325 mChecksumNeeded = isChecksumNeeded();
326 if (mChecksumNeeded) {
327 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
328 core_lib,
329 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700330
331 // The last argument of compileArguments is a nullptr, so remove 1 from
332 // the size.
333 std::unique_ptr<const char> compileCommandLine(
334 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
335
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800336 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
337 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700338 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800339
Yang Nicb170152015-04-16 10:27:02 -0700340 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800341 // cannot compute checksum but verification is enabled
342 mCtx->unlockMutex();
343 return false;
344 }
345 }
346 else {
347 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700348 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800349 }
350
351 // Append build checksum to commandline
352 // Handle the terminal nullptr in compileArguments
353 compileArguments.pop_back();
354 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700355 std::stringstream ss;
356 ss << std::hex << mBuildChecksum;
357 compileArguments.push_back(ss.str().c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800358 compileArguments.push_back(nullptr);
359
Tim Murraybf96a522015-01-23 15:37:03 -0800360 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800361 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800362
363 // Read RS info from the shared object to detect checksum mismatch
364 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
365 dlclose(mScriptSO);
366 mScriptSO = nullptr;
367 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700368 }
369
370 // If we can't, it's either not there or out of date. We compile the bit code and try loading
371 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800372 if (mScriptSO == nullptr) {
373 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700374 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800375 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700376 ALOGE("bcc: FAILS to compile '%s'", resName);
377 mCtx->unlockMutex();
378 return false;
379 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800380
Stephen Hines4c368af2015-05-06 00:43:02 -0700381 if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
382 cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800383 ALOGE("Linker: Failed to link object file '%s'", resName);
384 mCtx->unlockMutex();
385 return false;
386 }
387
Yang Ni1c44cb62015-01-22 12:02:27 -0800388 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800389 if (mScriptSO == nullptr) {
390 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700391 mCtx->unlockMutex();
392 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700393 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800394
395 // Read RS symbol information from the .so.
396 if (!storeRSInfoFromSO()) {
397 goto error;
398 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700399 }
Jason Sams709a0972012-11-15 18:18:04 -0800400
Yang Nic31585b2015-02-15 11:43:50 -0800401 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800402
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700403#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800404 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
405 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700406
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800407 if (!mScriptSO) {
408 goto error;
409 }
Jason Sams110f1812013-03-14 16:02:18 -0700410
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800411 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700412 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700413 }
414#endif
Jason Sams709a0972012-11-15 18:18:04 -0800415 mCtx->unlockMutex();
416 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700417
Jason Sams110f1812013-03-14 16:02:18 -0700418error:
419
420 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700421 if (mScriptSO) {
422 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800423 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700424 }
425 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800426}
427
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700428#ifndef RS_COMPATIBILITY_LIB
429
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700430const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
431 size_t bitcodeSize) {
432 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
433
434 // If we're debugging, use the debug library.
435 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
436 return SYSLIBPATH"/libclcore_debug.bc";
437 }
438
439 // If a callback has been registered to specify a library, use that.
440 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700441 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700442 return selectRTCallback((const char*)bitcode, bitcodeSize);
443 }
444
445 // Check for a platform specific library
446#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
447 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700448 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700449 // NEON-capable ARMv7a devices can use an accelerated math library
450 // for all reduced precision scripts.
451 // ARMv8 does not use NEON, as ASIMD can be used with all precision
452 // levels.
453 return SYSLIBPATH"/libclcore_neon.bc";
454 } else {
455 return defaultLib;
456 }
457#elif defined(__i386__) || defined(__x86_64__)
458 // x86 devices will use an optimized library.
459 return SYSLIBPATH"/libclcore_x86.bc";
460#else
461 return defaultLib;
462#endif
463}
464
465#endif
466
Jason Sams709a0972012-11-15 18:18:04 -0800467void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700468 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800469 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
470 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800471 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800472 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
473 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700474
475 // Bug, need to stash in metadata
476 if (mRootExpand) {
477 script->mHal.info.root = mRootExpand;
478 } else {
479 script->mHal.info.root = mRoot;
480 }
Jason Sams709a0972012-11-15 18:18:04 -0800481}
482
Jason Sams709a0972012-11-15 18:18:04 -0800483
Jason Samsbf2111d2015-01-26 18:13:41 -0800484bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700485 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700486 Allocation * aout,
487 const void * usr, uint32_t usrLen,
488 const RsScriptCall *sc,
489 MTLaunchStruct *mtls) {
490
491 memset(mtls, 0, sizeof(MTLaunchStruct));
492
Chris Wailesf3712132014-07-16 15:18:30 -0700493 for (int index = inLen; --index >= 0;) {
494 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700495
Chris Wailesf3712132014-07-16 15:18:30 -0700496 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700497 if (ain != nullptr &&
498 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
499
Chris Wailesf3712132014-07-16 15:18:30 -0700500 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
501 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800502 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700503 }
504 }
505
Chris Wailes44bef6f2014-08-12 13:51:10 -0700506 if (aout &&
507 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
508
Chris Wailesf3712132014-07-16 15:18:30 -0700509 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
510 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800511 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700512 }
513
Chris Wailesf3712132014-07-16 15:18:30 -0700514 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700515 const Allocation *ain0 = ains[0];
516 const Type *inType = ain0->getType();
517
Jason Samsc0d68472015-01-20 14:29:52 -0800518 mtls->fep.dim.x = inType->getDimX();
519 mtls->fep.dim.y = inType->getDimY();
520 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700521
522 for (int Index = inLen; --Index >= 1;) {
523 if (!ain0->hasSameDims(ains[Index])) {
524 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800525 "Failed to launch kernel; dimensions of input and output"
526 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700527
Jason Samsbf2111d2015-01-26 18:13:41 -0800528 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700529 }
530 }
531
Chris Wailes44bef6f2014-08-12 13:51:10 -0700532 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700533 const Type *outType = aout->getType();
534
Jason Samsc0d68472015-01-20 14:29:52 -0800535 mtls->fep.dim.x = outType->getDimX();
536 mtls->fep.dim.y = outType->getDimY();
537 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700538
Jason Samsa9139c72015-04-16 15:11:04 -0700539 } else if (sc != nullptr) {
540 mtls->fep.dim.x = sc->xEnd;
541 mtls->fep.dim.y = sc->yEnd;
542 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700543 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700544 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
545 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800546 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700547 }
548
Chris Wailes44bef6f2014-08-12 13:51:10 -0700549 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700550 if (!ains[0]->hasSameDims(aout)) {
551 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
552 "Failed to launch kernel; dimensions of input and output allocations do not match.");
553
Jason Samsbf2111d2015-01-26 18:13:41 -0800554 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700555 }
556 }
557
558 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800559 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700560 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800561 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
562 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700563 if (mtls->start.x >= mtls->end.x) {
564 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
565 "Failed to launch kernel; Invalid xStart or xEnd.");
566 return false;
567 }
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);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700575 if (mtls->start.y >= mtls->end.y) {
576 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
577 "Failed to launch kernel; Invalid yStart or yEnd.");
578 return false;
579 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700580 }
581
582 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800583 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700584 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800585 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
586 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700587 if (mtls->start.z >= mtls->end.z) {
588 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
589 "Failed to launch kernel; Invalid zStart or zEnd.");
590 return false;
591 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700592 }
593
Jason Samsbf2111d2015-01-26 18:13:41 -0800594 if (!sc || (sc->arrayEnd == 0)) {
595 mtls->end.array[0] = mtls->fep.dim.array[0];
596 } else {
597 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
598 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700599 if (mtls->start.array[0] >= mtls->end.array[0]) {
600 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
601 "Failed to launch kernel; Invalid arrayStart or arrayEnd.");
602 return false;
603 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800604 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700605
Jason Samsbf2111d2015-01-26 18:13:41 -0800606 if (!sc || (sc->array2End == 0)) {
607 mtls->end.array[1] = mtls->fep.dim.array[1];
608 } else {
609 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
610 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700611 if (mtls->start.array[1] >= mtls->end.array[1]) {
612 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
613 "Failed to launch kernel; Invalid array2Start or array2End.");
614 return false;
615 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800616 }
617
618 if (!sc || (sc->array3End == 0)) {
619 mtls->end.array[2] = mtls->fep.dim.array[2];
620 } else {
621 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
622 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700623 if (mtls->start.array[2] >= mtls->end.array[2]) {
624 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
625 "Failed to launch kernel; Invalid array3Start or array3End.");
626 return false;
627 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800628 }
629
630 if (!sc || (sc->array4End == 0)) {
631 mtls->end.array[3] = mtls->fep.dim.array[3];
632 } else {
633 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
634 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700635 if (mtls->start.array[3] >= mtls->end.array[3]) {
636 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
637 "Failed to launch kernel; Invalid array4Start or array4End.");
638 return false;
639 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800640 }
641
642
643 // The X & Y walkers always want 0-1 min even if dim is not present
644 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
645 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700646
647 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800648 if (ains) {
649 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
650 }
651 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700652 mtls->fep.usr = usr;
653 mtls->fep.usrLen = usrLen;
654 mtls->mSliceSize = 1;
655 mtls->mSliceNum = 0;
656
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700657 mtls->isThreadable = mIsThreadable;
658
Chris Wailesf3712132014-07-16 15:18:30 -0700659 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700660 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700661 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800662 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
663 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700664 }
665 }
666
Chris Wailes44bef6f2014-08-12 13:51:10 -0700667 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800668 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
669 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700670 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800671
672 // All validation passed, ok to launch threads
673 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700674}
675
Jason Sams709a0972012-11-15 18:18:04 -0800676
677void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700678 const Allocation ** ains,
679 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800680 Allocation * aout,
681 const void * usr,
682 uint32_t usrLen,
683 const RsScriptCall *sc) {
684
685 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700686
Jason Samsbf2111d2015-01-26 18:13:41 -0800687 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
688 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700689
Jason Samsbf2111d2015-01-26 18:13:41 -0800690 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
691 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
692 mCtx->setTLS(oldTLS);
693 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700694}
695
Jason Sams709a0972012-11-15 18:18:04 -0800696void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800697 mtls->script = this;
698 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800699 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700700 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800701 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800702}
703
704int RsdCpuScriptImpl::invokeRoot() {
705 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
706 int ret = mRoot();
707 mCtx->setTLS(oldTLS);
708 return ret;
709}
710
711void RsdCpuScriptImpl::invokeInit() {
712 if (mInit) {
713 mInit();
714 }
715}
716
717void RsdCpuScriptImpl::invokeFreeChildren() {
718 if (mFreeChildren) {
719 mFreeChildren();
720 }
721}
722
723void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
724 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800725 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800726 void * ap = nullptr;
727
728#if defined(__x86_64__)
729 // The invoked function could have input parameter of vector type for example float4 which
730 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
731 // So try to align void* params before passing them into RS exported function.
732
733 if ((uint8_t)(uint64_t)params & 0x0F) {
734 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
735 memcpy(ap, params, paramLength);
736 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800737 ALOGE("x86_64: invokeFunction memalign error, still use params which"
738 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800739 }
740 }
741#endif
Jason Sams709a0972012-11-15 18:18:04 -0800742
743 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800744 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800745 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800746
Yong Chenc246d912015-05-22 18:20:06 +0800747#if defined(__x86_64__)
748 free(ap);
749#endif
750
Jason Sams709a0972012-11-15 18:18:04 -0800751 mCtx->setTLS(oldTLS);
752}
753
754void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
755 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800756 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800757
758 //if (mIntrinsicID) {
759 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
760 //return;
761 //}
762
Yang Nid9bae682015-01-20 15:31:15 -0800763 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800764 if (!destPtr) {
765 //ALOGV("Calling setVar on slot = %i which is null", slot);
766 return;
767 }
768
769 memcpy(destPtr, data, dataLength);
770}
771
Tim Murray9c642392013-04-11 13:29:59 -0700772void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
773 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800774 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700775
Yang Nid9bae682015-01-20 15:31:15 -0800776 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700777 if (!srcPtr) {
778 //ALOGV("Calling setVar on slot = %i which is null", slot);
779 return;
780 }
781 memcpy(data, srcPtr, dataLength);
782}
783
784
Jason Sams709a0972012-11-15 18:18:04 -0800785void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
786 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700787 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800788 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800789 if (!destPtr) {
790 //ALOGV("Calling setVar on slot = %i which is null", slot);
791 return;
792 }
793
794 // We want to look at dimension in terms of integer components,
795 // but dimLength is given in terms of bytes.
796 dimLength /= sizeof(int);
797
798 // Only a single dimension is currently supported.
799 rsAssert(dimLength == 1);
800 if (dimLength == 1) {
801 // First do the increment loop.
802 size_t stride = elem->getSizeBytes();
803 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700804 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800805 elem->incRefs(cVal);
806 cVal += stride;
807 }
808
809 // Decrement loop comes after (to prevent race conditions).
810 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700811 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800812 elem->decRefs(oldVal);
813 oldVal += stride;
814 }
815 }
816
817 memcpy(destPtr, data, dataLength);
818}
819
820void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
821
822 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800823 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800824
Yang Nid9bae682015-01-20 15:31:15 -0800825 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800826 if (!destPtr) {
827 //ALOGV("Calling setVar on slot = %i which is null", slot);
828 return;
829 }
830
Chris Wailes44bef6f2014-08-12 13:51:10 -0700831 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800832 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800833 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800834 ptr = data->mHal.drvState.lod[0].mallocPtr;
835 }
836 memcpy(destPtr, &ptr, sizeof(void *));
837}
838
839void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
840
841 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800842 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800843
Yang Nid9bae682015-01-20 15:31:15 -0800844 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800845 if (!destPtr) {
846 //ALOGV("Calling setVar on slot = %i which is null", slot);
847 return;
848 }
849
Jason Sams05ef73f2014-08-05 14:59:22 -0700850 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800851}
852
Yang Ni062c2872015-02-20 15:20:00 -0800853const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
854 return mScriptExec->getFieldName(slot);
855}
856
Jason Sams709a0972012-11-15 18:18:04 -0800857RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Nid9bae682015-01-20 15:31:15 -0800858 if (mScriptExec != nullptr) {
859 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800860 }
Jason Sams110f1812013-03-14 16:02:18 -0700861 if (mBoundAllocs) delete[] mBoundAllocs;
862 if (mScriptSO) {
863 dlclose(mScriptSO);
864 }
Jason Sams709a0972012-11-15 18:18:04 -0800865}
866
867Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
868 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700869 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800870 }
871
872 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
873 Allocation *a = mBoundAllocs[ct];
874 if (!a) continue;
875 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
876 return a;
877 }
878 }
879 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700880 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800881}
882
Stephen Hines8409d642015-04-28 18:49:56 -0700883int RsdCpuScriptImpl::getGlobalEntries() const {
884 return mScriptExec->getGlobalEntries();
885}
886
887const char * RsdCpuScriptImpl::getGlobalName(int i) const {
888 return mScriptExec->getGlobalName(i);
889}
890
891const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
892 return mScriptExec->getGlobalAddress(i);
893}
894
895size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
896 return mScriptExec->getGlobalSize(i);
897}
898
Stephen Hines5aa018c2015-05-20 18:09:57 -0700899uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
900 return mScriptExec->getGlobalProperties(i);
901}
902
Chris Wailesf3712132014-07-16 15:18:30 -0700903void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
904 uint32_t inLen, Allocation * aout,
905 const void * usr, uint32_t usrLen,
906 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700907
Chris Wailesf3712132014-07-16 15:18:30 -0700908void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
909 uint32_t inLen, Allocation * aout,
910 const void * usr, uint32_t usrLen,
911 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700912
Jason Sams709a0972012-11-15 18:18:04 -0800913
914}
915}