blob: 40f1a65579bf38e6e401c2414ba48cff44a81232 [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 {
Stephen Hines8409d642015-04-28 18:49:56 -070058
59static const bool kDebugGlobalVariables = false;
60
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080061#ifndef RS_COMPATIBILITY_LIB
62
Stephen Hinesba17ae42013-06-05 17:18:04 -070063static bool is_force_recompile() {
64#ifdef RS_SERVER
65 return false;
66#else
67 char buf[PROPERTY_VALUE_MAX];
68
69 // Re-compile if floating point precision has been overridden.
70 property_get("debug.rs.precision", buf, "");
71 if (buf[0] != '\0') {
72 return true;
73 }
74
75 // Re-compile if debug.rs.forcerecompile is set.
76 property_get("debug.rs.forcerecompile", buf, "0");
77 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
78 return true;
79 } else {
80 return false;
81 }
82#endif // RS_SERVER
83}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070084
Chris Wailes6847e732014-08-11 17:30:51 -070085static void setCompileArguments(std::vector<const char*>* args,
86 const std::string& bcFileName,
87 const char* cacheDir, const char* resName,
88 const char* core_lib, bool useRSDebugContext,
Stephen Hines8409d642015-04-28 18:49:56 -070089 const char* bccPluginName, bool emitGlobalInfo,
90 bool emitGlobalInfoSkipConstant) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070091 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080092 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080093 args->push_back("-unroll-runtime");
94 args->push_back("-scalarize-load-store");
Stephen Hines8409d642015-04-28 18:49:56 -070095 if (emitGlobalInfo) {
96 args->push_back("-rs-global-info");
97 if (emitGlobalInfoSkipConstant) {
98 args->push_back("-rs-global-info-skip-constant");
99 }
100 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700101 args->push_back("-o");
102 args->push_back(resName);
103 args->push_back("-output_path");
104 args->push_back(cacheDir);
105 args->push_back("-bclib");
106 args->push_back(core_lib);
107 args->push_back("-mtriple");
108 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
109
Tim Murray358ffb82014-12-09 11:53:06 -0800110 // Enable workaround for A53 codegen by default.
111#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
112 args->push_back("-aarch64-fix-cortex-a53-835769");
113#endif
114
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700115 // Execute the bcc compiler.
116 if (useRSDebugContext) {
117 args->push_back("-rs-debug-ctx");
118 } else {
119 // Only load additional libraries for compiles that don't use
120 // the debug context.
121 if (bccPluginName && strlen(bccPluginName) > 0) {
122 args->push_back("-load");
123 args->push_back(bccPluginName);
124 }
125 }
126
Stephen Hines45e753a2015-01-19 20:58:44 -0800127 args->push_back("-fPIC");
128 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800129
Chris Wailes6847e732014-08-11 17:30:51 -0700130 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700131 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700132}
133
Chris Wailes6847e732014-08-11 17:30:51 -0700134static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700135 const char *bitcode,
136 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700137 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700138 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700139
Chris Wailes6847e732014-08-11 17:30:51 -0700140 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700141 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700142 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700143 return false;
144 }
145 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
146 fclose(bcfile);
147 if (nwritten != bitcodeSize) {
148 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700149 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700150 return false;
151 }
152
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700153 return android::renderscript::rsuExecuteCommand(
154 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
155 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700156}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700157
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800158bool isChecksumNeeded() {
159 char buf[PROPERTY_VALUE_MAX];
160 property_get("ro.debuggable", buf, "");
161 return (buf[0] == '1');
162}
163
164bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
165 int FD = open(fileName, O_RDONLY);
166 if (FD == -1) {
167 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
168 return false;
169 }
170
171 char buf[256];
172 while (true) {
173 ssize_t nread = read(FD, buf, sizeof(buf));
174 if (nread < 0) { // bail out on failed read
175 ALOGE("Error while computing checksum for file \'%s\'", fileName);
176 return false;
177 }
178
179 checksum = adler32(checksum, (const unsigned char *) buf, nread);
180 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
181 break;
182 }
183
Elliott Hughes2df57672015-05-15 17:06:47 -0700184 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800185 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
186 return false;
187 }
188 return true;
189}
Yang Nicb170152015-04-16 10:27:02 -0700190
191#endif // !defined(RS_COMPATIBILITY_LIB)
192} // namespace
193
194namespace android {
195namespace renderscript {
196
197#ifndef RS_COMPATIBILITY_LIB
198
199uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
200 const char *commandLine,
201 const char** bccFiles, size_t numFiles) {
202 uint32_t checksum = adler32(0L, Z_NULL, 0);
203
204 // include checksum of bitcode
205 if (bitcode != nullptr && bitcodeSize > 0) {
206 checksum = adler32(checksum, bitcode, bitcodeSize);
207 }
208
209 // include checksum of command line arguments
210 checksum = adler32(checksum, (const unsigned char *) commandLine,
211 strlen(commandLine));
212
213 // include checksum of bccFiles
214 for (size_t i = 0; i < numFiles; i++) {
215 const char* bccFile = bccFiles[i];
216 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
217 // return empty checksum instead of something partial/corrupt
218 return 0;
219 }
220 }
221
222 return checksum;
223}
224
Yang Nif02a2b02015-04-07 16:00:31 -0700225#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800226
Jason Sams709a0972012-11-15 18:18:04 -0800227RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
228 mCtx = ctx;
229 mScript = s;
230
Chris Wailes44bef6f2014-08-12 13:51:10 -0700231 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800232
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800233#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700234 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700235#endif
236
Tim Murraye195a3f2014-03-13 15:04:58 -0700237
Chris Wailes44bef6f2014-08-12 13:51:10 -0700238 mRoot = nullptr;
239 mRootExpand = nullptr;
240 mInit = nullptr;
241 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800242 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800243
Chris Wailes44bef6f2014-08-12 13:51:10 -0700244 mBoundAllocs = nullptr;
245 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800246 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800247
Yang Nicb170152015-04-16 10:27:02 -0700248 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800249 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800250}
251
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800252bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800253 // The shared object may have an invalid build checksum.
254 // Validate and fail early.
255 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Nicb170152015-04-16 10:27:02 -0700256 mCtx->getContext(), mScriptSO,
257 mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800258
259 if (mScriptExec == nullptr) {
260 return false;
261 }
262
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800263 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
264 if (mRoot) {
265 //ALOGE("Found root(): %p", mRoot);
266 }
267 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
268 if (mRootExpand) {
269 //ALOGE("Found root.expand(): %p", mRootExpand);
270 }
271 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
272 if (mInit) {
273 //ALOGE("Found init(): %p", mInit);
274 }
275 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
276 if (mFreeChildren) {
277 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
278 }
279
Yang Nid9bae682015-01-20 15:31:15 -0800280 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800281 if (varCount > 0) {
282 mBoundAllocs = new Allocation *[varCount];
283 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
284 }
285
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800286 mIsThreadable = mScriptExec->getThreadable();
287 //ALOGE("Script isThreadable? %d", mIsThreadable);
288
Stephen Hines8409d642015-04-28 18:49:56 -0700289 if (kDebugGlobalVariables) {
290 mScriptExec->dumpGlobalInfo();
291 }
292
Yang Nid9bae682015-01-20 15:31:15 -0800293 return true;
294}
295
Jason Sams709a0972012-11-15 18:18:04 -0800296bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
297 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800298 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800299 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
300 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800301 //ALOGE("rsdScriptInit %p %p", rsc, script);
302
303 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700304#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800305 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800306
Chris Wailes44bef6f2014-08-12 13:51:10 -0700307 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800308
Jason Sams709a0972012-11-15 18:18:04 -0800309 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700310 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800311 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
312 mCtx->unlockMutex();
313 return false;
314 }
315
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700316 // Run any compiler setup functions we have been provided with.
317 RSSetupCompilerCallback setupCompilerCallback =
318 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700319 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700320 setupCompilerCallback(mCompilerDriver);
321 }
322
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700323 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
324 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700325 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700326 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700327 return false;
328 }
329
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700330 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700331
332 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700333 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800334 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700335 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700336
Chris Wailes6847e732014-08-11 17:30:51 -0700337 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700338 bcFileName.append("/");
339 bcFileName.append(resName);
340 bcFileName.append(".bc");
341
342 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700343 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
344 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700345 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700346 useRSDebugContext, bccPluginName, emitGlobalInfo,
347 emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800348
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800349 mChecksumNeeded = isChecksumNeeded();
350 if (mChecksumNeeded) {
351 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
352 core_lib,
353 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700354
355 // The last argument of compileArguments is a nullptr, so remove 1 from
356 // the size.
357 std::unique_ptr<const char> compileCommandLine(
358 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
359
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800360 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
361 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700362 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800363
Yang Nicb170152015-04-16 10:27:02 -0700364 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800365 // cannot compute checksum but verification is enabled
366 mCtx->unlockMutex();
367 return false;
368 }
369 }
370 else {
371 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700372 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800373 }
374
375 // Append build checksum to commandline
376 // Handle the terminal nullptr in compileArguments
377 compileArguments.pop_back();
378 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700379 std::stringstream ss;
380 ss << std::hex << mBuildChecksum;
381 compileArguments.push_back(ss.str().c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800382 compileArguments.push_back(nullptr);
383
Tim Murraybf96a522015-01-23 15:37:03 -0800384 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800385 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800386
387 // Read RS info from the shared object to detect checksum mismatch
388 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
389 dlclose(mScriptSO);
390 mScriptSO = nullptr;
391 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700392 }
393
394 // If we can't, it's either not there or out of date. We compile the bit code and try loading
395 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800396 if (mScriptSO == nullptr) {
397 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700398 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800399 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700400 ALOGE("bcc: FAILS to compile '%s'", resName);
401 mCtx->unlockMutex();
402 return false;
403 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800404
Stephen Hines4c368af2015-05-06 00:43:02 -0700405 if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
406 cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800407 ALOGE("Linker: Failed to link object file '%s'", resName);
408 mCtx->unlockMutex();
409 return false;
410 }
411
Yang Ni1c44cb62015-01-22 12:02:27 -0800412 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800413 if (mScriptSO == nullptr) {
414 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700415 mCtx->unlockMutex();
416 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700417 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800418
419 // Read RS symbol information from the .so.
420 if (!storeRSInfoFromSO()) {
421 goto error;
422 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700423 }
Jason Sams709a0972012-11-15 18:18:04 -0800424
Yang Nic31585b2015-02-15 11:43:50 -0800425 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800426
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700427#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800428 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
429 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700430
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800431 if (!mScriptSO) {
432 goto error;
433 }
Jason Sams110f1812013-03-14 16:02:18 -0700434
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800435 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700436 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700437 }
438#endif
Jason Sams709a0972012-11-15 18:18:04 -0800439 mCtx->unlockMutex();
440 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700441
Jason Sams110f1812013-03-14 16:02:18 -0700442error:
443
444 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700445 if (mScriptSO) {
446 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800447 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700448 }
449 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800450}
451
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700452#ifndef RS_COMPATIBILITY_LIB
453
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700454const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
455 size_t bitcodeSize) {
456 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
457
458 // If we're debugging, use the debug library.
459 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
460 return SYSLIBPATH"/libclcore_debug.bc";
461 }
462
463 // If a callback has been registered to specify a library, use that.
464 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700465 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700466 return selectRTCallback((const char*)bitcode, bitcodeSize);
467 }
468
469 // Check for a platform specific library
470#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
471 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700472 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700473 // NEON-capable ARMv7a devices can use an accelerated math library
474 // for all reduced precision scripts.
475 // ARMv8 does not use NEON, as ASIMD can be used with all precision
476 // levels.
477 return SYSLIBPATH"/libclcore_neon.bc";
478 } else {
479 return defaultLib;
480 }
481#elif defined(__i386__) || defined(__x86_64__)
482 // x86 devices will use an optimized library.
483 return SYSLIBPATH"/libclcore_x86.bc";
484#else
485 return defaultLib;
486#endif
487}
488
489#endif
490
Jason Sams709a0972012-11-15 18:18:04 -0800491void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700492 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800493 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
494 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800495 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800496 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
497 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700498
499 // Bug, need to stash in metadata
500 if (mRootExpand) {
501 script->mHal.info.root = mRootExpand;
502 } else {
503 script->mHal.info.root = mRoot;
504 }
Jason Sams709a0972012-11-15 18:18:04 -0800505}
506
Jason Sams709a0972012-11-15 18:18:04 -0800507
Jason Samsbf2111d2015-01-26 18:13:41 -0800508bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700509 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700510 Allocation * aout,
511 const void * usr, uint32_t usrLen,
512 const RsScriptCall *sc,
513 MTLaunchStruct *mtls) {
514
515 memset(mtls, 0, sizeof(MTLaunchStruct));
516
Chris Wailesf3712132014-07-16 15:18:30 -0700517 for (int index = inLen; --index >= 0;) {
518 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700519
Chris Wailesf3712132014-07-16 15:18:30 -0700520 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700521 if (ain != nullptr &&
522 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
523
Chris Wailesf3712132014-07-16 15:18:30 -0700524 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
525 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800526 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700527 }
528 }
529
Chris Wailes44bef6f2014-08-12 13:51:10 -0700530 if (aout &&
531 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
532
Chris Wailesf3712132014-07-16 15:18:30 -0700533 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
534 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800535 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700536 }
537
Chris Wailesf3712132014-07-16 15:18:30 -0700538 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700539 const Allocation *ain0 = ains[0];
540 const Type *inType = ain0->getType();
541
Jason Samsc0d68472015-01-20 14:29:52 -0800542 mtls->fep.dim.x = inType->getDimX();
543 mtls->fep.dim.y = inType->getDimY();
544 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700545
546 for (int Index = inLen; --Index >= 1;) {
547 if (!ain0->hasSameDims(ains[Index])) {
548 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800549 "Failed to launch kernel; dimensions of input and output"
550 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700551
Jason Samsbf2111d2015-01-26 18:13:41 -0800552 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700553 }
554 }
555
Chris Wailes44bef6f2014-08-12 13:51:10 -0700556 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700557 const Type *outType = aout->getType();
558
Jason Samsc0d68472015-01-20 14:29:52 -0800559 mtls->fep.dim.x = outType->getDimX();
560 mtls->fep.dim.y = outType->getDimY();
561 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700562
Jason Samsa9139c72015-04-16 15:11:04 -0700563 } else if (sc != nullptr) {
564 mtls->fep.dim.x = sc->xEnd;
565 mtls->fep.dim.y = sc->yEnd;
566 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700567 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700568 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
569 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800570 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700571 }
572
Chris Wailes44bef6f2014-08-12 13:51:10 -0700573 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700574 if (!ains[0]->hasSameDims(aout)) {
575 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
576 "Failed to launch kernel; dimensions of input and output allocations do not match.");
577
Jason Samsbf2111d2015-01-26 18:13:41 -0800578 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700579 }
580 }
581
582 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800583 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700584 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800585 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
586 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700587 if (mtls->start.x >= mtls->end.x) {
588 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
589 "Failed to launch kernel; Invalid xStart or xEnd.");
590 return false;
591 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700592 }
593
594 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800595 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700596 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800597 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
598 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700599 if (mtls->start.y >= mtls->end.y) {
600 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
601 "Failed to launch kernel; Invalid yStart or yEnd.");
602 return false;
603 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700604 }
605
606 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800607 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700608 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800609 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
610 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700611 if (mtls->start.z >= mtls->end.z) {
612 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
613 "Failed to launch kernel; Invalid zStart or zEnd.");
614 return false;
615 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700616 }
617
Jason Samsbf2111d2015-01-26 18:13:41 -0800618 if (!sc || (sc->arrayEnd == 0)) {
619 mtls->end.array[0] = mtls->fep.dim.array[0];
620 } else {
621 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
622 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700623 if (mtls->start.array[0] >= mtls->end.array[0]) {
624 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
625 "Failed to launch kernel; Invalid arrayStart or arrayEnd.");
626 return false;
627 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800628 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700629
Jason Samsbf2111d2015-01-26 18:13:41 -0800630 if (!sc || (sc->array2End == 0)) {
631 mtls->end.array[1] = mtls->fep.dim.array[1];
632 } else {
633 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
634 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700635 if (mtls->start.array[1] >= mtls->end.array[1]) {
636 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
637 "Failed to launch kernel; Invalid array2Start or array2End.");
638 return false;
639 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800640 }
641
642 if (!sc || (sc->array3End == 0)) {
643 mtls->end.array[2] = mtls->fep.dim.array[2];
644 } else {
645 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
646 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700647 if (mtls->start.array[2] >= mtls->end.array[2]) {
648 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
649 "Failed to launch kernel; Invalid array3Start or array3End.");
650 return false;
651 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800652 }
653
654 if (!sc || (sc->array4End == 0)) {
655 mtls->end.array[3] = mtls->fep.dim.array[3];
656 } else {
657 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
658 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
Pirama Arumuga Nainar80afd422015-04-28 15:41:48 -0700659 if (mtls->start.array[3] >= mtls->end.array[3]) {
660 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
661 "Failed to launch kernel; Invalid array4Start or array4End.");
662 return false;
663 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800664 }
665
666
667 // The X & Y walkers always want 0-1 min even if dim is not present
668 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
669 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700670
671 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800672 if (ains) {
673 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
674 }
675 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700676 mtls->fep.usr = usr;
677 mtls->fep.usrLen = usrLen;
678 mtls->mSliceSize = 1;
679 mtls->mSliceNum = 0;
680
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700681 mtls->isThreadable = mIsThreadable;
682
Chris Wailesf3712132014-07-16 15:18:30 -0700683 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700684 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700685 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800686 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
687 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700688 }
689 }
690
Chris Wailes44bef6f2014-08-12 13:51:10 -0700691 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800692 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
693 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700694 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800695
696 // All validation passed, ok to launch threads
697 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700698}
699
Jason Sams709a0972012-11-15 18:18:04 -0800700
701void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700702 const Allocation ** ains,
703 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800704 Allocation * aout,
705 const void * usr,
706 uint32_t usrLen,
707 const RsScriptCall *sc) {
708
709 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700710
Jason Samsbf2111d2015-01-26 18:13:41 -0800711 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
712 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700713
Jason Samsbf2111d2015-01-26 18:13:41 -0800714 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
715 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
716 mCtx->setTLS(oldTLS);
717 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700718}
719
Jason Sams709a0972012-11-15 18:18:04 -0800720void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800721 mtls->script = this;
722 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800723 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700724 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800725 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800726}
727
728int RsdCpuScriptImpl::invokeRoot() {
729 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
730 int ret = mRoot();
731 mCtx->setTLS(oldTLS);
732 return ret;
733}
734
735void RsdCpuScriptImpl::invokeInit() {
736 if (mInit) {
737 mInit();
738 }
739}
740
741void RsdCpuScriptImpl::invokeFreeChildren() {
742 if (mFreeChildren) {
743 mFreeChildren();
744 }
745}
746
747void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
748 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800749 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800750 void * ap = nullptr;
751
752#if defined(__x86_64__)
753 // The invoked function could have input parameter of vector type for example float4 which
754 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
755 // So try to align void* params before passing them into RS exported function.
756
757 if ((uint8_t)(uint64_t)params & 0x0F) {
758 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
759 memcpy(ap, params, paramLength);
760 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800761 ALOGE("x86_64: invokeFunction memalign error, still use params which"
762 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800763 }
764 }
765#endif
Jason Sams709a0972012-11-15 18:18:04 -0800766
767 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800768 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800769 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800770
Yong Chenc246d912015-05-22 18:20:06 +0800771#if defined(__x86_64__)
772 free(ap);
773#endif
774
Jason Sams709a0972012-11-15 18:18:04 -0800775 mCtx->setTLS(oldTLS);
776}
777
778void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
779 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800780 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800781
782 //if (mIntrinsicID) {
783 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
784 //return;
785 //}
786
Yang Nid9bae682015-01-20 15:31:15 -0800787 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800788 if (!destPtr) {
789 //ALOGV("Calling setVar on slot = %i which is null", slot);
790 return;
791 }
792
793 memcpy(destPtr, data, dataLength);
794}
795
Tim Murray9c642392013-04-11 13:29:59 -0700796void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
797 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800798 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700799
Yang Nid9bae682015-01-20 15:31:15 -0800800 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700801 if (!srcPtr) {
802 //ALOGV("Calling setVar on slot = %i which is null", slot);
803 return;
804 }
805 memcpy(data, srcPtr, dataLength);
806}
807
808
Jason Sams709a0972012-11-15 18:18:04 -0800809void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
810 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700811 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800812 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800813 if (!destPtr) {
814 //ALOGV("Calling setVar on slot = %i which is null", slot);
815 return;
816 }
817
818 // We want to look at dimension in terms of integer components,
819 // but dimLength is given in terms of bytes.
820 dimLength /= sizeof(int);
821
822 // Only a single dimension is currently supported.
823 rsAssert(dimLength == 1);
824 if (dimLength == 1) {
825 // First do the increment loop.
826 size_t stride = elem->getSizeBytes();
827 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700828 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800829 elem->incRefs(cVal);
830 cVal += stride;
831 }
832
833 // Decrement loop comes after (to prevent race conditions).
834 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700835 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800836 elem->decRefs(oldVal);
837 oldVal += stride;
838 }
839 }
840
841 memcpy(destPtr, data, dataLength);
842}
843
844void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
845
846 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800847 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800848
Yang Nid9bae682015-01-20 15:31:15 -0800849 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800850 if (!destPtr) {
851 //ALOGV("Calling setVar on slot = %i which is null", slot);
852 return;
853 }
854
Chris Wailes44bef6f2014-08-12 13:51:10 -0700855 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800856 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800857 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800858 ptr = data->mHal.drvState.lod[0].mallocPtr;
859 }
860 memcpy(destPtr, &ptr, sizeof(void *));
861}
862
863void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
864
865 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800866 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800867
Yang Nid9bae682015-01-20 15:31:15 -0800868 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800869 if (!destPtr) {
870 //ALOGV("Calling setVar on slot = %i which is null", slot);
871 return;
872 }
873
Jason Sams05ef73f2014-08-05 14:59:22 -0700874 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800875}
876
Yang Ni062c2872015-02-20 15:20:00 -0800877const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
878 return mScriptExec->getFieldName(slot);
879}
880
Jason Sams709a0972012-11-15 18:18:04 -0800881RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -0700882#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -0800883 if (mCompilerDriver) {
884 delete mCompilerDriver;
885 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800886#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800887
Yang Nid9bae682015-01-20 15:31:15 -0800888 if (mScriptExec != nullptr) {
889 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800890 }
Jason Sams110f1812013-03-14 16:02:18 -0700891 if (mBoundAllocs) delete[] mBoundAllocs;
892 if (mScriptSO) {
893 dlclose(mScriptSO);
894 }
Jason Sams709a0972012-11-15 18:18:04 -0800895}
896
897Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
898 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700899 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800900 }
901
902 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
903 Allocation *a = mBoundAllocs[ct];
904 if (!a) continue;
905 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
906 return a;
907 }
908 }
909 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700910 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800911}
912
Stephen Hines8409d642015-04-28 18:49:56 -0700913int RsdCpuScriptImpl::getGlobalEntries() const {
914 return mScriptExec->getGlobalEntries();
915}
916
917const char * RsdCpuScriptImpl::getGlobalName(int i) const {
918 return mScriptExec->getGlobalName(i);
919}
920
921const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
922 return mScriptExec->getGlobalAddress(i);
923}
924
925size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
926 return mScriptExec->getGlobalSize(i);
927}
928
Stephen Hines5aa018c2015-05-20 18:09:57 -0700929uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
930 return mScriptExec->getGlobalProperties(i);
931}
932
Chris Wailesf3712132014-07-16 15:18:30 -0700933void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
934 uint32_t inLen, Allocation * aout,
935 const void * usr, uint32_t usrLen,
936 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700937
Chris Wailesf3712132014-07-16 15:18:30 -0700938void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
939 uint32_t inLen, Allocation * aout,
940 const void * usr, uint32_t usrLen,
941 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700942
Jason Sams709a0972012-11-15 18:18:04 -0800943
944}
945}