blob: 64eb7d269c27287bb0ffe9c5a3bef1af9acbab09 [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) {
Shih-wei Liaoa6ebec82010-12-23 23:04:21 +080040 LOGD(">>>> ScriptC ctor called, obj=%x", this);
Jason Samsbe36bf32010-05-11 14:03:58 -070041 mBccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -070042 memset(&mProgram, 0, sizeof(mProgram));
Jason Sams326e0dd2009-05-22 14:03:28 -070043}
44
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080045ScriptC::~ScriptC() {
Shih-wei Liaoa6ebec82010-12-23 23:04:21 +080046 LOGD(">>>> ~ScriptC()");
Jason Samsbe36bf32010-05-11 14:03:58 -070047 if (mBccScript) {
48 bccDeleteScript(mBccScript);
Shih-wei Liaoa6ebec82010-12-23 23:04:21 +080049 LOGD(">>>> ~ScriptC(mBCCScript)");
Jack Palevich1ef8b802009-05-28 15:53:04 -070050 }
Jason Samse402ed32009-11-03 11:25:42 -080051 free(mEnviroment.mScriptText);
52 mEnviroment.mScriptText = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070053}
54
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080055void ScriptC::setupScript(Context *rsc) {
Jason Samsc61346b2010-05-28 18:23:22 -070056 mEnviroment.mStartTimeMillis
57 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
58
Jason Samsbe36bf32010-05-11 14:03:58 -070059 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
Jason Sams900f1612010-09-16 18:18:29 -070060 if (mSlots[ct].get() && !mTypes[ct].get()) {
61 mTypes[ct].set(mSlots[ct]->getType());
62 }
63
64 if (!mTypes[ct].get())
Jason Samsbe36bf32010-05-11 14:03:58 -070065 continue;
Jason Sams900f1612010-09-16 18:18:29 -070066 void *ptr = NULL;
67 if (mSlots[ct].get()) {
68 ptr = mSlots[ct]->getPtr();
69 }
Jason Samsbe36bf32010-05-11 14:03:58 -070070 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
Jason Samsbe36bf32010-05-11 14:03:58 -070071
Jason Samsb9077f42010-09-22 15:57:41 -070072 if (rsc->props.mLogScripts) {
Jason Sams2fad7e42010-11-16 12:19:26 -080073 if (mSlots[ct].get() != NULL) {
74 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
75 } else {
76 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=null", rsc, ct, dest, ptr);
77 }
Jason Samsb9077f42010-09-22 15:57:41 -070078 }
Jason Samsbe36bf32010-05-11 14:03:58 -070079
80 if (dest) {
81 *dest = ptr;
Jason Samsada7f272009-09-24 14:55:38 -070082 }
83 }
84}
85
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086const Allocation *ScriptC::ptrToAllocation(const void *ptr) const {
Jason Samsce92d4b2010-05-17 14:55:34 -070087 if (!ptr) {
88 return NULL;
89 }
90 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
91 if (!mSlots[ct].get())
92 continue;
93 if (mSlots[ct]->getPtr() == ptr) {
94 return mSlots[ct].get();
95 }
96 }
97 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
98 return NULL;
99}
100
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101Script * ScriptC::setTLS(Script *sc) {
Jason Sams22fa3712010-05-19 17:22:57 -0700102 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
103 pthread_getspecific(Context::gThreadTLSKey);
104 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700105 Script *old = tls->mScript;
106 tls->mScript = sc;
107 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700108}
109
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110void ScriptC::setupGLState(Context *rsc) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700111 if (mEnviroment.mFragmentStore.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800112 rsc->setProgramStore(mEnviroment.mFragmentStore.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700113 }
114 if (mEnviroment.mFragment.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800115 rsc->setProgramFragment(mEnviroment.mFragment.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700116 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700117 if (mEnviroment.mVertex.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800118 rsc->setProgramVertex(mEnviroment.mVertex.get());
Jason Sams8ce125b2009-06-17 16:52:59 -0700119 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700120 if (mEnviroment.mRaster.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800121 rsc->setProgramRaster(mEnviroment.mRaster.get());
Jason Samsb681c8a2009-09-28 18:12:56 -0700122 }
Jason Samsc61346b2010-05-28 18:23:22 -0700123}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700124
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800125uint32_t ScriptC::run(Context *rsc) {
Jason Samsc61346b2010-05-28 18:23:22 -0700126 if (mProgram.mRoot == NULL) {
127 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
128 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700129 }
Jason Samsc61346b2010-05-28 18:23:22 -0700130
Jason Sams1f24db42010-12-11 17:42:30 -0800131 setupGLState(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700132 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700133
Jason Sams2dca84d2009-12-09 11:05:45 -0800134 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700135 Script * oldTLS = setTLS(this);
Jason Samsb9077f42010-09-22 15:57:41 -0700136
137 if (rsc->props.mLogScripts) {
138 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
139 }
140
Jason Samsbe36bf32010-05-11 14:03:58 -0700141 ret = mProgram.mRoot();
Jason Samsb9077f42010-09-22 15:57:41 -0700142
143 if (rsc->props.mLogScripts) {
144 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
145 }
146
Jason Samsc61346b2010-05-28 18:23:22 -0700147 setTLS(oldTLS);
Jason Samse45ac6e2009-07-20 14:31:06 -0700148 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700149}
150
Jason Sams7bf29dd2010-07-19 15:38:19 -0700151typedef struct {
152 Context *rsc;
153 ScriptC *script;
154 const Allocation * ain;
155 Allocation * aout;
156 const void * usr;
157
158 uint32_t mSliceSize;
159 volatile int mSliceNum;
160
161 const uint8_t *ptrIn;
162 uint32_t eStrideIn;
163 uint8_t *ptrOut;
164 uint32_t eStrideOut;
165
166 uint32_t xStart;
167 uint32_t xEnd;
168 uint32_t yStart;
169 uint32_t yEnd;
170 uint32_t zStart;
171 uint32_t zEnd;
172 uint32_t arrayStart;
173 uint32_t arrayEnd;
174
175 uint32_t dimX;
176 uint32_t dimY;
177 uint32_t dimZ;
178 uint32_t dimArray;
179} MTLaunchStruct;
180typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
181
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800182static void wc_xy(void *usr, uint32_t idx) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700183 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700184
185 while (1) {
186 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
187 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
188 uint32_t yEnd = yStart + mtls->mSliceSize;
189 yEnd = rsMin(yEnd, mtls->yEnd);
190 if (yEnd <= yStart) {
191 return;
192 }
193
194 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
Jason Sams8d957fa2010-09-28 14:41:22 -0700195 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700196 for (uint32_t y = yStart; y < yEnd; y++) {
197 uint32_t offset = mtls->dimX * y;
198 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
199 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
200
201 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
202 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
203 xPtrIn += mtls->eStrideIn;
204 xPtrOut += mtls->eStrideOut;
205 }
206 }
207 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700208}
209
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800210static void wc_x(void *usr, uint32_t idx) {
Jason Sams177f8442010-10-29 10:19:21 -0700211 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
212
213 while (1) {
214 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
215 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
216 uint32_t xEnd = xStart + mtls->mSliceSize;
217 xEnd = rsMin(xEnd, mtls->xEnd);
218 if (xEnd <= xStart) {
219 return;
220 }
221
222 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
223 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
224 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
225 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
226 for (uint32_t x = xStart; x < xEnd; x++) {
227 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
228 xPtrIn += mtls->eStrideIn;
229 xPtrOut += mtls->eStrideOut;
230 }
231 }
Jason Sams177f8442010-10-29 10:19:21 -0700232}
233
Jason Samsace3e012010-07-15 17:11:13 -0700234void ScriptC::runForEach(Context *rsc,
235 const Allocation * ain,
236 Allocation * aout,
237 const void * usr,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800238 const RsScriptCall *sc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700239 MTLaunchStruct mtls;
240 memset(&mtls, 0, sizeof(mtls));
Jason Sams60709252010-11-17 15:29:32 -0800241 Context::PushState ps(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700242
Jason Sams7bf29dd2010-07-19 15:38:19 -0700243 if (ain) {
244 mtls.dimX = ain->getType()->getDimX();
245 mtls.dimY = ain->getType()->getDimY();
246 mtls.dimZ = ain->getType()->getDimZ();
247 //mtls.dimArray = ain->getType()->getDimArray();
248 } else if (aout) {
249 mtls.dimX = aout->getType()->getDimX();
250 mtls.dimY = aout->getType()->getDimY();
251 mtls.dimZ = aout->getType()->getDimZ();
252 //mtls.dimArray = aout->getType()->getDimArray();
253 } else {
254 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
255 return;
256 }
Jason Samsace3e012010-07-15 17:11:13 -0700257
258 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700259 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700260 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700261 rsAssert(sc->xStart < mtls.dimX);
262 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700263 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700264 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
265 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
266 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700267 }
268
269 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700270 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700271 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700272 rsAssert(sc->yStart < mtls.dimY);
273 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700274 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700275 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
276 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
277 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700278 }
279
Jason Sams7bf29dd2010-07-19 15:38:19 -0700280 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
281 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
282 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
283 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700284
285 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700286
Jason Sams1f24db42010-12-11 17:42:30 -0800287 setupGLState(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700288 setupScript(rsc);
289 Script * oldTLS = setTLS(this);
290
Jason Sams7bf29dd2010-07-19 15:38:19 -0700291 mtls.rsc = rsc;
292 mtls.ain = ain;
293 mtls.aout = aout;
294 mtls.script = this;
295 mtls.usr = usr;
296 mtls.mSliceSize = 10;
297 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700298
Jason Sams7bf29dd2010-07-19 15:38:19 -0700299 mtls.ptrIn = NULL;
300 mtls.eStrideIn = 0;
301 if (ain) {
302 mtls.ptrIn = (const uint8_t *)ain->getPtr();
303 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700304 }
305
Jason Sams7bf29dd2010-07-19 15:38:19 -0700306 mtls.ptrOut = NULL;
307 mtls.eStrideOut = 0;
308 if (aout) {
309 mtls.ptrOut = (uint8_t *)aout->getPtr();
310 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
311 }
312
Jason Sams177f8442010-10-29 10:19:21 -0700313 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) {
314 if (mtls.dimY > 1) {
315 rsc->launchThreads(wc_xy, &mtls);
316 } else {
317 rsc->launchThreads(wc_x, &mtls);
318 }
Jason Sams18133402010-07-20 15:09:00 -0700319
320 //LOGE("launch 1");
Jason Sams18133402010-07-20 15:09:00 -0700321 } else {
Jason Sams8d957fa2010-09-28 14:41:22 -0700322 //LOGE("launch 3");
Jason Sams18133402010-07-20 15:09:00 -0700323 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
324 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
325 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
326 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
327 mtls.dimX * mtls.dimY * z +
328 mtls.dimX * y;
329 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
330 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700331
Jason Sams18133402010-07-20 15:09:00 -0700332 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
333 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
334 xPtrIn += mtls.eStrideIn;
335 xPtrOut += mtls.eStrideOut;
336 }
Jason Samsace3e012010-07-15 17:11:13 -0700337 }
338 }
339 }
Jason Samsc61346b2010-05-28 18:23:22 -0700340 }
Jason Sams18133402010-07-20 15:09:00 -0700341
Jason Samsc61346b2010-05-28 18:23:22 -0700342 setTLS(oldTLS);
343}
344
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800345void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) {
Jason Sams22fa3712010-05-19 17:22:57 -0700346 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
347 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
348 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
349 return;
350 }
Jason Samsc61346b2010-05-28 18:23:22 -0700351 setupScript(rsc);
352 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700353
Jason Samsb9077f42010-09-22 15:57:41 -0700354 if (rsc->props.mLogScripts) {
355 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
356 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700357 ((void (*)(const void *, uint32_t))
358 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700359 if (rsc->props.mLogScripts) {
360 LOGV("%p ScriptC::Invoke complete", rsc);
361 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700362
Jason Samsc61346b2010-05-28 18:23:22 -0700363 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700364}
365
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800366ScriptCState::ScriptCState() {
Stephen Hines01b7d292010-09-28 15:45:45 -0700367 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700368}
369
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800370ScriptCState::~ScriptCState() {
Stephen Hines01b7d292010-09-28 15:45:45 -0700371 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700372}
373
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800374void ScriptCState::init(Context *rsc) {
Stephen Hines01b7d292010-09-28 15:45:45 -0700375 clear(rsc);
376}
377
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800378void ScriptCState::clear(Context *rsc) {
Stephen Hines01b7d292010-09-28 15:45:45 -0700379 rsAssert(rsc);
Stephen Hines01b7d292010-09-28 15:45:45 -0700380 mScript.clear();
381 mScript.set(new ScriptC(rsc));
Jason Sams1f526332009-06-05 17:35:09 -0700382}
383
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800384static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700385 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700386 ScriptC *s = (ScriptC *)pContext;
Shih-wei Liaof17b13b2010-12-07 13:44:10 -0800387 if (!strcmp(name, "__isThreadable")) {
388 return (BCCvoid*) s->mEnviroment.mIsThreadable;
389 } else if (!strcmp(name, "__clearThreadable")) {
390 s->mEnviroment.mIsThreadable = false;
391 return NULL;
392 }
Jason Samsaeb094b2010-05-18 13:35:45 -0700393 sym = ScriptCState::lookupSymbol(name);
Jason Sams6bfc1b92010-11-01 14:26:30 -0700394 if (!sym) {
395 sym = ScriptCState::lookupSymbolCL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700396 }
Jason Sams6bfc1b92010-11-01 14:26:30 -0700397 if (!sym) {
398 sym = ScriptCState::lookupSymbolGL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700399 }
Jason Sams29df66f2009-07-16 15:08:06 -0700400 if (sym) {
Jason Sams6bfc1b92010-11-01 14:26:30 -0700401 s->mEnviroment.mIsThreadable &= sym->threadable;
Jason Sams29df66f2009-07-16 15:08:06 -0700402 return sym->mPtr;
403 }
Jason Sams29df66f2009-07-16 15:08:06 -0700404 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700405 return NULL;
406}
Jason Samsa4a54e42009-06-10 18:39:40 -0700407
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700408extern const char rs_runtime_lib_bc[];
409extern unsigned rs_runtime_lib_bc_size;
410
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800411void ScriptCState::runCompiler(Context *rsc,
412 ScriptC *s,
413 long modWhen,
414 long crc32,
415 const char *resName,
416 const char *cacheDir) {
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700417 {
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700418 s->mBccScript = bccCreateScript();
419 s->mEnviroment.mIsThreadable = true;
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700420 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
Shih-wei Liao9503b662010-11-08 01:33:59 -0800421 // bccReadBC() reads in the BitCode, if no cache file corresponding to
422 // the resName is found. Otherwise, bccReadBC() returns a negative value
423 // and the "else" branch will be taken.
424 if (bccReadBC(s->mBccScript,
425 s->mEnviroment.mScriptText,
426 s->mEnviroment.mScriptTextLength,
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800427 modWhen,
428 crc32,
Shih-wei Liao15849d92010-12-10 01:03:59 -0800429 resName,
430 cacheDir) >= 0) {
Shih-wei Liao9503b662010-11-08 01:33:59 -0800431 //bccLinkBC(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
432 bccCompileBC(s->mBccScript);
433 } else {
434 // bccReadBC returns a neagative value: Didn't read any script,
435 // So, use cached binary instead
Shih-wei Liaoff8ce9c2010-12-16 04:58:35 -0800436 if (bccLoadBinary(s->mBccScript)) { // LoadBinary fails ==> Recompile
437 bccReadBC(s->mBccScript,
438 s->mEnviroment.mScriptText,
439 s->mEnviroment.mScriptTextLength,
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800440 modWhen,
441 crc32,
Shih-wei Liaoa6ebec82010-12-23 23:04:21 +0800442 resName,
Shih-wei Liaoff8ce9c2010-12-16 04:58:35 -0800443 cacheDir);
444 bccCompileBC(s->mBccScript);
445 }
Shih-wei Liao9503b662010-11-08 01:33:59 -0800446 }
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700447 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
448 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
449 }
Jason Samsb9077f42010-09-22 15:57:41 -0700450 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700451
Jason Sams8c6bc692009-09-16 15:04:38 -0700452 if (s->mProgram.mInit) {
453 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700454 }
455
Jason Sams8c880902010-06-15 12:15:57 -0700456 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800457 if (s->mEnviroment.mInvokeFunctionCount <= 0)
Jason Sams8c880902010-06-15 12:15:57 -0700458 s->mEnviroment.mInvokeFunctions = NULL;
459 else {
460 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
461 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700462 }
463
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700464 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800465 if (s->mEnviroment.mFieldCount <= 0)
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700466 s->mEnviroment.mFieldAddress = NULL;
467 else {
468 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
469 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700470 s->initSlots();
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700471 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700472
Jason Sams8c6bc692009-09-16 15:04:38 -0700473 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
474 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700475 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700476 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700477
Jason Samsbe36bf32010-05-11 14:03:58 -0700478 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700479 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700480 BCCsizei pragmaCount;
481 BCCchar * str[pragmaMax];
482 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700483
Jason Sams10308932009-06-09 12:15:30 -0700484 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700485 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700486 if (!strcmp(str[ct], "version")) {
487 continue;
Jason Sams10308932009-06-09 12:15:30 -0700488 }
489
Jason Sams10308932009-06-09 12:15:30 -0700490 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700491 if (!strcmp(str[ct+1], "default")) {
492 continue;
493 }
494 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700495 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700496 continue;
497 }
Jason Sams10308932009-06-09 12:15:30 -0700498 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
499 }
500
501 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700502 if (!strcmp(str[ct+1], "default")) {
503 continue;
504 }
505 if (!strcmp(str[ct+1], "parent")) {
506 s->mEnviroment.mRaster.clear();
507 continue;
508 }
Jason Sams10308932009-06-09 12:15:30 -0700509 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
510 }
511
512 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700513 if (!strcmp(str[ct+1], "default")) {
514 continue;
515 }
516 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700517 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700518 continue;
519 }
Jason Sams10308932009-06-09 12:15:30 -0700520 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
521 }
522
Jason Samsb681c8a2009-09-28 18:12:56 -0700523 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700524 if (!strcmp(str[ct+1], "default")) {
525 continue;
526 }
527 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700528 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700529 continue;
530 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700531 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700532 }
533
534 }
535
Jason Samsd34b7252009-08-04 16:58:20 -0700536
Jason Sams10308932009-06-09 12:15:30 -0700537 } else {
538 // Deal with an error.
539 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700540}
541
542namespace android {
543namespace renderscript {
544
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800545void rsi_ScriptCBegin(Context * rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700546 ScriptCState *ss = &rsc->mScriptC;
Stephen Hines01b7d292010-09-28 15:45:45 -0700547 ss->clear(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700548}
549
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800550void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) {
Jason Sams1f526332009-06-05 17:35:09 -0700551 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800552
553 char *t = (char *)malloc(len + 1);
554 memcpy(t, text, len);
555 t[len] = 0;
556 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700557 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700558}
559
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800560
561RsScript rsi_ScriptCCreate(Context *rsc,
562 const char *packageName,
563 const char *resName,
564 const char *cacheDir)
Shih-wei Liao9503b662010-11-08 01:33:59 -0800565{
Jason Sams326e0dd2009-05-22 14:03:28 -0700566 ScriptCState *ss = &rsc->mScriptC;
567
Jason Sams225afd32010-10-21 14:06:55 -0700568 ObjectBaseRef<ScriptC> s(ss->mScript);
Stephen Hines01b7d292010-09-28 15:45:45 -0700569 ss->mScript.clear();
Jason Sams225afd32010-10-21 14:06:55 -0700570 s->incUserRef();
Jason Sams1f526332009-06-05 17:35:09 -0700571
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800572 // Open the apk and return the ZipArchive:
573 // int dexZipOpenArchive(const char* fileName, ZipArchive* pArchive)
574 ZipArchive archive;
575 long modWhen;
576 long crc32;
577 if (!dexZipOpenArchive(packageName, &archive)) { // Success
578 ZipEntry entry = dexZipFindEntry(&archive, resName);
579
580 int method;
581 size_t uncompLen;
582 size_t compLen;
583 off_t offset;
584 if (!dexZipGetEntryInfo(&archive,
585 entry,
586 &method,
587 &uncompLen,
588 &compLen,
589 &offset,
590 &modWhen,
591 &crc32)) {
592 } else {
593 LOGI("Coudn't get entry info for the bitcode in an apk");
594 }
595 } else {
596 LOGI("Couldn't open the archive and read the bitcode");
597 }
598
599 ss->runCompiler(rsc, s.get(), modWhen, crc32, resName, cacheDir);
Stephen Hines01b7d292010-09-28 15:45:45 -0700600 ss->clear(rsc);
601 return s.get();
Jason Sams326e0dd2009-05-22 14:03:28 -0700602}
603
Jason Sams326e0dd2009-05-22 14:03:28 -0700604}
605}