blob: a88af2fe475c7d96344ba482b055d772c71a8437 [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
Stephen Hines82e0a672014-05-05 15:40:56 -070028 #include <bcc/Config/Config.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070029 #include <bcinfo/MetadataExtractor.h>
Stephen Hinesba17ae42013-06-05 17:18:04 -070030 #include <cutils/properties.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070031
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -080032 #include <zlib.h>
33 #include <sys/file.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070034 #include <sys/types.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070035 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080036
37 #include <string>
38 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070039#endif
Jason Sams709a0972012-11-15 18:18:04 -080040
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080041#include <set>
42#include <string>
43#include <dlfcn.h>
44#include <stdlib.h>
45#include <string.h>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080046#include <iostream>
Yang Nicb170152015-04-16 10:27:02 -070047#include <sstream>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080048
Stephen Hinesba17ae42013-06-05 17:18:04 -070049namespace {
Stephen Hines8409d642015-04-28 18:49:56 -070050
51static const bool kDebugGlobalVariables = false;
52
Matt Wala14ce0072015-07-30 17:30:25 -070053static bool allocationLODIsNull(const android::renderscript::Allocation *alloc) {
54 // Even if alloc != nullptr, mallocPtr could be null if
55 // IO_OUTPUT/IO_INPUT with no bound surface.
56 return alloc && alloc->mHal.drvState.lod[0].mallocPtr == nullptr;
57}
58
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080059#ifndef RS_COMPATIBILITY_LIB
60
Stephen Hinesba17ae42013-06-05 17:18:04 -070061static bool is_force_recompile() {
62#ifdef RS_SERVER
63 return false;
64#else
65 char buf[PROPERTY_VALUE_MAX];
66
67 // Re-compile if floating point precision has been overridden.
68 property_get("debug.rs.precision", buf, "");
69 if (buf[0] != '\0') {
70 return true;
71 }
72
73 // Re-compile if debug.rs.forcerecompile is set.
74 property_get("debug.rs.forcerecompile", buf, "0");
75 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
76 return true;
77 } else {
78 return false;
79 }
80#endif // RS_SERVER
81}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070082
Chris Wailes6847e732014-08-11 17:30:51 -070083static void setCompileArguments(std::vector<const char*>* args,
84 const std::string& bcFileName,
85 const char* cacheDir, const char* resName,
86 const char* core_lib, bool useRSDebugContext,
Stephen Hines8409d642015-04-28 18:49:56 -070087 const char* bccPluginName, bool emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +010088 int optLevel, bool emitGlobalInfoSkipConstant) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070089 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080090 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080091 args->push_back("-unroll-runtime");
92 args->push_back("-scalarize-load-store");
Stephen Hines8409d642015-04-28 18:49:56 -070093 if (emitGlobalInfo) {
94 args->push_back("-rs-global-info");
95 if (emitGlobalInfoSkipConstant) {
96 args->push_back("-rs-global-info-skip-constant");
97 }
98 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070099 args->push_back("-o");
100 args->push_back(resName);
101 args->push_back("-output_path");
102 args->push_back(cacheDir);
103 args->push_back("-bclib");
104 args->push_back(core_lib);
105 args->push_back("-mtriple");
106 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
verena beckhamf5029802015-05-22 16:51:42 +0100107 args->push_back("-O");
108
109 switch (optLevel) {
Yang Ni7a106ad2016-03-10 16:06:36 -0800110 case 0:
verena beckhamf5029802015-05-22 16:51:42 +0100111 args->push_back("0");
112 break;
Yang Ni7a106ad2016-03-10 16:06:36 -0800113 case 3:
verena beckhamf5029802015-05-22 16:51:42 +0100114 args->push_back("3");
115 break;
116 default:
117 ALOGW("Expected optimization level of 0 or 3. Received %d", optLevel);
118 args->push_back("3");
119 break;
120 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700121
Tim Murray358ffb82014-12-09 11:53:06 -0800122 // Enable workaround for A53 codegen by default.
123#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
124 args->push_back("-aarch64-fix-cortex-a53-835769");
125#endif
126
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700127 // Execute the bcc compiler.
128 if (useRSDebugContext) {
129 args->push_back("-rs-debug-ctx");
130 } else {
131 // Only load additional libraries for compiles that don't use
132 // the debug context.
133 if (bccPluginName && strlen(bccPluginName) > 0) {
134 args->push_back("-load");
135 args->push_back(bccPluginName);
136 }
137 }
138
Stephen Hines45e753a2015-01-19 20:58:44 -0800139 args->push_back("-fPIC");
140 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800141
Chris Wailes6847e732014-08-11 17:30:51 -0700142 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700143 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700144}
145
Chris Wailes6847e732014-08-11 17:30:51 -0700146static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700147 const char *bitcode,
148 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700149 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700150 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700151
Chris Wailes6847e732014-08-11 17:30:51 -0700152 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700153 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700154 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700155 return false;
156 }
157 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
158 fclose(bcfile);
159 if (nwritten != bitcodeSize) {
160 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700161 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700162 return false;
163 }
164
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700165 return android::renderscript::rsuExecuteCommand(
166 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
167 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700168}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700169
Stephen Hines6a236ad2015-06-22 14:51:30 -0700170// The checksum is unnecessary under a few conditions, since the primary
171// use-case for it is debugging. If we are loading something from the
172// system partition (read-only), we know that it was precompiled as part of
173// application ahead of time (and thus the checksum is completely
174// unnecessary). The checksum is also unnecessary on release (non-debug)
175// builds, as the only way to get a shared object is to have compiled the
176// script once already. On a release build, there is no way to adjust the
177// other libraries/dependencies, and so the only reason to recompile would
178// be for a source APK change or an OTA. In either case, the APK would be
179// reinstalled, which would already clear the code_cache/ directory.
180bool isChecksumNeeded(const char *cacheDir) {
181 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
182 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
183 return false;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800184 char buf[PROPERTY_VALUE_MAX];
185 property_get("ro.debuggable", buf, "");
186 return (buf[0] == '1');
187}
188
189bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
190 int FD = open(fileName, O_RDONLY);
191 if (FD == -1) {
192 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
193 return false;
194 }
195
196 char buf[256];
197 while (true) {
198 ssize_t nread = read(FD, buf, sizeof(buf));
199 if (nread < 0) { // bail out on failed read
200 ALOGE("Error while computing checksum for file \'%s\'", fileName);
201 return false;
202 }
203
204 checksum = adler32(checksum, (const unsigned char *) buf, nread);
205 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
206 break;
207 }
208
Elliott Hughes2df57672015-05-15 17:06:47 -0700209 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800210 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
211 return false;
212 }
213 return true;
214}
Yang Nicb170152015-04-16 10:27:02 -0700215
216#endif // !defined(RS_COMPATIBILITY_LIB)
217} // namespace
218
219namespace android {
220namespace renderscript {
221
222#ifndef RS_COMPATIBILITY_LIB
223
224uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
225 const char *commandLine,
226 const char** bccFiles, size_t numFiles) {
227 uint32_t checksum = adler32(0L, Z_NULL, 0);
228
229 // include checksum of bitcode
230 if (bitcode != nullptr && bitcodeSize > 0) {
231 checksum = adler32(checksum, bitcode, bitcodeSize);
232 }
233
234 // include checksum of command line arguments
235 checksum = adler32(checksum, (const unsigned char *) commandLine,
236 strlen(commandLine));
237
238 // include checksum of bccFiles
239 for (size_t i = 0; i < numFiles; i++) {
240 const char* bccFile = bccFiles[i];
241 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
242 // return empty checksum instead of something partial/corrupt
243 return 0;
244 }
245 }
246
247 return checksum;
248}
249
Yang Nif02a2b02015-04-07 16:00:31 -0700250#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800251
Jason Sams709a0972012-11-15 18:18:04 -0800252RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
253 mCtx = ctx;
254 mScript = s;
255
Chris Wailes44bef6f2014-08-12 13:51:10 -0700256 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800257
Chris Wailes44bef6f2014-08-12 13:51:10 -0700258 mRoot = nullptr;
259 mRootExpand = nullptr;
260 mInit = nullptr;
261 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800262 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800263
Chris Wailes44bef6f2014-08-12 13:51:10 -0700264 mBoundAllocs = nullptr;
265 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800266 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800267
Yang Nicb170152015-04-16 10:27:02 -0700268 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800269 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800270}
271
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800272bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800273 // The shared object may have an invalid build checksum.
274 // Validate and fail early.
275 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Niade31372016-04-06 09:34:34 -0700276 mScriptSO, mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800277
278 if (mScriptExec == nullptr) {
279 return false;
280 }
281
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800282 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
283 if (mRoot) {
284 //ALOGE("Found root(): %p", mRoot);
285 }
286 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
287 if (mRootExpand) {
288 //ALOGE("Found root.expand(): %p", mRootExpand);
289 }
Matt Wala14ce0072015-07-30 17:30:25 -0700290 mInit = (InitOrDtorFunc_t) dlsym(mScriptSO, "init");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800291 if (mInit) {
292 //ALOGE("Found init(): %p", mInit);
293 }
Matt Wala14ce0072015-07-30 17:30:25 -0700294 mFreeChildren = (InitOrDtorFunc_t) dlsym(mScriptSO, ".rs.dtor");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800295 if (mFreeChildren) {
296 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
297 }
298
Yang Nid9bae682015-01-20 15:31:15 -0800299 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800300 if (varCount > 0) {
301 mBoundAllocs = new Allocation *[varCount];
302 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
303 }
304
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800305 mIsThreadable = mScriptExec->getThreadable();
306 //ALOGE("Script isThreadable? %d", mIsThreadable);
307
Stephen Hines8409d642015-04-28 18:49:56 -0700308 if (kDebugGlobalVariables) {
309 mScriptExec->dumpGlobalInfo();
310 }
311
Yang Nid9bae682015-01-20 15:31:15 -0800312 return true;
313}
314
Jason Sams709a0972012-11-15 18:18:04 -0800315bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
316 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800317 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800318 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
319 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800320 //ALOGE("rsdScriptInit %p %p", rsc, script);
321
322 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700323#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800324 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800325
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700326 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
327 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700328 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700329 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700330 return false;
331 }
332
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700333 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700334
335 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800336 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700337 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700338
verena beckhamf5029802015-05-22 16:51:42 +0100339 int optLevel = mCtx->getContext()->getOptLevel();
340
Chris Wailes6847e732014-08-11 17:30:51 -0700341 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700342 bcFileName.append("/");
343 bcFileName.append(resName);
344 bcFileName.append(".bc");
345
346 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700347 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
348 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700349 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700350 useRSDebugContext, bccPluginName, emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +0100351 optLevel, emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800352
Stephen Hines6a236ad2015-06-22 14:51:30 -0700353 mChecksumNeeded = isChecksumNeeded(cacheDir);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800354 if (mChecksumNeeded) {
355 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
356 core_lib,
357 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700358
359 // The last argument of compileArguments is a nullptr, so remove 1 from
360 // the size.
361 std::unique_ptr<const char> compileCommandLine(
362 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
363
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800364 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
365 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700366 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800367
Yang Nicb170152015-04-16 10:27:02 -0700368 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800369 // cannot compute checksum but verification is enabled
370 mCtx->unlockMutex();
371 return false;
372 }
373 }
374 else {
375 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700376 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800377 }
378
379 // Append build checksum to commandline
380 // Handle the terminal nullptr in compileArguments
381 compileArguments.pop_back();
382 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700383 std::stringstream ss;
384 ss << std::hex << mBuildChecksum;
385 compileArguments.push_back(ss.str().c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800386 compileArguments.push_back(nullptr);
387
Tim Murraybf96a522015-01-23 15:37:03 -0800388 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800389 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800390
391 // Read RS info from the shared object to detect checksum mismatch
392 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
393 dlclose(mScriptSO);
394 mScriptSO = nullptr;
395 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700396 }
397
398 // If we can't, it's either not there or out of date. We compile the bit code and try loading
399 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800400 if (mScriptSO == nullptr) {
401 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700402 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800403 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700404 ALOGE("bcc: FAILS to compile '%s'", resName);
405 mCtx->unlockMutex();
406 return false;
407 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800408
Stephen Hines4c368af2015-05-06 00:43:02 -0700409 if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
410 cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800411 ALOGE("Linker: Failed to link object file '%s'", resName);
412 mCtx->unlockMutex();
413 return false;
414 }
415
Yang Ni1c44cb62015-01-22 12:02:27 -0800416 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800417 if (mScriptSO == nullptr) {
418 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700419 mCtx->unlockMutex();
420 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700421 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800422
423 // Read RS symbol information from the .so.
424 if (!storeRSInfoFromSO()) {
425 goto error;
426 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700427 }
Jason Sams709a0972012-11-15 18:18:04 -0800428
Yang Nic31585b2015-02-15 11:43:50 -0800429 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800430
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700431#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800432 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
433 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700434
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800435 if (!mScriptSO) {
436 goto error;
437 }
Jason Sams110f1812013-03-14 16:02:18 -0700438
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800439 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700440 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700441 }
442#endif
Jason Sams709a0972012-11-15 18:18:04 -0800443 mCtx->unlockMutex();
444 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700445
Jason Sams110f1812013-03-14 16:02:18 -0700446error:
447
448 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700449 if (mScriptSO) {
450 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800451 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700452 }
453 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800454}
455
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700456#ifndef RS_COMPATIBILITY_LIB
457
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700458const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
459 size_t bitcodeSize) {
460 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
461
462 // If we're debugging, use the debug library.
463 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
464 return SYSLIBPATH"/libclcore_debug.bc";
465 }
466
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000467 if (ME.hasDebugInfo()) {
468 return SYSLIBPATH"/libclcore_g.bc";
469 }
470
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700471 // If a callback has been registered to specify a library, use that.
472 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700473 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700474 return selectRTCallback((const char*)bitcode, bitcodeSize);
475 }
476
477 // Check for a platform specific library
478#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
479 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700480 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700481 // NEON-capable ARMv7a devices can use an accelerated math library
482 // for all reduced precision scripts.
483 // ARMv8 does not use NEON, as ASIMD can be used with all precision
484 // levels.
485 return SYSLIBPATH"/libclcore_neon.bc";
486 } else {
487 return defaultLib;
488 }
489#elif defined(__i386__) || defined(__x86_64__)
490 // x86 devices will use an optimized library.
491 return SYSLIBPATH"/libclcore_x86.bc";
492#else
493 return defaultLib;
494#endif
495}
496
497#endif
498
Jason Sams709a0972012-11-15 18:18:04 -0800499void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700500 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800501 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700502 script->mHal.info.exportedReduceCount = mScriptExec->getExportedReduceCount();
David Gross6c1876b2016-01-15 11:52:14 -0800503 script->mHal.info.exportedReduceNewCount = mScriptExec->getExportedReduceNewCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700504 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800505 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800506 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800507 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
508 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700509
510 // Bug, need to stash in metadata
511 if (mRootExpand) {
512 script->mHal.info.root = mRootExpand;
513 } else {
514 script->mHal.info.root = mRoot;
515 }
Jason Sams709a0972012-11-15 18:18:04 -0800516}
517
Matt Wala14ce0072015-07-30 17:30:25 -0700518// Set up the launch dimensions, and write the values of the launch
519// dimensions into the mtls start/end fields.
520//
521// Inputs:
522// baseDim - base shape of the input
523// sc - used to constrain the launch dimensions
524//
525// Returns:
526// True on success, false on failure to set up
527bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
528 const RsLaunchDimensions &baseDim,
529 const RsScriptCall *sc) {
530 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800531
Matt Wala14ce0072015-07-30 17:30:25 -0700532#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
533 if (!sc || (sc->SC_FIELD##End == 0)) { \
534 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
535 } else { \
536 mtls->start.DIM_FIELD = \
537 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
538 mtls->end.DIM_FIELD = \
539 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
540 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
541 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
542 "Failed to launch kernel; Invalid " \
543 #SC_FIELD "Start or " #SC_FIELD "End."); \
544 return false; \
545 } \
546 }} while(0)
547
548 SET_UP_DIMENSION(x, x);
549 SET_UP_DIMENSION(y, y);
550 SET_UP_DIMENSION(z, z);
551 SET_UP_DIMENSION(array[0], array);
552 SET_UP_DIMENSION(array[1], array2);
553 SET_UP_DIMENSION(array[2], array3);
554 SET_UP_DIMENSION(array[3], array4);
555#undef SET_UP_DIMENSION
556
557 return true;
558}
559
David Gross6c1876b2016-01-15 11:52:14 -0800560// Preliminary work to prepare a simple reduce-style kernel for launch.
Matt Wala14ce0072015-07-30 17:30:25 -0700561bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation *ain,
562 const Allocation *aout,
563 const RsScriptCall *sc,
564 MTLaunchStructReduce *mtls) {
565 rsAssert(ain && aout);
566 memset(mtls, 0, sizeof(MTLaunchStructReduce));
567 mtls->dimPtr = &mtls->inputDim;
568
569 if (allocationLODIsNull(ain) || allocationLODIsNull(aout)) {
570 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
571 "reduce called with a null allocation");
572 return false;
573 }
574
575 // Set up the dimensions of the input.
576 const Type *inType = ain->getType();
577 mtls->inputDim.x = inType->getDimX();
578 rsAssert(inType->getDimY() == 0);
579
580 if (!setUpMtlsDimensions(mtls, mtls->inputDim, sc)) {
581 return false;
582 }
583
584 mtls->rs = mCtx;
585 // Currently not threaded.
586 mtls->isThreadable = false;
587 mtls->mSliceNum = -1;
588
589 // Set up input and output.
590 mtls->inBuf = static_cast<uint8_t *>(ain->getPointerUnchecked(0, 0));
591 mtls->outBuf = static_cast<uint8_t *>(aout->getPointerUnchecked(0, 0));
592
593 rsAssert(mtls->inBuf && mtls->outBuf);
594
595 return true;
596}
597
David Gross6c1876b2016-01-15 11:52:14 -0800598// Preliminary work to prepare a general reduce-style kernel for launch.
599bool RsdCpuScriptImpl::reduceNewMtlsSetup(const Allocation ** ains,
600 uint32_t inLen,
601 const Allocation * aout,
602 const RsScriptCall *sc,
603 MTLaunchStructReduceNew *mtls) {
604 rsAssert(ains && (inLen >= 1) && aout);
605 memset(mtls, 0, sizeof(MTLaunchStructReduceNew));
606 mtls->dimPtr = &mtls->redp.dim;
607
608 for (int index = inLen; --index >= 0;) {
609 if (allocationLODIsNull(ains[index])) {
610 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
611 "reduce called with null in allocations");
612 return false;
613 }
614 }
615
616 if (allocationLODIsNull(aout)) {
617 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
618 "reduce called with null out allocation");
619 return false;
620 }
621
622 const Allocation *ain0 = ains[0];
623 const Type *inType = ain0->getType();
624
625 mtls->redp.dim.x = inType->getDimX();
626 mtls->redp.dim.y = inType->getDimY();
627 mtls->redp.dim.z = inType->getDimZ();
628
629 for (int Index = inLen; --Index >= 1;) {
630 if (!ain0->hasSameDims(ains[Index])) {
631 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
632 "Failed to launch reduction kernel;"
633 "dimensions of input allocations do not match.");
634 return false;
635 }
636 }
637
638 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
639 return false;
640 }
641
642 // The X & Y walkers always want 0-1 min even if dim is not present
643 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
644 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
645
646 mtls->rs = mCtx;
647
David Gross35dbc8c2016-03-29 13:48:41 -0700648 mtls->mSliceNum = 0;
649 mtls->mSliceSize = 1;
650 mtls->isThreadable = mIsThreadable;
David Gross6c1876b2016-01-15 11:52:14 -0800651
652 // Set up output,
653 mtls->redp.outLen = 1;
654 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
655 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
656
657 // Set up input.
658 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
659 mtls->redp.inLen = inLen;
660 for (int index = inLen; --index >= 0;) {
661 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
662 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
663 }
664
665 // All validation passed, ok to launch threads
666 return true;
667}
668
Matt Wala14ce0072015-07-30 17:30:25 -0700669// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800670bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700671 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700672 Allocation * aout,
673 const void * usr, uint32_t usrLen,
674 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700675 MTLaunchStructForEach *mtls) {
Miao Wang68e00892016-02-25 12:44:10 -0800676 if (ains == nullptr && inLen != 0) {
677 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
678 "rsForEach called with none-zero inLen with null in allocations");
679 return false;
680 }
681
Matt Wala14ce0072015-07-30 17:30:25 -0700682 memset(mtls, 0, sizeof(MTLaunchStructForEach));
683 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700684
Chris Wailesf3712132014-07-16 15:18:30 -0700685 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700686 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700687 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
688 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800689 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700690 }
691 }
692
Matt Wala14ce0072015-07-30 17:30:25 -0700693 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700694 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
695 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800696 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700697 }
698
Chris Wailesf3712132014-07-16 15:18:30 -0700699 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700700 const Allocation *ain0 = ains[0];
701 const Type *inType = ain0->getType();
702
Jason Samsc0d68472015-01-20 14:29:52 -0800703 mtls->fep.dim.x = inType->getDimX();
704 mtls->fep.dim.y = inType->getDimY();
705 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700706
707 for (int Index = inLen; --Index >= 1;) {
708 if (!ain0->hasSameDims(ains[Index])) {
709 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
David Gross6c1876b2016-01-15 11:52:14 -0800710 "Failed to launch kernel; dimensions of input"
Yang Nie8f9fba2015-01-30 08:55:10 -0800711 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800712 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700713 }
714 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700715 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700716 const Type *outType = aout->getType();
717
Jason Samsc0d68472015-01-20 14:29:52 -0800718 mtls->fep.dim.x = outType->getDimX();
719 mtls->fep.dim.y = outType->getDimY();
720 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700721
Jason Samsa9139c72015-04-16 15:11:04 -0700722 } else if (sc != nullptr) {
723 mtls->fep.dim.x = sc->xEnd;
724 mtls->fep.dim.y = sc->yEnd;
725 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700726 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700727 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
728 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800729 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700730 }
731
Chris Wailes44bef6f2014-08-12 13:51:10 -0700732 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700733 if (!ains[0]->hasSameDims(aout)) {
734 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
735 "Failed to launch kernel; dimensions of input and output allocations do not match.");
736
Jason Samsbf2111d2015-01-26 18:13:41 -0800737 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700738 }
739 }
740
Matt Wala14ce0072015-07-30 17:30:25 -0700741 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
742 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700743 }
744
Jason Samsbf2111d2015-01-26 18:13:41 -0800745 // The X & Y walkers always want 0-1 min even if dim is not present
746 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
747 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700748 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800749 if (ains) {
750 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
751 }
752 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700753 mtls->fep.usr = usr;
754 mtls->fep.usrLen = usrLen;
755 mtls->mSliceSize = 1;
756 mtls->mSliceNum = 0;
757
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700758 mtls->isThreadable = mIsThreadable;
759
Chris Wailesf3712132014-07-16 15:18:30 -0700760 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700761 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700762 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800763 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
764 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700765 }
766 }
767
Chris Wailes44bef6f2014-08-12 13:51:10 -0700768 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800769 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
770 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700771 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800772
773 // All validation passed, ok to launch threads
774 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700775}
776
Jason Sams709a0972012-11-15 18:18:04 -0800777
778void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700779 const Allocation ** ains,
780 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800781 Allocation * aout,
782 const void * usr,
783 uint32_t usrLen,
784 const RsScriptCall *sc) {
785
Matt Wala14ce0072015-07-30 17:30:25 -0700786 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700787
Jason Samsbf2111d2015-01-26 18:13:41 -0800788 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
789 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700790
Jason Samsbf2111d2015-01-26 18:13:41 -0800791 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700792 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800793 mCtx->setTLS(oldTLS);
794 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700795}
796
Matt Wala14ce0072015-07-30 17:30:25 -0700797void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
798 const Allocation *ain,
799 Allocation *aout,
800 const RsScriptCall *sc) {
801 MTLaunchStructReduce mtls;
802
803 if (reduceMtlsSetup(ain, aout, sc, &mtls)) {
804 reduceKernelSetup(slot, &mtls);
805 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
806 mCtx->launchReduce(ain, aout, &mtls);
807 mCtx->setTLS(oldTLS);
808 }
809}
810
David Gross6c1876b2016-01-15 11:52:14 -0800811void RsdCpuScriptImpl::invokeReduceNew(uint32_t slot,
812 const Allocation ** ains, uint32_t inLen,
813 Allocation *aout,
814 const RsScriptCall *sc) {
815 MTLaunchStructReduceNew mtls;
816
817 if (reduceNewMtlsSetup(ains, inLen, aout, sc, &mtls)) {
818 reduceNewKernelSetup(slot, &mtls);
819 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
820 mCtx->launchReduceNew(ains, inLen, aout, &mtls);
821 mCtx->setTLS(oldTLS);
822 }
823}
824
Matt Wala14ce0072015-07-30 17:30:25 -0700825void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800826 mtls->script = this;
827 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800828 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700829 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800830}
831
Matt Wala14ce0072015-07-30 17:30:25 -0700832void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
833 mtls->script = this;
834 mtls->kernel = mScriptExec->getReduceFunction(slot);
835 rsAssert(mtls->kernel != nullptr);
836}
837
David Gross6c1876b2016-01-15 11:52:14 -0800838void RsdCpuScriptImpl::reduceNewKernelSetup(uint32_t slot, MTLaunchStructReduceNew *mtls) {
839 mtls->script = this;
840 mtls->redp.slot = slot;
841
842 const ReduceNewDescription *desc = mScriptExec->getReduceNewDescription(slot);
843 mtls->accumFunc = desc->accumFunc;
844 mtls->initFunc = desc->initFunc; // might legally be nullptr
David Gross35dbc8c2016-03-29 13:48:41 -0700845 mtls->combFunc = desc->combFunc; // might legally be nullptr
David Gross6c1876b2016-01-15 11:52:14 -0800846 mtls->outFunc = desc->outFunc; // might legally be nullptr
847 mtls->accumSize = desc->accumSize;
848
849 rsAssert(mtls->accumFunc != nullptr);
850}
851
Jason Sams709a0972012-11-15 18:18:04 -0800852int RsdCpuScriptImpl::invokeRoot() {
853 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
854 int ret = mRoot();
855 mCtx->setTLS(oldTLS);
856 return ret;
857}
858
859void RsdCpuScriptImpl::invokeInit() {
860 if (mInit) {
861 mInit();
862 }
863}
864
865void RsdCpuScriptImpl::invokeFreeChildren() {
866 if (mFreeChildren) {
867 mFreeChildren();
868 }
869}
870
871void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
872 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800873 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800874 void * ap = nullptr;
875
876#if defined(__x86_64__)
877 // The invoked function could have input parameter of vector type for example float4 which
878 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
879 // So try to align void* params before passing them into RS exported function.
880
881 if ((uint8_t)(uint64_t)params & 0x0F) {
882 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
883 memcpy(ap, params, paramLength);
884 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800885 ALOGE("x86_64: invokeFunction memalign error, still use params which"
886 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800887 }
888 }
889#endif
Jason Sams709a0972012-11-15 18:18:04 -0800890
891 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800892 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800893 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800894
Yong Chenc246d912015-05-22 18:20:06 +0800895#if defined(__x86_64__)
896 free(ap);
897#endif
898
Jason Sams709a0972012-11-15 18:18:04 -0800899 mCtx->setTLS(oldTLS);
900}
901
902void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
903 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800904 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800905
906 //if (mIntrinsicID) {
907 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
908 //return;
909 //}
910
Yang Nid9bae682015-01-20 15:31:15 -0800911 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800912 if (!destPtr) {
913 //ALOGV("Calling setVar on slot = %i which is null", slot);
914 return;
915 }
916
917 memcpy(destPtr, data, dataLength);
918}
919
Tim Murray9c642392013-04-11 13:29:59 -0700920void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
921 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800922 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700923
Yang Nid9bae682015-01-20 15:31:15 -0800924 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700925 if (!srcPtr) {
926 //ALOGV("Calling setVar on slot = %i which is null", slot);
927 return;
928 }
929 memcpy(data, srcPtr, dataLength);
930}
931
932
Jason Sams709a0972012-11-15 18:18:04 -0800933void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
934 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700935 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800936 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800937 if (!destPtr) {
938 //ALOGV("Calling setVar on slot = %i which is null", slot);
939 return;
940 }
941
942 // We want to look at dimension in terms of integer components,
943 // but dimLength is given in terms of bytes.
944 dimLength /= sizeof(int);
945
946 // Only a single dimension is currently supported.
947 rsAssert(dimLength == 1);
948 if (dimLength == 1) {
949 // First do the increment loop.
950 size_t stride = elem->getSizeBytes();
951 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700952 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800953 elem->incRefs(cVal);
954 cVal += stride;
955 }
956
957 // Decrement loop comes after (to prevent race conditions).
958 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700959 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800960 elem->decRefs(oldVal);
961 oldVal += stride;
962 }
963 }
964
965 memcpy(destPtr, data, dataLength);
966}
967
968void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
969
970 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800971 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800972
Yang Nid9bae682015-01-20 15:31:15 -0800973 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800974 if (!destPtr) {
975 //ALOGV("Calling setVar on slot = %i which is null", slot);
976 return;
977 }
978
Chris Wailes44bef6f2014-08-12 13:51:10 -0700979 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800980 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800981 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800982 ptr = data->mHal.drvState.lod[0].mallocPtr;
983 }
984 memcpy(destPtr, &ptr, sizeof(void *));
985}
986
987void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
988
989 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800990 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800991
Yang Nid9bae682015-01-20 15:31:15 -0800992 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800993 if (!destPtr) {
994 //ALOGV("Calling setVar on slot = %i which is null", slot);
995 return;
996 }
997
Jason Sams05ef73f2014-08-05 14:59:22 -0700998 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800999}
1000
Yang Ni062c2872015-02-20 15:20:00 -08001001const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
1002 return mScriptExec->getFieldName(slot);
1003}
1004
Jason Sams709a0972012-11-15 18:18:04 -08001005RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -07001006 delete mScriptExec;
1007 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -07001008 if (mScriptSO) {
1009 dlclose(mScriptSO);
1010 }
Jason Sams709a0972012-11-15 18:18:04 -08001011}
1012
1013Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1014 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001015 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001016 }
1017
1018 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1019 Allocation *a = mBoundAllocs[ct];
1020 if (!a) continue;
1021 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1022 return a;
1023 }
1024 }
1025 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001026 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001027}
1028
Stephen Hines8409d642015-04-28 18:49:56 -07001029int RsdCpuScriptImpl::getGlobalEntries() const {
1030 return mScriptExec->getGlobalEntries();
1031}
1032
1033const char * RsdCpuScriptImpl::getGlobalName(int i) const {
1034 return mScriptExec->getGlobalName(i);
1035}
1036
1037const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
1038 return mScriptExec->getGlobalAddress(i);
1039}
1040
1041size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
1042 return mScriptExec->getGlobalSize(i);
1043}
1044
Stephen Hines5aa018c2015-05-20 18:09:57 -07001045uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
1046 return mScriptExec->getGlobalProperties(i);
1047}
1048
Chris Wailesf3712132014-07-16 15:18:30 -07001049void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1050 uint32_t inLen, Allocation * aout,
1051 const void * usr, uint32_t usrLen,
1052 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001053
Chris Wailesf3712132014-07-16 15:18:30 -07001054void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1055 uint32_t inLen, Allocation * aout,
1056 const void * usr, uint32_t usrLen,
1057 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001058
Jason Sams709a0972012-11-15 18:18:04 -08001059
1060}
1061}