blob: 38a0bf57411937710b32c06603ab9604b7585d74 [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 -0800114#define RS_CACHE_DIR "com.android.renderscript.cache"
115
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700116// Attempt to load the shared library from origName, but then fall back to
Stephen Hines7d774852014-10-01 12:57:57 -0700117// creating a copy of the shared library if necessary (to ensure instancing).
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700118// This function returns the dlopen()-ed handle if successful.
119static void *loadSOHelper(const char *origName, const char *cacheDir,
120 const char *resName) {
121 // Keep track of which .so libraries have been loaded. Once a library is
Stephen Hines7d774852014-10-01 12:57:57 -0700122 // in the set (per-process granularity), we must instead make a copy of
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700123 // the original shared object (randomly named .so file) and load that one
124 // instead. If we don't do this, we end up aliasing global data between
125 // the various Script instances (which are supposed to be completely
126 // independent).
127 static std::set<std::string> LoadedLibraries;
128
Chris Wailes44bef6f2014-08-12 13:51:10 -0700129 void *loaded = nullptr;
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700130
131 // Skip everything if we don't even have the original library available.
132 if (access(origName, F_OK) != 0) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700133 return nullptr;
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700134 }
135
136 // Common path is that we have not loaded this Script/library before.
137 if (LoadedLibraries.find(origName) == LoadedLibraries.end()) {
138 loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL);
139 if (loaded) {
140 LoadedLibraries.insert(origName);
141 }
142 return loaded;
143 }
144
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700145 std::string newName(cacheDir);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800146
147 // Append RS_CACHE_DIR only if it is not found in cacheDir
148 // In driver mode, RS_CACHE_DIR is already appended to cacheDir.
149 if (newName.find(RS_CACHE_DIR) == std::string::npos) {
150 newName.append("/" RS_CACHE_DIR "/");
151 }
Stephen Hinesee48c0b2013-10-30 17:48:30 -0700152
153 if (!ensureCacheDirExists(newName.c_str())) {
154 ALOGE("Could not verify or create cache dir: %s", cacheDir);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700155 return nullptr;
Stephen Hinesee48c0b2013-10-30 17:48:30 -0700156 }
157
Stephen Hines7d774852014-10-01 12:57:57 -0700158 // Construct an appropriately randomized filename for the copy.
Stephen Hinesee48c0b2013-10-30 17:48:30 -0700159 newName.append("librs.");
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700160 newName.append(resName);
161 newName.append("#");
162 newName.append(getRandomString(6)); // 62^6 potential filename variants.
163 newName.append(".so");
164
Stephen Hines7d774852014-10-01 12:57:57 -0700165 int r = copyFile(newName.c_str(), origName);
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700166 if (r != 0) {
Stephen Hines7d774852014-10-01 12:57:57 -0700167 ALOGE("Could not create copy %s -> %s", origName, newName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700168 return nullptr;
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700169 }
170 loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL);
171 r = unlink(newName.c_str());
172 if (r != 0) {
Stephen Hines7d774852014-10-01 12:57:57 -0700173 ALOGE("Could not unlink copy %s", newName.c_str());
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700174 }
175 if (loaded) {
176 LoadedLibraries.insert(newName.c_str());
177 }
178
179 return loaded;
180}
181
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800182static std::string findSharedObjectName(const char *cacheDir,
183 const char *resName) {
184
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700185#ifndef RS_SERVER
186 std::string scriptSOName(cacheDir);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800187#ifdef RS_COMPATIBILITY_LIB
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700188 size_t cutPos = scriptSOName.rfind("cache");
189 if (cutPos != std::string::npos) {
190 scriptSOName.erase(cutPos);
191 } else {
192 ALOGE("Found peculiar cacheDir (missing \"cache\"): %s", cacheDir);
193 }
194 scriptSOName.append("/lib/librs.");
195#else
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800196 scriptSOName.append("/librs.");
197#endif
198
199#else
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700200 std::string scriptSOName("lib");
201#endif
202 scriptSOName.append(resName);
203 scriptSOName.append(".so");
204
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800205 return scriptSOName;
206}
207
208// Load the shared library referred to by cacheDir and resName. If we have
209// already loaded this library, we instead create a new copy (in the
210// cache dir) and then load that. We then immediately destroy the copy.
211// This is required behavior to implement script instancing for the support
212// library, since shared objects are loaded and de-duped by name only.
213static void *loadSharedLibrary(const char *cacheDir, const char *resName) {
214 void *loaded = nullptr;
215
216 std::string scriptSOName = findSharedObjectName(cacheDir, resName);
217
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700218 // We should check if we can load the library from the standard app
219 // location for shared libraries first.
220 loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName);
221
Chris Wailes44bef6f2014-08-12 13:51:10 -0700222 if (loaded == nullptr) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700223 ALOGE("Unable to open shared library (%s): %s",
224 scriptSOName.c_str(), dlerror());
225
Pirama Arumuga Nainarf0558cc2015-01-20 18:27:54 -0800226#ifdef RS_COMPATIBILITY_LIB
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700227 // One final attempt to find the library in "/system/lib".
228 // We do this to allow bundled applications to use the compatibility
229 // library fallback path. Those applications don't have a private
230 // library path, so they need to install to the system directly.
231 // Note that this is really just a testing path.
Chris Wailes93d6bc82014-07-28 16:54:38 -0700232 std::string scriptSONameSystem("/system/lib/librs.");
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700233 scriptSONameSystem.append(resName);
234 scriptSONameSystem.append(".so");
235 loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir,
236 resName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700237 if (loaded == nullptr) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700238 ALOGE("Unable to open system shared library (%s): %s",
239 scriptSONameSystem.c_str(), dlerror());
240 }
Pirama Arumuga Nainarf0558cc2015-01-20 18:27:54 -0800241#endif
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700242 }
243
244 return loaded;
245}
246
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800247#ifndef RS_COMPATIBILITY_LIB
248
Stephen Hinesba17ae42013-06-05 17:18:04 -0700249static bool is_force_recompile() {
250#ifdef RS_SERVER
251 return false;
252#else
253 char buf[PROPERTY_VALUE_MAX];
254
255 // Re-compile if floating point precision has been overridden.
256 property_get("debug.rs.precision", buf, "");
257 if (buf[0] != '\0') {
258 return true;
259 }
260
261 // Re-compile if debug.rs.forcerecompile is set.
262 property_get("debug.rs.forcerecompile", buf, "0");
263 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
264 return true;
265 } else {
266 return false;
267 }
268#endif // RS_SERVER
269}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700270
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700271const static char *BCC_EXE_PATH = "/system/bin/bcc";
272
Chris Wailes6847e732014-08-11 17:30:51 -0700273static void setCompileArguments(std::vector<const char*>* args,
274 const std::string& bcFileName,
275 const char* cacheDir, const char* resName,
276 const char* core_lib, bool useRSDebugContext,
277 const char* bccPluginName) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700278 rsAssert(cacheDir && resName && core_lib);
279 args->push_back(BCC_EXE_PATH);
Tim Murray687cfe82015-01-08 14:59:38 -0800280 args->push_back("-unroll-runtime");
281 args->push_back("-scalarize-load-store");
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700282 args->push_back("-o");
283 args->push_back(resName);
284 args->push_back("-output_path");
285 args->push_back(cacheDir);
286 args->push_back("-bclib");
287 args->push_back(core_lib);
288 args->push_back("-mtriple");
289 args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
290
Tim Murray358ffb82014-12-09 11:53:06 -0800291 // Enable workaround for A53 codegen by default.
292#if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND)
293 args->push_back("-aarch64-fix-cortex-a53-835769");
294#endif
295
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700296 // Execute the bcc compiler.
297 if (useRSDebugContext) {
298 args->push_back("-rs-debug-ctx");
299 } else {
300 // Only load additional libraries for compiles that don't use
301 // the debug context.
302 if (bccPluginName && strlen(bccPluginName) > 0) {
303 args->push_back("-load");
304 args->push_back(bccPluginName);
305 }
306 }
307
Stephen Hines45e753a2015-01-19 20:58:44 -0800308 args->push_back("-fPIC");
309 args->push_back("-embedRSInfo");
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800310
Chris Wailes6847e732014-08-11 17:30:51 -0700311 args->push_back(bcFileName.c_str());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700312 args->push_back(nullptr);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700313}
314
Chris Wailes6847e732014-08-11 17:30:51 -0700315static bool compileBitcode(const std::string &bcFileName,
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700316 const char *bitcode,
317 size_t bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700318 const char **compileArguments,
319 const std::string &compileCommandLine) {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700320 rsAssert(bitcode && bitcodeSize);
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700321
Chris Wailes6847e732014-08-11 17:30:51 -0700322 FILE *bcfile = fopen(bcFileName.c_str(), "w");
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700323 if (!bcfile) {
Chris Wailes6847e732014-08-11 17:30:51 -0700324 ALOGE("Could not write to %s", bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700325 return false;
326 }
327 size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile);
328 fclose(bcfile);
329 if (nwritten != bitcodeSize) {
330 ALOGE("Could not write %zu bytes to %s", bitcodeSize,
Chris Wailes6847e732014-08-11 17:30:51 -0700331 bcFileName.c_str());
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700332 return false;
333 }
334
335 pid_t pid = fork();
Stephen Hines00511322014-01-31 11:20:23 -0800336
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700337 switch (pid) {
338 case -1: { // Error occurred (we attempt no recovery)
339 ALOGE("Couldn't fork for bcc compiler execution");
340 return false;
341 }
342 case 0: { // Child process
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700343 ALOGV("Invoking BCC with: %s", compileCommandLine.c_str());
344 execv(BCC_EXE_PATH, (char* const*)compileArguments);
Stephen Hines00511322014-01-31 11:20:23 -0800345
Stephen Hines00511322014-01-31 11:20:23 -0800346 ALOGE("execv() failed: %s", strerror(errno));
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700347 abort();
348 return false;
349 }
350 default: { // Parent process (actual driver)
351 // Wait on child process to finish compiling the source.
352 int status = 0;
353 pid_t w = waitpid(pid, &status, 0);
354 if (w == -1) {
355 ALOGE("Could not wait for bcc compiler");
356 return false;
357 }
358
359 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
360 return true;
361 }
362
363 ALOGE("bcc compiler terminated unexpectedly");
364 return false;
365 }
366 }
367}
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700368
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800369const static char *LD_EXE_PATH = "/system/bin/ld.mc";
370
371static bool createSharedLib(const char *cacheDir, const char *resName) {
372 std::string sharedLibName = findSharedObjectName(cacheDir, resName);
373 std::string objFileName = cacheDir;
374 objFileName.append("/");
375 objFileName.append(resName);
376 objFileName.append(".o");
377
378 const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
379 std::vector<const char *> args = {
380 LD_EXE_PATH,
381 "-shared",
382 "-nostdlib",
383 compiler_rt,
384 "-mtriple", DEFAULT_TARGET_TRIPLE_STRING,
385 "-L", SYSLIBPATH,
386 "-lRSDriver", "-lm", "-lc",
387 objFileName.c_str(),
388 "-o", sharedLibName.c_str(),
389 nullptr
390 };
391
392 std::string cmdLineStr = bcc::getCommandLine(args.size()-1, args.data());
393
394 pid_t pid = fork();
395
396 switch (pid) {
397 case -1: { // Error occurred (we attempt no recovery)
398 ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH);
399 return false;
400 }
401 case 0: { // Child process
402 ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str());
403 execv(LD_EXE_PATH, (char* const*) args.data());
404
405 ALOGE("execv() failed: %s", strerror(errno));
406 abort();
407 return false;
408 }
409 default: { // Parent process (actual driver)
410 // Wait on child process to finish compiling the source.
411 int status = 0;
412 pid_t w = waitpid(pid, &status, 0);
413 if (w == -1) {
414 ALOGE("Could not wait for linker (%s)", LD_EXE_PATH);
415 return false;
416 }
417
418 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
419 return true;
420 }
421
422 ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH);
423 return false;
424 }
425 }
426}
Stephen Hinesba17ae42013-06-05 17:18:04 -0700427#endif // !defined(RS_COMPATIBILITY_LIB)
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700428} // namespace
Stephen Hinesba17ae42013-06-05 17:18:04 -0700429
Jason Sams709a0972012-11-15 18:18:04 -0800430namespace android {
431namespace renderscript {
432
Jason Sams110f1812013-03-14 16:02:18 -0700433#define MAXLINE 500
434#define MAKE_STR_HELPER(S) #S
435#define MAKE_STR(S) MAKE_STR_HELPER(S)
436#define EXPORT_VAR_STR "exportVarCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700437#define EXPORT_FUNC_STR "exportFuncCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700438#define EXPORT_FOREACH_STR "exportForEachCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700439#define OBJECT_SLOT_STR "objectSlotCount: "
Jason Sams110f1812013-03-14 16:02:18 -0700440
441// Copy up to a newline or size chars from str -> s, updating str
Chris Wailes44bef6f2014-08-12 13:51:10 -0700442// Returns s when successful and nullptr when '\0' is finally reached.
Jason Sams110f1812013-03-14 16:02:18 -0700443static char* strgets(char *s, int size, const char **ppstr) {
444 if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700445 return nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700446 }
447
448 int i;
449 for (i = 0; i < (size - 1); i++) {
450 s[i] = **ppstr;
451 (*ppstr)++;
452 if (s[i] == '\0') {
453 return s;
454 } else if (s[i] == '\n') {
455 s[i+1] = '\0';
456 return s;
457 }
458 }
459
460 // size has been exceeded.
461 s[i] = '\0';
462
463 return s;
464}
Jason Sams709a0972012-11-15 18:18:04 -0800465
466RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) {
467 mCtx = ctx;
468 mScript = s;
469
Chris Wailes44bef6f2014-08-12 13:51:10 -0700470 mScriptSO = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800471
Chris Wailes44bef6f2014-08-12 13:51:10 -0700472 mInvokeFunctions = nullptr;
473 mForEachFunctions = nullptr;
474 mFieldAddress = nullptr;
475 mFieldIsObject = nullptr;
476 mForEachSignatures = nullptr;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800477
478#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700479 mCompilerDriver = nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700480#endif
481
Tim Murraye195a3f2014-03-13 15:04:58 -0700482
Chris Wailes44bef6f2014-08-12 13:51:10 -0700483 mRoot = nullptr;
484 mRootExpand = nullptr;
485 mInit = nullptr;
486 mFreeChildren = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800487
Jason Sams709a0972012-11-15 18:18:04 -0800488
Chris Wailes44bef6f2014-08-12 13:51:10 -0700489 mBoundAllocs = nullptr;
490 mIntrinsicData = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800491 mIsThreadable = true;
492}
493
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800494bool RsdCpuScriptImpl::storeRSInfoFromSO() {
495 char line[MAXLINE];
496 size_t varCount = 0;
497 size_t funcCount = 0;
498 size_t forEachCount = 0;
499 size_t objectSlotCount = 0;
500
501 mRoot = (RootFunc_t) dlsym(mScriptSO, "root");
502 if (mRoot) {
503 //ALOGE("Found root(): %p", mRoot);
504 }
505 mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand");
506 if (mRootExpand) {
507 //ALOGE("Found root.expand(): %p", mRootExpand);
508 }
509 mInit = (InvokeFunc_t) dlsym(mScriptSO, "init");
510 if (mInit) {
511 //ALOGE("Found init(): %p", mInit);
512 }
513 mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor");
514 if (mFreeChildren) {
515 //ALOGE("Found .rs.dtor(): %p", mFreeChildren);
516 }
517
518 const char *rsInfo = (const char *) dlsym(mScriptSO, ".rs.info");
519 if (rsInfo) {
520 //ALOGE("Found .rs.info(): %p - %s", rsInfo, rsInfo);
521 }
522
523 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
524 goto error;
525 }
526 if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) {
527 ALOGE("Invalid export var count!: %s", line);
528 goto error;
529 }
530
531 mExportedVariableCount = varCount;
532 //ALOGE("varCount: %zu", varCount);
533 if (varCount > 0) {
534 // Start by creating/zeroing this member, since we don't want to
535 // accidentally clean up invalid pointers later (if we error out).
536 mFieldIsObject = new bool[varCount];
537 if (mFieldIsObject == nullptr) {
538 goto error;
539 }
540 memset(mFieldIsObject, 0, varCount * sizeof(*mFieldIsObject));
541 mFieldAddress = new void*[varCount];
542 if (mFieldAddress == nullptr) {
543 goto error;
544 }
545 for (size_t i = 0; i < varCount; ++i) {
546 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
547 goto error;
548 }
549 char *c = strrchr(line, '\n');
550 if (c) {
551 *c = '\0';
552 }
553 mFieldAddress[i] = dlsym(mScriptSO, line);
554 if (mFieldAddress[i] == nullptr) {
555 ALOGE("Failed to find variable address for %s: %s",
556 line, dlerror());
557 // Not a critical error if we don't find a global variable.
558 }
559 else {
560 //ALOGE("Found variable %s at %p", line,
561 //mFieldAddress[i]);
562 }
563 }
564 }
565
566 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
567 goto error;
568 }
569 if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) {
570 ALOGE("Invalid export func count!: %s", line);
571 goto error;
572 }
573
574 mExportedFunctionCount = funcCount;
575 //ALOGE("funcCount: %zu", funcCount);
576
577 if (funcCount > 0) {
578 mInvokeFunctions = new InvokeFunc_t[funcCount];
579 if (mInvokeFunctions == nullptr) {
580 goto error;
581 }
582 for (size_t i = 0; i < funcCount; ++i) {
583 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
584 goto error;
585 }
586 char *c = strrchr(line, '\n');
587 if (c) {
588 *c = '\0';
589 }
590
591 mInvokeFunctions[i] = (InvokeFunc_t) dlsym(mScriptSO, line);
592 if (mInvokeFunctions[i] == nullptr) {
593 ALOGE("Failed to get function address for %s(): %s",
594 line, dlerror());
595 goto error;
596 }
597 else {
598 //ALOGE("Found InvokeFunc_t %s at %p", line, mInvokeFunctions[i]);
599 }
600 }
601 }
602
603 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
604 goto error;
605 }
606 if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) {
607 ALOGE("Invalid export forEach count!: %s", line);
608 goto error;
609 }
610
611 if (forEachCount > 0) {
612
613 mForEachSignatures = new uint32_t[forEachCount];
614 if (mForEachSignatures == nullptr) {
615 goto error;
616 }
617 mForEachFunctions = new ForEachFunc_t[forEachCount];
618 if (mForEachFunctions == nullptr) {
619 goto error;
620 }
621 for (size_t i = 0; i < forEachCount; ++i) {
622 unsigned int tmpSig = 0;
623 char tmpName[MAXLINE];
624
625 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
626 goto error;
627 }
628 if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s",
629 &tmpSig, tmpName) != 2) {
630 ALOGE("Invalid export forEach!: %s", line);
631 goto error;
632 }
633
634 // Lookup the expanded ForEach kernel.
635 strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName));
636 mForEachSignatures[i] = tmpSig;
637 mForEachFunctions[i] =
638 (ForEachFunc_t) dlsym(mScriptSO, tmpName);
639 if (i != 0 && mForEachFunctions[i] == nullptr) {
640 // Ignore missing root.expand functions.
641 // root() is always specified at location 0.
642 ALOGE("Failed to find forEach function address for %s: %s",
643 tmpName, dlerror());
644 goto error;
645 }
646 else {
647 //ALOGE("Found forEach %s at %p", tmpName, mForEachFunctions[i]);
648 }
649 }
650 }
651
652 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
653 goto error;
654 }
655 if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) {
656 ALOGE("Invalid object slot count!: %s", line);
657 goto error;
658 }
659
660 if (objectSlotCount > 0) {
661 rsAssert(varCount > 0);
662 for (size_t i = 0; i < objectSlotCount; ++i) {
663 uint32_t varNum = 0;
664 if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
665 goto error;
666 }
667 if (sscanf(line, "%u", &varNum) != 1) {
668 ALOGE("Invalid object slot!: %s", line);
669 goto error;
670 }
671
672 if (varNum < varCount) {
673 mFieldIsObject[varNum] = true;
674 }
675 }
676 }
677
678 if (varCount > 0) {
679 mBoundAllocs = new Allocation *[varCount];
680 memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs));
681 }
682
683 if (mScriptSO == (void*)1) {
684 //rsdLookupRuntimeStub(script, "acos");
685 }
686
687 return true;
688
689error:
690 delete[] mInvokeFunctions;
691 delete[] mForEachFunctions;
692 delete[] mFieldAddress;
693 delete[] mFieldIsObject;
694 delete[] mForEachSignatures;
695 delete[] mBoundAllocs;
696
697 return false;
698}
699
Jason Sams709a0972012-11-15 18:18:04 -0800700bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir,
701 uint8_t const *bitcode, size_t bitcodeSize,
Stephen Hines00511322014-01-31 11:20:23 -0800702 uint32_t flags, char const *bccPluginName) {
Jason Sams709a0972012-11-15 18:18:04 -0800703 //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, bitcode, bitcodeSize, flags, lookupFunc);
704 //ALOGE("rsdScriptInit %p %p", rsc, script);
705
706 mCtx->lockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700707#ifndef RS_COMPATIBILITY_LIB
Stephen Hines00511322014-01-31 11:20:23 -0800708 bool useRSDebugContext = false;
Jason Sams709a0972012-11-15 18:18:04 -0800709
Chris Wailes44bef6f2014-08-12 13:51:10 -0700710 mCompilerDriver = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800711
Jason Sams709a0972012-11-15 18:18:04 -0800712 mCompilerDriver = new bcc::RSCompilerDriver();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700713 if (mCompilerDriver == nullptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800714 ALOGE("bcc: FAILS to create compiler driver (out of memory)");
715 mCtx->unlockMutex();
716 return false;
717 }
718
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700719 // Run any compiler setup functions we have been provided with.
720 RSSetupCompilerCallback setupCompilerCallback =
721 mCtx->getSetupCompilerCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700722 if (setupCompilerCallback != nullptr) {
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700723 setupCompilerCallback(mCompilerDriver);
724 }
725
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700726 bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize);
727 if (!bitcodeMetadata.extract()) {
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700728 ALOGE("Could not extract metadata from bitcode");
Stephen Hinesf94e8db2014-06-26 11:55:29 -0700729 mCtx->unlockMutex();
Stephen Hinesb58d9ad2013-06-19 19:26:19 -0700730 return false;
731 }
732
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700733 const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700734
735 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
Stephen Hinesf47e8b42013-04-18 01:06:29 -0700736 mCompilerDriver->setDebugContext(true);
Stephen Hines00511322014-01-31 11:20:23 -0800737 useRSDebugContext = true;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700738 }
Stephen Hinesba17ae42013-06-05 17:18:04 -0700739
Chris Wailes6847e732014-08-11 17:30:51 -0700740 std::string bcFileName(cacheDir);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700741 bcFileName.append("/");
742 bcFileName.append(resName);
743 bcFileName.append(".bc");
744
745 std::vector<const char*> compileArguments;
746 setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
747 useRSDebugContext, bccPluginName);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700748 // The last argument of compileArguments ia a nullptr, so remove 1 from the size.
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700749 std::string compileCommandLine =
750 bcc::getCommandLine(compileArguments.size() - 1, compileArguments.data());
751
Stephen Hines45e753a2015-01-19 20:58:44 -0800752 if (!is_force_recompile()) {
753 mScriptSO = loadSharedLibrary(cacheDir, resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700754 }
755
756 // If we can't, it's either not there or out of date. We compile the bit code and try loading
757 // again.
Stephen Hines45e753a2015-01-19 20:58:44 -0800758 if (mScriptSO == nullptr) {
759 if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize,
760 compileArguments.data(), compileCommandLine))
761 {
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700762 ALOGE("bcc: FAILS to compile '%s'", resName);
763 mCtx->unlockMutex();
764 return false;
765 }
Stephen Hines45e753a2015-01-19 20:58:44 -0800766
767 if (!createSharedLib(cacheDir, resName)) {
768 ALOGE("Linker: Failed to link object file '%s'", resName);
769 mCtx->unlockMutex();
770 return false;
771 }
772
773 mScriptSO = loadSharedLibrary(cacheDir, resName);
774 if (mScriptSO == nullptr) {
775 ALOGE("Unable to load '%s'", resName);
Jean-Luc Brouillet40e35cd2014-06-25 18:21:45 -0700776 mCtx->unlockMutex();
777 return false;
Stephen Hinesba17ae42013-06-05 17:18:04 -0700778 }
779 }
Jason Sams709a0972012-11-15 18:18:04 -0800780
Stephen Hines45e753a2015-01-19 20:58:44 -0800781 // Read RS symbol information from the .so.
782 if ( !mScriptSO) {
783 goto error;
Jason Sams709a0972012-11-15 18:18:04 -0800784 }
785
Stephen Hines45e753a2015-01-19 20:58:44 -0800786 if ( !storeRSInfoFromSO()) {
787 goto error;
Tim Murray29809d12014-05-28 12:04:19 -0700788 }
Jean-Luc Brouilletf4d216e2014-06-09 18:04:16 -0700789#else // RS_COMPATIBILITY_LIB is defined
Jason Sams110f1812013-03-14 16:02:18 -0700790
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700791 mScriptSO = loadSharedLibrary(cacheDir, resName);
Jason Sams110f1812013-03-14 16:02:18 -0700792
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800793 if (!mScriptSO) {
794 goto error;
795 }
Jason Sams110f1812013-03-14 16:02:18 -0700796
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -0800797 if (!storeRSInfoFromSO()) {
Stephen Hinesc2c11cc2013-07-19 01:07:42 -0700798 goto error;
Jason Sams110f1812013-03-14 16:02:18 -0700799 }
800#endif
Jason Sams709a0972012-11-15 18:18:04 -0800801 mCtx->unlockMutex();
802 return true;
Jason Sams110f1812013-03-14 16:02:18 -0700803
Jason Sams110f1812013-03-14 16:02:18 -0700804error:
805
806 mCtx->unlockMutex();
Jason Sams110f1812013-03-14 16:02:18 -0700807 if (mScriptSO) {
808 dlclose(mScriptSO);
809 }
810 return false;
Jason Sams709a0972012-11-15 18:18:04 -0800811}
812
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700813#ifndef RS_COMPATIBILITY_LIB
814
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700815const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode,
816 size_t bitcodeSize) {
817 const char* defaultLib = SYSLIBPATH"/libclcore.bc";
818
819 // If we're debugging, use the debug library.
820 if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
821 return SYSLIBPATH"/libclcore_debug.bc";
822 }
823
824 // If a callback has been registered to specify a library, use that.
825 RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback();
Chris Wailes44bef6f2014-08-12 13:51:10 -0700826 if (selectRTCallback != nullptr) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700827 return selectRTCallback((const char*)bitcode, bitcodeSize);
828 }
829
830 // Check for a platform specific library
831#if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON)
832 enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision();
Jean-Luc Brouilletf4d38362014-07-09 17:46:03 -0700833 if (prec == bcinfo::RS_FP_Relaxed) {
Jean-Luc Brouillet9ab50942014-06-18 18:10:32 -0700834 // NEON-capable ARMv7a devices can use an accelerated math library
835 // for all reduced precision scripts.
836 // ARMv8 does not use NEON, as ASIMD can be used with all precision
837 // levels.
838 return SYSLIBPATH"/libclcore_neon.bc";
839 } else {
840 return defaultLib;
841 }
842#elif defined(__i386__) || defined(__x86_64__)
843 // x86 devices will use an optimized library.
844 return SYSLIBPATH"/libclcore_x86.bc";
845#else
846 return defaultLib;
847#endif
848}
849
850#endif
851
Jason Sams709a0972012-11-15 18:18:04 -0800852void RsdCpuScriptImpl::populateScript(Script *script) {
Jason Sams110f1812013-03-14 16:02:18 -0700853 // Copy info over to runtime
854 script->mHal.info.exportedFunctionCount = mExportedFunctionCount;
855 script->mHal.info.exportedVariableCount = mExportedVariableCount;
856 script->mHal.info.exportedPragmaCount = 0;
857 script->mHal.info.exportedPragmaKeyList = 0;
858 script->mHal.info.exportedPragmaValueList = 0;
859
860 // Bug, need to stash in metadata
861 if (mRootExpand) {
862 script->mHal.info.root = mRootExpand;
863 } else {
864 script->mHal.info.root = mRoot;
865 }
Jason Sams709a0972012-11-15 18:18:04 -0800866}
867
Jason Sams709a0972012-11-15 18:18:04 -0800868
869typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
870
Chris Wailesf3712132014-07-16 15:18:30 -0700871void RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains,
872 uint32_t inLen,
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700873 Allocation * aout,
874 const void * usr, uint32_t usrLen,
875 const RsScriptCall *sc,
876 MTLaunchStruct *mtls) {
877
878 memset(mtls, 0, sizeof(MTLaunchStruct));
879
Chris Wailesf3712132014-07-16 15:18:30 -0700880 for (int index = inLen; --index >= 0;) {
881 const Allocation* ain = ains[index];
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700882
Chris Wailesf3712132014-07-16 15:18:30 -0700883 // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface
Chris Wailes44bef6f2014-08-12 13:51:10 -0700884 if (ain != nullptr &&
885 (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) {
886
Chris Wailesf3712132014-07-16 15:18:30 -0700887 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
888 "rsForEach called with null in allocations");
889 return;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700890 }
891 }
892
Chris Wailes44bef6f2014-08-12 13:51:10 -0700893 if (aout &&
894 (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) {
895
Chris Wailesf3712132014-07-16 15:18:30 -0700896 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
897 "rsForEach called with null out allocations");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700898 return;
899 }
900
Chris Wailesf3712132014-07-16 15:18:30 -0700901 if (inLen > 0) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700902 const Allocation *ain0 = ains[0];
903 const Type *inType = ain0->getType();
904
Jason Samsc0d68472015-01-20 14:29:52 -0800905 mtls->fep.dim.x = inType->getDimX();
906 mtls->fep.dim.y = inType->getDimY();
907 mtls->fep.dim.z = inType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700908
909 for (int Index = inLen; --Index >= 1;) {
910 if (!ain0->hasSameDims(ains[Index])) {
911 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
912 "Failed to launch kernel; dimensions of input and output allocations do not match.");
913
914 return;
915 }
916 }
917
Chris Wailes44bef6f2014-08-12 13:51:10 -0700918 } else if (aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700919 const Type *outType = aout->getType();
920
Jason Samsc0d68472015-01-20 14:29:52 -0800921 mtls->fep.dim.x = outType->getDimX();
922 mtls->fep.dim.y = outType->getDimY();
923 mtls->fep.dim.z = outType->getDimZ();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700924
925 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700926 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
927 "rsForEach called with null allocations");
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700928 return;
929 }
930
Chris Wailes44bef6f2014-08-12 13:51:10 -0700931 if (inLen > 0 && aout != nullptr) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700932 if (!ains[0]->hasSameDims(aout)) {
933 mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT,
934 "Failed to launch kernel; dimensions of input and output allocations do not match.");
935
936 return;
937 }
938 }
939
940 if (!sc || (sc->xEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -0800941 mtls->xEnd = mtls->fep.dim.x;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700942 } else {
Jason Samsc0d68472015-01-20 14:29:52 -0800943 rsAssert(sc->xStart < mtls->fep.dim.x);
944 rsAssert(sc->xEnd <= mtls->fep.dim.x);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700945 rsAssert(sc->xStart < sc->xEnd);
Jason Samsc0d68472015-01-20 14:29:52 -0800946 mtls->xStart = rsMin(mtls->fep.dim.x, sc->xStart);
947 mtls->xEnd = rsMin(mtls->fep.dim.x, sc->xEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700948 if (mtls->xStart >= mtls->xEnd) return;
949 }
950
951 if (!sc || (sc->yEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -0800952 mtls->yEnd = mtls->fep.dim.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700953 } else {
Jason Samsc0d68472015-01-20 14:29:52 -0800954 rsAssert(sc->yStart < mtls->fep.dim.y);
955 rsAssert(sc->yEnd <= mtls->fep.dim.y);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700956 rsAssert(sc->yStart < sc->yEnd);
Jason Samsc0d68472015-01-20 14:29:52 -0800957 mtls->yStart = rsMin(mtls->fep.dim.y, sc->yStart);
958 mtls->yEnd = rsMin(mtls->fep.dim.y, sc->yEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700959 if (mtls->yStart >= mtls->yEnd) return;
960 }
961
962 if (!sc || (sc->zEnd == 0)) {
Jason Samsc0d68472015-01-20 14:29:52 -0800963 mtls->zEnd = mtls->fep.dim.z;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700964 } else {
Jason Samsc0d68472015-01-20 14:29:52 -0800965 rsAssert(sc->zStart < mtls->fep.dim.z);
966 rsAssert(sc->zEnd <= mtls->fep.dim.z);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700967 rsAssert(sc->zStart < sc->zEnd);
Jason Samsc0d68472015-01-20 14:29:52 -0800968 mtls->zStart = rsMin(mtls->fep.dim.z, sc->zStart);
969 mtls->zEnd = rsMin(mtls->fep.dim.z, sc->zEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700970 if (mtls->zStart >= mtls->zEnd) return;
971 }
972
973 mtls->xEnd = rsMax((uint32_t)1, mtls->xEnd);
974 mtls->yEnd = rsMax((uint32_t)1, mtls->yEnd);
975 mtls->zEnd = rsMax((uint32_t)1, mtls->zEnd);
976 mtls->arrayEnd = rsMax((uint32_t)1, mtls->arrayEnd);
977
Chris Wailesf3712132014-07-16 15:18:30 -0700978 rsAssert(inLen == 0 || (ains[0]->getType()->getDimZ() == 0));
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700979
980 mtls->rsc = mCtx;
Jason Samsc0d68472015-01-20 14:29:52 -0800981 if (ains) {
982 memcpy(mtls->ains, ains, inLen * sizeof(ains[0]));
983 }
984 mtls->aout[0] = aout;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700985 mtls->fep.usr = usr;
986 mtls->fep.usrLen = usrLen;
987 mtls->mSliceSize = 1;
988 mtls->mSliceNum = 0;
989
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700990 mtls->isThreadable = mIsThreadable;
991
Chris Wailesf3712132014-07-16 15:18:30 -0700992 if (inLen > 0) {
Chris Wailesf3712132014-07-16 15:18:30 -0700993 mtls->fep.inLen = inLen;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700994 for (int index = inLen; --index >= 0;) {
Jason Samsc0d68472015-01-20 14:29:52 -0800995 mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr;
996 mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700997 }
998 }
999
Chris Wailes44bef6f2014-08-12 13:51:10 -07001000 if (aout != nullptr) {
Jason Samsc0d68472015-01-20 14:29:52 -08001001 mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr;
1002 mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes();
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001003 }
1004}
1005
Jason Sams709a0972012-11-15 18:18:04 -08001006
1007void RsdCpuScriptImpl::invokeForEach(uint32_t slot,
Chris Wailesf3712132014-07-16 15:18:30 -07001008 const Allocation ** ains,
1009 uint32_t inLen,
Jason Sams709a0972012-11-15 18:18:04 -08001010 Allocation * aout,
1011 const void * usr,
1012 uint32_t usrLen,
1013 const RsScriptCall *sc) {
1014
1015 MTLaunchStruct mtls;
Chris Wailes4b3c34e2014-06-11 12:00:29 -07001016
1017 forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls);
1018 forEachKernelSetup(slot, &mtls);
1019
1020 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1021 mCtx->launchThreads(ains, inLen, aout, sc, &mtls);
1022 mCtx->setTLS(oldTLS);
1023}
1024
Jason Sams709a0972012-11-15 18:18:04 -08001025void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) {
Jason Sams709a0972012-11-15 18:18:04 -08001026 mtls->script = this;
1027 mtls->fep.slot = slot;
Jason Sams110f1812013-03-14 16:02:18 -07001028 mtls->kernel = reinterpret_cast<ForEachFunc_t>(mForEachFunctions[slot]);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001029 rsAssert(mtls->kernel != nullptr);
Jason Sams110f1812013-03-14 16:02:18 -07001030 mtls->sig = mForEachSignatures[slot];
Jason Sams709a0972012-11-15 18:18:04 -08001031}
1032
1033int RsdCpuScriptImpl::invokeRoot() {
1034 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
1035 int ret = mRoot();
1036 mCtx->setTLS(oldTLS);
1037 return ret;
1038}
1039
1040void RsdCpuScriptImpl::invokeInit() {
1041 if (mInit) {
1042 mInit();
1043 }
1044}
1045
1046void RsdCpuScriptImpl::invokeFreeChildren() {
1047 if (mFreeChildren) {
1048 mFreeChildren();
1049 }
1050}
1051
1052void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params,
1053 size_t paramLength) {
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001054 //ALOGE("invoke %i %p %zu", slot, params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001055 void * ap = nullptr;
1056
1057#if defined(__x86_64__)
1058 // The invoked function could have input parameter of vector type for example float4 which
1059 // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform.
1060 // So try to align void* params before passing them into RS exported function.
1061
1062 if ((uint8_t)(uint64_t)params & 0x0F) {
1063 if ((ap = (void*)memalign(16, paramLength)) != nullptr) {
1064 memcpy(ap, params, paramLength);
1065 } else {
1066 ALOGE("x86_64: invokeFunction memalign error, still use params which is not 16 bytes aligned.");
1067 }
1068 }
1069#endif
Jason Sams709a0972012-11-15 18:18:04 -08001070
1071 RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001072 reinterpret_cast<void (*)(const void *, uint32_t)>(
1073 mInvokeFunctions[slot])(ap? (const void *) ap: params, paramLength);
Yong Cheneaba5a32014-12-12 13:25:18 +08001074
Jason Sams709a0972012-11-15 18:18:04 -08001075 mCtx->setTLS(oldTLS);
1076}
1077
1078void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) {
1079 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001080 //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength);
Jason Sams709a0972012-11-15 18:18:04 -08001081
1082 //if (mIntrinsicID) {
1083 //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength);
1084 //return;
1085 //}
1086
Jason Sams110f1812013-03-14 16:02:18 -07001087 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
Jason Sams709a0972012-11-15 18:18:04 -08001088 if (!destPtr) {
1089 //ALOGV("Calling setVar on slot = %i which is null", slot);
1090 return;
1091 }
1092
1093 memcpy(destPtr, data, dataLength);
1094}
1095
Tim Murray9c642392013-04-11 13:29:59 -07001096void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) {
1097 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001098 //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength);
Tim Murray9c642392013-04-11 13:29:59 -07001099
Tim Murray9c642392013-04-11 13:29:59 -07001100 int32_t *srcPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
Tim Murray9c642392013-04-11 13:29:59 -07001101 if (!srcPtr) {
1102 //ALOGV("Calling setVar on slot = %i which is null", slot);
1103 return;
1104 }
1105 memcpy(data, srcPtr, dataLength);
1106}
1107
1108
Jason Sams709a0972012-11-15 18:18:04 -08001109void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength,
1110 const Element *elem,
Stephen Hinesac8d1462014-06-25 00:01:23 -07001111 const uint32_t *dims, size_t dimLength) {
Jason Sams110f1812013-03-14 16:02:18 -07001112 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
Jason Sams709a0972012-11-15 18:18:04 -08001113 if (!destPtr) {
1114 //ALOGV("Calling setVar on slot = %i which is null", slot);
1115 return;
1116 }
1117
1118 // We want to look at dimension in terms of integer components,
1119 // but dimLength is given in terms of bytes.
1120 dimLength /= sizeof(int);
1121
1122 // Only a single dimension is currently supported.
1123 rsAssert(dimLength == 1);
1124 if (dimLength == 1) {
1125 // First do the increment loop.
1126 size_t stride = elem->getSizeBytes();
1127 const char *cVal = reinterpret_cast<const char *>(data);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001128 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001129 elem->incRefs(cVal);
1130 cVal += stride;
1131 }
1132
1133 // Decrement loop comes after (to prevent race conditions).
1134 char *oldVal = reinterpret_cast<char *>(destPtr);
Stephen Hinesac8d1462014-06-25 00:01:23 -07001135 for (uint32_t i = 0; i < dims[0]; i++) {
Jason Sams709a0972012-11-15 18:18:04 -08001136 elem->decRefs(oldVal);
1137 oldVal += stride;
1138 }
1139 }
1140
1141 memcpy(destPtr, data, dataLength);
1142}
1143
1144void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) {
1145
1146 //rsAssert(!script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001147 //ALOGE("setGlobalBind %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001148
Jason Sams110f1812013-03-14 16:02:18 -07001149 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
Jason Sams709a0972012-11-15 18:18:04 -08001150 if (!destPtr) {
1151 //ALOGV("Calling setVar on slot = %i which is null", slot);
1152 return;
1153 }
1154
Chris Wailes44bef6f2014-08-12 13:51:10 -07001155 void *ptr = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001156 mBoundAllocs[slot] = data;
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001157 if (data) {
Jason Sams709a0972012-11-15 18:18:04 -08001158 ptr = data->mHal.drvState.lod[0].mallocPtr;
1159 }
1160 memcpy(destPtr, &ptr, sizeof(void *));
1161}
1162
1163void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) {
1164
1165 //rsAssert(script->mFieldIsObject[slot]);
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001166 //ALOGE("setGlobalObj %i %p", slot, data);
Jason Sams709a0972012-11-15 18:18:04 -08001167
Jason Sams110f1812013-03-14 16:02:18 -07001168 int32_t *destPtr = reinterpret_cast<int32_t *>(mFieldAddress[slot]);
Jason Sams709a0972012-11-15 18:18:04 -08001169 if (!destPtr) {
1170 //ALOGV("Calling setVar on slot = %i which is null", slot);
1171 return;
1172 }
1173
Jason Sams05ef73f2014-08-05 14:59:22 -07001174 rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data);
Jason Sams709a0972012-11-15 18:18:04 -08001175}
1176
1177RsdCpuScriptImpl::~RsdCpuScriptImpl() {
Jason Sams110f1812013-03-14 16:02:18 -07001178#ifndef RS_COMPATIBILITY_LIB
Jason Sams709a0972012-11-15 18:18:04 -08001179
Jason Sams709a0972012-11-15 18:18:04 -08001180 if (mCompilerDriver) {
1181 delete mCompilerDriver;
1182 }
Tim Murraybee48d72014-06-13 12:44:47 -07001183
Stephen Hines45e753a2015-01-19 20:58:44 -08001184#endif
Pirama Arumuga Nainardc0d8f72014-12-02 15:23:38 -08001185
1186 if (mFieldIsObject) {
1187 for (size_t i = 0; i < mExportedVariableCount; ++i) {
1188 if (mFieldIsObject[i]) {
1189 if (mFieldAddress[i] != nullptr) {
1190 rs_object_base *obj_addr =
1191 reinterpret_cast<rs_object_base *>(mFieldAddress[i]);
1192 rsrClearObject(mCtx->getContext(), obj_addr);
1193 }
1194 }
1195 }
1196 }
1197
Jason Sams110f1812013-03-14 16:02:18 -07001198 if (mInvokeFunctions) delete[] mInvokeFunctions;
1199 if (mForEachFunctions) delete[] mForEachFunctions;
1200 if (mFieldAddress) delete[] mFieldAddress;
1201 if (mFieldIsObject) delete[] mFieldIsObject;
1202 if (mForEachSignatures) delete[] mForEachSignatures;
1203 if (mBoundAllocs) delete[] mBoundAllocs;
1204 if (mScriptSO) {
1205 dlclose(mScriptSO);
1206 }
Jason Sams709a0972012-11-15 18:18:04 -08001207}
1208
1209Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const {
1210 if (!ptr) {
Chris Wailes44bef6f2014-08-12 13:51:10 -07001211 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001212 }
1213
1214 for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) {
1215 Allocation *a = mBoundAllocs[ct];
1216 if (!a) continue;
1217 if (a->mHal.drvState.lod[0].mallocPtr == ptr) {
1218 return a;
1219 }
1220 }
1221 ALOGE("rsGetAllocation, failed to find %p", ptr);
Chris Wailes44bef6f2014-08-12 13:51:10 -07001222 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -08001223}
1224
Chris Wailesf3712132014-07-16 15:18:30 -07001225void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
1226 uint32_t inLen, Allocation * aout,
1227 const void * usr, uint32_t usrLen,
1228 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001229
Chris Wailesf3712132014-07-16 15:18:30 -07001230void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains,
1231 uint32_t inLen, Allocation * aout,
1232 const void * usr, uint32_t usrLen,
1233 const RsScriptCall *sc) {}
Jason Sams17e3cdc2013-09-09 17:32:16 -07001234
Jason Sams709a0972012-11-15 18:18:04 -08001235
1236}
1237}