blob: f3128661af14905b928865b1ad6f7a5c83a59217 [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"
Jason Sams709a0972012-11-15 18:18:04 -080019
Jason Sams110f1812013-03-14 16:02:18 -070020#ifdef RS_COMPATIBILITY_LIB
Jason Sams110f1812013-03-14 16:02:18 -070021 #include <stdio.h>
Stephen Hinesee48c0b2013-10-30 17:48:30 -070022 #include <sys/stat.h>
Stephen Hinesc2c11cc2013-07-19 01:07:42 -070023 #include <unistd.h>
Jason Sams110f1812013-03-14 16:02:18 -070024#else
25 #include <bcc/BCCContext.h>
Stephen Hines82e0a672014-05-05 15:40:56 -070026 #include <bcc/Config/Config.h>
Jason Sams110f1812013-03-14 16:02:18 -070027 #include <bcc/Renderscript/RSCompilerDriver.h>
28 #include <bcc/Renderscript/RSExecutable.h>
29 #include <bcc/Renderscript/RSInfo.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070030 #include <bcinfo/MetadataExtractor.h>
Stephen Hinesba17ae42013-06-05 17:18:04 -070031 #include <cutils/properties.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070032
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #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>
46#include <fstream>
47#include <iostream>
48
49#ifdef __LP64__
50#define SYSLIBPATH "/system/lib64"
51#else
52#define SYSLIBPATH "/system/lib"
53#endif
54
Stephen Hinesba17ae42013-06-05 17:18:04 -070055namespace {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -070056
57// Create a len length string containing random characters from [A-Za-z0-9].
58static std::string getRandomString(size_t len) {
59 char buf[len + 1];
60 for (size_t i = 0; i < len; i++) {
61 uint32_t r = arc4random() & 0xffff;
62 r %= 62;
63 if (r < 26) {
64 // lowercase
65 buf[i] = 'a' + r;
66 } else if (r < 52) {
67 // uppercase
68 buf[i] = 'A' + (r - 26);
69 } else {
70 // Use a number
71 buf[i] = '0' + (r - 52);
72 }
73 }
74 buf[len] = '\0';
75 return std::string(buf);
76}
77
Stephen Hinesee48c0b2013-10-30 17:48:30 -070078// Check if a path exists and attempt to create it if it doesn't.
79static bool ensureCacheDirExists(const char *path) {
80 if (access(path, R_OK | W_OK | X_OK) == 0) {
81 // Done if we can rwx the directory
82 return true;
83 }
84 if (mkdir(path, 0700) == 0) {
85 return true;
86 }
87 return false;
88}
89
Stephen Hines7d774852014-10-01 12:57:57 -070090// Copy the file named \p srcFile to \p dstFile.
91// Return 0 on success and -1 if anything wasn't copied.
92static int copyFile(const char *dstFile, const char *srcFile) {
93 std::ifstream srcStream(srcFile);
94 if (!srcStream) {
95 ALOGE("Could not verify or read source file: %s", srcFile);
96 return -1;
97 }
98 std::ofstream dstStream(dstFile);
99 if (!dstStream) {
100 ALOGE("Could not verify or write destination file: %s", dstFile);
101 return -1;
102 }
103 dstStream << srcStream.rdbuf();
104 if (!dstStream) {
105 ALOGE("Could not write destination file: %s", dstFile);
106 return -1;
107 }
108
109 srcStream.close();
110 dstStream.close();
111
112 return 0;
113}
114
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800115#define RS_CACHE_DIR "com.android.renderscript.cache"
116
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700117// Attempt to load the shared library from origName, but then fall back to
Stephen Hines7d774852014-10-01 12:57:57 -0700118// creating a copy of the shared library if necessary (to ensure instancing).
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700119// This function returns the dlopen()-ed handle if successful.
120static void *loadSOHelper(const char *origName, const char *cacheDir,
121 const char *resName) {
122 // Keep track of which .so libraries have been loaded. Once a library is
Stephen Hines7d774852014-10-01 12:57:57 -0700123 // in the set (per-process granularity), we must instead make a copy of
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700124 // the original shared object (randomly named .so file) and load that one
125 // instead. If we don't do this, we end up aliasing global data between
126 // the various Script instances (which are supposed to be completely
127 // independent).
128 static std::set<std::string> LoadedLibraries;
129
Chris Wailes44bef6f2014-08-12 13:51:10 -0700130 void *loaded = nullptr;
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700131
132 // Skip everything if we don't even have the original library available.
133 if (access(origName, F_OK) != 0) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700134 return nullptr;
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700135 }
136
137 // Common path is that we have not loaded this Script/library before.
138 if (LoadedLibraries.find(origName) == LoadedLibraries.end()) {
139 loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL);
140 if (loaded) {
141 LoadedLibraries.insert(origName);
142 }
143 return loaded;
144 }
145
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700146 std::string newName(cacheDir);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800147
148 // Append RS_CACHE_DIR only if it is not found in cacheDir
149 // In driver mode, RS_CACHE_DIR is already appended to cacheDir.
150 if (newName.find(RS_CACHE_DIR) == std::string::npos) {
151 newName.append("/" RS_CACHE_DIR "/");
152 }
Stephen Hinesee48c0b2013-10-30 17:48:30 -0700153
154 if (!ensureCacheDirExists(newName.c_str())) {
155 ALOGE("Could not verify or create cache dir: %s", cacheDir);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700156 return nullptr;
Stephen Hinesee48c0b2013-10-30 17:48:30 -0700157 }
158
Stephen Hines7d774852014-10-01 12:57:57 -0700159 // Construct an appropriately randomized filename for the copy.
Stephen Hinesee48c0b2013-10-30 17:48:30 -0700160 newName.append("librs.");
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700161 newName.append(resName);
162 newName.append("#");
163 newName.append(getRandomString(6)); // 62^6 potential filename variants.
164 newName.append(".so");
165
Stephen Hines7d774852014-10-01 12:57:57 -0700166 int r = copyFile(newName.c_str(), origName);
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700167 if (r != 0) {
Stephen Hines7d774852014-10-01 12:57:57 -0700168 ALOGE("Could not create copy %s -> %s", origName, newName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700169 return nullptr;
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700170 }
171 loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL);
172 r = unlink(newName.c_str());
173 if (r != 0) {
Stephen Hines7d774852014-10-01 12:57:57 -0700174 ALOGE("Could not unlink copy %s", newName.c_str());
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700175 }
176 if (loaded) {
177 LoadedLibraries.insert(newName.c_str());
178 }
179
180 return loaded;
181}
182
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800183static std::string findSharedObjectName(const char *cacheDir,
184 const char *resName) {
185
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700186#ifndef RS_SERVER
187 std::string scriptSOName(cacheDir);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800188#ifdef RS_COMPATIBILITY_LIB
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700189 size_t cutPos = scriptSOName.rfind("cache");
190 if (cutPos != std::string::npos) {
191 scriptSOName.erase(cutPos);
192 } else {
193 ALOGE("Found peculiar cacheDir (missing \"cache\"): %s", cacheDir);
194 }
195 scriptSOName.append("/lib/librs.");
196#else
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800197 scriptSOName.append("/librs.");
198#endif
199
200#else
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700201 std::string scriptSOName("lib");
202#endif
203 scriptSOName.append(resName);
204 scriptSOName.append(".so");
205
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800206 return scriptSOName;
207}
208
209// Load the shared library referred to by cacheDir and resName. If we have
210// already loaded this library, we instead create a new copy (in the
211// cache dir) and then load that. We then immediately destroy the copy.
212// This is required behavior to implement script instancing for the support
213// library, since shared objects are loaded and de-duped by name only.
214static void *loadSharedLibrary(const char *cacheDir, const char *resName) {
215 void *loaded = nullptr;
216
217 std::string scriptSOName = findSharedObjectName(cacheDir, resName);
218
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700219 // We should check if we can load the library from the standard app
220 // location for shared libraries first.
221 loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName);
222
Chris Wailes44bef6f2014-08-12 13:51:10 -0700223 if (loaded == nullptr) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700224 ALOGE("Unable to open shared library (%s): %s",
225 scriptSOName.c_str(), dlerror());
226
227 // One final attempt to find the library in "/system/lib".
228 // We do this to allow bundled applications to use the compatibility
229 // library fallback path. Those applications don't have a private
230 // library path, so they need to install to the system directly.
231 // Note that this is really just a testing path.
Chris Wailes93d6bc82014-07-28 16:54:38 -0700232 std::string scriptSONameSystem("/system/lib/librs.");
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700233 scriptSONameSystem.append(resName);
234 scriptSONameSystem.append(".so");
235 loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir,
236 resName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700237 if (loaded == nullptr) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700238 ALOGE("Unable to open system shared library (%s): %s",
239 scriptSONameSystem.c_str(), dlerror());
240 }
241 }
242
243 return loaded;
244}
245
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800246#ifndef RS_COMPATIBILITY_LIB
247
Pirama Arumuga Nainarb5215a52015-01-16 09:11:27 -0800248static inline bool is_skip_linkloader() {
249 return true;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800250}
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700251
Stephen Hinesba17ae42013-06-05 17:18:04 -0700252static bool is_force_recompile() {
253#ifdef RS_SERVER
254 return false;
255#else
256 char buf[PROPERTY_VALUE_MAX];
257
258 // Re-compile if floating point precision has been overridden.
259 property_get("debug.rs.precision", buf, "");
260 if (buf[0] != '\0') {
261 return true;
262 }
263
264 // Re-compile if debug.rs.forcerecompile is set.
265 property_get("debug.rs.forcerecompile", buf, "0");
266 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
267 return true;
268 } else {
269 return false;
270 }
271#endif // RS_SERVER
272}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700273
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700274const static char *BCC_EXE_PATH = "/system/bin/bcc";
275
Chris Wailes6847e732014-08-11 17:30:51 -0700276static void setCompileArguments(std::vector<const char*>* args,
277 const std::string& bcFileName,
278 const char* cacheDir, const char* resName,
279 const char* core_lib, bool useRSDebugContext,
280 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700281 rsAssert(cacheDir && resName && core_lib);
282 args->push_back(BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -0800283 args->push_back("-unroll-runtime");
284 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700285 args->push_back("-o");
286 args->push_back(resName);
287 args->push_back("-output_path");
288 args->push_back(cacheDir);
289 args->push_back("-bclib");
290 args->push_back(core_lib);
291 args->push_back("-mtriple");
292 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
293
Tim Murray358ffb82014-12-09 11:53:06 -0800294 // Enable workaround for A53 codegen by default.
295#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
296 args->push_back("-aarch64-fix-cortex-a53-835769");
297#endif
298
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700299 // Execute the bcc compiler.
300 if (useRSDebugContext) {
301 args->push_back("-rs-debug-ctx");
302 } else {
303 // Only load additional libraries for compiles that don't use
304 // the debug context.
305 if (bccPluginName && strlen(bccPluginName) > 0) {
306 args->push_back("-load");
307 args->push_back(bccPluginName);
308 }
309 }
310
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800311 if (is_skip_linkloader()) {
312 args->push_back("-fPIC");
313 args->push_back("-embedRSInfo");
314 }
315
Chris Wailes6847e732014-08-11 17:30:51 -0700316 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700317 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700318}
319
Chris Wailes6847e732014-08-11 17:30:51 -0700320static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700321 const char *bitcode,
322 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700323 const char **compileArguments,
324 const std::string &compileCommandLine) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700325 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700326
Chris Wailes6847e732014-08-11 17:30:51 -0700327 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700328 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700329 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700330 return false;
331 }
332 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
333 fclose(bcfile);
334 if (nwritten != bitcodeSize) {
335 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700336 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700337 return false;
338 }
339
340 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800341
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700342 switch (pid) {
343 case -1: { // Error occurred (we attempt no recovery)
344 ALOGE("Couldn't fork for bcc compiler execution");
345 return false;
346 }
347 case 0: { // Child process
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700348 ALOGV("Invoking BCC with: %s", compileCommandLine.c_str());
349 execv(BCC_EXE_PATH, (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800350
Stephen Hines00511322014-01-31 11:20:23 -0800351 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700352 abort();
353 return false;
354 }
355 default: { // Parent process (actual driver)
356 // Wait on child process to finish compiling the source.
357 int status = 0;
358 pid_t w = waitpid(pid, &status, 0);
359 if (w == -1) {
360 ALOGE("Could not wait for bcc compiler");
361 return false;
362 }
363
364 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
365 return true;
366 }
367
368 ALOGE("bcc compiler terminated unexpectedly");
369 return false;
370 }
371 }
372}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700373
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800374const static char *LD_EXE_PATH = "/system/bin/ld.mc";
375
376static bool createSharedLib(const char *cacheDir, const char *resName) {
377 std::string sharedLibName = findSharedObjectName(cacheDir, resName);
378 std::string objFileName = cacheDir;
379 objFileName.append("/");
380 objFileName.append(resName);
381 objFileName.append(".o");
382
383 const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
384 std::vector<const char *> args = {
385 LD_EXE_PATH,
386 "-shared",
387 "-nostdlib",
388 compiler_rt,
389 "-mtriple", DEFAULT_TARGET_TRIPLE_STRING,
390 "-L", SYSLIBPATH,
391 "-lRSDriver", "-lm", "-lc",
392 objFileName.c_str(),
393 "-o", sharedLibName.c_str(),
394 nullptr
395 };
396
397 std::string cmdLineStr = bcc::getCommandLine(args.size()-1, args.data());
398
399 pid_t pid = fork();
400
401 switch (pid) {
402 case -1: { // Error occurred (we attempt no recovery)
403 ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH);
404 return false;
405 }
406 case 0: { // Child process
407 ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str());
408 execv(LD_EXE_PATH, (char* const*) args.data());
409
410 ALOGE("execv() failed: %s", strerror(errno));
411 abort();
412 return false;
413 }
414 default: { // Parent process (actual driver)
415 // Wait on child process to finish compiling the source.
416 int status = 0;
417 pid_t w = waitpid(pid, &status, 0);
418 if (w == -1) {
419 ALOGE("Could not wait for linker (%s)", LD_EXE_PATH);
420 return false;
421 }
422
423 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
424 return true;
425 }
426
427 ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH);
428 return false;
429 }
430 }
431}
Stephen Hinesba17ae42013-06-05 17:18:04 -0700432#endif // !defined(RS_COMPATIBILITY_LIB)
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700433} // namespace
Stephen Hinesba17ae42013-06-05 17:18:04 -0700434
Jason Sams709a0972012-11-15 18:18:04 -0800435namespace android {
436namespace renderscript {
437
Jason Sams110f1812013-03-14 16:02:18 -0700438#define MAXLINE 500
439#define MAKE_STR_HELPER(S) #S
440#define MAKE_STR(S) MAKE_STR_HELPER(S)
441#define EXPORT_VAR_STR "exportVarCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700442#define EXPORT_FUNC_STR "exportFuncCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700443#define EXPORT_FOREACH_STR "exportForEachCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700444#define OBJECT_SLOT_STR "objectSlotCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700445
446// Copy up to a newline or size chars from str -> s, updating str
Chris Wailes44bef6f2014-08-12 13:51:10 -0700447// Returns s when successful and nullptr when '\0' is finally reached.
Jason Sams110f1812013-03-14 16:02:18 -0700448static char* strgets(char *s, int size, const char **ppstr) {
449 if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700450 return nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700451 }
452
453 int i;
454 for (i = 0; i < (size - 1); i++) {
455 s[i] = **ppstr;
456 (*ppstr)++;
457 if (s[i] == '\0') {
458 return s;
459 } else if (s[i] == '\n') {
460 s[i+1] = '\0';
461 return s;
462 }
463 }
464
465 // size has been exceeded.
466 s[i] = '\0';
467
468 return s;
469}
Jason Sams709a0972012-11-15 18:18:04 -0800470
471RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
472 mCtx = ctx;
473 mScript = s;
474
Chris Wailes44bef6f2014-08-12 13:51:10 -0700475 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800476
Chris Wailes44bef6f2014-08-12 13:51:10 -0700477 mInvokeFunctions = nullptr;
478 mForEachFunctions = nullptr;
479 mFieldAddress = nullptr;
480 mFieldIsObject = nullptr;
481 mForEachSignatures = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800482
483#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700484 mCompilerDriver = nullptr;
485 mExecutable = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700486#endif
487
Tim Murraye195a3f2014-03-13 15:04:58 -0700488
Chris Wailes44bef6f2014-08-12 13:51:10 -0700489 mRoot = nullptr;
490 mRootExpand = nullptr;
491 mInit = nullptr;
492 mFreeChildren = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800493
Jason Sams709a0972012-11-15 18:18:04 -0800494
Chris Wailes44bef6f2014-08-12 13:51:10 -0700495 mBoundAllocs = nullptr;
496 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800497 mIsThreadable = true;
498}
499
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800500bool RsdCpuScriptImpl::storeRSInfoFromSO() {
501 char line[MAXLINE];
502 size_t varCount = 0;
503 size_t funcCount = 0;
504 size_t forEachCount = 0;
505 size_t objectSlotCount = 0;
506
507 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
508 if (mRoot) {
509 //ALOGE("Found root(): %p", mRoot);
510 }
511 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
512 if (mRootExpand) {
513 //ALOGE("Found root.expand(): %p", mRootExpand);
514 }
515 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
516 if (mInit) {
517 //ALOGE("Found init(): %p", mInit);
518 }
519 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
520 if (mFreeChildren) {
521 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
522 }
523
524 const char *rsInfo = (const char *) dlsym(mScriptSO, ".rs.info");
525 if (rsInfo) {
526 //ALOGE("Found .rs.info(): %p - %s", rsInfo, rsInfo);
527 }
528
529 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
530 goto error;
531 }
532 if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
533 ALOGE("Invalid export var count!: %s", line);
534 goto error;
535 }
536
537 mExportedVariableCount = varCount;
538 //ALOGE("varCount: %zu", varCount);
539 if (varCount > 0) {
540 // Start by creating/zeroing this member, since we don't want to
541 // accidentally clean up invalid pointers later (if we error out).
542 mFieldIsObject = new bool[varCount];
543 if (mFieldIsObject == nullptr) {
544 goto error;
545 }
546 memset(mFieldIsObject, 0, varCount * sizeof(*mFieldIsObject));
547 mFieldAddress = new void*[varCount];
548 if (mFieldAddress == nullptr) {
549 goto error;
550 }
551 for (size_t i = 0; i < varCount; ++i) {
552 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
553 goto error;
554 }
555 char *c = strrchr(line, '\n');
556 if (c) {
557 *c = '\0';
558 }
559 mFieldAddress[i] = dlsym(mScriptSO, line);
560 if (mFieldAddress[i] == nullptr) {
561 ALOGE("Failed to find variable address for %s: %s",
562 line, dlerror());
563 // Not a critical error if we don't find a global variable.
564 }
565 else {
566 //ALOGE("Found variable %s at %p", line,
567 //mFieldAddress[i]);
568 }
569 }
570 }
571
572 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
573 goto error;
574 }
575 if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
576 ALOGE("Invalid export func count!: %s", line);
577 goto error;
578 }
579
580 mExportedFunctionCount = funcCount;
581 //ALOGE("funcCount: %zu", funcCount);
582
583 if (funcCount > 0) {
584 mInvokeFunctions = new InvokeFunc_t[funcCount];
585 if (mInvokeFunctions == nullptr) {
586 goto error;
587 }
588 for (size_t i = 0; i < funcCount; ++i) {
589 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
590 goto error;
591 }
592 char *c = strrchr(line, '\n');
593 if (c) {
594 *c = '\0';
595 }
596
597 mInvokeFunctions[i] = (InvokeFunc_t) dlsym(mScriptSO, line);
598 if (mInvokeFunctions[i] == nullptr) {
599 ALOGE("Failed to get function address for %s(): %s",
600 line, dlerror());
601 goto error;
602 }
603 else {
604 //ALOGE("Found InvokeFunc_t %s at %p", line, mInvokeFunctions[i]);
605 }
606 }
607 }
608
609 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
610 goto error;
611 }
612 if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
613 ALOGE("Invalid export forEach count!: %s", line);
614 goto error;
615 }
616
617 if (forEachCount > 0) {
618
619 mForEachSignatures = new uint32_t[forEachCount];
620 if (mForEachSignatures == nullptr) {
621 goto error;
622 }
623 mForEachFunctions = new ForEachFunc_t[forEachCount];
624 if (mForEachFunctions == nullptr) {
625 goto error;
626 }
627 for (size_t i = 0; i < forEachCount; ++i) {
628 unsigned int tmpSig = 0;
629 char tmpName[MAXLINE];
630
631 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
632 goto error;
633 }
634 if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
635 &tmpSig, tmpName) != 2) {
636 ALOGE("Invalid export forEach!: %s", line);
637 goto error;
638 }
639
640 // Lookup the expanded ForEach kernel.
641 strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
642 mForEachSignatures[i] = tmpSig;
643 mForEachFunctions[i] =
644 (ForEachFunc_t) dlsym(mScriptSO, tmpName);
645 if (i != 0 && mForEachFunctions[i] == nullptr) {
646 // Ignore missing root.expand functions.
647 // root() is always specified at location 0.
648 ALOGE("Failed to find forEach function address for %s: %s",
649 tmpName, dlerror());
650 goto error;
651 }
652 else {
653 //ALOGE("Found forEach %s at %p", tmpName, mForEachFunctions[i]);
654 }
655 }
656 }
657
658 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
659 goto error;
660 }
661 if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
662 ALOGE("Invalid object slot count!: %s", line);
663 goto error;
664 }
665
666 if (objectSlotCount > 0) {
667 rsAssert(varCount > 0);
668 for (size_t i = 0; i < objectSlotCount; ++i) {
669 uint32_t varNum = 0;
670 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
671 goto error;
672 }
673 if (sscanf(line, "%u", &varNum) != 1) {
674 ALOGE("Invalid object slot!: %s", line);
675 goto error;
676 }
677
678 if (varNum < varCount) {
679 mFieldIsObject[varNum] = true;
680 }
681 }
682 }
683
684 if (varCount > 0) {
685 mBoundAllocs = new Allocation *[varCount];
686 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
687 }
688
689 if (mScriptSO == (void*)1) {
690 //rsdLookupRuntimeStub(script, "acos");
691 }
692
693 return true;
694
695error:
696 delete[] mInvokeFunctions;
697 delete[] mForEachFunctions;
698 delete[] mFieldAddress;
699 delete[] mFieldIsObject;
700 delete[] mForEachSignatures;
701 delete[] mBoundAllocs;
702
703 return false;
704}
705
706#ifndef RS_COMPATIBILITY_LIB
707bool RsdCpuScriptImpl::storeRSInfoFromObj(bcinfo::MetadataExtractor &bitcodeMetadata) {
708
709 mExecutable->setThreadable(mIsThreadable);
710 if (!mExecutable->syncInfo()) {
711 ALOGW("bcc: FAILS to synchronize the RS info file to the disk");
712 }
713
714 mRoot = reinterpret_cast<int (*)()>(mExecutable->getSymbolAddress("root"));
715 mRootExpand =
716 reinterpret_cast<int (*)()>(mExecutable->getSymbolAddress("root.expand"));
717 mInit = reinterpret_cast<void (*)()>(mExecutable->getSymbolAddress("init"));
718 mFreeChildren =
719 reinterpret_cast<void (*)()>(mExecutable->getSymbolAddress(".rs.dtor"));
720
721
722 if (bitcodeMetadata.getExportVarCount()) {
723 mBoundAllocs = new Allocation *[bitcodeMetadata.getExportVarCount()];
724 memset(mBoundAllocs, 0, sizeof(void *) * bitcodeMetadata.getExportVarCount());
725 }
726
727 for (size_t i = 0; i < bitcodeMetadata.getExportForEachSignatureCount(); i++) {
728 char* name = new char[strlen(bitcodeMetadata.getExportForEachNameList()[i]) + 1];
729 mExportedForEachFuncList.push_back(
730 std::make_pair(name, bitcodeMetadata.getExportForEachSignatureList()[i]));
731 }
732
733 return true;
734}
735#endif
Jason Sams709a0972012-11-15 18:18:04 -0800736
737bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
738 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800739 uint32_t flags, char const *bccPluginName) {
Jason Sams709a0972012-11-15 18:18:04 -0800740 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
741 //ALOGE("rsdScriptInit %p %p", rsc, script);
742
743 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700744#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800745 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800746
Chris Wailes44bef6f2014-08-12 13:51:10 -0700747 mCompilerDriver = nullptr;
748 mExecutable = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800749
Jason Sams709a0972012-11-15 18:18:04 -0800750 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700751 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800752 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
753 mCtx->unlockMutex();
754 return false;
755 }
756
Stephen Hines25e3af52014-05-21 21:23:08 -0700757 // Configure symbol resolvers (via compiler-rt and the RS runtime).
758 mRSRuntime.setLookupFunction(lookupRuntimeStub);
759 mRSRuntime.setContext(this);
760 mResolver.chainResolver(mCompilerRuntime);
761 mResolver.chainResolver(mRSRuntime);
Jason Sams709a0972012-11-15 18:18:04 -0800762
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700763 // Run any compiler setup functions we have been provided with.
764 RSSetupCompilerCallback setupCompilerCallback =
765 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700766 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700767 setupCompilerCallback(mCompilerDriver);
768 }
769
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700770 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
771 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700772 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700773 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700774 return false;
775 }
776
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700777 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700778
779 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700780 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800781 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700782 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700783
Chris Wailes6847e732014-08-11 17:30:51 -0700784 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700785 bcFileName.append("/");
786 bcFileName.append(resName);
787 bcFileName.append(".bc");
788
789 std::vector<const char*> compileArguments;
790 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
791 useRSDebugContext, bccPluginName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700792 // The last argument of compileArguments ia a nullptr, so remove 1 from the size.
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700793 std::string compileCommandLine =
794 bcc::getCommandLine(compileArguments.size() - 1, compileArguments.data());
795
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800796 if (is_skip_linkloader()) {
797 if (!is_force_recompile()) {
798 mScriptSO = loadSharedLibrary(cacheDir, resName);
799 }
800 }
801 else if (!is_force_recompile()) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700802 // Load the compiled script that's in the cache, if any.
803 mExecutable = bcc::RSCompilerDriver::loadScript(cacheDir, resName, (const char*)bitcode,
804 bitcodeSize, compileCommandLine.c_str(),
805 mResolver);
806 }
807
808 // If we can't, it's either not there or out of date. We compile the bit code and try loading
809 // again.
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800810 if (is_skip_linkloader()) {
811 if (mScriptSO == nullptr) {
812 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
813 compileArguments.data(), compileCommandLine))
814 {
815 ALOGE("bcc: FAILS to compile '%s'", resName);
816 mCtx->unlockMutex();
817 return false;
818 }
819
820 if (!createSharedLib(cacheDir, resName)) {
821 ALOGE("Linker: Failed to link object file '%s'", resName);
822 mCtx->unlockMutex();
823 return false;
824 }
825
826 mScriptSO = loadSharedLibrary(cacheDir, resName);
827 if (mScriptSO == nullptr) {
828 ALOGE("Unable to load '%s'", resName);
829 mCtx->unlockMutex();
830 return false;
831 }
832 }
833 }
834 else if (mExecutable == nullptr) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700835 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize, compileArguments.data(),
836 compileCommandLine)) {
837 ALOGE("bcc: FAILS to compile '%s'", resName);
838 mCtx->unlockMutex();
839 return false;
840 }
841 mExecutable = bcc::RSCompilerDriver::loadScript(cacheDir, resName, (const char*)bitcode,
842 bitcodeSize, compileCommandLine.c_str(),
843 mResolver);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700844 if (mExecutable == nullptr) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700845 ALOGE("bcc: FAILS to load freshly compiled executable for '%s'", resName);
846 mCtx->unlockMutex();
847 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700848 }
849 }
Jason Sams709a0972012-11-15 18:18:04 -0800850
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800851 // if using the shared object path, read RS symbol information
852 // from the .so. Otherwise, read from the object files
853 if (!is_skip_linkloader()) {
854 storeRSInfoFromObj(bitcodeMetadata);
Jason Sams709a0972012-11-15 18:18:04 -0800855 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800856 else {
857 if ( !mScriptSO) {
858 goto error;
859 }
Jason Sams709a0972012-11-15 18:18:04 -0800860
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800861 if ( !storeRSInfoFromSO()) {
862 goto error;
863 }
Tim Murray29809d12014-05-28 12:04:19 -0700864 }
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700865#else // RS_COMPATIBILITY_LIB is defined
Jason Sams110f1812013-03-14 16:02:18 -0700866
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700867 mScriptSO = loadSharedLibrary(cacheDir, resName);
Jason Sams110f1812013-03-14 16:02:18 -0700868
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800869 if (!mScriptSO) {
870 goto error;
871 }
Jason Sams110f1812013-03-14 16:02:18 -0700872
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800873 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700874 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700875 }
876#endif
Jason Sams709a0972012-11-15 18:18:04 -0800877 mCtx->unlockMutex();
878 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700879
Jason Sams110f1812013-03-14 16:02:18 -0700880error:
881
882 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700883 if (mScriptSO) {
884 dlclose(mScriptSO);
885 }
886 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800887}
888
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700889#ifndef RS_COMPATIBILITY_LIB
890
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700891const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
892 size_t bitcodeSize) {
893 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
894
895 // If we're debugging, use the debug library.
896 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
897 return SYSLIBPATH"/libclcore_debug.bc";
898 }
899
900 // If a callback has been registered to specify a library, use that.
901 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700902 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700903 return selectRTCallback((const char*)bitcode, bitcodeSize);
904 }
905
906 // Check for a platform specific library
907#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
908 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700909 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700910 // NEON-capable ARMv7a devices can use an accelerated math library
911 // for all reduced precision scripts.
912 // ARMv8 does not use NEON, as ASIMD can be used with all precision
913 // levels.
914 return SYSLIBPATH"/libclcore_neon.bc";
915 } else {
916 return defaultLib;
917 }
918#elif defined(__i386__) || defined(__x86_64__)
919 // x86 devices will use an optimized library.
920 return SYSLIBPATH"/libclcore_x86.bc";
921#else
922 return defaultLib;
923#endif
924}
925
926#endif
927
Jason Sams709a0972012-11-15 18:18:04 -0800928void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700929#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -0800930 // Copy info over to runtime
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800931 if (!is_skip_linkloader()) {
932 script->mHal.info.exportedFunctionCount = mExecutable->getExportFuncAddrs().size();
933 script->mHal.info.exportedVariableCount = mExecutable->getExportVarAddrs().size();
934 script->mHal.info.exportedForeachFuncList = &mExportedForEachFuncList[0];
935 script->mHal.info.exportedPragmaCount = mExecutable->getPragmaKeys().size();
936 script->mHal.info.exportedPragmaKeyList =
937 const_cast<const char**>(&mExecutable->getPragmaKeys().front());
938 script->mHal.info.exportedPragmaValueList =
939 const_cast<const char**>(&mExecutable->getPragmaValues().front());
Jason Sams709a0972012-11-15 18:18:04 -0800940
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800941 if (mRootExpand) {
942 script->mHal.info.root = mRootExpand;
943 } else {
944 script->mHal.info.root = mRoot;
945 }
946 }
947 else {
948 // Copy info over to runtime
949 script->mHal.info.exportedFunctionCount = mExportedFunctionCount;
950 script->mHal.info.exportedVariableCount = mExportedVariableCount;
951 script->mHal.info.exportedPragmaCount = 0;
952 script->mHal.info.exportedPragmaKeyList = 0;
953 script->mHal.info.exportedPragmaValueList = 0;
954
955 // Bug, need to stash in metadata
956 if (mRootExpand) {
957 script->mHal.info.root = mRootExpand;
958 } else {
959 script->mHal.info.root = mRoot;
960 }
Jason Sams709a0972012-11-15 18:18:04 -0800961 }
Jason Sams110f1812013-03-14 16:02:18 -0700962#else
963 // Copy info over to runtime
964 script->mHal.info.exportedFunctionCount = mExportedFunctionCount;
965 script->mHal.info.exportedVariableCount = mExportedVariableCount;
966 script->mHal.info.exportedPragmaCount = 0;
967 script->mHal.info.exportedPragmaKeyList = 0;
968 script->mHal.info.exportedPragmaValueList = 0;
969
970 // Bug, need to stash in metadata
971 if (mRootExpand) {
972 script->mHal.info.root = mRootExpand;
973 } else {
974 script->mHal.info.root = mRoot;
975 }
976#endif
Jason Sams709a0972012-11-15 18:18:04 -0800977}
978
Jason Sams709a0972012-11-15 18:18:04 -0800979
980typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
981
Chris Wailesf3712132014-07-16 15:18:30 -0700982void RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
983 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700984 Allocation * aout,
985 const void * usr, uint32_t usrLen,
986 const RsScriptCall *sc,
987 MTLaunchStruct *mtls) {
988
989 memset(mtls, 0, sizeof(MTLaunchStruct));
990
Chris Wailesf3712132014-07-16 15:18:30 -0700991 for (int index = inLen; --index >= 0;) {
992 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700993
Chris Wailesf3712132014-07-16 15:18:30 -0700994 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700995 if (ain != nullptr &&
996 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
997
Chris Wailesf3712132014-07-16 15:18:30 -0700998 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
999 "rsForEach called with null in allocations");
1000 return;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001001 }
1002 }
1003
Chris Wailes44bef6f2014-08-12 13:51:10 -07001004 if (aout &&
1005 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
1006
Chris Wailesf3712132014-07-16 15:18:30 -07001007 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1008 "rsForEach called with null out allocations");
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001009 return;
1010 }
1011
Chris Wailesf3712132014-07-16 15:18:30 -07001012 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001013 const Allocation *ain0 = ains[0];
1014 const Type *inType = ain0->getType();
1015
Jason Samsc0d68472015-01-20 14:29:52 -08001016 mtls->fep.dim.x = inType->getDimX();
1017 mtls->fep.dim.y = inType->getDimY();
1018 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001019
1020 for (int Index = inLen; --Index >= 1;) {
1021 if (!ain0->hasSameDims(ains[Index])) {
1022 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1023 "Failed to launch kernel; dimensions of input and output allocations do not match.");
1024
1025 return;
1026 }
1027 }
1028
Chris Wailes44bef6f2014-08-12 13:51:10 -07001029 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001030 const Type *outType = aout->getType();
1031
Jason Samsc0d68472015-01-20 14:29:52 -08001032 mtls->fep.dim.x = outType->getDimX();
1033 mtls->fep.dim.y = outType->getDimY();
1034 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001035
1036 } else {
Chris Wailesf3712132014-07-16 15:18:30 -07001037 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1038 "rsForEach called with null allocations");
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001039 return;
1040 }
1041
Chris Wailes44bef6f2014-08-12 13:51:10 -07001042 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001043 if (!ains[0]->hasSameDims(aout)) {
1044 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1045 "Failed to launch kernel; dimensions of input and output allocations do not match.");
1046
1047 return;
1048 }
1049 }
1050
1051 if (!sc || (sc->xEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -08001052 mtls->xEnd = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001053 } else {
Jason Samsc0d68472015-01-20 14:29:52 -08001054 rsAssert(sc->xStart < mtls->fep.dim.x);
1055 rsAssert(sc->xEnd <= mtls->fep.dim.x);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001056 rsAssert(sc->xStart < sc->xEnd);
Jason Samsc0d68472015-01-20 14:29:52 -08001057 mtls->xStart = rsMin(mtls->fep.dim.x, sc->xStart);
1058 mtls->xEnd = rsMin(mtls->fep.dim.x, sc->xEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001059 if (mtls->xStart >= mtls->xEnd) return;
1060 }
1061
1062 if (!sc || (sc->yEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -08001063 mtls->yEnd = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001064 } else {
Jason Samsc0d68472015-01-20 14:29:52 -08001065 rsAssert(sc->yStart < mtls->fep.dim.y);
1066 rsAssert(sc->yEnd <= mtls->fep.dim.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001067 rsAssert(sc->yStart < sc->yEnd);
Jason Samsc0d68472015-01-20 14:29:52 -08001068 mtls->yStart = rsMin(mtls->fep.dim.y, sc->yStart);
1069 mtls->yEnd = rsMin(mtls->fep.dim.y, sc->yEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001070 if (mtls->yStart >= mtls->yEnd) return;
1071 }
1072
1073 if (!sc || (sc->zEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -08001074 mtls->zEnd = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001075 } else {
Jason Samsc0d68472015-01-20 14:29:52 -08001076 rsAssert(sc->zStart < mtls->fep.dim.z);
1077 rsAssert(sc->zEnd <= mtls->fep.dim.z);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001078 rsAssert(sc->zStart < sc->zEnd);
Jason Samsc0d68472015-01-20 14:29:52 -08001079 mtls->zStart = rsMin(mtls->fep.dim.z, sc->zStart);
1080 mtls->zEnd = rsMin(mtls->fep.dim.z, sc->zEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001081 if (mtls->zStart >= mtls->zEnd) return;
1082 }
1083
1084 mtls->xEnd = rsMax((uint32_t)1, mtls->xEnd);
1085 mtls->yEnd = rsMax((uint32_t)1, mtls->yEnd);
1086 mtls->zEnd = rsMax((uint32_t)1, mtls->zEnd);
1087 mtls->arrayEnd = rsMax((uint32_t)1, mtls->arrayEnd);
1088
Chris Wailesf3712132014-07-16 15:18:30 -07001089 rsAssert(inLen == 0 || (ains[0]->getType()->getDimZ() == 0));
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001090
1091 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -08001092 if (ains) {
1093 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
1094 }
1095 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001096 mtls->fep.usr = usr;
1097 mtls->fep.usrLen = usrLen;
1098 mtls->mSliceSize = 1;
1099 mtls->mSliceNum = 0;
1100
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001101 mtls->isThreadable = mIsThreadable;
1102
Chris Wailesf3712132014-07-16 15:18:30 -07001103 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -07001104 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001105 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -08001106 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
1107 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001108 }
1109 }
1110
Chris Wailes44bef6f2014-08-12 13:51:10 -07001111 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -08001112 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
1113 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001114 }
1115}
1116
Jason Sams709a0972012-11-15 18:18:04 -08001117
1118void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -07001119 const Allocation ** ains,
1120 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -08001121 Allocation * aout,
1122 const void * usr,
1123 uint32_t usrLen,
1124 const RsScriptCall *sc) {
1125
1126 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001127
1128 forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls);
1129 forEachKernelSetup(slot, &mtls);
1130
1131 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1132 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
1133 mCtx->setTLS(oldTLS);
1134}
1135
Jason Sams709a0972012-11-15 18:18:04 -08001136void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -08001137 mtls->script = this;
1138 mtls->fep.slot = slot;
Jason Sams110f1812013-03-14 16:02:18 -07001139#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001140 if (!is_skip_linkloader()) {
1141 rsAssert(slot < mExecutable->getExportForeachFuncAddrs().size());
1142 mtls->kernel = reinterpret_cast<ForEachFunc_t>(
1143 mExecutable->getExportForeachFuncAddrs()[slot]);
1144 rsAssert(mtls->kernel != nullptr);
1145 mtls->sig = mExecutable->getInfo().getExportForeachFuncs()[slot].second;
1146 }
1147 else {
1148 mtls->kernel = reinterpret_cast<ForEachFunc_t>(mForEachFunctions[slot]);
1149 rsAssert(mtls->kernel != nullptr);
1150 mtls->sig = mForEachSignatures[slot];
1151 }
Jason Sams110f1812013-03-14 16:02:18 -07001152#else
1153 mtls->kernel = reinterpret_cast<ForEachFunc_t>(mForEachFunctions[slot]);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001154 rsAssert(mtls->kernel != nullptr);
Jason Sams110f1812013-03-14 16:02:18 -07001155 mtls->sig = mForEachSignatures[slot];
1156#endif
Jason Sams709a0972012-11-15 18:18:04 -08001157}
1158
1159int RsdCpuScriptImpl::invokeRoot() {
1160 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1161 int ret = mRoot();
1162 mCtx->setTLS(oldTLS);
1163 return ret;
1164}
1165
1166void RsdCpuScriptImpl::invokeInit() {
1167 if (mInit) {
1168 mInit();
1169 }
1170}
1171
1172void RsdCpuScriptImpl::invokeFreeChildren() {
1173 if (mFreeChildren) {
1174 mFreeChildren();
1175 }
1176}
1177
1178void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
1179 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001180 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001181 void * ap = nullptr;
1182
1183#if defined(__x86_64__)
1184 // The invoked function could have input parameter of vector type for example float4 which
1185 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
1186 // So try to align void* params before passing them into RS exported function.
1187
1188 if ((uint8_t)(uint64_t)params & 0x0F) {
1189 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
1190 memcpy(ap, params, paramLength);
1191 } else {
1192 ALOGE("x86_64: invokeFunction memalign error, still use params which is not 16 bytes aligned.");
1193 }
1194 }
1195#endif
Jason Sams709a0972012-11-15 18:18:04 -08001196
1197 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Jason Sams110f1812013-03-14 16:02:18 -07001198#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001199 if (! is_skip_linkloader()) {
1200 reinterpret_cast<void (*)(const void *, uint32_t)>(
1201 mExecutable->getExportFuncAddrs()[slot])(
1202 ap? (const void *) ap : params, paramLength);
1203 }
1204 else {
1205 reinterpret_cast<void (*)(const void *, uint32_t)>(
1206 mInvokeFunctions[slot])(ap? (const void *) ap: params, paramLength);
1207 }
Jason Sams110f1812013-03-14 16:02:18 -07001208#else
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001209 reinterpret_cast<void (*)(const void *, uint32_t)>(
1210 mInvokeFunctions[slot])(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001211#endif
1212
Jason Sams709a0972012-11-15 18:18:04 -08001213 mCtx->setTLS(oldTLS);
1214}
1215
1216void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
1217 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001218 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -08001219
1220 //if (mIntrinsicID) {
1221 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
1222 //return;
1223 //}
1224
Jason Sams110f1812013-03-14 16:02:18 -07001225#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001226 int32_t *destPtr = nullptr;
1227 if (!is_skip_linkloader()) {
1228 destPtr = reinterpret_cast<int32_t *>(
1229 mExecutable->getExportVarAddrs()[slot]);
1230 }
1231 else {
1232 destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1233 }
Jason Sams110f1812013-03-14 16:02:18 -07001234#else
1235 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1236#endif
Jason Sams709a0972012-11-15 18:18:04 -08001237 if (!destPtr) {
1238 //ALOGV("Calling setVar on slot = %i which is null", slot);
1239 return;
1240 }
1241
1242 memcpy(destPtr, data, dataLength);
1243}
1244
Tim Murray9c642392013-04-11 13:29:59 -07001245void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
1246 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001247 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -07001248
1249#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001250 int32_t *srcPtr = nullptr;
1251 if (!is_skip_linkloader()) {
1252 srcPtr = reinterpret_cast<int32_t *>(
1253 mExecutable->getExportVarAddrs()[slot]);
1254 }
1255 else {
1256 srcPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1257 }
Tim Murray9c642392013-04-11 13:29:59 -07001258#else
1259 int32_t *srcPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1260#endif
1261 if (!srcPtr) {
1262 //ALOGV("Calling setVar on slot = %i which is null", slot);
1263 return;
1264 }
1265 memcpy(data, srcPtr, dataLength);
1266}
1267
1268
Jason Sams709a0972012-11-15 18:18:04 -08001269void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
1270 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -07001271 const uint32_t *dims, size_t dimLength) {
Jason Sams709a0972012-11-15 18:18:04 -08001272
Jason Sams110f1812013-03-14 16:02:18 -07001273#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001274 int32_t *destPtr = nullptr;
1275 if (!is_skip_linkloader()) {
1276 destPtr = reinterpret_cast<int32_t *>(
1277 mExecutable->getExportVarAddrs()[slot]);
1278 }
1279 else {
1280 destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1281 }
Jason Sams110f1812013-03-14 16:02:18 -07001282#else
1283 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1284#endif
Jason Sams709a0972012-11-15 18:18:04 -08001285 if (!destPtr) {
1286 //ALOGV("Calling setVar on slot = %i which is null", slot);
1287 return;
1288 }
1289
1290 // We want to look at dimension in terms of integer components,
1291 // but dimLength is given in terms of bytes.
1292 dimLength /= sizeof(int);
1293
1294 // Only a single dimension is currently supported.
1295 rsAssert(dimLength == 1);
1296 if (dimLength == 1) {
1297 // First do the increment loop.
1298 size_t stride = elem->getSizeBytes();
1299 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001300 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001301 elem->incRefs(cVal);
1302 cVal += stride;
1303 }
1304
1305 // Decrement loop comes after (to prevent race conditions).
1306 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001307 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001308 elem->decRefs(oldVal);
1309 oldVal += stride;
1310 }
1311 }
1312
1313 memcpy(destPtr, data, dataLength);
1314}
1315
1316void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
1317
1318 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001319 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001320
Jason Sams110f1812013-03-14 16:02:18 -07001321#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001322 int32_t *destPtr = nullptr;
1323 if (!is_skip_linkloader()) {
1324 destPtr = reinterpret_cast<int32_t *>(
1325 mExecutable->getExportVarAddrs()[slot]);
1326 }
1327 else {
1328 destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1329 }
Jason Sams110f1812013-03-14 16:02:18 -07001330#else
1331 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1332#endif
Jason Sams709a0972012-11-15 18:18:04 -08001333 if (!destPtr) {
1334 //ALOGV("Calling setVar on slot = %i which is null", slot);
1335 return;
1336 }
1337
Chris Wailes44bef6f2014-08-12 13:51:10 -07001338 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001339 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001340 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -08001341 ptr = data->mHal.drvState.lod[0].mallocPtr;
1342 }
1343 memcpy(destPtr, &ptr, sizeof(void *));
1344}
1345
1346void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
1347
1348 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001349 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001350
Jason Sams110f1812013-03-14 16:02:18 -07001351#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001352 int32_t *destPtr = nullptr;
1353 if (!is_skip_linkloader()) {
1354 destPtr = reinterpret_cast<int32_t *>(
1355 mExecutable->getExportVarAddrs()[slot]);
1356 }
1357 else {
1358 destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1359 }
Jason Sams110f1812013-03-14 16:02:18 -07001360#else
1361 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
1362#endif
Tim Murraye195a3f2014-03-13 15:04:58 -07001363
Jason Sams709a0972012-11-15 18:18:04 -08001364 if (!destPtr) {
1365 //ALOGV("Calling setVar on slot = %i which is null", slot);
1366 return;
1367 }
1368
Jason Sams05ef73f2014-08-05 14:59:22 -07001369 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -08001370}
1371
1372RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -07001373#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -08001374 if (mExecutable) {
Chris Wailes70d49712014-08-08 14:42:33 -07001375 std::vector<void *>::const_iterator var_addr_iter =
Jason Sams709a0972012-11-15 18:18:04 -08001376 mExecutable->getExportVarAddrs().begin();
Chris Wailes70d49712014-08-08 14:42:33 -07001377 std::vector<void *>::const_iterator var_addr_end =
Jason Sams709a0972012-11-15 18:18:04 -08001378 mExecutable->getExportVarAddrs().end();
1379
1380 bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_iter =
1381 mExecutable->getInfo().getObjectSlots().begin();
1382 bcc::RSInfo::ObjectSlotListTy::const_iterator is_object_end =
1383 mExecutable->getInfo().getObjectSlots().end();
1384
1385 while ((var_addr_iter != var_addr_end) &&
1386 (is_object_iter != is_object_end)) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001387 // The field address can be nullptr if the script-side has optimized
Jason Sams709a0972012-11-15 18:18:04 -08001388 // the corresponding global variable away.
Jason Sams05ef73f2014-08-05 14:59:22 -07001389 rs_object_base *obj_addr =
1390 reinterpret_cast<rs_object_base *>(*var_addr_iter);
Jason Sams709a0972012-11-15 18:18:04 -08001391 if (*is_object_iter) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001392 if (*var_addr_iter != nullptr && mCtx->getContext() != nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -08001393 rsrClearObject(mCtx->getContext(), obj_addr);
1394 }
1395 }
1396 var_addr_iter++;
1397 is_object_iter++;
1398 }
1399 }
1400
Jason Sams709a0972012-11-15 18:18:04 -08001401 if (mCompilerDriver) {
1402 delete mCompilerDriver;
1403 }
1404 if (mExecutable) {
1405 delete mExecutable;
1406 }
1407 if (mBoundAllocs) {
1408 delete[] mBoundAllocs;
1409 }
Tim Murraybee48d72014-06-13 12:44:47 -07001410
Tim Murray29809d12014-05-28 12:04:19 -07001411 for (size_t i = 0; i < mExportedForEachFuncList.size(); i++) {
1412 delete[] mExportedForEachFuncList[i].first;
1413 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001414
1415 if (mFieldIsObject) {
1416 for (size_t i = 0; i < mExportedVariableCount; ++i) {
1417 if (mFieldIsObject[i]) {
1418 if (mFieldAddress[i] != nullptr) {
1419 rs_object_base *obj_addr =
1420 reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
1421 rsrClearObject(mCtx->getContext(), obj_addr);
1422 }
1423 }
1424 }
1425 }
1426
1427 if (is_skip_linkloader()) {
1428 if (mInvokeFunctions) delete[] mInvokeFunctions;
1429 if (mForEachFunctions) delete[] mForEachFunctions;
1430 if (mFieldAddress) delete[] mFieldAddress;
1431 if (mFieldIsObject) delete[] mFieldIsObject;
1432 if (mForEachSignatures) delete[] mForEachSignatures;
1433 }
1434
Jason Sams110f1812013-03-14 16:02:18 -07001435#else
1436 if (mFieldIsObject) {
1437 for (size_t i = 0; i < mExportedVariableCount; ++i) {
1438 if (mFieldIsObject[i]) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001439 if (mFieldAddress[i] != nullptr) {
Jason Sams05ef73f2014-08-05 14:59:22 -07001440 rs_object_base *obj_addr =
1441 reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
Jason Sams110f1812013-03-14 16:02:18 -07001442 rsrClearObject(mCtx->getContext(), obj_addr);
1443 }
1444 }
1445 }
1446 }
1447
1448 if (mInvokeFunctions) delete[] mInvokeFunctions;
1449 if (mForEachFunctions) delete[] mForEachFunctions;
1450 if (mFieldAddress) delete[] mFieldAddress;
1451 if (mFieldIsObject) delete[] mFieldIsObject;
1452 if (mForEachSignatures) delete[] mForEachSignatures;
1453 if (mBoundAllocs) delete[] mBoundAllocs;
1454 if (mScriptSO) {
1455 dlclose(mScriptSO);
1456 }
1457#endif
Jason Sams709a0972012-11-15 18:18:04 -08001458}
1459
1460Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1461 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001462 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001463 }
1464
1465 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1466 Allocation *a = mBoundAllocs[ct];
1467 if (!a) continue;
1468 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1469 return a;
1470 }
1471 }
1472 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001473 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001474}
1475
Chris Wailesf3712132014-07-16 15:18:30 -07001476void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1477 uint32_t inLen, Allocation * aout,
1478 const void * usr, uint32_t usrLen,
1479 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001480
Chris Wailesf3712132014-07-16 15:18:30 -07001481void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1482 uint32_t inLen, Allocation * aout,
1483 const void * usr, uint32_t usrLen,
1484 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001485
Jason Sams709a0972012-11-15 18:18:04 -08001486
1487}
1488}