blob: af8b640737ea4fbde78bcc03e672a3fba0369b91 [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
26 #include <bcc/BCCContext.h>
Stephen Hines82e0a672014-05-05 15:40:56 -070027 #include <bcc/Config/Config.h>
Jason Sams110f1812013-03-14 16:02:18 -070028 #include <bcc/Renderscript/RSCompilerDriver.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
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080035
36 #include <string>
37 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070038#endif
Jason Sams709a0972012-11-15 18:18:04 -080039
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080040#include <set>
41#include <string>
42#include <dlfcn.h>
43#include <stdlib.h>
44#include <string.h>
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080045#include <iostream>
46
47#ifdef __LP64__
48#define SYSLIBPATH "/system/lib64"
49#else
50#define SYSLIBPATH "/system/lib"
51#endif
52
Stephen Hinesba17ae42013-06-05 17:18:04 -070053namespace {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080054#ifndef RS_COMPATIBILITY_LIB
55
Stephen Hinesba17ae42013-06-05 17:18:04 -070056static bool is_force_recompile() {
57#ifdef RS_SERVER
58 return false;
59#else
60 char buf[PROPERTY_VALUE_MAX];
61
62 // Re-compile if floating point precision has been overridden.
63 property_get("debug.rs.precision", buf, "");
64 if (buf[0] != '\0') {
65 return true;
66 }
67
68 // Re-compile if debug.rs.forcerecompile is set.
69 property_get("debug.rs.forcerecompile", buf, "0");
70 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
71 return true;
72 } else {
73 return false;
74 }
75#endif // RS_SERVER
76}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070077
Chris Wailes6847e732014-08-11 17:30:51 -070078static void setCompileArguments(std::vector<const char*>* args,
79 const std::string& bcFileName,
80 const char* cacheDir, const char* resName,
81 const char* core_lib, bool useRSDebugContext,
82 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070083 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -080084 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -080085 args->push_back("-unroll-runtime");
86 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -070087 args->push_back("-o");
88 args->push_back(resName);
89 args->push_back("-output_path");
90 args->push_back(cacheDir);
91 args->push_back("-bclib");
92 args->push_back(core_lib);
93 args->push_back("-mtriple");
94 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
95
Tim Murray358ffb82014-12-09 11:53:06 -080096 // Enable workaround for A53 codegen by default.
97#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
98 args->push_back("-aarch64-fix-cortex-a53-835769");
99#endif
100
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700101 // Execute the bcc compiler.
102 if (useRSDebugContext) {
103 args->push_back("-rs-debug-ctx");
104 } else {
105 // Only load additional libraries for compiles that don't use
106 // the debug context.
107 if (bccPluginName && strlen(bccPluginName) > 0) {
108 args->push_back("-load");
109 args->push_back(bccPluginName);
110 }
111 }
112
Stephen Hines45e753a2015-01-19 20:58:44 -0800113 args->push_back("-fPIC");
114 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800115
Chris Wailes6847e732014-08-11 17:30:51 -0700116 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700117 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700118}
119
Chris Wailes6847e732014-08-11 17:30:51 -0700120static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700121 const char *bitcode,
122 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700123 const char **compileArguments,
124 const std::string &compileCommandLine) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700125 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700126
Chris Wailes6847e732014-08-11 17:30:51 -0700127 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700128 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700129 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700130 return false;
131 }
132 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
133 fclose(bcfile);
134 if (nwritten != bitcodeSize) {
135 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700136 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700137 return false;
138 }
139
140 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800141
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700142 switch (pid) {
143 case -1: { // Error occurred (we attempt no recovery)
144 ALOGE("Couldn't fork for bcc compiler execution");
145 return false;
146 }
147 case 0: { // Child process
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700148 ALOGV("Invoking BCC with: %s", compileCommandLine.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800149 execv(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
150 (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800151
Stephen Hines00511322014-01-31 11:20:23 -0800152 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700153 abort();
154 return false;
155 }
156 default: { // Parent process (actual driver)
157 // Wait on child process to finish compiling the source.
158 int status = 0;
159 pid_t w = waitpid(pid, &status, 0);
160 if (w == -1) {
161 ALOGE("Could not wait for bcc compiler");
162 return false;
163 }
164
165 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
166 return true;
167 }
168
169 ALOGE("bcc compiler terminated unexpectedly");
170 return false;
171 }
172 }
173}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700174
Yang Ni1c44cb62015-01-22 12:02:27 -0800175#endif // !defined(RS_COMPATIBILITY_LIB)
176} // namespace
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800177
Yang Ni1c44cb62015-01-22 12:02:27 -0800178namespace android {
179namespace renderscript {
180
Jason Sams709a0972012-11-15 18:18:04 -0800181RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
182 mCtx = ctx;
183 mScript = s;
184
Chris Wailes44bef6f2014-08-12 13:51:10 -0700185 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800186
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800187#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700188 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700189#endif
190
Tim Murraye195a3f2014-03-13 15:04:58 -0700191
Chris Wailes44bef6f2014-08-12 13:51:10 -0700192 mRoot = nullptr;
193 mRootExpand = nullptr;
194 mInit = nullptr;
195 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800196 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800197
Chris Wailes44bef6f2014-08-12 13:51:10 -0700198 mBoundAllocs = nullptr;
199 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800200 mIsThreadable = true;
201}
202
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800203bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800204 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
205 if (mRoot) {
206 //ALOGE("Found root(): %p", mRoot);
207 }
208 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
209 if (mRootExpand) {
210 //ALOGE("Found root.expand(): %p", mRootExpand);
211 }
212 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
213 if (mInit) {
214 //ALOGE("Found init(): %p", mInit);
215 }
216 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
217 if (mFreeChildren) {
218 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
219 }
220
Yang Nid9bae682015-01-20 15:31:15 -0800221 mScriptExec = ScriptExecutable::createFromSharedObject(
222 mCtx->getContext(), mScriptSO);
223
224 if (mScriptExec == nullptr) {
225 return false;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800226 }
227
Yang Nid9bae682015-01-20 15:31:15 -0800228 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800229 if (varCount > 0) {
230 mBoundAllocs = new Allocation *[varCount];
231 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
232 }
233
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800234 mIsThreadable = mScriptExec->getThreadable();
235 //ALOGE("Script isThreadable? %d", mIsThreadable);
236
Yang Nid9bae682015-01-20 15:31:15 -0800237 return true;
238}
239
Jason Sams709a0972012-11-15 18:18:04 -0800240bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
241 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800242 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800243 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
244 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800245 //ALOGE("rsdScriptInit %p %p", rsc, script);
246
247 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700248#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800249 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800250
Chris Wailes44bef6f2014-08-12 13:51:10 -0700251 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800252
Jason Sams709a0972012-11-15 18:18:04 -0800253 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700254 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800255 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
256 mCtx->unlockMutex();
257 return false;
258 }
259
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700260 // Run any compiler setup functions we have been provided with.
261 RSSetupCompilerCallback setupCompilerCallback =
262 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700263 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700264 setupCompilerCallback(mCompilerDriver);
265 }
266
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700267 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
268 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700269 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700270 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700271 return false;
272 }
273
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700274 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700275
276 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700277 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800278 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700279 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700280
Chris Wailes6847e732014-08-11 17:30:51 -0700281 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700282 bcFileName.append("/");
283 bcFileName.append(resName);
284 bcFileName.append(".bc");
285
286 std::vector<const char*> compileArguments;
287 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
288 useRSDebugContext, bccPluginName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700289 // The last argument of compileArguments ia a nullptr, so remove 1 from the size.
Yang Ni2abfcc62015-02-17 16:05:19 -0800290 std::unique_ptr<const char> joined(
291 rsuJoinStrings(compileArguments.size() - 1, compileArguments.data()));
292 std::string compileCommandLine (joined.get());
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700293
Tim Murraybf96a522015-01-23 15:37:03 -0800294 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800295 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700296 }
297
298 // If we can't, it's either not there or out of date. We compile the bit code and try loading
299 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800300 if (mScriptSO == nullptr) {
301 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
302 compileArguments.data(), compileCommandLine))
303 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700304 ALOGE("bcc: FAILS to compile '%s'", resName);
305 mCtx->unlockMutex();
306 return false;
307 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800308
Yang Ni1c44cb62015-01-22 12:02:27 -0800309 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800310 ALOGE("Linker: Failed to link object file '%s'", resName);
311 mCtx->unlockMutex();
312 return false;
313 }
314
Yang Ni1c44cb62015-01-22 12:02:27 -0800315 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800316 if (mScriptSO == nullptr) {
317 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700318 mCtx->unlockMutex();
319 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700320 }
321 }
Jason Sams709a0972012-11-15 18:18:04 -0800322
Yang Nic31585b2015-02-15 11:43:50 -0800323 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800324
Stephen Hines45e753a2015-01-19 20:58:44 -0800325 // Read RS symbol information from the .so.
326 if ( !mScriptSO) {
327 goto error;
Jason Sams709a0972012-11-15 18:18:04 -0800328 }
329
Stephen Hines45e753a2015-01-19 20:58:44 -0800330 if ( !storeRSInfoFromSO()) {
331 goto error;
Tim Murray29809d12014-05-28 12:04:19 -0700332 }
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700333#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800334 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
335 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700336
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800337 if (!mScriptSO) {
338 goto error;
339 }
Jason Sams110f1812013-03-14 16:02:18 -0700340
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800341 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700342 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700343 }
344#endif
Jason Sams709a0972012-11-15 18:18:04 -0800345 mCtx->unlockMutex();
346 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700347
Jason Sams110f1812013-03-14 16:02:18 -0700348error:
349
350 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700351 if (mScriptSO) {
352 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800353 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700354 }
355 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800356}
357
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700358#ifndef RS_COMPATIBILITY_LIB
359
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700360const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
361 size_t bitcodeSize) {
362 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
363
364 // If we're debugging, use the debug library.
365 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
366 return SYSLIBPATH"/libclcore_debug.bc";
367 }
368
369 // If a callback has been registered to specify a library, use that.
370 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700371 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700372 return selectRTCallback((const char*)bitcode, bitcodeSize);
373 }
374
375 // Check for a platform specific library
376#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
377 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700378 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700379 // NEON-capable ARMv7a devices can use an accelerated math library
380 // for all reduced precision scripts.
381 // ARMv8 does not use NEON, as ASIMD can be used with all precision
382 // levels.
383 return SYSLIBPATH"/libclcore_neon.bc";
384 } else {
385 return defaultLib;
386 }
387#elif defined(__i386__) || defined(__x86_64__)
388 // x86 devices will use an optimized library.
389 return SYSLIBPATH"/libclcore_x86.bc";
390#else
391 return defaultLib;
392#endif
393}
394
395#endif
396
Jason Sams709a0972012-11-15 18:18:04 -0800397void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700398 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800399 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
400 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800401 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800402 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
403 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700404
405 // Bug, need to stash in metadata
406 if (mRootExpand) {
407 script->mHal.info.root = mRootExpand;
408 } else {
409 script->mHal.info.root = mRoot;
410 }
Jason Sams709a0972012-11-15 18:18:04 -0800411}
412
Jason Sams709a0972012-11-15 18:18:04 -0800413
414typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
415
Jason Samsbf2111d2015-01-26 18:13:41 -0800416bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700417 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700418 Allocation * aout,
419 const void * usr, uint32_t usrLen,
420 const RsScriptCall *sc,
421 MTLaunchStruct *mtls) {
422
423 memset(mtls, 0, sizeof(MTLaunchStruct));
424
Chris Wailesf3712132014-07-16 15:18:30 -0700425 for (int index = inLen; --index >= 0;) {
426 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700427
Chris Wailesf3712132014-07-16 15:18:30 -0700428 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700429 if (ain != nullptr &&
430 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
431
Chris Wailesf3712132014-07-16 15:18:30 -0700432 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
433 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800434 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700435 }
436 }
437
Chris Wailes44bef6f2014-08-12 13:51:10 -0700438 if (aout &&
439 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
440
Chris Wailesf3712132014-07-16 15:18:30 -0700441 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
442 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800443 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700444 }
445
Chris Wailesf3712132014-07-16 15:18:30 -0700446 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700447 const Allocation *ain0 = ains[0];
448 const Type *inType = ain0->getType();
449
Jason Samsc0d68472015-01-20 14:29:52 -0800450 mtls->fep.dim.x = inType->getDimX();
451 mtls->fep.dim.y = inType->getDimY();
452 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700453
454 for (int Index = inLen; --Index >= 1;) {
455 if (!ain0->hasSameDims(ains[Index])) {
456 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -0800457 "Failed to launch kernel; dimensions of input and output"
458 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700459
Jason Samsbf2111d2015-01-26 18:13:41 -0800460 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700461 }
462 }
463
Chris Wailes44bef6f2014-08-12 13:51:10 -0700464 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700465 const Type *outType = aout->getType();
466
Jason Samsc0d68472015-01-20 14:29:52 -0800467 mtls->fep.dim.x = outType->getDimX();
468 mtls->fep.dim.y = outType->getDimY();
469 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700470
471 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700472 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
473 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800474 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700475 }
476
Chris Wailes44bef6f2014-08-12 13:51:10 -0700477 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700478 if (!ains[0]->hasSameDims(aout)) {
479 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
480 "Failed to launch kernel; dimensions of input and output allocations do not match.");
481
Jason Samsbf2111d2015-01-26 18:13:41 -0800482 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700483 }
484 }
485
486 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800487 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700488 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800489 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
490 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
491 if (mtls->start.x >= mtls->end.x) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700492 }
493
494 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800495 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700496 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800497 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
498 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
499 if (mtls->start.y >= mtls->end.y) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700500 }
501
502 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800503 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700504 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800505 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
506 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
507 if (mtls->start.z >= mtls->end.z) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700508 }
509
Jason Samsbf2111d2015-01-26 18:13:41 -0800510 if (!sc || (sc->arrayEnd == 0)) {
511 mtls->end.array[0] = mtls->fep.dim.array[0];
512 } else {
513 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
514 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
515 if (mtls->start.array[0] >= mtls->end.array[0]) return false;
516 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700517
Jason Samsbf2111d2015-01-26 18:13:41 -0800518 if (!sc || (sc->array2End == 0)) {
519 mtls->end.array[1] = mtls->fep.dim.array[1];
520 } else {
521 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
522 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
523 if (mtls->start.array[1] >= mtls->end.array[1]) return false;
524 }
525
526 if (!sc || (sc->array3End == 0)) {
527 mtls->end.array[2] = mtls->fep.dim.array[2];
528 } else {
529 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
530 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
531 if (mtls->start.array[2] >= mtls->end.array[2]) return false;
532 }
533
534 if (!sc || (sc->array4End == 0)) {
535 mtls->end.array[3] = mtls->fep.dim.array[3];
536 } else {
537 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
538 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
539 if (mtls->start.array[3] >= mtls->end.array[3]) return false;
540 }
541
542
543 // The X & Y walkers always want 0-1 min even if dim is not present
544 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
545 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700546
547 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800548 if (ains) {
549 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
550 }
551 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700552 mtls->fep.usr = usr;
553 mtls->fep.usrLen = usrLen;
554 mtls->mSliceSize = 1;
555 mtls->mSliceNum = 0;
556
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700557 mtls->isThreadable = mIsThreadable;
558
Chris Wailesf3712132014-07-16 15:18:30 -0700559 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700560 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700561 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800562 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
563 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700564 }
565 }
566
Chris Wailes44bef6f2014-08-12 13:51:10 -0700567 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800568 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
569 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700570 }
Jason Samsbf2111d2015-01-26 18:13:41 -0800571
572 // All validation passed, ok to launch threads
573 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700574}
575
Jason Sams709a0972012-11-15 18:18:04 -0800576
577void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700578 const Allocation ** ains,
579 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800580 Allocation * aout,
581 const void * usr,
582 uint32_t usrLen,
583 const RsScriptCall *sc) {
584
585 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700586
Jason Samsbf2111d2015-01-26 18:13:41 -0800587 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
588 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700589
Jason Samsbf2111d2015-01-26 18:13:41 -0800590 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
591 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
592 mCtx->setTLS(oldTLS);
593 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700594}
595
Jason Sams709a0972012-11-15 18:18:04 -0800596void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800597 mtls->script = this;
598 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800599 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700600 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800601 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800602}
603
604int RsdCpuScriptImpl::invokeRoot() {
605 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
606 int ret = mRoot();
607 mCtx->setTLS(oldTLS);
608 return ret;
609}
610
611void RsdCpuScriptImpl::invokeInit() {
612 if (mInit) {
613 mInit();
614 }
615}
616
617void RsdCpuScriptImpl::invokeFreeChildren() {
618 if (mFreeChildren) {
619 mFreeChildren();
620 }
621}
622
623void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
624 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800625 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800626 void * ap = nullptr;
627
628#if defined(__x86_64__)
629 // The invoked function could have input parameter of vector type for example float4 which
630 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
631 // So try to align void* params before passing them into RS exported function.
632
633 if ((uint8_t)(uint64_t)params & 0x0F) {
634 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
635 memcpy(ap, params, paramLength);
636 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -0800637 ALOGE("x86_64: invokeFunction memalign error, still use params which"
638 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +0800639 }
640 }
641#endif
Jason Sams709a0972012-11-15 18:18:04 -0800642
643 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800644 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -0800645 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +0800646
Jason Sams709a0972012-11-15 18:18:04 -0800647 mCtx->setTLS(oldTLS);
648}
649
650void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
651 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800652 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -0800653
654 //if (mIntrinsicID) {
655 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
656 //return;
657 //}
658
Yang Nid9bae682015-01-20 15:31:15 -0800659 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800660 if (!destPtr) {
661 //ALOGV("Calling setVar on slot = %i which is null", slot);
662 return;
663 }
664
665 memcpy(destPtr, data, dataLength);
666}
667
Tim Murray9c642392013-04-11 13:29:59 -0700668void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
669 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800670 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -0700671
Yang Nid9bae682015-01-20 15:31:15 -0800672 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -0700673 if (!srcPtr) {
674 //ALOGV("Calling setVar on slot = %i which is null", slot);
675 return;
676 }
677 memcpy(data, srcPtr, dataLength);
678}
679
680
Jason Sams709a0972012-11-15 18:18:04 -0800681void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
682 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -0700683 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -0800684 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800685 if (!destPtr) {
686 //ALOGV("Calling setVar on slot = %i which is null", slot);
687 return;
688 }
689
690 // We want to look at dimension in terms of integer components,
691 // but dimLength is given in terms of bytes.
692 dimLength /= sizeof(int);
693
694 // Only a single dimension is currently supported.
695 rsAssert(dimLength == 1);
696 if (dimLength == 1) {
697 // First do the increment loop.
698 size_t stride = elem->getSizeBytes();
699 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700700 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800701 elem->incRefs(cVal);
702 cVal += stride;
703 }
704
705 // Decrement loop comes after (to prevent race conditions).
706 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -0700707 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -0800708 elem->decRefs(oldVal);
709 oldVal += stride;
710 }
711 }
712
713 memcpy(destPtr, data, dataLength);
714}
715
716void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
717
718 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800719 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800720
Yang Nid9bae682015-01-20 15:31:15 -0800721 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800722 if (!destPtr) {
723 //ALOGV("Calling setVar on slot = %i which is null", slot);
724 return;
725 }
726
Chris Wailes44bef6f2014-08-12 13:51:10 -0700727 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800728 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800729 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -0800730 ptr = data->mHal.drvState.lod[0].mallocPtr;
731 }
732 memcpy(destPtr, &ptr, sizeof(void *));
733}
734
735void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
736
737 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800738 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -0800739
Yang Nid9bae682015-01-20 15:31:15 -0800740 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -0800741 if (!destPtr) {
742 //ALOGV("Calling setVar on slot = %i which is null", slot);
743 return;
744 }
745
Jason Sams05ef73f2014-08-05 14:59:22 -0700746 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -0800747}
748
749RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -0700750#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -0800751 if (mCompilerDriver) {
752 delete mCompilerDriver;
753 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800754#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800755
Yang Nid9bae682015-01-20 15:31:15 -0800756 if (mScriptExec != nullptr) {
757 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800758 }
Jason Sams110f1812013-03-14 16:02:18 -0700759 if (mBoundAllocs) delete[] mBoundAllocs;
760 if (mScriptSO) {
761 dlclose(mScriptSO);
762 }
Jason Sams709a0972012-11-15 18:18:04 -0800763}
764
765Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
766 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700767 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800768 }
769
770 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
771 Allocation *a = mBoundAllocs[ct];
772 if (!a) continue;
773 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
774 return a;
775 }
776 }
777 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700778 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800779}
780
Chris Wailesf3712132014-07-16 15:18:30 -0700781void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
782 uint32_t inLen, Allocation * aout,
783 const void * usr, uint32_t usrLen,
784 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700785
Chris Wailesf3712132014-07-16 15:18:30 -0700786void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
787 uint32_t inLen, Allocation * aout,
788 const void * usr, uint32_t usrLen,
789 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -0700790
Jason Sams709a0972012-11-15 18:18:04 -0800791
792}
793}