| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1 | /* |
| 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 Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 7 | * |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 8 | * 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 Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 17 | #include "rsCpuCore.h" |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 18 | #include "rsCpuScript.h" |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 19 | |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 20 | #ifdef RS_COMPATIBILITY_LIB |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| Stephen Hines | ee48c0b | 2013-10-30 17:48:30 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 24 | #else |
| 25 | #include <bcc/BCCContext.h> |
| Stephen Hines | 82e0a67 | 2014-05-05 15:40:56 -0700 | [diff] [blame] | 26 | #include <bcc/Config/Config.h> |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 27 | #include <bcc/Renderscript/RSCompilerDriver.h> |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 28 | #include <bcc/Renderscript/RSInfo.h> |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 29 | #include <bcinfo/MetadataExtractor.h> |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 31 | |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <unistd.h> |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string> |
| 37 | #include <vector> |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 38 | #endif |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 39 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 40 | #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 Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 54 | namespace { |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 55 | |
| 56 | // Create a len length string containing random characters from [A-Za-z0-9]. |
| 57 | static 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 Hines | ee48c0b | 2013-10-30 17:48:30 -0700 | [diff] [blame] | 77 | // Check if a path exists and attempt to create it if it doesn't. |
| 78 | static 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 Hines | 7d77485 | 2014-10-01 12:57:57 -0700 | [diff] [blame] | 89 | // Copy the file named \p srcFile to \p dstFile. |
| 90 | // Return 0 on success and -1 if anything wasn't copied. |
| 91 | static 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 Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 114 | static std::string findSharedObjectName(const char *cacheDir, |
| 115 | const char *resName) { |
| 116 | |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 117 | #ifndef RS_SERVER |
| 118 | std::string scriptSOName(cacheDir); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 119 | #ifdef RS_COMPATIBILITY_LIB |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 120 | 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 Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 128 | scriptSOName.append("/librs."); |
| 129 | #endif |
| 130 | |
| 131 | #else |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 132 | std::string scriptSOName("lib"); |
| 133 | #endif |
| 134 | scriptSOName.append(resName); |
| 135 | scriptSOName.append(".so"); |
| 136 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 137 | return scriptSOName; |
| 138 | } |
| 139 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 140 | #ifndef RS_COMPATIBILITY_LIB |
| 141 | |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 142 | static 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 Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 163 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 164 | static void setCompileArguments(std::vector<const char*>* args, |
| 165 | const std::string& bcFileName, |
| 166 | const char* cacheDir, const char* resName, |
| 167 | const char* core_lib, bool useRSDebugContext, |
| 168 | const char* bccPluginName) { |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 169 | rsAssert(cacheDir && resName && core_lib); |
| Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame^] | 170 | args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH); |
| Tim Murray | 687cfe8 | 2015-01-08 14:59:38 -0800 | [diff] [blame] | 171 | args->push_back("-unroll-runtime"); |
| 172 | args->push_back("-scalarize-load-store"); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 173 | args->push_back("-o"); |
| 174 | args->push_back(resName); |
| 175 | args->push_back("-output_path"); |
| 176 | args->push_back(cacheDir); |
| 177 | args->push_back("-bclib"); |
| 178 | args->push_back(core_lib); |
| 179 | args->push_back("-mtriple"); |
| 180 | args->push_back(DEFAULT_TARGET_TRIPLE_STRING); |
| 181 | |
| Tim Murray | 358ffb8 | 2014-12-09 11:53:06 -0800 | [diff] [blame] | 182 | // Enable workaround for A53 codegen by default. |
| 183 | #if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND) |
| 184 | args->push_back("-aarch64-fix-cortex-a53-835769"); |
| 185 | #endif |
| 186 | |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 187 | // Execute the bcc compiler. |
| 188 | if (useRSDebugContext) { |
| 189 | args->push_back("-rs-debug-ctx"); |
| 190 | } else { |
| 191 | // Only load additional libraries for compiles that don't use |
| 192 | // the debug context. |
| 193 | if (bccPluginName && strlen(bccPluginName) > 0) { |
| 194 | args->push_back("-load"); |
| 195 | args->push_back(bccPluginName); |
| 196 | } |
| 197 | } |
| 198 | |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 199 | args->push_back("-fPIC"); |
| 200 | args->push_back("-embedRSInfo"); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 201 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 202 | args->push_back(bcFileName.c_str()); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 203 | args->push_back(nullptr); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 206 | static bool compileBitcode(const std::string &bcFileName, |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 207 | const char *bitcode, |
| 208 | size_t bitcodeSize, |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 209 | const char **compileArguments, |
| 210 | const std::string &compileCommandLine) { |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 211 | rsAssert(bitcode && bitcodeSize); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 212 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 213 | FILE *bcfile = fopen(bcFileName.c_str(), "w"); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 214 | if (!bcfile) { |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 215 | ALOGE("Could not write to %s", bcFileName.c_str()); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile); |
| 219 | fclose(bcfile); |
| 220 | if (nwritten != bitcodeSize) { |
| 221 | ALOGE("Could not write %zu bytes to %s", bitcodeSize, |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 222 | bcFileName.c_str()); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | |
| 226 | pid_t pid = fork(); |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 227 | |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 228 | switch (pid) { |
| 229 | case -1: { // Error occurred (we attempt no recovery) |
| 230 | ALOGE("Couldn't fork for bcc compiler execution"); |
| 231 | return false; |
| 232 | } |
| 233 | case 0: { // Child process |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 234 | ALOGV("Invoking BCC with: %s", compileCommandLine.c_str()); |
| Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame^] | 235 | execv(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH, |
| 236 | (char* const*)compileArguments); |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 237 | |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 238 | ALOGE("execv() failed: %s", strerror(errno)); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 239 | abort(); |
| 240 | return false; |
| 241 | } |
| 242 | default: { // Parent process (actual driver) |
| 243 | // Wait on child process to finish compiling the source. |
| 244 | int status = 0; |
| 245 | pid_t w = waitpid(pid, &status, 0); |
| 246 | if (w == -1) { |
| 247 | ALOGE("Could not wait for bcc compiler"); |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | ALOGE("bcc compiler terminated unexpectedly"); |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | } |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 260 | |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 261 | #endif // !defined(RS_COMPATIBILITY_LIB) |
| 262 | } // namespace |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 263 | |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 264 | namespace android { |
| 265 | namespace renderscript { |
| 266 | |
| 267 | const char* SharedLibraryUtils::LD_EXE_PATH = "/system/bin/ld.mc"; |
| 268 | const char* SharedLibraryUtils::RS_CACHE_DIR = "com.android.renderscript.cache"; |
| 269 | |
| 270 | #ifndef RS_COMPATIBILITY_LIB |
| 271 | |
| 272 | bool SharedLibraryUtils::createSharedLibrary(const char *cacheDir, const char *resName) { |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 273 | std::string sharedLibName = findSharedObjectName(cacheDir, resName); |
| 274 | std::string objFileName = cacheDir; |
| 275 | objFileName.append("/"); |
| 276 | objFileName.append(resName); |
| 277 | objFileName.append(".o"); |
| 278 | |
| 279 | const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so"; |
| 280 | std::vector<const char *> args = { |
| 281 | LD_EXE_PATH, |
| 282 | "-shared", |
| 283 | "-nostdlib", |
| 284 | compiler_rt, |
| 285 | "-mtriple", DEFAULT_TARGET_TRIPLE_STRING, |
| 286 | "-L", SYSLIBPATH, |
| 287 | "-lRSDriver", "-lm", "-lc", |
| 288 | objFileName.c_str(), |
| 289 | "-o", sharedLibName.c_str(), |
| 290 | nullptr |
| 291 | }; |
| 292 | |
| 293 | std::string cmdLineStr = bcc::getCommandLine(args.size()-1, args.data()); |
| 294 | |
| 295 | pid_t pid = fork(); |
| 296 | |
| 297 | switch (pid) { |
| 298 | case -1: { // Error occurred (we attempt no recovery) |
| 299 | ALOGE("Couldn't fork for linker (%s) execution", LD_EXE_PATH); |
| 300 | return false; |
| 301 | } |
| 302 | case 0: { // Child process |
| 303 | ALOGV("Invoking ld.mc with args '%s'", cmdLineStr.c_str()); |
| 304 | execv(LD_EXE_PATH, (char* const*) args.data()); |
| 305 | |
| 306 | ALOGE("execv() failed: %s", strerror(errno)); |
| 307 | abort(); |
| 308 | return false; |
| 309 | } |
| 310 | default: { // Parent process (actual driver) |
| 311 | // Wait on child process to finish compiling the source. |
| 312 | int status = 0; |
| 313 | pid_t w = waitpid(pid, &status, 0); |
| 314 | if (w == -1) { |
| 315 | ALOGE("Could not wait for linker (%s)", LD_EXE_PATH); |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | ALOGE("Linker (%s) terminated unexpectedly", LD_EXE_PATH); |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | } |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 328 | |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 329 | #endif // RS_COMPATIBILITY_LIB |
| 330 | |
| 331 | void* SharedLibraryUtils::loadSharedLibrary(const char *cacheDir, const char *resName) { |
| 332 | void *loaded = nullptr; |
| 333 | |
| 334 | std::string scriptSOName = findSharedObjectName(cacheDir, resName); |
| 335 | |
| 336 | // We should check if we can load the library from the standard app |
| 337 | // location for shared libraries first. |
| 338 | loaded = loadSOHelper(scriptSOName.c_str(), cacheDir, resName); |
| 339 | |
| 340 | if (loaded == nullptr) { |
| 341 | ALOGE("Unable to open shared library (%s): %s", |
| 342 | scriptSOName.c_str(), dlerror()); |
| 343 | |
| 344 | #ifdef RS_COMPATIBILITY_LIB |
| 345 | // One final attempt to find the library in "/system/lib". |
| 346 | // We do this to allow bundled applications to use the compatibility |
| 347 | // library fallback path. Those applications don't have a private |
| 348 | // library path, so they need to install to the system directly. |
| 349 | // Note that this is really just a testing path. |
| 350 | std::string scriptSONameSystem("/system/lib/librs."); |
| 351 | scriptSONameSystem.append(resName); |
| 352 | scriptSONameSystem.append(".so"); |
| 353 | loaded = loadSOHelper(scriptSONameSystem.c_str(), cacheDir, |
| 354 | resName); |
| 355 | if (loaded == nullptr) { |
| 356 | ALOGE("Unable to open system shared library (%s): %s", |
| 357 | scriptSONameSystem.c_str(), dlerror()); |
| 358 | } |
| 359 | #endif |
| 360 | } |
| 361 | |
| 362 | return loaded; |
| 363 | } |
| 364 | |
| 365 | void* SharedLibraryUtils::loadSOHelper(const char *origName, const char *cacheDir, |
| 366 | const char *resName) { |
| 367 | // Keep track of which .so libraries have been loaded. Once a library is |
| 368 | // in the set (per-process granularity), we must instead make a copy of |
| 369 | // the original shared object (randomly named .so file) and load that one |
| 370 | // instead. If we don't do this, we end up aliasing global data between |
| 371 | // the various Script instances (which are supposed to be completely |
| 372 | // independent). |
| 373 | static std::set<std::string> LoadedLibraries; |
| 374 | |
| 375 | void *loaded = nullptr; |
| 376 | |
| 377 | // Skip everything if we don't even have the original library available. |
| 378 | if (access(origName, F_OK) != 0) { |
| 379 | return nullptr; |
| 380 | } |
| 381 | |
| 382 | // Common path is that we have not loaded this Script/library before. |
| 383 | if (LoadedLibraries.find(origName) == LoadedLibraries.end()) { |
| 384 | loaded = dlopen(origName, RTLD_NOW | RTLD_LOCAL); |
| 385 | if (loaded) { |
| 386 | LoadedLibraries.insert(origName); |
| 387 | } |
| 388 | return loaded; |
| 389 | } |
| 390 | |
| 391 | std::string newName(cacheDir); |
| 392 | |
| 393 | // Append RS_CACHE_DIR only if it is not found in cacheDir |
| 394 | // In driver mode, RS_CACHE_DIR is already appended to cacheDir. |
| 395 | if (newName.find(RS_CACHE_DIR) == std::string::npos) { |
| 396 | newName.append("/"); |
| 397 | newName.append(RS_CACHE_DIR); |
| 398 | newName.append("/"); |
| 399 | } |
| 400 | |
| 401 | if (!ensureCacheDirExists(newName.c_str())) { |
| 402 | ALOGE("Could not verify or create cache dir: %s", cacheDir); |
| 403 | return nullptr; |
| 404 | } |
| 405 | |
| 406 | // Construct an appropriately randomized filename for the copy. |
| 407 | newName.append("librs."); |
| 408 | newName.append(resName); |
| 409 | newName.append("#"); |
| 410 | newName.append(getRandomString(6)); // 62^6 potential filename variants. |
| 411 | newName.append(".so"); |
| 412 | |
| 413 | int r = copyFile(newName.c_str(), origName); |
| 414 | if (r != 0) { |
| 415 | ALOGE("Could not create copy %s -> %s", origName, newName.c_str()); |
| 416 | return nullptr; |
| 417 | } |
| 418 | loaded = dlopen(newName.c_str(), RTLD_NOW | RTLD_LOCAL); |
| 419 | r = unlink(newName.c_str()); |
| 420 | if (r != 0) { |
| 421 | ALOGE("Could not unlink copy %s", newName.c_str()); |
| 422 | } |
| 423 | if (loaded) { |
| 424 | LoadedLibraries.insert(newName.c_str()); |
| 425 | } |
| 426 | |
| 427 | return loaded; |
| 428 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 429 | |
| Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame^] | 430 | const char* RsdCpuScriptImpl::BCC_EXE_PATH = "/system/bin/bcc"; |
| 431 | |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 432 | #define MAXLINE 500 |
| 433 | #define MAKE_STR_HELPER(S) #S |
| 434 | #define MAKE_STR(S) MAKE_STR_HELPER(S) |
| 435 | #define EXPORT_VAR_STR "exportVarCount: " |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 436 | #define EXPORT_FUNC_STR "exportFuncCount: " |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 437 | #define EXPORT_FOREACH_STR "exportForEachCount: " |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 438 | #define OBJECT_SLOT_STR "objectSlotCount: " |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 439 | #define PRAGMA_STR "pragmaCount: " |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 440 | #define THREADABLE_STR "isThreadable: " |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 441 | |
| 442 | // Copy up to a newline or size chars from str -> s, updating str |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 443 | // Returns s when successful and nullptr when '\0' is finally reached. |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 444 | static char* strgets(char *s, int size, const char **ppstr) { |
| 445 | if (!ppstr || !*ppstr || **ppstr == '\0' || size < 1) { |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 446 | return nullptr; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | int i; |
| 450 | for (i = 0; i < (size - 1); i++) { |
| 451 | s[i] = **ppstr; |
| 452 | (*ppstr)++; |
| 453 | if (s[i] == '\0') { |
| 454 | return s; |
| 455 | } else if (s[i] == '\n') { |
| 456 | s[i+1] = '\0'; |
| 457 | return s; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // size has been exceeded. |
| 462 | s[i] = '\0'; |
| 463 | |
| 464 | return s; |
| 465 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 466 | |
| 467 | RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) { |
| 468 | mCtx = ctx; |
| 469 | mScript = s; |
| 470 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 471 | mScriptSO = nullptr; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 472 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 473 | #ifndef RS_COMPATIBILITY_LIB |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 474 | mCompilerDriver = nullptr; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 475 | #endif |
| 476 | |
| Tim Murray | e195a3f | 2014-03-13 15:04:58 -0700 | [diff] [blame] | 477 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 478 | mRoot = nullptr; |
| 479 | mRootExpand = nullptr; |
| 480 | mInit = nullptr; |
| 481 | mFreeChildren = nullptr; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 482 | mScriptExec = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 483 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 484 | mBoundAllocs = nullptr; |
| 485 | mIntrinsicData = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 486 | mIsThreadable = true; |
| 487 | } |
| 488 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 489 | bool RsdCpuScriptImpl::storeRSInfoFromSO() { |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 490 | mRoot = (RootFunc_t) dlsym(mScriptSO, "root"); |
| 491 | if (mRoot) { |
| 492 | //ALOGE("Found root(): %p", mRoot); |
| 493 | } |
| 494 | mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand"); |
| 495 | if (mRootExpand) { |
| 496 | //ALOGE("Found root.expand(): %p", mRootExpand); |
| 497 | } |
| 498 | mInit = (InvokeFunc_t) dlsym(mScriptSO, "init"); |
| 499 | if (mInit) { |
| 500 | //ALOGE("Found init(): %p", mInit); |
| 501 | } |
| 502 | mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor"); |
| 503 | if (mFreeChildren) { |
| 504 | //ALOGE("Found .rs.dtor(): %p", mFreeChildren); |
| 505 | } |
| 506 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 507 | mScriptExec = ScriptExecutable::createFromSharedObject( |
| 508 | mCtx->getContext(), mScriptSO); |
| 509 | |
| 510 | if (mScriptExec == nullptr) { |
| 511 | return false; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 514 | size_t varCount = mScriptExec->getExportedVariableCount(); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 515 | if (varCount > 0) { |
| 516 | mBoundAllocs = new Allocation *[varCount]; |
| 517 | memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs)); |
| 518 | } |
| 519 | |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 520 | mIsThreadable = mScriptExec->getThreadable(); |
| 521 | //ALOGE("Script isThreadable? %d", mIsThreadable); |
| 522 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 523 | return true; |
| 524 | } |
| 525 | |
| 526 | ScriptExecutable* ScriptExecutable::createFromSharedObject( |
| 527 | Context* RSContext, void* sharedObj) { |
| 528 | char line[MAXLINE]; |
| 529 | |
| 530 | size_t varCount = 0; |
| 531 | size_t funcCount = 0; |
| 532 | size_t forEachCount = 0; |
| 533 | size_t objectSlotCount = 0; |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 534 | size_t pragmaCount = 0; |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 535 | bool isThreadable = true; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 536 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 537 | void** fieldAddress = nullptr; |
| 538 | bool* fieldIsObject = nullptr; |
| 539 | InvokeFunc_t* invokeFunctions = nullptr; |
| 540 | ForEachFunc_t* forEachFunctions = nullptr; |
| 541 | uint32_t* forEachSignatures = nullptr; |
| 542 | const char ** pragmaKeys = nullptr; |
| 543 | const char ** pragmaValues = nullptr; |
| 544 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 545 | const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info"); |
| 546 | |
| 547 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| 548 | return nullptr; |
| 549 | } |
| 550 | if (sscanf(line, EXPORT_VAR_STR "%zu", &varCount) != 1) { |
| 551 | ALOGE("Invalid export var count!: %s", line); |
| 552 | return nullptr; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 555 | fieldAddress = new void*[varCount]; |
| 556 | if (fieldAddress == nullptr) { |
| 557 | return nullptr; |
| 558 | } |
| 559 | |
| 560 | fieldIsObject = new bool[varCount]; |
| 561 | if (fieldIsObject == nullptr) { |
| 562 | goto error; |
| 563 | } |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 564 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 565 | for (size_t i = 0; i < varCount; ++i) { |
| 566 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 567 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 568 | } |
| 569 | char *c = strrchr(line, '\n'); |
| 570 | if (c) { |
| 571 | *c = '\0'; |
| 572 | } |
| 573 | void* addr = dlsym(sharedObj, line); |
| 574 | if (addr == nullptr) { |
| 575 | ALOGE("Failed to find variable address for %s: %s", |
| 576 | line, dlerror()); |
| 577 | // Not a critical error if we don't find a global variable. |
| 578 | } |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 579 | fieldAddress[i] = addr; |
| 580 | fieldIsObject[i] = false; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 581 | } |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 582 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 583 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 584 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 585 | } |
| 586 | if (sscanf(line, EXPORT_FUNC_STR "%zu", &funcCount) != 1) { |
| 587 | ALOGE("Invalid export func count!: %s", line); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 588 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 591 | invokeFunctions = new InvokeFunc_t[funcCount]; |
| 592 | if (invokeFunctions == nullptr) { |
| 593 | goto error; |
| 594 | } |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 595 | |
| 596 | for (size_t i = 0; i < funcCount; ++i) { |
| 597 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 598 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 599 | } |
| 600 | char *c = strrchr(line, '\n'); |
| 601 | if (c) { |
| 602 | *c = '\0'; |
| 603 | } |
| 604 | |
| 605 | invokeFunctions[i] = (InvokeFunc_t) dlsym(sharedObj, line); |
| 606 | if (invokeFunctions[i] == nullptr) { |
| 607 | ALOGE("Failed to get function address for %s(): %s", |
| 608 | line, dlerror()); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 609 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 614 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 615 | } |
| 616 | if (sscanf(line, EXPORT_FOREACH_STR "%zu", &forEachCount) != 1) { |
| 617 | ALOGE("Invalid export forEach count!: %s", line); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 618 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 621 | forEachFunctions = new ForEachFunc_t[forEachCount]; |
| 622 | if (forEachFunctions == nullptr) { |
| 623 | goto error; |
| 624 | } |
| 625 | |
| 626 | forEachSignatures = new uint32_t[forEachCount]; |
| 627 | if (forEachSignatures == nullptr) { |
| 628 | goto error; |
| 629 | } |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 630 | |
| 631 | for (size_t i = 0; i < forEachCount; ++i) { |
| 632 | unsigned int tmpSig = 0; |
| 633 | char tmpName[MAXLINE]; |
| 634 | |
| 635 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 636 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 637 | } |
| 638 | if (sscanf(line, "%u - %" MAKE_STR(MAXLINE) "s", |
| 639 | &tmpSig, tmpName) != 2) { |
| 640 | ALOGE("Invalid export forEach!: %s", line); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 641 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // Lookup the expanded ForEach kernel. |
| 645 | strncat(tmpName, ".expand", MAXLINE-1-strlen(tmpName)); |
| 646 | forEachSignatures[i] = tmpSig; |
| 647 | forEachFunctions[i] = |
| 648 | (ForEachFunc_t) dlsym(sharedObj, tmpName); |
| 649 | if (i != 0 && forEachFunctions[i] == nullptr) { |
| 650 | // Ignore missing root.expand functions. |
| 651 | // root() is always specified at location 0. |
| 652 | ALOGE("Failed to find forEach function address for %s: %s", |
| 653 | tmpName, dlerror()); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 654 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 659 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 660 | } |
| 661 | if (sscanf(line, OBJECT_SLOT_STR "%zu", &objectSlotCount) != 1) { |
| 662 | ALOGE("Invalid object slot count!: %s", line); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 663 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 666 | for (size_t i = 0; i < objectSlotCount; ++i) { |
| 667 | uint32_t varNum = 0; |
| 668 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 669 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 670 | } |
| 671 | if (sscanf(line, "%u", &varNum) != 1) { |
| 672 | ALOGE("Invalid object slot!: %s", line); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 673 | goto error; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | if (varNum < varCount) { |
| 677 | fieldIsObject[varNum] = true; |
| 678 | } |
| 679 | } |
| 680 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 681 | #ifndef RS_COMPATIBILITY_LIB |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 682 | // Do not attempt to read pragmas or isThreadable flag in compat lib path. |
| 683 | // Neither is applicable for compat lib |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 684 | |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 685 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 686 | goto error; |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (sscanf(line, PRAGMA_STR "%zu", &pragmaCount) != 1) { |
| 690 | ALOGE("Invalid pragma count!: %s", line); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 691 | goto error; |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 692 | } |
| 693 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 694 | pragmaKeys = new const char*[pragmaCount]; |
| 695 | if (pragmaKeys == nullptr) { |
| 696 | goto error; |
| 697 | } |
| 698 | |
| 699 | pragmaValues = new const char*[pragmaCount]; |
| 700 | if (pragmaValues == nullptr) { |
| 701 | goto error; |
| 702 | } |
| 703 | |
| 704 | bzero(pragmaKeys, sizeof(char*) * pragmaCount); |
| 705 | bzero(pragmaValues, sizeof(char*) * pragmaCount); |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 706 | |
| 707 | for (size_t i = 0; i < pragmaCount; ++i) { |
| 708 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| 709 | ALOGE("Unable to read pragma at index %zu!", i); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 710 | goto error; |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | char key[MAXLINE]; |
| 714 | char value[MAXLINE] = ""; // initialize in case value is empty |
| 715 | |
| 716 | // pragmas can just have a key and no value. Only check to make sure |
| 717 | // that the key is not empty |
| 718 | if (sscanf(line, "%" MAKE_STR(MAXLINE) "s - %" MAKE_STR(MAXLINE) "s", |
| 719 | key, value) == 0 || |
| 720 | strlen(key) == 0) |
| 721 | { |
| 722 | ALOGE("Invalid pragma value!: %s", line); |
| 723 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 724 | goto error; |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | char *pKey = new char[strlen(key)+1]; |
| 728 | strcpy(pKey, key); |
| 729 | pragmaKeys[i] = pKey; |
| 730 | |
| 731 | char *pValue = new char[strlen(value)+1]; |
| 732 | strcpy(pValue, value); |
| 733 | pragmaValues[i] = pValue; |
| 734 | //ALOGE("Pragma %zu: Key: '%s' Value: '%s'", i, pKey, pValue); |
| 735 | } |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 736 | |
| 737 | if (strgets(line, MAXLINE, &rsInfo) == nullptr) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 738 | goto error; |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | char tmpFlag[4]; |
| 742 | if (sscanf(line, THREADABLE_STR "%4s", tmpFlag) != 1) { |
| 743 | ALOGE("Invalid threadable flag!: %s", line); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 744 | goto error; |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 745 | } |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 746 | if (strcmp(tmpFlag, "yes") == 0) { |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 747 | isThreadable = true; |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 748 | } else if (strcmp(tmpFlag, "no") == 0) { |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 749 | isThreadable = false; |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 750 | } else { |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 751 | ALOGE("Invalid threadable flag!: %s", tmpFlag); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 752 | goto error; |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 755 | #endif // RS_COMPATIBILITY_LIB |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 756 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 757 | return new ScriptExecutable( |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 758 | RSContext, fieldAddress, fieldIsObject, varCount, |
| 759 | invokeFunctions, funcCount, |
| 760 | forEachFunctions, forEachSignatures, forEachCount, |
| 761 | pragmaKeys, pragmaValues, pragmaCount, |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 762 | isThreadable); |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 763 | |
| 764 | error: |
| 765 | |
| 766 | #ifndef RS_COMPATIBILITY_LIB |
| 767 | for (size_t idx = 0; idx < pragmaCount; ++idx) { |
| Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame^] | 768 | delete [] pragmaKeys[idx]; |
| 769 | delete [] pragmaValues[idx]; |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | delete[] pragmaValues; |
| 773 | delete[] pragmaKeys; |
| 774 | #endif // RS_COMPATIBILITY_LIB |
| 775 | |
| 776 | delete[] forEachSignatures; |
| 777 | delete[] forEachFunctions; |
| 778 | delete[] invokeFunctions; |
| 779 | delete[] fieldIsObject; |
| 780 | delete[] fieldAddress; |
| 781 | |
| 782 | return nullptr; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 783 | } |
| 784 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 785 | bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir, |
| 786 | uint8_t const *bitcode, size_t bitcodeSize, |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 787 | uint32_t flags, char const *bccPluginName) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 788 | //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, |
| 789 | // bitcode, bitcodeSize, flags, lookupFunc); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 790 | //ALOGE("rsdScriptInit %p %p", rsc, script); |
| 791 | |
| 792 | mCtx->lockMutex(); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 793 | #ifndef RS_COMPATIBILITY_LIB |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 794 | bool useRSDebugContext = false; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 795 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 796 | mCompilerDriver = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 797 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 798 | mCompilerDriver = new bcc::RSCompilerDriver(); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 799 | if (mCompilerDriver == nullptr) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 800 | ALOGE("bcc: FAILS to create compiler driver (out of memory)"); |
| 801 | mCtx->unlockMutex(); |
| 802 | return false; |
| 803 | } |
| 804 | |
| Stephen Hines | b7d9c80 | 2013-04-29 19:13:09 -0700 | [diff] [blame] | 805 | // Run any compiler setup functions we have been provided with. |
| 806 | RSSetupCompilerCallback setupCompilerCallback = |
| 807 | mCtx->getSetupCompilerCallback(); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 808 | if (setupCompilerCallback != nullptr) { |
| Stephen Hines | b7d9c80 | 2013-04-29 19:13:09 -0700 | [diff] [blame] | 809 | setupCompilerCallback(mCompilerDriver); |
| 810 | } |
| 811 | |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 812 | bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize); |
| 813 | if (!bitcodeMetadata.extract()) { |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 814 | ALOGE("Could not extract metadata from bitcode"); |
| Stephen Hines | f94e8db | 2014-06-26 11:55:29 -0700 | [diff] [blame] | 815 | mCtx->unlockMutex(); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 816 | return false; |
| 817 | } |
| 818 | |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 819 | const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize); |
| Stephen Hines | cca3d6c | 2013-04-15 01:06:39 -0700 | [diff] [blame] | 820 | |
| 821 | if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) { |
| Stephen Hines | f47e8b4 | 2013-04-18 01:06:29 -0700 | [diff] [blame] | 822 | mCompilerDriver->setDebugContext(true); |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 823 | useRSDebugContext = true; |
| Stephen Hines | cca3d6c | 2013-04-15 01:06:39 -0700 | [diff] [blame] | 824 | } |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 825 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 826 | std::string bcFileName(cacheDir); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 827 | bcFileName.append("/"); |
| 828 | bcFileName.append(resName); |
| 829 | bcFileName.append(".bc"); |
| 830 | |
| 831 | std::vector<const char*> compileArguments; |
| 832 | setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib, |
| 833 | useRSDebugContext, bccPluginName); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 834 | // The last argument of compileArguments ia a nullptr, so remove 1 from the size. |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 835 | std::string compileCommandLine = |
| 836 | bcc::getCommandLine(compileArguments.size() - 1, compileArguments.data()); |
| 837 | |
| Tim Murray | bf96a52 | 2015-01-23 15:37:03 -0800 | [diff] [blame] | 838 | if (!is_force_recompile() && !useRSDebugContext) { |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 839 | mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | // If we can't, it's either not there or out of date. We compile the bit code and try loading |
| 843 | // again. |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 844 | if (mScriptSO == nullptr) { |
| 845 | if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize, |
| 846 | compileArguments.data(), compileCommandLine)) |
| 847 | { |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 848 | ALOGE("bcc: FAILS to compile '%s'", resName); |
| 849 | mCtx->unlockMutex(); |
| 850 | return false; |
| 851 | } |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 852 | |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 853 | if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) { |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 854 | ALOGE("Linker: Failed to link object file '%s'", resName); |
| 855 | mCtx->unlockMutex(); |
| 856 | return false; |
| 857 | } |
| 858 | |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 859 | mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName); |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 860 | if (mScriptSO == nullptr) { |
| 861 | ALOGE("Unable to load '%s'", resName); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 862 | mCtx->unlockMutex(); |
| 863 | return false; |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 866 | |
| Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame^] | 867 | mBitcodeFilePath = bcFileName; |
| 868 | |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 869 | // Read RS symbol information from the .so. |
| 870 | if ( !mScriptSO) { |
| 871 | goto error; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 872 | } |
| 873 | |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 874 | if ( !storeRSInfoFromSO()) { |
| 875 | goto error; |
| Tim Murray | 29809d1 | 2014-05-28 12:04:19 -0700 | [diff] [blame] | 876 | } |
| Jean-Luc Brouillet | f4d216e | 2014-06-09 18:04:16 -0700 | [diff] [blame] | 877 | #else // RS_COMPATIBILITY_LIB is defined |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 878 | |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 879 | mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 880 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 881 | if (!mScriptSO) { |
| 882 | goto error; |
| 883 | } |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 884 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 885 | if (!storeRSInfoFromSO()) { |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 886 | goto error; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 887 | } |
| 888 | #endif |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 889 | mCtx->unlockMutex(); |
| 890 | return true; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 891 | |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 892 | error: |
| 893 | |
| 894 | mCtx->unlockMutex(); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 895 | if (mScriptSO) { |
| 896 | dlclose(mScriptSO); |
| 897 | } |
| 898 | return false; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 901 | #ifndef RS_COMPATIBILITY_LIB |
| 902 | |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 903 | const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode, |
| 904 | size_t bitcodeSize) { |
| 905 | const char* defaultLib = SYSLIBPATH"/libclcore.bc"; |
| 906 | |
| 907 | // If we're debugging, use the debug library. |
| 908 | if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) { |
| 909 | return SYSLIBPATH"/libclcore_debug.bc"; |
| 910 | } |
| 911 | |
| 912 | // If a callback has been registered to specify a library, use that. |
| 913 | RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback(); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 914 | if (selectRTCallback != nullptr) { |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 915 | return selectRTCallback((const char*)bitcode, bitcodeSize); |
| 916 | } |
| 917 | |
| 918 | // Check for a platform specific library |
| 919 | #if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON) |
| 920 | enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision(); |
| Jean-Luc Brouillet | f4d3836 | 2014-07-09 17:46:03 -0700 | [diff] [blame] | 921 | if (prec == bcinfo::RS_FP_Relaxed) { |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 922 | // NEON-capable ARMv7a devices can use an accelerated math library |
| 923 | // for all reduced precision scripts. |
| 924 | // ARMv8 does not use NEON, as ASIMD can be used with all precision |
| 925 | // levels. |
| 926 | return SYSLIBPATH"/libclcore_neon.bc"; |
| 927 | } else { |
| 928 | return defaultLib; |
| 929 | } |
| 930 | #elif defined(__i386__) || defined(__x86_64__) |
| 931 | // x86 devices will use an optimized library. |
| 932 | return SYSLIBPATH"/libclcore_x86.bc"; |
| 933 | #else |
| 934 | return defaultLib; |
| 935 | #endif |
| 936 | } |
| 937 | |
| 938 | #endif |
| 939 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 940 | void RsdCpuScriptImpl::populateScript(Script *script) { |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 941 | // Copy info over to runtime |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 942 | script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount(); |
| 943 | script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount(); |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 944 | script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();; |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 945 | script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys(); |
| 946 | script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues(); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 947 | |
| 948 | // Bug, need to stash in metadata |
| 949 | if (mRootExpand) { |
| 950 | script->mHal.info.root = mRootExpand; |
| 951 | } else { |
| 952 | script->mHal.info.root = mRoot; |
| 953 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 956 | |
| 957 | typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); |
| 958 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 959 | bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains, |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 960 | uint32_t inLen, |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 961 | Allocation * aout, |
| 962 | const void * usr, uint32_t usrLen, |
| 963 | const RsScriptCall *sc, |
| 964 | MTLaunchStruct *mtls) { |
| 965 | |
| 966 | memset(mtls, 0, sizeof(MTLaunchStruct)); |
| 967 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 968 | for (int index = inLen; --index >= 0;) { |
| 969 | const Allocation* ain = ains[index]; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 970 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 971 | // possible for this to occur if IO_OUTPUT/IO_INPUT with no bound surface |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 972 | if (ain != nullptr && |
| 973 | (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) { |
| 974 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 975 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 976 | "rsForEach called with null in allocations"); |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 977 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 981 | if (aout && |
| 982 | (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) { |
| 983 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 984 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 985 | "rsForEach called with null out allocations"); |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 986 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 989 | if (inLen > 0) { |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 990 | const Allocation *ain0 = ains[0]; |
| 991 | const Type *inType = ain0->getType(); |
| 992 | |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 993 | mtls->fep.dim.x = inType->getDimX(); |
| 994 | mtls->fep.dim.y = inType->getDimY(); |
| 995 | mtls->fep.dim.z = inType->getDimZ(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 996 | |
| 997 | for (int Index = inLen; --Index >= 1;) { |
| 998 | if (!ain0->hasSameDims(ains[Index])) { |
| 999 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 1000 | "Failed to launch kernel; dimensions of input and output" |
| 1001 | "allocations do not match."); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1002 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1003 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1007 | } else if (aout != nullptr) { |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1008 | const Type *outType = aout->getType(); |
| 1009 | |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 1010 | mtls->fep.dim.x = outType->getDimX(); |
| 1011 | mtls->fep.dim.y = outType->getDimY(); |
| 1012 | mtls->fep.dim.z = outType->getDimZ(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1013 | |
| 1014 | } else { |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 1015 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 1016 | "rsForEach called with null allocations"); |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1017 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1020 | if (inLen > 0 && aout != nullptr) { |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1021 | if (!ains[0]->hasSameDims(aout)) { |
| 1022 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 1023 | "Failed to launch kernel; dimensions of input and output allocations do not match."); |
| 1024 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1025 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (!sc || (sc->xEnd == 0)) { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1030 | mtls->end.x = mtls->fep.dim.x; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1031 | } else { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1032 | mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart); |
| 1033 | mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd); |
| 1034 | if (mtls->start.x >= mtls->end.x) return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if (!sc || (sc->yEnd == 0)) { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1038 | mtls->end.y = mtls->fep.dim.y; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1039 | } else { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1040 | mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart); |
| 1041 | mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd); |
| 1042 | if (mtls->start.y >= mtls->end.y) return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | if (!sc || (sc->zEnd == 0)) { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1046 | mtls->end.z = mtls->fep.dim.z; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1047 | } else { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1048 | mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart); |
| 1049 | mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd); |
| 1050 | if (mtls->start.z >= mtls->end.z) return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1053 | if (!sc || (sc->arrayEnd == 0)) { |
| 1054 | mtls->end.array[0] = mtls->fep.dim.array[0]; |
| 1055 | } else { |
| 1056 | mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart); |
| 1057 | mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd); |
| 1058 | if (mtls->start.array[0] >= mtls->end.array[0]) return false; |
| 1059 | } |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1060 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1061 | if (!sc || (sc->array2End == 0)) { |
| 1062 | mtls->end.array[1] = mtls->fep.dim.array[1]; |
| 1063 | } else { |
| 1064 | mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start); |
| 1065 | mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End); |
| 1066 | if (mtls->start.array[1] >= mtls->end.array[1]) return false; |
| 1067 | } |
| 1068 | |
| 1069 | if (!sc || (sc->array3End == 0)) { |
| 1070 | mtls->end.array[2] = mtls->fep.dim.array[2]; |
| 1071 | } else { |
| 1072 | mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start); |
| 1073 | mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End); |
| 1074 | if (mtls->start.array[2] >= mtls->end.array[2]) return false; |
| 1075 | } |
| 1076 | |
| 1077 | if (!sc || (sc->array4End == 0)) { |
| 1078 | mtls->end.array[3] = mtls->fep.dim.array[3]; |
| 1079 | } else { |
| 1080 | mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start); |
| 1081 | mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End); |
| 1082 | if (mtls->start.array[3] >= mtls->end.array[3]) return false; |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | // The X & Y walkers always want 0-1 min even if dim is not present |
| 1087 | mtls->end.x = rsMax((uint32_t)1, mtls->end.x); |
| 1088 | mtls->end.y = rsMax((uint32_t)1, mtls->end.y); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1089 | |
| 1090 | mtls->rsc = mCtx; |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 1091 | if (ains) { |
| 1092 | memcpy(mtls->ains, ains, inLen * sizeof(ains[0])); |
| 1093 | } |
| 1094 | mtls->aout[0] = aout; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1095 | mtls->fep.usr = usr; |
| 1096 | mtls->fep.usrLen = usrLen; |
| 1097 | mtls->mSliceSize = 1; |
| 1098 | mtls->mSliceNum = 0; |
| 1099 | |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1100 | mtls->isThreadable = mIsThreadable; |
| 1101 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 1102 | if (inLen > 0) { |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 1103 | mtls->fep.inLen = inLen; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1104 | for (int index = inLen; --index >= 0;) { |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 1105 | mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr; |
| 1106 | mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1110 | if (aout != nullptr) { |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 1111 | mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr; |
| 1112 | mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1113 | } |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1114 | |
| 1115 | // All validation passed, ok to launch threads |
| 1116 | return true; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1119 | |
| 1120 | void RsdCpuScriptImpl::invokeForEach(uint32_t slot, |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 1121 | const Allocation ** ains, |
| 1122 | uint32_t inLen, |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1123 | Allocation * aout, |
| 1124 | const void * usr, |
| 1125 | uint32_t usrLen, |
| 1126 | const RsScriptCall *sc) { |
| 1127 | |
| 1128 | MTLaunchStruct mtls; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1129 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1130 | if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) { |
| 1131 | forEachKernelSetup(slot, &mtls); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1132 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 1133 | RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); |
| 1134 | mCtx->launchThreads(ains, inLen, aout, sc, &mtls); |
| 1135 | mCtx->setTLS(oldTLS); |
| 1136 | } |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1139 | void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1140 | mtls->script = this; |
| 1141 | mtls->fep.slot = slot; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1142 | mtls->kernel = mScriptExec->getForEachFunction(slot); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1143 | rsAssert(mtls->kernel != nullptr); |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1144 | mtls->sig = mScriptExec->getForEachSignature(slot); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | int RsdCpuScriptImpl::invokeRoot() { |
| 1148 | RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); |
| 1149 | int ret = mRoot(); |
| 1150 | mCtx->setTLS(oldTLS); |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
| 1154 | void RsdCpuScriptImpl::invokeInit() { |
| 1155 | if (mInit) { |
| 1156 | mInit(); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | void RsdCpuScriptImpl::invokeFreeChildren() { |
| 1161 | if (mFreeChildren) { |
| 1162 | mFreeChildren(); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params, |
| 1167 | size_t paramLength) { |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1168 | //ALOGE("invoke %i %p %zu", slot, params, paramLength); |
| Yong Chen | eaba5a3 | 2014-12-12 13:25:18 +0800 | [diff] [blame] | 1169 | void * ap = nullptr; |
| 1170 | |
| 1171 | #if defined(__x86_64__) |
| 1172 | // The invoked function could have input parameter of vector type for example float4 which |
| 1173 | // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform. |
| 1174 | // So try to align void* params before passing them into RS exported function. |
| 1175 | |
| 1176 | if ((uint8_t)(uint64_t)params & 0x0F) { |
| 1177 | if ((ap = (void*)memalign(16, paramLength)) != nullptr) { |
| 1178 | memcpy(ap, params, paramLength); |
| 1179 | } else { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 1180 | ALOGE("x86_64: invokeFunction memalign error, still use params which" |
| 1181 | " is not 16 bytes aligned."); |
| Yong Chen | eaba5a3 | 2014-12-12 13:25:18 +0800 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | #endif |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1185 | |
| 1186 | RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1187 | reinterpret_cast<void (*)(const void *, uint32_t)>( |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1188 | mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength); |
| Yong Chen | eaba5a3 | 2014-12-12 13:25:18 +0800 | [diff] [blame] | 1189 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1190 | mCtx->setTLS(oldTLS); |
| 1191 | } |
| 1192 | |
| 1193 | void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { |
| 1194 | //rsAssert(!script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1195 | //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1196 | |
| 1197 | //if (mIntrinsicID) { |
| 1198 | //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength); |
| 1199 | //return; |
| 1200 | //} |
| 1201 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1202 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1203 | if (!destPtr) { |
| 1204 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | memcpy(destPtr, data, dataLength); |
| 1209 | } |
| 1210 | |
| Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 1211 | void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) { |
| 1212 | //rsAssert(!script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1213 | //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength); |
| Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 1214 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1215 | int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 1216 | if (!srcPtr) { |
| 1217 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 1218 | return; |
| 1219 | } |
| 1220 | memcpy(data, srcPtr, dataLength); |
| 1221 | } |
| 1222 | |
| 1223 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1224 | void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength, |
| 1225 | const Element *elem, |
| Stephen Hines | ac8d146 | 2014-06-25 00:01:23 -0700 | [diff] [blame] | 1226 | const uint32_t *dims, size_t dimLength) { |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1227 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1228 | if (!destPtr) { |
| 1229 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | // We want to look at dimension in terms of integer components, |
| 1234 | // but dimLength is given in terms of bytes. |
| 1235 | dimLength /= sizeof(int); |
| 1236 | |
| 1237 | // Only a single dimension is currently supported. |
| 1238 | rsAssert(dimLength == 1); |
| 1239 | if (dimLength == 1) { |
| 1240 | // First do the increment loop. |
| 1241 | size_t stride = elem->getSizeBytes(); |
| 1242 | const char *cVal = reinterpret_cast<const char *>(data); |
| Stephen Hines | ac8d146 | 2014-06-25 00:01:23 -0700 | [diff] [blame] | 1243 | for (uint32_t i = 0; i < dims[0]; i++) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1244 | elem->incRefs(cVal); |
| 1245 | cVal += stride; |
| 1246 | } |
| 1247 | |
| 1248 | // Decrement loop comes after (to prevent race conditions). |
| 1249 | char *oldVal = reinterpret_cast<char *>(destPtr); |
| Stephen Hines | ac8d146 | 2014-06-25 00:01:23 -0700 | [diff] [blame] | 1250 | for (uint32_t i = 0; i < dims[0]; i++) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1251 | elem->decRefs(oldVal); |
| 1252 | oldVal += stride; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | memcpy(destPtr, data, dataLength); |
| 1257 | } |
| 1258 | |
| 1259 | void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) { |
| 1260 | |
| 1261 | //rsAssert(!script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1262 | //ALOGE("setGlobalBind %i %p", slot, data); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1263 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1264 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1265 | if (!destPtr) { |
| 1266 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 1267 | return; |
| 1268 | } |
| 1269 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1270 | void *ptr = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1271 | mBoundAllocs[slot] = data; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1272 | if (data) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1273 | ptr = data->mHal.drvState.lod[0].mallocPtr; |
| 1274 | } |
| 1275 | memcpy(destPtr, &ptr, sizeof(void *)); |
| 1276 | } |
| 1277 | |
| 1278 | void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| 1279 | |
| 1280 | //rsAssert(script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1281 | //ALOGE("setGlobalObj %i %p", slot, data); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1282 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1283 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1284 | if (!destPtr) { |
| 1285 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| Jason Sams | 05ef73f | 2014-08-05 14:59:22 -0700 | [diff] [blame] | 1289 | rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | RsdCpuScriptImpl::~RsdCpuScriptImpl() { |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 1293 | #ifndef RS_COMPATIBILITY_LIB |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1294 | if (mCompilerDriver) { |
| 1295 | delete mCompilerDriver; |
| 1296 | } |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 1297 | #endif |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1298 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 1299 | if (mScriptExec != nullptr) { |
| 1300 | delete mScriptExec; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 1301 | } |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 1302 | if (mBoundAllocs) delete[] mBoundAllocs; |
| 1303 | if (mScriptSO) { |
| 1304 | dlclose(mScriptSO); |
| 1305 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const { |
| 1309 | if (!ptr) { |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1310 | return nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) { |
| 1314 | Allocation *a = mBoundAllocs[ct]; |
| 1315 | if (!a) continue; |
| 1316 | if (a->mHal.drvState.lod[0].mallocPtr == ptr) { |
| 1317 | return a; |
| 1318 | } |
| 1319 | } |
| 1320 | ALOGE("rsGetAllocation, failed to find %p", ptr); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1321 | return nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 1324 | void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains, |
| 1325 | uint32_t inLen, Allocation * aout, |
| 1326 | const void * usr, uint32_t usrLen, |
| 1327 | const RsScriptCall *sc) {} |
| Jason Sams | 17e3cdc | 2013-09-09 17:32:16 -0700 | [diff] [blame] | 1328 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 1329 | void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains, |
| 1330 | uint32_t inLen, Allocation * aout, |
| 1331 | const void * usr, uint32_t usrLen, |
| 1332 | const RsScriptCall *sc) {} |
| Jason Sams | 17e3cdc | 2013-09-09 17:32:16 -0700 | [diff] [blame] | 1333 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1334 | |
| 1335 | } |
| 1336 | } |