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