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