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