blob: f2f3d2e9d483053fb09a32a0a6691286b7e625d7 [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
Chris Wailes6847e732014-08-11 17:30:51 -0700135 FILE *bcfile = fopen(bcFileName.c_str(), "w");
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) {
164 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
165 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
166 return false;
Miao Wang82e135c2017-02-27 23:35:35 -0800167 char buf[PROP_VALUE_MAX];
Miao Wang2a611682017-02-27 17:36:40 -0800168 android::renderscript::property_get("ro.debuggable", buf, "");
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800169 return (buf[0] == '1');
170}
171
172bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
173 int FD = open(fileName, O_RDONLY);
174 if (FD == -1) {
175 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
176 return false;
177 }
178
179 char buf[256];
180 while (true) {
181 ssize_t nread = read(FD, buf, sizeof(buf));
182 if (nread < 0) { // bail out on failed read
183 ALOGE("Error while computing checksum for file \'%s\'", fileName);
184 return false;
185 }
186
187 checksum = adler32(checksum, (const unsigned char *) buf, nread);
188 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
189 break;
190 }
191
Elliott Hughes2df57672015-05-15 17:06:47 -0700192 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800193 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
194 return false;
195 }
196 return true;
197}
Yang Nicb170152015-04-16 10:27:02 -0700198
199#endif // !defined(RS_COMPATIBILITY_LIB)
200} // namespace
201
202namespace android {
203namespace renderscript {
204
205#ifndef RS_COMPATIBILITY_LIB
206
207uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
208 const char *commandLine,
209 const char** bccFiles, size_t numFiles) {
210 uint32_t checksum = adler32(0L, Z_NULL, 0);
211
212 // include checksum of bitcode
213 if (bitcode != nullptr && bitcodeSize > 0) {
214 checksum = adler32(checksum, bitcode, bitcodeSize);
215 }
216
217 // include checksum of command line arguments
218 checksum = adler32(checksum, (const unsigned char *) commandLine,
219 strlen(commandLine));
220
221 // include checksum of bccFiles
222 for (size_t i = 0; i < numFiles; i++) {
223 const char* bccFile = bccFiles[i];
224 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
225 // return empty checksum instead of something partial/corrupt
226 return 0;
227 }
228 }
229
230 return checksum;
231}
232
Yang Nif02a2b02015-04-07 16:00:31 -0700233#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800234
Jason Sams709a0972012-11-15 18:18:04 -0800235RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
236 mCtx = ctx;
237 mScript = s;
238
Chris Wailes44bef6f2014-08-12 13:51:10 -0700239 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800240
Chris Wailes44bef6f2014-08-12 13:51:10 -0700241 mRoot = nullptr;
242 mRootExpand = nullptr;
243 mInit = nullptr;
244 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800245 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800246
Chris Wailes44bef6f2014-08-12 13:51:10 -0700247 mBoundAllocs = nullptr;
248 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800249 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800250
Yang Nicb170152015-04-16 10:27:02 -0700251 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800252 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800253}
254
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800255bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800256 // The shared object may have an invalid build checksum.
257 // Validate and fail early.
258 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Niade31372016-04-06 09:34:34 -0700259 mScriptSO, mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800260
261 if (mScriptExec == nullptr) {
262 return false;
263 }
264
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800265 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
266 if (mRoot) {
267 //ALOGE("Found root(): %p", mRoot);
268 }
269 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
270 if (mRootExpand) {
271 //ALOGE("Found root.expand(): %p", mRootExpand);
272 }
Matt Wala14ce0072015-07-30 17:30:25 -0700273 mInit = (InitOrDtorFunc_t) dlsym(mScriptSO, "init");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800274 if (mInit) {
275 //ALOGE("Found init(): %p", mInit);
276 }
Matt Wala14ce0072015-07-30 17:30:25 -0700277 mFreeChildren = (InitOrDtorFunc_t) dlsym(mScriptSO, ".rs.dtor");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800278 if (mFreeChildren) {
279 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
280 }
281
Yang Nid9bae682015-01-20 15:31:15 -0800282 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800283 if (varCount > 0) {
284 mBoundAllocs = new Allocation *[varCount];
285 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
286 }
287
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800288 mIsThreadable = mScriptExec->getThreadable();
289 //ALOGE("Script isThreadable? %d", mIsThreadable);
290
Stephen Hines8409d642015-04-28 18:49:56 -0700291 if (kDebugGlobalVariables) {
292 mScriptExec->dumpGlobalInfo();
293 }
294
Yang Nid9bae682015-01-20 15:31:15 -0800295 return true;
296}
297
Jason Sams709a0972012-11-15 18:18:04 -0800298bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
299 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800300 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800301 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
302 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800303 //ALOGE("rsdScriptInit %p %p", rsc, script);
304
305 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700306#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800307 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800308
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700309 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
310 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700311 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700312 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700313 return false;
314 }
315
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700316 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700317
318 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800319 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700320 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700321
verena beckhamf5029802015-05-22 16:51:42 +0100322 int optLevel = mCtx->getContext()->getOptLevel();
323
Chris Wailes6847e732014-08-11 17:30:51 -0700324 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700325 bcFileName.append("/");
326 bcFileName.append(resName);
327 bcFileName.append(".bc");
328
329 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700330 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
331 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700332 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700333 useRSDebugContext, bccPluginName, emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +0100334 optLevel, emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800335
Stephen Hines6a236ad2015-06-22 14:51:30 -0700336 mChecksumNeeded = isChecksumNeeded(cacheDir);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800337 if (mChecksumNeeded) {
338 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
339 core_lib,
340 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700341
342 // The last argument of compileArguments is a nullptr, so remove 1 from
343 // the size.
344 std::unique_ptr<const char> compileCommandLine(
345 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
346
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800347 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
348 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700349 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800350
Yang Nicb170152015-04-16 10:27:02 -0700351 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800352 // cannot compute checksum but verification is enabled
353 mCtx->unlockMutex();
354 return false;
355 }
356 }
357 else {
358 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700359 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800360 }
361
362 // Append build checksum to commandline
363 // Handle the terminal nullptr in compileArguments
364 compileArguments.pop_back();
365 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700366 std::stringstream ss;
367 ss << std::hex << mBuildChecksum;
Yang Ni578419f2016-06-27 16:12:25 -0700368 std::string checksumStr(ss.str());
369 compileArguments.push_back(checksumStr.c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800370 compileArguments.push_back(nullptr);
371
Yang Nia845c352017-05-01 15:53:23 -0700372 const bool reuse = !is_force_recompile() && !useRSDebugContext;
373 if (reuse) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800374 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800375
376 // Read RS info from the shared object to detect checksum mismatch
377 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
378 dlclose(mScriptSO);
379 mScriptSO = nullptr;
380 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700381 }
382
Yang Nia845c352017-05-01 15:53:23 -0700383 // If reuse is desired and we can't, it's either not there or out of date.
384 // We compile the bit code and try loading again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800385 if (mScriptSO == nullptr) {
386 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700387 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800388 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700389 ALOGE("bcc: FAILS to compile '%s'", resName);
390 mCtx->unlockMutex();
391 return false;
392 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800393
Yang Nia845c352017-05-01 15:53:23 -0700394 std::string SOPath;
395
396 if (!SharedLibraryUtils::createSharedLibrary(
397 mCtx->getContext()->getDriverName(), cacheDir, resName, reuse,
398 &SOPath)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800399 ALOGE("Linker: Failed to link object file '%s'", resName);
400 mCtx->unlockMutex();
401 return false;
402 }
403
Yang Nia845c352017-05-01 15:53:23 -0700404 if (reuse) {
405 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
406 } else {
407 mScriptSO = SharedLibraryUtils::loadAndDeleteSharedLibrary(SOPath.c_str());
408 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800409 if (mScriptSO == nullptr) {
410 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700411 mCtx->unlockMutex();
412 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700413 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800414
415 // Read RS symbol information from the .so.
416 if (!storeRSInfoFromSO()) {
417 goto error;
418 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700419 }
Jason Sams709a0972012-11-15 18:18:04 -0800420
Miao Wang82e135c2017-02-27 23:35:35 -0800421 mBitcodeFilePath.assign(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800422
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700423#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800424 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
425 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700426
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800427 if (!mScriptSO) {
428 goto error;
429 }
Jason Sams110f1812013-03-14 16:02:18 -0700430
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800431 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700432 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700433 }
434#endif
Jason Sams709a0972012-11-15 18:18:04 -0800435 mCtx->unlockMutex();
436 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700437
Jason Sams110f1812013-03-14 16:02:18 -0700438error:
439
440 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700441 if (mScriptSO) {
442 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800443 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700444 }
445 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800446}
447
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700448#ifndef RS_COMPATIBILITY_LIB
449
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700450const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
451 size_t bitcodeSize) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200452 const char* defaultLib = SYSLIBPATH_BC"/libclcore.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700453
454 // If we're debugging, use the debug library.
455 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200456 return SYSLIBPATH_BC"/libclcore_debug.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700457 }
458
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000459 if (ME.hasDebugInfo()) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200460 return SYSLIBPATH_BC"/libclcore_g.bc";
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000461 }
462
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700463 // If a callback has been registered to specify a library, use that.
464 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700465 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700466 return selectRTCallback((const char*)bitcode, bitcodeSize);
467 }
468
469 // Check for a platform specific library
470#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
471 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700472 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700473 // NEON-capable ARMv7a devices can use an accelerated math library
474 // for all reduced precision scripts.
475 // ARMv8 does not use NEON, as ASIMD can be used with all precision
476 // levels.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200477 return SYSLIBPATH_BC"/libclcore_neon.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700478 } else {
479 return defaultLib;
480 }
481#elif defined(__i386__) || defined(__x86_64__)
482 // x86 devices will use an optimized library.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200483 return SYSLIBPATH_BC"/libclcore_x86.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700484#else
485 return defaultLib;
486#endif
487}
488
489#endif
490
Jason Sams709a0972012-11-15 18:18:04 -0800491void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700492 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800493 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700494 script->mHal.info.exportedReduceCount = mScriptExec->getExportedReduceCount();
495 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800496 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800497 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800498 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
499 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700500
501 // Bug, need to stash in metadata
502 if (mRootExpand) {
503 script->mHal.info.root = mRootExpand;
504 } else {
505 script->mHal.info.root = mRoot;
506 }
Jason Sams709a0972012-11-15 18:18:04 -0800507}
508
Matt Wala14ce0072015-07-30 17:30:25 -0700509// Set up the launch dimensions, and write the values of the launch
510// dimensions into the mtls start/end fields.
511//
512// Inputs:
513// baseDim - base shape of the input
514// sc - used to constrain the launch dimensions
515//
516// Returns:
517// True on success, false on failure to set up
518bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
519 const RsLaunchDimensions &baseDim,
520 const RsScriptCall *sc) {
521 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800522
Matt Wala14ce0072015-07-30 17:30:25 -0700523#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
524 if (!sc || (sc->SC_FIELD##End == 0)) { \
525 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
526 } else { \
527 mtls->start.DIM_FIELD = \
528 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
529 mtls->end.DIM_FIELD = \
530 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
531 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
532 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
533 "Failed to launch kernel; Invalid " \
534 #SC_FIELD "Start or " #SC_FIELD "End."); \
535 return false; \
536 } \
537 }} while(0)
538
539 SET_UP_DIMENSION(x, x);
540 SET_UP_DIMENSION(y, y);
541 SET_UP_DIMENSION(z, z);
Yang Ni7784b5d2016-05-04 09:44:48 -0700542 // Checks and setup of fields other than x, y, z are ignored, since those
543 // fields are not used in the runtime and are not visible in the Java API.
Matt Wala14ce0072015-07-30 17:30:25 -0700544#undef SET_UP_DIMENSION
545
546 return true;
547}
548
David Grossfc7ab792016-06-01 14:45:47 -0700549// Preliminary work to prepare a general reduce-style kernel for launch.
550bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation ** ains,
551 uint32_t inLen,
552 const Allocation * aout,
Matt Wala14ce0072015-07-30 17:30:25 -0700553 const RsScriptCall *sc,
554 MTLaunchStructReduce *mtls) {
David Gross6c1876b2016-01-15 11:52:14 -0800555 rsAssert(ains && (inLen >= 1) && aout);
David Grossfc7ab792016-06-01 14:45:47 -0700556 memset(mtls, 0, sizeof(MTLaunchStructReduce));
David Gross6c1876b2016-01-15 11:52:14 -0800557 mtls->dimPtr = &mtls->redp.dim;
558
559 for (int index = inLen; --index >= 0;) {
560 if (allocationLODIsNull(ains[index])) {
561 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
562 "reduce called with null in allocations");
563 return false;
564 }
565 }
566
567 if (allocationLODIsNull(aout)) {
568 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
569 "reduce called with null out allocation");
570 return false;
571 }
572
573 const Allocation *ain0 = ains[0];
574 const Type *inType = ain0->getType();
575
576 mtls->redp.dim.x = inType->getDimX();
577 mtls->redp.dim.y = inType->getDimY();
578 mtls->redp.dim.z = inType->getDimZ();
579
580 for (int Index = inLen; --Index >= 1;) {
581 if (!ain0->hasSameDims(ains[Index])) {
582 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
583 "Failed to launch reduction kernel;"
584 "dimensions of input allocations do not match.");
585 return false;
586 }
587 }
588
589 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
590 return false;
591 }
592
593 // The X & Y walkers always want 0-1 min even if dim is not present
594 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
595 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
596
597 mtls->rs = mCtx;
598
David Gross35dbc8c2016-03-29 13:48:41 -0700599 mtls->mSliceNum = 0;
600 mtls->mSliceSize = 1;
601 mtls->isThreadable = mIsThreadable;
David Gross6c1876b2016-01-15 11:52:14 -0800602
603 // Set up output,
604 mtls->redp.outLen = 1;
605 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
606 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
607
608 // Set up input.
609 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
610 mtls->redp.inLen = inLen;
611 for (int index = inLen; --index >= 0;) {
612 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
613 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
614 }
615
616 // All validation passed, ok to launch threads
617 return true;
618}
619
Matt Wala14ce0072015-07-30 17:30:25 -0700620// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800621bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700622 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700623 Allocation * aout,
624 const void * usr, uint32_t usrLen,
625 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700626 MTLaunchStructForEach *mtls) {
Miao Wang68e00892016-02-25 12:44:10 -0800627 if (ains == nullptr && inLen != 0) {
628 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
629 "rsForEach called with none-zero inLen with null in allocations");
630 return false;
631 }
632
Matt Wala14ce0072015-07-30 17:30:25 -0700633 memset(mtls, 0, sizeof(MTLaunchStructForEach));
634 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700635
Chris Wailesf3712132014-07-16 15:18:30 -0700636 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700637 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700638 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
639 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800640 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700641 }
642 }
643
Matt Wala14ce0072015-07-30 17:30:25 -0700644 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700645 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
646 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800647 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700648 }
649
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700650 // The only situation where ains[j] is null is when inLen==1 and j==0;
651 // and that can only happen for an old-style kernel in API level 11~13,
652 // where the input allocation cannot be skipped if the output allocation is specified.
653 if (inLen != 0)
654 rsAssert((inLen == 1) || (ains[0] != nullptr));
655
656 if (inLen > 0 && ains[0]) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700657 const Allocation *ain0 = ains[0];
658 const Type *inType = ain0->getType();
659
Jason Samsc0d68472015-01-20 14:29:52 -0800660 mtls->fep.dim.x = inType->getDimX();
661 mtls->fep.dim.y = inType->getDimY();
662 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700663
664 for (int Index = inLen; --Index >= 1;) {
665 if (!ain0->hasSameDims(ains[Index])) {
666 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700667 "Failed to launch kernel; dimensions of input "
Yang Nie8f9fba2015-01-30 08:55:10 -0800668 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800669 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700670 }
671 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700672 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700673 const Type *outType = aout->getType();
674
Jason Samsc0d68472015-01-20 14:29:52 -0800675 mtls->fep.dim.x = outType->getDimX();
676 mtls->fep.dim.y = outType->getDimY();
677 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700678
Jason Samsa9139c72015-04-16 15:11:04 -0700679 } else if (sc != nullptr) {
680 mtls->fep.dim.x = sc->xEnd;
681 mtls->fep.dim.y = sc->yEnd;
682 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700683 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700684 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
685 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800686 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700687 }
688
Chris Wailes44bef6f2014-08-12 13:51:10 -0700689 if (inLen > 0 && aout != nullptr) {
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700690 if (ains[0] && !ains[0]->hasSameDims(aout)) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700691 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
692 "Failed to launch kernel; dimensions of input and output allocations do not match.");
693
Jason Samsbf2111d2015-01-26 18:13:41 -0800694 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700695 }
696 }
697
Matt Wala14ce0072015-07-30 17:30:25 -0700698 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
699 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700700 }
701
Jason Samsbf2111d2015-01-26 18:13:41 -0800702 // The X & Y walkers always want 0-1 min even if dim is not present
703 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
704 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700705 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800706 if (ains) {
707 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
708 }
709 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700710 mtls->fep.usr = usr;
711 mtls->fep.usrLen = usrLen;
712 mtls->mSliceSize = 1;
713 mtls->mSliceNum = 0;
714
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700715 mtls->isThreadable = mIsThreadable;
716
Chris Wailesf3712132014-07-16 15:18:30 -0700717 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700718 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700719 for (int index = inLen; --index >= 0;) {
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700720 if (ains[index] == nullptr) {
721 // In old style kernels, the first and only input allocation could be null.
722 // Not allowed in newer styles.
723 rsAssert(inLen == 1 && index == 0);
724 continue;
725 }
Jason Samsc0d68472015-01-20 14:29:52 -0800726 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
727 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700728 }
729 }
730
Chris Wailes44bef6f2014-08-12 13:51:10 -0700731 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800732 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
733 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700734 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800735
736 // All validation passed, ok to launch threads
737 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700738}
739
Jason Sams709a0972012-11-15 18:18:04 -0800740
741void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700742 const Allocation ** ains,
743 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800744 Allocation * aout,
745 const void * usr,
746 uint32_t usrLen,
747 const RsScriptCall *sc) {
748
Matt Wala14ce0072015-07-30 17:30:25 -0700749 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700750
Jason Samsbf2111d2015-01-26 18:13:41 -0800751 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
752 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700753
Jason Samsbf2111d2015-01-26 18:13:41 -0800754 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700755 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800756 mCtx->setTLS(oldTLS);
757 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700758}
759
Matt Wala14ce0072015-07-30 17:30:25 -0700760void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
David Grossfc7ab792016-06-01 14:45:47 -0700761 const Allocation ** ains, uint32_t inLen,
Matt Wala14ce0072015-07-30 17:30:25 -0700762 Allocation *aout,
763 const RsScriptCall *sc) {
David Grossfc7ab792016-06-01 14:45:47 -0700764 MTLaunchStructReduce mtls;
Matt Wala14ce0072015-07-30 17:30:25 -0700765
David Grossfc7ab792016-06-01 14:45:47 -0700766 if (reduceMtlsSetup(ains, inLen, aout, sc, &mtls)) {
767 reduceKernelSetup(slot, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800768 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
David Grossfc7ab792016-06-01 14:45:47 -0700769 mCtx->launchReduce(ains, inLen, aout, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800770 mCtx->setTLS(oldTLS);
771 }
772}
773
Matt Wala14ce0072015-07-30 17:30:25 -0700774void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800775 mtls->script = this;
776 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800777 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700778 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800779}
780
Matt Wala14ce0072015-07-30 17:30:25 -0700781void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
782 mtls->script = this;
David Gross6c1876b2016-01-15 11:52:14 -0800783 mtls->redp.slot = slot;
784
David Grossfc7ab792016-06-01 14:45:47 -0700785 const ReduceDescription *desc = mScriptExec->getReduceDescription(slot);
David Gross6c1876b2016-01-15 11:52:14 -0800786 mtls->accumFunc = desc->accumFunc;
787 mtls->initFunc = desc->initFunc; // might legally be nullptr
David Gross35dbc8c2016-03-29 13:48:41 -0700788 mtls->combFunc = desc->combFunc; // might legally be nullptr
David Gross6c1876b2016-01-15 11:52:14 -0800789 mtls->outFunc = desc->outFunc; // might legally be nullptr
790 mtls->accumSize = desc->accumSize;
791
792 rsAssert(mtls->accumFunc != nullptr);
793}
794
Jason Sams709a0972012-11-15 18:18:04 -0800795int RsdCpuScriptImpl::invokeRoot() {
796 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
797 int ret = mRoot();
798 mCtx->setTLS(oldTLS);
799 return ret;
800}
801
802void RsdCpuScriptImpl::invokeInit() {
803 if (mInit) {
804 mInit();
805 }
806}
807
808void RsdCpuScriptImpl::invokeFreeChildren() {
809 if (mFreeChildren) {
810 mFreeChildren();
811 }
812}
813
814void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
815 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800816 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800817 void * ap = nullptr;
818
819#if defined(__x86_64__)
820 // The invoked function could have input parameter of vector type for example float4 which
821 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
822 // So try to align void* params before passing them into RS exported function.
823
824 if ((uint8_t)(uint64_t)params & 0x0F) {
825 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
826 memcpy(ap, params, paramLength);
827 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800828 ALOGE("x86_64: invokeFunction memalign error, still use params which"
829 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800830 }
831 }
832#endif
Jason Sams709a0972012-11-15 18:18:04 -0800833
834 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800835 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800836 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800837
Yong Chenc246d912015-05-22 18:20:06 +0800838#if defined(__x86_64__)
839 free(ap);
840#endif
841
Jason Sams709a0972012-11-15 18:18:04 -0800842 mCtx->setTLS(oldTLS);
843}
844
845void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
846 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800847 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800848
849 //if (mIntrinsicID) {
850 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
851 //return;
852 //}
853
Yang Nid9bae682015-01-20 15:31:15 -0800854 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800855 if (!destPtr) {
856 //ALOGV("Calling setVar on slot = %i which is null", slot);
857 return;
858 }
859
860 memcpy(destPtr, data, dataLength);
861}
862
Tim Murray9c642392013-04-11 13:29:59 -0700863void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
864 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800865 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700866
Yang Nid9bae682015-01-20 15:31:15 -0800867 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700868 if (!srcPtr) {
869 //ALOGV("Calling setVar on slot = %i which is null", slot);
870 return;
871 }
872 memcpy(data, srcPtr, dataLength);
873}
874
875
Jason Sams709a0972012-11-15 18:18:04 -0800876void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
877 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700878 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800879 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800880 if (!destPtr) {
881 //ALOGV("Calling setVar on slot = %i which is null", slot);
882 return;
883 }
884
885 // We want to look at dimension in terms of integer components,
886 // but dimLength is given in terms of bytes.
887 dimLength /= sizeof(int);
888
889 // Only a single dimension is currently supported.
890 rsAssert(dimLength == 1);
891 if (dimLength == 1) {
892 // First do the increment loop.
893 size_t stride = elem->getSizeBytes();
894 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700895 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800896 elem->incRefs(cVal);
897 cVal += stride;
898 }
899
900 // Decrement loop comes after (to prevent race conditions).
901 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700902 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800903 elem->decRefs(oldVal);
904 oldVal += stride;
905 }
906 }
907
908 memcpy(destPtr, data, dataLength);
909}
910
911void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
912
913 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800914 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800915
Yang Nid9bae682015-01-20 15:31:15 -0800916 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800917 if (!destPtr) {
918 //ALOGV("Calling setVar on slot = %i which is null", slot);
919 return;
920 }
921
Chris Wailes44bef6f2014-08-12 13:51:10 -0700922 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800923 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800924 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800925 ptr = data->mHal.drvState.lod[0].mallocPtr;
926 }
927 memcpy(destPtr, &ptr, sizeof(void *));
928}
929
930void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
931
932 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800933 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800934
Yang Nid9bae682015-01-20 15:31:15 -0800935 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800936 if (!destPtr) {
937 //ALOGV("Calling setVar on slot = %i which is null", slot);
938 return;
939 }
940
Jason Sams05ef73f2014-08-05 14:59:22 -0700941 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800942}
943
Yang Ni062c2872015-02-20 15:20:00 -0800944const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
945 return mScriptExec->getFieldName(slot);
946}
947
Jason Sams709a0972012-11-15 18:18:04 -0800948RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700949 delete mScriptExec;
950 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700951 if (mScriptSO) {
952 dlclose(mScriptSO);
953 }
Jason Sams709a0972012-11-15 18:18:04 -0800954}
955
956Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
957 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700958 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800959 }
960
961 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
962 Allocation *a = mBoundAllocs[ct];
963 if (!a) continue;
964 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
965 return a;
966 }
967 }
968 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700969 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800970}
971
Stephen Hines8409d642015-04-28 18:49:56 -0700972int RsdCpuScriptImpl::getGlobalEntries() const {
973 return mScriptExec->getGlobalEntries();
974}
975
976const char * RsdCpuScriptImpl::getGlobalName(int i) const {
977 return mScriptExec->getGlobalName(i);
978}
979
980const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
981 return mScriptExec->getGlobalAddress(i);
982}
983
984size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
985 return mScriptExec->getGlobalSize(i);
986}
987
Stephen Hines5aa018c2015-05-20 18:09:57 -0700988uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
989 return mScriptExec->getGlobalProperties(i);
990}
991
Chris Wailesf3712132014-07-16 15:18:30 -0700992void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
993 uint32_t inLen, Allocation * aout,
994 const void * usr, uint32_t usrLen,
995 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700996
Chris Wailesf3712132014-07-16 15:18:30 -0700997void RsdCpuScriptImpl::postLaunch(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
Jason Sams709a0972012-11-15 18:18:04 -08001002
Rahul Chaudhry7974fc02017-02-09 12:33:28 -08001003} // namespace renderscript
1004} // namespace android