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