blob: 1c7fdac85519d3d72f42dde2da28b472e7b7cfdc [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>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070028 #include <bcinfo/MetadataExtractor.h>
Stephen Hinesba17ae42013-06-05 17:18:04 -070029 #include <cutils/properties.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070030
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080034
35 #include <string>
36 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070037#endif
Jason Sams709a0972012-11-15 18:18:04 -080038
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080039#include <set>
40#include <string>
41#include <dlfcn.h>
42#include <stdlib.h>
43#include <string.h>
44#include <fstream>
45#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 {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -070054
55// Create a len length string containing random characters from [A-Za-z0-9].
56static std::string getRandomString(size_t len) {
57 char buf[len + 1];
58 for (size_t i = 0; i < len; i++) {
59 uint32_t r = arc4random() & 0xffff;
60 r %= 62;
61 if (r < 26) {
62 // lowercase
63 buf[i] = 'a' + r;
64 } else if (r < 52) {
65 // uppercase
66 buf[i] = 'A' + (r - 26);
67 } else {
68 // Use a number
69 buf[i] = '0' + (r - 52);
70 }
71 }
72 buf[len] = '\0';
73 return std::string(buf);
74}
75
Stephen Hinesee48c0b2013-10-30 17:48:30 -070076// Check if a path exists and attempt to create it if it doesn't.
77static bool ensureCacheDirExists(const char *path) {
78 if (access(path, R_OK | W_OK | X_OK) == 0) {
79 // Done if we can rwx the directory
80 return true;
81 }
82 if (mkdir(path, 0700) == 0) {
83 return true;
84 }
85 return false;
86}
87
Stephen Hines7d774852014-10-01 12:57:57 -070088// Copy the file named \p srcFile to \p dstFile.
89// Return 0 on success and -1 if anything wasn't copied.
90static int copyFile(const char *dstFile, const char *srcFile) {
91 std::ifstream srcStream(srcFile);
92 if (!srcStream) {
93 ALOGE("Could not verify or read source file: %s", srcFile);
94 return -1;
95 }
96 std::ofstream dstStream(dstFile);
97 if (!dstStream) {
98 ALOGE("Could not verify or write destination file: %s", dstFile);
99 return -1;
100 }
101 dstStream << srcStream.rdbuf();
102 if (!dstStream) {
103 ALOGE("Could not write destination file: %s", dstFile);
104 return -1;
105 }
106
107 srcStream.close();
108 dstStream.close();
109
110 return 0;
111}
112
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800113static std::string findSharedObjectName(const char *cacheDir,
114 const char *resName) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700115#ifndef RS_SERVER
116 std::string scriptSOName(cacheDir);
Miao Wangf3213d72015-01-14 10:03:07 -0800117#if defined(RS_COMPATIBILITY_LIB) && !defined(__LP64__)
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700118 size_t cutPos = scriptSOName.rfind("cache");
119 if (cutPos != std::string::npos) {
120 scriptSOName.erase(cutPos);
121 } else {
122 ALOGE("Found peculiar cacheDir (missing \"cache\"): %s", cacheDir);
123 }
124 scriptSOName.append("/lib/librs.");
125#else
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800126 scriptSOName.append("/librs.");
Miao Wangf3213d72015-01-14 10:03:07 -0800127#endif // RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800128
129#else
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700130 std::string scriptSOName("lib");
Miao Wangf3213d72015-01-14 10:03:07 -0800131#endif // RS_SERVER
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700132 scriptSOName.append(resName);
133 scriptSOName.append(".so");
134
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800135 return scriptSOName;
136}
137
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800138#ifndef RS_COMPATIBILITY_LIB
139
Stephen Hinesba17ae42013-06-05 17:18:04 -0700140static bool is_force_recompile() {
141#ifdef RS_SERVER
142 return false;
143#else
144 char buf[PROPERTY_VALUE_MAX];
145
146 // Re-compile if floating point precision has been overridden.
147 property_get("debug.rs.precision", buf, "");
148 if (buf[0] != '\0') {
149 return true;
150 }
151
152 // Re-compile if debug.rs.forcerecompile is set.
153 property_get("debug.rs.forcerecompile", buf, "0");
154 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
155 return true;
156 } else {
157 return false;
158 }
159#endif // RS_SERVER
160}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700161
Chris Wailes6847e732014-08-11 17:30:51 -0700162static void setCompileArguments(std::vector<const char*>* args,
163 const std::string& bcFileName,
164 const char* cacheDir, const char* resName,
165 const char* core_lib, bool useRSDebugContext,
166 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700167 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -0800168 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -0800169 args->push_back("-unroll-runtime");
170 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700171 args->push_back("-o");
172 args->push_back(resName);
173 args->push_back("-output_path");
174 args->push_back(cacheDir);
175 args->push_back("-bclib");
176 args->push_back(core_lib);
177 args->push_back("-mtriple");
178 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
179
Tim Murray358ffb82014-12-09 11:53:06 -0800180 // Enable workaround for A53 codegen by default.
181#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
182 args->push_back("-aarch64-fix-cortex-a53-835769");
183#endif
184
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700185 // Execute the bcc compiler.
186 if (useRSDebugContext) {
187 args->push_back("-rs-debug-ctx");
188 } else {
189 // Only load additional libraries for compiles that don't use
190 // the debug context.
191 if (bccPluginName && strlen(bccPluginName) > 0) {
192 args->push_back("-load");
193 args->push_back(bccPluginName);
194 }
195 }
196
Stephen Hines45e753a2015-01-19 20:58:44 -0800197 args->push_back("-fPIC");
198 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800199
Chris Wailes6847e732014-08-11 17:30:51 -0700200 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700201 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700202}
203
Chris Wailes6847e732014-08-11 17:30:51 -0700204static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700205 const char *bitcode,
206 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700207 const char **compileArguments,
208 const std::string &compileCommandLine) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700209 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700210
Chris Wailes6847e732014-08-11 17:30:51 -0700211 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700212 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700213 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700214 return false;
215 }
216 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
217 fclose(bcfile);
218 if (nwritten != bitcodeSize) {
219 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700220 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700221 return false;
222 }
223
224 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800225
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700226 switch (pid) {
227 case -1: { // Error occurred (we attempt no recovery)
228 ALOGE("Couldn't fork for bcc compiler execution");
229 return false;
230 }
231 case 0: { // Child process
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700232 ALOGV("Invoking BCC with: %s", compileCommandLine.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800233 execv(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
234 (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800235
Stephen Hines00511322014-01-31 11:20:23 -0800236 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700237 abort();
238 return false;
239 }
240 default: { // Parent process (actual driver)
241 // Wait on child process to finish compiling the source.
242 int status = 0;
243 pid_t w = waitpid(pid, &status, 0);
244 if (w == -1) {
245 ALOGE("Could not wait for bcc compiler");
246 return false;
247 }
248
249 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
250 return true;
251 }
252
253 ALOGE("bcc compiler terminated unexpectedly");
254 return false;
255 }
256 }
257}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700258
Pirama Arumuga Nainar508b1af2015-02-19 10:46:29 -0800259std::string getCommandLine(int argc, const char* const* argv) {
260 std::string s;
261 for (int i = 0; i < argc; i++) {
262 if (i > 0) {
263 s += ' ';
264 }
265 s += argv[i];
266 }
267 return s;
268}
269
Yang Ni1c44cb62015-01-22 12:02:27 -0800270#endif // !defined(RS_COMPATIBILITY_LIB)
271} // namespace
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800272
Yang Ni1c44cb62015-01-22 12:02:27 -0800273namespace android {
274namespace renderscript {
275
276const char* SharedLibraryUtils::LD_EXE_PATH = "/system/bin/ld.mc";
277const char* SharedLibraryUtils::RS_CACHE_DIR = "com.android.renderscript.cache";
278
279#ifndef RS_COMPATIBILITY_LIB
280
281bool SharedLibraryUtils::createSharedLibrary(const char *cacheDir, const char *resName) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800282 std::string sharedLibName = findSharedObjectName(cacheDir, resName);
283 std::string objFileName = cacheDir;
284 objFileName.append("/");
285 objFileName.append(resName);
286 objFileName.append(".o");
287
288 const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
289 std::vector<const char *> args = {
290 LD_EXE_PATH,
291 "-shared",
292 "-nostdlib",
293 compiler_rt,
294 "-mtriple", DEFAULT_TARGET_TRIPLE_STRING,
295 "-L", SYSLIBPATH,
296 "-lRSDriver", "-lm", "-lc",
297 objFileName.c_str(),
298 "-o", sharedLibName.c_str(),
299 nullptr
300 };
301
Pirama Arumuga Nainar508b1af2015-02-19 10:46:29 -0800302 std::string cmdLineStr = getCommandLine(args.size()-1, args.data());
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800303
304 pid_t pid = fork();
305
306 switch (pid) {
307 case -1: { // Error occurred (we attempt no recovery)
308 ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH);
309 return false;
310 }
311 case 0: { // Child process
312 ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str());
313 execv(LD_EXE_PATH, (char* const*) args.data());
314
315 ALOGE("execv() failed: %s", strerror(errno));
316 abort();
317 return false;
318 }
319 default: { // Parent process (actual driver)
320 // Wait on child process to finish compiling the source.
321 int status = 0;
322 pid_t w = waitpid(pid, &status, 0);
323 if (w == -1) {
324 ALOGE("Could not wait for linker (%s)", LD_EXE_PATH);
325 return false;
326 }
327
328 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
329 return true;
330 }
331
332 ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH);
333 return false;
334 }
335 }
336}
Stephen Hinesba17ae42013-06-05 17:18:04 -0700337
Yang Ni1c44cb62015-01-22 12:02:27 -0800338#endif // RS_COMPATIBILITY_LIB
339
Miao Wangf3213d72015-01-14 10:03:07 -0800340
341void* SharedLibraryUtils::loadSharedLibrary(const char *cacheDir, const char *resName, const char *nativeLibDir) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800342 void *loaded = nullptr;
343
Miao Wangf3213d72015-01-14 10:03:07 -0800344#if defined(RS_COMPATIBILITY_LIB) && defined(__LP64__)
345 std::string scriptSOName = findSharedObjectName(nativeLibDir, resName);
346#else
Yang Ni1c44cb62015-01-22 12:02:27 -0800347 std::string scriptSOName = findSharedObjectName(cacheDir, resName);
Miao Wangf3213d72015-01-14 10:03:07 -0800348#endif
Yang Ni1c44cb62015-01-22 12:02:27 -0800349
350 // We should check if we can load the library from the standard app
351 // location for shared libraries first.
352 loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName);
353
354 if (loaded == nullptr) {
355 ALOGE("Unable to open shared library (%s): %s",
356 scriptSOName.c_str(), dlerror());
357
358#ifdef RS_COMPATIBILITY_LIB
359 // One final attempt to find the library in "/system/lib".
360 // We do this to allow bundled applications to use the compatibility
361 // library fallback path. Those applications don't have a private
362 // library path, so they need to install to the system directly.
363 // Note that this is really just a testing path.
364 std::string scriptSONameSystem("/system/lib/librs.");
365 scriptSONameSystem.append(resName);
366 scriptSONameSystem.append(".so");
367 loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir,
368 resName);
369 if (loaded == nullptr) {
370 ALOGE("Unable to open system shared library (%s): %s",
371 scriptSONameSystem.c_str(), dlerror());
372 }
373#endif
374 }
375
376 return loaded;
377}
378
379void* SharedLibraryUtils::loadSOHelper(const char *origName, const char *cacheDir,
380 const char *resName) {
381 // Keep track of which .so libraries have been loaded. Once a library is
382 // in the set (per-process granularity), we must instead make a copy of
383 // the original shared object (randomly named .so file) and load that one
384 // instead. If we don't do this, we end up aliasing global data between
385 // the various Script instances (which are supposed to be completely
386 // independent).
387 static std::set<std::string> LoadedLibraries;
388
389 void *loaded = nullptr;
390
391 // Skip everything if we don't even have the original library available.
392 if (access(origName, F_OK) != 0) {
393 return nullptr;
394 }
395
396 // Common path is that we have not loaded this Script/library before.
397 if (LoadedLibraries.find(origName) == LoadedLibraries.end()) {
398 loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL);
399 if (loaded) {
400 LoadedLibraries.insert(origName);
401 }
402 return loaded;
403 }
404
405 std::string newName(cacheDir);
406
407 // Append RS_CACHE_DIR only if it is not found in cacheDir
408 // In driver mode, RS_CACHE_DIR is already appended to cacheDir.
409 if (newName.find(RS_CACHE_DIR) == std::string::npos) {
410 newName.append("/");
411 newName.append(RS_CACHE_DIR);
412 newName.append("/");
413 }
414
415 if (!ensureCacheDirExists(newName.c_str())) {
416 ALOGE("Could not verify or create cache dir: %s", cacheDir);
417 return nullptr;
418 }
419
420 // Construct an appropriately randomized filename for the copy.
421 newName.append("librs.");
422 newName.append(resName);
423 newName.append("#");
424 newName.append(getRandomString(6)); // 62^6 potential filename variants.
425 newName.append(".so");
426
427 int r = copyFile(newName.c_str(), origName);
428 if (r != 0) {
429 ALOGE("Could not create copy %s -> %s", origName, newName.c_str());
430 return nullptr;
431 }
432 loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL);
433 r = unlink(newName.c_str());
434 if (r != 0) {
435 ALOGE("Could not unlink copy %s", newName.c_str());
436 }
437 if (loaded) {
438 LoadedLibraries.insert(newName.c_str());
439 }
440
441 return loaded;
442}
Jason Sams709a0972012-11-15 18:18:04 -0800443
Yang Nida0f0692015-01-12 13:03:40 -0800444const char* RsdCpuScriptImpl::BCC_EXE_PATH = "/system/bin/bcc";
445
Jason Sams110f1812013-03-14 16:02:18 -0700446#define MAXLINE 500
447#define MAKE_STR_HELPER(S) #S
448#define MAKE_STR(S) MAKE_STR_HELPER(S)
449#define EXPORT_VAR_STR "exportVarCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700450#define EXPORT_FUNC_STR "exportFuncCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700451#define EXPORT_FOREACH_STR "exportForEachCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700452#define OBJECT_SLOT_STR "objectSlotCount: "
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800453#define PRAGMA_STR "pragmaCount: "
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800454#define THREADABLE_STR "isThreadable: "
Jason Sams110f1812013-03-14 16:02:18 -0700455
456// Copy up to a newline or size chars from str -> s, updating str
Chris Wailes44bef6f2014-08-12 13:51:10 -0700457// Returns s when successful and nullptr when '\0' is finally reached.
Jason Sams110f1812013-03-14 16:02:18 -0700458static char* strgets(char *s, int size, const char **ppstr) {
459 if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700460 return nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700461 }
462
463 int i;
464 for (i = 0; i < (size - 1); i++) {
465 s[i] = **ppstr;
466 (*ppstr)++;
467 if (s[i] == '\0') {
468 return s;
469 } else if (s[i] == '\n') {
470 s[i+1] = '\0';
471 return s;
472 }
473 }
474
475 // size has been exceeded.
476 s[i] = '\0';
477
478 return s;
479}
Jason Sams709a0972012-11-15 18:18:04 -0800480
481RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
482 mCtx = ctx;
483 mScript = s;
484
Chris Wailes44bef6f2014-08-12 13:51:10 -0700485 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800486
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800487#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700488 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700489#endif
490
Tim Murraye195a3f2014-03-13 15:04:58 -0700491
Chris Wailes44bef6f2014-08-12 13:51:10 -0700492 mRoot = nullptr;
493 mRootExpand = nullptr;
494 mInit = nullptr;
495 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800496 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800497
Chris Wailes44bef6f2014-08-12 13:51:10 -0700498 mBoundAllocs = nullptr;
499 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800500 mIsThreadable = true;
501}
502
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800503bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800504 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
505 if (mRoot) {
506 //ALOGE("Found root(): %p", mRoot);
507 }
508 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
509 if (mRootExpand) {
510 //ALOGE("Found root.expand(): %p", mRootExpand);
511 }
512 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
513 if (mInit) {
514 //ALOGE("Found init(): %p", mInit);
515 }
516 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
517 if (mFreeChildren) {
518 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
519 }
520
Yang Nid9bae682015-01-20 15:31:15 -0800521 mScriptExec = ScriptExecutable::createFromSharedObject(
522 mCtx->getContext(), mScriptSO);
523
524 if (mScriptExec == nullptr) {
525 return false;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800526 }
527
Yang Nid9bae682015-01-20 15:31:15 -0800528 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800529 if (varCount > 0) {
530 mBoundAllocs = new Allocation *[varCount];
531 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
532 }
533
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800534 mIsThreadable = mScriptExec->getThreadable();
535 //ALOGE("Script isThreadable? %d", mIsThreadable);
536
Yang Nid9bae682015-01-20 15:31:15 -0800537 return true;
538}
539
540ScriptExecutable* ScriptExecutable::createFromSharedObject(
541 Context* RSContext, void* sharedObj) {
542 char line[MAXLINE];
543
544 size_t varCount = 0;
545 size_t funcCount = 0;
546 size_t forEachCount = 0;
547 size_t objectSlotCount = 0;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800548 size_t pragmaCount = 0;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800549 bool isThreadable = true;
Yang Nid9bae682015-01-20 15:31:15 -0800550
Yang Nie8f9fba2015-01-30 08:55:10 -0800551 void** fieldAddress = nullptr;
552 bool* fieldIsObject = nullptr;
553 InvokeFunc_t* invokeFunctions = nullptr;
554 ForEachFunc_t* forEachFunctions = nullptr;
555 uint32_t* forEachSignatures = nullptr;
556 const char ** pragmaKeys = nullptr;
557 const char ** pragmaValues = nullptr;
558
Yang Nid9bae682015-01-20 15:31:15 -0800559 const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info");
560
561 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
562 return nullptr;
563 }
564 if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
565 ALOGE("Invalid export var count!: %s", line);
566 return nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800567 }
568
Yang Nie8f9fba2015-01-30 08:55:10 -0800569 fieldAddress = new void*[varCount];
570 if (fieldAddress == nullptr) {
571 return nullptr;
572 }
573
574 fieldIsObject = new bool[varCount];
575 if (fieldIsObject == nullptr) {
576 goto error;
577 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800578
Yang Nid9bae682015-01-20 15:31:15 -0800579 for (size_t i = 0; i < varCount; ++i) {
580 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800581 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800582 }
583 char *c = strrchr(line, '\n');
584 if (c) {
585 *c = '\0';
586 }
587 void* addr = dlsym(sharedObj, line);
588 if (addr == nullptr) {
589 ALOGE("Failed to find variable address for %s: %s",
590 line, dlerror());
591 // Not a critical error if we don't find a global variable.
592 }
Yang Nie8f9fba2015-01-30 08:55:10 -0800593 fieldAddress[i] = addr;
594 fieldIsObject[i] = false;
Yang Nid9bae682015-01-20 15:31:15 -0800595 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800596
Yang Nid9bae682015-01-20 15:31:15 -0800597 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800598 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800599 }
600 if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
601 ALOGE("Invalid export func count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800602 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800603 }
604
Yang Nie8f9fba2015-01-30 08:55:10 -0800605 invokeFunctions = new InvokeFunc_t[funcCount];
606 if (invokeFunctions == nullptr) {
607 goto error;
608 }
Yang Nid9bae682015-01-20 15:31:15 -0800609
610 for (size_t i = 0; i < funcCount; ++i) {
611 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800612 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800613 }
614 char *c = strrchr(line, '\n');
615 if (c) {
616 *c = '\0';
617 }
618
619 invokeFunctions[i] = (InvokeFunc_t) dlsym(sharedObj, line);
620 if (invokeFunctions[i] == nullptr) {
621 ALOGE("Failed to get function address for %s(): %s",
622 line, dlerror());
Yang Nie8f9fba2015-01-30 08:55:10 -0800623 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800624 }
625 }
626
627 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800628 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800629 }
630 if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
631 ALOGE("Invalid export forEach count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800632 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800633 }
634
Yang Nie8f9fba2015-01-30 08:55:10 -0800635 forEachFunctions = new ForEachFunc_t[forEachCount];
636 if (forEachFunctions == nullptr) {
637 goto error;
638 }
639
640 forEachSignatures = new uint32_t[forEachCount];
641 if (forEachSignatures == nullptr) {
642 goto error;
643 }
Yang Nid9bae682015-01-20 15:31:15 -0800644
645 for (size_t i = 0; i < forEachCount; ++i) {
646 unsigned int tmpSig = 0;
647 char tmpName[MAXLINE];
648
649 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800650 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800651 }
652 if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
653 &tmpSig, tmpName) != 2) {
654 ALOGE("Invalid export forEach!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800655 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800656 }
657
658 // Lookup the expanded ForEach kernel.
659 strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
660 forEachSignatures[i] = tmpSig;
661 forEachFunctions[i] =
662 (ForEachFunc_t) dlsym(sharedObj, tmpName);
663 if (i != 0 && forEachFunctions[i] == nullptr) {
664 // Ignore missing root.expand functions.
665 // root() is always specified at location 0.
666 ALOGE("Failed to find forEach function address for %s: %s",
667 tmpName, dlerror());
Yang Nie8f9fba2015-01-30 08:55:10 -0800668 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800669 }
670 }
671
672 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800673 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800674 }
675 if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
676 ALOGE("Invalid object slot count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800677 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800678 }
679
Yang Nid9bae682015-01-20 15:31:15 -0800680 for (size_t i = 0; i < objectSlotCount; ++i) {
681 uint32_t varNum = 0;
682 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800683 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800684 }
685 if (sscanf(line, "%u", &varNum) != 1) {
686 ALOGE("Invalid object slot!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800687 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800688 }
689
690 if (varNum < varCount) {
691 fieldIsObject[varNum] = true;
692 }
693 }
694
Yang Nie8f9fba2015-01-30 08:55:10 -0800695#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800696 // Do not attempt to read pragmas or isThreadable flag in compat lib path.
697 // Neither is applicable for compat lib
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800698
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800699 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800700 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800701 }
702
703 if (sscanf(line, PRAGMA_STR "%zu", &pragmaCount) != 1) {
704 ALOGE("Invalid pragma count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800705 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800706 }
707
Yang Nie8f9fba2015-01-30 08:55:10 -0800708 pragmaKeys = new const char*[pragmaCount];
709 if (pragmaKeys == nullptr) {
710 goto error;
711 }
712
713 pragmaValues = new const char*[pragmaCount];
714 if (pragmaValues == nullptr) {
715 goto error;
716 }
717
718 bzero(pragmaKeys, sizeof(char*) * pragmaCount);
719 bzero(pragmaValues, sizeof(char*) * pragmaCount);
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800720
721 for (size_t i = 0; i < pragmaCount; ++i) {
722 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
723 ALOGE("Unable to read pragma at index %zu!", i);
Yang Nie8f9fba2015-01-30 08:55:10 -0800724 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800725 }
726
727 char key[MAXLINE];
728 char value[MAXLINE] = ""; // initialize in case value is empty
729
730 // pragmas can just have a key and no value. Only check to make sure
731 // that the key is not empty
732 if (sscanf(line, "%" MAKE_STR(MAXLINE) "s - %" MAKE_STR(MAXLINE) "s",
733 key, value) == 0 ||
734 strlen(key) == 0)
735 {
736 ALOGE("Invalid pragma value!: %s", line);
737
Yang Nie8f9fba2015-01-30 08:55:10 -0800738 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800739 }
740
741 char *pKey = new char[strlen(key)+1];
742 strcpy(pKey, key);
743 pragmaKeys[i] = pKey;
744
745 char *pValue = new char[strlen(value)+1];
746 strcpy(pValue, value);
747 pragmaValues[i] = pValue;
748 //ALOGE("Pragma %zu: Key: '%s' Value: '%s'", i, pKey, pValue);
749 }
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800750
751 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800752 goto error;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800753 }
754
755 char tmpFlag[4];
756 if (sscanf(line, THREADABLE_STR "%4s", tmpFlag) != 1) {
757 ALOGE("Invalid threadable flag!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800758 goto error;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800759 }
Yang Nie8f9fba2015-01-30 08:55:10 -0800760 if (strcmp(tmpFlag, "yes") == 0) {
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800761 isThreadable = true;
Yang Nie8f9fba2015-01-30 08:55:10 -0800762 } else if (strcmp(tmpFlag, "no") == 0) {
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800763 isThreadable = false;
Yang Nie8f9fba2015-01-30 08:55:10 -0800764 } else {
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800765 ALOGE("Invalid threadable flag!: %s", tmpFlag);
Yang Nie8f9fba2015-01-30 08:55:10 -0800766 goto error;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800767 }
768
Yang Nie8f9fba2015-01-30 08:55:10 -0800769#endif // RS_COMPATIBILITY_LIB
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800770
Yang Nid9bae682015-01-20 15:31:15 -0800771 return new ScriptExecutable(
Yang Nie8f9fba2015-01-30 08:55:10 -0800772 RSContext, fieldAddress, fieldIsObject, varCount,
773 invokeFunctions, funcCount,
774 forEachFunctions, forEachSignatures, forEachCount,
775 pragmaKeys, pragmaValues, pragmaCount,
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800776 isThreadable);
Yang Nie8f9fba2015-01-30 08:55:10 -0800777
778error:
779
780#ifndef RS_COMPATIBILITY_LIB
781 for (size_t idx = 0; idx < pragmaCount; ++idx) {
Yang Nida0f0692015-01-12 13:03:40 -0800782 delete [] pragmaKeys[idx];
783 delete [] pragmaValues[idx];
Yang Nie8f9fba2015-01-30 08:55:10 -0800784 }
785
786 delete[] pragmaValues;
787 delete[] pragmaKeys;
788#endif // RS_COMPATIBILITY_LIB
789
790 delete[] forEachSignatures;
791 delete[] forEachFunctions;
792 delete[] invokeFunctions;
793 delete[] fieldIsObject;
794 delete[] fieldAddress;
795
796 return nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800797}
798
Jason Sams709a0972012-11-15 18:18:04 -0800799bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
800 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800801 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800802 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
803 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800804 //ALOGE("rsdScriptInit %p %p", rsc, script);
805
806 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700807#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800808 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800809
Chris Wailes44bef6f2014-08-12 13:51:10 -0700810 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800811
Jason Sams709a0972012-11-15 18:18:04 -0800812 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700813 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800814 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
815 mCtx->unlockMutex();
816 return false;
817 }
818
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700819 // Run any compiler setup functions we have been provided with.
820 RSSetupCompilerCallback setupCompilerCallback =
821 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700822 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700823 setupCompilerCallback(mCompilerDriver);
824 }
825
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700826 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
827 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700828 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700829 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700830 return false;
831 }
832
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700833 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700834
835 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700836 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800837 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700838 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700839
Chris Wailes6847e732014-08-11 17:30:51 -0700840 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700841 bcFileName.append("/");
842 bcFileName.append(resName);
843 bcFileName.append(".bc");
844
845 std::vector<const char*> compileArguments;
846 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
847 useRSDebugContext, bccPluginName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700848 // The last argument of compileArguments ia a nullptr, so remove 1 from the size.
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700849 std::string compileCommandLine =
Pirama Arumuga Nainar508b1af2015-02-19 10:46:29 -0800850 getCommandLine(compileArguments.size() - 1, compileArguments.data());
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700851
Tim Murraybf96a522015-01-23 15:37:03 -0800852 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800853 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700854 }
855
856 // If we can't, it's either not there or out of date. We compile the bit code and try loading
857 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800858 if (mScriptSO == nullptr) {
859 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
860 compileArguments.data(), compileCommandLine))
861 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700862 ALOGE("bcc: FAILS to compile '%s'", resName);
863 mCtx->unlockMutex();
864 return false;
865 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800866
Yang Ni1c44cb62015-01-22 12:02:27 -0800867 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800868 ALOGE("Linker: Failed to link object file '%s'", resName);
869 mCtx->unlockMutex();
870 return false;
871 }
872
Yang Ni1c44cb62015-01-22 12:02:27 -0800873 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800874 if (mScriptSO == nullptr) {
875 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700876 mCtx->unlockMutex();
877 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700878 }
879 }
Jason Sams709a0972012-11-15 18:18:04 -0800880
Yang Nic31585b2015-02-15 11:43:50 -0800881 mBitcodeFilePath.setTo(bcFileName.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800882
Stephen Hines45e753a2015-01-19 20:58:44 -0800883 // Read RS symbol information from the .so.
884 if ( !mScriptSO) {
885 goto error;
Jason Sams709a0972012-11-15 18:18:04 -0800886 }
887
Stephen Hines45e753a2015-01-19 20:58:44 -0800888 if ( !storeRSInfoFromSO()) {
889 goto error;
Tim Murray29809d12014-05-28 12:04:19 -0700890 }
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700891#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800892 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
893 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700894
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800895 if (!mScriptSO) {
896 goto error;
897 }
Jason Sams110f1812013-03-14 16:02:18 -0700898
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800899 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700900 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700901 }
902#endif
Jason Sams709a0972012-11-15 18:18:04 -0800903 mCtx->unlockMutex();
904 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700905
Jason Sams110f1812013-03-14 16:02:18 -0700906error:
907
908 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700909 if (mScriptSO) {
910 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800911 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700912 }
913 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800914}
915
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700916#ifndef RS_COMPATIBILITY_LIB
917
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700918const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
919 size_t bitcodeSize) {
920 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
921
922 // If we're debugging, use the debug library.
923 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
924 return SYSLIBPATH"/libclcore_debug.bc";
925 }
926
927 // If a callback has been registered to specify a library, use that.
928 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700929 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700930 return selectRTCallback((const char*)bitcode, bitcodeSize);
931 }
932
933 // Check for a platform specific library
934#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
935 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700936 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700937 // NEON-capable ARMv7a devices can use an accelerated math library
938 // for all reduced precision scripts.
939 // ARMv8 does not use NEON, as ASIMD can be used with all precision
940 // levels.
941 return SYSLIBPATH"/libclcore_neon.bc";
942 } else {
943 return defaultLib;
944 }
945#elif defined(__i386__) || defined(__x86_64__)
946 // x86 devices will use an optimized library.
947 return SYSLIBPATH"/libclcore_x86.bc";
948#else
949 return defaultLib;
950#endif
951}
952
953#endif
954
Jason Sams709a0972012-11-15 18:18:04 -0800955void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700956 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800957 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
958 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800959 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800960 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
961 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700962
963 // Bug, need to stash in metadata
964 if (mRootExpand) {
965 script->mHal.info.root = mRootExpand;
966 } else {
967 script->mHal.info.root = mRoot;
968 }
Jason Sams709a0972012-11-15 18:18:04 -0800969}
970
Jason Sams709a0972012-11-15 18:18:04 -0800971
972typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
973
Jason Samsbf2111d2015-01-26 18:13:41 -0800974bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700975 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700976 Allocation * aout,
977 const void * usr, uint32_t usrLen,
978 const RsScriptCall *sc,
979 MTLaunchStruct *mtls) {
980
981 memset(mtls, 0, sizeof(MTLaunchStruct));
982
Chris Wailesf3712132014-07-16 15:18:30 -0700983 for (int index = inLen; --index >= 0;) {
984 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700985
Chris Wailesf3712132014-07-16 15:18:30 -0700986 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700987 if (ain != nullptr &&
988 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
989
Chris Wailesf3712132014-07-16 15:18:30 -0700990 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
991 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800992 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700993 }
994 }
995
Chris Wailes44bef6f2014-08-12 13:51:10 -0700996 if (aout &&
997 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
998
Chris Wailesf3712132014-07-16 15:18:30 -0700999 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1000 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -08001001 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001002 }
1003
Chris Wailesf3712132014-07-16 15:18:30 -07001004 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001005 const Allocation *ain0 = ains[0];
1006 const Type *inType = ain0->getType();
1007
Jason Samsc0d68472015-01-20 14:29:52 -08001008 mtls->fep.dim.x = inType->getDimX();
1009 mtls->fep.dim.y = inType->getDimY();
1010 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001011
1012 for (int Index = inLen; --Index >= 1;) {
1013 if (!ain0->hasSameDims(ains[Index])) {
1014 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -08001015 "Failed to launch kernel; dimensions of input and output"
1016 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001017
Jason Samsbf2111d2015-01-26 18:13:41 -08001018 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001019 }
1020 }
1021
Chris Wailes44bef6f2014-08-12 13:51:10 -07001022 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001023 const Type *outType = aout->getType();
1024
Jason Samsc0d68472015-01-20 14:29:52 -08001025 mtls->fep.dim.x = outType->getDimX();
1026 mtls->fep.dim.y = outType->getDimY();
1027 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001028
1029 } else {
Chris Wailesf3712132014-07-16 15:18:30 -07001030 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1031 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -08001032 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001033 }
1034
Chris Wailes44bef6f2014-08-12 13:51:10 -07001035 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001036 if (!ains[0]->hasSameDims(aout)) {
1037 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1038 "Failed to launch kernel; dimensions of input and output allocations do not match.");
1039
Jason Samsbf2111d2015-01-26 18:13:41 -08001040 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001041 }
1042 }
1043
1044 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -08001045 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001046 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -08001047 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
1048 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
1049 if (mtls->start.x >= mtls->end.x) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001050 }
1051
1052 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -08001053 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001054 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -08001055 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
1056 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
1057 if (mtls->start.y >= mtls->end.y) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001058 }
1059
1060 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -08001061 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001062 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -08001063 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
1064 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
1065 if (mtls->start.z >= mtls->end.z) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001066 }
1067
Jason Samsbf2111d2015-01-26 18:13:41 -08001068 if (!sc || (sc->arrayEnd == 0)) {
1069 mtls->end.array[0] = mtls->fep.dim.array[0];
1070 } else {
1071 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
1072 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
1073 if (mtls->start.array[0] >= mtls->end.array[0]) return false;
1074 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001075
Jason Samsbf2111d2015-01-26 18:13:41 -08001076 if (!sc || (sc->array2End == 0)) {
1077 mtls->end.array[1] = mtls->fep.dim.array[1];
1078 } else {
1079 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
1080 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
1081 if (mtls->start.array[1] >= mtls->end.array[1]) return false;
1082 }
1083
1084 if (!sc || (sc->array3End == 0)) {
1085 mtls->end.array[2] = mtls->fep.dim.array[2];
1086 } else {
1087 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
1088 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
1089 if (mtls->start.array[2] >= mtls->end.array[2]) return false;
1090 }
1091
1092 if (!sc || (sc->array4End == 0)) {
1093 mtls->end.array[3] = mtls->fep.dim.array[3];
1094 } else {
1095 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
1096 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
1097 if (mtls->start.array[3] >= mtls->end.array[3]) return false;
1098 }
1099
1100
1101 // The X & Y walkers always want 0-1 min even if dim is not present
1102 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
1103 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001104
1105 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -08001106 if (ains) {
1107 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
1108 }
1109 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001110 mtls->fep.usr = usr;
1111 mtls->fep.usrLen = usrLen;
1112 mtls->mSliceSize = 1;
1113 mtls->mSliceNum = 0;
1114
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001115 mtls->isThreadable = mIsThreadable;
1116
Chris Wailesf3712132014-07-16 15:18:30 -07001117 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -07001118 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001119 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -08001120 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
1121 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001122 }
1123 }
1124
Chris Wailes44bef6f2014-08-12 13:51:10 -07001125 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -08001126 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
1127 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001128 }
Jason Samsbf2111d2015-01-26 18:13:41 -08001129
1130 // All validation passed, ok to launch threads
1131 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001132}
1133
Jason Sams709a0972012-11-15 18:18:04 -08001134
1135void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -07001136 const Allocation ** ains,
1137 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -08001138 Allocation * aout,
1139 const void * usr,
1140 uint32_t usrLen,
1141 const RsScriptCall *sc) {
1142
1143 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001144
Jason Samsbf2111d2015-01-26 18:13:41 -08001145 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
1146 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001147
Jason Samsbf2111d2015-01-26 18:13:41 -08001148 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1149 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
1150 mCtx->setTLS(oldTLS);
1151 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001152}
1153
Jason Sams709a0972012-11-15 18:18:04 -08001154void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -08001155 mtls->script = this;
1156 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -08001157 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001158 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -08001159 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -08001160}
1161
1162int RsdCpuScriptImpl::invokeRoot() {
1163 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1164 int ret = mRoot();
1165 mCtx->setTLS(oldTLS);
1166 return ret;
1167}
1168
1169void RsdCpuScriptImpl::invokeInit() {
1170 if (mInit) {
1171 mInit();
1172 }
1173}
1174
1175void RsdCpuScriptImpl::invokeFreeChildren() {
1176 if (mFreeChildren) {
1177 mFreeChildren();
1178 }
1179}
1180
1181void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
1182 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001183 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001184 void * ap = nullptr;
1185
1186#if defined(__x86_64__)
1187 // The invoked function could have input parameter of vector type for example float4 which
1188 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
1189 // So try to align void* params before passing them into RS exported function.
1190
1191 if ((uint8_t)(uint64_t)params & 0x0F) {
1192 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
1193 memcpy(ap, params, paramLength);
1194 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -08001195 ALOGE("x86_64: invokeFunction memalign error, still use params which"
1196 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +08001197 }
1198 }
1199#endif
Jason Sams709a0972012-11-15 18:18:04 -08001200
1201 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001202 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -08001203 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001204
Jason Sams709a0972012-11-15 18:18:04 -08001205 mCtx->setTLS(oldTLS);
1206}
1207
1208void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
1209 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001210 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -08001211
1212 //if (mIntrinsicID) {
1213 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
1214 //return;
1215 //}
1216
Yang Nid9bae682015-01-20 15:31:15 -08001217 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001218 if (!destPtr) {
1219 //ALOGV("Calling setVar on slot = %i which is null", slot);
1220 return;
1221 }
1222
1223 memcpy(destPtr, data, dataLength);
1224}
1225
Tim Murray9c642392013-04-11 13:29:59 -07001226void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
1227 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001228 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -07001229
Yang Nid9bae682015-01-20 15:31:15 -08001230 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -07001231 if (!srcPtr) {
1232 //ALOGV("Calling setVar on slot = %i which is null", slot);
1233 return;
1234 }
1235 memcpy(data, srcPtr, dataLength);
1236}
1237
1238
Jason Sams709a0972012-11-15 18:18:04 -08001239void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
1240 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -07001241 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -08001242 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001243 if (!destPtr) {
1244 //ALOGV("Calling setVar on slot = %i which is null", slot);
1245 return;
1246 }
1247
1248 // We want to look at dimension in terms of integer components,
1249 // but dimLength is given in terms of bytes.
1250 dimLength /= sizeof(int);
1251
1252 // Only a single dimension is currently supported.
1253 rsAssert(dimLength == 1);
1254 if (dimLength == 1) {
1255 // First do the increment loop.
1256 size_t stride = elem->getSizeBytes();
1257 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001258 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001259 elem->incRefs(cVal);
1260 cVal += stride;
1261 }
1262
1263 // Decrement loop comes after (to prevent race conditions).
1264 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001265 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001266 elem->decRefs(oldVal);
1267 oldVal += stride;
1268 }
1269 }
1270
1271 memcpy(destPtr, data, dataLength);
1272}
1273
1274void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
1275
1276 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001277 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001278
Yang Nid9bae682015-01-20 15:31:15 -08001279 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001280 if (!destPtr) {
1281 //ALOGV("Calling setVar on slot = %i which is null", slot);
1282 return;
1283 }
1284
Chris Wailes44bef6f2014-08-12 13:51:10 -07001285 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001286 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001287 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -08001288 ptr = data->mHal.drvState.lod[0].mallocPtr;
1289 }
1290 memcpy(destPtr, &ptr, sizeof(void *));
1291}
1292
1293void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
1294
1295 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001296 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001297
Yang Nid9bae682015-01-20 15:31:15 -08001298 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001299 if (!destPtr) {
1300 //ALOGV("Calling setVar on slot = %i which is null", slot);
1301 return;
1302 }
1303
Jason Sams05ef73f2014-08-05 14:59:22 -07001304 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -08001305}
1306
1307RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -07001308#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -08001309 if (mCompilerDriver) {
1310 delete mCompilerDriver;
1311 }
Stephen Hines45e753a2015-01-19 20:58:44 -08001312#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001313
Yang Nid9bae682015-01-20 15:31:15 -08001314 if (mScriptExec != nullptr) {
1315 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001316 }
Jason Sams110f1812013-03-14 16:02:18 -07001317 if (mBoundAllocs) delete[] mBoundAllocs;
1318 if (mScriptSO) {
1319 dlclose(mScriptSO);
1320 }
Jason Sams709a0972012-11-15 18:18:04 -08001321}
1322
1323Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1324 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001325 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001326 }
1327
1328 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1329 Allocation *a = mBoundAllocs[ct];
1330 if (!a) continue;
1331 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1332 return a;
1333 }
1334 }
1335 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001336 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001337}
1338
Chris Wailesf3712132014-07-16 15:18:30 -07001339void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1340 uint32_t inLen, Allocation * aout,
1341 const void * usr, uint32_t usrLen,
1342 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001343
Chris Wailesf3712132014-07-16 15:18:30 -07001344void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1345 uint32_t inLen, Allocation * aout,
1346 const void * usr, uint32_t usrLen,
1347 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001348
Jason Sams709a0972012-11-15 18:18:04 -08001349
1350}
1351}