blob: 545e92f32e98ebe76991d9761478701e087fdc54 [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
Jean-Luc Brouillet03fab682017-02-16 21:07:20 -080028 #include <bcc/Config.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070029 #include <bcinfo/MetadataExtractor.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070030
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -080031 #include <zlib.h>
32 #include <sys/file.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070033 #include <sys/types.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070034 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080035
36 #include <string>
37 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070038#endif
Jason Sams709a0972012-11-15 18:18:04 -080039
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080040#include <set>
41#include <string>
42#include <dlfcn.h>
43#include <stdlib.h>
44#include <string.h>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080045#include <iostream>
Yang Nicb170152015-04-16 10:27:02 -070046#include <sstream>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080047
Stephen Hinesba17ae42013-06-05 17:18:04 -070048namespace {
Stephen Hines8409d642015-04-28 18:49:56 -070049
50static const bool kDebugGlobalVariables = false;
51
Matt Wala14ce0072015-07-30 17:30:25 -070052static bool allocationLODIsNull(const android::renderscript::Allocation *alloc) {
53 // Even if alloc != nullptr, mallocPtr could be null if
54 // IO_OUTPUT/IO_INPUT with no bound surface.
55 return alloc && alloc->mHal.drvState.lod[0].mallocPtr == nullptr;
56}
57
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080058#ifndef RS_COMPATIBILITY_LIB
59
Stephen Hinesba17ae42013-06-05 17:18:04 -070060static bool is_force_recompile() {
Miao Wang82e135c2017-02-27 23:35:35 -080061 char buf[PROP_VALUE_MAX];
Stephen Hinesba17ae42013-06-05 17:18:04 -070062
63 // Re-compile if floating point precision has been overridden.
Miao Wang2a611682017-02-27 17:36:40 -080064 android::renderscript::property_get("debug.rs.precision", buf, "");
Stephen Hinesba17ae42013-06-05 17:18:04 -070065 if (buf[0] != '\0') {
66 return true;
67 }
68
69 // Re-compile if debug.rs.forcerecompile is set.
Miao Wang2a611682017-02-27 17:36:40 -080070 android::renderscript::property_get("debug.rs.forcerecompile", buf, "0");
Stephen Hinesba17ae42013-06-05 17:18:04 -070071 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
72 return true;
73 } else {
74 return false;
75 }
Stephen Hinesba17ae42013-06-05 17:18:04 -070076}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070077
Chris Wailes6847e732014-08-11 17:30:51 -070078static void setCompileArguments(std::vector<const char*>* args,
79 const std::string& bcFileName,
80 const char* cacheDir, const char* resName,
81 const char* core_lib, bool useRSDebugContext,
Stephen Hines8409d642015-04-28 18:49:56 -070082 const char* bccPluginName, bool emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +010083 int optLevel, bool emitGlobalInfoSkipConstant) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070084 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080085 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080086 args->push_back("-unroll-runtime");
87 args->push_back("-scalarize-load-store");
Stephen Hines8409d642015-04-28 18:49:56 -070088 if (emitGlobalInfo) {
89 args->push_back("-rs-global-info");
90 if (emitGlobalInfoSkipConstant) {
91 args->push_back("-rs-global-info-skip-constant");
92 }
93 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070094 args->push_back("-o");
95 args->push_back(resName);
96 args->push_back("-output_path");
97 args->push_back(cacheDir);
98 args->push_back("-bclib");
99 args->push_back(core_lib);
100 args->push_back("-mtriple");
101 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
verena beckhamf5029802015-05-22 16:51:42 +0100102 args->push_back("-O");
103
104 switch (optLevel) {
Yang Ni7a106ad2016-03-10 16:06:36 -0800105 case 0:
verena beckhamf5029802015-05-22 16:51:42 +0100106 args->push_back("0");
107 break;
Yang Ni7a106ad2016-03-10 16:06:36 -0800108 case 3:
verena beckhamf5029802015-05-22 16:51:42 +0100109 args->push_back("3");
110 break;
111 default:
112 ALOGW("Expected optimization level of 0 or 3. Received %d", optLevel);
113 args->push_back("3");
114 break;
115 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700116
Tim Murray358ffb82014-12-09 11:53:06 -0800117 // Enable workaround for A53 codegen by default.
118#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
119 args->push_back("-aarch64-fix-cortex-a53-835769");
120#endif
121
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700122 // Execute the bcc compiler.
123 if (useRSDebugContext) {
124 args->push_back("-rs-debug-ctx");
125 } else {
126 // Only load additional libraries for compiles that don't use
127 // the debug context.
128 if (bccPluginName && strlen(bccPluginName) > 0) {
129 args->push_back("-load");
130 args->push_back(bccPluginName);
131 }
132 }
133
Stephen Hines45e753a2015-01-19 20:58:44 -0800134 args->push_back("-fPIC");
135 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800136
Chris Wailes6847e732014-08-11 17:30:51 -0700137 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700138 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700139}
140
Chris Wailes6847e732014-08-11 17:30:51 -0700141static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700142 const char *bitcode,
143 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700144 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700145 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700146
Chris Wailes6847e732014-08-11 17:30:51 -0700147 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700148 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700149 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700150 return false;
151 }
152 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
153 fclose(bcfile);
154 if (nwritten != bitcodeSize) {
155 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700156 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700157 return false;
158 }
159
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700160 return android::renderscript::rsuExecuteCommand(
161 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
162 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700163}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700164
Stephen Hines6a236ad2015-06-22 14:51:30 -0700165// The checksum is unnecessary under a few conditions, since the primary
166// use-case for it is debugging. If we are loading something from the
167// system partition (read-only), we know that it was precompiled as part of
168// application ahead of time (and thus the checksum is completely
169// unnecessary). The checksum is also unnecessary on release (non-debug)
170// builds, as the only way to get a shared object is to have compiled the
171// script once already. On a release build, there is no way to adjust the
172// other libraries/dependencies, and so the only reason to recompile would
173// be for a source APK change or an OTA. In either case, the APK would be
174// reinstalled, which would already clear the code_cache/ directory.
175bool isChecksumNeeded(const char *cacheDir) {
176 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
177 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
178 return false;
Miao Wang82e135c2017-02-27 23:35:35 -0800179 char buf[PROP_VALUE_MAX];
Miao Wang2a611682017-02-27 17:36:40 -0800180 android::renderscript::property_get("ro.debuggable", buf, "");
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800181 return (buf[0] == '1');
182}
183
184bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
185 int FD = open(fileName, O_RDONLY);
186 if (FD == -1) {
187 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
188 return false;
189 }
190
191 char buf[256];
192 while (true) {
193 ssize_t nread = read(FD, buf, sizeof(buf));
194 if (nread < 0) { // bail out on failed read
195 ALOGE("Error while computing checksum for file \'%s\'", fileName);
196 return false;
197 }
198
199 checksum = adler32(checksum, (const unsigned char *) buf, nread);
200 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
201 break;
202 }
203
Elliott Hughes2df57672015-05-15 17:06:47 -0700204 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800205 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
206 return false;
207 }
208 return true;
209}
Yang Nicb170152015-04-16 10:27:02 -0700210
211#endif // !defined(RS_COMPATIBILITY_LIB)
212} // namespace
213
214namespace android {
215namespace renderscript {
216
217#ifndef RS_COMPATIBILITY_LIB
218
219uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
220 const char *commandLine,
221 const char** bccFiles, size_t numFiles) {
222 uint32_t checksum = adler32(0L, Z_NULL, 0);
223
224 // include checksum of bitcode
225 if (bitcode != nullptr && bitcodeSize > 0) {
226 checksum = adler32(checksum, bitcode, bitcodeSize);
227 }
228
229 // include checksum of command line arguments
230 checksum = adler32(checksum, (const unsigned char *) commandLine,
231 strlen(commandLine));
232
233 // include checksum of bccFiles
234 for (size_t i = 0; i < numFiles; i++) {
235 const char* bccFile = bccFiles[i];
236 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
237 // return empty checksum instead of something partial/corrupt
238 return 0;
239 }
240 }
241
242 return checksum;
243}
244
Yang Nif02a2b02015-04-07 16:00:31 -0700245#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800246
Jason Sams709a0972012-11-15 18:18:04 -0800247RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
248 mCtx = ctx;
249 mScript = s;
250
Chris Wailes44bef6f2014-08-12 13:51:10 -0700251 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800252
Chris Wailes44bef6f2014-08-12 13:51:10 -0700253 mRoot = nullptr;
254 mRootExpand = nullptr;
255 mInit = nullptr;
256 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800257 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800258
Chris Wailes44bef6f2014-08-12 13:51:10 -0700259 mBoundAllocs = nullptr;
260 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800261 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800262
Yang Nicb170152015-04-16 10:27:02 -0700263 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800264 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800265}
266
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800267bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800268 // The shared object may have an invalid build checksum.
269 // Validate and fail early.
270 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Niade31372016-04-06 09:34:34 -0700271 mScriptSO, 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 }
Matt Wala14ce0072015-07-30 17:30:25 -0700285 mInit = (InitOrDtorFunc_t) dlsym(mScriptSO, "init");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800286 if (mInit) {
287 //ALOGE("Found init(): %p", mInit);
288 }
Matt Wala14ce0072015-07-30 17:30:25 -0700289 mFreeChildren = (InitOrDtorFunc_t) dlsym(mScriptSO, ".rs.dtor");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800290 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;
Yang Ni578419f2016-06-27 16:12:25 -0700380 std::string checksumStr(ss.str());
381 compileArguments.push_back(checksumStr.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
Miao Wang82e135c2017-02-27 23:35:35 -0800425 mBitcodeFilePath.assign(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) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200456 const char* defaultLib = SYSLIBPATH_BC"/libclcore.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700457
458 // If we're debugging, use the debug library.
459 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200460 return SYSLIBPATH_BC"/libclcore_debug.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700461 }
462
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000463 if (ME.hasDebugInfo()) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200464 return SYSLIBPATH_BC"/libclcore_g.bc";
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000465 }
466
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700467 // If a callback has been registered to specify a library, use that.
468 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700469 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700470 return selectRTCallback((const char*)bitcode, bitcodeSize);
471 }
472
473 // Check for a platform specific library
474#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
475 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700476 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700477 // NEON-capable ARMv7a devices can use an accelerated math library
478 // for all reduced precision scripts.
479 // ARMv8 does not use NEON, as ASIMD can be used with all precision
480 // levels.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200481 return SYSLIBPATH_BC"/libclcore_neon.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700482 } else {
483 return defaultLib;
484 }
485#elif defined(__i386__) || defined(__x86_64__)
486 // x86 devices will use an optimized library.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200487 return SYSLIBPATH_BC"/libclcore_x86.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700488#else
489 return defaultLib;
490#endif
491}
492
493#endif
494
Jason Sams709a0972012-11-15 18:18:04 -0800495void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700496 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800497 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700498 script->mHal.info.exportedReduceCount = mScriptExec->getExportedReduceCount();
499 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800500 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800501 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800502 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
503 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700504
505 // Bug, need to stash in metadata
506 if (mRootExpand) {
507 script->mHal.info.root = mRootExpand;
508 } else {
509 script->mHal.info.root = mRoot;
510 }
Jason Sams709a0972012-11-15 18:18:04 -0800511}
512
Matt Wala14ce0072015-07-30 17:30:25 -0700513// Set up the launch dimensions, and write the values of the launch
514// dimensions into the mtls start/end fields.
515//
516// Inputs:
517// baseDim - base shape of the input
518// sc - used to constrain the launch dimensions
519//
520// Returns:
521// True on success, false on failure to set up
522bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
523 const RsLaunchDimensions &baseDim,
524 const RsScriptCall *sc) {
525 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800526
Matt Wala14ce0072015-07-30 17:30:25 -0700527#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
528 if (!sc || (sc->SC_FIELD##End == 0)) { \
529 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
530 } else { \
531 mtls->start.DIM_FIELD = \
532 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
533 mtls->end.DIM_FIELD = \
534 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
535 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
536 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
537 "Failed to launch kernel; Invalid " \
538 #SC_FIELD "Start or " #SC_FIELD "End."); \
539 return false; \
540 } \
541 }} while(0)
542
543 SET_UP_DIMENSION(x, x);
544 SET_UP_DIMENSION(y, y);
545 SET_UP_DIMENSION(z, z);
Yang Ni7784b5d2016-05-04 09:44:48 -0700546 // Checks and setup of fields other than x, y, z are ignored, since those
547 // fields are not used in the runtime and are not visible in the Java API.
Matt Wala14ce0072015-07-30 17:30:25 -0700548#undef SET_UP_DIMENSION
549
550 return true;
551}
552
David Grossfc7ab792016-06-01 14:45:47 -0700553// Preliminary work to prepare a general reduce-style kernel for launch.
554bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation ** ains,
555 uint32_t inLen,
556 const Allocation * aout,
Matt Wala14ce0072015-07-30 17:30:25 -0700557 const RsScriptCall *sc,
558 MTLaunchStructReduce *mtls) {
David Gross6c1876b2016-01-15 11:52:14 -0800559 rsAssert(ains && (inLen >= 1) && aout);
David Grossfc7ab792016-06-01 14:45:47 -0700560 memset(mtls, 0, sizeof(MTLaunchStructReduce));
David Gross6c1876b2016-01-15 11:52:14 -0800561 mtls->dimPtr = &mtls->redp.dim;
562
563 for (int index = inLen; --index >= 0;) {
564 if (allocationLODIsNull(ains[index])) {
565 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
566 "reduce called with null in allocations");
567 return false;
568 }
569 }
570
571 if (allocationLODIsNull(aout)) {
572 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
573 "reduce called with null out allocation");
574 return false;
575 }
576
577 const Allocation *ain0 = ains[0];
578 const Type *inType = ain0->getType();
579
580 mtls->redp.dim.x = inType->getDimX();
581 mtls->redp.dim.y = inType->getDimY();
582 mtls->redp.dim.z = inType->getDimZ();
583
584 for (int Index = inLen; --Index >= 1;) {
585 if (!ain0->hasSameDims(ains[Index])) {
586 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
587 "Failed to launch reduction kernel;"
588 "dimensions of input allocations do not match.");
589 return false;
590 }
591 }
592
593 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
594 return false;
595 }
596
597 // The X & Y walkers always want 0-1 min even if dim is not present
598 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
599 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
600
601 mtls->rs = mCtx;
602
David Gross35dbc8c2016-03-29 13:48:41 -0700603 mtls->mSliceNum = 0;
604 mtls->mSliceSize = 1;
605 mtls->isThreadable = mIsThreadable;
David Gross6c1876b2016-01-15 11:52:14 -0800606
607 // Set up output,
608 mtls->redp.outLen = 1;
609 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
610 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
611
612 // Set up input.
613 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
614 mtls->redp.inLen = inLen;
615 for (int index = inLen; --index >= 0;) {
616 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
617 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
618 }
619
620 // All validation passed, ok to launch threads
621 return true;
622}
623
Matt Wala14ce0072015-07-30 17:30:25 -0700624// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800625bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700626 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700627 Allocation * aout,
628 const void * usr, uint32_t usrLen,
629 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700630 MTLaunchStructForEach *mtls) {
Miao Wang68e00892016-02-25 12:44:10 -0800631 if (ains == nullptr && inLen != 0) {
632 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
633 "rsForEach called with none-zero inLen with null in allocations");
634 return false;
635 }
636
Matt Wala14ce0072015-07-30 17:30:25 -0700637 memset(mtls, 0, sizeof(MTLaunchStructForEach));
638 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700639
Chris Wailesf3712132014-07-16 15:18:30 -0700640 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700641 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700642 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
643 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800644 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700645 }
646 }
647
Matt Wala14ce0072015-07-30 17:30:25 -0700648 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700649 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
650 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800651 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700652 }
653
Chris Wailesf3712132014-07-16 15:18:30 -0700654 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700655 const Allocation *ain0 = ains[0];
656 const Type *inType = ain0->getType();
657
Jason Samsc0d68472015-01-20 14:29:52 -0800658 mtls->fep.dim.x = inType->getDimX();
659 mtls->fep.dim.y = inType->getDimY();
660 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700661
662 for (int Index = inLen; --Index >= 1;) {
663 if (!ain0->hasSameDims(ains[Index])) {
664 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
David Gross6c1876b2016-01-15 11:52:14 -0800665 "Failed to launch kernel; dimensions of input"
Yang Nie8f9fba2015-01-30 08:55:10 -0800666 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800667 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700668 }
669 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700670 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700671 const Type *outType = aout->getType();
672
Jason Samsc0d68472015-01-20 14:29:52 -0800673 mtls->fep.dim.x = outType->getDimX();
674 mtls->fep.dim.y = outType->getDimY();
675 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700676
Jason Samsa9139c72015-04-16 15:11:04 -0700677 } else if (sc != nullptr) {
678 mtls->fep.dim.x = sc->xEnd;
679 mtls->fep.dim.y = sc->yEnd;
680 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700681 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700682 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
683 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800684 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700685 }
686
Chris Wailes44bef6f2014-08-12 13:51:10 -0700687 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700688 if (!ains[0]->hasSameDims(aout)) {
689 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
690 "Failed to launch kernel; dimensions of input and output allocations do not match.");
691
Jason Samsbf2111d2015-01-26 18:13:41 -0800692 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700693 }
694 }
695
Matt Wala14ce0072015-07-30 17:30:25 -0700696 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
697 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700698 }
699
Jason Samsbf2111d2015-01-26 18:13:41 -0800700 // The X & Y walkers always want 0-1 min even if dim is not present
701 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
702 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700703 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800704 if (ains) {
705 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
706 }
707 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700708 mtls->fep.usr = usr;
709 mtls->fep.usrLen = usrLen;
710 mtls->mSliceSize = 1;
711 mtls->mSliceNum = 0;
712
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700713 mtls->isThreadable = mIsThreadable;
714
Chris Wailesf3712132014-07-16 15:18:30 -0700715 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700716 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700717 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800718 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
719 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700720 }
721 }
722
Chris Wailes44bef6f2014-08-12 13:51:10 -0700723 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800724 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
725 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700726 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800727
728 // All validation passed, ok to launch threads
729 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700730}
731
Jason Sams709a0972012-11-15 18:18:04 -0800732
733void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700734 const Allocation ** ains,
735 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800736 Allocation * aout,
737 const void * usr,
738 uint32_t usrLen,
739 const RsScriptCall *sc) {
740
Matt Wala14ce0072015-07-30 17:30:25 -0700741 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700742
Jason Samsbf2111d2015-01-26 18:13:41 -0800743 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
744 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700745
Jason Samsbf2111d2015-01-26 18:13:41 -0800746 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700747 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800748 mCtx->setTLS(oldTLS);
749 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700750}
751
Matt Wala14ce0072015-07-30 17:30:25 -0700752void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
David Grossfc7ab792016-06-01 14:45:47 -0700753 const Allocation ** ains, uint32_t inLen,
Matt Wala14ce0072015-07-30 17:30:25 -0700754 Allocation *aout,
755 const RsScriptCall *sc) {
David Grossfc7ab792016-06-01 14:45:47 -0700756 MTLaunchStructReduce mtls;
Matt Wala14ce0072015-07-30 17:30:25 -0700757
David Grossfc7ab792016-06-01 14:45:47 -0700758 if (reduceMtlsSetup(ains, inLen, aout, sc, &mtls)) {
759 reduceKernelSetup(slot, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800760 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
David Grossfc7ab792016-06-01 14:45:47 -0700761 mCtx->launchReduce(ains, inLen, aout, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800762 mCtx->setTLS(oldTLS);
763 }
764}
765
Matt Wala14ce0072015-07-30 17:30:25 -0700766void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800767 mtls->script = this;
768 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800769 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700770 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800771}
772
Matt Wala14ce0072015-07-30 17:30:25 -0700773void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
774 mtls->script = this;
David Gross6c1876b2016-01-15 11:52:14 -0800775 mtls->redp.slot = slot;
776
David Grossfc7ab792016-06-01 14:45:47 -0700777 const ReduceDescription *desc = mScriptExec->getReduceDescription(slot);
David Gross6c1876b2016-01-15 11:52:14 -0800778 mtls->accumFunc = desc->accumFunc;
779 mtls->initFunc = desc->initFunc; // might legally be nullptr
David Gross35dbc8c2016-03-29 13:48:41 -0700780 mtls->combFunc = desc->combFunc; // might legally be nullptr
David Gross6c1876b2016-01-15 11:52:14 -0800781 mtls->outFunc = desc->outFunc; // might legally be nullptr
782 mtls->accumSize = desc->accumSize;
783
784 rsAssert(mtls->accumFunc != nullptr);
785}
786
Jason Sams709a0972012-11-15 18:18:04 -0800787int RsdCpuScriptImpl::invokeRoot() {
788 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
789 int ret = mRoot();
790 mCtx->setTLS(oldTLS);
791 return ret;
792}
793
794void RsdCpuScriptImpl::invokeInit() {
795 if (mInit) {
796 mInit();
797 }
798}
799
800void RsdCpuScriptImpl::invokeFreeChildren() {
801 if (mFreeChildren) {
802 mFreeChildren();
803 }
804}
805
806void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
807 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800808 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800809 void * ap = nullptr;
810
811#if defined(__x86_64__)
812 // The invoked function could have input parameter of vector type for example float4 which
813 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
814 // So try to align void* params before passing them into RS exported function.
815
816 if ((uint8_t)(uint64_t)params & 0x0F) {
817 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
818 memcpy(ap, params, paramLength);
819 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800820 ALOGE("x86_64: invokeFunction memalign error, still use params which"
821 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800822 }
823 }
824#endif
Jason Sams709a0972012-11-15 18:18:04 -0800825
826 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800827 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800828 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800829
Yong Chenc246d912015-05-22 18:20:06 +0800830#if defined(__x86_64__)
831 free(ap);
832#endif
833
Jason Sams709a0972012-11-15 18:18:04 -0800834 mCtx->setTLS(oldTLS);
835}
836
837void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
838 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800839 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800840
841 //if (mIntrinsicID) {
842 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
843 //return;
844 //}
845
Yang Nid9bae682015-01-20 15:31:15 -0800846 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800847 if (!destPtr) {
848 //ALOGV("Calling setVar on slot = %i which is null", slot);
849 return;
850 }
851
852 memcpy(destPtr, data, dataLength);
853}
854
Tim Murray9c642392013-04-11 13:29:59 -0700855void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
856 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800857 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700858
Yang Nid9bae682015-01-20 15:31:15 -0800859 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700860 if (!srcPtr) {
861 //ALOGV("Calling setVar on slot = %i which is null", slot);
862 return;
863 }
864 memcpy(data, srcPtr, dataLength);
865}
866
867
Jason Sams709a0972012-11-15 18:18:04 -0800868void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
869 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700870 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800871 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800872 if (!destPtr) {
873 //ALOGV("Calling setVar on slot = %i which is null", slot);
874 return;
875 }
876
877 // We want to look at dimension in terms of integer components,
878 // but dimLength is given in terms of bytes.
879 dimLength /= sizeof(int);
880
881 // Only a single dimension is currently supported.
882 rsAssert(dimLength == 1);
883 if (dimLength == 1) {
884 // First do the increment loop.
885 size_t stride = elem->getSizeBytes();
886 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700887 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800888 elem->incRefs(cVal);
889 cVal += stride;
890 }
891
892 // Decrement loop comes after (to prevent race conditions).
893 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700894 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800895 elem->decRefs(oldVal);
896 oldVal += stride;
897 }
898 }
899
900 memcpy(destPtr, data, dataLength);
901}
902
903void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
904
905 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800906 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800907
Yang Nid9bae682015-01-20 15:31:15 -0800908 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800909 if (!destPtr) {
910 //ALOGV("Calling setVar on slot = %i which is null", slot);
911 return;
912 }
913
Chris Wailes44bef6f2014-08-12 13:51:10 -0700914 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800915 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800916 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800917 ptr = data->mHal.drvState.lod[0].mallocPtr;
918 }
919 memcpy(destPtr, &ptr, sizeof(void *));
920}
921
922void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
923
924 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800925 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800926
Yang Nid9bae682015-01-20 15:31:15 -0800927 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800928 if (!destPtr) {
929 //ALOGV("Calling setVar on slot = %i which is null", slot);
930 return;
931 }
932
Jason Sams05ef73f2014-08-05 14:59:22 -0700933 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800934}
935
Yang Ni062c2872015-02-20 15:20:00 -0800936const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
937 return mScriptExec->getFieldName(slot);
938}
939
Jason Sams709a0972012-11-15 18:18:04 -0800940RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700941 delete mScriptExec;
942 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700943 if (mScriptSO) {
944 dlclose(mScriptSO);
945 }
Jason Sams709a0972012-11-15 18:18:04 -0800946}
947
948Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
949 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700950 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800951 }
952
953 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
954 Allocation *a = mBoundAllocs[ct];
955 if (!a) continue;
956 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
957 return a;
958 }
959 }
960 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700961 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800962}
963
Stephen Hines8409d642015-04-28 18:49:56 -0700964int RsdCpuScriptImpl::getGlobalEntries() const {
965 return mScriptExec->getGlobalEntries();
966}
967
968const char * RsdCpuScriptImpl::getGlobalName(int i) const {
969 return mScriptExec->getGlobalName(i);
970}
971
972const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
973 return mScriptExec->getGlobalAddress(i);
974}
975
976size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
977 return mScriptExec->getGlobalSize(i);
978}
979
Stephen Hines5aa018c2015-05-20 18:09:57 -0700980uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
981 return mScriptExec->getGlobalProperties(i);
982}
983
Chris Wailesf3712132014-07-16 15:18:30 -0700984void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
985 uint32_t inLen, Allocation * aout,
986 const void * usr, uint32_t usrLen,
987 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700988
Chris Wailesf3712132014-07-16 15:18:30 -0700989void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
990 uint32_t inLen, Allocation * aout,
991 const void * usr, uint32_t usrLen,
992 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700993
Jason Sams709a0972012-11-15 18:18:04 -0800994
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800995} // namespace renderscript
996} // namespace android