blob: a495246a9bb2017023b439ba188039dec1e7ad61 [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
Chris Wailes6847e732014-08-11 17:30:51 -070060static void setCompileArguments(std::vector<const char*>* args,
61 const std::string& bcFileName,
62 const char* cacheDir, const char* resName,
63 const char* core_lib, bool useRSDebugContext,
Stephen Hines8409d642015-04-28 18:49:56 -070064 const char* bccPluginName, bool emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +010065 int optLevel, bool emitGlobalInfoSkipConstant) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070066 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080067 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080068 args->push_back("-unroll-runtime");
69 args->push_back("-scalarize-load-store");
Stephen Hines8409d642015-04-28 18:49:56 -070070 if (emitGlobalInfo) {
71 args->push_back("-rs-global-info");
72 if (emitGlobalInfoSkipConstant) {
73 args->push_back("-rs-global-info-skip-constant");
74 }
75 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070076 args->push_back("-o");
77 args->push_back(resName);
78 args->push_back("-output_path");
79 args->push_back(cacheDir);
80 args->push_back("-bclib");
81 args->push_back(core_lib);
82 args->push_back("-mtriple");
83 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
verena beckhamf5029802015-05-22 16:51:42 +010084 args->push_back("-O");
85
86 switch (optLevel) {
Yang Ni7a106ad2016-03-10 16:06:36 -080087 case 0:
verena beckhamf5029802015-05-22 16:51:42 +010088 args->push_back("0");
89 break;
Yang Ni7a106ad2016-03-10 16:06:36 -080090 case 3:
verena beckhamf5029802015-05-22 16:51:42 +010091 args->push_back("3");
92 break;
93 default:
94 ALOGW("Expected optimization level of 0 or 3. Received %d", optLevel);
95 args->push_back("3");
96 break;
97 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070098
Tim Murray358ffb82014-12-09 11:53:06 -080099 // Enable workaround for A53 codegen by default.
100#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
101 args->push_back("-aarch64-fix-cortex-a53-835769");
102#endif
103
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700104 // Execute the bcc compiler.
105 if (useRSDebugContext) {
106 args->push_back("-rs-debug-ctx");
107 } else {
108 // Only load additional libraries for compiles that don't use
109 // the debug context.
110 if (bccPluginName && strlen(bccPluginName) > 0) {
Jiyong Park8d421ca2017-06-13 18:09:33 +0900111#ifdef __ANDROID__
112 // For Android, -plugin option must be used in order to load the
113 // vendor plugin from the sphal namespace.
114 args->push_back("-plugin");
115#else
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700116 args->push_back("-load");
Jiyong Park8d421ca2017-06-13 18:09:33 +0900117#endif
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700118 args->push_back(bccPluginName);
119 }
120 }
121
Stephen Hines45e753a2015-01-19 20:58:44 -0800122 args->push_back("-fPIC");
123 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800124
Chris Wailes6847e732014-08-11 17:30:51 -0700125 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700126 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700127}
128
Chris Wailes6847e732014-08-11 17:30:51 -0700129static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700130 const char *bitcode,
131 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700132 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700133 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700134
Nick Kralevich95e17022018-12-11 14:35:57 -0800135 FILE *bcfile = fopen(bcFileName.c_str(), "we");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700136 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700137 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700138 return false;
139 }
140 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
141 fclose(bcfile);
142 if (nwritten != bitcodeSize) {
143 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700144 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700145 return false;
146 }
147
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700148 return android::renderscript::rsuExecuteCommand(
149 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
150 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700151}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700152
Stephen Hines6a236ad2015-06-22 14:51:30 -0700153// The checksum is unnecessary under a few conditions, since the primary
154// use-case for it is debugging. If we are loading something from the
155// system partition (read-only), we know that it was precompiled as part of
156// application ahead of time (and thus the checksum is completely
157// unnecessary). The checksum is also unnecessary on release (non-debug)
158// builds, as the only way to get a shared object is to have compiled the
159// script once already. On a release build, there is no way to adjust the
160// other libraries/dependencies, and so the only reason to recompile would
161// be for a source APK change or an OTA. In either case, the APK would be
162// reinstalled, which would already clear the code_cache/ directory.
163bool isChecksumNeeded(const char *cacheDir) {
Justin Yune0f1e2e2017-12-05 18:42:57 +0900164 static const std::string sysLibPathVndk = getVndkSysLibPath();
Stephen Hines6a236ad2015-06-22 14:51:30 -0700165 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
Justin Yune0f1e2e2017-12-05 18:42:57 +0900166 (::strcmp(sysLibPathVndk.c_str(), cacheDir) == 0) ||
Stephen Hines6a236ad2015-06-22 14:51:30 -0700167 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
168 return false;
Miao Wang82e135c2017-02-27 23:35:35 -0800169 char buf[PROP_VALUE_MAX];
Miao Wang2a611682017-02-27 17:36:40 -0800170 android::renderscript::property_get("ro.debuggable", buf, "");
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800171 return (buf[0] == '1');
172}
173
174bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
Nick Kralevich95e17022018-12-11 14:35:57 -0800175 int FD = open(fileName, O_RDONLY | O_CLOEXEC);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800176 if (FD == -1) {
177 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
178 return false;
179 }
180
181 char buf[256];
182 while (true) {
183 ssize_t nread = read(FD, buf, sizeof(buf));
184 if (nread < 0) { // bail out on failed read
185 ALOGE("Error while computing checksum for file \'%s\'", fileName);
186 return false;
187 }
188
189 checksum = adler32(checksum, (const unsigned char *) buf, nread);
190 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
191 break;
192 }
193
Elliott Hughes2df57672015-05-15 17:06:47 -0700194 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800195 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
196 return false;
197 }
198 return true;
199}
Yang Nicb170152015-04-16 10:27:02 -0700200
201#endif // !defined(RS_COMPATIBILITY_LIB)
202} // namespace
203
204namespace android {
205namespace renderscript {
206
207#ifndef RS_COMPATIBILITY_LIB
208
209uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
210 const char *commandLine,
211 const char** bccFiles, size_t numFiles) {
212 uint32_t checksum = adler32(0L, Z_NULL, 0);
213
214 // include checksum of bitcode
215 if (bitcode != nullptr && bitcodeSize > 0) {
216 checksum = adler32(checksum, bitcode, bitcodeSize);
217 }
218
219 // include checksum of command line arguments
220 checksum = adler32(checksum, (const unsigned char *) commandLine,
221 strlen(commandLine));
222
223 // include checksum of bccFiles
224 for (size_t i = 0; i < numFiles; i++) {
225 const char* bccFile = bccFiles[i];
226 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
227 // return empty checksum instead of something partial/corrupt
228 return 0;
229 }
230 }
231
232 return checksum;
233}
234
Yang Nif02a2b02015-04-07 16:00:31 -0700235#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800236
Jason Sams709a0972012-11-15 18:18:04 -0800237RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
238 mCtx = ctx;
239 mScript = s;
240
Chris Wailes44bef6f2014-08-12 13:51:10 -0700241 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800242
Chris Wailes44bef6f2014-08-12 13:51:10 -0700243 mRoot = nullptr;
244 mRootExpand = nullptr;
245 mInit = nullptr;
246 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800247 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800248
Chris Wailes44bef6f2014-08-12 13:51:10 -0700249 mBoundAllocs = nullptr;
250 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800251 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800252
Yang Nicb170152015-04-16 10:27:02 -0700253 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800254 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800255}
256
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800257bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800258 // The shared object may have an invalid build checksum.
259 // Validate and fail early.
260 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Niade31372016-04-06 09:34:34 -0700261 mScriptSO, mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800262
263 if (mScriptExec == nullptr) {
264 return false;
265 }
266
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800267 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
268 if (mRoot) {
269 //ALOGE("Found root(): %p", mRoot);
270 }
271 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
272 if (mRootExpand) {
273 //ALOGE("Found root.expand(): %p", mRootExpand);
274 }
Matt Wala14ce0072015-07-30 17:30:25 -0700275 mInit = (InitOrDtorFunc_t) dlsym(mScriptSO, "init");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800276 if (mInit) {
277 //ALOGE("Found init(): %p", mInit);
278 }
Matt Wala14ce0072015-07-30 17:30:25 -0700279 mFreeChildren = (InitOrDtorFunc_t) dlsym(mScriptSO, ".rs.dtor");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800280 if (mFreeChildren) {
281 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
282 }
283
Yang Nid9bae682015-01-20 15:31:15 -0800284 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800285 if (varCount > 0) {
286 mBoundAllocs = new Allocation *[varCount];
287 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
288 }
289
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800290 mIsThreadable = mScriptExec->getThreadable();
291 //ALOGE("Script isThreadable? %d", mIsThreadable);
292
Stephen Hines8409d642015-04-28 18:49:56 -0700293 if (kDebugGlobalVariables) {
294 mScriptExec->dumpGlobalInfo();
295 }
296
Yang Nid9bae682015-01-20 15:31:15 -0800297 return true;
298}
299
Jason Sams709a0972012-11-15 18:18:04 -0800300bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
301 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800302 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800303 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
304 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800305 //ALOGE("rsdScriptInit %p %p", rsc, script);
306
307 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700308#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800309 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800310
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700311 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
312 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700313 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700314 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700315 return false;
316 }
317
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700318 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700319
320 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800321 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700322 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700323
verena beckhamf5029802015-05-22 16:51:42 +0100324 int optLevel = mCtx->getContext()->getOptLevel();
325
Chris Wailes6847e732014-08-11 17:30:51 -0700326 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700327 bcFileName.append("/");
328 bcFileName.append(resName);
329 bcFileName.append(".bc");
330
331 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700332 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
333 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700334 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700335 useRSDebugContext, bccPluginName, emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +0100336 optLevel, emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800337
Stephen Hines6a236ad2015-06-22 14:51:30 -0700338 mChecksumNeeded = isChecksumNeeded(cacheDir);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800339 if (mChecksumNeeded) {
340 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
341 core_lib,
342 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700343
344 // The last argument of compileArguments is a nullptr, so remove 1 from
345 // the size.
346 std::unique_ptr<const char> compileCommandLine(
347 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
348
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800349 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
350 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700351 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800352
Yang Nicb170152015-04-16 10:27:02 -0700353 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800354 // cannot compute checksum but verification is enabled
355 mCtx->unlockMutex();
356 return false;
357 }
358 }
359 else {
Jeff Sharkey4ed859f2020-07-31 09:30:15 -0600360 // add a placeholder/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700361 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800362 }
363
364 // Append build checksum to commandline
365 // Handle the terminal nullptr in compileArguments
366 compileArguments.pop_back();
367 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700368 std::stringstream ss;
369 ss << std::hex << mBuildChecksum;
Yang Ni578419f2016-06-27 16:12:25 -0700370 std::string checksumStr(ss.str());
371 compileArguments.push_back(checksumStr.c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800372 compileArguments.push_back(nullptr);
373
Yang Ni29791302017-05-01 15:53:23 -0700374 const bool reuse = !is_force_recompile() && !useRSDebugContext;
375 if (reuse) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800376 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800377
378 // Read RS info from the shared object to detect checksum mismatch
379 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
380 dlclose(mScriptSO);
381 mScriptSO = nullptr;
382 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700383 }
384
Yang Ni29791302017-05-01 15:53:23 -0700385 // If reuse is desired and we can't, it's either not there or out of date.
386 // We compile the bit code and try loading again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800387 if (mScriptSO == nullptr) {
388 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700389 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800390 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700391 ALOGE("bcc: FAILS to compile '%s'", resName);
392 mCtx->unlockMutex();
393 return false;
394 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800395
Yang Ni29791302017-05-01 15:53:23 -0700396 std::string SOPath;
397
398 if (!SharedLibraryUtils::createSharedLibrary(
399 mCtx->getContext()->getDriverName(), cacheDir, resName, reuse,
400 &SOPath)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800401 ALOGE("Linker: Failed to link object file '%s'", resName);
402 mCtx->unlockMutex();
403 return false;
404 }
405
Yang Ni29791302017-05-01 15:53:23 -0700406 if (reuse) {
407 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
408 } else {
409 mScriptSO = SharedLibraryUtils::loadAndDeleteSharedLibrary(SOPath.c_str());
410 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800411 if (mScriptSO == nullptr) {
412 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700413 mCtx->unlockMutex();
414 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700415 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800416
417 // Read RS symbol information from the .so.
418 if (!storeRSInfoFromSO()) {
419 goto error;
420 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700421 }
Jason Sams709a0972012-11-15 18:18:04 -0800422
Miao Wang82e135c2017-02-27 23:35:35 -0800423 mBitcodeFilePath.assign(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800424
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700425#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800426 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
427 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700428
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800429 if (!mScriptSO) {
430 goto error;
431 }
Jason Sams110f1812013-03-14 16:02:18 -0700432
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800433 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700434 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700435 }
436#endif
Jason Sams709a0972012-11-15 18:18:04 -0800437 mCtx->unlockMutex();
438 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700439
Jason Sams110f1812013-03-14 16:02:18 -0700440error:
441
442 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700443 if (mScriptSO) {
444 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800445 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700446 }
447 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800448}
449
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700450#ifndef RS_COMPATIBILITY_LIB
451
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700452const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
453 size_t bitcodeSize) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200454 const char* defaultLib = SYSLIBPATH_BC"/libclcore.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700455
456 // If we're debugging, use the debug library.
457 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Yang Nifb709342017-05-16 14:20:03 -0700458 if (ME.hasDebugInfo()) {
459 return SYSLIBPATH_BC"/libclcore_debug_g.bc";
460 }
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200461 return SYSLIBPATH_BC"/libclcore_debug.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700462 }
463
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000464 if (ME.hasDebugInfo()) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200465 return SYSLIBPATH_BC"/libclcore_g.bc";
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000466 }
467
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700468 // 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.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200482 return SYSLIBPATH_BC"/libclcore_neon.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700483 } else {
484 return defaultLib;
485 }
486#elif defined(__i386__) || defined(__x86_64__)
487 // x86 devices will use an optimized library.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200488 return SYSLIBPATH_BC"/libclcore_x86.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700489#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();
500 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800501 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800502 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800503 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
504 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700505
506 // Bug, need to stash in metadata
507 if (mRootExpand) {
508 script->mHal.info.root = mRootExpand;
509 } else {
510 script->mHal.info.root = mRoot;
511 }
Jason Sams709a0972012-11-15 18:18:04 -0800512}
513
Matt Wala14ce0072015-07-30 17:30:25 -0700514// Set up the launch dimensions, and write the values of the launch
515// dimensions into the mtls start/end fields.
516//
517// Inputs:
518// baseDim - base shape of the input
519// sc - used to constrain the launch dimensions
520//
521// Returns:
522// True on success, false on failure to set up
523bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
524 const RsLaunchDimensions &baseDim,
525 const RsScriptCall *sc) {
526 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800527
Matt Wala14ce0072015-07-30 17:30:25 -0700528#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
529 if (!sc || (sc->SC_FIELD##End == 0)) { \
530 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
531 } else { \
532 mtls->start.DIM_FIELD = \
533 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
534 mtls->end.DIM_FIELD = \
535 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
536 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
537 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
538 "Failed to launch kernel; Invalid " \
539 #SC_FIELD "Start or " #SC_FIELD "End."); \
540 return false; \
541 } \
542 }} while(0)
543
544 SET_UP_DIMENSION(x, x);
545 SET_UP_DIMENSION(y, y);
546 SET_UP_DIMENSION(z, z);
Yang Ni7784b5d2016-05-04 09:44:48 -0700547 // Checks and setup of fields other than x, y, z are ignored, since those
548 // fields are not used in the runtime and are not visible in the Java API.
Matt Wala14ce0072015-07-30 17:30:25 -0700549#undef SET_UP_DIMENSION
550
551 return true;
552}
553
David Grossfc7ab792016-06-01 14:45:47 -0700554// Preliminary work to prepare a general reduce-style kernel for launch.
555bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation ** ains,
556 uint32_t inLen,
557 const Allocation * aout,
Matt Wala14ce0072015-07-30 17:30:25 -0700558 const RsScriptCall *sc,
559 MTLaunchStructReduce *mtls) {
David Gross6c1876b2016-01-15 11:52:14 -0800560 rsAssert(ains && (inLen >= 1) && aout);
David Grossfc7ab792016-06-01 14:45:47 -0700561 memset(mtls, 0, sizeof(MTLaunchStructReduce));
David Gross6c1876b2016-01-15 11:52:14 -0800562 mtls->dimPtr = &mtls->redp.dim;
563
564 for (int index = inLen; --index >= 0;) {
565 if (allocationLODIsNull(ains[index])) {
566 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
567 "reduce called with null in allocations");
568 return false;
569 }
570 }
571
572 if (allocationLODIsNull(aout)) {
573 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
574 "reduce called with null out allocation");
575 return false;
576 }
577
578 const Allocation *ain0 = ains[0];
579 const Type *inType = ain0->getType();
580
581 mtls->redp.dim.x = inType->getDimX();
582 mtls->redp.dim.y = inType->getDimY();
583 mtls->redp.dim.z = inType->getDimZ();
584
585 for (int Index = inLen; --Index >= 1;) {
586 if (!ain0->hasSameDims(ains[Index])) {
587 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
588 "Failed to launch reduction kernel;"
589 "dimensions of input allocations do not match.");
590 return false;
591 }
592 }
593
594 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
595 return false;
596 }
597
598 // The X & Y walkers always want 0-1 min even if dim is not present
599 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
600 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
601
602 mtls->rs = mCtx;
603
David Gross35dbc8c2016-03-29 13:48:41 -0700604 mtls->mSliceNum = 0;
605 mtls->mSliceSize = 1;
606 mtls->isThreadable = mIsThreadable;
David Gross6c1876b2016-01-15 11:52:14 -0800607
608 // Set up output,
609 mtls->redp.outLen = 1;
610 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
611 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
612
613 // Set up input.
614 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
615 mtls->redp.inLen = inLen;
616 for (int index = inLen; --index >= 0;) {
617 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
618 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
619 }
620
621 // All validation passed, ok to launch threads
622 return true;
623}
624
Matt Wala14ce0072015-07-30 17:30:25 -0700625// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800626bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700627 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700628 Allocation * aout,
629 const void * usr, uint32_t usrLen,
630 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700631 MTLaunchStructForEach *mtls) {
Miao Wang68e00892016-02-25 12:44:10 -0800632 if (ains == nullptr && inLen != 0) {
633 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
634 "rsForEach called with none-zero inLen with null in allocations");
635 return false;
636 }
637
Matt Wala14ce0072015-07-30 17:30:25 -0700638 memset(mtls, 0, sizeof(MTLaunchStructForEach));
639 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700640
Chris Wailesf3712132014-07-16 15:18:30 -0700641 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700642 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700643 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
644 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800645 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700646 }
647 }
648
Matt Wala14ce0072015-07-30 17:30:25 -0700649 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700650 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
651 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800652 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700653 }
654
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700655 // The only situation where ains[j] is null is when inLen==1 and j==0;
656 // and that can only happen for an old-style kernel in API level 11~13,
657 // where the input allocation cannot be skipped if the output allocation is specified.
658 if (inLen != 0)
659 rsAssert((inLen == 1) || (ains[0] != nullptr));
660
661 if (inLen > 0 && ains[0]) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700662 const Allocation *ain0 = ains[0];
663 const Type *inType = ain0->getType();
664
Jason Samsc0d68472015-01-20 14:29:52 -0800665 mtls->fep.dim.x = inType->getDimX();
666 mtls->fep.dim.y = inType->getDimY();
667 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700668
669 for (int Index = inLen; --Index >= 1;) {
670 if (!ain0->hasSameDims(ains[Index])) {
671 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700672 "Failed to launch kernel; dimensions of input "
Yang Nie8f9fba2015-01-30 08:55:10 -0800673 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800674 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700675 }
676 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700677 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700678 const Type *outType = aout->getType();
679
Jason Samsc0d68472015-01-20 14:29:52 -0800680 mtls->fep.dim.x = outType->getDimX();
681 mtls->fep.dim.y = outType->getDimY();
682 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700683
Jason Samsa9139c72015-04-16 15:11:04 -0700684 } else if (sc != nullptr) {
685 mtls->fep.dim.x = sc->xEnd;
686 mtls->fep.dim.y = sc->yEnd;
687 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700688 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700689 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
690 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800691 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700692 }
693
Chris Wailes44bef6f2014-08-12 13:51:10 -0700694 if (inLen > 0 && aout != nullptr) {
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700695 if (ains[0] && !ains[0]->hasSameDims(aout)) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700696 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
697 "Failed to launch kernel; dimensions of input and output allocations do not match.");
698
Jason Samsbf2111d2015-01-26 18:13:41 -0800699 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700700 }
701 }
702
Matt Wala14ce0072015-07-30 17:30:25 -0700703 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
704 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700705 }
706
Jason Samsbf2111d2015-01-26 18:13:41 -0800707 // The X & Y walkers always want 0-1 min even if dim is not present
708 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
709 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700710 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800711 if (ains) {
712 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
713 }
714 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700715 mtls->fep.usr = usr;
716 mtls->fep.usrLen = usrLen;
717 mtls->mSliceSize = 1;
718 mtls->mSliceNum = 0;
719
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700720 mtls->isThreadable = mIsThreadable;
721
Chris Wailesf3712132014-07-16 15:18:30 -0700722 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700723 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700724 for (int index = inLen; --index >= 0;) {
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700725 if (ains[index] == nullptr) {
726 // In old style kernels, the first and only input allocation could be null.
727 // Not allowed in newer styles.
728 rsAssert(inLen == 1 && index == 0);
729 continue;
730 }
Jason Samsc0d68472015-01-20 14:29:52 -0800731 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
732 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700733 }
734 }
735
Chris Wailes44bef6f2014-08-12 13:51:10 -0700736 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800737 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
738 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700739 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800740
741 // All validation passed, ok to launch threads
742 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700743}
744
Jason Sams709a0972012-11-15 18:18:04 -0800745
746void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700747 const Allocation ** ains,
748 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800749 Allocation * aout,
750 const void * usr,
751 uint32_t usrLen,
752 const RsScriptCall *sc) {
753
Matt Wala14ce0072015-07-30 17:30:25 -0700754 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700755
Jason Samsbf2111d2015-01-26 18:13:41 -0800756 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
757 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700758
Jason Samsbf2111d2015-01-26 18:13:41 -0800759 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700760 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800761 mCtx->setTLS(oldTLS);
762 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700763}
764
Matt Wala14ce0072015-07-30 17:30:25 -0700765void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
David Grossfc7ab792016-06-01 14:45:47 -0700766 const Allocation ** ains, uint32_t inLen,
Matt Wala14ce0072015-07-30 17:30:25 -0700767 Allocation *aout,
768 const RsScriptCall *sc) {
David Grossfc7ab792016-06-01 14:45:47 -0700769 MTLaunchStructReduce mtls;
Matt Wala14ce0072015-07-30 17:30:25 -0700770
David Grossfc7ab792016-06-01 14:45:47 -0700771 if (reduceMtlsSetup(ains, inLen, aout, sc, &mtls)) {
772 reduceKernelSetup(slot, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800773 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
David Grossfc7ab792016-06-01 14:45:47 -0700774 mCtx->launchReduce(ains, inLen, aout, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800775 mCtx->setTLS(oldTLS);
776 }
777}
778
Matt Wala14ce0072015-07-30 17:30:25 -0700779void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800780 mtls->script = this;
781 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800782 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700783 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800784}
785
Matt Wala14ce0072015-07-30 17:30:25 -0700786void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
787 mtls->script = this;
David Gross6c1876b2016-01-15 11:52:14 -0800788 mtls->redp.slot = slot;
789
David Grossfc7ab792016-06-01 14:45:47 -0700790 const ReduceDescription *desc = mScriptExec->getReduceDescription(slot);
David Gross6c1876b2016-01-15 11:52:14 -0800791 mtls->accumFunc = desc->accumFunc;
792 mtls->initFunc = desc->initFunc; // might legally be nullptr
David Gross35dbc8c2016-03-29 13:48:41 -0700793 mtls->combFunc = desc->combFunc; // might legally be nullptr
David Gross6c1876b2016-01-15 11:52:14 -0800794 mtls->outFunc = desc->outFunc; // might legally be nullptr
795 mtls->accumSize = desc->accumSize;
796
797 rsAssert(mtls->accumFunc != nullptr);
798}
799
Jason Sams709a0972012-11-15 18:18:04 -0800800int RsdCpuScriptImpl::invokeRoot() {
801 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
802 int ret = mRoot();
803 mCtx->setTLS(oldTLS);
804 return ret;
805}
806
807void RsdCpuScriptImpl::invokeInit() {
808 if (mInit) {
809 mInit();
810 }
811}
812
813void RsdCpuScriptImpl::invokeFreeChildren() {
814 if (mFreeChildren) {
815 mFreeChildren();
816 }
817}
818
819void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
820 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800821 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800822 void * ap = nullptr;
823
824#if defined(__x86_64__)
825 // The invoked function could have input parameter of vector type for example float4 which
826 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
827 // So try to align void* params before passing them into RS exported function.
828
829 if ((uint8_t)(uint64_t)params & 0x0F) {
830 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
831 memcpy(ap, params, paramLength);
832 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800833 ALOGE("x86_64: invokeFunction memalign error, still use params which"
834 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800835 }
836 }
837#endif
Jason Sams709a0972012-11-15 18:18:04 -0800838
839 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800840 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800841 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800842
Yong Chenc246d912015-05-22 18:20:06 +0800843#if defined(__x86_64__)
844 free(ap);
845#endif
846
Jason Sams709a0972012-11-15 18:18:04 -0800847 mCtx->setTLS(oldTLS);
848}
849
850void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
851 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800852 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800853
854 //if (mIntrinsicID) {
855 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
856 //return;
857 //}
858
Yang Nid9bae682015-01-20 15:31:15 -0800859 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800860 if (!destPtr) {
861 //ALOGV("Calling setVar on slot = %i which is null", slot);
862 return;
863 }
864
865 memcpy(destPtr, data, dataLength);
866}
867
Tim Murray9c642392013-04-11 13:29:59 -0700868void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
869 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800870 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700871
Yang Nid9bae682015-01-20 15:31:15 -0800872 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700873 if (!srcPtr) {
874 //ALOGV("Calling setVar on slot = %i which is null", slot);
875 return;
876 }
877 memcpy(data, srcPtr, dataLength);
878}
879
880
Jason Sams709a0972012-11-15 18:18:04 -0800881void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
882 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700883 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800884 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800885 if (!destPtr) {
886 //ALOGV("Calling setVar on slot = %i which is null", slot);
887 return;
888 }
889
890 // We want to look at dimension in terms of integer components,
891 // but dimLength is given in terms of bytes.
892 dimLength /= sizeof(int);
893
894 // Only a single dimension is currently supported.
895 rsAssert(dimLength == 1);
896 if (dimLength == 1) {
897 // First do the increment loop.
898 size_t stride = elem->getSizeBytes();
899 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700900 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800901 elem->incRefs(cVal);
902 cVal += stride;
903 }
904
905 // Decrement loop comes after (to prevent race conditions).
906 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700907 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800908 elem->decRefs(oldVal);
909 oldVal += stride;
910 }
911 }
912
913 memcpy(destPtr, data, dataLength);
914}
915
916void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
917
918 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800919 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800920
Yang Nid9bae682015-01-20 15:31:15 -0800921 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800922 if (!destPtr) {
923 //ALOGV("Calling setVar on slot = %i which is null", slot);
924 return;
925 }
926
Chris Wailes44bef6f2014-08-12 13:51:10 -0700927 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800928 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800929 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800930 ptr = data->mHal.drvState.lod[0].mallocPtr;
931 }
932 memcpy(destPtr, &ptr, sizeof(void *));
933}
934
935void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
936
937 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800938 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800939
Yang Nid9bae682015-01-20 15:31:15 -0800940 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800941 if (!destPtr) {
942 //ALOGV("Calling setVar on slot = %i which is null", slot);
943 return;
944 }
945
Jason Sams05ef73f2014-08-05 14:59:22 -0700946 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800947}
948
Yang Ni062c2872015-02-20 15:20:00 -0800949const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
950 return mScriptExec->getFieldName(slot);
951}
952
Jason Sams709a0972012-11-15 18:18:04 -0800953RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700954 delete mScriptExec;
955 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700956 if (mScriptSO) {
957 dlclose(mScriptSO);
958 }
Jason Sams709a0972012-11-15 18:18:04 -0800959}
960
961Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
962 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700963 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800964 }
965
966 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
967 Allocation *a = mBoundAllocs[ct];
968 if (!a) continue;
969 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
970 return a;
971 }
972 }
973 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700974 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800975}
976
Stephen Hines8409d642015-04-28 18:49:56 -0700977int RsdCpuScriptImpl::getGlobalEntries() const {
978 return mScriptExec->getGlobalEntries();
979}
980
981const char * RsdCpuScriptImpl::getGlobalName(int i) const {
982 return mScriptExec->getGlobalName(i);
983}
984
985const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
986 return mScriptExec->getGlobalAddress(i);
987}
988
989size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
990 return mScriptExec->getGlobalSize(i);
991}
992
Stephen Hines5aa018c2015-05-20 18:09:57 -0700993uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
994 return mScriptExec->getGlobalProperties(i);
995}
996
Chris Wailesf3712132014-07-16 15:18:30 -0700997void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
998 uint32_t inLen, Allocation * aout,
999 const void * usr, uint32_t usrLen,
1000 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001001
Chris Wailesf3712132014-07-16 15:18:30 -07001002void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1003 uint32_t inLen, Allocation * aout,
1004 const void * usr, uint32_t usrLen,
1005 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001006
Jason Sams709a0972012-11-15 18:18:04 -08001007
Rahul Chaudhry7974fc02017-02-09 12:33:28 -08001008} // namespace renderscript
1009} // namespace android