blob: 60d08be1a2074cdf14aece2876ff2443328d466d [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) {
111 args->push_back("-load");
112 args->push_back(bccPluginName);
113 }
114 }
115
Stephen Hines45e753a2015-01-19 20:58:44 -0800116 args->push_back("-fPIC");
117 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800118
Chris Wailes6847e732014-08-11 17:30:51 -0700119 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700120 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700121}
122
Chris Wailes6847e732014-08-11 17:30:51 -0700123static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700124 const char *bitcode,
125 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700126 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700127 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700128
Chris Wailes6847e732014-08-11 17:30:51 -0700129 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700130 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700131 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700132 return false;
133 }
134 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
135 fclose(bcfile);
136 if (nwritten != bitcodeSize) {
137 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700138 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700139 return false;
140 }
141
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700142 return android::renderscript::rsuExecuteCommand(
143 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
144 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700145}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700146
Stephen Hines6a236ad2015-06-22 14:51:30 -0700147// The checksum is unnecessary under a few conditions, since the primary
148// use-case for it is debugging. If we are loading something from the
149// system partition (read-only), we know that it was precompiled as part of
150// application ahead of time (and thus the checksum is completely
151// unnecessary). The checksum is also unnecessary on release (non-debug)
152// builds, as the only way to get a shared object is to have compiled the
153// script once already. On a release build, there is no way to adjust the
154// other libraries/dependencies, and so the only reason to recompile would
155// be for a source APK change or an OTA. In either case, the APK would be
156// reinstalled, which would already clear the code_cache/ directory.
157bool isChecksumNeeded(const char *cacheDir) {
158 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
159 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
160 return false;
Miao Wang82e135c2017-02-27 23:35:35 -0800161 char buf[PROP_VALUE_MAX];
Miao Wang2a611682017-02-27 17:36:40 -0800162 android::renderscript::property_get("ro.debuggable", buf, "");
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800163 return (buf[0] == '1');
164}
165
166bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
167 int FD = open(fileName, O_RDONLY);
168 if (FD == -1) {
169 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
170 return false;
171 }
172
173 char buf[256];
174 while (true) {
175 ssize_t nread = read(FD, buf, sizeof(buf));
176 if (nread < 0) { // bail out on failed read
177 ALOGE("Error while computing checksum for file \'%s\'", fileName);
178 return false;
179 }
180
181 checksum = adler32(checksum, (const unsigned char *) buf, nread);
182 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
183 break;
184 }
185
Elliott Hughes2df57672015-05-15 17:06:47 -0700186 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800187 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
188 return false;
189 }
190 return true;
191}
Yang Nicb170152015-04-16 10:27:02 -0700192
193#endif // !defined(RS_COMPATIBILITY_LIB)
194} // namespace
195
196namespace android {
197namespace renderscript {
198
199#ifndef RS_COMPATIBILITY_LIB
200
201uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
202 const char *commandLine,
203 const char** bccFiles, size_t numFiles) {
204 uint32_t checksum = adler32(0L, Z_NULL, 0);
205
206 // include checksum of bitcode
207 if (bitcode != nullptr && bitcodeSize > 0) {
208 checksum = adler32(checksum, bitcode, bitcodeSize);
209 }
210
211 // include checksum of command line arguments
212 checksum = adler32(checksum, (const unsigned char *) commandLine,
213 strlen(commandLine));
214
215 // include checksum of bccFiles
216 for (size_t i = 0; i < numFiles; i++) {
217 const char* bccFile = bccFiles[i];
218 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
219 // return empty checksum instead of something partial/corrupt
220 return 0;
221 }
222 }
223
224 return checksum;
225}
226
Yang Nif02a2b02015-04-07 16:00:31 -0700227#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800228
Jason Sams709a0972012-11-15 18:18:04 -0800229RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
230 mCtx = ctx;
231 mScript = s;
232
Chris Wailes44bef6f2014-08-12 13:51:10 -0700233 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800234
Chris Wailes44bef6f2014-08-12 13:51:10 -0700235 mRoot = nullptr;
236 mRootExpand = nullptr;
237 mInit = nullptr;
238 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800239 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800240
Chris Wailes44bef6f2014-08-12 13:51:10 -0700241 mBoundAllocs = nullptr;
242 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800243 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800244
Yang Nicb170152015-04-16 10:27:02 -0700245 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800246 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800247}
248
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800249bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800250 // The shared object may have an invalid build checksum.
251 // Validate and fail early.
252 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Niade31372016-04-06 09:34:34 -0700253 mScriptSO, mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800254
255 if (mScriptExec == nullptr) {
256 return false;
257 }
258
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800259 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
260 if (mRoot) {
261 //ALOGE("Found root(): %p", mRoot);
262 }
263 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
264 if (mRootExpand) {
265 //ALOGE("Found root.expand(): %p", mRootExpand);
266 }
Matt Wala14ce0072015-07-30 17:30:25 -0700267 mInit = (InitOrDtorFunc_t) dlsym(mScriptSO, "init");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800268 if (mInit) {
269 //ALOGE("Found init(): %p", mInit);
270 }
Matt Wala14ce0072015-07-30 17:30:25 -0700271 mFreeChildren = (InitOrDtorFunc_t) dlsym(mScriptSO, ".rs.dtor");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800272 if (mFreeChildren) {
273 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
274 }
275
Yang Nid9bae682015-01-20 15:31:15 -0800276 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800277 if (varCount > 0) {
278 mBoundAllocs = new Allocation *[varCount];
279 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
280 }
281
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800282 mIsThreadable = mScriptExec->getThreadable();
283 //ALOGE("Script isThreadable? %d", mIsThreadable);
284
Stephen Hines8409d642015-04-28 18:49:56 -0700285 if (kDebugGlobalVariables) {
286 mScriptExec->dumpGlobalInfo();
287 }
288
Yang Nid9bae682015-01-20 15:31:15 -0800289 return true;
290}
291
Jason Sams709a0972012-11-15 18:18:04 -0800292bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
293 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800294 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800295 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
296 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800297 //ALOGE("rsdScriptInit %p %p", rsc, script);
298
299 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700300#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800301 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800302
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700303 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
304 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700305 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700306 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700307 return false;
308 }
309
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700310 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700311
312 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800313 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700314 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700315
verena beckhamf5029802015-05-22 16:51:42 +0100316 int optLevel = mCtx->getContext()->getOptLevel();
317
Chris Wailes6847e732014-08-11 17:30:51 -0700318 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700319 bcFileName.append("/");
320 bcFileName.append(resName);
321 bcFileName.append(".bc");
322
323 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700324 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
325 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700326 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700327 useRSDebugContext, bccPluginName, emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +0100328 optLevel, emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800329
Stephen Hines6a236ad2015-06-22 14:51:30 -0700330 mChecksumNeeded = isChecksumNeeded(cacheDir);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800331 if (mChecksumNeeded) {
332 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
333 core_lib,
334 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700335
336 // The last argument of compileArguments is a nullptr, so remove 1 from
337 // the size.
338 std::unique_ptr<const char> compileCommandLine(
339 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
340
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800341 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
342 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700343 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800344
Yang Nicb170152015-04-16 10:27:02 -0700345 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800346 // cannot compute checksum but verification is enabled
347 mCtx->unlockMutex();
348 return false;
349 }
350 }
351 else {
352 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700353 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800354 }
355
356 // Append build checksum to commandline
357 // Handle the terminal nullptr in compileArguments
358 compileArguments.pop_back();
359 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700360 std::stringstream ss;
361 ss << std::hex << mBuildChecksum;
Yang Ni578419f2016-06-27 16:12:25 -0700362 std::string checksumStr(ss.str());
363 compileArguments.push_back(checksumStr.c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800364 compileArguments.push_back(nullptr);
365
Yang Nia845c352017-05-01 15:53:23 -0700366 const bool reuse = !is_force_recompile() && !useRSDebugContext;
367 if (reuse) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800368 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800369
370 // Read RS info from the shared object to detect checksum mismatch
371 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
372 dlclose(mScriptSO);
373 mScriptSO = nullptr;
374 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700375 }
376
Yang Nia845c352017-05-01 15:53:23 -0700377 // If reuse is desired and we can't, it's either not there or out of date.
378 // We compile the bit code and try loading again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800379 if (mScriptSO == nullptr) {
380 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700381 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800382 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700383 ALOGE("bcc: FAILS to compile '%s'", resName);
384 mCtx->unlockMutex();
385 return false;
386 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800387
Yang Nia845c352017-05-01 15:53:23 -0700388 std::string SOPath;
389
390 if (!SharedLibraryUtils::createSharedLibrary(
391 mCtx->getContext()->getDriverName(), cacheDir, resName, reuse,
392 &SOPath)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800393 ALOGE("Linker: Failed to link object file '%s'", resName);
394 mCtx->unlockMutex();
395 return false;
396 }
397
Yang Nia845c352017-05-01 15:53:23 -0700398 if (reuse) {
399 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
400 } else {
401 mScriptSO = SharedLibraryUtils::loadAndDeleteSharedLibrary(SOPath.c_str());
402 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800403 if (mScriptSO == nullptr) {
404 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700405 mCtx->unlockMutex();
406 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700407 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800408
409 // Read RS symbol information from the .so.
410 if (!storeRSInfoFromSO()) {
411 goto error;
412 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700413 }
Jason Sams709a0972012-11-15 18:18:04 -0800414
Miao Wang82e135c2017-02-27 23:35:35 -0800415 mBitcodeFilePath.assign(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800416
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700417#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800418 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
419 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700420
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800421 if (!mScriptSO) {
422 goto error;
423 }
Jason Sams110f1812013-03-14 16:02:18 -0700424
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800425 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700426 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700427 }
428#endif
Jason Sams709a0972012-11-15 18:18:04 -0800429 mCtx->unlockMutex();
430 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700431
Jason Sams110f1812013-03-14 16:02:18 -0700432error:
433
434 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700435 if (mScriptSO) {
436 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800437 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700438 }
439 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800440}
441
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700442#ifndef RS_COMPATIBILITY_LIB
443
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700444const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
445 size_t bitcodeSize) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200446 const char* defaultLib = SYSLIBPATH_BC"/libclcore.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700447
448 // If we're debugging, use the debug library.
449 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200450 return SYSLIBPATH_BC"/libclcore_debug.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700451 }
452
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000453 if (ME.hasDebugInfo()) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200454 return SYSLIBPATH_BC"/libclcore_g.bc";
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000455 }
456
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700457 // If a callback has been registered to specify a library, use that.
458 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700459 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700460 return selectRTCallback((const char*)bitcode, bitcodeSize);
461 }
462
463 // Check for a platform specific library
464#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
465 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700466 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700467 // NEON-capable ARMv7a devices can use an accelerated math library
468 // for all reduced precision scripts.
469 // ARMv8 does not use NEON, as ASIMD can be used with all precision
470 // levels.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200471 return SYSLIBPATH_BC"/libclcore_neon.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700472 } else {
473 return defaultLib;
474 }
475#elif defined(__i386__) || defined(__x86_64__)
476 // x86 devices will use an optimized library.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200477 return SYSLIBPATH_BC"/libclcore_x86.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700478#else
479 return defaultLib;
480#endif
481}
482
483#endif
484
Jason Sams709a0972012-11-15 18:18:04 -0800485void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700486 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800487 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700488 script->mHal.info.exportedReduceCount = mScriptExec->getExportedReduceCount();
489 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800490 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800491 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800492 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
493 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700494
495 // Bug, need to stash in metadata
496 if (mRootExpand) {
497 script->mHal.info.root = mRootExpand;
498 } else {
499 script->mHal.info.root = mRoot;
500 }
Jason Sams709a0972012-11-15 18:18:04 -0800501}
502
Matt Wala14ce0072015-07-30 17:30:25 -0700503// Set up the launch dimensions, and write the values of the launch
504// dimensions into the mtls start/end fields.
505//
506// Inputs:
507// baseDim - base shape of the input
508// sc - used to constrain the launch dimensions
509//
510// Returns:
511// True on success, false on failure to set up
512bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
513 const RsLaunchDimensions &baseDim,
514 const RsScriptCall *sc) {
515 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800516
Matt Wala14ce0072015-07-30 17:30:25 -0700517#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
518 if (!sc || (sc->SC_FIELD##End == 0)) { \
519 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
520 } else { \
521 mtls->start.DIM_FIELD = \
522 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
523 mtls->end.DIM_FIELD = \
524 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
525 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
526 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
527 "Failed to launch kernel; Invalid " \
528 #SC_FIELD "Start or " #SC_FIELD "End."); \
529 return false; \
530 } \
531 }} while(0)
532
533 SET_UP_DIMENSION(x, x);
534 SET_UP_DIMENSION(y, y);
535 SET_UP_DIMENSION(z, z);
Yang Ni7784b5d2016-05-04 09:44:48 -0700536 // Checks and setup of fields other than x, y, z are ignored, since those
537 // fields are not used in the runtime and are not visible in the Java API.
Matt Wala14ce0072015-07-30 17:30:25 -0700538#undef SET_UP_DIMENSION
539
540 return true;
541}
542
David Grossfc7ab792016-06-01 14:45:47 -0700543// Preliminary work to prepare a general reduce-style kernel for launch.
544bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation ** ains,
545 uint32_t inLen,
546 const Allocation * aout,
Matt Wala14ce0072015-07-30 17:30:25 -0700547 const RsScriptCall *sc,
548 MTLaunchStructReduce *mtls) {
David Gross6c1876b2016-01-15 11:52:14 -0800549 rsAssert(ains && (inLen >= 1) && aout);
David Grossfc7ab792016-06-01 14:45:47 -0700550 memset(mtls, 0, sizeof(MTLaunchStructReduce));
David Gross6c1876b2016-01-15 11:52:14 -0800551 mtls->dimPtr = &mtls->redp.dim;
552
553 for (int index = inLen; --index >= 0;) {
554 if (allocationLODIsNull(ains[index])) {
555 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
556 "reduce called with null in allocations");
557 return false;
558 }
559 }
560
561 if (allocationLODIsNull(aout)) {
562 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
563 "reduce called with null out allocation");
564 return false;
565 }
566
567 const Allocation *ain0 = ains[0];
568 const Type *inType = ain0->getType();
569
570 mtls->redp.dim.x = inType->getDimX();
571 mtls->redp.dim.y = inType->getDimY();
572 mtls->redp.dim.z = inType->getDimZ();
573
574 for (int Index = inLen; --Index >= 1;) {
575 if (!ain0->hasSameDims(ains[Index])) {
576 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
577 "Failed to launch reduction kernel;"
578 "dimensions of input allocations do not match.");
579 return false;
580 }
581 }
582
583 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
584 return false;
585 }
586
587 // The X & Y walkers always want 0-1 min even if dim is not present
588 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
589 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
590
591 mtls->rs = mCtx;
592
David Gross35dbc8c2016-03-29 13:48:41 -0700593 mtls->mSliceNum = 0;
594 mtls->mSliceSize = 1;
595 mtls->isThreadable = mIsThreadable;
David Gross6c1876b2016-01-15 11:52:14 -0800596
597 // Set up output,
598 mtls->redp.outLen = 1;
599 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
600 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
601
602 // Set up input.
603 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
604 mtls->redp.inLen = inLen;
605 for (int index = inLen; --index >= 0;) {
606 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
607 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
608 }
609
610 // All validation passed, ok to launch threads
611 return true;
612}
613
Matt Wala14ce0072015-07-30 17:30:25 -0700614// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800615bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700616 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700617 Allocation * aout,
618 const void * usr, uint32_t usrLen,
619 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700620 MTLaunchStructForEach *mtls) {
Miao Wang68e00892016-02-25 12:44:10 -0800621 if (ains == nullptr && inLen != 0) {
622 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
623 "rsForEach called with none-zero inLen with null in allocations");
624 return false;
625 }
626
Matt Wala14ce0072015-07-30 17:30:25 -0700627 memset(mtls, 0, sizeof(MTLaunchStructForEach));
628 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700629
Chris Wailesf3712132014-07-16 15:18:30 -0700630 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700631 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700632 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
633 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800634 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700635 }
636 }
637
Matt Wala14ce0072015-07-30 17:30:25 -0700638 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700639 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
640 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800641 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700642 }
643
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700644 // The only situation where ains[j] is null is when inLen==1 and j==0;
645 // and that can only happen for an old-style kernel in API level 11~13,
646 // where the input allocation cannot be skipped if the output allocation is specified.
647 if (inLen != 0)
648 rsAssert((inLen == 1) || (ains[0] != nullptr));
649
650 if (inLen > 0 && ains[0]) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700651 const Allocation *ain0 = ains[0];
652 const Type *inType = ain0->getType();
653
Jason Samsc0d68472015-01-20 14:29:52 -0800654 mtls->fep.dim.x = inType->getDimX();
655 mtls->fep.dim.y = inType->getDimY();
656 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700657
658 for (int Index = inLen; --Index >= 1;) {
659 if (!ain0->hasSameDims(ains[Index])) {
660 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700661 "Failed to launch kernel; dimensions of input "
Yang Nie8f9fba2015-01-30 08:55:10 -0800662 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800663 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700664 }
665 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700666 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700667 const Type *outType = aout->getType();
668
Jason Samsc0d68472015-01-20 14:29:52 -0800669 mtls->fep.dim.x = outType->getDimX();
670 mtls->fep.dim.y = outType->getDimY();
671 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700672
Jason Samsa9139c72015-04-16 15:11:04 -0700673 } else if (sc != nullptr) {
674 mtls->fep.dim.x = sc->xEnd;
675 mtls->fep.dim.y = sc->yEnd;
676 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700677 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700678 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
679 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800680 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700681 }
682
Chris Wailes44bef6f2014-08-12 13:51:10 -0700683 if (inLen > 0 && aout != nullptr) {
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700684 if (ains[0] && !ains[0]->hasSameDims(aout)) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700685 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
686 "Failed to launch kernel; dimensions of input and output allocations do not match.");
687
Jason Samsbf2111d2015-01-26 18:13:41 -0800688 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700689 }
690 }
691
Matt Wala14ce0072015-07-30 17:30:25 -0700692 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
693 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700694 }
695
Jason Samsbf2111d2015-01-26 18:13:41 -0800696 // The X & Y walkers always want 0-1 min even if dim is not present
697 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
698 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700699 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800700 if (ains) {
701 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
702 }
703 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700704 mtls->fep.usr = usr;
705 mtls->fep.usrLen = usrLen;
706 mtls->mSliceSize = 1;
707 mtls->mSliceNum = 0;
708
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700709 mtls->isThreadable = mIsThreadable;
710
Chris Wailesf3712132014-07-16 15:18:30 -0700711 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700712 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700713 for (int index = inLen; --index >= 0;) {
I-Jui (Ray) Sungefc33a92017-05-12 16:05:14 -0700714 if (ains[index] == nullptr) {
715 // In old style kernels, the first and only input allocation could be null.
716 // Not allowed in newer styles.
717 rsAssert(inLen == 1 && index == 0);
718 continue;
719 }
Jason Samsc0d68472015-01-20 14:29:52 -0800720 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
721 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700722 }
723 }
724
Chris Wailes44bef6f2014-08-12 13:51:10 -0700725 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800726 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
727 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700728 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800729
730 // All validation passed, ok to launch threads
731 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700732}
733
Jason Sams709a0972012-11-15 18:18:04 -0800734
735void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700736 const Allocation ** ains,
737 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800738 Allocation * aout,
739 const void * usr,
740 uint32_t usrLen,
741 const RsScriptCall *sc) {
742
Matt Wala14ce0072015-07-30 17:30:25 -0700743 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700744
Jason Samsbf2111d2015-01-26 18:13:41 -0800745 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
746 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700747
Jason Samsbf2111d2015-01-26 18:13:41 -0800748 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700749 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800750 mCtx->setTLS(oldTLS);
751 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700752}
753
Matt Wala14ce0072015-07-30 17:30:25 -0700754void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
David Grossfc7ab792016-06-01 14:45:47 -0700755 const Allocation ** ains, uint32_t inLen,
Matt Wala14ce0072015-07-30 17:30:25 -0700756 Allocation *aout,
757 const RsScriptCall *sc) {
David Grossfc7ab792016-06-01 14:45:47 -0700758 MTLaunchStructReduce mtls;
Matt Wala14ce0072015-07-30 17:30:25 -0700759
David Grossfc7ab792016-06-01 14:45:47 -0700760 if (reduceMtlsSetup(ains, inLen, aout, sc, &mtls)) {
761 reduceKernelSetup(slot, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800762 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
David Grossfc7ab792016-06-01 14:45:47 -0700763 mCtx->launchReduce(ains, inLen, aout, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800764 mCtx->setTLS(oldTLS);
765 }
766}
767
Matt Wala14ce0072015-07-30 17:30:25 -0700768void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800769 mtls->script = this;
770 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800771 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700772 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800773}
774
Matt Wala14ce0072015-07-30 17:30:25 -0700775void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
776 mtls->script = this;
David Gross6c1876b2016-01-15 11:52:14 -0800777 mtls->redp.slot = slot;
778
David Grossfc7ab792016-06-01 14:45:47 -0700779 const ReduceDescription *desc = mScriptExec->getReduceDescription(slot);
David Gross6c1876b2016-01-15 11:52:14 -0800780 mtls->accumFunc = desc->accumFunc;
781 mtls->initFunc = desc->initFunc; // might legally be nullptr
David Gross35dbc8c2016-03-29 13:48:41 -0700782 mtls->combFunc = desc->combFunc; // might legally be nullptr
David Gross6c1876b2016-01-15 11:52:14 -0800783 mtls->outFunc = desc->outFunc; // might legally be nullptr
784 mtls->accumSize = desc->accumSize;
785
786 rsAssert(mtls->accumFunc != nullptr);
787}
788
Jason Sams709a0972012-11-15 18:18:04 -0800789int RsdCpuScriptImpl::invokeRoot() {
790 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
791 int ret = mRoot();
792 mCtx->setTLS(oldTLS);
793 return ret;
794}
795
796void RsdCpuScriptImpl::invokeInit() {
797 if (mInit) {
798 mInit();
799 }
800}
801
802void RsdCpuScriptImpl::invokeFreeChildren() {
803 if (mFreeChildren) {
804 mFreeChildren();
805 }
806}
807
808void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
809 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800810 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800811 void * ap = nullptr;
812
813#if defined(__x86_64__)
814 // The invoked function could have input parameter of vector type for example float4 which
815 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
816 // So try to align void* params before passing them into RS exported function.
817
818 if ((uint8_t)(uint64_t)params & 0x0F) {
819 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
820 memcpy(ap, params, paramLength);
821 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800822 ALOGE("x86_64: invokeFunction memalign error, still use params which"
823 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800824 }
825 }
826#endif
Jason Sams709a0972012-11-15 18:18:04 -0800827
828 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800829 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800830 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800831
Yong Chenc246d912015-05-22 18:20:06 +0800832#if defined(__x86_64__)
833 free(ap);
834#endif
835
Jason Sams709a0972012-11-15 18:18:04 -0800836 mCtx->setTLS(oldTLS);
837}
838
839void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
840 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800841 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800842
843 //if (mIntrinsicID) {
844 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
845 //return;
846 //}
847
Yang Nid9bae682015-01-20 15:31:15 -0800848 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800849 if (!destPtr) {
850 //ALOGV("Calling setVar on slot = %i which is null", slot);
851 return;
852 }
853
854 memcpy(destPtr, data, dataLength);
855}
856
Tim Murray9c642392013-04-11 13:29:59 -0700857void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
858 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800859 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700860
Yang Nid9bae682015-01-20 15:31:15 -0800861 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700862 if (!srcPtr) {
863 //ALOGV("Calling setVar on slot = %i which is null", slot);
864 return;
865 }
866 memcpy(data, srcPtr, dataLength);
867}
868
869
Jason Sams709a0972012-11-15 18:18:04 -0800870void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
871 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700872 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800873 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800874 if (!destPtr) {
875 //ALOGV("Calling setVar on slot = %i which is null", slot);
876 return;
877 }
878
879 // We want to look at dimension in terms of integer components,
880 // but dimLength is given in terms of bytes.
881 dimLength /= sizeof(int);
882
883 // Only a single dimension is currently supported.
884 rsAssert(dimLength == 1);
885 if (dimLength == 1) {
886 // First do the increment loop.
887 size_t stride = elem->getSizeBytes();
888 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700889 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800890 elem->incRefs(cVal);
891 cVal += stride;
892 }
893
894 // Decrement loop comes after (to prevent race conditions).
895 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700896 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800897 elem->decRefs(oldVal);
898 oldVal += stride;
899 }
900 }
901
902 memcpy(destPtr, data, dataLength);
903}
904
905void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
906
907 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800908 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800909
Yang Nid9bae682015-01-20 15:31:15 -0800910 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800911 if (!destPtr) {
912 //ALOGV("Calling setVar on slot = %i which is null", slot);
913 return;
914 }
915
Chris Wailes44bef6f2014-08-12 13:51:10 -0700916 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800917 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800918 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800919 ptr = data->mHal.drvState.lod[0].mallocPtr;
920 }
921 memcpy(destPtr, &ptr, sizeof(void *));
922}
923
924void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
925
926 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800927 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800928
Yang Nid9bae682015-01-20 15:31:15 -0800929 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800930 if (!destPtr) {
931 //ALOGV("Calling setVar on slot = %i which is null", slot);
932 return;
933 }
934
Jason Sams05ef73f2014-08-05 14:59:22 -0700935 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800936}
937
Yang Ni062c2872015-02-20 15:20:00 -0800938const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
939 return mScriptExec->getFieldName(slot);
940}
941
Jason Sams709a0972012-11-15 18:18:04 -0800942RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700943 delete mScriptExec;
944 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700945 if (mScriptSO) {
946 dlclose(mScriptSO);
947 }
Jason Sams709a0972012-11-15 18:18:04 -0800948}
949
950Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
951 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700952 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800953 }
954
955 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
956 Allocation *a = mBoundAllocs[ct];
957 if (!a) continue;
958 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
959 return a;
960 }
961 }
962 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700963 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800964}
965
Stephen Hines8409d642015-04-28 18:49:56 -0700966int RsdCpuScriptImpl::getGlobalEntries() const {
967 return mScriptExec->getGlobalEntries();
968}
969
970const char * RsdCpuScriptImpl::getGlobalName(int i) const {
971 return mScriptExec->getGlobalName(i);
972}
973
974const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
975 return mScriptExec->getGlobalAddress(i);
976}
977
978size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
979 return mScriptExec->getGlobalSize(i);
980}
981
Stephen Hines5aa018c2015-05-20 18:09:57 -0700982uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
983 return mScriptExec->getGlobalProperties(i);
984}
985
Chris Wailesf3712132014-07-16 15:18:30 -0700986void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
987 uint32_t inLen, Allocation * aout,
988 const void * usr, uint32_t usrLen,
989 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700990
Chris Wailesf3712132014-07-16 15:18:30 -0700991void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
992 uint32_t inLen, Allocation * aout,
993 const void * usr, uint32_t usrLen,
994 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700995
Jason Sams709a0972012-11-15 18:18:04 -0800996
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800997} // namespace renderscript
998} // namespace android