blob: a618a17632331fa471ea6fbc4b0097c9b991add2 [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>
Jason Sams110f1812013-03-14 16:02:18 -070028 #include <bcc/Renderscript/RSInfo.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070029 #include <bcinfo/MetadataExtractor.h>
Stephen Hinesba17ae42013-06-05 17:18:04 -070030 #include <cutils/properties.h>
Stephen Hinesb58d9ad2013-06-19 19:26:19 -070031
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
Stephen Hines00511322014-01-31 11:20:23 -080035
36 #include <string>
37 #include <vector>
Jason Sams110f1812013-03-14 16:02:18 -070038#endif
Jason Sams709a0972012-11-15 18:18:04 -080039
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -080040#include <set>
41#include <string>
42#include <dlfcn.h>
43#include <stdlib.h>
44#include <string.h>
45#include <fstream>
46#include <iostream>
47
48#ifdef __LP64__
49#define SYSLIBPATH "/system/lib64"
50#else
51#define SYSLIBPATH "/system/lib"
52#endif
53
Stephen Hinesba17ae42013-06-05 17:18:04 -070054namespace {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -070055
56// Create a len length string containing random characters from [A-Za-z0-9].
57static std::string getRandomString(size_t len) {
58 char buf[len + 1];
59 for (size_t i = 0; i < len; i++) {
60 uint32_t r = arc4random() & 0xffff;
61 r %= 62;
62 if (r < 26) {
63 // lowercase
64 buf[i] = 'a' + r;
65 } else if (r < 52) {
66 // uppercase
67 buf[i] = 'A' + (r - 26);
68 } else {
69 // Use a number
70 buf[i] = '0' + (r - 52);
71 }
72 }
73 buf[len] = '\0';
74 return std::string(buf);
75}
76
Stephen Hinesee48c0b2013-10-30 17:48:30 -070077// Check if a path exists and attempt to create it if it doesn't.
78static bool ensureCacheDirExists(const char *path) {
79 if (access(path, R_OK | W_OK | X_OK) == 0) {
80 // Done if we can rwx the directory
81 return true;
82 }
83 if (mkdir(path, 0700) == 0) {
84 return true;
85 }
86 return false;
87}
88
Stephen Hines7d774852014-10-01 12:57:57 -070089// Copy the file named \p srcFile to \p dstFile.
90// Return 0 on success and -1 if anything wasn't copied.
91static int copyFile(const char *dstFile, const char *srcFile) {
92 std::ifstream srcStream(srcFile);
93 if (!srcStream) {
94 ALOGE("Could not verify or read source file: %s", srcFile);
95 return -1;
96 }
97 std::ofstream dstStream(dstFile);
98 if (!dstStream) {
99 ALOGE("Could not verify or write destination file: %s", dstFile);
100 return -1;
101 }
102 dstStream << srcStream.rdbuf();
103 if (!dstStream) {
104 ALOGE("Could not write destination file: %s", dstFile);
105 return -1;
106 }
107
108 srcStream.close();
109 dstStream.close();
110
111 return 0;
112}
113
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800114static std::string findSharedObjectName(const char *cacheDir,
115 const char *resName) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700116#ifndef RS_SERVER
117 std::string scriptSOName(cacheDir);
Miao Wangf3213d72015-01-14 10:03:07 -0800118#if defined(RS_COMPATIBILITY_LIB) && !defined(__LP64__)
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700119 size_t cutPos = scriptSOName.rfind("cache");
120 if (cutPos != std::string::npos) {
121 scriptSOName.erase(cutPos);
122 } else {
123 ALOGE("Found peculiar cacheDir (missing \"cache\"): %s", cacheDir);
124 }
125 scriptSOName.append("/lib/librs.");
126#else
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800127 scriptSOName.append("/librs.");
Miao Wangf3213d72015-01-14 10:03:07 -0800128#endif // RS_COMPATIBILITY_LIB
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800129
130#else
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700131 std::string scriptSOName("lib");
Miao Wangf3213d72015-01-14 10:03:07 -0800132#endif // RS_SERVER
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700133 scriptSOName.append(resName);
134 scriptSOName.append(".so");
135
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800136 return scriptSOName;
137}
138
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800139#ifndef RS_COMPATIBILITY_LIB
140
Stephen Hinesba17ae42013-06-05 17:18:04 -0700141static bool is_force_recompile() {
142#ifdef RS_SERVER
143 return false;
144#else
145 char buf[PROPERTY_VALUE_MAX];
146
147 // Re-compile if floating point precision has been overridden.
148 property_get("debug.rs.precision", buf, "");
149 if (buf[0] != '\0') {
150 return true;
151 }
152
153 // Re-compile if debug.rs.forcerecompile is set.
154 property_get("debug.rs.forcerecompile", buf, "0");
155 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
156 return true;
157 } else {
158 return false;
159 }
160#endif // RS_SERVER
161}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700162
Chris Wailes6847e732014-08-11 17:30:51 -0700163static void setCompileArguments(std::vector<const char*>* args,
164 const std::string& bcFileName,
165 const char* cacheDir, const char* resName,
166 const char* core_lib, bool useRSDebugContext,
167 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700168 rsAssert(cacheDir && resName && core_lib);
Yang Nida0f0692015-01-12 13:03:40 -0800169 args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -0800170 args->push_back("-unroll-runtime");
171 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700172 args->push_back("-o");
173 args->push_back(resName);
174 args->push_back("-output_path");
175 args->push_back(cacheDir);
176 args->push_back("-bclib");
177 args->push_back(core_lib);
178 args->push_back("-mtriple");
179 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
180
Tim Murray358ffb82014-12-09 11:53:06 -0800181 // Enable workaround for A53 codegen by default.
182#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
183 args->push_back("-aarch64-fix-cortex-a53-835769");
184#endif
185
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700186 // Execute the bcc compiler.
187 if (useRSDebugContext) {
188 args->push_back("-rs-debug-ctx");
189 } else {
190 // Only load additional libraries for compiles that don't use
191 // the debug context.
192 if (bccPluginName && strlen(bccPluginName) > 0) {
193 args->push_back("-load");
194 args->push_back(bccPluginName);
195 }
196 }
197
Stephen Hines45e753a2015-01-19 20:58:44 -0800198 args->push_back("-fPIC");
199 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800200
Chris Wailes6847e732014-08-11 17:30:51 -0700201 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700202 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700203}
204
Chris Wailes6847e732014-08-11 17:30:51 -0700205static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700206 const char *bitcode,
207 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700208 const char **compileArguments,
209 const std::string &compileCommandLine) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700210 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700211
Chris Wailes6847e732014-08-11 17:30:51 -0700212 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700213 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700214 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700215 return false;
216 }
217 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
218 fclose(bcfile);
219 if (nwritten != bitcodeSize) {
220 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700221 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700222 return false;
223 }
224
225 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800226
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700227 switch (pid) {
228 case -1: { // Error occurred (we attempt no recovery)
229 ALOGE("Couldn't fork for bcc compiler execution");
230 return false;
231 }
232 case 0: { // Child process
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700233 ALOGV("Invoking BCC with: %s", compileCommandLine.c_str());
Yang Nida0f0692015-01-12 13:03:40 -0800234 execv(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH,
235 (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800236
Stephen Hines00511322014-01-31 11:20:23 -0800237 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700238 abort();
239 return false;
240 }
241 default: { // Parent process (actual driver)
242 // Wait on child process to finish compiling the source.
243 int status = 0;
244 pid_t w = waitpid(pid, &status, 0);
245 if (w == -1) {
246 ALOGE("Could not wait for bcc compiler");
247 return false;
248 }
249
250 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
251 return true;
252 }
253
254 ALOGE("bcc compiler terminated unexpectedly");
255 return false;
256 }
257 }
258}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700259
Yang Ni1c44cb62015-01-22 12:02:27 -0800260#endif // !defined(RS_COMPATIBILITY_LIB)
261} // namespace
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800262
Yang Ni1c44cb62015-01-22 12:02:27 -0800263namespace android {
264namespace renderscript {
265
266const char* SharedLibraryUtils::LD_EXE_PATH = "/system/bin/ld.mc";
267const char* SharedLibraryUtils::RS_CACHE_DIR = "com.android.renderscript.cache";
268
269#ifndef RS_COMPATIBILITY_LIB
270
271bool SharedLibraryUtils::createSharedLibrary(const char *cacheDir, const char *resName) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800272 std::string sharedLibName = findSharedObjectName(cacheDir, resName);
273 std::string objFileName = cacheDir;
274 objFileName.append("/");
275 objFileName.append(resName);
276 objFileName.append(".o");
277
278 const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
279 std::vector<const char *> args = {
280 LD_EXE_PATH,
281 "-shared",
282 "-nostdlib",
283 compiler_rt,
284 "-mtriple", DEFAULT_TARGET_TRIPLE_STRING,
285 "-L", SYSLIBPATH,
286 "-lRSDriver", "-lm", "-lc",
287 objFileName.c_str(),
288 "-o", sharedLibName.c_str(),
289 nullptr
290 };
291
292 std::string cmdLineStr = bcc::getCommandLine(args.size()-1, args.data());
293
294 pid_t pid = fork();
295
296 switch (pid) {
297 case -1: { // Error occurred (we attempt no recovery)
298 ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH);
299 return false;
300 }
301 case 0: { // Child process
302 ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str());
303 execv(LD_EXE_PATH, (char* const*) args.data());
304
305 ALOGE("execv() failed: %s", strerror(errno));
306 abort();
307 return false;
308 }
309 default: { // Parent process (actual driver)
310 // Wait on child process to finish compiling the source.
311 int status = 0;
312 pid_t w = waitpid(pid, &status, 0);
313 if (w == -1) {
314 ALOGE("Could not wait for linker (%s)", LD_EXE_PATH);
315 return false;
316 }
317
318 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
319 return true;
320 }
321
322 ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH);
323 return false;
324 }
325 }
326}
Stephen Hinesba17ae42013-06-05 17:18:04 -0700327
Yang Ni1c44cb62015-01-22 12:02:27 -0800328#endif // RS_COMPATIBILITY_LIB
329
Miao Wangf3213d72015-01-14 10:03:07 -0800330
331void* SharedLibraryUtils::loadSharedLibrary(const char *cacheDir, const char *resName, const char *nativeLibDir) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800332 void *loaded = nullptr;
333
Miao Wangf3213d72015-01-14 10:03:07 -0800334#if defined(RS_COMPATIBILITY_LIB) && defined(__LP64__)
335 std::string scriptSOName = findSharedObjectName(nativeLibDir, resName);
336#else
Yang Ni1c44cb62015-01-22 12:02:27 -0800337 std::string scriptSOName = findSharedObjectName(cacheDir, resName);
Miao Wangf3213d72015-01-14 10:03:07 -0800338#endif
Yang Ni1c44cb62015-01-22 12:02:27 -0800339
340 // We should check if we can load the library from the standard app
341 // location for shared libraries first.
342 loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName);
343
344 if (loaded == nullptr) {
345 ALOGE("Unable to open shared library (%s): %s",
346 scriptSOName.c_str(), dlerror());
347
348#ifdef RS_COMPATIBILITY_LIB
349 // One final attempt to find the library in "/system/lib".
350 // We do this to allow bundled applications to use the compatibility
351 // library fallback path. Those applications don't have a private
352 // library path, so they need to install to the system directly.
353 // Note that this is really just a testing path.
354 std::string scriptSONameSystem("/system/lib/librs.");
355 scriptSONameSystem.append(resName);
356 scriptSONameSystem.append(".so");
357 loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir,
358 resName);
359 if (loaded == nullptr) {
360 ALOGE("Unable to open system shared library (%s): %s",
361 scriptSONameSystem.c_str(), dlerror());
362 }
363#endif
364 }
365
366 return loaded;
367}
368
369void* SharedLibraryUtils::loadSOHelper(const char *origName, const char *cacheDir,
370 const char *resName) {
371 // Keep track of which .so libraries have been loaded. Once a library is
372 // in the set (per-process granularity), we must instead make a copy of
373 // the original shared object (randomly named .so file) and load that one
374 // instead. If we don't do this, we end up aliasing global data between
375 // the various Script instances (which are supposed to be completely
376 // independent).
377 static std::set<std::string> LoadedLibraries;
378
379 void *loaded = nullptr;
380
381 // Skip everything if we don't even have the original library available.
382 if (access(origName, F_OK) != 0) {
383 return nullptr;
384 }
385
386 // Common path is that we have not loaded this Script/library before.
387 if (LoadedLibraries.find(origName) == LoadedLibraries.end()) {
388 loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL);
389 if (loaded) {
390 LoadedLibraries.insert(origName);
391 }
392 return loaded;
393 }
394
395 std::string newName(cacheDir);
396
397 // Append RS_CACHE_DIR only if it is not found in cacheDir
398 // In driver mode, RS_CACHE_DIR is already appended to cacheDir.
399 if (newName.find(RS_CACHE_DIR) == std::string::npos) {
400 newName.append("/");
401 newName.append(RS_CACHE_DIR);
402 newName.append("/");
403 }
404
405 if (!ensureCacheDirExists(newName.c_str())) {
406 ALOGE("Could not verify or create cache dir: %s", cacheDir);
407 return nullptr;
408 }
409
410 // Construct an appropriately randomized filename for the copy.
411 newName.append("librs.");
412 newName.append(resName);
413 newName.append("#");
414 newName.append(getRandomString(6)); // 62^6 potential filename variants.
415 newName.append(".so");
416
417 int r = copyFile(newName.c_str(), origName);
418 if (r != 0) {
419 ALOGE("Could not create copy %s -> %s", origName, newName.c_str());
420 return nullptr;
421 }
422 loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL);
423 r = unlink(newName.c_str());
424 if (r != 0) {
425 ALOGE("Could not unlink copy %s", newName.c_str());
426 }
427 if (loaded) {
428 LoadedLibraries.insert(newName.c_str());
429 }
430
431 return loaded;
432}
Jason Sams709a0972012-11-15 18:18:04 -0800433
Yang Nida0f0692015-01-12 13:03:40 -0800434const char* RsdCpuScriptImpl::BCC_EXE_PATH = "/system/bin/bcc";
435
Jason Sams110f1812013-03-14 16:02:18 -0700436#define MAXLINE 500
437#define MAKE_STR_HELPER(S) #S
438#define MAKE_STR(S) MAKE_STR_HELPER(S)
439#define EXPORT_VAR_STR "exportVarCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700440#define EXPORT_FUNC_STR "exportFuncCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700441#define EXPORT_FOREACH_STR "exportForEachCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700442#define OBJECT_SLOT_STR "objectSlotCount: "
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800443#define PRAGMA_STR "pragmaCount: "
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800444#define THREADABLE_STR "isThreadable: "
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
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800477#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700478 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700479#endif
480
Tim Murraye195a3f2014-03-13 15:04:58 -0700481
Chris Wailes44bef6f2014-08-12 13:51:10 -0700482 mRoot = nullptr;
483 mRootExpand = nullptr;
484 mInit = nullptr;
485 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800486 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800487
Chris Wailes44bef6f2014-08-12 13:51:10 -0700488 mBoundAllocs = nullptr;
489 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800490 mIsThreadable = true;
491}
492
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800493bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800494 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
495 if (mRoot) {
496 //ALOGE("Found root(): %p", mRoot);
497 }
498 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
499 if (mRootExpand) {
500 //ALOGE("Found root.expand(): %p", mRootExpand);
501 }
502 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
503 if (mInit) {
504 //ALOGE("Found init(): %p", mInit);
505 }
506 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
507 if (mFreeChildren) {
508 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
509 }
510
Yang Nid9bae682015-01-20 15:31:15 -0800511 mScriptExec = ScriptExecutable::createFromSharedObject(
512 mCtx->getContext(), mScriptSO);
513
514 if (mScriptExec == nullptr) {
515 return false;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800516 }
517
Yang Nid9bae682015-01-20 15:31:15 -0800518 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800519 if (varCount > 0) {
520 mBoundAllocs = new Allocation *[varCount];
521 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
522 }
523
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800524 mIsThreadable = mScriptExec->getThreadable();
525 //ALOGE("Script isThreadable? %d", mIsThreadable);
526
Yang Nid9bae682015-01-20 15:31:15 -0800527 return true;
528}
529
530ScriptExecutable* ScriptExecutable::createFromSharedObject(
531 Context* RSContext, void* sharedObj) {
532 char line[MAXLINE];
533
534 size_t varCount = 0;
535 size_t funcCount = 0;
536 size_t forEachCount = 0;
537 size_t objectSlotCount = 0;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800538 size_t pragmaCount = 0;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800539 bool isThreadable = true;
Yang Nid9bae682015-01-20 15:31:15 -0800540
Yang Nie8f9fba2015-01-30 08:55:10 -0800541 void** fieldAddress = nullptr;
542 bool* fieldIsObject = nullptr;
543 InvokeFunc_t* invokeFunctions = nullptr;
544 ForEachFunc_t* forEachFunctions = nullptr;
545 uint32_t* forEachSignatures = nullptr;
546 const char ** pragmaKeys = nullptr;
547 const char ** pragmaValues = nullptr;
548
Yang Nid9bae682015-01-20 15:31:15 -0800549 const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info");
550
551 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
552 return nullptr;
553 }
554 if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
555 ALOGE("Invalid export var count!: %s", line);
556 return nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800557 }
558
Yang Nie8f9fba2015-01-30 08:55:10 -0800559 fieldAddress = new void*[varCount];
560 if (fieldAddress == nullptr) {
561 return nullptr;
562 }
563
564 fieldIsObject = new bool[varCount];
565 if (fieldIsObject == nullptr) {
566 goto error;
567 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800568
Yang Nid9bae682015-01-20 15:31:15 -0800569 for (size_t i = 0; i < varCount; ++i) {
570 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800571 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800572 }
573 char *c = strrchr(line, '\n');
574 if (c) {
575 *c = '\0';
576 }
577 void* addr = dlsym(sharedObj, line);
578 if (addr == nullptr) {
579 ALOGE("Failed to find variable address for %s: %s",
580 line, dlerror());
581 // Not a critical error if we don't find a global variable.
582 }
Yang Nie8f9fba2015-01-30 08:55:10 -0800583 fieldAddress[i] = addr;
584 fieldIsObject[i] = false;
Yang Nid9bae682015-01-20 15:31:15 -0800585 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800586
Yang Nid9bae682015-01-20 15:31:15 -0800587 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800588 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800589 }
590 if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
591 ALOGE("Invalid export func count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800592 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800593 }
594
Yang Nie8f9fba2015-01-30 08:55:10 -0800595 invokeFunctions = new InvokeFunc_t[funcCount];
596 if (invokeFunctions == nullptr) {
597 goto error;
598 }
Yang Nid9bae682015-01-20 15:31:15 -0800599
600 for (size_t i = 0; i < funcCount; ++i) {
601 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800602 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800603 }
604 char *c = strrchr(line, '\n');
605 if (c) {
606 *c = '\0';
607 }
608
609 invokeFunctions[i] = (InvokeFunc_t) dlsym(sharedObj, line);
610 if (invokeFunctions[i] == nullptr) {
611 ALOGE("Failed to get function address for %s(): %s",
612 line, dlerror());
Yang Nie8f9fba2015-01-30 08:55:10 -0800613 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800614 }
615 }
616
617 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800618 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800619 }
620 if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
621 ALOGE("Invalid export forEach count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800622 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800623 }
624
Yang Nie8f9fba2015-01-30 08:55:10 -0800625 forEachFunctions = new ForEachFunc_t[forEachCount];
626 if (forEachFunctions == nullptr) {
627 goto error;
628 }
629
630 forEachSignatures = new uint32_t[forEachCount];
631 if (forEachSignatures == nullptr) {
632 goto error;
633 }
Yang Nid9bae682015-01-20 15:31:15 -0800634
635 for (size_t i = 0; i < forEachCount; ++i) {
636 unsigned int tmpSig = 0;
637 char tmpName[MAXLINE];
638
639 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800640 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800641 }
642 if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
643 &tmpSig, tmpName) != 2) {
644 ALOGE("Invalid export forEach!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800645 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800646 }
647
648 // Lookup the expanded ForEach kernel.
649 strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
650 forEachSignatures[i] = tmpSig;
651 forEachFunctions[i] =
652 (ForEachFunc_t) dlsym(sharedObj, tmpName);
653 if (i != 0 && forEachFunctions[i] == nullptr) {
654 // Ignore missing root.expand functions.
655 // root() is always specified at location 0.
656 ALOGE("Failed to find forEach function address for %s: %s",
657 tmpName, dlerror());
Yang Nie8f9fba2015-01-30 08:55:10 -0800658 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800659 }
660 }
661
662 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800663 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800664 }
665 if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
666 ALOGE("Invalid object slot count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800667 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800668 }
669
Yang Nid9bae682015-01-20 15:31:15 -0800670 for (size_t i = 0; i < objectSlotCount; ++i) {
671 uint32_t varNum = 0;
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, "%u", &varNum) != 1) {
676 ALOGE("Invalid object slot!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800677 goto error;
Yang Nid9bae682015-01-20 15:31:15 -0800678 }
679
680 if (varNum < varCount) {
681 fieldIsObject[varNum] = true;
682 }
683 }
684
Yang Nie8f9fba2015-01-30 08:55:10 -0800685#ifndef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800686 // Do not attempt to read pragmas or isThreadable flag in compat lib path.
687 // Neither is applicable for compat lib
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800688
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800689 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800690 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800691 }
692
693 if (sscanf(line, PRAGMA_STR "%zu", &pragmaCount) != 1) {
694 ALOGE("Invalid pragma count!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800695 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800696 }
697
Yang Nie8f9fba2015-01-30 08:55:10 -0800698 pragmaKeys = new const char*[pragmaCount];
699 if (pragmaKeys == nullptr) {
700 goto error;
701 }
702
703 pragmaValues = new const char*[pragmaCount];
704 if (pragmaValues == nullptr) {
705 goto error;
706 }
707
708 bzero(pragmaKeys, sizeof(char*) * pragmaCount);
709 bzero(pragmaValues, sizeof(char*) * pragmaCount);
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800710
711 for (size_t i = 0; i < pragmaCount; ++i) {
712 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
713 ALOGE("Unable to read pragma at index %zu!", i);
Yang Nie8f9fba2015-01-30 08:55:10 -0800714 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800715 }
716
717 char key[MAXLINE];
718 char value[MAXLINE] = ""; // initialize in case value is empty
719
720 // pragmas can just have a key and no value. Only check to make sure
721 // that the key is not empty
722 if (sscanf(line, "%" MAKE_STR(MAXLINE) "s - %" MAKE_STR(MAXLINE) "s",
723 key, value) == 0 ||
724 strlen(key) == 0)
725 {
726 ALOGE("Invalid pragma value!: %s", line);
727
Yang Nie8f9fba2015-01-30 08:55:10 -0800728 goto error;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800729 }
730
731 char *pKey = new char[strlen(key)+1];
732 strcpy(pKey, key);
733 pragmaKeys[i] = pKey;
734
735 char *pValue = new char[strlen(value)+1];
736 strcpy(pValue, value);
737 pragmaValues[i] = pValue;
738 //ALOGE("Pragma %zu: Key: '%s' Value: '%s'", i, pKey, pValue);
739 }
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800740
741 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800742 goto error;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800743 }
744
745 char tmpFlag[4];
746 if (sscanf(line, THREADABLE_STR "%4s", tmpFlag) != 1) {
747 ALOGE("Invalid threadable flag!: %s", line);
Yang Nie8f9fba2015-01-30 08:55:10 -0800748 goto error;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800749 }
Yang Nie8f9fba2015-01-30 08:55:10 -0800750 if (strcmp(tmpFlag, "yes") == 0) {
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800751 isThreadable = true;
Yang Nie8f9fba2015-01-30 08:55:10 -0800752 } else if (strcmp(tmpFlag, "no") == 0) {
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800753 isThreadable = false;
Yang Nie8f9fba2015-01-30 08:55:10 -0800754 } else {
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800755 ALOGE("Invalid threadable flag!: %s", tmpFlag);
Yang Nie8f9fba2015-01-30 08:55:10 -0800756 goto error;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800757 }
758
Yang Nie8f9fba2015-01-30 08:55:10 -0800759#endif // RS_COMPATIBILITY_LIB
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800760
Yang Nid9bae682015-01-20 15:31:15 -0800761 return new ScriptExecutable(
Yang Nie8f9fba2015-01-30 08:55:10 -0800762 RSContext, fieldAddress, fieldIsObject, varCount,
763 invokeFunctions, funcCount,
764 forEachFunctions, forEachSignatures, forEachCount,
765 pragmaKeys, pragmaValues, pragmaCount,
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800766 isThreadable);
Yang Nie8f9fba2015-01-30 08:55:10 -0800767
768error:
769
770#ifndef RS_COMPATIBILITY_LIB
771 for (size_t idx = 0; idx < pragmaCount; ++idx) {
Yang Nida0f0692015-01-12 13:03:40 -0800772 delete [] pragmaKeys[idx];
773 delete [] pragmaValues[idx];
Yang Nie8f9fba2015-01-30 08:55:10 -0800774 }
775
776 delete[] pragmaValues;
777 delete[] pragmaKeys;
778#endif // RS_COMPATIBILITY_LIB
779
780 delete[] forEachSignatures;
781 delete[] forEachFunctions;
782 delete[] invokeFunctions;
783 delete[] fieldIsObject;
784 delete[] fieldAddress;
785
786 return nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800787}
788
Jason Sams709a0972012-11-15 18:18:04 -0800789bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
790 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800791 uint32_t flags, char const *bccPluginName) {
Yang Nie8f9fba2015-01-30 08:55:10 -0800792 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir,
793 // bitcode, bitcodeSize, flags, lookupFunc);
Jason Sams709a0972012-11-15 18:18:04 -0800794 //ALOGE("rsdScriptInit %p %p", rsc, script);
795
796 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700797#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800798 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800799
Chris Wailes44bef6f2014-08-12 13:51:10 -0700800 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800801
Jason Sams709a0972012-11-15 18:18:04 -0800802 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700803 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800804 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
805 mCtx->unlockMutex();
806 return false;
807 }
808
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700809 // Run any compiler setup functions we have been provided with.
810 RSSetupCompilerCallback setupCompilerCallback =
811 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700812 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700813 setupCompilerCallback(mCompilerDriver);
814 }
815
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700816 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
817 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700818 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700819 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700820 return false;
821 }
822
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700823 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700824
825 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700826 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800827 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700828 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700829
Chris Wailes6847e732014-08-11 17:30:51 -0700830 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700831 bcFileName.append("/");
832 bcFileName.append(resName);
833 bcFileName.append(".bc");
834
835 std::vector<const char*> compileArguments;
836 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
837 useRSDebugContext, bccPluginName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700838 // The last argument of compileArguments ia a nullptr, so remove 1 from the size.
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700839 std::string compileCommandLine =
840 bcc::getCommandLine(compileArguments.size() - 1, compileArguments.data());
841
Tim Murraybf96a522015-01-23 15:37:03 -0800842 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800843 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700844 }
845
846 // If we can't, it's either not there or out of date. We compile the bit code and try loading
847 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800848 if (mScriptSO == nullptr) {
849 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
850 compileArguments.data(), compileCommandLine))
851 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700852 ALOGE("bcc: FAILS to compile '%s'", resName);
853 mCtx->unlockMutex();
854 return false;
855 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800856
Yang Ni1c44cb62015-01-22 12:02:27 -0800857 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800858 ALOGE("Linker: Failed to link object file '%s'", resName);
859 mCtx->unlockMutex();
860 return false;
861 }
862
Yang Ni1c44cb62015-01-22 12:02:27 -0800863 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800864 if (mScriptSO == nullptr) {
865 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700866 mCtx->unlockMutex();
867 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700868 }
869 }
Jason Sams709a0972012-11-15 18:18:04 -0800870
Yang Nida0f0692015-01-12 13:03:40 -0800871 mBitcodeFilePath = bcFileName;
872
Stephen Hines45e753a2015-01-19 20:58:44 -0800873 // Read RS symbol information from the .so.
874 if ( !mScriptSO) {
875 goto error;
Jason Sams709a0972012-11-15 18:18:04 -0800876 }
877
Stephen Hines45e753a2015-01-19 20:58:44 -0800878 if ( !storeRSInfoFromSO()) {
879 goto error;
Tim Murray29809d12014-05-28 12:04:19 -0700880 }
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700881#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800882 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
883 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700884
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800885 if (!mScriptSO) {
886 goto error;
887 }
Jason Sams110f1812013-03-14 16:02:18 -0700888
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800889 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700890 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700891 }
892#endif
Jason Sams709a0972012-11-15 18:18:04 -0800893 mCtx->unlockMutex();
894 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700895
Jason Sams110f1812013-03-14 16:02:18 -0700896error:
897
898 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700899 if (mScriptSO) {
900 dlclose(mScriptSO);
Yang Nieb9aa672015-01-27 14:32:25 -0800901 mScriptSO = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700902 }
903 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800904}
905
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700906#ifndef RS_COMPATIBILITY_LIB
907
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700908const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
909 size_t bitcodeSize) {
910 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
911
912 // If we're debugging, use the debug library.
913 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
914 return SYSLIBPATH"/libclcore_debug.bc";
915 }
916
917 // If a callback has been registered to specify a library, use that.
918 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700919 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700920 return selectRTCallback((const char*)bitcode, bitcodeSize);
921 }
922
923 // Check for a platform specific library
924#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
925 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700926 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700927 // NEON-capable ARMv7a devices can use an accelerated math library
928 // for all reduced precision scripts.
929 // ARMv8 does not use NEON, as ASIMD can be used with all precision
930 // levels.
931 return SYSLIBPATH"/libclcore_neon.bc";
932 } else {
933 return defaultLib;
934 }
935#elif defined(__i386__) || defined(__x86_64__)
936 // x86 devices will use an optimized library.
937 return SYSLIBPATH"/libclcore_x86.bc";
938#else
939 return defaultLib;
940#endif
941}
942
943#endif
944
Jason Sams709a0972012-11-15 18:18:04 -0800945void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700946 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800947 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
948 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800949 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
Yang Nie8f9fba2015-01-30 08:55:10 -0800950 script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys();
951 script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues();
Jason Sams110f1812013-03-14 16:02:18 -0700952
953 // Bug, need to stash in metadata
954 if (mRootExpand) {
955 script->mHal.info.root = mRootExpand;
956 } else {
957 script->mHal.info.root = mRoot;
958 }
Jason Sams709a0972012-11-15 18:18:04 -0800959}
960
Jason Sams709a0972012-11-15 18:18:04 -0800961
962typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
963
Jason Samsbf2111d2015-01-26 18:13:41 -0800964bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700965 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700966 Allocation * aout,
967 const void * usr, uint32_t usrLen,
968 const RsScriptCall *sc,
969 MTLaunchStruct *mtls) {
970
971 memset(mtls, 0, sizeof(MTLaunchStruct));
972
Chris Wailesf3712132014-07-16 15:18:30 -0700973 for (int index = inLen; --index >= 0;) {
974 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700975
Chris Wailesf3712132014-07-16 15:18:30 -0700976 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700977 if (ain != nullptr &&
978 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
979
Chris Wailesf3712132014-07-16 15:18:30 -0700980 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
981 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800982 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700983 }
984 }
985
Chris Wailes44bef6f2014-08-12 13:51:10 -0700986 if (aout &&
987 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
988
Chris Wailesf3712132014-07-16 15:18:30 -0700989 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
990 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800991 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700992 }
993
Chris Wailesf3712132014-07-16 15:18:30 -0700994 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700995 const Allocation *ain0 = ains[0];
996 const Type *inType = ain0->getType();
997
Jason Samsc0d68472015-01-20 14:29:52 -0800998 mtls->fep.dim.x = inType->getDimX();
999 mtls->fep.dim.y = inType->getDimY();
1000 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001001
1002 for (int Index = inLen; --Index >= 1;) {
1003 if (!ain0->hasSameDims(ains[Index])) {
1004 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
Yang Nie8f9fba2015-01-30 08:55:10 -08001005 "Failed to launch kernel; dimensions of input and output"
1006 "allocations do not match.");
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001007
Jason Samsbf2111d2015-01-26 18:13:41 -08001008 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001009 }
1010 }
1011
Chris Wailes44bef6f2014-08-12 13:51:10 -07001012 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001013 const Type *outType = aout->getType();
1014
Jason Samsc0d68472015-01-20 14:29:52 -08001015 mtls->fep.dim.x = outType->getDimX();
1016 mtls->fep.dim.y = outType->getDimY();
1017 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001018
1019 } else {
Chris Wailesf3712132014-07-16 15:18:30 -07001020 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1021 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -08001022 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001023 }
1024
Chris Wailes44bef6f2014-08-12 13:51:10 -07001025 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001026 if (!ains[0]->hasSameDims(aout)) {
1027 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
1028 "Failed to launch kernel; dimensions of input and output allocations do not match.");
1029
Jason Samsbf2111d2015-01-26 18:13:41 -08001030 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001031 }
1032 }
1033
1034 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -08001035 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001036 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -08001037 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
1038 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
1039 if (mtls->start.x >= mtls->end.x) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001040 }
1041
1042 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -08001043 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001044 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -08001045 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
1046 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
1047 if (mtls->start.y >= mtls->end.y) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001048 }
1049
1050 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -08001051 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001052 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -08001053 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
1054 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
1055 if (mtls->start.z >= mtls->end.z) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001056 }
1057
Jason Samsbf2111d2015-01-26 18:13:41 -08001058 if (!sc || (sc->arrayEnd == 0)) {
1059 mtls->end.array[0] = mtls->fep.dim.array[0];
1060 } else {
1061 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
1062 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
1063 if (mtls->start.array[0] >= mtls->end.array[0]) return false;
1064 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001065
Jason Samsbf2111d2015-01-26 18:13:41 -08001066 if (!sc || (sc->array2End == 0)) {
1067 mtls->end.array[1] = mtls->fep.dim.array[1];
1068 } else {
1069 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
1070 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
1071 if (mtls->start.array[1] >= mtls->end.array[1]) return false;
1072 }
1073
1074 if (!sc || (sc->array3End == 0)) {
1075 mtls->end.array[2] = mtls->fep.dim.array[2];
1076 } else {
1077 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
1078 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
1079 if (mtls->start.array[2] >= mtls->end.array[2]) return false;
1080 }
1081
1082 if (!sc || (sc->array4End == 0)) {
1083 mtls->end.array[3] = mtls->fep.dim.array[3];
1084 } else {
1085 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
1086 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
1087 if (mtls->start.array[3] >= mtls->end.array[3]) return false;
1088 }
1089
1090
1091 // The X & Y walkers always want 0-1 min even if dim is not present
1092 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
1093 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001094
1095 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -08001096 if (ains) {
1097 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
1098 }
1099 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001100 mtls->fep.usr = usr;
1101 mtls->fep.usrLen = usrLen;
1102 mtls->mSliceSize = 1;
1103 mtls->mSliceNum = 0;
1104
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001105 mtls->isThreadable = mIsThreadable;
1106
Chris Wailesf3712132014-07-16 15:18:30 -07001107 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -07001108 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001109 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -08001110 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
1111 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001112 }
1113 }
1114
Chris Wailes44bef6f2014-08-12 13:51:10 -07001115 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -08001116 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
1117 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001118 }
Jason Samsbf2111d2015-01-26 18:13:41 -08001119
1120 // All validation passed, ok to launch threads
1121 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001122}
1123
Jason Sams709a0972012-11-15 18:18:04 -08001124
1125void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -07001126 const Allocation ** ains,
1127 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -08001128 Allocation * aout,
1129 const void * usr,
1130 uint32_t usrLen,
1131 const RsScriptCall *sc) {
1132
1133 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001134
Jason Samsbf2111d2015-01-26 18:13:41 -08001135 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
1136 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001137
Jason Samsbf2111d2015-01-26 18:13:41 -08001138 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1139 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
1140 mCtx->setTLS(oldTLS);
1141 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001142}
1143
Jason Sams709a0972012-11-15 18:18:04 -08001144void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -08001145 mtls->script = this;
1146 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -08001147 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001148 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -08001149 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -08001150}
1151
1152int RsdCpuScriptImpl::invokeRoot() {
1153 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1154 int ret = mRoot();
1155 mCtx->setTLS(oldTLS);
1156 return ret;
1157}
1158
1159void RsdCpuScriptImpl::invokeInit() {
1160 if (mInit) {
1161 mInit();
1162 }
1163}
1164
1165void RsdCpuScriptImpl::invokeFreeChildren() {
1166 if (mFreeChildren) {
1167 mFreeChildren();
1168 }
1169}
1170
1171void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
1172 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001173 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001174 void * ap = nullptr;
1175
1176#if defined(__x86_64__)
1177 // The invoked function could have input parameter of vector type for example float4 which
1178 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
1179 // So try to align void* params before passing them into RS exported function.
1180
1181 if ((uint8_t)(uint64_t)params & 0x0F) {
1182 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
1183 memcpy(ap, params, paramLength);
1184 } else {
Yang Nie8f9fba2015-01-30 08:55:10 -08001185 ALOGE("x86_64: invokeFunction memalign error, still use params which"
1186 " is not 16 bytes aligned.");
Yong Cheneaba5a32014-12-12 13:25:18 +08001187 }
1188 }
1189#endif
Jason Sams709a0972012-11-15 18:18:04 -08001190
1191 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001192 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -08001193 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001194
Jason Sams709a0972012-11-15 18:18:04 -08001195 mCtx->setTLS(oldTLS);
1196}
1197
1198void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
1199 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001200 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -08001201
1202 //if (mIntrinsicID) {
1203 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
1204 //return;
1205 //}
1206
Yang Nid9bae682015-01-20 15:31:15 -08001207 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001208 if (!destPtr) {
1209 //ALOGV("Calling setVar on slot = %i which is null", slot);
1210 return;
1211 }
1212
1213 memcpy(destPtr, data, dataLength);
1214}
1215
Tim Murray9c642392013-04-11 13:29:59 -07001216void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
1217 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001218 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -07001219
Yang Nid9bae682015-01-20 15:31:15 -08001220 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -07001221 if (!srcPtr) {
1222 //ALOGV("Calling setVar on slot = %i which is null", slot);
1223 return;
1224 }
1225 memcpy(data, srcPtr, dataLength);
1226}
1227
1228
Jason Sams709a0972012-11-15 18:18:04 -08001229void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
1230 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -07001231 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -08001232 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001233 if (!destPtr) {
1234 //ALOGV("Calling setVar on slot = %i which is null", slot);
1235 return;
1236 }
1237
1238 // We want to look at dimension in terms of integer components,
1239 // but dimLength is given in terms of bytes.
1240 dimLength /= sizeof(int);
1241
1242 // Only a single dimension is currently supported.
1243 rsAssert(dimLength == 1);
1244 if (dimLength == 1) {
1245 // First do the increment loop.
1246 size_t stride = elem->getSizeBytes();
1247 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001248 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001249 elem->incRefs(cVal);
1250 cVal += stride;
1251 }
1252
1253 // Decrement loop comes after (to prevent race conditions).
1254 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001255 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001256 elem->decRefs(oldVal);
1257 oldVal += stride;
1258 }
1259 }
1260
1261 memcpy(destPtr, data, dataLength);
1262}
1263
1264void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
1265
1266 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001267 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001268
Yang Nid9bae682015-01-20 15:31:15 -08001269 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001270 if (!destPtr) {
1271 //ALOGV("Calling setVar on slot = %i which is null", slot);
1272 return;
1273 }
1274
Chris Wailes44bef6f2014-08-12 13:51:10 -07001275 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001276 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001277 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -08001278 ptr = data->mHal.drvState.lod[0].mallocPtr;
1279 }
1280 memcpy(destPtr, &ptr, sizeof(void *));
1281}
1282
1283void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
1284
1285 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001286 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001287
Yang Nid9bae682015-01-20 15:31:15 -08001288 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001289 if (!destPtr) {
1290 //ALOGV("Calling setVar on slot = %i which is null", slot);
1291 return;
1292 }
1293
Jason Sams05ef73f2014-08-05 14:59:22 -07001294 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -08001295}
1296
1297RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -07001298#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -08001299 if (mCompilerDriver) {
1300 delete mCompilerDriver;
1301 }
Stephen Hines45e753a2015-01-19 20:58:44 -08001302#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001303
Yang Nid9bae682015-01-20 15:31:15 -08001304 if (mScriptExec != nullptr) {
1305 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001306 }
Jason Sams110f1812013-03-14 16:02:18 -07001307 if (mBoundAllocs) delete[] mBoundAllocs;
1308 if (mScriptSO) {
1309 dlclose(mScriptSO);
1310 }
Jason Sams709a0972012-11-15 18:18:04 -08001311}
1312
1313Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1314 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001315 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001316 }
1317
1318 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1319 Allocation *a = mBoundAllocs[ct];
1320 if (!a) continue;
1321 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1322 return a;
1323 }
1324 }
1325 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001326 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001327}
1328
Chris Wailesf3712132014-07-16 15:18:30 -07001329void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1330 uint32_t inLen, Allocation * aout,
1331 const void * usr, uint32_t usrLen,
1332 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001333
Chris Wailesf3712132014-07-16 15:18:30 -07001334void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1335 uint32_t inLen, Allocation * aout,
1336 const void * usr, uint32_t usrLen,
1337 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001338
Jason Sams709a0972012-11-15 18:18:04 -08001339
1340}
1341}