| 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" |
| Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 19 | #include "rsCpuExecutable.h" |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 20 | |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 21 | #ifdef RS_COMPATIBILITY_LIB |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| Stephen Hines | ee48c0b | 2013-10-30 17:48:30 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 25 | #else |
| Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 26 | #include "rsCppUtils.h" |
| 27 | |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 28 | #include <bcc/BCCContext.h> |
| Stephen Hines | 82e0a67 | 2014-05-05 15:40:56 -0700 | [diff] [blame] | 29 | #include <bcc/Config/Config.h> |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 30 | #include <bcc/Renderscript/RSCompilerDriver.h> |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 31 | #include <bcinfo/MetadataExtractor.h> |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 33 | |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 34 | #include <zlib.h> |
| 35 | #include <sys/file.h> |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 36 | #include <sys/types.h> |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 37 | #include <unistd.h> |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 38 | |
| 39 | #include <string> |
| 40 | #include <vector> |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 41 | #endif |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 42 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 43 | #include <set> |
| 44 | #include <string> |
| 45 | #include <dlfcn.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 48 | #include <iostream> |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 49 | #include <sstream> |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 50 | |
| 51 | #ifdef __LP64__ |
| 52 | #define SYSLIBPATH "/system/lib64" |
| 53 | #else |
| 54 | #define SYSLIBPATH "/system/lib" |
| 55 | #endif |
| 56 | |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 57 | namespace { |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 58 | |
| 59 | static const bool kDebugGlobalVariables = false; |
| 60 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 61 | #ifndef RS_COMPATIBILITY_LIB |
| 62 | |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 63 | static bool is_force_recompile() { |
| 64 | #ifdef RS_SERVER |
| 65 | return false; |
| 66 | #else |
| 67 | char buf[PROPERTY_VALUE_MAX]; |
| 68 | |
| 69 | // Re-compile if floating point precision has been overridden. |
| 70 | property_get("debug.rs.precision", buf, ""); |
| 71 | if (buf[0] != '\0') { |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | // Re-compile if debug.rs.forcerecompile is set. |
| 76 | property_get("debug.rs.forcerecompile", buf, "0"); |
| 77 | if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) { |
| 78 | return true; |
| 79 | } else { |
| 80 | return false; |
| 81 | } |
| 82 | #endif // RS_SERVER |
| 83 | } |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 84 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 85 | static void setCompileArguments(std::vector<const char*>* args, |
| 86 | const std::string& bcFileName, |
| 87 | const char* cacheDir, const char* resName, |
| 88 | const char* core_lib, bool useRSDebugContext, |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 89 | const char* bccPluginName, bool emitGlobalInfo, |
| 90 | bool emitGlobalInfoSkipConstant) { |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 91 | rsAssert(cacheDir && resName && core_lib); |
| Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame] | 92 | args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH); |
| Tim Murray | 687cfe8 | 2015-01-08 14:59:38 -0800 | [diff] [blame] | 93 | args->push_back("-unroll-runtime"); |
| 94 | args->push_back("-scalarize-load-store"); |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 95 | if (emitGlobalInfo) { |
| 96 | args->push_back("-rs-global-info"); |
| 97 | if (emitGlobalInfoSkipConstant) { |
| 98 | args->push_back("-rs-global-info-skip-constant"); |
| 99 | } |
| 100 | } |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 101 | args->push_back("-o"); |
| 102 | args->push_back(resName); |
| 103 | args->push_back("-output_path"); |
| 104 | args->push_back(cacheDir); |
| 105 | args->push_back("-bclib"); |
| 106 | args->push_back(core_lib); |
| 107 | args->push_back("-mtriple"); |
| 108 | args->push_back(DEFAULT_TARGET_TRIPLE_STRING); |
| 109 | |
| Tim Murray | 358ffb8 | 2014-12-09 11:53:06 -0800 | [diff] [blame] | 110 | // Enable workaround for A53 codegen by default. |
| 111 | #if defined(__aarch64__) && !defined(DISABLE_A53_WORKAROUND) |
| 112 | args->push_back("-aarch64-fix-cortex-a53-835769"); |
| 113 | #endif |
| 114 | |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 115 | // Execute the bcc compiler. |
| 116 | if (useRSDebugContext) { |
| 117 | args->push_back("-rs-debug-ctx"); |
| 118 | } else { |
| 119 | // Only load additional libraries for compiles that don't use |
| 120 | // the debug context. |
| 121 | if (bccPluginName && strlen(bccPluginName) > 0) { |
| 122 | args->push_back("-load"); |
| 123 | args->push_back(bccPluginName); |
| 124 | } |
| 125 | } |
| 126 | |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 127 | args->push_back("-fPIC"); |
| 128 | args->push_back("-embedRSInfo"); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 129 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 130 | args->push_back(bcFileName.c_str()); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 131 | args->push_back(nullptr); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 134 | static bool compileBitcode(const std::string &bcFileName, |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 135 | const char *bitcode, |
| 136 | size_t bitcodeSize, |
| Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 137 | std::vector<const char *> &compileArguments) { |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 138 | rsAssert(bitcode && bitcodeSize); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 139 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 140 | FILE *bcfile = fopen(bcFileName.c_str(), "w"); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 141 | if (!bcfile) { |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 142 | ALOGE("Could not write to %s", bcFileName.c_str()); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | size_t nwritten = fwrite(bitcode, 1, bitcodeSize, bcfile); |
| 146 | fclose(bcfile); |
| 147 | if (nwritten != bitcodeSize) { |
| 148 | ALOGE("Could not write %zu bytes to %s", bitcodeSize, |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 149 | bcFileName.c_str()); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
| Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 153 | return android::renderscript::rsuExecuteCommand( |
| 154 | android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH, |
| 155 | compileArguments.size()-1, compileArguments.data()); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 156 | } |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 157 | |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 158 | bool isChecksumNeeded() { |
| 159 | char buf[PROPERTY_VALUE_MAX]; |
| 160 | property_get("ro.debuggable", buf, ""); |
| 161 | return (buf[0] == '1'); |
| 162 | } |
| 163 | |
| 164 | bool addFileToChecksum(const char *fileName, uint32_t &checksum) { |
| 165 | int FD = open(fileName, O_RDONLY); |
| 166 | if (FD == -1) { |
| 167 | ALOGE("Cannot open file \'%s\' to compute checksum", fileName); |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | char buf[256]; |
| 172 | while (true) { |
| 173 | ssize_t nread = read(FD, buf, sizeof(buf)); |
| 174 | if (nread < 0) { // bail out on failed read |
| 175 | ALOGE("Error while computing checksum for file \'%s\'", fileName); |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | checksum = adler32(checksum, (const unsigned char *) buf, nread); |
| 180 | if (static_cast<size_t>(nread) < sizeof(buf)) // EOF |
| 181 | break; |
| 182 | } |
| 183 | |
| Elliott Hughes | 2df5767 | 2015-05-15 17:06:47 -0700 | [diff] [blame] | 184 | if (close(FD) != 0) { |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 185 | ALOGE("Cannot close file \'%s\' after computing checksum", fileName); |
| 186 | return false; |
| 187 | } |
| 188 | return true; |
| 189 | } |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 190 | |
| 191 | #endif // !defined(RS_COMPATIBILITY_LIB) |
| 192 | } // namespace |
| 193 | |
| 194 | namespace android { |
| 195 | namespace renderscript { |
| 196 | |
| 197 | #ifndef RS_COMPATIBILITY_LIB |
| 198 | |
| 199 | uint32_t constructBuildChecksum(uint8_t const *bitcode, size_t bitcodeSize, |
| 200 | const char *commandLine, |
| 201 | const char** bccFiles, size_t numFiles) { |
| 202 | uint32_t checksum = adler32(0L, Z_NULL, 0); |
| 203 | |
| 204 | // include checksum of bitcode |
| 205 | if (bitcode != nullptr && bitcodeSize > 0) { |
| 206 | checksum = adler32(checksum, bitcode, bitcodeSize); |
| 207 | } |
| 208 | |
| 209 | // include checksum of command line arguments |
| 210 | checksum = adler32(checksum, (const unsigned char *) commandLine, |
| 211 | strlen(commandLine)); |
| 212 | |
| 213 | // include checksum of bccFiles |
| 214 | for (size_t i = 0; i < numFiles; i++) { |
| 215 | const char* bccFile = bccFiles[i]; |
| 216 | if (bccFile[0] != 0 && !addFileToChecksum(bccFile, checksum)) { |
| 217 | // return empty checksum instead of something partial/corrupt |
| 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return checksum; |
| 223 | } |
| 224 | |
| Yang Ni | f02a2b0 | 2015-04-07 16:00:31 -0700 | [diff] [blame] | 225 | #endif // !RS_COMPATIBILITY_LIB |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 226 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 227 | RsdCpuScriptImpl::RsdCpuScriptImpl(RsdCpuReferenceImpl *ctx, const Script *s) { |
| 228 | mCtx = ctx; |
| 229 | mScript = s; |
| 230 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 231 | mScriptSO = nullptr; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 232 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 233 | #ifndef RS_COMPATIBILITY_LIB |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 234 | mCompilerDriver = nullptr; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 235 | #endif |
| 236 | |
| Tim Murray | e195a3f | 2014-03-13 15:04:58 -0700 | [diff] [blame] | 237 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 238 | mRoot = nullptr; |
| 239 | mRootExpand = nullptr; |
| 240 | mInit = nullptr; |
| 241 | mFreeChildren = nullptr; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 242 | mScriptExec = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 243 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 244 | mBoundAllocs = nullptr; |
| 245 | mIntrinsicData = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 246 | mIsThreadable = true; |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 247 | |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 248 | mBuildChecksum = 0; |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 249 | mChecksumNeeded = false; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 252 | bool RsdCpuScriptImpl::storeRSInfoFromSO() { |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 253 | // The shared object may have an invalid build checksum. |
| 254 | // Validate and fail early. |
| 255 | mScriptExec = ScriptExecutable::createFromSharedObject( |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 256 | mCtx->getContext(), mScriptSO, |
| 257 | mChecksumNeeded ? mBuildChecksum : 0); |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 258 | |
| 259 | if (mScriptExec == nullptr) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 263 | mRoot = (RootFunc_t) dlsym(mScriptSO, "root"); |
| 264 | if (mRoot) { |
| 265 | //ALOGE("Found root(): %p", mRoot); |
| 266 | } |
| 267 | mRootExpand = (RootFunc_t) dlsym(mScriptSO, "root.expand"); |
| 268 | if (mRootExpand) { |
| 269 | //ALOGE("Found root.expand(): %p", mRootExpand); |
| 270 | } |
| 271 | mInit = (InvokeFunc_t) dlsym(mScriptSO, "init"); |
| 272 | if (mInit) { |
| 273 | //ALOGE("Found init(): %p", mInit); |
| 274 | } |
| 275 | mFreeChildren = (InvokeFunc_t) dlsym(mScriptSO, ".rs.dtor"); |
| 276 | if (mFreeChildren) { |
| 277 | //ALOGE("Found .rs.dtor(): %p", mFreeChildren); |
| 278 | } |
| 279 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 280 | size_t varCount = mScriptExec->getExportedVariableCount(); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 281 | if (varCount > 0) { |
| 282 | mBoundAllocs = new Allocation *[varCount]; |
| 283 | memset(mBoundAllocs, 0, varCount * sizeof(*mBoundAllocs)); |
| 284 | } |
| 285 | |
| Pirama Arumuga Nainar | 68173de | 2015-01-28 12:12:36 -0800 | [diff] [blame] | 286 | mIsThreadable = mScriptExec->getThreadable(); |
| 287 | //ALOGE("Script isThreadable? %d", mIsThreadable); |
| 288 | |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 289 | if (kDebugGlobalVariables) { |
| 290 | mScriptExec->dumpGlobalInfo(); |
| 291 | } |
| 292 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 296 | bool RsdCpuScriptImpl::init(char const *resName, char const *cacheDir, |
| 297 | uint8_t const *bitcode, size_t bitcodeSize, |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 298 | uint32_t flags, char const *bccPluginName) { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 299 | //ALOGE("rsdScriptCreate %p %p %p %p %i %i %p", rsc, resName, cacheDir, |
| 300 | // bitcode, bitcodeSize, flags, lookupFunc); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 301 | //ALOGE("rsdScriptInit %p %p", rsc, script); |
| 302 | |
| 303 | mCtx->lockMutex(); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 304 | #ifndef RS_COMPATIBILITY_LIB |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 305 | bool useRSDebugContext = false; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 306 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 307 | mCompilerDriver = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 308 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 309 | mCompilerDriver = new bcc::RSCompilerDriver(); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 310 | if (mCompilerDriver == nullptr) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 311 | ALOGE("bcc: FAILS to create compiler driver (out of memory)"); |
| 312 | mCtx->unlockMutex(); |
| 313 | return false; |
| 314 | } |
| 315 | |
| Stephen Hines | b7d9c80 | 2013-04-29 19:13:09 -0700 | [diff] [blame] | 316 | // Run any compiler setup functions we have been provided with. |
| 317 | RSSetupCompilerCallback setupCompilerCallback = |
| 318 | mCtx->getSetupCompilerCallback(); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 319 | if (setupCompilerCallback != nullptr) { |
| Stephen Hines | b7d9c80 | 2013-04-29 19:13:09 -0700 | [diff] [blame] | 320 | setupCompilerCallback(mCompilerDriver); |
| 321 | } |
| 322 | |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 323 | bcinfo::MetadataExtractor bitcodeMetadata((const char *) bitcode, bitcodeSize); |
| 324 | if (!bitcodeMetadata.extract()) { |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 325 | ALOGE("Could not extract metadata from bitcode"); |
| Stephen Hines | f94e8db | 2014-06-26 11:55:29 -0700 | [diff] [blame] | 326 | mCtx->unlockMutex(); |
| Stephen Hines | b58d9ad | 2013-06-19 19:26:19 -0700 | [diff] [blame] | 327 | return false; |
| 328 | } |
| 329 | |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 330 | const char* core_lib = findCoreLib(bitcodeMetadata, (const char*)bitcode, bitcodeSize); |
| Stephen Hines | cca3d6c | 2013-04-15 01:06:39 -0700 | [diff] [blame] | 331 | |
| 332 | if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) { |
| Stephen Hines | f47e8b4 | 2013-04-18 01:06:29 -0700 | [diff] [blame] | 333 | mCompilerDriver->setDebugContext(true); |
| Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 334 | useRSDebugContext = true; |
| Stephen Hines | cca3d6c | 2013-04-15 01:06:39 -0700 | [diff] [blame] | 335 | } |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 336 | |
| Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 337 | std::string bcFileName(cacheDir); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 338 | bcFileName.append("/"); |
| 339 | bcFileName.append(resName); |
| 340 | bcFileName.append(".bc"); |
| 341 | |
| 342 | std::vector<const char*> compileArguments; |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 343 | bool emitGlobalInfo = mCtx->getEmbedGlobalInfo(); |
| 344 | bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant(); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 345 | setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib, |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 346 | useRSDebugContext, bccPluginName, emitGlobalInfo, |
| 347 | emitGlobalInfoSkipConstant); |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 348 | |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 349 | mChecksumNeeded = isChecksumNeeded(); |
| 350 | if (mChecksumNeeded) { |
| 351 | std::vector<const char *> bccFiles = { BCC_EXE_PATH, |
| 352 | core_lib, |
| 353 | }; |
| Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 354 | |
| 355 | // The last argument of compileArguments is a nullptr, so remove 1 from |
| 356 | // the size. |
| 357 | std::unique_ptr<const char> compileCommandLine( |
| 358 | rsuJoinStrings(compileArguments.size()-1, compileArguments.data())); |
| 359 | |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 360 | mBuildChecksum = constructBuildChecksum(bitcode, bitcodeSize, |
| 361 | compileCommandLine.get(), |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 362 | bccFiles.data(), bccFiles.size()); |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 363 | |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 364 | if (mBuildChecksum == 0) { |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 365 | // cannot compute checksum but verification is enabled |
| 366 | mCtx->unlockMutex(); |
| 367 | return false; |
| 368 | } |
| 369 | } |
| 370 | else { |
| 371 | // add a dummy/constant as a checksum if verification is disabled |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 372 | mBuildChecksum = 0xabadcafe; |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // Append build checksum to commandline |
| 376 | // Handle the terminal nullptr in compileArguments |
| 377 | compileArguments.pop_back(); |
| 378 | compileArguments.push_back("-build-checksum"); |
| Yang Ni | cb17015 | 2015-04-16 10:27:02 -0700 | [diff] [blame] | 379 | std::stringstream ss; |
| 380 | ss << std::hex << mBuildChecksum; |
| 381 | compileArguments.push_back(ss.str().c_str()); |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 382 | compileArguments.push_back(nullptr); |
| 383 | |
| Tim Murray | bf96a52 | 2015-01-23 15:37:03 -0800 | [diff] [blame] | 384 | if (!is_force_recompile() && !useRSDebugContext) { |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 385 | mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName); |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 386 | |
| 387 | // Read RS info from the shared object to detect checksum mismatch |
| 388 | if (mScriptSO != nullptr && !storeRSInfoFromSO()) { |
| 389 | dlclose(mScriptSO); |
| 390 | mScriptSO = nullptr; |
| 391 | } |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // If we can't, it's either not there or out of date. We compile the bit code and try loading |
| 395 | // again. |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 396 | if (mScriptSO == nullptr) { |
| 397 | if (!compileBitcode(bcFileName, (const char*)bitcode, bitcodeSize, |
| Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 398 | compileArguments)) |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 399 | { |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 400 | ALOGE("bcc: FAILS to compile '%s'", resName); |
| 401 | mCtx->unlockMutex(); |
| 402 | return false; |
| 403 | } |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 404 | |
| Stephen Hines | 4c368af | 2015-05-06 00:43:02 -0700 | [diff] [blame] | 405 | if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(), |
| 406 | cacheDir, resName)) { |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 407 | ALOGE("Linker: Failed to link object file '%s'", resName); |
| 408 | mCtx->unlockMutex(); |
| 409 | return false; |
| 410 | } |
| 411 | |
| Yang Ni | 1c44cb6 | 2015-01-22 12:02:27 -0800 | [diff] [blame] | 412 | mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName); |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 413 | if (mScriptSO == nullptr) { |
| 414 | ALOGE("Unable to load '%s'", resName); |
| Jean-Luc Brouillet | 40e35cd | 2014-06-25 18:21:45 -0700 | [diff] [blame] | 415 | mCtx->unlockMutex(); |
| 416 | return false; |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 417 | } |
| Pirama Arumuga Nainar | aa6757f | 2015-02-13 20:02:50 -0800 | [diff] [blame] | 418 | |
| 419 | // Read RS symbol information from the .so. |
| 420 | if (!storeRSInfoFromSO()) { |
| 421 | goto error; |
| 422 | } |
| Stephen Hines | ba17ae4 | 2013-06-05 17:18:04 -0700 | [diff] [blame] | 423 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 424 | |
| Yang Ni | c31585b | 2015-02-15 11:43:50 -0800 | [diff] [blame] | 425 | mBitcodeFilePath.setTo(bcFileName.c_str()); |
| Yang Ni | da0f069 | 2015-01-12 13:03:40 -0800 | [diff] [blame] | 426 | |
| Jean-Luc Brouillet | f4d216e | 2014-06-09 18:04:16 -0700 | [diff] [blame] | 427 | #else // RS_COMPATIBILITY_LIB is defined |
| Miao Wang | f3213d7 | 2015-01-14 10:03:07 -0800 | [diff] [blame] | 428 | const char *nativeLibDir = mCtx->getContext()->getNativeLibDir(); |
| 429 | mScriptSO = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName, nativeLibDir); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 430 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 431 | if (!mScriptSO) { |
| 432 | goto error; |
| 433 | } |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 434 | |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 435 | if (!storeRSInfoFromSO()) { |
| Stephen Hines | c2c11cc | 2013-07-19 01:07:42 -0700 | [diff] [blame] | 436 | goto error; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 437 | } |
| 438 | #endif |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 439 | mCtx->unlockMutex(); |
| 440 | return true; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 441 | |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 442 | error: |
| 443 | |
| 444 | mCtx->unlockMutex(); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 445 | if (mScriptSO) { |
| 446 | dlclose(mScriptSO); |
| Yang Ni | eb9aa67 | 2015-01-27 14:32:25 -0800 | [diff] [blame] | 447 | mScriptSO = nullptr; |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 448 | } |
| 449 | return false; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 452 | #ifndef RS_COMPATIBILITY_LIB |
| 453 | |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 454 | const char* RsdCpuScriptImpl::findCoreLib(const bcinfo::MetadataExtractor& ME, const char* bitcode, |
| 455 | size_t bitcodeSize) { |
| 456 | const char* defaultLib = SYSLIBPATH"/libclcore.bc"; |
| 457 | |
| 458 | // If we're debugging, use the debug library. |
| 459 | if (mCtx->getContext()->getContextType() == RS_CONTEXT_TYPE_DEBUG) { |
| 460 | return SYSLIBPATH"/libclcore_debug.bc"; |
| 461 | } |
| 462 | |
| 463 | // If a callback has been registered to specify a library, use that. |
| 464 | RSSelectRTCallback selectRTCallback = mCtx->getSelectRTCallback(); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 465 | if (selectRTCallback != nullptr) { |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 466 | return selectRTCallback((const char*)bitcode, bitcodeSize); |
| 467 | } |
| 468 | |
| 469 | // Check for a platform specific library |
| 470 | #if defined(ARCH_ARM_HAVE_NEON) && !defined(DISABLE_CLCORE_NEON) |
| 471 | enum bcinfo::RSFloatPrecision prec = ME.getRSFloatPrecision(); |
| Jean-Luc Brouillet | f4d3836 | 2014-07-09 17:46:03 -0700 | [diff] [blame] | 472 | if (prec == bcinfo::RS_FP_Relaxed) { |
| Jean-Luc Brouillet | 9ab5094 | 2014-06-18 18:10:32 -0700 | [diff] [blame] | 473 | // NEON-capable ARMv7a devices can use an accelerated math library |
| 474 | // for all reduced precision scripts. |
| 475 | // ARMv8 does not use NEON, as ASIMD can be used with all precision |
| 476 | // levels. |
| 477 | return SYSLIBPATH"/libclcore_neon.bc"; |
| 478 | } else { |
| 479 | return defaultLib; |
| 480 | } |
| 481 | #elif defined(__i386__) || defined(__x86_64__) |
| 482 | // x86 devices will use an optimized library. |
| 483 | return SYSLIBPATH"/libclcore_x86.bc"; |
| 484 | #else |
| 485 | return defaultLib; |
| 486 | #endif |
| 487 | } |
| 488 | |
| 489 | #endif |
| 490 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 491 | void RsdCpuScriptImpl::populateScript(Script *script) { |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 492 | // Copy info over to runtime |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 493 | script->mHal.info.exportedFunctionCount = mScriptExec->getExportedFunctionCount(); |
| 494 | script->mHal.info.exportedVariableCount = mScriptExec->getExportedVariableCount(); |
| Pirama Arumuga Nainar | 577194a | 2015-01-23 14:27:33 -0800 | [diff] [blame] | 495 | script->mHal.info.exportedPragmaCount = mScriptExec->getPragmaCount();; |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 496 | script->mHal.info.exportedPragmaKeyList = mScriptExec->getPragmaKeys(); |
| 497 | script->mHal.info.exportedPragmaValueList = mScriptExec->getPragmaValues(); |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 498 | |
| 499 | // Bug, need to stash in metadata |
| 500 | if (mRootExpand) { |
| 501 | script->mHal.info.root = mRootExpand; |
| 502 | } else { |
| 503 | script->mHal.info.root = mRoot; |
| 504 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 507 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 508 | bool RsdCpuScriptImpl::forEachMtlsSetup(const Allocation ** ains, |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 509 | uint32_t inLen, |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 510 | Allocation * aout, |
| 511 | const void * usr, uint32_t usrLen, |
| 512 | const RsScriptCall *sc, |
| 513 | MTLaunchStruct *mtls) { |
| 514 | |
| 515 | memset(mtls, 0, sizeof(MTLaunchStruct)); |
| 516 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 517 | for (int index = inLen; --index >= 0;) { |
| 518 | const Allocation* ain = ains[index]; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 519 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 520 | // 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] | 521 | if (ain != nullptr && |
| 522 | (const uint8_t *)ain->mHal.drvState.lod[0].mallocPtr == nullptr) { |
| 523 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 524 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 525 | "rsForEach called with null in allocations"); |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 526 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 530 | if (aout && |
| 531 | (const uint8_t *)aout->mHal.drvState.lod[0].mallocPtr == nullptr) { |
| 532 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 533 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 534 | "rsForEach called with null out allocations"); |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 535 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 538 | if (inLen > 0) { |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 539 | const Allocation *ain0 = ains[0]; |
| 540 | const Type *inType = ain0->getType(); |
| 541 | |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 542 | mtls->fep.dim.x = inType->getDimX(); |
| 543 | mtls->fep.dim.y = inType->getDimY(); |
| 544 | mtls->fep.dim.z = inType->getDimZ(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 545 | |
| 546 | for (int Index = inLen; --Index >= 1;) { |
| 547 | if (!ain0->hasSameDims(ains[Index])) { |
| 548 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 549 | "Failed to launch kernel; dimensions of input and output" |
| 550 | "allocations do not match."); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 551 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 552 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 556 | } else if (aout != nullptr) { |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 557 | const Type *outType = aout->getType(); |
| 558 | |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 559 | mtls->fep.dim.x = outType->getDimX(); |
| 560 | mtls->fep.dim.y = outType->getDimY(); |
| 561 | mtls->fep.dim.z = outType->getDimZ(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 562 | |
| Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 563 | } else if (sc != nullptr) { |
| 564 | mtls->fep.dim.x = sc->xEnd; |
| 565 | mtls->fep.dim.y = sc->yEnd; |
| 566 | mtls->fep.dim.z = 0; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 567 | } else { |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 568 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 569 | "rsForEach called with null allocations"); |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 570 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 573 | if (inLen > 0 && aout != nullptr) { |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 574 | if (!ains[0]->hasSameDims(aout)) { |
| 575 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 576 | "Failed to launch kernel; dimensions of input and output allocations do not match."); |
| 577 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 578 | return false; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | if (!sc || (sc->xEnd == 0)) { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 583 | mtls->end.x = mtls->fep.dim.x; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 584 | } else { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 585 | mtls->start.x = rsMin(mtls->fep.dim.x, sc->xStart); |
| 586 | mtls->end.x = rsMin(mtls->fep.dim.x, sc->xEnd); |
| Pirama Arumuga Nainar | 80afd42 | 2015-04-28 15:41:48 -0700 | [diff] [blame] | 587 | if (mtls->start.x >= mtls->end.x) { |
| 588 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 589 | "Failed to launch kernel; Invalid xStart or xEnd."); |
| 590 | return false; |
| 591 | } |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if (!sc || (sc->yEnd == 0)) { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 595 | mtls->end.y = mtls->fep.dim.y; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 596 | } else { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 597 | mtls->start.y = rsMin(mtls->fep.dim.y, sc->yStart); |
| 598 | mtls->end.y = rsMin(mtls->fep.dim.y, sc->yEnd); |
| Pirama Arumuga Nainar | 80afd42 | 2015-04-28 15:41:48 -0700 | [diff] [blame] | 599 | if (mtls->start.y >= mtls->end.y) { |
| 600 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 601 | "Failed to launch kernel; Invalid yStart or yEnd."); |
| 602 | return false; |
| 603 | } |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | if (!sc || (sc->zEnd == 0)) { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 607 | mtls->end.z = mtls->fep.dim.z; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 608 | } else { |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 609 | mtls->start.z = rsMin(mtls->fep.dim.z, sc->zStart); |
| 610 | mtls->end.z = rsMin(mtls->fep.dim.z, sc->zEnd); |
| Pirama Arumuga Nainar | 80afd42 | 2015-04-28 15:41:48 -0700 | [diff] [blame] | 611 | if (mtls->start.z >= mtls->end.z) { |
| 612 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 613 | "Failed to launch kernel; Invalid zStart or zEnd."); |
| 614 | return false; |
| 615 | } |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 618 | if (!sc || (sc->arrayEnd == 0)) { |
| 619 | mtls->end.array[0] = mtls->fep.dim.array[0]; |
| 620 | } else { |
| 621 | mtls->start.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayStart); |
| 622 | mtls->end.array[0] = rsMin(mtls->fep.dim.array[0], sc->arrayEnd); |
| Pirama Arumuga Nainar | 80afd42 | 2015-04-28 15:41:48 -0700 | [diff] [blame] | 623 | if (mtls->start.array[0] >= mtls->end.array[0]) { |
| 624 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 625 | "Failed to launch kernel; Invalid arrayStart or arrayEnd."); |
| 626 | return false; |
| 627 | } |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 628 | } |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 629 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 630 | if (!sc || (sc->array2End == 0)) { |
| 631 | mtls->end.array[1] = mtls->fep.dim.array[1]; |
| 632 | } else { |
| 633 | mtls->start.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2Start); |
| 634 | mtls->end.array[1] = rsMin(mtls->fep.dim.array[1], sc->array2End); |
| Pirama Arumuga Nainar | 80afd42 | 2015-04-28 15:41:48 -0700 | [diff] [blame] | 635 | if (mtls->start.array[1] >= mtls->end.array[1]) { |
| 636 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 637 | "Failed to launch kernel; Invalid array2Start or array2End."); |
| 638 | return false; |
| 639 | } |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | if (!sc || (sc->array3End == 0)) { |
| 643 | mtls->end.array[2] = mtls->fep.dim.array[2]; |
| 644 | } else { |
| 645 | mtls->start.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3Start); |
| 646 | mtls->end.array[2] = rsMin(mtls->fep.dim.array[2], sc->array3End); |
| Pirama Arumuga Nainar | 80afd42 | 2015-04-28 15:41:48 -0700 | [diff] [blame] | 647 | if (mtls->start.array[2] >= mtls->end.array[2]) { |
| 648 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 649 | "Failed to launch kernel; Invalid array3Start or array3End."); |
| 650 | return false; |
| 651 | } |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (!sc || (sc->array4End == 0)) { |
| 655 | mtls->end.array[3] = mtls->fep.dim.array[3]; |
| 656 | } else { |
| 657 | mtls->start.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4Start); |
| 658 | mtls->end.array[3] = rsMin(mtls->fep.dim.array[3], sc->array4End); |
| Pirama Arumuga Nainar | 80afd42 | 2015-04-28 15:41:48 -0700 | [diff] [blame] | 659 | if (mtls->start.array[3] >= mtls->end.array[3]) { |
| 660 | mCtx->getContext()->setError(RS_ERROR_BAD_SCRIPT, |
| 661 | "Failed to launch kernel; Invalid array4Start or array4End."); |
| 662 | return false; |
| 663 | } |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | |
| 667 | // The X & Y walkers always want 0-1 min even if dim is not present |
| 668 | mtls->end.x = rsMax((uint32_t)1, mtls->end.x); |
| 669 | mtls->end.y = rsMax((uint32_t)1, mtls->end.y); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 670 | |
| 671 | mtls->rsc = mCtx; |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 672 | if (ains) { |
| 673 | memcpy(mtls->ains, ains, inLen * sizeof(ains[0])); |
| 674 | } |
| 675 | mtls->aout[0] = aout; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 676 | mtls->fep.usr = usr; |
| 677 | mtls->fep.usrLen = usrLen; |
| 678 | mtls->mSliceSize = 1; |
| 679 | mtls->mSliceNum = 0; |
| 680 | |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 681 | mtls->isThreadable = mIsThreadable; |
| 682 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 683 | if (inLen > 0) { |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 684 | mtls->fep.inLen = inLen; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 685 | for (int index = inLen; --index >= 0;) { |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 686 | mtls->fep.inPtr[index] = (const uint8_t*)ains[index]->mHal.drvState.lod[0].mallocPtr; |
| 687 | mtls->fep.inStride[index] = ains[index]->getType()->getElementSizeBytes(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 691 | if (aout != nullptr) { |
| Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 692 | mtls->fep.outPtr[0] = (uint8_t *)aout->mHal.drvState.lod[0].mallocPtr; |
| 693 | mtls->fep.outStride[0] = aout->getType()->getElementSizeBytes(); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 694 | } |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 695 | |
| 696 | // All validation passed, ok to launch threads |
| 697 | return true; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 700 | |
| 701 | void RsdCpuScriptImpl::invokeForEach(uint32_t slot, |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 702 | const Allocation ** ains, |
| 703 | uint32_t inLen, |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 704 | Allocation * aout, |
| 705 | const void * usr, |
| 706 | uint32_t usrLen, |
| 707 | const RsScriptCall *sc) { |
| 708 | |
| 709 | MTLaunchStruct mtls; |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 710 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 711 | if (forEachMtlsSetup(ains, inLen, aout, usr, usrLen, sc, &mtls)) { |
| 712 | forEachKernelSetup(slot, &mtls); |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 713 | |
| Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 714 | RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); |
| 715 | mCtx->launchThreads(ains, inLen, aout, sc, &mtls); |
| 716 | mCtx->setTLS(oldTLS); |
| 717 | } |
| Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 720 | void RsdCpuScriptImpl::forEachKernelSetup(uint32_t slot, MTLaunchStruct *mtls) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 721 | mtls->script = this; |
| 722 | mtls->fep.slot = slot; |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 723 | mtls->kernel = mScriptExec->getForEachFunction(slot); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 724 | rsAssert(mtls->kernel != nullptr); |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 725 | mtls->sig = mScriptExec->getForEachSignature(slot); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | int RsdCpuScriptImpl::invokeRoot() { |
| 729 | RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); |
| 730 | int ret = mRoot(); |
| 731 | mCtx->setTLS(oldTLS); |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | void RsdCpuScriptImpl::invokeInit() { |
| 736 | if (mInit) { |
| 737 | mInit(); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | void RsdCpuScriptImpl::invokeFreeChildren() { |
| 742 | if (mFreeChildren) { |
| 743 | mFreeChildren(); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | void RsdCpuScriptImpl::invokeFunction(uint32_t slot, const void *params, |
| 748 | size_t paramLength) { |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 749 | //ALOGE("invoke %i %p %zu", slot, params, paramLength); |
| Yong Chen | eaba5a3 | 2014-12-12 13:25:18 +0800 | [diff] [blame] | 750 | void * ap = nullptr; |
| 751 | |
| 752 | #if defined(__x86_64__) |
| 753 | // The invoked function could have input parameter of vector type for example float4 which |
| 754 | // requires void* params to be 16 bytes aligned when using SSE instructions for x86_64 platform. |
| 755 | // So try to align void* params before passing them into RS exported function. |
| 756 | |
| 757 | if ((uint8_t)(uint64_t)params & 0x0F) { |
| 758 | if ((ap = (void*)memalign(16, paramLength)) != nullptr) { |
| 759 | memcpy(ap, params, paramLength); |
| 760 | } else { |
| Yang Ni | e8f9fba | 2015-01-30 08:55:10 -0800 | [diff] [blame] | 761 | ALOGE("x86_64: invokeFunction memalign error, still use params which" |
| 762 | " is not 16 bytes aligned."); |
| Yong Chen | eaba5a3 | 2014-12-12 13:25:18 +0800 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | #endif |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 766 | |
| 767 | RsdCpuScriptImpl * oldTLS = mCtx->setTLS(this); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 768 | reinterpret_cast<void (*)(const void *, uint32_t)>( |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 769 | mScriptExec->getInvokeFunction(slot))(ap? (const void *) ap: params, paramLength); |
| Yong Chen | eaba5a3 | 2014-12-12 13:25:18 +0800 | [diff] [blame] | 770 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 771 | mCtx->setTLS(oldTLS); |
| 772 | } |
| 773 | |
| 774 | void RsdCpuScriptImpl::setGlobalVar(uint32_t slot, const void *data, size_t dataLength) { |
| 775 | //rsAssert(!script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 776 | //ALOGE("setGlobalVar %i %p %zu", slot, data, dataLength); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 777 | |
| 778 | //if (mIntrinsicID) { |
| 779 | //mIntrinsicFuncs.setVar(dc, script, drv->mIntrinsicData, slot, data, dataLength); |
| 780 | //return; |
| 781 | //} |
| 782 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 783 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 784 | if (!destPtr) { |
| 785 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | memcpy(destPtr, data, dataLength); |
| 790 | } |
| 791 | |
| Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 792 | void RsdCpuScriptImpl::getGlobalVar(uint32_t slot, void *data, size_t dataLength) { |
| 793 | //rsAssert(!script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 794 | //ALOGE("getGlobalVar %i %p %zu", slot, data, dataLength); |
| Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 795 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 796 | int32_t *srcPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 797 | if (!srcPtr) { |
| 798 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 799 | return; |
| 800 | } |
| 801 | memcpy(data, srcPtr, dataLength); |
| 802 | } |
| 803 | |
| 804 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 805 | void RsdCpuScriptImpl::setGlobalVarWithElemDims(uint32_t slot, const void *data, size_t dataLength, |
| 806 | const Element *elem, |
| Stephen Hines | ac8d146 | 2014-06-25 00:01:23 -0700 | [diff] [blame] | 807 | const uint32_t *dims, size_t dimLength) { |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 808 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 809 | if (!destPtr) { |
| 810 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 811 | return; |
| 812 | } |
| 813 | |
| 814 | // We want to look at dimension in terms of integer components, |
| 815 | // but dimLength is given in terms of bytes. |
| 816 | dimLength /= sizeof(int); |
| 817 | |
| 818 | // Only a single dimension is currently supported. |
| 819 | rsAssert(dimLength == 1); |
| 820 | if (dimLength == 1) { |
| 821 | // First do the increment loop. |
| 822 | size_t stride = elem->getSizeBytes(); |
| 823 | const char *cVal = reinterpret_cast<const char *>(data); |
| Stephen Hines | ac8d146 | 2014-06-25 00:01:23 -0700 | [diff] [blame] | 824 | for (uint32_t i = 0; i < dims[0]; i++) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 825 | elem->incRefs(cVal); |
| 826 | cVal += stride; |
| 827 | } |
| 828 | |
| 829 | // Decrement loop comes after (to prevent race conditions). |
| 830 | char *oldVal = reinterpret_cast<char *>(destPtr); |
| Stephen Hines | ac8d146 | 2014-06-25 00:01:23 -0700 | [diff] [blame] | 831 | for (uint32_t i = 0; i < dims[0]; i++) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 832 | elem->decRefs(oldVal); |
| 833 | oldVal += stride; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | memcpy(destPtr, data, dataLength); |
| 838 | } |
| 839 | |
| 840 | void RsdCpuScriptImpl::setGlobalBind(uint32_t slot, Allocation *data) { |
| 841 | |
| 842 | //rsAssert(!script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 843 | //ALOGE("setGlobalBind %i %p", slot, data); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 844 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 845 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 846 | if (!destPtr) { |
| 847 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 848 | return; |
| 849 | } |
| 850 | |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 851 | void *ptr = nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 852 | mBoundAllocs[slot] = data; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 853 | if (data) { |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 854 | ptr = data->mHal.drvState.lod[0].mallocPtr; |
| 855 | } |
| 856 | memcpy(destPtr, &ptr, sizeof(void *)); |
| 857 | } |
| 858 | |
| 859 | void RsdCpuScriptImpl::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| 860 | |
| 861 | //rsAssert(script->mFieldIsObject[slot]); |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 862 | //ALOGE("setGlobalObj %i %p", slot, data); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 863 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 864 | int32_t *destPtr = reinterpret_cast<int32_t *>(mScriptExec->getFieldAddress(slot)); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 865 | if (!destPtr) { |
| 866 | //ALOGV("Calling setVar on slot = %i which is null", slot); |
| 867 | return; |
| 868 | } |
| 869 | |
| Jason Sams | 05ef73f | 2014-08-05 14:59:22 -0700 | [diff] [blame] | 870 | rsrSetObject(mCtx->getContext(), (rs_object_base *)destPtr, data); |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| Yang Ni | 062c287 | 2015-02-20 15:20:00 -0800 | [diff] [blame] | 873 | const char* RsdCpuScriptImpl::getFieldName(uint32_t slot) const { |
| 874 | return mScriptExec->getFieldName(slot); |
| 875 | } |
| 876 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 877 | RsdCpuScriptImpl::~RsdCpuScriptImpl() { |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 878 | #ifndef RS_COMPATIBILITY_LIB |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 879 | if (mCompilerDriver) { |
| 880 | delete mCompilerDriver; |
| 881 | } |
| Stephen Hines | 45e753a | 2015-01-19 20:58:44 -0800 | [diff] [blame] | 882 | #endif |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 883 | |
| Yang Ni | d9bae68 | 2015-01-20 15:31:15 -0800 | [diff] [blame] | 884 | if (mScriptExec != nullptr) { |
| 885 | delete mScriptExec; |
| Pirama Arumuga Nainar | dc0d8f7 | 2014-12-02 15:23:38 -0800 | [diff] [blame] | 886 | } |
| Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 887 | if (mBoundAllocs) delete[] mBoundAllocs; |
| 888 | if (mScriptSO) { |
| 889 | dlclose(mScriptSO); |
| 890 | } |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | Allocation * RsdCpuScriptImpl::getAllocationForPointer(const void *ptr) const { |
| 894 | if (!ptr) { |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 895 | return nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | for (uint32_t ct=0; ct < mScript->mHal.info.exportedVariableCount; ct++) { |
| 899 | Allocation *a = mBoundAllocs[ct]; |
| 900 | if (!a) continue; |
| 901 | if (a->mHal.drvState.lod[0].mallocPtr == ptr) { |
| 902 | return a; |
| 903 | } |
| 904 | } |
| 905 | ALOGE("rsGetAllocation, failed to find %p", ptr); |
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 906 | return nullptr; |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 907 | } |
| 908 | |
| Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 909 | int RsdCpuScriptImpl::getGlobalEntries() const { |
| 910 | return mScriptExec->getGlobalEntries(); |
| 911 | } |
| 912 | |
| 913 | const char * RsdCpuScriptImpl::getGlobalName(int i) const { |
| 914 | return mScriptExec->getGlobalName(i); |
| 915 | } |
| 916 | |
| 917 | const void * RsdCpuScriptImpl::getGlobalAddress(int i) const { |
| 918 | return mScriptExec->getGlobalAddress(i); |
| 919 | } |
| 920 | |
| 921 | size_t RsdCpuScriptImpl::getGlobalSize(int i) const { |
| 922 | return mScriptExec->getGlobalSize(i); |
| 923 | } |
| 924 | |
| Stephen Hines | 5aa018c | 2015-05-20 18:09:57 -0700 | [diff] [blame] | 925 | uint32_t RsdCpuScriptImpl::getGlobalProperties(int i) const { |
| 926 | return mScriptExec->getGlobalProperties(i); |
| 927 | } |
| 928 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 929 | void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains, |
| 930 | uint32_t inLen, Allocation * aout, |
| 931 | const void * usr, uint32_t usrLen, |
| 932 | const RsScriptCall *sc) {} |
| Jason Sams | 17e3cdc | 2013-09-09 17:32:16 -0700 | [diff] [blame] | 933 | |
| Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 934 | void RsdCpuScriptImpl::postLaunch(uint32_t slot, const Allocation ** ains, |
| 935 | uint32_t inLen, Allocation * aout, |
| 936 | const void * usr, uint32_t usrLen, |
| 937 | const RsScriptCall *sc) {} |
| Jason Sams | 17e3cdc | 2013-09-09 17:32:16 -0700 | [diff] [blame] | 938 | |
| Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 939 | |
| 940 | } |
| 941 | } |