blob: 94a1db8bf4a8d1bdc9ee3ff25cd173a6e2950056 [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
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700163const static char *BCC_EXE_PATH = "/system/bin/bcc";
164
Chris Wailes6847e732014-08-11 17:30:51 -0700165static void setCompileArguments(std::vector<const char*>* args,
166 const std::string& bcFileName,
167 const char* cacheDir, const char* resName,
168 const char* core_lib, bool useRSDebugContext,
169 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700170 rsAssert(cacheDir && resName && core_lib);
171 args->push_back(BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -0800172 args->push_back("-unroll-runtime");
173 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700174 args->push_back("-o");
175 args->push_back(resName);
176 args->push_back("-output_path");
177 args->push_back(cacheDir);
178 args->push_back("-bclib");
179 args->push_back(core_lib);
180 args->push_back("-mtriple");
181 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
182
Tim Murray358ffb82014-12-09 11:53:06 -0800183 // Enable workaround for A53 codegen by default.
184#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
185 args->push_back("-aarch64-fix-cortex-a53-835769");
186#endif
187
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700188 // Execute the bcc compiler.
189 if (useRSDebugContext) {
190 args->push_back("-rs-debug-ctx");
191 } else {
192 // Only load additional libraries for compiles that don't use
193 // the debug context.
194 if (bccPluginName && strlen(bccPluginName) > 0) {
195 args->push_back("-load");
196 args->push_back(bccPluginName);
197 }
198 }
199
Stephen Hines45e753a2015-01-19 20:58:44 -0800200 args->push_back("-fPIC");
201 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800202
Chris Wailes6847e732014-08-11 17:30:51 -0700203 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700204 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700205}
206
Chris Wailes6847e732014-08-11 17:30:51 -0700207static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700208 const char *bitcode,
209 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700210 const char **compileArguments,
211 const std::string &compileCommandLine) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700212 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700213
Chris Wailes6847e732014-08-11 17:30:51 -0700214 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700215 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700216 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700217 return false;
218 }
219 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
220 fclose(bcfile);
221 if (nwritten != bitcodeSize) {
222 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700223 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700224 return false;
225 }
226
227 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800228
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700229 switch (pid) {
230 case -1: { // Error occurred (we attempt no recovery)
231 ALOGE("Couldn't fork for bcc compiler execution");
232 return false;
233 }
234 case 0: { // Child process
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700235 ALOGV("Invoking BCC with: %s", compileCommandLine.c_str());
236 execv(BCC_EXE_PATH, (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800237
Stephen Hines00511322014-01-31 11:20:23 -0800238 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700239 abort();
240 return false;
241 }
242 default: { // Parent process (actual driver)
243 // Wait on child process to finish compiling the source.
244 int status = 0;
245 pid_t w = waitpid(pid, &status, 0);
246 if (w == -1) {
247 ALOGE("Could not wait for bcc compiler");
248 return false;
249 }
250
251 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
252 return true;
253 }
254
255 ALOGE("bcc compiler terminated unexpectedly");
256 return false;
257 }
258 }
259}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700260
Yang Ni1c44cb62015-01-22 12:02:27 -0800261#endif // !defined(RS_COMPATIBILITY_LIB)
262} // namespace
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800263
Yang Ni1c44cb62015-01-22 12:02:27 -0800264namespace android {
265namespace renderscript {
266
267const char* SharedLibraryUtils::LD_EXE_PATH = "/system/bin/ld.mc";
268const char* SharedLibraryUtils::RS_CACHE_DIR = "com.android.renderscript.cache";
269
270#ifndef RS_COMPATIBILITY_LIB
271
272bool SharedLibraryUtils::createSharedLibrary(const char *cacheDir, const char *resName) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800273 std::string sharedLibName = findSharedObjectName(cacheDir, resName);
274 std::string objFileName = cacheDir;
275 objFileName.append("/");
276 objFileName.append(resName);
277 objFileName.append(".o");
278
279 const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
280 std::vector<const char *> args = {
281 LD_EXE_PATH,
282 "-shared",
283 "-nostdlib",
284 compiler_rt,
285 "-mtriple", DEFAULT_TARGET_TRIPLE_STRING,
286 "-L", SYSLIBPATH,
287 "-lRSDriver", "-lm", "-lc",
288 objFileName.c_str(),
289 "-o", sharedLibName.c_str(),
290 nullptr
291 };
292
293 std::string cmdLineStr = bcc::getCommandLine(args.size()-1, args.data());
294
295 pid_t pid = fork();
296
297 switch (pid) {
298 case -1: { // Error occurred (we attempt no recovery)
299 ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH);
300 return false;
301 }
302 case 0: { // Child process
303 ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str());
304 execv(LD_EXE_PATH, (char* const*) args.data());
305
306 ALOGE("execv() failed: %s", strerror(errno));
307 abort();
308 return false;
309 }
310 default: { // Parent process (actual driver)
311 // Wait on child process to finish compiling the source.
312 int status = 0;
313 pid_t w = waitpid(pid, &status, 0);
314 if (w == -1) {
315 ALOGE("Could not wait for linker (%s)", LD_EXE_PATH);
316 return false;
317 }
318
319 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
320 return true;
321 }
322
323 ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH);
324 return false;
325 }
326 }
327}
Stephen Hinesba17ae42013-06-05 17:18:04 -0700328
Yang Ni1c44cb62015-01-22 12:02:27 -0800329#endif // RS_COMPATIBILITY_LIB
330
Miao Wangf3213d72015-01-14 10:03:07 -0800331
332void* SharedLibraryUtils::loadSharedLibrary(const char *cacheDir, const char *resName, const char *nativeLibDir) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800333 void *loaded = nullptr;
334
Miao Wangf3213d72015-01-14 10:03:07 -0800335#if defined(RS_COMPATIBILITY_LIB) && defined(__LP64__)
336 std::string scriptSOName = findSharedObjectName(nativeLibDir, resName);
337#else
Yang Ni1c44cb62015-01-22 12:02:27 -0800338 std::string scriptSOName = findSharedObjectName(cacheDir, resName);
Miao Wangf3213d72015-01-14 10:03:07 -0800339#endif
Yang Ni1c44cb62015-01-22 12:02:27 -0800340
341 // We should check if we can load the library from the standard app
342 // location for shared libraries first.
343 loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName);
344
345 if (loaded == nullptr) {
346 ALOGE("Unable to open shared library (%s): %s",
347 scriptSOName.c_str(), dlerror());
348
349#ifdef RS_COMPATIBILITY_LIB
350 // One final attempt to find the library in "/system/lib".
351 // We do this to allow bundled applications to use the compatibility
352 // library fallback path. Those applications don't have a private
353 // library path, so they need to install to the system directly.
354 // Note that this is really just a testing path.
355 std::string scriptSONameSystem("/system/lib/librs.");
356 scriptSONameSystem.append(resName);
357 scriptSONameSystem.append(".so");
358 loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir,
359 resName);
360 if (loaded == nullptr) {
361 ALOGE("Unable to open system shared library (%s): %s",
362 scriptSONameSystem.c_str(), dlerror());
363 }
364#endif
365 }
366
367 return loaded;
368}
369
370void* SharedLibraryUtils::loadSOHelper(const char *origName, const char *cacheDir,
371 const char *resName) {
372 // Keep track of which .so libraries have been loaded. Once a library is
373 // in the set (per-process granularity), we must instead make a copy of
374 // the original shared object (randomly named .so file) and load that one
375 // instead. If we don't do this, we end up aliasing global data between
376 // the various Script instances (which are supposed to be completely
377 // independent).
378 static std::set<std::string> LoadedLibraries;
379
380 void *loaded = nullptr;
381
382 // Skip everything if we don't even have the original library available.
383 if (access(origName, F_OK) != 0) {
384 return nullptr;
385 }
386
387 // Common path is that we have not loaded this Script/library before.
388 if (LoadedLibraries.find(origName) == LoadedLibraries.end()) {
389 loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL);
390 if (loaded) {
391 LoadedLibraries.insert(origName);
392 }
393 return loaded;
394 }
395
396 std::string newName(cacheDir);
397
398 // Append RS_CACHE_DIR only if it is not found in cacheDir
399 // In driver mode, RS_CACHE_DIR is already appended to cacheDir.
400 if (newName.find(RS_CACHE_DIR) == std::string::npos) {
401 newName.append("/");
402 newName.append(RS_CACHE_DIR);
403 newName.append("/");
404 }
405
406 if (!ensureCacheDirExists(newName.c_str())) {
407 ALOGE("Could not verify or create cache dir: %s", cacheDir);
408 return nullptr;
409 }
410
411 // Construct an appropriately randomized filename for the copy.
412 newName.append("librs.");
413 newName.append(resName);
414 newName.append("#");
415 newName.append(getRandomString(6)); // 62^6 potential filename variants.
416 newName.append(".so");
417
418 int r = copyFile(newName.c_str(), origName);
419 if (r != 0) {
420 ALOGE("Could not create copy %s -> %s", origName, newName.c_str());
421 return nullptr;
422 }
423 loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL);
424 r = unlink(newName.c_str());
425 if (r != 0) {
426 ALOGE("Could not unlink copy %s", newName.c_str());
427 }
428 if (loaded) {
429 LoadedLibraries.insert(newName.c_str());
430 }
431
432 return loaded;
433}
Jason Sams709a0972012-11-15 18:18:04 -0800434
Jason Sams110f1812013-03-14 16:02:18 -0700435#define MAXLINE 500
436#define MAKE_STR_HELPER(S) #S
437#define MAKE_STR(S) MAKE_STR_HELPER(S)
438#define EXPORT_VAR_STR "exportVarCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700439#define EXPORT_FUNC_STR "exportFuncCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700440#define EXPORT_FOREACH_STR "exportForEachCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700441#define OBJECT_SLOT_STR "objectSlotCount: "
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800442#define PRAGMA_STR "pragmaCount: "
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800443#define THREADABLE_STR "isThreadable: "
Jason Sams110f1812013-03-14 16:02:18 -0700444
445// Copy up to a newline or size chars from str -> s, updating str
Chris Wailes44bef6f2014-08-12 13:51:10 -0700446// Returns s when successful and nullptr when '\0' is finally reached.
Jason Sams110f1812013-03-14 16:02:18 -0700447static char* strgets(char *s, int size, const char **ppstr) {
448 if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700449 return nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700450 }
451
452 int i;
453 for (i = 0; i < (size - 1); i++) {
454 s[i] = **ppstr;
455 (*ppstr)++;
456 if (s[i] == '\0') {
457 return s;
458 } else if (s[i] == '\n') {
459 s[i+1] = '\0';
460 return s;
461 }
462 }
463
464 // size has been exceeded.
465 s[i] = '\0';
466
467 return s;
468}
Jason Sams709a0972012-11-15 18:18:04 -0800469
470RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
471 mCtx = ctx;
472 mScript = s;
473
Chris Wailes44bef6f2014-08-12 13:51:10 -0700474 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800475
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800476#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700477 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700478#endif
479
Tim Murraye195a3f2014-03-13 15:04:58 -0700480
Chris Wailes44bef6f2014-08-12 13:51:10 -0700481 mRoot = nullptr;
482 mRootExpand = nullptr;
483 mInit = nullptr;
484 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800485 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800486
Chris Wailes44bef6f2014-08-12 13:51:10 -0700487 mBoundAllocs = nullptr;
488 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800489 mIsThreadable = true;
490}
491
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800492bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800493 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
494 if (mRoot) {
495 //ALOGE("Found root(): %p", mRoot);
496 }
497 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
498 if (mRootExpand) {
499 //ALOGE("Found root.expand(): %p", mRootExpand);
500 }
501 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
502 if (mInit) {
503 //ALOGE("Found init(): %p", mInit);
504 }
505 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
506 if (mFreeChildren) {
507 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
508 }
509
Yang Nid9bae682015-01-20 15:31:15 -0800510 mScriptExec = ScriptExecutable::createFromSharedObject(
511 mCtx->getContext(), mScriptSO);
512
513 if (mScriptExec == nullptr) {
514 return false;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800515 }
516
Yang Nid9bae682015-01-20 15:31:15 -0800517 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800518 if (varCount > 0) {
519 mBoundAllocs = new Allocation *[varCount];
520 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
521 }
522
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800523 mIsThreadable = mScriptExec->getThreadable();
524 //ALOGE("Script isThreadable? %d", mIsThreadable);
525
Yang Nid9bae682015-01-20 15:31:15 -0800526 return true;
527}
528
529ScriptExecutable* ScriptExecutable::createFromSharedObject(
530 Context* RSContext, void* sharedObj) {
531 char line[MAXLINE];
532
533 size_t varCount = 0;
534 size_t funcCount = 0;
535 size_t forEachCount = 0;
536 size_t objectSlotCount = 0;
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800537 size_t pragmaCount = 0;
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800538 bool isThreadable = true;
Yang Nid9bae682015-01-20 15:31:15 -0800539
540 const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info");
541
542 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
543 return nullptr;
544 }
545 if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
546 ALOGE("Invalid export var count!: %s", line);
547 return nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800548 }
549
Yang Nid9bae682015-01-20 15:31:15 -0800550 std::vector<void*> fieldAddress;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800551
Yang Nid9bae682015-01-20 15:31:15 -0800552 for (size_t i = 0; i < varCount; ++i) {
553 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
554 return nullptr;
555 }
556 char *c = strrchr(line, '\n');
557 if (c) {
558 *c = '\0';
559 }
560 void* addr = dlsym(sharedObj, line);
561 if (addr == nullptr) {
562 ALOGE("Failed to find variable address for %s: %s",
563 line, dlerror());
564 // Not a critical error if we don't find a global variable.
565 }
566 fieldAddress.push_back(addr);
567 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800568
Yang Nid9bae682015-01-20 15:31:15 -0800569 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
570 return nullptr;
571 }
572 if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
573 ALOGE("Invalid export func count!: %s", line);
574 return nullptr;
575 }
576
577 std::vector<InvokeFunc_t> invokeFunctions(funcCount);
578
579 for (size_t i = 0; i < funcCount; ++i) {
580 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
581 return nullptr ;
582 }
583 char *c = strrchr(line, '\n');
584 if (c) {
585 *c = '\0';
586 }
587
588 invokeFunctions[i] = (InvokeFunc_t) dlsym(sharedObj, line);
589 if (invokeFunctions[i] == nullptr) {
590 ALOGE("Failed to get function address for %s(): %s",
591 line, dlerror());
592 return nullptr;
593 }
594 }
595
596 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
597 return nullptr;
598 }
599 if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
600 ALOGE("Invalid export forEach count!: %s", line);
601 return nullptr;
602 }
603
604 std::vector<ForEachFunc_t> forEachFunctions(forEachCount);
605 std::vector<uint32_t> forEachSignatures(forEachCount);
606
607 for (size_t i = 0; i < forEachCount; ++i) {
608 unsigned int tmpSig = 0;
609 char tmpName[MAXLINE];
610
611 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
612 return nullptr;
613 }
614 if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
615 &tmpSig, tmpName) != 2) {
616 ALOGE("Invalid export forEach!: %s", line);
617 return nullptr;
618 }
619
620 // Lookup the expanded ForEach kernel.
621 strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
622 forEachSignatures[i] = tmpSig;
623 forEachFunctions[i] =
624 (ForEachFunc_t) dlsym(sharedObj, tmpName);
625 if (i != 0 && forEachFunctions[i] == nullptr) {
626 // Ignore missing root.expand functions.
627 // root() is always specified at location 0.
628 ALOGE("Failed to find forEach function address for %s: %s",
629 tmpName, dlerror());
630 return nullptr;
631 }
632 }
633
634 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
635 return nullptr;
636 }
637 if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
638 ALOGE("Invalid object slot count!: %s", line);
639 return nullptr;
640 }
641
642 std::vector<bool> fieldIsObject(varCount, false);
643
644 rsAssert(varCount > 0);
645 for (size_t i = 0; i < objectSlotCount; ++i) {
646 uint32_t varNum = 0;
647 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
648 return nullptr;
649 }
650 if (sscanf(line, "%u", &varNum) != 1) {
651 ALOGE("Invalid object slot!: %s", line);
652 return nullptr;
653 }
654
655 if (varNum < varCount) {
656 fieldIsObject[varNum] = true;
657 }
658 }
659
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800660#ifdef RS_COMPATIBILITY_LIB
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800661 // Do not attempt to read pragmas or isThreadable flag in compat lib path.
662 // Neither is applicable for compat lib
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800663 std::vector<const char *> pragmaKeys(pragmaCount);
664 std::vector<const char *> pragmaValues(pragmaCount);
665
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800666 isThreadable = true;
667
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800668#else
669 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
670 return nullptr;
671 }
672
673 if (sscanf(line, PRAGMA_STR "%zu", &pragmaCount) != 1) {
674 ALOGE("Invalid pragma count!: %s", line);
675 return nullptr;
676 }
677
678 std::vector<const char *> pragmaKeys(pragmaCount);
679 std::vector<const char *> pragmaValues(pragmaCount);
680
681 for (size_t i = 0; i < pragmaCount; ++i) {
682 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
683 ALOGE("Unable to read pragma at index %zu!", i);
684 return nullptr;
685 }
686
687 char key[MAXLINE];
688 char value[MAXLINE] = ""; // initialize in case value is empty
689
690 // pragmas can just have a key and no value. Only check to make sure
691 // that the key is not empty
692 if (sscanf(line, "%" MAKE_STR(MAXLINE) "s - %" MAKE_STR(MAXLINE) "s",
693 key, value) == 0 ||
694 strlen(key) == 0)
695 {
696 ALOGE("Invalid pragma value!: %s", line);
697
698 // free previously allocated keys and values
699 for (size_t idx = 0; idx < i; ++idx) {
700 delete [] pragmaKeys[idx];
701 delete [] pragmaValues[idx];
702 }
703 return nullptr;
704 }
705
706 char *pKey = new char[strlen(key)+1];
707 strcpy(pKey, key);
708 pragmaKeys[i] = pKey;
709
710 char *pValue = new char[strlen(value)+1];
711 strcpy(pValue, value);
712 pragmaValues[i] = pValue;
713 //ALOGE("Pragma %zu: Key: '%s' Value: '%s'", i, pKey, pValue);
714 }
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800715
716 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
717 return nullptr;
718 }
719
720 char tmpFlag[4];
721 if (sscanf(line, THREADABLE_STR "%4s", tmpFlag) != 1) {
722 ALOGE("Invalid threadable flag!: %s", line);
723 return nullptr;
724 }
725 if (strcmp(tmpFlag, "yes") == 0)
726 isThreadable = true;
727 else if (strcmp(tmpFlag, "no") == 0)
728 isThreadable = false;
729 else {
730 ALOGE("Invalid threadable flag!: %s", tmpFlag);
731 return nullptr;
732 }
733
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800734#endif
735
Yang Nid9bae682015-01-20 15:31:15 -0800736 return new ScriptExecutable(
737 RSContext, fieldAddress, fieldIsObject, invokeFunctions,
Pirama Arumuga Nainar68173de2015-01-28 12:12:36 -0800738 forEachFunctions, forEachSignatures, pragmaKeys, pragmaValues,
739 isThreadable);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800740}
741
Jason Sams709a0972012-11-15 18:18:04 -0800742bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
743 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800744 uint32_t flags, char const *bccPluginName) {
Jason Sams709a0972012-11-15 18:18:04 -0800745 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
746 //ALOGE("rsdScriptInit %p %p", rsc, script);
747
748 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700749#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800750 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800751
Chris Wailes44bef6f2014-08-12 13:51:10 -0700752 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800753
Jason Sams709a0972012-11-15 18:18:04 -0800754 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700755 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800756 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
757 mCtx->unlockMutex();
758 return false;
759 }
760
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700761 // Run any compiler setup functions we have been provided with.
762 RSSetupCompilerCallback setupCompilerCallback =
763 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700764 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700765 setupCompilerCallback(mCompilerDriver);
766 }
767
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700768 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
769 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700770 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700771 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700772 return false;
773 }
774
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700775 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700776
777 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700778 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800779 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700780 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700781
Chris Wailes6847e732014-08-11 17:30:51 -0700782 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700783 bcFileName.append("/");
784 bcFileName.append(resName);
785 bcFileName.append(".bc");
786
787 std::vector<const char*> compileArguments;
788 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
789 useRSDebugContext, bccPluginName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700790 // The last argument of compileArguments ia a nullptr, so remove 1 from the size.
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700791 std::string compileCommandLine =
792 bcc::getCommandLine(compileArguments.size() - 1, compileArguments.data());
793
Tim Murraybf96a522015-01-23 15:37:03 -0800794 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800795 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700796 }
797
798 // If we can't, it's either not there or out of date. We compile the bit code and try loading
799 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800800 if (mScriptSO == nullptr) {
801 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
802 compileArguments.data(), compileCommandLine))
803 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700804 ALOGE("bcc: FAILS to compile '%s'", resName);
805 mCtx->unlockMutex();
806 return false;
807 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800808
Yang Ni1c44cb62015-01-22 12:02:27 -0800809 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800810 ALOGE("Linker: Failed to link object file '%s'", resName);
811 mCtx->unlockMutex();
812 return false;
813 }
814
Yang Ni1c44cb62015-01-22 12:02:27 -0800815 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800816 if (mScriptSO == nullptr) {
817 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700818 mCtx->unlockMutex();
819 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700820 }
821 }
Jason Sams709a0972012-11-15 18:18:04 -0800822
Stephen Hines45e753a2015-01-19 20:58:44 -0800823 // Read RS symbol information from the .so.
824 if ( !mScriptSO) {
825 goto error;
Jason Sams709a0972012-11-15 18:18:04 -0800826 }
827
Stephen Hines45e753a2015-01-19 20:58:44 -0800828 if ( !storeRSInfoFromSO()) {
829 goto error;
Tim Murray29809d12014-05-28 12:04:19 -0700830 }
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700831#else // RS_COMPATIBILITY_LIB is defined
Miao Wangf3213d72015-01-14 10:03:07 -0800832 const char *nativeLibDir = mCtx->getContext()->getNativeLibDir();
833 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir);
Jason Sams110f1812013-03-14 16:02:18 -0700834
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800835 if (!mScriptSO) {
836 goto error;
837 }
Jason Sams110f1812013-03-14 16:02:18 -0700838
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800839 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700840 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700841 }
842#endif
Jason Sams709a0972012-11-15 18:18:04 -0800843 mCtx->unlockMutex();
844 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700845
Jason Sams110f1812013-03-14 16:02:18 -0700846error:
847
848 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700849 if (mScriptSO) {
850 dlclose(mScriptSO);
851 }
852 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800853}
854
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700855#ifndef RS_COMPATIBILITY_LIB
856
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700857const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
858 size_t bitcodeSize) {
859 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
860
861 // If we're debugging, use the debug library.
862 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
863 return SYSLIBPATH"/libclcore_debug.bc";
864 }
865
866 // If a callback has been registered to specify a library, use that.
867 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700868 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700869 return selectRTCallback((const char*)bitcode, bitcodeSize);
870 }
871
872 // Check for a platform specific library
873#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
874 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700875 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700876 // NEON-capable ARMv7a devices can use an accelerated math library
877 // for all reduced precision scripts.
878 // ARMv8 does not use NEON, as ASIMD can be used with all precision
879 // levels.
880 return SYSLIBPATH"/libclcore_neon.bc";
881 } else {
882 return defaultLib;
883 }
884#elif defined(__i386__) || defined(__x86_64__)
885 // x86 devices will use an optimized library.
886 return SYSLIBPATH"/libclcore_x86.bc";
887#else
888 return defaultLib;
889#endif
890}
891
892#endif
893
Jason Sams709a0972012-11-15 18:18:04 -0800894void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700895 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800896 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
897 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainar577194a2015-01-23 14:27:33 -0800898 script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();;
899 script->mHal.info.exportedPragmaKeyList =
900 const_cast<const char**>(&mScriptExec->getPragmaKeys().front());
901 script->mHal.info.exportedPragmaValueList =
902 const_cast<const char**>(&mScriptExec->getPragmaValues().front());
Jason Sams110f1812013-03-14 16:02:18 -0700903
904 // Bug, need to stash in metadata
905 if (mRootExpand) {
906 script->mHal.info.root = mRootExpand;
907 } else {
908 script->mHal.info.root = mRoot;
909 }
Jason Sams709a0972012-11-15 18:18:04 -0800910}
911
Jason Sams709a0972012-11-15 18:18:04 -0800912
913typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
914
Jason Samsbf2111d2015-01-26 18:13:41 -0800915bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
Chris Wailesf3712132014-07-16 15:18:30 -0700916 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700917 Allocation * aout,
918 const void * usr, uint32_t usrLen,
919 const RsScriptCall *sc,
920 MTLaunchStruct *mtls) {
921
922 memset(mtls, 0, sizeof(MTLaunchStruct));
923
Chris Wailesf3712132014-07-16 15:18:30 -0700924 for (int index = inLen; --index >= 0;) {
925 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700926
Chris Wailesf3712132014-07-16 15:18:30 -0700927 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700928 if (ain != nullptr &&
929 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
930
Chris Wailesf3712132014-07-16 15:18:30 -0700931 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
932 "rsForEach called with null in allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800933 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700934 }
935 }
936
Chris Wailes44bef6f2014-08-12 13:51:10 -0700937 if (aout &&
938 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
939
Chris Wailesf3712132014-07-16 15:18:30 -0700940 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
941 "rsForEach called with null out allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800942 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700943 }
944
Chris Wailesf3712132014-07-16 15:18:30 -0700945 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700946 const Allocation *ain0 = ains[0];
947 const Type *inType = ain0->getType();
948
Jason Samsc0d68472015-01-20 14:29:52 -0800949 mtls->fep.dim.x = inType->getDimX();
950 mtls->fep.dim.y = inType->getDimY();
951 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700952
953 for (int Index = inLen; --Index >= 1;) {
954 if (!ain0->hasSameDims(ains[Index])) {
955 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
956 "Failed to launch kernel; dimensions of input and output allocations do not match.");
957
Jason Samsbf2111d2015-01-26 18:13:41 -0800958 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700959 }
960 }
961
Chris Wailes44bef6f2014-08-12 13:51:10 -0700962 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700963 const Type *outType = aout->getType();
964
Jason Samsc0d68472015-01-20 14:29:52 -0800965 mtls->fep.dim.x = outType->getDimX();
966 mtls->fep.dim.y = outType->getDimY();
967 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700968
969 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700970 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
971 "rsForEach called with null allocations");
Jason Samsbf2111d2015-01-26 18:13:41 -0800972 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700973 }
974
Chris Wailes44bef6f2014-08-12 13:51:10 -0700975 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700976 if (!ains[0]->hasSameDims(aout)) {
977 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
978 "Failed to launch kernel; dimensions of input and output allocations do not match.");
979
Jason Samsbf2111d2015-01-26 18:13:41 -0800980 return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700981 }
982 }
983
984 if (!sc || (sc->xEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800985 mtls->end.x = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700986 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800987 mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart);
988 mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd);
989 if (mtls->start.x >= mtls->end.x) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700990 }
991
992 if (!sc || (sc->yEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -0800993 mtls->end.y = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700994 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -0800995 mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart);
996 mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd);
997 if (mtls->start.y >= mtls->end.y) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700998 }
999
1000 if (!sc || (sc->zEnd == 0)) {
Jason Samsbf2111d2015-01-26 18:13:41 -08001001 mtls->end.z = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001002 } else {
Jason Samsbf2111d2015-01-26 18:13:41 -08001003 mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart);
1004 mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd);
1005 if (mtls->start.z >= mtls->end.z) return false;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001006 }
1007
Jason Samsbf2111d2015-01-26 18:13:41 -08001008 if (!sc || (sc->arrayEnd == 0)) {
1009 mtls->end.array[0] = mtls->fep.dim.array[0];
1010 } else {
1011 mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart);
1012 mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd);
1013 if (mtls->start.array[0] >= mtls->end.array[0]) return false;
1014 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001015
Jason Samsbf2111d2015-01-26 18:13:41 -08001016 if (!sc || (sc->array2End == 0)) {
1017 mtls->end.array[1] = mtls->fep.dim.array[1];
1018 } else {
1019 mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start);
1020 mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End);
1021 if (mtls->start.array[1] >= mtls->end.array[1]) return false;
1022 }
1023
1024 if (!sc || (sc->array3End == 0)) {
1025 mtls->end.array[2] = mtls->fep.dim.array[2];
1026 } else {
1027 mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start);
1028 mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End);
1029 if (mtls->start.array[2] >= mtls->end.array[2]) return false;
1030 }
1031
1032 if (!sc || (sc->array4End == 0)) {
1033 mtls->end.array[3] = mtls->fep.dim.array[3];
1034 } else {
1035 mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start);
1036 mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End);
1037 if (mtls->start.array[3] >= mtls->end.array[3]) return false;
1038 }
1039
1040
1041 // The X & Y walkers always want 0-1 min even if dim is not present
1042 mtls->end.x = rsMax((uint32_t)1, mtls->end.x);
1043 mtls->end.y = rsMax((uint32_t)1, mtls->end.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001044
1045 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -08001046 if (ains) {
1047 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
1048 }
1049 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001050 mtls->fep.usr = usr;
1051 mtls->fep.usrLen = usrLen;
1052 mtls->mSliceSize = 1;
1053 mtls->mSliceNum = 0;
1054
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001055 mtls->isThreadable = mIsThreadable;
1056
Chris Wailesf3712132014-07-16 15:18:30 -07001057 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -07001058 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001059 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -08001060 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
1061 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001062 }
1063 }
1064
Chris Wailes44bef6f2014-08-12 13:51:10 -07001065 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -08001066 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
1067 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001068 }
Jason Samsbf2111d2015-01-26 18:13:41 -08001069
1070 // All validation passed, ok to launch threads
1071 return true;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001072}
1073
Jason Sams709a0972012-11-15 18:18:04 -08001074
1075void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -07001076 const Allocation ** ains,
1077 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -08001078 Allocation * aout,
1079 const void * usr,
1080 uint32_t usrLen,
1081 const RsScriptCall *sc) {
1082
1083 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001084
Jason Samsbf2111d2015-01-26 18:13:41 -08001085 if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) {
1086 forEachKernelSetup(slot, &mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001087
Jason Samsbf2111d2015-01-26 18:13:41 -08001088 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1089 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
1090 mCtx->setTLS(oldTLS);
1091 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001092}
1093
Jason Sams709a0972012-11-15 18:18:04 -08001094void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -08001095 mtls->script = this;
1096 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -08001097 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001098 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -08001099 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -08001100}
1101
1102int RsdCpuScriptImpl::invokeRoot() {
1103 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1104 int ret = mRoot();
1105 mCtx->setTLS(oldTLS);
1106 return ret;
1107}
1108
1109void RsdCpuScriptImpl::invokeInit() {
1110 if (mInit) {
1111 mInit();
1112 }
1113}
1114
1115void RsdCpuScriptImpl::invokeFreeChildren() {
1116 if (mFreeChildren) {
1117 mFreeChildren();
1118 }
1119}
1120
1121void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
1122 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001123 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001124 void * ap = nullptr;
1125
1126#if defined(__x86_64__)
1127 // The invoked function could have input parameter of vector type for example float4 which
1128 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
1129 // So try to align void* params before passing them into RS exported function.
1130
1131 if ((uint8_t)(uint64_t)params & 0x0F) {
1132 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
1133 memcpy(ap, params, paramLength);
1134 } else {
1135 ALOGE("x86_64: invokeFunction memalign error, still use params which is not 16 bytes aligned.");
1136 }
1137 }
1138#endif
Jason Sams709a0972012-11-15 18:18:04 -08001139
1140 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001141 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -08001142 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001143
Jason Sams709a0972012-11-15 18:18:04 -08001144 mCtx->setTLS(oldTLS);
1145}
1146
1147void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
1148 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001149 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -08001150
1151 //if (mIntrinsicID) {
1152 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
1153 //return;
1154 //}
1155
Yang Nid9bae682015-01-20 15:31:15 -08001156 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001157 if (!destPtr) {
1158 //ALOGV("Calling setVar on slot = %i which is null", slot);
1159 return;
1160 }
1161
1162 memcpy(destPtr, data, dataLength);
1163}
1164
Tim Murray9c642392013-04-11 13:29:59 -07001165void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
1166 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001167 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -07001168
Yang Nid9bae682015-01-20 15:31:15 -08001169 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -07001170 if (!srcPtr) {
1171 //ALOGV("Calling setVar on slot = %i which is null", slot);
1172 return;
1173 }
1174 memcpy(data, srcPtr, dataLength);
1175}
1176
1177
Jason Sams709a0972012-11-15 18:18:04 -08001178void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
1179 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -07001180 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -08001181 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001182 if (!destPtr) {
1183 //ALOGV("Calling setVar on slot = %i which is null", slot);
1184 return;
1185 }
1186
1187 // We want to look at dimension in terms of integer components,
1188 // but dimLength is given in terms of bytes.
1189 dimLength /= sizeof(int);
1190
1191 // Only a single dimension is currently supported.
1192 rsAssert(dimLength == 1);
1193 if (dimLength == 1) {
1194 // First do the increment loop.
1195 size_t stride = elem->getSizeBytes();
1196 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001197 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001198 elem->incRefs(cVal);
1199 cVal += stride;
1200 }
1201
1202 // Decrement loop comes after (to prevent race conditions).
1203 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001204 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001205 elem->decRefs(oldVal);
1206 oldVal += stride;
1207 }
1208 }
1209
1210 memcpy(destPtr, data, dataLength);
1211}
1212
1213void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
1214
1215 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001216 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001217
Yang Nid9bae682015-01-20 15:31:15 -08001218 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001219 if (!destPtr) {
1220 //ALOGV("Calling setVar on slot = %i which is null", slot);
1221 return;
1222 }
1223
Chris Wailes44bef6f2014-08-12 13:51:10 -07001224 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001225 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001226 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -08001227 ptr = data->mHal.drvState.lod[0].mallocPtr;
1228 }
1229 memcpy(destPtr, &ptr, sizeof(void *));
1230}
1231
1232void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
1233
1234 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001235 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001236
Yang Nid9bae682015-01-20 15:31:15 -08001237 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001238 if (!destPtr) {
1239 //ALOGV("Calling setVar on slot = %i which is null", slot);
1240 return;
1241 }
1242
Jason Sams05ef73f2014-08-05 14:59:22 -07001243 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -08001244}
1245
1246RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -07001247#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -08001248 if (mCompilerDriver) {
1249 delete mCompilerDriver;
1250 }
Stephen Hines45e753a2015-01-19 20:58:44 -08001251#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001252
Yang Nid9bae682015-01-20 15:31:15 -08001253 if (mScriptExec != nullptr) {
1254 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001255 }
Jason Sams110f1812013-03-14 16:02:18 -07001256 if (mBoundAllocs) delete[] mBoundAllocs;
1257 if (mScriptSO) {
1258 dlclose(mScriptSO);
1259 }
Jason Sams709a0972012-11-15 18:18:04 -08001260}
1261
1262Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1263 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001264 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001265 }
1266
1267 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1268 Allocation *a = mBoundAllocs[ct];
1269 if (!a) continue;
1270 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1271 return a;
1272 }
1273 }
1274 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001275 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001276}
1277
Chris Wailesf3712132014-07-16 15:18:30 -07001278void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1279 uint32_t inLen, Allocation * aout,
1280 const void * usr, uint32_t usrLen,
1281 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001282
Chris Wailesf3712132014-07-16 15:18:30 -07001283void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1284 uint32_t inLen, Allocation * aout,
1285 const void * usr, uint32_t usrLen,
1286 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001287
Jason Sams709a0972012-11-15 18:18:04 -08001288
1289}
1290}