blob: b04e1d28d577b730ac154767f94c9515f23c5a52 [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 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() {
Stephen Hinesba17ae42013-06-05 17:18:04 -070062 char buf[PROPERTY_VALUE_MAX];
63
64 // Re-compile if floating point precision has been overridden.
65 property_get("debug.rs.precision", buf, "");
66 if (buf[0] != '\0') {
67 return true;
68 }
69
70 // Re-compile if debug.rs.forcerecompile is set.
71 property_get("debug.rs.forcerecompile", buf, "0");
72 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
73 return true;
74 } else {
75 return false;
76 }
Stephen Hinesba17ae42013-06-05 17:18:04 -070077}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070078
Chris Wailes6847e732014-08-11 17:30:51 -070079static void setCompileArguments(std::vector<const char*>* args,
80 const std::string& bcFileName,
81 const char* cacheDir, const char* resName,
82 const char* core_lib, bool useRSDebugContext,
Stephen Hines8409d642015-04-28 18:49:56 -070083 const char* bccPluginName, bool emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +010084 int optLevel, bool emitGlobalInfoSkipConstant) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070085 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080086 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080087 args->push_back("-unroll-runtime");
88 args->push_back("-scalarize-load-store");
Stephen Hines8409d642015-04-28 18:49:56 -070089 if (emitGlobalInfo) {
90 args->push_back("-rs-global-info");
91 if (emitGlobalInfoSkipConstant) {
92 args->push_back("-rs-global-info-skip-constant");
93 }
94 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070095 args->push_back("-o");
96 args->push_back(resName);
97 args->push_back("-output_path");
98 args->push_back(cacheDir);
99 args->push_back("-bclib");
100 args->push_back(core_lib);
101 args->push_back("-mtriple");
102 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
verena beckhamf5029802015-05-22 16:51:42 +0100103 args->push_back("-O");
104
105 switch (optLevel) {
Yang Ni7a106ad2016-03-10 16:06:36 -0800106 case 0:
verena beckhamf5029802015-05-22 16:51:42 +0100107 args->push_back("0");
108 break;
Yang Ni7a106ad2016-03-10 16:06:36 -0800109 case 3:
verena beckhamf5029802015-05-22 16:51:42 +0100110 args->push_back("3");
111 break;
112 default:
113 ALOGW("Expected optimization level of 0 or 3. Received %d", optLevel);
114 args->push_back("3");
115 break;
116 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700117
Tim Murray358ffb82014-12-09 11:53:06 -0800118 // Enable workaround for A53 codegen by default.
119#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
120 args->push_back("-aarch64-fix-cortex-a53-835769");
121#endif
122
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700123 // Execute the bcc compiler.
124 if (useRSDebugContext) {
125 args->push_back("-rs-debug-ctx");
126 } else {
127 // Only load additional libraries for compiles that don't use
128 // the debug context.
129 if (bccPluginName && strlen(bccPluginName) > 0) {
130 args->push_back("-load");
131 args->push_back(bccPluginName);
132 }
133 }
134
Stephen Hines45e753a2015-01-19 20:58:44 -0800135 args->push_back("-fPIC");
136 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800137
Chris Wailes6847e732014-08-11 17:30:51 -0700138 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700139 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700140}
141
Chris Wailes6847e732014-08-11 17:30:51 -0700142static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700143 const char *bitcode,
144 size_t bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700145 std::vector<const char *> &compileArguments) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700146 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700147
Chris Wailes6847e732014-08-11 17:30:51 -0700148 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700149 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700150 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700151 return false;
152 }
153 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
154 fclose(bcfile);
155 if (nwritten != bitcodeSize) {
156 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700157 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700158 return false;
159 }
160
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700161 return android::renderscript::rsuExecuteCommand(
162 android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
163 compileArguments.size()-1, compileArguments.data());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700164}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700165
Stephen Hines6a236ad2015-06-22 14:51:30 -0700166// The checksum is unnecessary under a few conditions, since the primary
167// use-case for it is debugging. If we are loading something from the
168// system partition (read-only), we know that it was precompiled as part of
169// application ahead of time (and thus the checksum is completely
170// unnecessary). The checksum is also unnecessary on release (non-debug)
171// builds, as the only way to get a shared object is to have compiled the
172// script once already. On a release build, there is no way to adjust the
173// other libraries/dependencies, and so the only reason to recompile would
174// be for a source APK change or an OTA. In either case, the APK would be
175// reinstalled, which would already clear the code_cache/ directory.
176bool isChecksumNeeded(const char *cacheDir) {
177 if ((::strcmp(SYSLIBPATH, cacheDir) == 0) ||
178 (::strcmp(SYSLIBPATH_VENDOR, cacheDir) == 0))
179 return false;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800180 char buf[PROPERTY_VALUE_MAX];
181 property_get("ro.debuggable", buf, "");
182 return (buf[0] == '1');
183}
184
185bool addFileToChecksum(const char *fileName, uint32_t &checksum) {
186 int FD = open(fileName, O_RDONLY);
187 if (FD == -1) {
188 ALOGE("Cannot open file \'%s\' to compute checksum", fileName);
189 return false;
190 }
191
192 char buf[256];
193 while (true) {
194 ssize_t nread = read(FD, buf, sizeof(buf));
195 if (nread < 0) { // bail out on failed read
196 ALOGE("Error while computing checksum for file \'%s\'", fileName);
197 return false;
198 }
199
200 checksum = adler32(checksum, (const unsigned char *) buf, nread);
201 if (static_cast<size_t>(nread) < sizeof(buf)) // EOF
202 break;
203 }
204
Elliott Hughes2df57672015-05-15 17:06:47 -0700205 if (close(FD) != 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800206 ALOGE("Cannot close file \'%s\' after computing checksum", fileName);
207 return false;
208 }
209 return true;
210}
Yang Nicb170152015-04-16 10:27:02 -0700211
212#endif // !defined(RS_COMPATIBILITY_LIB)
213} // namespace
214
215namespace android {
216namespace renderscript {
217
218#ifndef RS_COMPATIBILITY_LIB
219
220uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize,
221 const char *commandLine,
222 const char** bccFiles, size_t numFiles) {
223 uint32_t checksum = adler32(0L, Z_NULL, 0);
224
225 // include checksum of bitcode
226 if (bitcode != nullptr && bitcodeSize > 0) {
227 checksum = adler32(checksum, bitcode, bitcodeSize);
228 }
229
230 // include checksum of command line arguments
231 checksum = adler32(checksum, (const unsigned char *) commandLine,
232 strlen(commandLine));
233
234 // include checksum of bccFiles
235 for (size_t i = 0; i < numFiles; i++) {
236 const char* bccFile = bccFiles[i];
237 if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) {
238 // return empty checksum instead of something partial/corrupt
239 return 0;
240 }
241 }
242
243 return checksum;
244}
245
Yang Nif02a2b02015-04-07 16:00:31 -0700246#endif // !RS_COMPATIBILITY_LIB
Yang Ni1c44cb62015-01-22 12:02:27 -0800247
Jason Sams709a0972012-11-15 18:18:04 -0800248RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
249 mCtx = ctx;
250 mScript = s;
251
Chris Wailes44bef6f2014-08-12 13:51:10 -0700252 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800253
Chris Wailes44bef6f2014-08-12 13:51:10 -0700254 mRoot = nullptr;
255 mRootExpand = nullptr;
256 mInit = nullptr;
257 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800258 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800259
Chris Wailes44bef6f2014-08-12 13:51:10 -0700260 mBoundAllocs = nullptr;
261 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800262 mIsThreadable = true;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800263
Yang Nicb170152015-04-16 10:27:02 -0700264 mBuildChecksum = 0;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800265 mChecksumNeeded = false;
Jason Sams709a0972012-11-15 18:18:04 -0800266}
267
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800268bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800269 // The shared object may have an invalid build checksum.
270 // Validate and fail early.
271 mScriptExec = ScriptExecutable::createFromSharedObject(
Yang Niade31372016-04-06 09:34:34 -0700272 mScriptSO, mChecksumNeeded ? mBuildChecksum : 0);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800273
274 if (mScriptExec == nullptr) {
275 return false;
276 }
277
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800278 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
279 if (mRoot) {
280 //ALOGE("Found root(): %p", mRoot);
281 }
282 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
283 if (mRootExpand) {
284 //ALOGE("Found root.expand(): %p", mRootExpand);
285 }
Matt Wala14ce0072015-07-30 17:30:25 -0700286 mInit = (InitOrDtorFunc_t) dlsym(mScriptSO, "init");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800287 if (mInit) {
288 //ALOGE("Found init(): %p", mInit);
289 }
Matt Wala14ce0072015-07-30 17:30:25 -0700290 mFreeChildren = (InitOrDtorFunc_t) dlsym(mScriptSO, ".rs.dtor");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800291 if (mFreeChildren) {
292 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
293 }
294
Yang Nid9bae682015-01-20 15:31:15 -0800295 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800296 if (varCount > 0) {
297 mBoundAllocs = new Allocation *[varCount];
298 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
299 }
300
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800301 mIsThreadable = mScriptExec->getThreadable();
302 //ALOGE("Script isThreadable? %d", mIsThreadable);
303
Stephen Hines8409d642015-04-28 18:49:56 -0700304 if (kDebugGlobalVariables) {
305 mScriptExec->dumpGlobalInfo();
306 }
307
Yang Nid9bae682015-01-20 15:31:15 -0800308 return true;
309}
310
Jason Sams709a0972012-11-15 18:18:04 -0800311bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
312 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800313 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800314 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
315 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800316 //ALOGE("rsdScriptInit %p %p", rsc, script);
317
318 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700319#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800320 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800321
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700322 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
323 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700324 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700325 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700326 return false;
327 }
328
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700329 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700330
331 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hines00511322014-01-31 11:20:23 -0800332 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700333 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700334
verena beckhamf5029802015-05-22 16:51:42 +0100335 int optLevel = mCtx->getContext()->getOptLevel();
336
Chris Wailes6847e732014-08-11 17:30:51 -0700337 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700338 bcFileName.append("/");
339 bcFileName.append(resName);
340 bcFileName.append(".bc");
341
342 std::vector<const char*> compileArguments;
Stephen Hines8409d642015-04-28 18:49:56 -0700343 bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
344 bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700345 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
Stephen Hines8409d642015-04-28 18:49:56 -0700346 useRSDebugContext, bccPluginName, emitGlobalInfo,
verena beckhamf5029802015-05-22 16:51:42 +0100347 optLevel, emitGlobalInfoSkipConstant);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800348
Stephen Hines6a236ad2015-06-22 14:51:30 -0700349 mChecksumNeeded = isChecksumNeeded(cacheDir);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800350 if (mChecksumNeeded) {
351 std::vector<const char *> bccFiles = { BCC_EXE_PATH,
352 core_lib,
353 };
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700354
355 // The last argument of compileArguments is a nullptr, so remove 1 from
356 // the size.
357 std::unique_ptr<const char> compileCommandLine(
358 rsuJoinStrings(compileArguments.size()-1, compileArguments.data()));
359
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800360 mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize,
361 compileCommandLine.get(),
Yang Nicb170152015-04-16 10:27:02 -0700362 bccFiles.data(), bccFiles.size());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800363
Yang Nicb170152015-04-16 10:27:02 -0700364 if (mBuildChecksum == 0) {
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800365 // cannot compute checksum but verification is enabled
366 mCtx->unlockMutex();
367 return false;
368 }
369 }
370 else {
371 // add a dummy/constant as a checksum if verification is disabled
Yang Nicb170152015-04-16 10:27:02 -0700372 mBuildChecksum = 0xabadcafe;
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800373 }
374
375 // Append build checksum to commandline
376 // Handle the terminal nullptr in compileArguments
377 compileArguments.pop_back();
378 compileArguments.push_back("-build-checksum");
Yang Nicb170152015-04-16 10:27:02 -0700379 std::stringstream ss;
380 ss << std::hex << mBuildChecksum;
Yang Ni578419f2016-06-27 16:12:25 -0700381 std::string checksumStr(ss.str());
382 compileArguments.push_back(checksumStr.c_str());
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800383 compileArguments.push_back(nullptr);
384
Tim Murraybf96a522015-01-23 15:37:03 -0800385 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800386 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800387
388 // Read RS info from the shared object to detect checksum mismatch
389 if (mScriptSO != nullptr && !storeRSInfoFromSO()) {
390 dlclose(mScriptSO);
391 mScriptSO = nullptr;
392 }
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700393 }
394
395 // If we can't, it's either not there or out of date. We compile the bit code and try loading
396 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800397 if (mScriptSO == nullptr) {
398 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700399 compileArguments))
Stephen Hines45e753a2015-01-19 20:58:44 -0800400 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700401 ALOGE("bcc: FAILS to compile '%s'", resName);
402 mCtx->unlockMutex();
403 return false;
404 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800405
Stephen Hines4c368af2015-05-06 00:43:02 -0700406 if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
407 cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800408 ALOGE("Linker: Failed to link object file '%s'", resName);
409 mCtx->unlockMutex();
410 return false;
411 }
412
Yang Ni1c44cb62015-01-22 12:02:27 -0800413 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800414 if (mScriptSO == nullptr) {
415 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700416 mCtx->unlockMutex();
417 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700418 }
Pirama Arumuga Nainaraa6757f2015-02-13 20:02:50 -0800419
420 // Read RS symbol information from the .so.
421 if (!storeRSInfoFromSO()) {
422 goto error;
423 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700424 }
Jason Sams709a0972012-11-15 18:18:04 -0800425
Yang Nic31585b2015-02-15 11:43:50 -0800426 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800427
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700428#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800429 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
430 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700431
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800432 if (!mScriptSO) {
433 goto error;
434 }
Jason Sams110f1812013-03-14 16:02:18 -0700435
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800436 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700437 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700438 }
439#endif
Jason Sams709a0972012-11-15 18:18:04 -0800440 mCtx->unlockMutex();
441 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700442
Jason Sams110f1812013-03-14 16:02:18 -0700443error:
444
445 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700446 if (mScriptSO) {
447 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800448 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700449 }
450 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800451}
452
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700453#ifndef RS_COMPATIBILITY_LIB
454
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700455const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
456 size_t bitcodeSize) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200457 const char* defaultLib = SYSLIBPATH_BC"/libclcore.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700458
459 // If we're debugging, use the debug library.
460 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200461 return SYSLIBPATH_BC"/libclcore_debug.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700462 }
463
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000464 if (ME.hasDebugInfo()) {
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200465 return SYSLIBPATH_BC"/libclcore_g.bc";
Verena Beckhamfb99e0f2015-11-18 12:31:43 +0000466 }
467
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700468 // If a callback has been registered to specify a library, use that.
469 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700470 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700471 return selectRTCallback((const char*)bitcode, bitcodeSize);
472 }
473
474 // Check for a platform specific library
475#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
476 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700477 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700478 // NEON-capable ARMv7a devices can use an accelerated math library
479 // for all reduced precision scripts.
480 // ARMv8 does not use NEON, as ASIMD can be used with all precision
481 // levels.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200482 return SYSLIBPATH_BC"/libclcore_neon.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700483 } else {
484 return defaultLib;
485 }
486#elif defined(__i386__) || defined(__x86_64__)
487 // x86 devices will use an optimized library.
Victor Khimenkocfb1d0b2016-10-28 17:05:22 +0200488 return SYSLIBPATH_BC"/libclcore_x86.bc";
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700489#else
490 return defaultLib;
491#endif
492}
493
494#endif
495
Jason Sams709a0972012-11-15 18:18:04 -0800496void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700497 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800498 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
Matt Wala14ce0072015-07-30 17:30:25 -0700499 script->mHal.info.exportedReduceCount = mScriptExec->getExportedReduceCount();
500 script->mHal.info.exportedForEachCount = mScriptExec->getExportedForEachCount();
Yang Nid9bae682015-01-20 15:31:15 -0800501 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800502 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800503 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
504 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700505
506 // Bug, need to stash in metadata
507 if (mRootExpand) {
508 script->mHal.info.root = mRootExpand;
509 } else {
510 script->mHal.info.root = mRoot;
511 }
Jason Sams709a0972012-11-15 18:18:04 -0800512}
513
Matt Wala14ce0072015-07-30 17:30:25 -0700514// Set up the launch dimensions, and write the values of the launch
515// dimensions into the mtls start/end fields.
516//
517// Inputs:
518// baseDim - base shape of the input
519// sc - used to constrain the launch dimensions
520//
521// Returns:
522// True on success, false on failure to set up
523bool RsdCpuScriptImpl::setUpMtlsDimensions(MTLaunchStructCommon *mtls,
524 const RsLaunchDimensions &baseDim,
525 const RsScriptCall *sc) {
526 rsAssert(mtls);
Jason Sams709a0972012-11-15 18:18:04 -0800527
Matt Wala14ce0072015-07-30 17:30:25 -0700528#define SET_UP_DIMENSION(DIM_FIELD, SC_FIELD) do { \
529 if (!sc || (sc->SC_FIELD##End == 0)) { \
530 mtls->end.DIM_FIELD = baseDim.DIM_FIELD; \
531 } else { \
532 mtls->start.DIM_FIELD = \
533 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##Start); \
534 mtls->end.DIM_FIELD = \
535 rsMin(baseDim.DIM_FIELD, sc->SC_FIELD##End); \
536 if (mtls->start.DIM_FIELD >= mtls->end.DIM_FIELD) { \
537 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, \
538 "Failed to launch kernel; Invalid " \
539 #SC_FIELD "Start or " #SC_FIELD "End."); \
540 return false; \
541 } \
542 }} while(0)
543
544 SET_UP_DIMENSION(x, x);
545 SET_UP_DIMENSION(y, y);
546 SET_UP_DIMENSION(z, z);
Yang Ni7784b5d2016-05-04 09:44:48 -0700547 // Checks and setup of fields other than x, y, z are ignored, since those
548 // fields are not used in the runtime and are not visible in the Java API.
Matt Wala14ce0072015-07-30 17:30:25 -0700549#undef SET_UP_DIMENSION
550
551 return true;
552}
553
David Grossfc7ab792016-06-01 14:45:47 -0700554// Preliminary work to prepare a general reduce-style kernel for launch.
555bool RsdCpuScriptImpl::reduceMtlsSetup(const Allocation ** ains,
556 uint32_t inLen,
557 const Allocation * aout,
Matt Wala14ce0072015-07-30 17:30:25 -0700558 const RsScriptCall *sc,
559 MTLaunchStructReduce *mtls) {
David Gross6c1876b2016-01-15 11:52:14 -0800560 rsAssert(ains && (inLen >= 1) && aout);
David Grossfc7ab792016-06-01 14:45:47 -0700561 memset(mtls, 0, sizeof(MTLaunchStructReduce));
David Gross6c1876b2016-01-15 11:52:14 -0800562 mtls->dimPtr = &mtls->redp.dim;
563
564 for (int index = inLen; --index >= 0;) {
565 if (allocationLODIsNull(ains[index])) {
566 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
567 "reduce called with null in allocations");
568 return false;
569 }
570 }
571
572 if (allocationLODIsNull(aout)) {
573 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
574 "reduce called with null out allocation");
575 return false;
576 }
577
578 const Allocation *ain0 = ains[0];
579 const Type *inType = ain0->getType();
580
581 mtls->redp.dim.x = inType->getDimX();
582 mtls->redp.dim.y = inType->getDimY();
583 mtls->redp.dim.z = inType->getDimZ();
584
585 for (int Index = inLen; --Index >= 1;) {
586 if (!ain0->hasSameDims(ains[Index])) {
587 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
588 "Failed to launch reduction kernel;"
589 "dimensions of input allocations do not match.");
590 return false;
591 }
592 }
593
594 if (!setUpMtlsDimensions(mtls, mtls->redp.dim, sc)) {
595 return false;
596 }
597
598 // The X & Y walkers always want 0-1 min even if dim is not present
599 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
600 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
601
602 mtls->rs = mCtx;
603
David Gross35dbc8c2016-03-29 13:48:41 -0700604 mtls->mSliceNum = 0;
605 mtls->mSliceSize = 1;
606 mtls->isThreadable = mIsThreadable;
David Gross6c1876b2016-01-15 11:52:14 -0800607
608 // Set up output,
609 mtls->redp.outLen = 1;
610 mtls->redp.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
611 mtls->redp.outStride[0] = aout->getType()->getElementSizeBytes();
612
613 // Set up input.
614 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
615 mtls->redp.inLen = inLen;
616 for (int index = inLen; --index >= 0;) {
617 mtls->redp.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
618 mtls->redp.inStride[index] = ains[index]->getType()->getElementSizeBytes();
619 }
620
621 // All validation passed, ok to launch threads
622 return true;
623}
624
Matt Wala14ce0072015-07-30 17:30:25 -0700625// Preliminary work to prepare a forEach-style kernel for launch.
Jason Samsbf2111d2015-01-26 18:13:41 -0800626bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700627 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700628 Allocation * aout,
629 const void * usr, uint32_t usrLen,
630 const RsScriptCall *sc,
Matt Wala14ce0072015-07-30 17:30:25 -0700631 MTLaunchStructForEach *mtls) {
Miao Wang68e00892016-02-25 12:44:10 -0800632 if (ains == nullptr && inLen != 0) {
633 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
634 "rsForEach called with none-zero inLen with null in allocations");
635 return false;
636 }
637
Matt Wala14ce0072015-07-30 17:30:25 -0700638 memset(mtls, 0, sizeof(MTLaunchStructForEach));
639 mtls->dimPtr = &mtls->fep.dim;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700640
Chris Wailesf3712132014-07-16 15:18:30 -0700641 for (int index = inLen; --index >= 0;) {
Matt Wala14ce0072015-07-30 17:30:25 -0700642 if (allocationLODIsNull(ains[index])) {
Chris Wailesf3712132014-07-16 15:18:30 -0700643 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
644 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800645 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700646 }
647 }
648
Matt Wala14ce0072015-07-30 17:30:25 -0700649 if (allocationLODIsNull(aout)) {
Chris Wailesf3712132014-07-16 15:18:30 -0700650 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
651 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800652 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700653 }
654
Chris Wailesf3712132014-07-16 15:18:30 -0700655 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700656 const Allocation *ain0 = ains[0];
657 const Type *inType = ain0->getType();
658
Jason Samsc0d68472015-01-20 14:29:52 -0800659 mtls->fep.dim.x = inType->getDimX();
660 mtls->fep.dim.y = inType->getDimY();
661 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700662
663 for (int Index = inLen; --Index >= 1;) {
664 if (!ain0->hasSameDims(ains[Index])) {
665 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
David Gross6c1876b2016-01-15 11:52:14 -0800666 "Failed to launch kernel; dimensions of input"
Yang Nie8f9fba2015-01-30 08:55:10 -0800667 "allocations do not match.");
Jason Samsbf2111d2015-01-26 18:13:41 -0800668 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700669 }
670 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700671 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700672 const Type *outType = aout->getType();
673
Jason Samsc0d68472015-01-20 14:29:52 -0800674 mtls->fep.dim.x = outType->getDimX();
675 mtls->fep.dim.y = outType->getDimY();
676 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700677
Jason Samsa9139c72015-04-16 15:11:04 -0700678 } else if (sc != nullptr) {
679 mtls->fep.dim.x = sc->xEnd;
680 mtls->fep.dim.y = sc->yEnd;
681 mtls->fep.dim.z = 0;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700682 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700683 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
684 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800685 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700686 }
687
Chris Wailes44bef6f2014-08-12 13:51:10 -0700688 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700689 if (!ains[0]->hasSameDims(aout)) {
690 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
691 "Failed to launch kernel; dimensions of input and output allocations do not match.");
692
Jason Samsbf2111d2015-01-26 18:13:41 -0800693 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700694 }
695 }
696
Matt Wala14ce0072015-07-30 17:30:25 -0700697 if (!setUpMtlsDimensions(mtls, mtls->fep.dim, sc)) {
698 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700699 }
700
Jason Samsbf2111d2015-01-26 18:13:41 -0800701 // The X & Y walkers always want 0-1 min even if dim is not present
702 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
703 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Matt Wala14ce0072015-07-30 17:30:25 -0700704 mtls->rs = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800705 if (ains) {
706 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
707 }
708 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700709 mtls->fep.usr = usr;
710 mtls->fep.usrLen = usrLen;
711 mtls->mSliceSize = 1;
712 mtls->mSliceNum = 0;
713
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700714 mtls->isThreadable = mIsThreadable;
715
Chris Wailesf3712132014-07-16 15:18:30 -0700716 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700717 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700718 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800719 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
720 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700721 }
722 }
723
Chris Wailes44bef6f2014-08-12 13:51:10 -0700724 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800725 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
726 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700727 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800728
729 // All validation passed, ok to launch threads
730 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700731}
732
Jason Sams709a0972012-11-15 18:18:04 -0800733
734void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700735 const Allocation ** ains,
736 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800737 Allocation * aout,
738 const void * usr,
739 uint32_t usrLen,
740 const RsScriptCall *sc) {
741
Matt Wala14ce0072015-07-30 17:30:25 -0700742 MTLaunchStructForEach mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700743
Jason Samsbf2111d2015-01-26 18:13:41 -0800744 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
745 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700746
Jason Samsbf2111d2015-01-26 18:13:41 -0800747 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Matt Wala14ce0072015-07-30 17:30:25 -0700748 mCtx->launchForEach(ains, inLen, aout, sc, &mtls);
Jason Samsbf2111d2015-01-26 18:13:41 -0800749 mCtx->setTLS(oldTLS);
750 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700751}
752
Matt Wala14ce0072015-07-30 17:30:25 -0700753void RsdCpuScriptImpl::invokeReduce(uint32_t slot,
David Grossfc7ab792016-06-01 14:45:47 -0700754 const Allocation ** ains, uint32_t inLen,
Matt Wala14ce0072015-07-30 17:30:25 -0700755 Allocation *aout,
756 const RsScriptCall *sc) {
David Grossfc7ab792016-06-01 14:45:47 -0700757 MTLaunchStructReduce mtls;
Matt Wala14ce0072015-07-30 17:30:25 -0700758
David Grossfc7ab792016-06-01 14:45:47 -0700759 if (reduceMtlsSetup(ains, inLen, aout, sc, &mtls)) {
760 reduceKernelSetup(slot, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800761 RsdCpuScriptImpl *oldTLS = mCtx->setTLS(this);
David Grossfc7ab792016-06-01 14:45:47 -0700762 mCtx->launchReduce(ains, inLen, aout, &mtls);
David Gross6c1876b2016-01-15 11:52:14 -0800763 mCtx->setTLS(oldTLS);
764 }
765}
766
Matt Wala14ce0072015-07-30 17:30:25 -0700767void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStructForEach *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800768 mtls->script = this;
769 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800770 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700771 rsAssert(mtls->kernel != nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800772}
773
Matt Wala14ce0072015-07-30 17:30:25 -0700774void RsdCpuScriptImpl::reduceKernelSetup(uint32_t slot, MTLaunchStructReduce *mtls) {
775 mtls->script = this;
David Gross6c1876b2016-01-15 11:52:14 -0800776 mtls->redp.slot = slot;
777
David Grossfc7ab792016-06-01 14:45:47 -0700778 const ReduceDescription *desc = mScriptExec->getReduceDescription(slot);
David Gross6c1876b2016-01-15 11:52:14 -0800779 mtls->accumFunc = desc->accumFunc;
780 mtls->initFunc = desc->initFunc; // might legally be nullptr
David Gross35dbc8c2016-03-29 13:48:41 -0700781 mtls->combFunc = desc->combFunc; // might legally be nullptr
David Gross6c1876b2016-01-15 11:52:14 -0800782 mtls->outFunc = desc->outFunc; // might legally be nullptr
783 mtls->accumSize = desc->accumSize;
784
785 rsAssert(mtls->accumFunc != nullptr);
786}
787
Jason Sams709a0972012-11-15 18:18:04 -0800788int RsdCpuScriptImpl::invokeRoot() {
789 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
790 int ret = mRoot();
791 mCtx->setTLS(oldTLS);
792 return ret;
793}
794
795void RsdCpuScriptImpl::invokeInit() {
796 if (mInit) {
797 mInit();
798 }
799}
800
801void RsdCpuScriptImpl::invokeFreeChildren() {
802 if (mFreeChildren) {
803 mFreeChildren();
804 }
805}
806
807void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
808 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800809 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800810 void * ap = nullptr;
811
812#if defined(__x86_64__)
813 // The invoked function could have input parameter of vector type for example float4 which
814 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
815 // So try to align void* params before passing them into RS exported function.
816
817 if ((uint8_t)(uint64_t)params & 0x0F) {
818 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
819 memcpy(ap, params, paramLength);
820 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800821 ALOGE("x86_64: invokeFunction memalign error, still use params which"
822 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800823 }
824 }
825#endif
Jason Sams709a0972012-11-15 18:18:04 -0800826
827 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800828 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800829 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800830
Yong Chenc246d912015-05-22 18:20:06 +0800831#if defined(__x86_64__)
832 free(ap);
833#endif
834
Jason Sams709a0972012-11-15 18:18:04 -0800835 mCtx->setTLS(oldTLS);
836}
837
838void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
839 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800840 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800841
842 //if (mIntrinsicID) {
843 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
844 //return;
845 //}
846
Yang Nid9bae682015-01-20 15:31:15 -0800847 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800848 if (!destPtr) {
849 //ALOGV("Calling setVar on slot = %i which is null", slot);
850 return;
851 }
852
853 memcpy(destPtr, data, dataLength);
854}
855
Tim Murray9c642392013-04-11 13:29:59 -0700856void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
857 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800858 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700859
Yang Nid9bae682015-01-20 15:31:15 -0800860 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700861 if (!srcPtr) {
862 //ALOGV("Calling setVar on slot = %i which is null", slot);
863 return;
864 }
865 memcpy(data, srcPtr, dataLength);
866}
867
868
Jason Sams709a0972012-11-15 18:18:04 -0800869void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
870 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700871 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800872 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800873 if (!destPtr) {
874 //ALOGV("Calling setVar on slot = %i which is null", slot);
875 return;
876 }
877
878 // We want to look at dimension in terms of integer components,
879 // but dimLength is given in terms of bytes.
880 dimLength /= sizeof(int);
881
882 // Only a single dimension is currently supported.
883 rsAssert(dimLength == 1);
884 if (dimLength == 1) {
885 // First do the increment loop.
886 size_t stride = elem->getSizeBytes();
887 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700888 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800889 elem->incRefs(cVal);
890 cVal += stride;
891 }
892
893 // Decrement loop comes after (to prevent race conditions).
894 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700895 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800896 elem->decRefs(oldVal);
897 oldVal += stride;
898 }
899 }
900
901 memcpy(destPtr, data, dataLength);
902}
903
904void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
905
906 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800907 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800908
Yang Nid9bae682015-01-20 15:31:15 -0800909 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800910 if (!destPtr) {
911 //ALOGV("Calling setVar on slot = %i which is null", slot);
912 return;
913 }
914
Chris Wailes44bef6f2014-08-12 13:51:10 -0700915 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800916 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800917 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800918 ptr = data->mHal.drvState.lod[0].mallocPtr;
919 }
920 memcpy(destPtr, &ptr, sizeof(void *));
921}
922
923void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
924
925 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800926 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800927
Yang Nid9bae682015-01-20 15:31:15 -0800928 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800929 if (!destPtr) {
930 //ALOGV("Calling setVar on slot = %i which is null", slot);
931 return;
932 }
933
Jason Sams05ef73f2014-08-05 14:59:22 -0700934 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800935}
936
Yang Ni062c2872015-02-20 15:20:00 -0800937const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const {
938 return mScriptExec->getFieldName(slot);
939}
940
Jason Sams709a0972012-11-15 18:18:04 -0800941RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Yang Ni1efae292015-06-27 15:45:18 -0700942 delete mScriptExec;
943 delete[] mBoundAllocs;
Jason Sams110f1812013-03-14 16:02:18 -0700944 if (mScriptSO) {
945 dlclose(mScriptSO);
946 }
Jason Sams709a0972012-11-15 18:18:04 -0800947}
948
949Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
950 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700951 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800952 }
953
954 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
955 Allocation *a = mBoundAllocs[ct];
956 if (!a) continue;
957 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
958 return a;
959 }
960 }
961 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700962 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800963}
964
Stephen Hines8409d642015-04-28 18:49:56 -0700965int RsdCpuScriptImpl::getGlobalEntries() const {
966 return mScriptExec->getGlobalEntries();
967}
968
969const char * RsdCpuScriptImpl::getGlobalName(int i) const {
970 return mScriptExec->getGlobalName(i);
971}
972
973const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
974 return mScriptExec->getGlobalAddress(i);
975}
976
977size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
978 return mScriptExec->getGlobalSize(i);
979}
980
Stephen Hines5aa018c2015-05-20 18:09:57 -0700981uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const {
982 return mScriptExec->getGlobalProperties(i);
983}
984
Chris Wailesf3712132014-07-16 15:18:30 -0700985void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
986 uint32_t inLen, Allocation * aout,
987 const void * usr, uint32_t usrLen,
988 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700989
Chris Wailesf3712132014-07-16 15:18:30 -0700990void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
991 uint32_t inLen, Allocation * aout,
992 const void * usr, uint32_t usrLen,
993 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700994
Jason Sams709a0972012-11-15 18:18:04 -0800995
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800996} // namespace renderscript
997} // namespace android