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