Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 4419977 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 |
| 7 | * |
| 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 | |
| 17 | #include "rsContext.h" |
| 18 | #include "rsScriptC.h" |
Jack Palevich | 1ef8b80 | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 19 | |
Jean-Luc Brouillet | f4d216e | 2014-06-09 18:04:16 -0700 | [diff] [blame] | 20 | #if !defined(RS_COMPATIBILITY_LIB) && !defined(ANDROID_RS_SERIALIZE) |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 21 | #include <bcinfo/BitcodeTranslator.h> |
Stephen Hines | f8d4469 | 2011-11-22 19:43:58 -0800 | [diff] [blame] | 22 | #include <bcinfo/BitcodeWrapper.h> |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 23 | #endif |
| 24 | |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | |
Stephen Hines | d969fdc | 2016-06-14 16:19:40 -0700 | [diff] [blame] | 27 | #include <sstream> |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 28 | #include <string> |
| 29 | |
Elliott Hughes | ed5c8b4 | 2016-10-05 09:49:16 -0700 | [diff] [blame] | 30 | #ifdef _WIN32 |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 31 | /* Define the default path separator for the platform. */ |
| 32 | #define OS_PATH_SEPARATOR '\\' |
| 33 | #define OS_PATH_SEPARATOR_STR "\\" |
| 34 | |
Elliott Hughes | ed5c8b4 | 2016-10-05 09:49:16 -0700 | [diff] [blame] | 35 | #else /* not _WIN32 */ |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 36 | |
| 37 | /* Define the default path separator for the platform. */ |
| 38 | #define OS_PATH_SEPARATOR '/' |
| 39 | #define OS_PATH_SEPARATOR_STR "/" |
| 40 | |
| 41 | #endif |
| 42 | |
Chih-Hung Hsieh | 11496ac | 2016-11-15 15:14:05 -0800 | [diff] [blame] | 43 | using android::renderscript::ScriptC; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 45 | #define GET_TLS() Context::ScriptTLSStruct * tls = \ |
| 46 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ |
| 47 | Context * rsc = tls->mContext; \ |
| 48 | ScriptC * sc = (ScriptC *) tls->mScript |
| 49 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 50 | ScriptC::ScriptC(Context *rsc) : Script(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 53 | ScriptC::~ScriptC() { |
Jason Sams | 77020c5 | 2011-11-22 12:49:11 -0800 | [diff] [blame] | 54 | if (mInitialized) { |
| 55 | mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this); |
| 56 | mRSC->mHal.funcs.script.destroy(mRSC, this); |
| 57 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 60 | #ifndef RS_COMPATIBILITY_LIB |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 61 | bool ScriptC::createCacheDir(const char *cacheDir) { |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 62 | std::string currentDir; |
| 63 | const std::string cacheDirString(cacheDir); |
| 64 | |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 65 | struct stat statBuf; |
| 66 | int statReturn = stat(cacheDir, &statBuf); |
| 67 | if (!statReturn) { |
| 68 | return true; |
| 69 | } |
| 70 | |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 71 | // Start from the beginning of the cacheDirString. |
| 72 | int currPos = 0; |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 73 | |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 74 | // Reserve space in currentDir for the entire cacheDir path. |
| 75 | currentDir.reserve(cacheDirString.length()); |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 76 | |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 77 | while (currPos >= 0) { |
| 78 | /* |
| 79 | * The character at currPos should be a path separator. We need to look |
| 80 | * for the next one. |
| 81 | */ |
Chih-Hung Hsieh | 431758a | 2018-09-24 14:00:41 -0700 | [diff] [blame] | 82 | int nextPos = cacheDirString.find(OS_PATH_SEPARATOR, currPos + 1); |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 83 | |
| 84 | if (nextPos > 0) { |
| 85 | // A new path separator has been found. |
| 86 | currentDir += cacheDirString.substr(currPos, nextPos - currPos); |
| 87 | } else { |
| 88 | // There are no more path separators. |
| 89 | currentDir += cacheDirString.substr(currPos); |
| 90 | } |
| 91 | |
| 92 | currPos = nextPos; |
| 93 | |
| 94 | statReturn = stat(currentDir.c_str(), &statBuf); |
| 95 | |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 96 | if (statReturn) { |
| 97 | if (errno == ENOENT) { |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 98 | if (mkdir(currentDir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR)) { |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 99 | ALOGE("Couldn't create cache directory: %s", |
Chris Wailes | 6847e73 | 2014-08-11 17:30:51 -0700 | [diff] [blame] | 100 | currentDir.c_str()); |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 101 | ALOGE("Error: %s", strerror(errno)); |
| 102 | return false; |
| 103 | } |
| 104 | } else { |
| 105 | ALOGE("Stat error: %s", strerror(errno)); |
| 106 | return false; |
| 107 | } |
| 108 | } |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 109 | } |
| 110 | return true; |
| 111 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 112 | #endif |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 113 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 114 | void ScriptC::setupScript(Context *rsc) { |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 115 | mEnviroment.mStartTimeMillis |
| 116 | = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 117 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 118 | for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) { |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 119 | if (mSlots[ct].get() && !mTypes[ct].get()) { |
| 120 | mTypes[ct].set(mSlots[ct]->getType()); |
| 121 | } |
| 122 | |
| 123 | if (!mTypes[ct].get()) |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 124 | continue; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 125 | rsc->mHal.funcs.script.setGlobalBind(rsc, this, ct, mSlots[ct].get()); |
Jason Sams | ada7f27 | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 129 | void ScriptC::setupGLState(Context *rsc) { |
Miao Wang | 59f6142 | 2017-03-14 14:23:52 -0700 | [diff] [blame] | 130 | #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB) |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 131 | if (mEnviroment.mFragmentStore.get()) { |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 132 | rsc->setProgramStore(mEnviroment.mFragmentStore.get()); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 133 | } |
| 134 | if (mEnviroment.mFragment.get()) { |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 135 | rsc->setProgramFragment(mEnviroment.mFragment.get()); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 136 | } |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 137 | if (mEnviroment.mVertex.get()) { |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 138 | rsc->setProgramVertex(mEnviroment.mVertex.get()); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 139 | } |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 140 | if (mEnviroment.mRaster.get()) { |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 141 | rsc->setProgramRaster(mEnviroment.mRaster.get()); |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 142 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 143 | #endif |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 144 | } |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 145 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 146 | uint32_t ScriptC::run(Context *rsc) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 147 | if (mHal.info.root == nullptr) { |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 148 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); |
| 149 | return 0; |
Joe Onorato | 9c4e4ca | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 150 | } |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 151 | |
Jason Sams | 1f24db4 | 2010-12-11 17:42:30 -0800 | [diff] [blame] | 152 | setupGLState(rsc); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 153 | setupScript(rsc); |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 154 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 155 | uint32_t ret = 0; |
Jason Sams | b9077f4 | 2010-09-22 15:57:41 -0700 | [diff] [blame] | 156 | |
| 157 | if (rsc->props.mLogScripts) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 158 | ALOGV("%p ScriptC::run invoking root, ptr %p", rsc, mHal.info.root); |
Jason Sams | b9077f4 | 2010-09-22 15:57:41 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 161 | ret = rsc->mHal.funcs.script.invokeRoot(rsc, this); |
Jason Sams | b9077f4 | 2010-09-22 15:57:41 -0700 | [diff] [blame] | 162 | |
| 163 | if (rsc->props.mLogScripts) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 164 | ALOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret); |
Jason Sams | b9077f4 | 2010-09-22 15:57:41 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 167 | return ret; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jason Sams | 177f844 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 170 | |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 171 | void ScriptC::runForEach(Context *rsc, |
Stephen Hines | 4419977 | 2012-02-21 20:13:12 -0800 | [diff] [blame] | 172 | uint32_t slot, |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 173 | const Allocation ** ains, |
| 174 | size_t inLen, |
| 175 | Allocation * aout, |
| 176 | const void * usr, |
| 177 | size_t usrBytes, |
| 178 | const RsScriptCall *sc) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 179 | if (slot >= mHal.info.exportedForEachCount) { |
| 180 | rsc->setError(RS_ERROR_BAD_SCRIPT, |
| 181 | "The forEach kernel index is out of bounds"); |
| 182 | return; |
| 183 | } |
| 184 | |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 185 | // Trace this function call. |
| 186 | // To avoid overhead we only build the string if tracing is actually |
| 187 | // enabled. |
Stephen Hines | d969fdc | 2016-06-14 16:19:40 -0700 | [diff] [blame] | 188 | std::stringstream ss; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 189 | if (ATRACE_ENABLED()) { |
Stephen Hines | d969fdc | 2016-06-14 16:19:40 -0700 | [diff] [blame] | 190 | ss << "runForEach slot[" << slot << "]"; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 191 | } |
Yang Ni | 578419f | 2016-06-27 16:12:25 -0700 | [diff] [blame] | 192 | std::string msgStr(ss.str()); |
| 193 | ATRACE_NAME(msgStr.c_str()); |
Stephen Hines | d969fdc | 2016-06-14 16:19:40 -0700 | [diff] [blame] | 194 | |
Jason Sams | 4efe3d3 | 2015-03-18 18:28:06 -0700 | [diff] [blame] | 195 | if (mRSC->hadFatalError()) return; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 196 | |
| 197 | Context::PushState ps(rsc); |
| 198 | |
| 199 | setupGLState(rsc); |
| 200 | setupScript(rsc); |
| 201 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 202 | if (rsc->props.mLogScripts) { |
| 203 | ALOGV("%p ScriptC::runForEach invoking slot %i, ptr %p", rsc, slot, this); |
| 204 | } |
| 205 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 206 | if (rsc->mHal.funcs.script.invokeForEachMulti != nullptr) { |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 207 | rsc->mHal.funcs.script.invokeForEachMulti(rsc, this, slot, ains, inLen, |
| 208 | aout, usr, usrBytes, sc); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 209 | |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 210 | } else if (inLen == 1) { |
| 211 | rsc->mHal.funcs.script.invokeForEach(rsc, this, slot, ains[0], aout, |
| 212 | usr, usrBytes, sc); |
| 213 | |
| 214 | } else { |
| 215 | rsc->setError(RS_ERROR_FATAL_DRIVER, |
| 216 | "Driver support for multi-input not present"); |
| 217 | } |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 218 | } |
| 219 | |
David Gross | fc7ab79 | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 220 | void ScriptC::runReduce(Context *rsc, uint32_t slot, |
| 221 | const Allocation ** ains, size_t inLen, |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 222 | Allocation *aout, const RsScriptCall *sc) { |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 223 | // TODO: Record the name of the kernel in the tracing information. |
| 224 | ATRACE_CALL(); |
| 225 | |
David Gross | fc7ab79 | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 226 | if (slot >= mHal.info.exportedReduceCount) { |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 227 | rsc->setError(RS_ERROR_BAD_SCRIPT, "The general reduce kernel index is out of bounds"); |
| 228 | return; |
| 229 | } |
| 230 | if (mRSC->hadFatalError()) return; |
| 231 | |
| 232 | setupScript(rsc); |
| 233 | |
| 234 | if (rsc->props.mLogScripts) { |
David Gross | fc7ab79 | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 235 | ALOGV("%p ScriptC::runReduce invoking slot %i, ptr %p", rsc, slot, this); |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 236 | } |
| 237 | |
David Gross | fc7ab79 | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 238 | rsc->mHal.funcs.script.invokeReduce(rsc, this, slot, ains, inLen, aout, sc); |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 241 | void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, size_t len) { |
Tim Murray | fa85e91 | 2013-05-23 13:19:36 -0700 | [diff] [blame] | 242 | ATRACE_CALL(); |
| 243 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 244 | if (slot >= mHal.info.exportedFunctionCount) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 245 | rsc->setError(RS_ERROR_BAD_SCRIPT, "The invokable index is out of bounds"); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 246 | return; |
| 247 | } |
Jason Sams | 4efe3d3 | 2015-03-18 18:28:06 -0700 | [diff] [blame] | 248 | if (mRSC->hadFatalError()) return; |
| 249 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 250 | setupScript(rsc); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 251 | |
Jason Sams | b9077f4 | 2010-09-22 15:57:41 -0700 | [diff] [blame] | 252 | if (rsc->props.mLogScripts) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 253 | ALOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, this); |
Jason Sams | b9077f4 | 2010-09-22 15:57:41 -0700 | [diff] [blame] | 254 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 255 | rsc->mHal.funcs.script.invokeFunction(rsc, this, slot, data, len); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Stephen Hines | f6af3bd | 2014-12-23 17:47:43 -0800 | [diff] [blame] | 258 | static const bool kDebugBitcode = false; |
| 259 | |
| 260 | #ifndef RS_COMPATIBILITY_LIB |
Stephen Hines | f6af3bd | 2014-12-23 17:47:43 -0800 | [diff] [blame] | 261 | |
| 262 | static bool dumpBitcodeFile(const char *cacheDir, const char *resName, |
| 263 | const char *suffix, const uint8_t *bitcode, |
| 264 | size_t bitcodeLen) { |
| 265 | std::string f(cacheDir); |
| 266 | f.append("/"); |
| 267 | f.append(resName); |
| 268 | f.append("#"); |
| 269 | f.append(suffix); |
| 270 | f.append(".bc"); |
| 271 | |
| 272 | if (!ScriptC::createCacheDir(cacheDir)) { |
| 273 | return false; |
| 274 | } |
| 275 | |
Nick Kralevich | 95e1702 | 2018-12-11 14:35:57 -0800 | [diff] [blame] | 276 | FILE *fp = fopen(f.c_str(), "we"); |
Stephen Hines | f6af3bd | 2014-12-23 17:47:43 -0800 | [diff] [blame] | 277 | if (!fp) { |
| 278 | ALOGE("Could not open %s", f.c_str()); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | size_t nWritten = fwrite(bitcode, 1, bitcodeLen, fp); |
| 283 | fclose(fp); |
| 284 | if (nWritten != bitcodeLen) { |
| 285 | ALOGE("Could not write %s", f.c_str()); |
| 286 | return false; |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | |
Stephen Hines | f6af3bd | 2014-12-23 17:47:43 -0800 | [diff] [blame] | 291 | #endif // !RS_COMPATIBILITY_LIB |
| 292 | |
| 293 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 294 | bool ScriptC::runCompiler(Context *rsc, |
| 295 | const char *resName, |
| 296 | const char *cacheDir, |
| 297 | const uint8_t *bitcode, |
| 298 | size_t bitcodeLen) { |
Tim Murray | fa85e91 | 2013-05-23 13:19:36 -0700 | [diff] [blame] | 299 | ATRACE_CALL(); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 300 | //ALOGE("runCompiler %p %p %p %p %p %i", rsc, this, resName, cacheDir, bitcode, bitcodeLen); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 301 | #ifndef RS_COMPATIBILITY_LIB |
Stephen Hines | f8d4469 | 2011-11-22 19:43:58 -0800 | [diff] [blame] | 302 | uint32_t sdkVersion = 0; |
| 303 | bcinfo::BitcodeWrapper bcWrapper((const char *)bitcode, bitcodeLen); |
| 304 | if (!bcWrapper.unwrap()) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 305 | ALOGE("Bitcode is not in proper container format (raw or wrapper)"); |
Stephen Hines | f8d4469 | 2011-11-22 19:43:58 -0800 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |
Stephen Hines | f8d4469 | 2011-11-22 19:43:58 -0800 | [diff] [blame] | 309 | if (bcWrapper.getBCFileType() == bcinfo::BC_WRAPPER) { |
| 310 | sdkVersion = bcWrapper.getTargetAPI(); |
| 311 | } |
| 312 | |
| 313 | if (sdkVersion == 0) { |
| 314 | // This signals that we didn't have a wrapper containing information |
| 315 | // about the bitcode. |
| 316 | sdkVersion = rsc->getTargetSdkVersion(); |
| 317 | } |
| 318 | |
Stephen Hines | 5d95a78 | 2015-04-13 20:11:48 -0700 | [diff] [blame] | 319 | // Save off the sdkVersion, so that we can handle broken cases later. |
| 320 | // Bug 19734267 |
| 321 | mApiLevel = sdkVersion; |
| 322 | |
Stephen Hines | 98f401d | 2015-12-03 00:44:46 -0800 | [diff] [blame] | 323 | bcinfo::BitcodeTranslator BT((const char *)bitcode, bitcodeLen, |
| 324 | sdkVersion); |
| 325 | if (!BT.translate()) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 326 | ALOGE("Failed to translate bitcode from version: %u", sdkVersion); |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 327 | return false; |
| 328 | } |
Stephen Hines | 98f401d | 2015-12-03 00:44:46 -0800 | [diff] [blame] | 329 | bitcode = (const uint8_t *) BT.getTranslatedBitcode(); |
| 330 | bitcodeLen = BT.getTranslatedBitcodeSize(); |
Shih-wei Liao | 37150de | 2011-01-07 18:17:07 -0800 | [diff] [blame] | 331 | |
Stephen Hines | f6af3bd | 2014-12-23 17:47:43 -0800 | [diff] [blame] | 332 | if (kDebugBitcode) { |
| 333 | if (!dumpBitcodeFile(cacheDir, resName, "after", bitcode, bitcodeLen)) { |
| 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | |
Stephen McGroarty | 15c1d06 | 2015-09-02 16:03:38 +0100 | [diff] [blame] | 338 | |
| 339 | // Set the optimization level of bcc to be the same as the |
| 340 | // optimization level used to compile the bitcode. |
| 341 | rsc->setOptLevel(bcWrapper.getOptimizationLevel()); |
| 342 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 343 | if (!cacheDir) { |
| 344 | // MUST BE FIXED BEFORE ANYTHING USING C++ API IS RELEASED |
| 345 | cacheDir = getenv("EXTERNAL_STORAGE"); |
| 346 | ALOGV("Cache dir changed to %s", cacheDir); |
| 347 | } |
| 348 | |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 349 | // ensure that cache dir exists |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 350 | if (cacheDir && !createCacheDir(cacheDir)) { |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 351 | return false; |
| 352 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 353 | #endif |
Tim Murray | e78c14b | 2012-10-01 15:27:18 -0700 | [diff] [blame] | 354 | |
Jason Sams | 77020c5 | 2011-11-22 12:49:11 -0800 | [diff] [blame] | 355 | if (!rsc->mHal.funcs.script.init(rsc, this, resName, cacheDir, bitcode, bitcodeLen, 0)) { |
| 356 | return false; |
| 357 | } |
Shih-wei Liao | 37150de | 2011-01-07 18:17:07 -0800 | [diff] [blame] | 358 | |
Jason Sams | 77020c5 | 2011-11-22 12:49:11 -0800 | [diff] [blame] | 359 | mInitialized = true; |
Miao Wang | 59f6142 | 2017-03-14 14:23:52 -0700 | [diff] [blame] | 360 | #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB) |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 361 | mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); |
| 362 | mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); |
| 363 | mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore()); |
| 364 | mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 365 | #endif |
Shih-wei Liao | 37150de | 2011-01-07 18:17:07 -0800 | [diff] [blame] | 366 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 367 | rsc->mHal.funcs.script.invokeInit(rsc, this); |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 368 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 369 | for (size_t i=0; i < mHal.info.exportedPragmaCount; ++i) { |
| 370 | const char * key = mHal.info.exportedPragmaKeyList[i]; |
| 371 | const char * value = mHal.info.exportedPragmaValueList[i]; |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 372 | //ALOGE("pragma %s %s", keys[i], values[i]); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 373 | if (!strcmp(key, "version")) { |
| 374 | if (!strcmp(value, "1")) { |
Stephen Hines | b5dc6af | 2011-01-18 14:10:44 -0800 | [diff] [blame] | 375 | continue; |
| 376 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 377 | ALOGE("Invalid version pragma value: %s\n", value); |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 378 | return false; |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Miao Wang | 59f6142 | 2017-03-14 14:23:52 -0700 | [diff] [blame] | 381 | #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB) |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 382 | if (!strcmp(key, "stateVertex")) { |
| 383 | if (!strcmp(value, "default")) { |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 384 | continue; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 385 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 386 | if (!strcmp(value, "parent")) { |
| 387 | mEnviroment.mVertex.clear(); |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 388 | continue; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 389 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 390 | ALOGE("Unrecognized value %s passed to stateVertex", value); |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 391 | return false; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 392 | } |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 393 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 394 | if (!strcmp(key, "stateRaster")) { |
| 395 | if (!strcmp(value, "default")) { |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 396 | continue; |
| 397 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 398 | if (!strcmp(value, "parent")) { |
| 399 | mEnviroment.mRaster.clear(); |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 400 | continue; |
| 401 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 402 | ALOGE("Unrecognized value %s passed to stateRaster", value); |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 403 | return false; |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 406 | if (!strcmp(key, "stateFragment")) { |
| 407 | if (!strcmp(value, "default")) { |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 408 | continue; |
| 409 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 410 | if (!strcmp(value, "parent")) { |
| 411 | mEnviroment.mFragment.clear(); |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 412 | continue; |
| 413 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 414 | ALOGE("Unrecognized value %s passed to stateFragment", value); |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 415 | return false; |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 418 | if (!strcmp(key, "stateStore")) { |
| 419 | if (!strcmp(value, "default")) { |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 420 | continue; |
| 421 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 422 | if (!strcmp(value, "parent")) { |
| 423 | mEnviroment.mFragmentStore.clear(); |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 424 | continue; |
| 425 | } |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 426 | ALOGE("Unrecognized value %s passed to stateStore", value); |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 427 | return false; |
Stephen Hines | 7b337b1 | 2011-01-17 17:31:58 -0800 | [diff] [blame] | 428 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 429 | #endif |
| 430 | |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 431 | } |
Jason Sams | 2e8665d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 432 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 433 | mSlots = new ObjectBaseRef<Allocation>[mHal.info.exportedVariableCount]; |
| 434 | mTypes = new ObjectBaseRef<const Type>[mHal.info.exportedVariableCount]; |
Jason Sams | 2e8665d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 435 | |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 436 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | namespace android { |
| 440 | namespace renderscript { |
| 441 | |
Shih-wei Liao | ce8a079 | 2010-12-20 20:45:56 +0800 | [diff] [blame] | 442 | RsScript rsi_ScriptCCreate(Context *rsc, |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 443 | const char *resName, size_t resName_length, |
| 444 | const char *cacheDir, size_t cacheDir_length, |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 445 | const char *text, size_t text_length) |
Shih-wei Liao | 9503b66 | 2010-11-08 01:33:59 -0800 | [diff] [blame] | 446 | { |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 447 | ScriptC *s = new ScriptC(rsc); |
Jason Sams | 1f52633 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 448 | |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 449 | if (!s->runCompiler(rsc, resName, cacheDir, (uint8_t *)text, text_length)) { |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 450 | // Error during compile, destroy s and return null. |
Stephen Hines | 4769d68 | 2012-02-02 13:23:20 -0800 | [diff] [blame] | 451 | ObjectBase::checkDelete(s); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 452 | return nullptr; |
Jason Sams | 26b2c9f | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 453 | } |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 454 | |
| 455 | s->incUserRef(); |
Jason Sams | 249d453 | 2011-01-23 17:48:45 -0800 | [diff] [blame] | 456 | return s; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Rahul Chaudhry | 7974fc0 | 2017-02-09 12:33:28 -0800 | [diff] [blame] | 459 | } // namespace renderscript |
| 460 | } // namespace android |