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