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