blob: 09e7ab79d04b4ccbaecc26359080c8f00eee74fb [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
Stephen Hinesba17ae42013-06-05 17:18:04 -070049namespace {
Stephen Hines8409d642015-04-28 18:49:56 -070050
51static const bool kDebugGlobalVariables = false;
52
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080053#ifndef RS_COMPATIBILITY_LIB
54
Stephen Hinesba17ae42013-06-05 17:18:04 -070055static bool is_force_recompile() {
56#ifdef RS_SERVER
57 return false;
58#else
59 char buf[PROPERTY_VALUE_MAX];
60
61 // Re-compile if floating point precision has been overridden.
62 property_get("debug.rs.precision", buf, "");
63 if (buf[0] != '\0') {
64 return true;
65 }
66
67 // Re-compile if debug.rs.forcerecompile is set.
68 property_get("debug.rs.forcerecompile", buf, "0");
69 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
70 return true;
71 } else {
72 return false;
73 }
74#endif // RS_SERVER
75}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070076
Chris Wailes6847e732014-08-11 17:30:51 -070077static void setCompileArguments(std::vector<const char*>* args,
78 const std::string& bcFileName,
79 const char* cacheDir, const char* resName,
80 const char* core_lib, bool useRSDebugContext,
Stephen Hines8409d642015-04-28 18:49:56 -070081 const char* bccPluginName, bool emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +010082 int optLevel, bool emitGlobalInfoSkipConstant) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070083 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080084 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080085 args->push_back("-unroll-runtime");
86 args->push_back("-scalarize-load-store");
Stephen Hines8409d642015-04-28 18:49:56 -070087 if (emitGlobalInfo) {
88 args->push_back("-rs-global-info");
89 if (emitGlobalInfoSkipConstant) {
90 args->push_back("-rs-global-info-skip-constant");
91 }
92 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070093 args->push_back("-o");
94 args->push_back(resName);
95 args->push_back("-output_path");
96 args->push_back(cacheDir);
97 args->push_back("-bclib");
98 args->push_back(core_lib);
99 args->push_back("-mtriple");
100 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
verena beckhamf5029802015-05-22 16:51:42 +0100101 args->push_back("-O");
102
103 switch (optLevel) {
104 case (0):
105 args->push_back("0");
106 break;
107 case (3):
108 args->push_back("3");
109 break;
110 default:
111 ALOGW("Expected optimization level of 0 or 3. Received %d", optLevel);
112 args->push_back("3");
113 break;
114 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700115
Tim Murray358ffb82014-12-09 11:53:06 -0800116 // Enable workaround for A53 codegen by default.
117#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
118 args->push_back("-aarch64-fix-cortex-a53-835769");
119#endif
120
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700121 // Execute the bcc compiler.
122 if (useRSDebugContext) {
123 args->push_back("-rs-debug-ctx");
124 } else {
125 // Only load additional libraries for compiles that don't use
126 // the debug context.
127 if (bccPluginName && strlen(bccPluginName) > 0) {
128 args->push_back("-load");
129 args->push_back(bccPluginName);
130 }
131 }
132
Stephen Hines45e753a2015-01-19 20:58:44 -0800133 args->push_back("-fPIC");
134 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800135
Chris Wailes6847e732014-08-11 17:30:51 -0700136 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700137 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700138}
139
Chris Wailes6847e732014-08-11 17:30:51 -0700140static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700141 const char *bitcode,
142 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700143 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700144 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700145
Chris Wailes6847e732014-08-11 17:30:51 -0700146 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700147 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700148 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700149 return false;
150 }
151 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
152 fclose(bcfile);
153 if (nwritten != bitcodeSize) {
154 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700155 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700156 return false;
157 }
158
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700159 return android::renderscript::rsuExecuteCommand(
160 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
161 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700162}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700163
Stephen Hines6a236ad2015-06-22 14:51:30 -0700164// The checksum is unnecessary under a few conditions, since the primary
165// use-case for it is debugging. If we are loading something from the
166// system partition (read-only), we know that it was precompiled as part of
167// application ahead of time (and thus the checksum is completely
168// unnecessary). The checksum is also unnecessary on release (non-debug)
169// builds, as the only way to get a shared object is to have compiled the
170// script once already. On a release build, there is no way to adjust the
171// other libraries/dependencies, and so the only reason to recompile would
172// be for a source APK change or an OTA. In either case, the APK would be
173// reinstalled, which would already clear the code_cache/ directory.
174bool isChecksumNeeded(const char *cacheDir) {
175 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
176 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
177 return false;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800178 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
Elliott Hughes2df57672015-05-15 17:06:47 -0700203 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800204 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
205 return false;
206 }
207 return true;
208}
Yang Nicb170152015-04-16 10:27:02 -0700209
210#endif // !defined(RS_COMPATIBILITY_LIB)
211} // namespace
212
213namespace android {
214namespace renderscript {
215
216#ifndef RS_COMPATIBILITY_LIB
217
218uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
219 const char *commandLine,
220 const char** bccFiles, size_t numFiles) {
221 uint32_t checksum = adler32(0L, Z_NULL, 0);
222
223 // include checksum of bitcode
224 if (bitcode != nullptr && bitcodeSize > 0) {
225 checksum = adler32(checksum, bitcode, bitcodeSize);
226 }
227
228 // include checksum of command line arguments
229 checksum = adler32(checksum, (const unsigned char *) commandLine,
230 strlen(commandLine));
231
232 // include checksum of bccFiles
233 for (size_t i = 0; i < numFiles; i++) {
234 const char* bccFile = bccFiles[i];
235 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
236 // return empty checksum instead of something partial/corrupt
237 return 0;
238 }
239 }
240
241 return checksum;
242}
243
Yang Nif02a2b02015-04-07 16:00:31 -0700244#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800245
Jason Sams709a0972012-11-15 18:18:04 -0800246RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
247 mCtx = ctx;
248 mScript = s;
249
Chris Wailes44bef6f2014-08-12 13:51:10 -0700250 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800251
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
Yang Nicb170152015-04-16 10:27:02 -0700262 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800263 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(
Yang Nicb170152015-04-16 10:27:02 -0700270 mCtx->getContext(), mScriptSO,
271 mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800272
273 if (mScriptExec == nullptr) {
274 return false;
275 }
276
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800277 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
278 if (mRoot) {
279 //ALOGE("Found root(): %p", mRoot);
280 }
281 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
282 if (mRootExpand) {
283 //ALOGE("Found root.expand(): %p", mRootExpand);
284 }
285 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
286 if (mInit) {
287 //ALOGE("Found init(): %p", mInit);
288 }
289 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
290 if (mFreeChildren) {
291 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
292 }
293
Yang Nid9bae682015-01-20 15:31:15 -0800294 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800295 if (varCount > 0) {
296 mBoundAllocs = new Allocation *[varCount];
297 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
298 }
299
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800300 mIsThreadable = mScriptExec->getThreadable();
301 //ALOGE("Script isThreadable? %d", mIsThreadable);
302
Stephen Hines8409d642015-04-28 18:49:56 -0700303 if (kDebugGlobalVariables) {
304 mScriptExec->dumpGlobalInfo();
305 }
306
Yang Nid9bae682015-01-20 15:31:15 -0800307 return true;
308}
309
Jason Sams709a0972012-11-15 18:18:04 -0800310bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
311 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800312 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800313 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
314 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800315 //ALOGE("rsdScriptInit %p %p", rsc, script);
316
317 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700318#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800319 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800320
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700321 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
322 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700323 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700324 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700325 return false;
326 }
327
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700328 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700329
330 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800331 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700332 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700333
verena beckhamf5029802015-05-22 16:51:42 +0100334 int optLevel = mCtx->getContext()->getOptLevel();
335
Chris Wailes6847e732014-08-11 17:30:51 -0700336 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700337 bcFileName.append("/");
338 bcFileName.append(resName);
339 bcFileName.append(".bc");
340
341 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700342 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
343 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700344 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700345 useRSDebugContext, bccPluginName, emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +0100346 optLevel, emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800347
Stephen Hines6a236ad2015-06-22 14:51:30 -0700348 mChecksumNeeded = isChecksumNeeded(cacheDir);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800349 if (mChecksumNeeded) {
350 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
351 core_lib,
352 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700353
354 // The last argument of compileArguments is a nullptr, so remove 1 from
355 // the size.
356 std::unique_ptr<const char> compileCommandLine(
357 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
358
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800359 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
360 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700361 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800362
Yang Nicb170152015-04-16 10:27:02 -0700363 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800364 // cannot compute checksum but verification is enabled
365 mCtx->unlockMutex();
366 return false;
367 }
368 }
369 else {
370 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700371 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800372 }
373
374 // Append build checksum to commandline
375 // Handle the terminal nullptr in compileArguments
376 compileArguments.pop_back();
377 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700378 std::stringstream ss;
379 ss << std::hex << mBuildChecksum;
380 compileArguments.push_back(ss.str().c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800381 compileArguments.push_back(nullptr);
382
Tim Murraybf96a522015-01-23 15:37:03 -0800383 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800384 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800385
386 // Read RS info from the shared object to detect checksum mismatch
387 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
388 dlclose(mScriptSO);
389 mScriptSO = nullptr;
390 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700391 }
392
393 // If we can't, it's either not there or out of date. We compile the bit code and try loading
394 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800395 if (mScriptSO == nullptr) {
396 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700397 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800398 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700399 ALOGE("bcc: FAILS to compile '%s'", resName);
400 mCtx->unlockMutex();
401 return false;
402 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800403
Stephen Hines4c368af2015-05-06 00:43:02 -0700404 if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
405 cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800406 ALOGE("Linker: Failed to link object file '%s'", resName);
407 mCtx->unlockMutex();
408 return false;
409 }
410
Yang Ni1c44cb62015-01-22 12:02:27 -0800411 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800412 if (mScriptSO == nullptr) {
413 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700414 mCtx->unlockMutex();
415 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700416 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800417
418 // Read RS symbol information from the .so.
419 if (!storeRSInfoFromSO()) {
420 goto error;
421 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700422 }
Jason Sams709a0972012-11-15 18:18:04 -0800423
Yang Nic31585b2015-02-15 11:43:50 -0800424 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800425
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700426#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800427 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
428 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700429
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800430 if (!mScriptSO) {
431 goto error;
432 }
Jason Sams110f1812013-03-14 16:02:18 -0700433
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800434 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700435 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700436 }
437#endif
Jason Sams709a0972012-11-15 18:18:04 -0800438 mCtx->unlockMutex();
439 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700440
Jason Sams110f1812013-03-14 16:02:18 -0700441error:
442
443 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700444 if (mScriptSO) {
445 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800446 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700447 }
448 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800449}
450
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700451#ifndef RS_COMPATIBILITY_LIB
452
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700453const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
454 size_t bitcodeSize) {
455 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
456
457 // If we're debugging, use the debug library.
458 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
459 return SYSLIBPATH"/libclcore_debug.bc";
460 }
461
462 // If a callback has been registered to specify a library, use that.
463 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700464 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700465 return selectRTCallback((const char*)bitcode, bitcodeSize);
466 }
467
468 // Check for a platform specific library
469#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
470 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700471 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700472 // NEON-capable ARMv7a devices can use an accelerated math library
473 // for all reduced precision scripts.
474 // ARMv8 does not use NEON, as ASIMD can be used with all precision
475 // levels.
476 return SYSLIBPATH"/libclcore_neon.bc";
477 } else {
478 return defaultLib;
479 }
480#elif defined(__i386__) || defined(__x86_64__)
481 // x86 devices will use an optimized library.
482 return SYSLIBPATH"/libclcore_x86.bc";
483#else
484 return defaultLib;
485#endif
486}
487
488#endif
489
Jason Sams709a0972012-11-15 18:18:04 -0800490void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700491 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800492 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
493 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800494 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800495 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
496 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700497
498 // Bug, need to stash in metadata
499 if (mRootExpand) {
500 script->mHal.info.root = mRootExpand;
501 } else {
502 script->mHal.info.root = mRoot;
503 }
Jason Sams709a0972012-11-15 18:18:04 -0800504}
505
Jason Sams709a0972012-11-15 18:18:04 -0800506
Jason Samsbf2111d2015-01-26 18:13:41 -0800507bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700508 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700509 Allocation * aout,
510 const void * usr, uint32_t usrLen,
511 const RsScriptCall *sc,
512 MTLaunchStruct *mtls) {
513
514 memset(mtls, 0, sizeof(MTLaunchStruct));
515
Chris Wailesf3712132014-07-16 15:18:30 -0700516 for (int index = inLen; --index >= 0;) {
517 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700518
Chris Wailesf3712132014-07-16 15:18:30 -0700519 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700520 if (ain != nullptr &&
521 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
522
Chris Wailesf3712132014-07-16 15:18:30 -0700523 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
524 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800525 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700526 }
527 }
528
Chris Wailes44bef6f2014-08-12 13:51:10 -0700529 if (aout &&
530 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
531
Chris Wailesf3712132014-07-16 15:18:30 -0700532 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
533 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800534 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700535 }
536
Chris Wailesf3712132014-07-16 15:18:30 -0700537 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700538 const Allocation *ain0 = ains[0];
539 const Type *inType = ain0->getType();
540
Jason Samsc0d68472015-01-20 14:29:52 -0800541 mtls->fep.dim.x = inType->getDimX();
542 mtls->fep.dim.y = inType->getDimY();
543 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700544
545 for (int Index = inLen; --Index >= 1;) {
546 if (!ain0->hasSameDims(ains[Index])) {
547 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800548 "Failed to launch kernel; dimensions of input and output"
549 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700550
Jason Samsbf2111d2015-01-26 18:13:41 -0800551 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700552 }
553 }
554
Chris Wailes44bef6f2014-08-12 13:51:10 -0700555 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700556 const Type *outType = aout->getType();
557
Jason Samsc0d68472015-01-20 14:29:52 -0800558 mtls->fep.dim.x = outType->getDimX();
559 mtls->fep.dim.y = outType->getDimY();
560 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700561
Jason Samsa9139c72015-04-16 15:11:04 -0700562 } else if (sc != nullptr) {
563 mtls->fep.dim.x = sc->xEnd;
564 mtls->fep.dim.y = sc->yEnd;
565 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700566 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700567 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
568 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800569 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700570 }
571
Chris Wailes44bef6f2014-08-12 13:51:10 -0700572 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700573 if (!ains[0]->hasSameDims(aout)) {
574 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
575 "Failed to launch kernel; dimensions of input and output allocations do not match.");
576
Jason Samsbf2111d2015-01-26 18:13:41 -0800577 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700578 }
579 }
580
581 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800582 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700583 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800584 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
585 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700586 if (mtls->start.x >= mtls->end.x) {
587 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
588 "Failed to launch kernel; Invalid xStart or xEnd.");
589 return false;
590 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700591 }
592
593 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800594 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700595 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800596 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
597 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700598 if (mtls->start.y >= mtls->end.y) {
599 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
600 "Failed to launch kernel; Invalid yStart or yEnd.");
601 return false;
602 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700603 }
604
605 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800606 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700607 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800608 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
609 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700610 if (mtls->start.z >= mtls->end.z) {
611 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
612 "Failed to launch kernel; Invalid zStart or zEnd.");
613 return false;
614 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700615 }
616
Jason Samsbf2111d2015-01-26 18:13:41 -0800617 if (!sc || (sc->arrayEnd == 0)) {
618 mtls->end.array[0] = mtls->fep.dim.array[0];
619 } else {
620 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
621 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700622 if (mtls->start.array[0] >= mtls->end.array[0]) {
623 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
624 "Failed to launch kernel; Invalid arrayStart or arrayEnd.");
625 return false;
626 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800627 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700628
Jason Samsbf2111d2015-01-26 18:13:41 -0800629 if (!sc || (sc->array2End == 0)) {
630 mtls->end.array[1] = mtls->fep.dim.array[1];
631 } else {
632 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
633 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700634 if (mtls->start.array[1] >= mtls->end.array[1]) {
635 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
636 "Failed to launch kernel; Invalid array2Start or array2End.");
637 return false;
638 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800639 }
640
641 if (!sc || (sc->array3End == 0)) {
642 mtls->end.array[2] = mtls->fep.dim.array[2];
643 } else {
644 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
645 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700646 if (mtls->start.array[2] >= mtls->end.array[2]) {
647 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
648 "Failed to launch kernel; Invalid array3Start or array3End.");
649 return false;
650 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800651 }
652
653 if (!sc || (sc->array4End == 0)) {
654 mtls->end.array[3] = mtls->fep.dim.array[3];
655 } else {
656 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
657 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700658 if (mtls->start.array[3] >= mtls->end.array[3]) {
659 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
660 "Failed to launch kernel; Invalid array4Start or array4End.");
661 return false;
662 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800663 }
664
665
666 // The X & Y walkers always want 0-1 min even if dim is not present
667 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
668 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700669
670 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800671 if (ains) {
672 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
673 }
674 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700675 mtls->fep.usr = usr;
676 mtls->fep.usrLen = usrLen;
677 mtls->mSliceSize = 1;
678 mtls->mSliceNum = 0;
679
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700680 mtls->isThreadable = mIsThreadable;
681
Chris Wailesf3712132014-07-16 15:18:30 -0700682 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700683 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700684 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800685 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
686 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700687 }
688 }
689
Chris Wailes44bef6f2014-08-12 13:51:10 -0700690 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800691 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
692 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700693 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800694
695 // All validation passed, ok to launch threads
696 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700697}
698
Jason Sams709a0972012-11-15 18:18:04 -0800699
700void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700701 const Allocation ** ains,
702 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800703 Allocation * aout,
704 const void * usr,
705 uint32_t usrLen,
706 const RsScriptCall *sc) {
707
708 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700709
Jason Samsbf2111d2015-01-26 18:13:41 -0800710 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
711 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700712
Jason Samsbf2111d2015-01-26 18:13:41 -0800713 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
714 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
715 mCtx->setTLS(oldTLS);
716 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700717}
718
Jason Sams709a0972012-11-15 18:18:04 -0800719void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800720 mtls->script = this;
721 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800722 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700723 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800724 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800725}
726
727int RsdCpuScriptImpl::invokeRoot() {
728 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
729 int ret = mRoot();
730 mCtx->setTLS(oldTLS);
731 return ret;
732}
733
734void RsdCpuScriptImpl::invokeInit() {
735 if (mInit) {
736 mInit();
737 }
738}
739
740void RsdCpuScriptImpl::invokeFreeChildren() {
741 if (mFreeChildren) {
742 mFreeChildren();
743 }
744}
745
746void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
747 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800748 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800749 void * ap = nullptr;
750
751#if defined(__x86_64__)
752 // The invoked function could have input parameter of vector type for example float4 which
753 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
754 // So try to align void* params before passing them into RS exported function.
755
756 if ((uint8_t)(uint64_t)params & 0x0F) {
757 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
758 memcpy(ap, params, paramLength);
759 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800760 ALOGE("x86_64: invokeFunction memalign error, still use params which"
761 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800762 }
763 }
764#endif
Jason Sams709a0972012-11-15 18:18:04 -0800765
766 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800767 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800768 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800769
Yong Chenc246d912015-05-22 18:20:06 +0800770#if defined(__x86_64__)
771 free(ap);
772#endif
773
Jason Sams709a0972012-11-15 18:18:04 -0800774 mCtx->setTLS(oldTLS);
775}
776
777void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
778 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800779 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800780
781 //if (mIntrinsicID) {
782 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
783 //return;
784 //}
785
Yang Nid9bae682015-01-20 15:31:15 -0800786 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800787 if (!destPtr) {
788 //ALOGV("Calling setVar on slot = %i which is null", slot);
789 return;
790 }
791
792 memcpy(destPtr, data, dataLength);
793}
794
Tim Murray9c642392013-04-11 13:29:59 -0700795void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
796 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800797 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700798
Yang Nid9bae682015-01-20 15:31:15 -0800799 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700800 if (!srcPtr) {
801 //ALOGV("Calling setVar on slot = %i which is null", slot);
802 return;
803 }
804 memcpy(data, srcPtr, dataLength);
805}
806
807
Jason Sams709a0972012-11-15 18:18:04 -0800808void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
809 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700810 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800811 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800812 if (!destPtr) {
813 //ALOGV("Calling setVar on slot = %i which is null", slot);
814 return;
815 }
816
817 // We want to look at dimension in terms of integer components,
818 // but dimLength is given in terms of bytes.
819 dimLength /= sizeof(int);
820
821 // Only a single dimension is currently supported.
822 rsAssert(dimLength == 1);
823 if (dimLength == 1) {
824 // First do the increment loop.
825 size_t stride = elem->getSizeBytes();
826 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700827 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800828 elem->incRefs(cVal);
829 cVal += stride;
830 }
831
832 // Decrement loop comes after (to prevent race conditions).
833 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700834 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800835 elem->decRefs(oldVal);
836 oldVal += stride;
837 }
838 }
839
840 memcpy(destPtr, data, dataLength);
841}
842
843void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
844
845 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800846 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800847
Yang Nid9bae682015-01-20 15:31:15 -0800848 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800849 if (!destPtr) {
850 //ALOGV("Calling setVar on slot = %i which is null", slot);
851 return;
852 }
853
Chris Wailes44bef6f2014-08-12 13:51:10 -0700854 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800855 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800856 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800857 ptr = data->mHal.drvState.lod[0].mallocPtr;
858 }
859 memcpy(destPtr, &ptr, sizeof(void *));
860}
861
862void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
863
864 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800865 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800866
Yang Nid9bae682015-01-20 15:31:15 -0800867 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800868 if (!destPtr) {
869 //ALOGV("Calling setVar on slot = %i which is null", slot);
870 return;
871 }
872
Jason Sams05ef73f2014-08-05 14:59:22 -0700873 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800874}
875
Yang Ni062c2872015-02-20 15:20:00 -0800876const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
877 return mScriptExec->getFieldName(slot);
878}
879
Jason Sams709a0972012-11-15 18:18:04 -0800880RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700881 delete mScriptExec;
882 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700883 if (mScriptSO) {
884 dlclose(mScriptSO);
885 }
Jason Sams709a0972012-11-15 18:18:04 -0800886}
887
888Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
889 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700890 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800891 }
892
893 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
894 Allocation *a = mBoundAllocs[ct];
895 if (!a) continue;
896 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
897 return a;
898 }
899 }
900 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700901 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800902}
903
Stephen Hines8409d642015-04-28 18:49:56 -0700904int RsdCpuScriptImpl::getGlobalEntries() const {
905 return mScriptExec->getGlobalEntries();
906}
907
908const char * RsdCpuScriptImpl::getGlobalName(int i) const {
909 return mScriptExec->getGlobalName(i);
910}
911
912const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
913 return mScriptExec->getGlobalAddress(i);
914}
915
916size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
917 return mScriptExec->getGlobalSize(i);
918}
919
Stephen Hines5aa018c2015-05-20 18:09:57 -0700920uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
921 return mScriptExec->getGlobalProperties(i);
922}
923
Chris Wailesf3712132014-07-16 15:18:30 -0700924void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
925 uint32_t inLen, Allocation * aout,
926 const void * usr, uint32_t usrLen,
927 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700928
Chris Wailesf3712132014-07-16 15:18:30 -0700929void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
930 uint32_t inLen, Allocation * aout,
931 const void * usr, uint32_t usrLen,
932 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700933
Jason Sams709a0972012-11-15 18:18:04 -0800934
935}
936}