blob: 454b06774145791c3bc07b118e96b846899403c6 [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) {
116
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700117#ifndef RS_SERVER
118 std::string scriptSOName(cacheDir);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800119#ifdef RS_COMPATIBILITY_LIB
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700120 size_t cutPos = scriptSOName.rfind("cache");
121 if (cutPos != std::string::npos) {
122 scriptSOName.erase(cutPos);
123 } else {
124 ALOGE("Found peculiar cacheDir (missing \"cache\"): %s", cacheDir);
125 }
126 scriptSOName.append("/lib/librs.");
127#else
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800128 scriptSOName.append("/librs.");
129#endif
130
131#else
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700132 std::string scriptSOName("lib");
133#endif
134 scriptSOName.append(resName);
135 scriptSOName.append(".so");
136
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800137 return scriptSOName;
138}
139
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800140#ifndef RS_COMPATIBILITY_LIB
141
Stephen Hinesba17ae42013-06-05 17:18:04 -0700142static bool is_force_recompile() {
143#ifdef RS_SERVER
144 return false;
145#else
146 char buf[PROPERTY_VALUE_MAX];
147
148 // Re-compile if floating point precision has been overridden.
149 property_get("debug.rs.precision", buf, "");
150 if (buf[0] != '\0') {
151 return true;
152 }
153
154 // Re-compile if debug.rs.forcerecompile is set.
155 property_get("debug.rs.forcerecompile", buf, "0");
156 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
157 return true;
158 } else {
159 return false;
160 }
161#endif // RS_SERVER
162}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700163
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700164const static char *BCC_EXE_PATH = "/system/bin/bcc";
165
Chris Wailes6847e732014-08-11 17:30:51 -0700166static void setCompileArguments(std::vector<const char*>* args,
167 const std::string& bcFileName,
168 const char* cacheDir, const char* resName,
169 const char* core_lib, bool useRSDebugContext,
170 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700171 rsAssert(cacheDir && resName && core_lib);
172 args->push_back(BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -0800173 args->push_back("-unroll-runtime");
174 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700175 args->push_back("-o");
176 args->push_back(resName);
177 args->push_back("-output_path");
178 args->push_back(cacheDir);
179 args->push_back("-bclib");
180 args->push_back(core_lib);
181 args->push_back("-mtriple");
182 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
183
Tim Murray358ffb82014-12-09 11:53:06 -0800184 // Enable workaround for A53 codegen by default.
185#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
186 args->push_back("-aarch64-fix-cortex-a53-835769");
187#endif
188
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700189 // Execute the bcc compiler.
190 if (useRSDebugContext) {
191 args->push_back("-rs-debug-ctx");
192 } else {
193 // Only load additional libraries for compiles that don't use
194 // the debug context.
195 if (bccPluginName && strlen(bccPluginName) > 0) {
196 args->push_back("-load");
197 args->push_back(bccPluginName);
198 }
199 }
200
Stephen Hines45e753a2015-01-19 20:58:44 -0800201 args->push_back("-fPIC");
202 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800203
Chris Wailes6847e732014-08-11 17:30:51 -0700204 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700205 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700206}
207
Chris Wailes6847e732014-08-11 17:30:51 -0700208static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700209 const char *bitcode,
210 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700211 const char **compileArguments,
212 const std::string &compileCommandLine) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700213 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700214
Chris Wailes6847e732014-08-11 17:30:51 -0700215 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700216 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700217 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700218 return false;
219 }
220 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
221 fclose(bcfile);
222 if (nwritten != bitcodeSize) {
223 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700224 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700225 return false;
226 }
227
228 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800229
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700230 switch (pid) {
231 case -1: { // Error occurred (we attempt no recovery)
232 ALOGE("Couldn't fork for bcc compiler execution");
233 return false;
234 }
235 case 0: { // Child process
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700236 ALOGV("Invoking BCC with: %s", compileCommandLine.c_str());
237 execv(BCC_EXE_PATH, (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800238
Stephen Hines00511322014-01-31 11:20:23 -0800239 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700240 abort();
241 return false;
242 }
243 default: { // Parent process (actual driver)
244 // Wait on child process to finish compiling the source.
245 int status = 0;
246 pid_t w = waitpid(pid, &status, 0);
247 if (w == -1) {
248 ALOGE("Could not wait for bcc compiler");
249 return false;
250 }
251
252 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
253 return true;
254 }
255
256 ALOGE("bcc compiler terminated unexpectedly");
257 return false;
258 }
259 }
260}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700261
Yang Ni1c44cb62015-01-22 12:02:27 -0800262#endif // !defined(RS_COMPATIBILITY_LIB)
263} // namespace
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800264
Yang Ni1c44cb62015-01-22 12:02:27 -0800265namespace android {
266namespace renderscript {
267
268const char* SharedLibraryUtils::LD_EXE_PATH = "/system/bin/ld.mc";
269const char* SharedLibraryUtils::RS_CACHE_DIR = "com.android.renderscript.cache";
270
271#ifndef RS_COMPATIBILITY_LIB
272
273bool SharedLibraryUtils::createSharedLibrary(const char *cacheDir, const char *resName) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800274 std::string sharedLibName = findSharedObjectName(cacheDir, resName);
275 std::string objFileName = cacheDir;
276 objFileName.append("/");
277 objFileName.append(resName);
278 objFileName.append(".o");
279
280 const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
281 std::vector<const char *> args = {
282 LD_EXE_PATH,
283 "-shared",
284 "-nostdlib",
285 compiler_rt,
286 "-mtriple", DEFAULT_TARGET_TRIPLE_STRING,
287 "-L", SYSLIBPATH,
288 "-lRSDriver", "-lm", "-lc",
289 objFileName.c_str(),
290 "-o", sharedLibName.c_str(),
291 nullptr
292 };
293
294 std::string cmdLineStr = bcc::getCommandLine(args.size()-1, args.data());
295
296 pid_t pid = fork();
297
298 switch (pid) {
299 case -1: { // Error occurred (we attempt no recovery)
300 ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH);
301 return false;
302 }
303 case 0: { // Child process
304 ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str());
305 execv(LD_EXE_PATH, (char* const*) args.data());
306
307 ALOGE("execv() failed: %s", strerror(errno));
308 abort();
309 return false;
310 }
311 default: { // Parent process (actual driver)
312 // Wait on child process to finish compiling the source.
313 int status = 0;
314 pid_t w = waitpid(pid, &status, 0);
315 if (w == -1) {
316 ALOGE("Could not wait for linker (%s)", LD_EXE_PATH);
317 return false;
318 }
319
320 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
321 return true;
322 }
323
324 ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH);
325 return false;
326 }
327 }
328}
Stephen Hinesba17ae42013-06-05 17:18:04 -0700329
Yang Ni1c44cb62015-01-22 12:02:27 -0800330#endif // RS_COMPATIBILITY_LIB
331
332void* SharedLibraryUtils::loadSharedLibrary(const char *cacheDir, const char *resName) {
333 void *loaded = nullptr;
334
335 std::string scriptSOName = findSharedObjectName(cacheDir, resName);
336
337 // We should check if we can load the library from the standard app
338 // location for shared libraries first.
339 loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName);
340
341 if (loaded == nullptr) {
342 ALOGE("Unable to open shared library (%s): %s",
343 scriptSOName.c_str(), dlerror());
344
345#ifdef RS_COMPATIBILITY_LIB
346 // One final attempt to find the library in "/system/lib".
347 // We do this to allow bundled applications to use the compatibility
348 // library fallback path. Those applications don't have a private
349 // library path, so they need to install to the system directly.
350 // Note that this is really just a testing path.
351 std::string scriptSONameSystem("/system/lib/librs.");
352 scriptSONameSystem.append(resName);
353 scriptSONameSystem.append(".so");
354 loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir,
355 resName);
356 if (loaded == nullptr) {
357 ALOGE("Unable to open system shared library (%s): %s",
358 scriptSONameSystem.c_str(), dlerror());
359 }
360#endif
361 }
362
363 return loaded;
364}
365
366void* SharedLibraryUtils::loadSOHelper(const char *origName, const char *cacheDir,
367 const char *resName) {
368 // Keep track of which .so libraries have been loaded. Once a library is
369 // in the set (per-process granularity), we must instead make a copy of
370 // the original shared object (randomly named .so file) and load that one
371 // instead. If we don't do this, we end up aliasing global data between
372 // the various Script instances (which are supposed to be completely
373 // independent).
374 static std::set<std::string> LoadedLibraries;
375
376 void *loaded = nullptr;
377
378 // Skip everything if we don't even have the original library available.
379 if (access(origName, F_OK) != 0) {
380 return nullptr;
381 }
382
383 // Common path is that we have not loaded this Script/library before.
384 if (LoadedLibraries.find(origName) == LoadedLibraries.end()) {
385 loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL);
386 if (loaded) {
387 LoadedLibraries.insert(origName);
388 }
389 return loaded;
390 }
391
392 std::string newName(cacheDir);
393
394 // Append RS_CACHE_DIR only if it is not found in cacheDir
395 // In driver mode, RS_CACHE_DIR is already appended to cacheDir.
396 if (newName.find(RS_CACHE_DIR) == std::string::npos) {
397 newName.append("/");
398 newName.append(RS_CACHE_DIR);
399 newName.append("/");
400 }
401
402 if (!ensureCacheDirExists(newName.c_str())) {
403 ALOGE("Could not verify or create cache dir: %s", cacheDir);
404 return nullptr;
405 }
406
407 // Construct an appropriately randomized filename for the copy.
408 newName.append("librs.");
409 newName.append(resName);
410 newName.append("#");
411 newName.append(getRandomString(6)); // 62^6 potential filename variants.
412 newName.append(".so");
413
414 int r = copyFile(newName.c_str(), origName);
415 if (r != 0) {
416 ALOGE("Could not create copy %s -> %s", origName, newName.c_str());
417 return nullptr;
418 }
419 loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL);
420 r = unlink(newName.c_str());
421 if (r != 0) {
422 ALOGE("Could not unlink copy %s", newName.c_str());
423 }
424 if (loaded) {
425 LoadedLibraries.insert(newName.c_str());
426 }
427
428 return loaded;
429}
Jason Sams709a0972012-11-15 18:18:04 -0800430
Jason Sams110f1812013-03-14 16:02:18 -0700431#define MAXLINE 500
432#define MAKE_STR_HELPER(S) #S
433#define MAKE_STR(S) MAKE_STR_HELPER(S)
434#define EXPORT_VAR_STR "exportVarCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700435#define EXPORT_FUNC_STR "exportFuncCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700436#define EXPORT_FOREACH_STR "exportForEachCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700437#define OBJECT_SLOT_STR "objectSlotCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700438
439// Copy up to a newline or size chars from str -> s, updating str
Chris Wailes44bef6f2014-08-12 13:51:10 -0700440// Returns s when successful and nullptr when '\0' is finally reached.
Jason Sams110f1812013-03-14 16:02:18 -0700441static char* strgets(char *s, int size, const char **ppstr) {
442 if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700443 return nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700444 }
445
446 int i;
447 for (i = 0; i < (size - 1); i++) {
448 s[i] = **ppstr;
449 (*ppstr)++;
450 if (s[i] == '\0') {
451 return s;
452 } else if (s[i] == '\n') {
453 s[i+1] = '\0';
454 return s;
455 }
456 }
457
458 // size has been exceeded.
459 s[i] = '\0';
460
461 return s;
462}
Jason Sams709a0972012-11-15 18:18:04 -0800463
464RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
465 mCtx = ctx;
466 mScript = s;
467
Chris Wailes44bef6f2014-08-12 13:51:10 -0700468 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800469
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800470#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700471 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700472#endif
473
Tim Murraye195a3f2014-03-13 15:04:58 -0700474
Chris Wailes44bef6f2014-08-12 13:51:10 -0700475 mRoot = nullptr;
476 mRootExpand = nullptr;
477 mInit = nullptr;
478 mFreeChildren = nullptr;
Yang Nid9bae682015-01-20 15:31:15 -0800479 mScriptExec = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800480
Chris Wailes44bef6f2014-08-12 13:51:10 -0700481 mBoundAllocs = nullptr;
482 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800483 mIsThreadable = true;
484}
485
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800486bool RsdCpuScriptImpl::storeRSInfoFromSO() {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800487 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
488 if (mRoot) {
489 //ALOGE("Found root(): %p", mRoot);
490 }
491 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
492 if (mRootExpand) {
493 //ALOGE("Found root.expand(): %p", mRootExpand);
494 }
495 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
496 if (mInit) {
497 //ALOGE("Found init(): %p", mInit);
498 }
499 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
500 if (mFreeChildren) {
501 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
502 }
503
Yang Nid9bae682015-01-20 15:31:15 -0800504 mScriptExec = ScriptExecutable::createFromSharedObject(
505 mCtx->getContext(), mScriptSO);
506
507 if (mScriptExec == nullptr) {
508 return false;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800509 }
510
Yang Nid9bae682015-01-20 15:31:15 -0800511 size_t varCount = mScriptExec->getExportedVariableCount();
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800512 if (varCount > 0) {
513 mBoundAllocs = new Allocation *[varCount];
514 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
515 }
516
Yang Nid9bae682015-01-20 15:31:15 -0800517 return true;
518}
519
520ScriptExecutable* ScriptExecutable::createFromSharedObject(
521 Context* RSContext, void* sharedObj) {
522 char line[MAXLINE];
523
524 size_t varCount = 0;
525 size_t funcCount = 0;
526 size_t forEachCount = 0;
527 size_t objectSlotCount = 0;
528
529 const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info");
530
531 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
532 return nullptr;
533 }
534 if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
535 ALOGE("Invalid export var count!: %s", line);
536 return nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800537 }
538
Yang Nid9bae682015-01-20 15:31:15 -0800539 std::vector<void*> fieldAddress;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800540
Yang Nid9bae682015-01-20 15:31:15 -0800541 for (size_t i = 0; i < varCount; ++i) {
542 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
543 return nullptr;
544 }
545 char *c = strrchr(line, '\n');
546 if (c) {
547 *c = '\0';
548 }
549 void* addr = dlsym(sharedObj, line);
550 if (addr == nullptr) {
551 ALOGE("Failed to find variable address for %s: %s",
552 line, dlerror());
553 // Not a critical error if we don't find a global variable.
554 }
555 fieldAddress.push_back(addr);
556 }
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800557
Yang Nid9bae682015-01-20 15:31:15 -0800558 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
559 return nullptr;
560 }
561 if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
562 ALOGE("Invalid export func count!: %s", line);
563 return nullptr;
564 }
565
566 std::vector<InvokeFunc_t> invokeFunctions(funcCount);
567
568 for (size_t i = 0; i < funcCount; ++i) {
569 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
570 return nullptr ;
571 }
572 char *c = strrchr(line, '\n');
573 if (c) {
574 *c = '\0';
575 }
576
577 invokeFunctions[i] = (InvokeFunc_t) dlsym(sharedObj, line);
578 if (invokeFunctions[i] == nullptr) {
579 ALOGE("Failed to get function address for %s(): %s",
580 line, dlerror());
581 return nullptr;
582 }
583 }
584
585 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
586 return nullptr;
587 }
588 if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
589 ALOGE("Invalid export forEach count!: %s", line);
590 return nullptr;
591 }
592
593 std::vector<ForEachFunc_t> forEachFunctions(forEachCount);
594 std::vector<uint32_t> forEachSignatures(forEachCount);
595
596 for (size_t i = 0; i < forEachCount; ++i) {
597 unsigned int tmpSig = 0;
598 char tmpName[MAXLINE];
599
600 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
601 return nullptr;
602 }
603 if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
604 &tmpSig, tmpName) != 2) {
605 ALOGE("Invalid export forEach!: %s", line);
606 return nullptr;
607 }
608
609 // Lookup the expanded ForEach kernel.
610 strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
611 forEachSignatures[i] = tmpSig;
612 forEachFunctions[i] =
613 (ForEachFunc_t) dlsym(sharedObj, tmpName);
614 if (i != 0 && forEachFunctions[i] == nullptr) {
615 // Ignore missing root.expand functions.
616 // root() is always specified at location 0.
617 ALOGE("Failed to find forEach function address for %s: %s",
618 tmpName, dlerror());
619 return nullptr;
620 }
621 }
622
623 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
624 return nullptr;
625 }
626 if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
627 ALOGE("Invalid object slot count!: %s", line);
628 return nullptr;
629 }
630
631 std::vector<bool> fieldIsObject(varCount, false);
632
633 rsAssert(varCount > 0);
634 for (size_t i = 0; i < objectSlotCount; ++i) {
635 uint32_t varNum = 0;
636 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
637 return nullptr;
638 }
639 if (sscanf(line, "%u", &varNum) != 1) {
640 ALOGE("Invalid object slot!: %s", line);
641 return nullptr;
642 }
643
644 if (varNum < varCount) {
645 fieldIsObject[varNum] = true;
646 }
647 }
648
649 return new ScriptExecutable(
650 RSContext, fieldAddress, fieldIsObject, invokeFunctions,
651 forEachFunctions, forEachSignatures);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800652}
653
Jason Sams709a0972012-11-15 18:18:04 -0800654bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
655 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800656 uint32_t flags, char const *bccPluginName) {
Jason Sams709a0972012-11-15 18:18:04 -0800657 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
658 //ALOGE("rsdScriptInit %p %p", rsc, script);
659
660 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700661#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800662 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800663
Chris Wailes44bef6f2014-08-12 13:51:10 -0700664 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800665
Jason Sams709a0972012-11-15 18:18:04 -0800666 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700667 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800668 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
669 mCtx->unlockMutex();
670 return false;
671 }
672
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700673 // Run any compiler setup functions we have been provided with.
674 RSSetupCompilerCallback setupCompilerCallback =
675 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700676 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700677 setupCompilerCallback(mCompilerDriver);
678 }
679
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700680 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
681 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700682 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700683 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700684 return false;
685 }
686
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700687 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700688
689 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700690 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800691 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700692 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700693
Chris Wailes6847e732014-08-11 17:30:51 -0700694 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700695 bcFileName.append("/");
696 bcFileName.append(resName);
697 bcFileName.append(".bc");
698
699 std::vector<const char*> compileArguments;
700 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
701 useRSDebugContext, bccPluginName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700702 // The last argument of compileArguments ia a nullptr, so remove 1 from the size.
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700703 std::string compileCommandLine =
704 bcc::getCommandLine(compileArguments.size() - 1, compileArguments.data());
705
Tim Murraybf96a522015-01-23 15:37:03 -0800706 if (!is_force_recompile() && !useRSDebugContext) {
Yang Ni1c44cb62015-01-22 12:02:27 -0800707 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700708 }
709
710 // If we can't, it's either not there or out of date. We compile the bit code and try loading
711 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800712 if (mScriptSO == nullptr) {
713 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
714 compileArguments.data(), compileCommandLine))
715 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700716 ALOGE("bcc: FAILS to compile '%s'", resName);
717 mCtx->unlockMutex();
718 return false;
719 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800720
Yang Ni1c44cb62015-01-22 12:02:27 -0800721 if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
Stephen Hines45e753a2015-01-19 20:58:44 -0800722 ALOGE("Linker: Failed to link object file '%s'", resName);
723 mCtx->unlockMutex();
724 return false;
725 }
726
Yang Ni1c44cb62015-01-22 12:02:27 -0800727 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Stephen Hines45e753a2015-01-19 20:58:44 -0800728 if (mScriptSO == nullptr) {
729 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700730 mCtx->unlockMutex();
731 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700732 }
733 }
Jason Sams709a0972012-11-15 18:18:04 -0800734
Stephen Hines45e753a2015-01-19 20:58:44 -0800735 // Read RS symbol information from the .so.
736 if ( !mScriptSO) {
737 goto error;
Jason Sams709a0972012-11-15 18:18:04 -0800738 }
739
Stephen Hines45e753a2015-01-19 20:58:44 -0800740 if ( !storeRSInfoFromSO()) {
741 goto error;
Tim Murray29809d12014-05-28 12:04:19 -0700742 }
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700743#else // RS_COMPATIBILITY_LIB is defined
Jason Sams110f1812013-03-14 16:02:18 -0700744
Yang Ni1c44cb62015-01-22 12:02:27 -0800745 mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
Jason Sams110f1812013-03-14 16:02:18 -0700746
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800747 if (!mScriptSO) {
748 goto error;
749 }
Jason Sams110f1812013-03-14 16:02:18 -0700750
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800751 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700752 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700753 }
754#endif
Jason Sams709a0972012-11-15 18:18:04 -0800755 mCtx->unlockMutex();
756 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700757
Jason Sams110f1812013-03-14 16:02:18 -0700758error:
759
760 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700761 if (mScriptSO) {
762 dlclose(mScriptSO);
763 }
764 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800765}
766
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700767#ifndef RS_COMPATIBILITY_LIB
768
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700769const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
770 size_t bitcodeSize) {
771 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
772
773 // If we're debugging, use the debug library.
774 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
775 return SYSLIBPATH"/libclcore_debug.bc";
776 }
777
778 // If a callback has been registered to specify a library, use that.
779 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700780 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700781 return selectRTCallback((const char*)bitcode, bitcodeSize);
782 }
783
784 // Check for a platform specific library
785#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
786 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700787 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700788 // NEON-capable ARMv7a devices can use an accelerated math library
789 // for all reduced precision scripts.
790 // ARMv8 does not use NEON, as ASIMD can be used with all precision
791 // levels.
792 return SYSLIBPATH"/libclcore_neon.bc";
793 } else {
794 return defaultLib;
795 }
796#elif defined(__i386__) || defined(__x86_64__)
797 // x86 devices will use an optimized library.
798 return SYSLIBPATH"/libclcore_x86.bc";
799#else
800 return defaultLib;
801#endif
802}
803
804#endif
805
Jason Sams709a0972012-11-15 18:18:04 -0800806void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700807 // Copy info over to runtime
Yang Nid9bae682015-01-20 15:31:15 -0800808 script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount();
809 script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount();
Jason Sams110f1812013-03-14 16:02:18 -0700810 script->mHal.info.exportedPragmaCount = 0;
811 script->mHal.info.exportedPragmaKeyList = 0;
812 script->mHal.info.exportedPragmaValueList = 0;
813
814 // Bug, need to stash in metadata
815 if (mRootExpand) {
816 script->mHal.info.root = mRootExpand;
817 } else {
818 script->mHal.info.root = mRoot;
819 }
Jason Sams709a0972012-11-15 18:18:04 -0800820}
821
Jason Sams709a0972012-11-15 18:18:04 -0800822
823typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
824
Chris Wailesf3712132014-07-16 15:18:30 -0700825void RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
826 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700827 Allocation * aout,
828 const void * usr, uint32_t usrLen,
829 const RsScriptCall *sc,
830 MTLaunchStruct *mtls) {
831
832 memset(mtls, 0, sizeof(MTLaunchStruct));
833
Chris Wailesf3712132014-07-16 15:18:30 -0700834 for (int index = inLen; --index >= 0;) {
835 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700836
Chris Wailesf3712132014-07-16 15:18:30 -0700837 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700838 if (ain != nullptr &&
839 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
840
Chris Wailesf3712132014-07-16 15:18:30 -0700841 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
842 "rsForEach called with null in allocations");
843 return;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700844 }
845 }
846
Chris Wailes44bef6f2014-08-12 13:51:10 -0700847 if (aout &&
848 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
849
Chris Wailesf3712132014-07-16 15:18:30 -0700850 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
851 "rsForEach called with null out allocations");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700852 return;
853 }
854
Chris Wailesf3712132014-07-16 15:18:30 -0700855 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700856 const Allocation *ain0 = ains[0];
857 const Type *inType = ain0->getType();
858
Jason Samsc0d68472015-01-20 14:29:52 -0800859 mtls->fep.dim.x = inType->getDimX();
860 mtls->fep.dim.y = inType->getDimY();
861 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700862
863 for (int Index = inLen; --Index >= 1;) {
864 if (!ain0->hasSameDims(ains[Index])) {
865 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
866 "Failed to launch kernel; dimensions of input and output allocations do not match.");
867
868 return;
869 }
870 }
871
Chris Wailes44bef6f2014-08-12 13:51:10 -0700872 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700873 const Type *outType = aout->getType();
874
Jason Samsc0d68472015-01-20 14:29:52 -0800875 mtls->fep.dim.x = outType->getDimX();
876 mtls->fep.dim.y = outType->getDimY();
877 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700878
879 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700880 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
881 "rsForEach called with null allocations");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700882 return;
883 }
884
Chris Wailes44bef6f2014-08-12 13:51:10 -0700885 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700886 if (!ains[0]->hasSameDims(aout)) {
887 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
888 "Failed to launch kernel; dimensions of input and output allocations do not match.");
889
890 return;
891 }
892 }
893
894 if (!sc || (sc->xEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -0800895 mtls->xEnd = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700896 } else {
Jason Samsc0d68472015-01-20 14:29:52 -0800897 rsAssert(sc->xStart < mtls->fep.dim.x);
898 rsAssert(sc->xEnd <= mtls->fep.dim.x);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700899 rsAssert(sc->xStart < sc->xEnd);
Jason Samsc0d68472015-01-20 14:29:52 -0800900 mtls->xStart = rsMin(mtls->fep.dim.x, sc->xStart);
901 mtls->xEnd = rsMin(mtls->fep.dim.x, sc->xEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700902 if (mtls->xStart >= mtls->xEnd) return;
903 }
904
905 if (!sc || (sc->yEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -0800906 mtls->yEnd = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700907 } else {
Jason Samsc0d68472015-01-20 14:29:52 -0800908 rsAssert(sc->yStart < mtls->fep.dim.y);
909 rsAssert(sc->yEnd <= mtls->fep.dim.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700910 rsAssert(sc->yStart < sc->yEnd);
Jason Samsc0d68472015-01-20 14:29:52 -0800911 mtls->yStart = rsMin(mtls->fep.dim.y, sc->yStart);
912 mtls->yEnd = rsMin(mtls->fep.dim.y, sc->yEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700913 if (mtls->yStart >= mtls->yEnd) return;
914 }
915
916 if (!sc || (sc->zEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -0800917 mtls->zEnd = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700918 } else {
Jason Samsc0d68472015-01-20 14:29:52 -0800919 rsAssert(sc->zStart < mtls->fep.dim.z);
920 rsAssert(sc->zEnd <= mtls->fep.dim.z);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700921 rsAssert(sc->zStart < sc->zEnd);
Jason Samsc0d68472015-01-20 14:29:52 -0800922 mtls->zStart = rsMin(mtls->fep.dim.z, sc->zStart);
923 mtls->zEnd = rsMin(mtls->fep.dim.z, sc->zEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700924 if (mtls->zStart >= mtls->zEnd) return;
925 }
926
927 mtls->xEnd = rsMax((uint32_t)1, mtls->xEnd);
928 mtls->yEnd = rsMax((uint32_t)1, mtls->yEnd);
929 mtls->zEnd = rsMax((uint32_t)1, mtls->zEnd);
930 mtls->arrayEnd = rsMax((uint32_t)1, mtls->arrayEnd);
931
Chris Wailesf3712132014-07-16 15:18:30 -0700932 rsAssert(inLen == 0 || (ains[0]->getType()->getDimZ() == 0));
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700933
934 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800935 if (ains) {
936 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
937 }
938 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700939 mtls->fep.usr = usr;
940 mtls->fep.usrLen = usrLen;
941 mtls->mSliceSize = 1;
942 mtls->mSliceNum = 0;
943
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700944 mtls->isThreadable = mIsThreadable;
945
Chris Wailesf3712132014-07-16 15:18:30 -0700946 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700947 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700948 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800949 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
950 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700951 }
952 }
953
Chris Wailes44bef6f2014-08-12 13:51:10 -0700954 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -0800955 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
956 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700957 }
958}
959
Jason Sams709a0972012-11-15 18:18:04 -0800960
961void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -0700962 const Allocation ** ains,
963 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -0800964 Allocation * aout,
965 const void * usr,
966 uint32_t usrLen,
967 const RsScriptCall *sc) {
968
969 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700970
971 forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls);
972 forEachKernelSetup(slot, &mtls);
973
974 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
975 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
976 mCtx->setTLS(oldTLS);
977}
978
Jason Sams709a0972012-11-15 18:18:04 -0800979void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -0800980 mtls->script = this;
981 mtls->fep.slot = slot;
Yang Nid9bae682015-01-20 15:31:15 -0800982 mtls->kernel = mScriptExec->getForEachFunction(slot);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700983 rsAssert(mtls->kernel != nullptr);
Yang Nid9bae682015-01-20 15:31:15 -0800984 mtls->sig = mScriptExec->getForEachSignature(slot);
Jason Sams709a0972012-11-15 18:18:04 -0800985}
986
987int RsdCpuScriptImpl::invokeRoot() {
988 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
989 int ret = mRoot();
990 mCtx->setTLS(oldTLS);
991 return ret;
992}
993
994void RsdCpuScriptImpl::invokeInit() {
995 if (mInit) {
996 mInit();
997 }
998}
999
1000void RsdCpuScriptImpl::invokeFreeChildren() {
1001 if (mFreeChildren) {
1002 mFreeChildren();
1003 }
1004}
1005
1006void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
1007 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001008 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001009 void * ap = nullptr;
1010
1011#if defined(__x86_64__)
1012 // The invoked function could have input parameter of vector type for example float4 which
1013 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
1014 // So try to align void* params before passing them into RS exported function.
1015
1016 if ((uint8_t)(uint64_t)params & 0x0F) {
1017 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
1018 memcpy(ap, params, paramLength);
1019 } else {
1020 ALOGE("x86_64: invokeFunction memalign error, still use params which is not 16 bytes aligned.");
1021 }
1022 }
1023#endif
Jason Sams709a0972012-11-15 18:18:04 -08001024
1025 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001026 reinterpret_cast<void (*)(const void *, uint32_t)>(
Yang Nid9bae682015-01-20 15:31:15 -08001027 mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001028
Jason Sams709a0972012-11-15 18:18:04 -08001029 mCtx->setTLS(oldTLS);
1030}
1031
1032void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
1033 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001034 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -08001035
1036 //if (mIntrinsicID) {
1037 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
1038 //return;
1039 //}
1040
Yang Nid9bae682015-01-20 15:31:15 -08001041 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001042 if (!destPtr) {
1043 //ALOGV("Calling setVar on slot = %i which is null", slot);
1044 return;
1045 }
1046
1047 memcpy(destPtr, data, dataLength);
1048}
1049
Tim Murray9c642392013-04-11 13:29:59 -07001050void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
1051 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001052 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -07001053
Yang Nid9bae682015-01-20 15:31:15 -08001054 int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Tim Murray9c642392013-04-11 13:29:59 -07001055 if (!srcPtr) {
1056 //ALOGV("Calling setVar on slot = %i which is null", slot);
1057 return;
1058 }
1059 memcpy(data, srcPtr, dataLength);
1060}
1061
1062
Jason Sams709a0972012-11-15 18:18:04 -08001063void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
1064 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -07001065 const uint32_t *dims, size_t dimLength) {
Yang Nid9bae682015-01-20 15:31:15 -08001066 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001067 if (!destPtr) {
1068 //ALOGV("Calling setVar on slot = %i which is null", slot);
1069 return;
1070 }
1071
1072 // We want to look at dimension in terms of integer components,
1073 // but dimLength is given in terms of bytes.
1074 dimLength /= sizeof(int);
1075
1076 // Only a single dimension is currently supported.
1077 rsAssert(dimLength == 1);
1078 if (dimLength == 1) {
1079 // First do the increment loop.
1080 size_t stride = elem->getSizeBytes();
1081 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001082 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001083 elem->incRefs(cVal);
1084 cVal += stride;
1085 }
1086
1087 // Decrement loop comes after (to prevent race conditions).
1088 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001089 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001090 elem->decRefs(oldVal);
1091 oldVal += stride;
1092 }
1093 }
1094
1095 memcpy(destPtr, data, dataLength);
1096}
1097
1098void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
1099
1100 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001101 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001102
Yang Nid9bae682015-01-20 15:31:15 -08001103 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001104 if (!destPtr) {
1105 //ALOGV("Calling setVar on slot = %i which is null", slot);
1106 return;
1107 }
1108
Chris Wailes44bef6f2014-08-12 13:51:10 -07001109 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001110 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001111 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -08001112 ptr = data->mHal.drvState.lod[0].mallocPtr;
1113 }
1114 memcpy(destPtr, &ptr, sizeof(void *));
1115}
1116
1117void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
1118
1119 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001120 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001121
Yang Nid9bae682015-01-20 15:31:15 -08001122 int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot));
Jason Sams709a0972012-11-15 18:18:04 -08001123 if (!destPtr) {
1124 //ALOGV("Calling setVar on slot = %i which is null", slot);
1125 return;
1126 }
1127
Jason Sams05ef73f2014-08-05 14:59:22 -07001128 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -08001129}
1130
1131RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -07001132#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -08001133 if (mCompilerDriver) {
1134 delete mCompilerDriver;
1135 }
Stephen Hines45e753a2015-01-19 20:58:44 -08001136#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001137
Yang Nid9bae682015-01-20 15:31:15 -08001138 if (mScriptExec != nullptr) {
1139 delete mScriptExec;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001140 }
Jason Sams110f1812013-03-14 16:02:18 -07001141 if (mBoundAllocs) delete[] mBoundAllocs;
1142 if (mScriptSO) {
1143 dlclose(mScriptSO);
1144 }
Jason Sams709a0972012-11-15 18:18:04 -08001145}
1146
1147Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1148 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001149 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001150 }
1151
1152 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1153 Allocation *a = mBoundAllocs[ct];
1154 if (!a) continue;
1155 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1156 return a;
1157 }
1158 }
1159 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001160 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001161}
1162
Chris Wailesf3712132014-07-16 15:18:30 -07001163void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1164 uint32_t inLen, Allocation * aout,
1165 const void * usr, uint32_t usrLen,
1166 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001167
Chris Wailesf3712132014-07-16 15:18:30 -07001168void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1169 uint32_t inLen, Allocation * aout,
1170 const void * usr, uint32_t usrLen,
1171 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001172
Jason Sams709a0972012-11-15 18:18:04 -08001173
1174}
1175}