blob: 5197eeb2216f5bc96258c9b83e6edc5dc98d7104 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 Pomazaue7b4b862010-09-07 17:33:01 -070020#include "../../compile/libbcc/include/bcc/bcc.h"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070021#include "utils/Timers.h"
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -070022#include "utils/StopWatch.h"
Shih-wei Liaoce8a0792010-12-20 20:45:56 +080023extern "C" {
24#include "libdex/ZipArchive.h"
25}
Jack Palevich1ef8b802009-05-28 15:53:04 -070026
Jason Sams1aa5a4e2009-06-22 17:15:15 -070027#include <GLES/gl.h>
28#include <GLES/glext.h>
29
Jason Sams326e0dd2009-05-22 14:03:28 -070030using namespace android;
31using namespace android::renderscript;
32
Jason Samse5769102009-06-19 16:03:18 -070033#define GET_TLS() Context::ScriptTLSStruct * tls = \
34 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
35 Context * rsc = tls->mContext; \
36 ScriptC * sc = (ScriptC *) tls->mScript
37
Jason Sams326e0dd2009-05-22 14:03:28 -070038
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080039ScriptC::ScriptC(Context *rsc) : Script(rsc) {
Jason Samsbe36bf32010-05-11 14:03:58 -070040 mBccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -070041 memset(&mProgram, 0, sizeof(mProgram));
Jason Sams326e0dd2009-05-22 14:03:28 -070042}
43
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044ScriptC::~ScriptC() {
Jason Samsbe36bf32010-05-11 14:03:58 -070045 if (mBccScript) {
46 bccDeleteScript(mBccScript);
Jack Palevich1ef8b802009-05-28 15:53:04 -070047 }
Jason Samse402ed32009-11-03 11:25:42 -080048 free(mEnviroment.mScriptText);
49 mEnviroment.mScriptText = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070050}
51
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080052void ScriptC::setupScript(Context *rsc) {
Jason Samsc61346b2010-05-28 18:23:22 -070053 mEnviroment.mStartTimeMillis
54 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
55
Jason Samsbe36bf32010-05-11 14:03:58 -070056 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
Jason Sams900f1612010-09-16 18:18:29 -070057 if (mSlots[ct].get() && !mTypes[ct].get()) {
58 mTypes[ct].set(mSlots[ct]->getType());
59 }
60
61 if (!mTypes[ct].get())
Jason Samsbe36bf32010-05-11 14:03:58 -070062 continue;
Jason Sams900f1612010-09-16 18:18:29 -070063 void *ptr = NULL;
64 if (mSlots[ct].get()) {
65 ptr = mSlots[ct]->getPtr();
66 }
Jason Samsbe36bf32010-05-11 14:03:58 -070067 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
Jason Samsbe36bf32010-05-11 14:03:58 -070068
Jason Samsb9077f42010-09-22 15:57:41 -070069 if (rsc->props.mLogScripts) {
Jason Sams2fad7e42010-11-16 12:19:26 -080070 if (mSlots[ct].get() != NULL) {
71 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
72 } else {
73 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=null", rsc, ct, dest, ptr);
74 }
Jason Samsb9077f42010-09-22 15:57:41 -070075 }
Jason Samsbe36bf32010-05-11 14:03:58 -070076
77 if (dest) {
78 *dest = ptr;
Jason Samsada7f272009-09-24 14:55:38 -070079 }
80 }
81}
82
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080083const Allocation *ScriptC::ptrToAllocation(const void *ptr) const {
Jason Samsce92d4b2010-05-17 14:55:34 -070084 if (!ptr) {
85 return NULL;
86 }
87 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
88 if (!mSlots[ct].get())
89 continue;
90 if (mSlots[ct]->getPtr() == ptr) {
91 return mSlots[ct].get();
92 }
93 }
94 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
95 return NULL;
96}
97
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080098Script * ScriptC::setTLS(Script *sc) {
Jason Sams22fa3712010-05-19 17:22:57 -070099 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
100 pthread_getspecific(Context::gThreadTLSKey);
101 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700102 Script *old = tls->mScript;
103 tls->mScript = sc;
104 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700105}
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107void ScriptC::setupGLState(Context *rsc) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700108 if (mEnviroment.mFragmentStore.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800109 rsc->setProgramStore(mEnviroment.mFragmentStore.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700110 }
111 if (mEnviroment.mFragment.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800112 rsc->setProgramFragment(mEnviroment.mFragment.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700113 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700114 if (mEnviroment.mVertex.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800115 rsc->setProgramVertex(mEnviroment.mVertex.get());
Jason Sams8ce125b2009-06-17 16:52:59 -0700116 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700117 if (mEnviroment.mRaster.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800118 rsc->setProgramRaster(mEnviroment.mRaster.get());
Jason Samsb681c8a2009-09-28 18:12:56 -0700119 }
Jason Samsc61346b2010-05-28 18:23:22 -0700120}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700121
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800122uint32_t ScriptC::run(Context *rsc) {
Jason Samsc61346b2010-05-28 18:23:22 -0700123 if (mProgram.mRoot == NULL) {
124 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
125 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700126 }
Jason Samsc61346b2010-05-28 18:23:22 -0700127
Jason Sams1f24db42010-12-11 17:42:30 -0800128 setupGLState(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700129 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700130
Jason Sams2dca84d2009-12-09 11:05:45 -0800131 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700132 Script * oldTLS = setTLS(this);
Jason Samsb9077f42010-09-22 15:57:41 -0700133
134 if (rsc->props.mLogScripts) {
135 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
136 }
137
Jason Samsbe36bf32010-05-11 14:03:58 -0700138 ret = mProgram.mRoot();
Jason Samsb9077f42010-09-22 15:57:41 -0700139
140 if (rsc->props.mLogScripts) {
141 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
142 }
143
Jason Samsc61346b2010-05-28 18:23:22 -0700144 setTLS(oldTLS);
Jason Samse45ac6e2009-07-20 14:31:06 -0700145 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700146}
147
Jason Sams7bf29dd2010-07-19 15:38:19 -0700148typedef struct {
149 Context *rsc;
150 ScriptC *script;
151 const Allocation * ain;
152 Allocation * aout;
153 const void * usr;
154
155 uint32_t mSliceSize;
156 volatile int mSliceNum;
157
158 const uint8_t *ptrIn;
159 uint32_t eStrideIn;
160 uint8_t *ptrOut;
161 uint32_t eStrideOut;
162
163 uint32_t xStart;
164 uint32_t xEnd;
165 uint32_t yStart;
166 uint32_t yEnd;
167 uint32_t zStart;
168 uint32_t zEnd;
169 uint32_t arrayStart;
170 uint32_t arrayEnd;
171
172 uint32_t dimX;
173 uint32_t dimY;
174 uint32_t dimZ;
175 uint32_t dimArray;
176} MTLaunchStruct;
177typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
178
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800179static void wc_xy(void *usr, uint32_t idx) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700180 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700181
182 while (1) {
183 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
184 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
185 uint32_t yEnd = yStart + mtls->mSliceSize;
186 yEnd = rsMin(yEnd, mtls->yEnd);
187 if (yEnd <= yStart) {
188 return;
189 }
190
191 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
Jason Sams8d957fa2010-09-28 14:41:22 -0700192 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700193 for (uint32_t y = yStart; y < yEnd; y++) {
194 uint32_t offset = mtls->dimX * y;
195 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
196 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
197
198 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
199 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
200 xPtrIn += mtls->eStrideIn;
201 xPtrOut += mtls->eStrideOut;
202 }
203 }
204 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700205}
206
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800207static void wc_x(void *usr, uint32_t idx) {
Jason Sams177f8442010-10-29 10:19:21 -0700208 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
209
210 while (1) {
211 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
212 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
213 uint32_t xEnd = xStart + mtls->mSliceSize;
214 xEnd = rsMin(xEnd, mtls->xEnd);
215 if (xEnd <= xStart) {
216 return;
217 }
218
219 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
220 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
221 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
222 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
223 for (uint32_t x = xStart; x < xEnd; x++) {
224 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
225 xPtrIn += mtls->eStrideIn;
226 xPtrOut += mtls->eStrideOut;
227 }
228 }
Jason Sams177f8442010-10-29 10:19:21 -0700229}
230
Jason Samsace3e012010-07-15 17:11:13 -0700231void ScriptC::runForEach(Context *rsc,
232 const Allocation * ain,
233 Allocation * aout,
234 const void * usr,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800235 const RsScriptCall *sc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700236 MTLaunchStruct mtls;
237 memset(&mtls, 0, sizeof(mtls));
Jason Sams60709252010-11-17 15:29:32 -0800238 Context::PushState ps(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700239
Jason Sams7bf29dd2010-07-19 15:38:19 -0700240 if (ain) {
241 mtls.dimX = ain->getType()->getDimX();
242 mtls.dimY = ain->getType()->getDimY();
243 mtls.dimZ = ain->getType()->getDimZ();
244 //mtls.dimArray = ain->getType()->getDimArray();
245 } else if (aout) {
246 mtls.dimX = aout->getType()->getDimX();
247 mtls.dimY = aout->getType()->getDimY();
248 mtls.dimZ = aout->getType()->getDimZ();
249 //mtls.dimArray = aout->getType()->getDimArray();
250 } else {
251 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
252 return;
253 }
Jason Samsace3e012010-07-15 17:11:13 -0700254
255 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700256 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700257 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700258 rsAssert(sc->xStart < mtls.dimX);
259 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700260 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700261 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
262 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
263 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700264 }
265
266 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700267 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700268 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700269 rsAssert(sc->yStart < mtls.dimY);
270 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700271 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700272 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
273 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
274 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700275 }
276
Jason Sams7bf29dd2010-07-19 15:38:19 -0700277 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
278 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
279 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
280 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700281
282 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700283
Jason Sams1f24db42010-12-11 17:42:30 -0800284 setupGLState(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700285 setupScript(rsc);
286 Script * oldTLS = setTLS(this);
287
Jason Sams7bf29dd2010-07-19 15:38:19 -0700288 mtls.rsc = rsc;
289 mtls.ain = ain;
290 mtls.aout = aout;
291 mtls.script = this;
292 mtls.usr = usr;
293 mtls.mSliceSize = 10;
294 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700295
Jason Sams7bf29dd2010-07-19 15:38:19 -0700296 mtls.ptrIn = NULL;
297 mtls.eStrideIn = 0;
298 if (ain) {
299 mtls.ptrIn = (const uint8_t *)ain->getPtr();
300 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700301 }
302
Jason Sams7bf29dd2010-07-19 15:38:19 -0700303 mtls.ptrOut = NULL;
304 mtls.eStrideOut = 0;
305 if (aout) {
306 mtls.ptrOut = (uint8_t *)aout->getPtr();
307 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
308 }
309
Jason Sams177f8442010-10-29 10:19:21 -0700310 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) {
311 if (mtls.dimY > 1) {
312 rsc->launchThreads(wc_xy, &mtls);
313 } else {
314 rsc->launchThreads(wc_x, &mtls);
315 }
Jason Sams18133402010-07-20 15:09:00 -0700316
317 //LOGE("launch 1");
Jason Sams18133402010-07-20 15:09:00 -0700318 } else {
Jason Sams8d957fa2010-09-28 14:41:22 -0700319 //LOGE("launch 3");
Jason Sams18133402010-07-20 15:09:00 -0700320 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
321 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
322 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
323 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
324 mtls.dimX * mtls.dimY * z +
325 mtls.dimX * y;
326 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
327 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700328
Jason Sams18133402010-07-20 15:09:00 -0700329 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
330 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
331 xPtrIn += mtls.eStrideIn;
332 xPtrOut += mtls.eStrideOut;
333 }
Jason Samsace3e012010-07-15 17:11:13 -0700334 }
335 }
336 }
Jason Samsc61346b2010-05-28 18:23:22 -0700337 }
Jason Sams18133402010-07-20 15:09:00 -0700338
Jason Samsc61346b2010-05-28 18:23:22 -0700339 setTLS(oldTLS);
340}
341
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800342void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) {
Jason Sams22fa3712010-05-19 17:22:57 -0700343 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
344 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
345 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
346 return;
347 }
Jason Samsc61346b2010-05-28 18:23:22 -0700348 setupScript(rsc);
349 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700350
Jason Samsb9077f42010-09-22 15:57:41 -0700351 if (rsc->props.mLogScripts) {
352 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
353 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700354 ((void (*)(const void *, uint32_t))
355 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700356 if (rsc->props.mLogScripts) {
357 LOGV("%p ScriptC::Invoke complete", rsc);
358 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700359
Jason Samsc61346b2010-05-28 18:23:22 -0700360 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700361}
362
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800363ScriptCState::ScriptCState() {
Stephen Hines01b7d292010-09-28 15:45:45 -0700364 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700365}
366
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800367ScriptCState::~ScriptCState() {
Stephen Hines01b7d292010-09-28 15:45:45 -0700368 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700369}
370
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800371void ScriptCState::init(Context *rsc) {
Stephen Hines01b7d292010-09-28 15:45:45 -0700372 clear(rsc);
373}
374
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800375void ScriptCState::clear(Context *rsc) {
Stephen Hines01b7d292010-09-28 15:45:45 -0700376 rsAssert(rsc);
Stephen Hines01b7d292010-09-28 15:45:45 -0700377 mScript.clear();
378 mScript.set(new ScriptC(rsc));
Jason Sams1f526332009-06-05 17:35:09 -0700379}
380
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800381static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700382 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700383 ScriptC *s = (ScriptC *)pContext;
Shih-wei Liaof17b13b2010-12-07 13:44:10 -0800384 if (!strcmp(name, "__isThreadable")) {
385 return (BCCvoid*) s->mEnviroment.mIsThreadable;
386 } else if (!strcmp(name, "__clearThreadable")) {
387 s->mEnviroment.mIsThreadable = false;
388 return NULL;
389 }
Jason Samsaeb094b2010-05-18 13:35:45 -0700390 sym = ScriptCState::lookupSymbol(name);
Jason Sams6bfc1b92010-11-01 14:26:30 -0700391 if (!sym) {
392 sym = ScriptCState::lookupSymbolCL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700393 }
Jason Sams6bfc1b92010-11-01 14:26:30 -0700394 if (!sym) {
395 sym = ScriptCState::lookupSymbolGL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700396 }
Jason Sams29df66f2009-07-16 15:08:06 -0700397 if (sym) {
Jason Sams6bfc1b92010-11-01 14:26:30 -0700398 s->mEnviroment.mIsThreadable &= sym->threadable;
Jason Sams29df66f2009-07-16 15:08:06 -0700399 return sym->mPtr;
400 }
Jason Sams29df66f2009-07-16 15:08:06 -0700401 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700402 return NULL;
403}
Jason Samsa4a54e42009-06-10 18:39:40 -0700404
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700405extern const char rs_runtime_lib_bc[];
406extern unsigned rs_runtime_lib_bc_size;
407
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800408void ScriptCState::runCompiler(Context *rsc,
409 ScriptC *s,
410 long modWhen,
411 long crc32,
412 const char *resName,
413 const char *cacheDir) {
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700414 {
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700415 s->mBccScript = bccCreateScript();
416 s->mEnviroment.mIsThreadable = true;
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700417 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
Shih-wei Liao9503b662010-11-08 01:33:59 -0800418 // bccReadBC() reads in the BitCode, if no cache file corresponding to
419 // the resName is found. Otherwise, bccReadBC() returns a negative value
420 // and the "else" branch will be taken.
421 if (bccReadBC(s->mBccScript,
422 s->mEnviroment.mScriptText,
423 s->mEnviroment.mScriptTextLength,
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800424 modWhen,
425 crc32,
Shih-wei Liao15849d92010-12-10 01:03:59 -0800426 resName,
427 cacheDir) >= 0) {
Shih-wei Liao9503b662010-11-08 01:33:59 -0800428 //bccLinkBC(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
429 bccCompileBC(s->mBccScript);
430 } else {
431 // bccReadBC returns a neagative value: Didn't read any script,
432 // So, use cached binary instead
Shih-wei Liaoff8ce9c2010-12-16 04:58:35 -0800433 if (bccLoadBinary(s->mBccScript)) { // LoadBinary fails ==> Recompile
434 bccReadBC(s->mBccScript,
435 s->mEnviroment.mScriptText,
436 s->mEnviroment.mScriptTextLength,
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800437 modWhen,
438 crc32,
Shih-wei Liaoff8ce9c2010-12-16 04:58:35 -0800439 NULL,
440 cacheDir);
441 bccCompileBC(s->mBccScript);
442 }
Shih-wei Liao9503b662010-11-08 01:33:59 -0800443 }
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700444 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
445 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
446 }
Jason Samsb9077f42010-09-22 15:57:41 -0700447 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700448
Jason Sams8c6bc692009-09-16 15:04:38 -0700449 if (s->mProgram.mInit) {
450 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700451 }
452
Jason Sams8c880902010-06-15 12:15:57 -0700453 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800454 if (s->mEnviroment.mInvokeFunctionCount <= 0)
Jason Sams8c880902010-06-15 12:15:57 -0700455 s->mEnviroment.mInvokeFunctions = NULL;
456 else {
457 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
458 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700459 }
460
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700461 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800462 if (s->mEnviroment.mFieldCount <= 0)
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700463 s->mEnviroment.mFieldAddress = NULL;
464 else {
465 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
466 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700467 s->initSlots();
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700468 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700469
Jason Sams8c6bc692009-09-16 15:04:38 -0700470 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
471 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700472 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700473 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700474
Jason Samsbe36bf32010-05-11 14:03:58 -0700475 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700476 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700477 BCCsizei pragmaCount;
478 BCCchar * str[pragmaMax];
479 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700480
Jason Sams10308932009-06-09 12:15:30 -0700481 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700482 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700483 if (!strcmp(str[ct], "version")) {
484 continue;
Jason Sams10308932009-06-09 12:15:30 -0700485 }
486
Jason Sams10308932009-06-09 12:15:30 -0700487 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700488 if (!strcmp(str[ct+1], "default")) {
489 continue;
490 }
491 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700492 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700493 continue;
494 }
Jason Sams10308932009-06-09 12:15:30 -0700495 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
496 }
497
498 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700499 if (!strcmp(str[ct+1], "default")) {
500 continue;
501 }
502 if (!strcmp(str[ct+1], "parent")) {
503 s->mEnviroment.mRaster.clear();
504 continue;
505 }
Jason Sams10308932009-06-09 12:15:30 -0700506 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
507 }
508
509 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700510 if (!strcmp(str[ct+1], "default")) {
511 continue;
512 }
513 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700514 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700515 continue;
516 }
Jason Sams10308932009-06-09 12:15:30 -0700517 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
518 }
519
Jason Samsb681c8a2009-09-28 18:12:56 -0700520 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700521 if (!strcmp(str[ct+1], "default")) {
522 continue;
523 }
524 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700525 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700526 continue;
527 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700528 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700529 }
530
531 }
532
Jason Samsd34b7252009-08-04 16:58:20 -0700533
Jason Sams10308932009-06-09 12:15:30 -0700534 } else {
535 // Deal with an error.
536 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700537}
538
539namespace android {
540namespace renderscript {
541
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800542void rsi_ScriptCBegin(Context * rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700543 ScriptCState *ss = &rsc->mScriptC;
Stephen Hines01b7d292010-09-28 15:45:45 -0700544 ss->clear(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700545}
546
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800547void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) {
Jason Sams1f526332009-06-05 17:35:09 -0700548 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800549
550 char *t = (char *)malloc(len + 1);
551 memcpy(t, text, len);
552 t[len] = 0;
553 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700554 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700555}
556
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800557
558RsScript rsi_ScriptCCreate(Context *rsc,
559 const char *packageName,
560 const char *resName,
561 const char *cacheDir)
Shih-wei Liao9503b662010-11-08 01:33:59 -0800562{
Jason Sams326e0dd2009-05-22 14:03:28 -0700563 ScriptCState *ss = &rsc->mScriptC;
564
Jason Sams225afd32010-10-21 14:06:55 -0700565 ObjectBaseRef<ScriptC> s(ss->mScript);
Stephen Hines01b7d292010-09-28 15:45:45 -0700566 ss->mScript.clear();
Jason Sams225afd32010-10-21 14:06:55 -0700567 s->incUserRef();
Jason Sams1f526332009-06-05 17:35:09 -0700568
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800569 // Open the apk and return the ZipArchive:
570 // int dexZipOpenArchive(const char* fileName, ZipArchive* pArchive)
571 ZipArchive archive;
572 long modWhen;
573 long crc32;
574 if (!dexZipOpenArchive(packageName, &archive)) { // Success
575 ZipEntry entry = dexZipFindEntry(&archive, resName);
576
577 int method;
578 size_t uncompLen;
579 size_t compLen;
580 off_t offset;
581 if (!dexZipGetEntryInfo(&archive,
582 entry,
583 &method,
584 &uncompLen,
585 &compLen,
586 &offset,
587 &modWhen,
588 &crc32)) {
589 } else {
590 LOGI("Coudn't get entry info for the bitcode in an apk");
591 }
592 } else {
593 LOGI("Couldn't open the archive and read the bitcode");
594 }
595
596 ss->runCompiler(rsc, s.get(), modWhen, crc32, resName, cacheDir);
Stephen Hines01b7d292010-09-28 15:45:45 -0700597 ss->clear(rsc);
598 return s.get();
Jason Sams326e0dd2009-05-22 14:03:28 -0700599}
600
Jason Sams326e0dd2009-05-22 14:03:28 -0700601}
602}