Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 |
| 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" |
| 19 | #include "rsMatrix.h" |
Anatol Pomazau | c24ffe6 | 2010-09-07 17:33:01 -0700 | [diff] [blame] | 20 | #include "../../compile/libbcc/include/bcc/bcc.h" |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 21 | #include "utils/Timers.h" |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 22 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 23 | #include <GLES/gl.h> |
| 24 | #include <GLES/glext.h> |
| 25 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 29 | #define GET_TLS() Context::ScriptTLSStruct * tls = \ |
| 30 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ |
| 31 | Context * rsc = tls->mContext; \ |
| 32 | ScriptC * sc = (ScriptC *) tls->mScript |
| 33 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 35 | ScriptC::ScriptC(Context *rsc) : Script(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 36 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 37 | mAllocFile = __FILE__; |
| 38 | mAllocLine = __LINE__; |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 39 | mBccScript = NULL; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 40 | memset(&mProgram, 0, sizeof(mProgram)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ScriptC::~ScriptC() |
| 44 | { |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 45 | if (mBccScript) { |
| 46 | bccDeleteScript(mBccScript); |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 47 | } |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 48 | free(mEnviroment.mScriptText); |
| 49 | mEnviroment.mScriptText = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 52 | void ScriptC::setupScript(Context *rsc) |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 53 | { |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 54 | setupGLState(rsc); |
| 55 | mEnviroment.mStartTimeMillis |
| 56 | = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 57 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 58 | for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 59 | if (mSlots[ct].get() && !mTypes[ct].get()) { |
| 60 | mTypes[ct].set(mSlots[ct]->getType()); |
| 61 | } |
| 62 | |
| 63 | if (!mTypes[ct].get()) |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 64 | continue; |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 65 | void *ptr = NULL; |
| 66 | if (mSlots[ct].get()) { |
| 67 | ptr = mSlots[ct]->getPtr(); |
| 68 | } |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 69 | void **dest = ((void ***)mEnviroment.mFieldAddress)[ct]; |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 70 | |
Jason Sams | f119b91 | 2010-09-22 15:57:41 -0700 | [diff] [blame^] | 71 | if (rsc->props.mLogScripts) { |
| 72 | LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType()); |
| 73 | |
| 74 | //const uint32_t *p32 = (const uint32_t *)ptr; |
| 75 | //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) { |
| 76 | //LOGE(" %i = 0x%08x ", ct2, p32[ct2]); |
| 77 | //} |
| 78 | } |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 79 | |
| 80 | if (dest) { |
| 81 | *dest = ptr; |
| 82 | } else { |
| 83 | LOGE("ScriptC::setupScript, NULL var binding address."); |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Jason Sams | 1de0b87 | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 88 | const Allocation *ScriptC::ptrToAllocation(const void *ptr) const |
| 89 | { |
| 90 | if (!ptr) { |
| 91 | return NULL; |
| 92 | } |
| 93 | for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { |
| 94 | if (!mSlots[ct].get()) |
| 95 | continue; |
| 96 | if (mSlots[ct]->getPtr() == ptr) { |
| 97 | return mSlots[ct].get(); |
| 98 | } |
| 99 | } |
| 100 | LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 104 | Script * ScriptC::setTLS(Script *sc) |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 105 | { |
| 106 | Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *) |
| 107 | pthread_getspecific(Context::gThreadTLSKey); |
| 108 | rsAssert(tls); |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 109 | Script *old = tls->mScript; |
| 110 | tls->mScript = sc; |
| 111 | return old; |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 114 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 115 | void ScriptC::setupGLState(Context *rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 116 | { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 117 | if (mEnviroment.mFragmentStore.get()) { |
| 118 | rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); |
| 119 | } |
| 120 | if (mEnviroment.mFragment.get()) { |
| 121 | rsc->setFragment(mEnviroment.mFragment.get()); |
| 122 | } |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 123 | if (mEnviroment.mVertex.get()) { |
| 124 | rsc->setVertex(mEnviroment.mVertex.get()); |
| 125 | } |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 126 | if (mEnviroment.mRaster.get()) { |
| 127 | rsc->setRaster(mEnviroment.mRaster.get()); |
| 128 | } |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 129 | } |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 130 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 131 | uint32_t ScriptC::run(Context *rsc) |
| 132 | { |
| 133 | if (mProgram.mRoot == NULL) { |
| 134 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); |
| 135 | return 0; |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 136 | } |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 137 | |
| 138 | setupScript(rsc); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 139 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 140 | uint32_t ret = 0; |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 141 | Script * oldTLS = setTLS(this); |
Jason Sams | f119b91 | 2010-09-22 15:57:41 -0700 | [diff] [blame^] | 142 | |
| 143 | if (rsc->props.mLogScripts) { |
| 144 | LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot); |
| 145 | } |
| 146 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 147 | ret = mProgram.mRoot(); |
Jason Sams | f119b91 | 2010-09-22 15:57:41 -0700 | [diff] [blame^] | 148 | |
| 149 | if (rsc->props.mLogScripts) { |
| 150 | LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret); |
| 151 | } |
| 152 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 153 | setTLS(oldTLS); |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 154 | return ret; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 157 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 158 | typedef struct { |
| 159 | Context *rsc; |
| 160 | ScriptC *script; |
| 161 | const Allocation * ain; |
| 162 | Allocation * aout; |
| 163 | const void * usr; |
| 164 | |
| 165 | uint32_t mSliceSize; |
| 166 | volatile int mSliceNum; |
| 167 | |
| 168 | const uint8_t *ptrIn; |
| 169 | uint32_t eStrideIn; |
| 170 | uint8_t *ptrOut; |
| 171 | uint32_t eStrideOut; |
| 172 | |
| 173 | uint32_t xStart; |
| 174 | uint32_t xEnd; |
| 175 | uint32_t yStart; |
| 176 | uint32_t yEnd; |
| 177 | uint32_t zStart; |
| 178 | uint32_t zEnd; |
| 179 | uint32_t arrayStart; |
| 180 | uint32_t arrayEnd; |
| 181 | |
| 182 | uint32_t dimX; |
| 183 | uint32_t dimY; |
| 184 | uint32_t dimZ; |
| 185 | uint32_t dimArray; |
| 186 | } MTLaunchStruct; |
| 187 | typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); |
| 188 | |
| 189 | static void wc_xy(void *usr, uint32_t idx) |
| 190 | { |
| 191 | MTLaunchStruct *mtls = (MTLaunchStruct *)usr; |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 192 | |
| 193 | while (1) { |
| 194 | uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); |
| 195 | uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize; |
| 196 | uint32_t yEnd = yStart + mtls->mSliceSize; |
| 197 | yEnd = rsMin(yEnd, mtls->yEnd); |
| 198 | if (yEnd <= yStart) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd); |
| 203 | |
| 204 | for (uint32_t y = yStart; y < yEnd; y++) { |
| 205 | uint32_t offset = mtls->dimX * y; |
| 206 | uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset); |
| 207 | const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset); |
| 208 | |
| 209 | for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) { |
| 210 | ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0); |
| 211 | xPtrIn += mtls->eStrideIn; |
| 212 | xPtrOut += mtls->eStrideOut; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 219 | void ScriptC::runForEach(Context *rsc, |
| 220 | const Allocation * ain, |
| 221 | Allocation * aout, |
| 222 | const void * usr, |
| 223 | const RsScriptCall *sc) |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 224 | { |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 225 | MTLaunchStruct mtls; |
| 226 | memset(&mtls, 0, sizeof(mtls)); |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 227 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 228 | if (ain) { |
| 229 | mtls.dimX = ain->getType()->getDimX(); |
| 230 | mtls.dimY = ain->getType()->getDimY(); |
| 231 | mtls.dimZ = ain->getType()->getDimZ(); |
| 232 | //mtls.dimArray = ain->getType()->getDimArray(); |
| 233 | } else if (aout) { |
| 234 | mtls.dimX = aout->getType()->getDimX(); |
| 235 | mtls.dimY = aout->getType()->getDimY(); |
| 236 | mtls.dimZ = aout->getType()->getDimZ(); |
| 237 | //mtls.dimArray = aout->getType()->getDimArray(); |
| 238 | } else { |
| 239 | rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations"); |
| 240 | return; |
| 241 | } |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 242 | |
| 243 | if (!sc || (sc->xEnd == 0)) { |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 244 | mtls.xEnd = mtls.dimX; |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 245 | } else { |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 246 | rsAssert(sc->xStart < mtls.dimX); |
| 247 | rsAssert(sc->xEnd <= mtls.dimX); |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 248 | rsAssert(sc->xStart < sc->xEnd); |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 249 | mtls.xStart = rsMin(mtls.dimX, sc->xStart); |
| 250 | mtls.xEnd = rsMin(mtls.dimX, sc->xEnd); |
| 251 | if (mtls.xStart >= mtls.xEnd) return; |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if (!sc || (sc->yEnd == 0)) { |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 255 | mtls.yEnd = mtls.dimY; |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 256 | } else { |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 257 | rsAssert(sc->yStart < mtls.dimY); |
| 258 | rsAssert(sc->yEnd <= mtls.dimY); |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 259 | rsAssert(sc->yStart < sc->yEnd); |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 260 | mtls.yStart = rsMin(mtls.dimY, sc->yStart); |
| 261 | mtls.yEnd = rsMin(mtls.dimY, sc->yEnd); |
| 262 | if (mtls.yStart >= mtls.yEnd) return; |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 265 | mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd); |
| 266 | mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd); |
| 267 | mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd); |
| 268 | mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd); |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 269 | |
| 270 | rsAssert(ain->getType()->getDimZ() == 0); |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 271 | |
| 272 | setupScript(rsc); |
| 273 | Script * oldTLS = setTLS(this); |
| 274 | |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 275 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 276 | mtls.rsc = rsc; |
| 277 | mtls.ain = ain; |
| 278 | mtls.aout = aout; |
| 279 | mtls.script = this; |
| 280 | mtls.usr = usr; |
| 281 | mtls.mSliceSize = 10; |
| 282 | mtls.mSliceNum = 0; |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 283 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 284 | mtls.ptrIn = NULL; |
| 285 | mtls.eStrideIn = 0; |
| 286 | if (ain) { |
| 287 | mtls.ptrIn = (const uint8_t *)ain->getPtr(); |
| 288 | mtls.eStrideIn = ain->getType()->getElementSizeBytes(); |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 291 | mtls.ptrOut = NULL; |
| 292 | mtls.eStrideOut = 0; |
| 293 | if (aout) { |
| 294 | mtls.ptrOut = (uint8_t *)aout->getPtr(); |
| 295 | mtls.eStrideOut = aout->getType()->getElementSizeBytes(); |
| 296 | } |
| 297 | |
| 298 | |
Jason Sams | 2cbd298 | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 299 | if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable && |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 300 | ((mtls.dimY * mtls.dimZ * mtls.dimArray) > 1)) { |
| 301 | |
| 302 | //LOGE("launch 1"); |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 303 | rsc->launchThreads(wc_xy, &mtls); |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 304 | //LOGE("launch 2"); |
| 305 | } else { |
| 306 | for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) { |
| 307 | for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) { |
| 308 | for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) { |
| 309 | uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar + |
| 310 | mtls.dimX * mtls.dimY * z + |
| 311 | mtls.dimX * y; |
| 312 | uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset); |
| 313 | const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset); |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 314 | |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 315 | for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) { |
| 316 | ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar); |
| 317 | xPtrIn += mtls.eStrideIn; |
| 318 | xPtrOut += mtls.eStrideOut; |
| 319 | } |
Jason Sams | 8f8a572 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 323 | } |
Jason Sams | c7f4e41 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 324 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 325 | setTLS(oldTLS); |
| 326 | } |
| 327 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 328 | void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) |
| 329 | { |
| 330 | //LOGE("rsi_ScriptInvoke %i", slot); |
| 331 | if ((slot >= mEnviroment.mInvokeFunctionCount) || |
| 332 | (mEnviroment.mInvokeFunctions[slot] == NULL)) { |
| 333 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); |
| 334 | return; |
| 335 | } |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 336 | setupScript(rsc); |
| 337 | Script * oldTLS = setTLS(this); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 338 | |
Jason Sams | f119b91 | 2010-09-22 15:57:41 -0700 | [diff] [blame^] | 339 | if (rsc->props.mLogScripts) { |
| 340 | LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]); |
| 341 | } |
Jason Sams | e29f3e7 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 342 | ((void (*)(const void *, uint32_t)) |
| 343 | mEnviroment.mInvokeFunctions[slot])(data, len); |
Jason Sams | f119b91 | 2010-09-22 15:57:41 -0700 | [diff] [blame^] | 344 | if (rsc->props.mLogScripts) { |
| 345 | LOGV("%p ScriptC::Invoke complete", rsc); |
| 346 | } |
Jason Sams | e29f3e7 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 347 | |
Jason Sams | f17bccc | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 348 | setTLS(oldTLS); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 351 | ScriptCState::ScriptCState() |
| 352 | { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 353 | mScript = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 354 | clear(); |
| 355 | } |
| 356 | |
| 357 | ScriptCState::~ScriptCState() |
| 358 | { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 359 | delete mScript; |
| 360 | mScript = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void ScriptCState::clear() |
| 364 | { |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 365 | for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 366 | mConstantBufferTypes[ct].clear(); |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 367 | mSlotWritable[ct] = false; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 368 | } |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 369 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 370 | delete mScript; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 371 | mScript = new ScriptC(NULL); |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 374 | static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 375 | { |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 376 | const ScriptCState::SymbolTable_t *sym; |
Jason Sams | 2cbd298 | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 377 | ScriptC *s = (ScriptC *)pContext; |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 378 | sym = ScriptCState::lookupSymbol(name); |
| 379 | if (sym) { |
| 380 | return sym->mPtr; |
| 381 | } |
Jason Sams | 2cbd298 | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 382 | s->mEnviroment.mIsThreadable = false; |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 383 | sym = ScriptCState::lookupSymbolCL(name); |
| 384 | if (sym) { |
| 385 | return sym->mPtr; |
| 386 | } |
| 387 | sym = ScriptCState::lookupSymbolGL(name); |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 388 | if (sym) { |
| 389 | return sym->mPtr; |
| 390 | } |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 391 | LOGE("ScriptC sym lookup failed for %s", name); |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 392 | return NULL; |
| 393 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 394 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 395 | void ScriptCState::runCompiler(Context *rsc, ScriptC *s) |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 396 | { |
Jason Sams | f119b91 | 2010-09-22 15:57:41 -0700 | [diff] [blame^] | 397 | LOGV("%p ScriptCState::runCompiler ", rsc); |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 398 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 399 | s->mBccScript = bccCreateScript(); |
Jason Sams | 2cbd298 | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 400 | s->mEnviroment.mIsThreadable = true; |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 401 | bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength); |
Jason Sams | 2cbd298 | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 402 | bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 403 | bccCompileScript(s->mBccScript); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 404 | bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot); |
| 405 | bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit); |
Jason Sams | f119b91 | 2010-09-22 15:57:41 -0700 | [diff] [blame^] | 406 | LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit); |
Jason Sams | 764205c | 2009-07-16 17:47:40 -0700 | [diff] [blame] | 407 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 408 | if (s->mProgram.mInit) { |
| 409 | s->mProgram.mInit(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 412 | bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL); |
| 413 | if(s->mEnviroment.mInvokeFunctionCount <= 0) |
| 414 | s->mEnviroment.mInvokeFunctions = NULL; |
| 415 | else { |
| 416 | s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t)); |
| 417 | bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Shih-wei Liao | 0787646 | 2010-07-20 16:43:25 -0700 | [diff] [blame] | 420 | bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL); |
| 421 | if(s->mEnviroment.mFieldCount <= 0) |
| 422 | s->mEnviroment.mFieldAddress = NULL; |
| 423 | else { |
| 424 | s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *)); |
| 425 | bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress); |
| 426 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 427 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 428 | s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); |
| 429 | s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 430 | s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore()); |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 431 | s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 432 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 433 | if (s->mProgram.mRoot) { |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 434 | const static int pragmaMax = 16; |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 435 | BCCsizei pragmaCount; |
| 436 | BCCchar * str[pragmaMax]; |
| 437 | bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 438 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 439 | for (int ct=0; ct < pragmaCount; ct+=2) { |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 440 | //LOGE("pragme %s %s", str[ct], str[ct+1]); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 441 | if (!strcmp(str[ct], "version")) { |
| 442 | continue; |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 445 | if (!strcmp(str[ct], "stateVertex")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 446 | if (!strcmp(str[ct+1], "default")) { |
| 447 | continue; |
| 448 | } |
| 449 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 450 | s->mEnviroment.mVertex.clear(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 451 | continue; |
| 452 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 453 | LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); |
| 454 | } |
| 455 | |
| 456 | if (!strcmp(str[ct], "stateRaster")) { |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 457 | if (!strcmp(str[ct+1], "default")) { |
| 458 | continue; |
| 459 | } |
| 460 | if (!strcmp(str[ct+1], "parent")) { |
| 461 | s->mEnviroment.mRaster.clear(); |
| 462 | continue; |
| 463 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 464 | LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); |
| 465 | } |
| 466 | |
| 467 | if (!strcmp(str[ct], "stateFragment")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 468 | if (!strcmp(str[ct+1], "default")) { |
| 469 | continue; |
| 470 | } |
| 471 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 472 | s->mEnviroment.mFragment.clear(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 473 | continue; |
| 474 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 475 | LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); |
| 476 | } |
| 477 | |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 478 | if (!strcmp(str[ct], "stateStore")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 479 | if (!strcmp(str[ct+1], "default")) { |
| 480 | continue; |
| 481 | } |
| 482 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 483 | s->mEnviroment.mFragmentStore.clear(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 484 | continue; |
| 485 | } |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 486 | LOGE("Unreconized value %s passed to stateStore", str[ct+1]); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | } |
| 490 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 491 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 492 | } else { |
| 493 | // Deal with an error. |
| 494 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 497 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 498 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 499 | namespace android { |
| 500 | namespace renderscript { |
| 501 | |
| 502 | void rsi_ScriptCBegin(Context * rsc) |
| 503 | { |
| 504 | ScriptCState *ss = &rsc->mScriptC; |
| 505 | ss->clear(); |
| 506 | } |
| 507 | |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 508 | void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) |
| 509 | { |
| 510 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 511 | |
| 512 | char *t = (char *)malloc(len + 1); |
| 513 | memcpy(t, text, len); |
| 514 | t[len] = 0; |
| 515 | ss->mScript->mEnviroment.mScriptText = t; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 516 | ss->mScript->mEnviroment.mScriptTextLength = len; |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 520 | RsScript rsi_ScriptCCreate(Context * rsc) |
| 521 | { |
| 522 | ScriptCState *ss = &rsc->mScriptC; |
| 523 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 524 | ScriptC *s = ss->mScript; |
| 525 | ss->mScript = NULL; |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 526 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 527 | ss->runCompiler(rsc, s); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 528 | s->incUserRef(); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 529 | s->setContext(rsc); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 530 | for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 531 | s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get()); |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 532 | s->mSlotWritable[ct] = ss->mSlotWritable[ct]; |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 533 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 534 | |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 535 | ss->clear(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 536 | return s; |
| 537 | } |
| 538 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | |