blob: cf1b869f3994d74463cd0e8edbc0691fa45f18dd [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 Ni29791302017-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 Ni29791302017-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 Ni29791302017-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 Ni29791302017-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) {
Yang Nifb709342017-05-16 14:20:03 -0700450 if (ME.hasDebugInfo()) {
451 return SYSLIBPATH_BC"/libclcore_debug_g.bc";
452 }
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200453 return SYSLIBPATH_BC"/libclcore_debug.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700454 }
455
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000456 if (ME.hasDebugInfo()) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200457 return SYSLIBPATH_BC"/libclcore_g.bc";
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000458 }
459
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700460 // If a callback has been registered to specify a library, use that.
461 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700462 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700463 return selectRTCallback((const char*)bitcode, bitcodeSize);
464 }
465
466 // Check for a platform specific library
467#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
468 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700469 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700470 // NEON-capable ARMv7a devices can use an accelerated math library
471 // for all reduced precision scripts.
472 // ARMv8 does not use NEON, as ASIMD can be used with all precision
473 // levels.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200474 return SYSLIBPATH_BC"/libclcore_neon.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700475 } else {
476 return defaultLib;
477 }
478#elif defined(__i386__) || defined(__x86_64__)
479 // x86 devices will use an optimized library.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200480 return SYSLIBPATH_BC"/libclcore_x86.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700481#else
482 return defaultLib;
483#endif
484}
485
486#endif
487
Jason Sams709a0972012-11-15 18:18:04 -0800488void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700489 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800490 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700491 script->mHal.info.exportedReduceCount = mScriptExec->getExportedReduceCount();
492 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800493 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800494 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800495 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
496 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700497
498 // Bug, need to stash in metadata
499 if (mRootExpand) {
500 script->mHal.info.root = mRootExpand;
501 } else {
502 script->mHal.info.root = mRoot;
503 }
Jason Sams709a0972012-11-15 18:18:04 -0800504}
505
Matt Wala14ce0072015-07-30 17:30:25 -0700506// Set up the launch dimensions, and write the values of the launch
507// dimensions into the mtls start/end fields.
508//
509// Inputs:
510// baseDim - base shape of the input
511// sc - used to constrain the launch dimensions
512//
513// Returns:
514// True on success, false on failure to set up
515bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
516 const RsLaunchDimensions &baseDim,
517 const RsScriptCall *sc) {
518 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800519
Matt Wala14ce0072015-07-30 17:30:25 -0700520#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
521 if (!sc || (sc->SC_FIELD##End == 0)) { \
522 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
523 } else { \
524 mtls->start.DIM_FIELD = \
525 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
526 mtls->end.DIM_FIELD = \
527 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
528 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
529 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
530 "Failed to launch kernel; Invalid " \
531 #SC_FIELD "Start or " #SC_FIELD "End."); \
532 return false; \
533 } \
534 }} while(0)
535
536 SET_UP_DIMENSION(x, x);
537 SET_UP_DIMENSION(y, y);
538 SET_UP_DIMENSION(z, z);
Yang Ni7784b5d2016-05-04 09:44:48 -0700539 // Checks and setup of fields other than x, y, z are ignored, since those
540 // fields are not used in the runtime and are not visible in the Java API.
Matt Wala14ce0072015-07-30 17:30:25 -0700541#undef SET_UP_DIMENSION
542
543 return true;
544}
545
David Grossfc7ab792016-06-01 14:45:47 -0700546// Preliminary work to prepare a general reduce-style kernel for launch.
547bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation ** ains,
548 uint32_t inLen,
549 const Allocation * aout,
Matt Wala14ce0072015-07-30 17:30:25 -0700550 const RsScriptCall *sc,
551 MTLaunchStructReduce *mtls) {
David Gross6c1876b2016-01-15 11:52:14 -0800552 rsAssert(ains && (inLen >= 1) && aout);
David Grossfc7ab792016-06-01 14:45:47 -0700553 memset(mtls, 0, sizeof(MTLaunchStructReduce));
David Gross6c1876b2016-01-15 11:52:14 -0800554 mtls->dimPtr = &mtls->redp.dim;
555
556 for (int index = inLen; --index >= 0;) {
557 if (allocationLODIsNull(ains[index])) {
558 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
559 "reduce called with null in allocations");
560 return false;
561 }
562 }
563
564 if (allocationLODIsNull(aout)) {
565 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
566 "reduce called with null out allocation");
567 return false;
568 }
569
570 const Allocation *ain0 = ains[0];
571 const Type *inType = ain0->getType();
572
573 mtls->redp.dim.x = inType->getDimX();
574 mtls->redp.dim.y = inType->getDimY();
575 mtls->redp.dim.z = inType->getDimZ();
576
577 for (int Index = inLen; --Index >= 1;) {
578 if (!ain0->hasSameDims(ains[Index])) {
579 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
580 "Failed to launch reduction kernel;"
581 "dimensions of input allocations do not match.");
582 return false;
583 }
584 }
585
586 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
587 return false;
588 }
589
590 // The X & Y walkers always want 0-1 min even if dim is not present
591 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
592 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
593
594 mtls->rs = mCtx;
595
David Gross35dbc8c2016-03-29 13:48:41 -0700596 mtls->mSliceNum = 0;
597 mtls->mSliceSize = 1;
598 mtls->isThreadable = mIsThreadable;
David Gross6c1876b2016-01-15 11:52:14 -0800599
600 // Set up output,
601 mtls->redp.outLen = 1;
602 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
603 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
604
605 // Set up input.
606 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
607 mtls->redp.inLen = inLen;
608 for (int index = inLen; --index >= 0;) {
609 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
610 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
611 }
612
613 // All validation passed, ok to launch threads
614 return true;
615}
616
Matt Wala14ce0072015-07-30 17:30:25 -0700617// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800618bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700619 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700620 Allocation * aout,
621 const void * usr, uint32_t usrLen,
622 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700623 MTLaunchStructForEach *mtls) {
Miao Wang68e00892016-02-25 12:44:10 -0800624 if (ains == nullptr && inLen != 0) {
625 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
626 "rsForEach called with none-zero inLen with null in allocations");
627 return false;
628 }
629
Matt Wala14ce0072015-07-30 17:30:25 -0700630 memset(mtls, 0, sizeof(MTLaunchStructForEach));
631 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700632
Chris Wailesf3712132014-07-16 15:18:30 -0700633 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700634 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700635 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
636 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800637 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700638 }
639 }
640
Matt Wala14ce0072015-07-30 17:30:25 -0700641 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700642 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
643 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800644 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700645 }
646
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700647 // The only situation where ains[j] is null is when inLen==1 and j==0;
648 // and that can only happen for an old-style kernel in API level 11~13,
649 // where the input allocation cannot be skipped if the output allocation is specified.
650 if (inLen != 0)
651 rsAssert((inLen == 1) || (ains[0] != nullptr));
652
653 if (inLen > 0 && ains[0]) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700654 const Allocation *ain0 = ains[0];
655 const Type *inType = ain0->getType();
656
Jason Samsc0d68472015-01-20 14:29:52 -0800657 mtls->fep.dim.x = inType->getDimX();
658 mtls->fep.dim.y = inType->getDimY();
659 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700660
661 for (int Index = inLen; --Index >= 1;) {
662 if (!ain0->hasSameDims(ains[Index])) {
663 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700664 "Failed to launch kernel; dimensions of input "
Yang Nie8f9fba2015-01-30 08:55:10 -0800665 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800666 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700667 }
668 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700669 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700670 const Type *outType = aout->getType();
671
Jason Samsc0d68472015-01-20 14:29:52 -0800672 mtls->fep.dim.x = outType->getDimX();
673 mtls->fep.dim.y = outType->getDimY();
674 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700675
Jason Samsa9139c72015-04-16 15:11:04 -0700676 } else if (sc != nullptr) {
677 mtls->fep.dim.x = sc->xEnd;
678 mtls->fep.dim.y = sc->yEnd;
679 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700680 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700681 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
682 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800683 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700684 }
685
Chris Wailes44bef6f2014-08-12 13:51:10 -0700686 if (inLen > 0 && aout != nullptr) {
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700687 if (ains[0] && !ains[0]->hasSameDims(aout)) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700688 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
689 "Failed to launch kernel; dimensions of input and output allocations do not match.");
690
Jason Samsbf2111d2015-01-26 18:13:41 -0800691 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700692 }
693 }
694
Matt Wala14ce0072015-07-30 17:30:25 -0700695 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
696 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700697 }
698
Jason Samsbf2111d2015-01-26 18:13:41 -0800699 // The X & Y walkers always want 0-1 min even if dim is not present
700 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
701 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700702 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800703 if (ains) {
704 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
705 }
706 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700707 mtls->fep.usr = usr;
708 mtls->fep.usrLen = usrLen;
709 mtls->mSliceSize = 1;
710 mtls->mSliceNum = 0;
711
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700712 mtls->isThreadable = mIsThreadable;
713
Chris Wailesf3712132014-07-16 15:18:30 -0700714 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700715 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700716 for (int index = inLen; --index >= 0;) {
I-Jui (Ray) Sungf4b6eeb2017-05-12 16:05:14 -0700717 if (ains[index] == nullptr) {
718 // In old style kernels, the first and only input allocation could be null.
719 // Not allowed in newer styles.
720 rsAssert(inLen == 1 && index == 0);
721 continue;
722 }
Jason Samsc0d68472015-01-20 14:29:52 -0800723 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
724 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700725 }
726 }
727
Chris Wailes44bef6f2014-08-12 13:51:10 -0700728 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800729 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
730 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700731 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800732
733 // All validation passed, ok to launch threads
734 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700735}
736
Jason Sams709a0972012-11-15 18:18:04 -0800737
738void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700739 const Allocation ** ains,
740 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800741 Allocation * aout,
742 const void * usr,
743 uint32_t usrLen,
744 const RsScriptCall *sc) {
745
Matt Wala14ce0072015-07-30 17:30:25 -0700746 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700747
Jason Samsbf2111d2015-01-26 18:13:41 -0800748 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
749 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700750
Jason Samsbf2111d2015-01-26 18:13:41 -0800751 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700752 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800753 mCtx->setTLS(oldTLS);
754 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700755}
756
Matt Wala14ce0072015-07-30 17:30:25 -0700757void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
David Grossfc7ab792016-06-01 14:45:47 -0700758 const Allocation ** ains, uint32_t inLen,
Matt Wala14ce0072015-07-30 17:30:25 -0700759 Allocation *aout,
760 const RsScriptCall *sc) {
David Grossfc7ab792016-06-01 14:45:47 -0700761 MTLaunchStructReduce mtls;
Matt Wala14ce0072015-07-30 17:30:25 -0700762
David Grossfc7ab792016-06-01 14:45:47 -0700763 if (reduceMtlsSetup(ains, inLen, aout, sc, &mtls)) {
764 reduceKernelSetup(slot, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800765 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
David Grossfc7ab792016-06-01 14:45:47 -0700766 mCtx->launchReduce(ains, inLen, aout, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800767 mCtx->setTLS(oldTLS);
768 }
769}
770
Matt Wala14ce0072015-07-30 17:30:25 -0700771void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800772 mtls->script = this;
773 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800774 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700775 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800776}
777
Matt Wala14ce0072015-07-30 17:30:25 -0700778void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
779 mtls->script = this;
David Gross6c1876b2016-01-15 11:52:14 -0800780 mtls->redp.slot = slot;
781
David Grossfc7ab792016-06-01 14:45:47 -0700782 const ReduceDescription *desc = mScriptExec->getReduceDescription(slot);
David Gross6c1876b2016-01-15 11:52:14 -0800783 mtls->accumFunc = desc->accumFunc;
784 mtls->initFunc = desc->initFunc; // might legally be nullptr
David Gross35dbc8c2016-03-29 13:48:41 -0700785 mtls->combFunc = desc->combFunc; // might legally be nullptr
David Gross6c1876b2016-01-15 11:52:14 -0800786 mtls->outFunc = desc->outFunc; // might legally be nullptr
787 mtls->accumSize = desc->accumSize;
788
789 rsAssert(mtls->accumFunc != nullptr);
790}
791
Jason Sams709a0972012-11-15 18:18:04 -0800792int RsdCpuScriptImpl::invokeRoot() {
793 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
794 int ret = mRoot();
795 mCtx->setTLS(oldTLS);
796 return ret;
797}
798
799void RsdCpuScriptImpl::invokeInit() {
800 if (mInit) {
801 mInit();
802 }
803}
804
805void RsdCpuScriptImpl::invokeFreeChildren() {
806 if (mFreeChildren) {
807 mFreeChildren();
808 }
809}
810
811void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
812 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800813 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800814 void * ap = nullptr;
815
816#if defined(__x86_64__)
817 // The invoked function could have input parameter of vector type for example float4 which
818 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
819 // So try to align void* params before passing them into RS exported function.
820
821 if ((uint8_t)(uint64_t)params & 0x0F) {
822 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
823 memcpy(ap, params, paramLength);
824 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800825 ALOGE("x86_64: invokeFunction memalign error, still use params which"
826 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800827 }
828 }
829#endif
Jason Sams709a0972012-11-15 18:18:04 -0800830
831 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800832 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800833 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800834
Yong Chenc246d912015-05-22 18:20:06 +0800835#if defined(__x86_64__)
836 free(ap);
837#endif
838
Jason Sams709a0972012-11-15 18:18:04 -0800839 mCtx->setTLS(oldTLS);
840}
841
842void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
843 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800844 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800845
846 //if (mIntrinsicID) {
847 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
848 //return;
849 //}
850
Yang Nid9bae682015-01-20 15:31:15 -0800851 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800852 if (!destPtr) {
853 //ALOGV("Calling setVar on slot = %i which is null", slot);
854 return;
855 }
856
857 memcpy(destPtr, data, dataLength);
858}
859
Tim Murray9c642392013-04-11 13:29:59 -0700860void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
861 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800862 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700863
Yang Nid9bae682015-01-20 15:31:15 -0800864 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700865 if (!srcPtr) {
866 //ALOGV("Calling setVar on slot = %i which is null", slot);
867 return;
868 }
869 memcpy(data, srcPtr, dataLength);
870}
871
872
Jason Sams709a0972012-11-15 18:18:04 -0800873void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
874 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700875 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800876 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800877 if (!destPtr) {
878 //ALOGV("Calling setVar on slot = %i which is null", slot);
879 return;
880 }
881
882 // We want to look at dimension in terms of integer components,
883 // but dimLength is given in terms of bytes.
884 dimLength /= sizeof(int);
885
886 // Only a single dimension is currently supported.
887 rsAssert(dimLength == 1);
888 if (dimLength == 1) {
889 // First do the increment loop.
890 size_t stride = elem->getSizeBytes();
891 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700892 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800893 elem->incRefs(cVal);
894 cVal += stride;
895 }
896
897 // Decrement loop comes after (to prevent race conditions).
898 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700899 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800900 elem->decRefs(oldVal);
901 oldVal += stride;
902 }
903 }
904
905 memcpy(destPtr, data, dataLength);
906}
907
908void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
909
910 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800911 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800912
Yang Nid9bae682015-01-20 15:31:15 -0800913 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800914 if (!destPtr) {
915 //ALOGV("Calling setVar on slot = %i which is null", slot);
916 return;
917 }
918
Chris Wailes44bef6f2014-08-12 13:51:10 -0700919 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800920 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800921 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800922 ptr = data->mHal.drvState.lod[0].mallocPtr;
923 }
924 memcpy(destPtr, &ptr, sizeof(void *));
925}
926
927void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
928
929 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800930 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800931
Yang Nid9bae682015-01-20 15:31:15 -0800932 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800933 if (!destPtr) {
934 //ALOGV("Calling setVar on slot = %i which is null", slot);
935 return;
936 }
937
Jason Sams05ef73f2014-08-05 14:59:22 -0700938 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800939}
940
Yang Ni062c2872015-02-20 15:20:00 -0800941const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
942 return mScriptExec->getFieldName(slot);
943}
944
Jason Sams709a0972012-11-15 18:18:04 -0800945RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700946 delete mScriptExec;
947 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700948 if (mScriptSO) {
949 dlclose(mScriptSO);
950 }
Jason Sams709a0972012-11-15 18:18:04 -0800951}
952
953Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
954 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700955 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800956 }
957
958 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
959 Allocation *a = mBoundAllocs[ct];
960 if (!a) continue;
961 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
962 return a;
963 }
964 }
965 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700966 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800967}
968
Stephen Hines8409d642015-04-28 18:49:56 -0700969int RsdCpuScriptImpl::getGlobalEntries() const {
970 return mScriptExec->getGlobalEntries();
971}
972
973const char * RsdCpuScriptImpl::getGlobalName(int i) const {
974 return mScriptExec->getGlobalName(i);
975}
976
977const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
978 return mScriptExec->getGlobalAddress(i);
979}
980
981size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
982 return mScriptExec->getGlobalSize(i);
983}
984
Stephen Hines5aa018c2015-05-20 18:09:57 -0700985uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
986 return mScriptExec->getGlobalProperties(i);
987}
988
Chris Wailesf3712132014-07-16 15:18:30 -0700989void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
990 uint32_t inLen, Allocation * aout,
991 const void * usr, uint32_t usrLen,
992 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700993
Chris Wailesf3712132014-07-16 15:18:30 -0700994void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
995 uint32_t inLen, Allocation * aout,
996 const void * usr, uint32_t usrLen,
997 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700998
Jason Sams709a0972012-11-15 18:18:04 -0800999
Rahul Chaudhry7974fc02017-02-09 12:33:28 -08001000} // namespace renderscript
1001} // namespace android