blob: 7308b5407fcfb01e5d223b2db0875f18fab7975c [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
Matt Wala14ce0072015-07-30 17:30:25 -070053static bool allocationLODIsNull(const android::renderscript::Allocation *alloc) {
54 // Even if alloc != nullptr, mallocPtr could be null if
55 // IO_OUTPUT/IO_INPUT with no bound surface.
56 return alloc && alloc->mHal.drvState.lod[0].mallocPtr == nullptr;
57}
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,
verena beckhamf5029802015-05-22 16:51:42 +010088 int optLevel, 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);
verena beckhamf5029802015-05-22 16:51:42 +0100107 args->push_back("-O");
108
109 switch (optLevel) {
110 case (0):
111 args->push_back("0");
112 break;
113 case (3):
114 args->push_back("3");
115 break;
116 default:
117 ALOGW("Expected optimization level of 0 or 3. Received %d", optLevel);
118 args->push_back("3");
119 break;
120 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700121
Tim Murray358ffb82014-12-09 11:53:06 -0800122 // Enable workaround for A53 codegen by default.
123#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
124 args->push_back("-aarch64-fix-cortex-a53-835769");
125#endif
126
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700127 // Execute the bcc compiler.
128 if (useRSDebugContext) {
129 args->push_back("-rs-debug-ctx");
130 } else {
131 // Only load additional libraries for compiles that don't use
132 // the debug context.
133 if (bccPluginName && strlen(bccPluginName) > 0) {
134 args->push_back("-load");
135 args->push_back(bccPluginName);
136 }
137 }
138
Stephen Hines45e753a2015-01-19 20:58:44 -0800139 args->push_back("-fPIC");
140 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800141
Chris Wailes6847e732014-08-11 17:30:51 -0700142 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700143 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700144}
145
Chris Wailes6847e732014-08-11 17:30:51 -0700146static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700147 const char *bitcode,
148 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700149 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700150 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700151
Chris Wailes6847e732014-08-11 17:30:51 -0700152 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700153 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700154 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700155 return false;
156 }
157 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
158 fclose(bcfile);
159 if (nwritten != bitcodeSize) {
160 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700161 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700162 return false;
163 }
164
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700165 return android::renderscript::rsuExecuteCommand(
166 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
167 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700168}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700169
Stephen Hines6a236ad2015-06-22 14:51:30 -0700170// The checksum is unnecessary under a few conditions, since the primary
171// use-case for it is debugging. If we are loading something from the
172// system partition (read-only), we know that it was precompiled as part of
173// application ahead of time (and thus the checksum is completely
174// unnecessary). The checksum is also unnecessary on release (non-debug)
175// builds, as the only way to get a shared object is to have compiled the
176// script once already. On a release build, there is no way to adjust the
177// other libraries/dependencies, and so the only reason to recompile would
178// be for a source APK change or an OTA. In either case, the APK would be
179// reinstalled, which would already clear the code_cache/ directory.
180bool isChecksumNeeded(const char *cacheDir) {
181 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
182 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
183 return false;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800184 char buf[PROPERTY_VALUE_MAX];
185 property_get("ro.debuggable", buf, "");
186 return (buf[0] == '1');
187}
188
189bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
190 int FD = open(fileName, O_RDONLY);
191 if (FD == -1) {
192 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
193 return false;
194 }
195
196 char buf[256];
197 while (true) {
198 ssize_t nread = read(FD, buf, sizeof(buf));
199 if (nread < 0) { // bail out on failed read
200 ALOGE("Error while computing checksum for file \'%s\'", fileName);
201 return false;
202 }
203
204 checksum = adler32(checksum, (const unsigned char *) buf, nread);
205 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
206 break;
207 }
208
Elliott Hughes2df57672015-05-15 17:06:47 -0700209 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800210 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
211 return false;
212 }
213 return true;
214}
Yang Nicb170152015-04-16 10:27:02 -0700215
216#endif // !defined(RS_COMPATIBILITY_LIB)
217} // namespace
218
219namespace android {
220namespace renderscript {
221
222#ifndef RS_COMPATIBILITY_LIB
223
224uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
225 const char *commandLine,
226 const char** bccFiles, size_t numFiles) {
227 uint32_t checksum = adler32(0L, Z_NULL, 0);
228
229 // include checksum of bitcode
230 if (bitcode != nullptr && bitcodeSize > 0) {
231 checksum = adler32(checksum, bitcode, bitcodeSize);
232 }
233
234 // include checksum of command line arguments
235 checksum = adler32(checksum, (const unsigned char *) commandLine,
236 strlen(commandLine));
237
238 // include checksum of bccFiles
239 for (size_t i = 0; i < numFiles; i++) {
240 const char* bccFile = bccFiles[i];
241 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
242 // return empty checksum instead of something partial/corrupt
243 return 0;
244 }
245 }
246
247 return checksum;
248}
249
Yang Nif02a2b02015-04-07 16:00:31 -0700250#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800251
Jason Sams709a0972012-11-15 18:18:04 -0800252RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
253 mCtx = ctx;
254 mScript = s;
255
Chris Wailes44bef6f2014-08-12 13:51:10 -0700256 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800257
Chris Wailes44bef6f2014-08-12 13:51:10 -0700258 mRoot = nullptr;
259 mRootExpand = nullptr;
260 mInit = nullptr;
261 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800262 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800263
Chris Wailes44bef6f2014-08-12 13:51:10 -0700264 mBoundAllocs = nullptr;
265 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800266 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800267
Yang Nicb170152015-04-16 10:27:02 -0700268 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800269 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800270}
271
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800272bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800273 // The shared object may have an invalid build checksum.
274 // Validate and fail early.
275 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Nicb170152015-04-16 10:27:02 -0700276 mCtx->getContext(), mScriptSO,
277 mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800278
279 if (mScriptExec == nullptr) {
280 return false;
281 }
282
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800283 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
284 if (mRoot) {
285 //ALOGE("Found root(): %p", mRoot);
286 }
287 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
288 if (mRootExpand) {
289 //ALOGE("Found root.expand(): %p", mRootExpand);
290 }
Matt Wala14ce0072015-07-30 17:30:25 -0700291 mInit = (InitOrDtorFunc_t) dlsym(mScriptSO, "init");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800292 if (mInit) {
293 //ALOGE("Found init(): %p", mInit);
294 }
Matt Wala14ce0072015-07-30 17:30:25 -0700295 mFreeChildren = (InitOrDtorFunc_t) dlsym(mScriptSO, ".rs.dtor");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800296 if (mFreeChildren) {
297 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
298 }
299
Yang Nid9bae682015-01-20 15:31:15 -0800300 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800301 if (varCount > 0) {
302 mBoundAllocs = new Allocation *[varCount];
303 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
304 }
305
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800306 mIsThreadable = mScriptExec->getThreadable();
307 //ALOGE("Script isThreadable? %d", mIsThreadable);
308
Stephen Hines8409d642015-04-28 18:49:56 -0700309 if (kDebugGlobalVariables) {
310 mScriptExec->dumpGlobalInfo();
311 }
312
Yang Nid9bae682015-01-20 15:31:15 -0800313 return true;
314}
315
Jason Sams709a0972012-11-15 18:18:04 -0800316bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
317 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800318 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800319 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
320 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800321 //ALOGE("rsdScriptInit %p %p", rsc, script);
322
323 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700324#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800325 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800326
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700327 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
328 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700329 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700330 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700331 return false;
332 }
333
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700334 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700335
336 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800337 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700338 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700339
verena beckhamf5029802015-05-22 16:51:42 +0100340 int optLevel = mCtx->getContext()->getOptLevel();
341
Chris Wailes6847e732014-08-11 17:30:51 -0700342 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700343 bcFileName.append("/");
344 bcFileName.append(resName);
345 bcFileName.append(".bc");
346
347 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700348 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
349 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700350 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700351 useRSDebugContext, bccPluginName, emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +0100352 optLevel, emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800353
Stephen Hines6a236ad2015-06-22 14:51:30 -0700354 mChecksumNeeded = isChecksumNeeded(cacheDir);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800355 if (mChecksumNeeded) {
356 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
357 core_lib,
358 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700359
360 // The last argument of compileArguments is a nullptr, so remove 1 from
361 // the size.
362 std::unique_ptr<const char> compileCommandLine(
363 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
364
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800365 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
366 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700367 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800368
Yang Nicb170152015-04-16 10:27:02 -0700369 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800370 // cannot compute checksum but verification is enabled
371 mCtx->unlockMutex();
372 return false;
373 }
374 }
375 else {
376 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700377 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800378 }
379
380 // Append build checksum to commandline
381 // Handle the terminal nullptr in compileArguments
382 compileArguments.pop_back();
383 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700384 std::stringstream ss;
385 ss << std::hex << mBuildChecksum;
386 compileArguments.push_back(ss.str().c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800387 compileArguments.push_back(nullptr);
388
Tim Murraybf96a522015-01-23 15:37:03 -0800389 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800390 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800391
392 // Read RS info from the shared object to detect checksum mismatch
393 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
394 dlclose(mScriptSO);
395 mScriptSO = nullptr;
396 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700397 }
398
399 // If we can't, it's either not there or out of date. We compile the bit code and try loading
400 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800401 if (mScriptSO == nullptr) {
402 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700403 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800404 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700405 ALOGE("bcc: FAILS to compile '%s'", resName);
406 mCtx->unlockMutex();
407 return false;
408 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800409
Stephen Hines4c368af2015-05-06 00:43:02 -0700410 if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
411 cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800412 ALOGE("Linker: Failed to link object file '%s'", resName);
413 mCtx->unlockMutex();
414 return false;
415 }
416
Yang Ni1c44cb62015-01-22 12:02:27 -0800417 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800418 if (mScriptSO == nullptr) {
419 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700420 mCtx->unlockMutex();
421 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700422 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800423
424 // Read RS symbol information from the .so.
425 if (!storeRSInfoFromSO()) {
426 goto error;
427 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700428 }
Jason Sams709a0972012-11-15 18:18:04 -0800429
Yang Nic31585b2015-02-15 11:43:50 -0800430 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800431
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700432#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800433 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
434 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700435
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800436 if (!mScriptSO) {
437 goto error;
438 }
Jason Sams110f1812013-03-14 16:02:18 -0700439
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800440 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700441 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700442 }
443#endif
Jason Sams709a0972012-11-15 18:18:04 -0800444 mCtx->unlockMutex();
445 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700446
Jason Sams110f1812013-03-14 16:02:18 -0700447error:
448
449 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700450 if (mScriptSO) {
451 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800452 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700453 }
454 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800455}
456
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700457#ifndef RS_COMPATIBILITY_LIB
458
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700459const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
460 size_t bitcodeSize) {
461 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
462
463 // If we're debugging, use the debug library.
464 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
465 return SYSLIBPATH"/libclcore_debug.bc";
466 }
467
468 // If a callback has been registered to specify a library, use that.
469 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700470 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700471 return selectRTCallback((const char*)bitcode, bitcodeSize);
472 }
473
474 // Check for a platform specific library
475#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
476 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700477 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700478 // NEON-capable ARMv7a devices can use an accelerated math library
479 // for all reduced precision scripts.
480 // ARMv8 does not use NEON, as ASIMD can be used with all precision
481 // levels.
482 return SYSLIBPATH"/libclcore_neon.bc";
483 } else {
484 return defaultLib;
485 }
486#elif defined(__i386__) || defined(__x86_64__)
487 // x86 devices will use an optimized library.
488 return SYSLIBPATH"/libclcore_x86.bc";
489#else
490 return defaultLib;
491#endif
492}
493
494#endif
495
Jason Sams709a0972012-11-15 18:18:04 -0800496void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700497 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800498 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700499 script->mHal.info.exportedReduceCount = mScriptExec->getExportedReduceCount();
David Gross6c1876b2016-01-15 11:52:14 -0800500 script->mHal.info.exportedReduceNewCount = mScriptExec->getExportedReduceNewCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700501 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800502 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800503 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800504 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
505 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700506
507 // Bug, need to stash in metadata
508 if (mRootExpand) {
509 script->mHal.info.root = mRootExpand;
510 } else {
511 script->mHal.info.root = mRoot;
512 }
Jason Sams709a0972012-11-15 18:18:04 -0800513}
514
Matt Wala14ce0072015-07-30 17:30:25 -0700515// Set up the launch dimensions, and write the values of the launch
516// dimensions into the mtls start/end fields.
517//
518// Inputs:
519// baseDim - base shape of the input
520// sc - used to constrain the launch dimensions
521//
522// Returns:
523// True on success, false on failure to set up
524bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
525 const RsLaunchDimensions &baseDim,
526 const RsScriptCall *sc) {
527 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800528
Matt Wala14ce0072015-07-30 17:30:25 -0700529#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
530 if (!sc || (sc->SC_FIELD##End == 0)) { \
531 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
532 } else { \
533 mtls->start.DIM_FIELD = \
534 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
535 mtls->end.DIM_FIELD = \
536 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
537 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
538 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
539 "Failed to launch kernel; Invalid " \
540 #SC_FIELD "Start or " #SC_FIELD "End."); \
541 return false; \
542 } \
543 }} while(0)
544
545 SET_UP_DIMENSION(x, x);
546 SET_UP_DIMENSION(y, y);
547 SET_UP_DIMENSION(z, z);
548 SET_UP_DIMENSION(array[0], array);
549 SET_UP_DIMENSION(array[1], array2);
550 SET_UP_DIMENSION(array[2], array3);
551 SET_UP_DIMENSION(array[3], array4);
552#undef SET_UP_DIMENSION
553
554 return true;
555}
556
David Gross6c1876b2016-01-15 11:52:14 -0800557// Preliminary work to prepare a simple reduce-style kernel for launch.
Matt Wala14ce0072015-07-30 17:30:25 -0700558bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation *ain,
559 const Allocation *aout,
560 const RsScriptCall *sc,
561 MTLaunchStructReduce *mtls) {
562 rsAssert(ain && aout);
563 memset(mtls, 0, sizeof(MTLaunchStructReduce));
564 mtls->dimPtr = &mtls->inputDim;
565
566 if (allocationLODIsNull(ain) || allocationLODIsNull(aout)) {
567 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
568 "reduce called with a null allocation");
569 return false;
570 }
571
572 // Set up the dimensions of the input.
573 const Type *inType = ain->getType();
574 mtls->inputDim.x = inType->getDimX();
575 rsAssert(inType->getDimY() == 0);
576
577 if (!setUpMtlsDimensions(mtls, mtls->inputDim, sc)) {
578 return false;
579 }
580
581 mtls->rs = mCtx;
582 // Currently not threaded.
583 mtls->isThreadable = false;
584 mtls->mSliceNum = -1;
585
586 // Set up input and output.
587 mtls->inBuf = static_cast<uint8_t *>(ain->getPointerUnchecked(0, 0));
588 mtls->outBuf = static_cast<uint8_t *>(aout->getPointerUnchecked(0, 0));
589
590 rsAssert(mtls->inBuf && mtls->outBuf);
591
592 return true;
593}
594
David Gross6c1876b2016-01-15 11:52:14 -0800595// Preliminary work to prepare a general reduce-style kernel for launch.
596bool RsdCpuScriptImpl::reduceNewMtlsSetup(const Allocation ** ains,
597 uint32_t inLen,
598 const Allocation * aout,
599 const RsScriptCall *sc,
600 MTLaunchStructReduceNew *mtls) {
601 rsAssert(ains && (inLen >= 1) && aout);
602 memset(mtls, 0, sizeof(MTLaunchStructReduceNew));
603 mtls->dimPtr = &mtls->redp.dim;
604
605 for (int index = inLen; --index >= 0;) {
606 if (allocationLODIsNull(ains[index])) {
607 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
608 "reduce called with null in allocations");
609 return false;
610 }
611 }
612
613 if (allocationLODIsNull(aout)) {
614 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
615 "reduce called with null out allocation");
616 return false;
617 }
618
619 const Allocation *ain0 = ains[0];
620 const Type *inType = ain0->getType();
621
622 mtls->redp.dim.x = inType->getDimX();
623 mtls->redp.dim.y = inType->getDimY();
624 mtls->redp.dim.z = inType->getDimZ();
625
626 for (int Index = inLen; --Index >= 1;) {
627 if (!ain0->hasSameDims(ains[Index])) {
628 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
629 "Failed to launch reduction kernel;"
630 "dimensions of input allocations do not match.");
631 return false;
632 }
633 }
634
635 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
636 return false;
637 }
638
639 // The X & Y walkers always want 0-1 min even if dim is not present
640 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
641 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
642
643 mtls->rs = mCtx;
644
645 // Currently not threaded.
646 mtls->isThreadable = false;
647 mtls->mSliceNum = -1;
648
649 // Set up output,
650 mtls->redp.outLen = 1;
651 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
652 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
653
654 // Set up input.
655 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
656 mtls->redp.inLen = inLen;
657 for (int index = inLen; --index >= 0;) {
658 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
659 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
660 }
661
662 // All validation passed, ok to launch threads
663 return true;
664}
665
Matt Wala14ce0072015-07-30 17:30:25 -0700666// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800667bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700668 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700669 Allocation * aout,
670 const void * usr, uint32_t usrLen,
671 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700672 MTLaunchStructForEach *mtls) {
673 memset(mtls, 0, sizeof(MTLaunchStructForEach));
674 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700675
Chris Wailesf3712132014-07-16 15:18:30 -0700676 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700677 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700678 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
679 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800680 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700681 }
682 }
683
Matt Wala14ce0072015-07-30 17:30:25 -0700684 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700685 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
686 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800687 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700688 }
689
Chris Wailesf3712132014-07-16 15:18:30 -0700690 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700691 const Allocation *ain0 = ains[0];
692 const Type *inType = ain0->getType();
693
Jason Samsc0d68472015-01-20 14:29:52 -0800694 mtls->fep.dim.x = inType->getDimX();
695 mtls->fep.dim.y = inType->getDimY();
696 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700697
698 for (int Index = inLen; --Index >= 1;) {
699 if (!ain0->hasSameDims(ains[Index])) {
700 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
David Gross6c1876b2016-01-15 11:52:14 -0800701 "Failed to launch kernel; dimensions of input"
Yang Nie8f9fba2015-01-30 08:55:10 -0800702 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800703 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700704 }
705 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700706 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700707 const Type *outType = aout->getType();
708
Jason Samsc0d68472015-01-20 14:29:52 -0800709 mtls->fep.dim.x = outType->getDimX();
710 mtls->fep.dim.y = outType->getDimY();
711 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700712
Jason Samsa9139c72015-04-16 15:11:04 -0700713 } else if (sc != nullptr) {
714 mtls->fep.dim.x = sc->xEnd;
715 mtls->fep.dim.y = sc->yEnd;
716 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700717 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700718 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
719 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800720 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700721 }
722
Chris Wailes44bef6f2014-08-12 13:51:10 -0700723 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700724 if (!ains[0]->hasSameDims(aout)) {
725 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
726 "Failed to launch kernel; dimensions of input and output allocations do not match.");
727
Jason Samsbf2111d2015-01-26 18:13:41 -0800728 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700729 }
730 }
731
Matt Wala14ce0072015-07-30 17:30:25 -0700732 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
733 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700734 }
735
Jason Samsbf2111d2015-01-26 18:13:41 -0800736 // The X & Y walkers always want 0-1 min even if dim is not present
737 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
738 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700739 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800740 if (ains) {
741 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
742 }
743 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700744 mtls->fep.usr = usr;
745 mtls->fep.usrLen = usrLen;
746 mtls->mSliceSize = 1;
747 mtls->mSliceNum = 0;
748
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700749 mtls->isThreadable = mIsThreadable;
750
Chris Wailesf3712132014-07-16 15:18:30 -0700751 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700752 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700753 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800754 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
755 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700756 }
757 }
758
Chris Wailes44bef6f2014-08-12 13:51:10 -0700759 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800760 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
761 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700762 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800763
764 // All validation passed, ok to launch threads
765 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700766}
767
Jason Sams709a0972012-11-15 18:18:04 -0800768
769void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700770 const Allocation ** ains,
771 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800772 Allocation * aout,
773 const void * usr,
774 uint32_t usrLen,
775 const RsScriptCall *sc) {
776
Matt Wala14ce0072015-07-30 17:30:25 -0700777 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700778
Jason Samsbf2111d2015-01-26 18:13:41 -0800779 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
780 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700781
Jason Samsbf2111d2015-01-26 18:13:41 -0800782 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700783 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800784 mCtx->setTLS(oldTLS);
785 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700786}
787
Matt Wala14ce0072015-07-30 17:30:25 -0700788void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
789 const Allocation *ain,
790 Allocation *aout,
791 const RsScriptCall *sc) {
792 MTLaunchStructReduce mtls;
793
794 if (reduceMtlsSetup(ain, aout, sc, &mtls)) {
795 reduceKernelSetup(slot, &mtls);
796 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
797 mCtx->launchReduce(ain, aout, &mtls);
798 mCtx->setTLS(oldTLS);
799 }
800}
801
David Gross6c1876b2016-01-15 11:52:14 -0800802void RsdCpuScriptImpl::invokeReduceNew(uint32_t slot,
803 const Allocation ** ains, uint32_t inLen,
804 Allocation *aout,
805 const RsScriptCall *sc) {
806 MTLaunchStructReduceNew mtls;
807
808 if (reduceNewMtlsSetup(ains, inLen, aout, sc, &mtls)) {
809 reduceNewKernelSetup(slot, &mtls);
810 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
811 mCtx->launchReduceNew(ains, inLen, aout, &mtls);
812 mCtx->setTLS(oldTLS);
813 }
814}
815
Matt Wala14ce0072015-07-30 17:30:25 -0700816void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800817 mtls->script = this;
818 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800819 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700820 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800821}
822
Matt Wala14ce0072015-07-30 17:30:25 -0700823void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
824 mtls->script = this;
825 mtls->kernel = mScriptExec->getReduceFunction(slot);
826 rsAssert(mtls->kernel != nullptr);
827}
828
David Gross6c1876b2016-01-15 11:52:14 -0800829void RsdCpuScriptImpl::reduceNewKernelSetup(uint32_t slot, MTLaunchStructReduceNew *mtls) {
830 mtls->script = this;
831 mtls->redp.slot = slot;
832
833 const ReduceNewDescription *desc = mScriptExec->getReduceNewDescription(slot);
834 mtls->accumFunc = desc->accumFunc;
835 mtls->initFunc = desc->initFunc; // might legally be nullptr
836 mtls->outFunc = desc->outFunc; // might legally be nullptr
837 mtls->accumSize = desc->accumSize;
838
839 rsAssert(mtls->accumFunc != nullptr);
840}
841
Jason Sams709a0972012-11-15 18:18:04 -0800842int RsdCpuScriptImpl::invokeRoot() {
843 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
844 int ret = mRoot();
845 mCtx->setTLS(oldTLS);
846 return ret;
847}
848
849void RsdCpuScriptImpl::invokeInit() {
850 if (mInit) {
851 mInit();
852 }
853}
854
855void RsdCpuScriptImpl::invokeFreeChildren() {
856 if (mFreeChildren) {
857 mFreeChildren();
858 }
859}
860
861void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
862 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800863 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800864 void * ap = nullptr;
865
866#if defined(__x86_64__)
867 // The invoked function could have input parameter of vector type for example float4 which
868 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
869 // So try to align void* params before passing them into RS exported function.
870
871 if ((uint8_t)(uint64_t)params & 0x0F) {
872 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
873 memcpy(ap, params, paramLength);
874 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800875 ALOGE("x86_64: invokeFunction memalign error, still use params which"
876 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800877 }
878 }
879#endif
Jason Sams709a0972012-11-15 18:18:04 -0800880
881 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800882 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800883 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800884
Yong Chenc246d912015-05-22 18:20:06 +0800885#if defined(__x86_64__)
886 free(ap);
887#endif
888
Jason Sams709a0972012-11-15 18:18:04 -0800889 mCtx->setTLS(oldTLS);
890}
891
892void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
893 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800894 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800895
896 //if (mIntrinsicID) {
897 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
898 //return;
899 //}
900
Yang Nid9bae682015-01-20 15:31:15 -0800901 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800902 if (!destPtr) {
903 //ALOGV("Calling setVar on slot = %i which is null", slot);
904 return;
905 }
906
907 memcpy(destPtr, data, dataLength);
908}
909
Tim Murray9c642392013-04-11 13:29:59 -0700910void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
911 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800912 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700913
Yang Nid9bae682015-01-20 15:31:15 -0800914 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700915 if (!srcPtr) {
916 //ALOGV("Calling setVar on slot = %i which is null", slot);
917 return;
918 }
919 memcpy(data, srcPtr, dataLength);
920}
921
922
Jason Sams709a0972012-11-15 18:18:04 -0800923void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
924 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700925 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800926 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800927 if (!destPtr) {
928 //ALOGV("Calling setVar on slot = %i which is null", slot);
929 return;
930 }
931
932 // We want to look at dimension in terms of integer components,
933 // but dimLength is given in terms of bytes.
934 dimLength /= sizeof(int);
935
936 // Only a single dimension is currently supported.
937 rsAssert(dimLength == 1);
938 if (dimLength == 1) {
939 // First do the increment loop.
940 size_t stride = elem->getSizeBytes();
941 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700942 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800943 elem->incRefs(cVal);
944 cVal += stride;
945 }
946
947 // Decrement loop comes after (to prevent race conditions).
948 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700949 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800950 elem->decRefs(oldVal);
951 oldVal += stride;
952 }
953 }
954
955 memcpy(destPtr, data, dataLength);
956}
957
958void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
959
960 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800961 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800962
Yang Nid9bae682015-01-20 15:31:15 -0800963 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800964 if (!destPtr) {
965 //ALOGV("Calling setVar on slot = %i which is null", slot);
966 return;
967 }
968
Chris Wailes44bef6f2014-08-12 13:51:10 -0700969 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800970 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800971 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800972 ptr = data->mHal.drvState.lod[0].mallocPtr;
973 }
974 memcpy(destPtr, &ptr, sizeof(void *));
975}
976
977void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
978
979 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800980 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800981
Yang Nid9bae682015-01-20 15:31:15 -0800982 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800983 if (!destPtr) {
984 //ALOGV("Calling setVar on slot = %i which is null", slot);
985 return;
986 }
987
Jason Sams05ef73f2014-08-05 14:59:22 -0700988 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800989}
990
Yang Ni062c2872015-02-20 15:20:00 -0800991const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
992 return mScriptExec->getFieldName(slot);
993}
994
Jason Sams709a0972012-11-15 18:18:04 -0800995RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700996 delete mScriptExec;
997 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700998 if (mScriptSO) {
999 dlclose(mScriptSO);
1000 }
Jason Sams709a0972012-11-15 18:18:04 -08001001}
1002
1003Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1004 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001005 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001006 }
1007
1008 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1009 Allocation *a = mBoundAllocs[ct];
1010 if (!a) continue;
1011 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1012 return a;
1013 }
1014 }
1015 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001016 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001017}
1018
Stephen Hines8409d642015-04-28 18:49:56 -07001019int RsdCpuScriptImpl::getGlobalEntries() const {
1020 return mScriptExec->getGlobalEntries();
1021}
1022
1023const char * RsdCpuScriptImpl::getGlobalName(int i) const {
1024 return mScriptExec->getGlobalName(i);
1025}
1026
1027const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
1028 return mScriptExec->getGlobalAddress(i);
1029}
1030
1031size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
1032 return mScriptExec->getGlobalSize(i);
1033}
1034
Stephen Hines5aa018c2015-05-20 18:09:57 -07001035uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
1036 return mScriptExec->getGlobalProperties(i);
1037}
1038
Chris Wailesf3712132014-07-16 15:18:30 -07001039void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1040 uint32_t inLen, Allocation * aout,
1041 const void * usr, uint32_t usrLen,
1042 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001043
Chris Wailesf3712132014-07-16 15:18:30 -07001044void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1045 uint32_t inLen, Allocation * aout,
1046 const void * usr, uint32_t usrLen,
1047 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001048
Jason Sams709a0972012-11-15 18:18:04 -08001049
1050}
1051}