blob: c692bc1e997882f97233160fff027d8a871fef45 [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"
Jack Palevich1ef8b802009-05-28 15:53:04 -070023
Jason Sams1aa5a4e2009-06-22 17:15:15 -070024#include <GLES/gl.h>
25#include <GLES/glext.h>
26
Jason Sams326e0dd2009-05-22 14:03:28 -070027using namespace android;
28using namespace android::renderscript;
29
Jason Samse5769102009-06-19 16:03:18 -070030#define GET_TLS() Context::ScriptTLSStruct * tls = \
31 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
32 Context * rsc = tls->mContext; \
33 ScriptC * sc = (ScriptC *) tls->mScript
34
Jason Sams326e0dd2009-05-22 14:03:28 -070035
Jason Samse514b452009-09-25 14:51:22 -070036ScriptC::ScriptC(Context *rsc) : Script(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070037{
Jason Samsbe36bf32010-05-11 14:03:58 -070038 mBccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -070039 memset(&mProgram, 0, sizeof(mProgram));
Jason Sams326e0dd2009-05-22 14:03:28 -070040}
41
42ScriptC::~ScriptC()
43{
Jason Samsbe36bf32010-05-11 14:03:58 -070044 if (mBccScript) {
45 bccDeleteScript(mBccScript);
Jack Palevich1ef8b802009-05-28 15:53:04 -070046 }
Jason Samse402ed32009-11-03 11:25:42 -080047 free(mEnviroment.mScriptText);
48 mEnviroment.mScriptText = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070049}
50
Jason Samsc61346b2010-05-28 18:23:22 -070051void ScriptC::setupScript(Context *rsc)
Jason Samsada7f272009-09-24 14:55:38 -070052{
Jason Samsc61346b2010-05-28 18:23:22 -070053 setupGLState(rsc);
54 mEnviroment.mStartTimeMillis
55 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
56
Jason Samsbe36bf32010-05-11 14:03:58 -070057 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
Jason Sams900f1612010-09-16 18:18:29 -070058 if (mSlots[ct].get() && !mTypes[ct].get()) {
59 mTypes[ct].set(mSlots[ct]->getType());
60 }
61
62 if (!mTypes[ct].get())
Jason Samsbe36bf32010-05-11 14:03:58 -070063 continue;
Jason Sams900f1612010-09-16 18:18:29 -070064 void *ptr = NULL;
65 if (mSlots[ct].get()) {
66 ptr = mSlots[ct]->getPtr();
67 }
Jason Samsbe36bf32010-05-11 14:03:58 -070068 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
Jason Samsbe36bf32010-05-11 14:03:58 -070069
Jason Samsb9077f42010-09-22 15:57:41 -070070 if (rsc->props.mLogScripts) {
71 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
72
73 //const uint32_t *p32 = (const uint32_t *)ptr;
74 //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) {
75 //LOGE(" %i = 0x%08x ", ct2, p32[ct2]);
76 //}
77 }
Jason Samsbe36bf32010-05-11 14:03:58 -070078
79 if (dest) {
80 *dest = ptr;
Jason Samsada7f272009-09-24 14:55:38 -070081 }
82 }
83}
84
Jason Samsce92d4b2010-05-17 14:55:34 -070085const Allocation *ScriptC::ptrToAllocation(const void *ptr) const
86{
87 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
Jason Samsc61346b2010-05-28 18:23:22 -0700101Script * ScriptC::setTLS(Script *sc)
Jason Sams22fa3712010-05-19 17:22:57 -0700102{
103 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
104 pthread_getspecific(Context::gThreadTLSKey);
105 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700106 Script *old = tls->mScript;
107 tls->mScript = sc;
108 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700109}
110
Jason Sams326e0dd2009-05-22 14:03:28 -0700111
Jason Samsc61346b2010-05-28 18:23:22 -0700112void ScriptC::setupGLState(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700113{
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700114 if (mEnviroment.mFragmentStore.get()) {
115 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
116 }
117 if (mEnviroment.mFragment.get()) {
118 rsc->setFragment(mEnviroment.mFragment.get());
119 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700120 if (mEnviroment.mVertex.get()) {
121 rsc->setVertex(mEnviroment.mVertex.get());
122 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700123 if (mEnviroment.mRaster.get()) {
124 rsc->setRaster(mEnviroment.mRaster.get());
125 }
Jason Samsc61346b2010-05-28 18:23:22 -0700126}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700127
Jason Samsc61346b2010-05-28 18:23:22 -0700128uint32_t ScriptC::run(Context *rsc)
129{
130 if (mProgram.mRoot == NULL) {
131 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
132 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700133 }
Jason Samsc61346b2010-05-28 18:23:22 -0700134
135 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700136
Jason Sams2dca84d2009-12-09 11:05:45 -0800137 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700138 Script * oldTLS = setTLS(this);
Jason Samsb9077f42010-09-22 15:57:41 -0700139
140 if (rsc->props.mLogScripts) {
141 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
142 }
143
Jason Samsbe36bf32010-05-11 14:03:58 -0700144 ret = mProgram.mRoot();
Jason Samsb9077f42010-09-22 15:57:41 -0700145
146 if (rsc->props.mLogScripts) {
147 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
148 }
149
Jason Samsc61346b2010-05-28 18:23:22 -0700150 setTLS(oldTLS);
Jason Samse45ac6e2009-07-20 14:31:06 -0700151 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700152}
153
Jason Samsc61346b2010-05-28 18:23:22 -0700154
Jason Sams7bf29dd2010-07-19 15:38:19 -0700155typedef struct {
156 Context *rsc;
157 ScriptC *script;
158 const Allocation * ain;
159 Allocation * aout;
160 const void * usr;
161
162 uint32_t mSliceSize;
163 volatile int mSliceNum;
164
165 const uint8_t *ptrIn;
166 uint32_t eStrideIn;
167 uint8_t *ptrOut;
168 uint32_t eStrideOut;
169
170 uint32_t xStart;
171 uint32_t xEnd;
172 uint32_t yStart;
173 uint32_t yEnd;
174 uint32_t zStart;
175 uint32_t zEnd;
176 uint32_t arrayStart;
177 uint32_t arrayEnd;
178
179 uint32_t dimX;
180 uint32_t dimY;
181 uint32_t dimZ;
182 uint32_t dimArray;
183} MTLaunchStruct;
184typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
185
186static void wc_xy(void *usr, uint32_t idx)
187{
188 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700189
190 while (1) {
191 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
192 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
193 uint32_t yEnd = yStart + mtls->mSliceSize;
194 yEnd = rsMin(yEnd, mtls->yEnd);
195 if (yEnd <= yStart) {
196 return;
197 }
198
199 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
Jason Sams8d957fa2010-09-28 14:41:22 -0700200 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700201 for (uint32_t y = yStart; y < yEnd; y++) {
202 uint32_t offset = mtls->dimX * y;
203 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
204 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
205
206 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
207 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
208 xPtrIn += mtls->eStrideIn;
209 xPtrOut += mtls->eStrideOut;
210 }
211 }
212 }
213
214}
215
Jason Sams177f8442010-10-29 10:19:21 -0700216static void wc_x(void *usr, uint32_t idx)
217{
218 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
219
220 while (1) {
221 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
222 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
223 uint32_t xEnd = xStart + mtls->mSliceSize;
224 xEnd = rsMin(xEnd, mtls->xEnd);
225 if (xEnd <= xStart) {
226 return;
227 }
228
229 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
230 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
231 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
232 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
233 for (uint32_t x = xStart; x < xEnd; x++) {
234 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
235 xPtrIn += mtls->eStrideIn;
236 xPtrOut += mtls->eStrideOut;
237 }
238 }
239
240}
241
Jason Samsace3e012010-07-15 17:11:13 -0700242void ScriptC::runForEach(Context *rsc,
243 const Allocation * ain,
244 Allocation * aout,
245 const void * usr,
246 const RsScriptCall *sc)
Jason Samsc61346b2010-05-28 18:23:22 -0700247{
Jason Sams7bf29dd2010-07-19 15:38:19 -0700248 MTLaunchStruct mtls;
249 memset(&mtls, 0, sizeof(mtls));
Jason Samsc61346b2010-05-28 18:23:22 -0700250
Jason Sams7bf29dd2010-07-19 15:38:19 -0700251 if (ain) {
252 mtls.dimX = ain->getType()->getDimX();
253 mtls.dimY = ain->getType()->getDimY();
254 mtls.dimZ = ain->getType()->getDimZ();
255 //mtls.dimArray = ain->getType()->getDimArray();
256 } else if (aout) {
257 mtls.dimX = aout->getType()->getDimX();
258 mtls.dimY = aout->getType()->getDimY();
259 mtls.dimZ = aout->getType()->getDimZ();
260 //mtls.dimArray = aout->getType()->getDimArray();
261 } else {
262 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
263 return;
264 }
Jason Samsace3e012010-07-15 17:11:13 -0700265
266 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700267 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700268 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700269 rsAssert(sc->xStart < mtls.dimX);
270 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700271 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700272 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
273 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
274 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700275 }
276
277 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700278 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700279 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700280 rsAssert(sc->yStart < mtls.dimY);
281 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700282 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700283 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
284 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
285 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700286 }
287
Jason Sams7bf29dd2010-07-19 15:38:19 -0700288 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
289 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
290 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
291 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700292
293 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700294
295 setupScript(rsc);
296 Script * oldTLS = setTLS(this);
297
Jason Samsace3e012010-07-15 17:11:13 -0700298
Jason Sams7bf29dd2010-07-19 15:38:19 -0700299 mtls.rsc = rsc;
300 mtls.ain = ain;
301 mtls.aout = aout;
302 mtls.script = this;
303 mtls.usr = usr;
304 mtls.mSliceSize = 10;
305 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700306
Jason Sams7bf29dd2010-07-19 15:38:19 -0700307 mtls.ptrIn = NULL;
308 mtls.eStrideIn = 0;
309 if (ain) {
310 mtls.ptrIn = (const uint8_t *)ain->getPtr();
311 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700312 }
313
Jason Sams7bf29dd2010-07-19 15:38:19 -0700314 mtls.ptrOut = NULL;
315 mtls.eStrideOut = 0;
316 if (aout) {
317 mtls.ptrOut = (uint8_t *)aout->getPtr();
318 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
319 }
320
Jason Sams177f8442010-10-29 10:19:21 -0700321 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) {
322 if (mtls.dimY > 1) {
323 rsc->launchThreads(wc_xy, &mtls);
324 } else {
325 rsc->launchThreads(wc_x, &mtls);
326 }
Jason Sams18133402010-07-20 15:09:00 -0700327
328 //LOGE("launch 1");
Jason Sams18133402010-07-20 15:09:00 -0700329 } else {
Jason Sams8d957fa2010-09-28 14:41:22 -0700330 //LOGE("launch 3");
Jason Sams18133402010-07-20 15:09:00 -0700331 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
332 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
333 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
334 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
335 mtls.dimX * mtls.dimY * z +
336 mtls.dimX * y;
337 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
338 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700339
Jason Sams18133402010-07-20 15:09:00 -0700340 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
341 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
342 xPtrIn += mtls.eStrideIn;
343 xPtrOut += mtls.eStrideOut;
344 }
Jason Samsace3e012010-07-15 17:11:13 -0700345 }
346 }
347 }
Jason Samsc61346b2010-05-28 18:23:22 -0700348 }
Jason Sams18133402010-07-20 15:09:00 -0700349
Jason Samsc61346b2010-05-28 18:23:22 -0700350 setTLS(oldTLS);
351}
352
Jason Sams22fa3712010-05-19 17:22:57 -0700353void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
354{
355 //LOGE("rsi_ScriptInvoke %i", slot);
356 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
357 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
358 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
359 return;
360 }
Jason Samsc61346b2010-05-28 18:23:22 -0700361 setupScript(rsc);
362 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700363
Jason Samsb9077f42010-09-22 15:57:41 -0700364 if (rsc->props.mLogScripts) {
365 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
366 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700367 ((void (*)(const void *, uint32_t))
368 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700369 if (rsc->props.mLogScripts) {
370 LOGV("%p ScriptC::Invoke complete", rsc);
371 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700372
Jason Samsc61346b2010-05-28 18:23:22 -0700373 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700374}
375
Jason Sams326e0dd2009-05-22 14:03:28 -0700376ScriptCState::ScriptCState()
377{
Stephen Hines01b7d292010-09-28 15:45:45 -0700378 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700379}
380
381ScriptCState::~ScriptCState()
382{
Stephen Hines01b7d292010-09-28 15:45:45 -0700383 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700384}
385
Stephen Hines01b7d292010-09-28 15:45:45 -0700386void ScriptCState::init(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700387{
Stephen Hines01b7d292010-09-28 15:45:45 -0700388 clear(rsc);
389}
390
391void ScriptCState::clear(Context *rsc)
392{
393 rsAssert(rsc);
Stephen Hines01b7d292010-09-28 15:45:45 -0700394 mScript.clear();
395 mScript.set(new ScriptC(rsc));
Jason Sams1f526332009-06-05 17:35:09 -0700396}
397
Jason Samsbe36bf32010-05-11 14:03:58 -0700398static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700399{
Jason Samsaeb094b2010-05-18 13:35:45 -0700400 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700401 ScriptC *s = (ScriptC *)pContext;
Jason Samsaeb094b2010-05-18 13:35:45 -0700402 sym = ScriptCState::lookupSymbol(name);
Jason Sams6bfc1b92010-11-01 14:26:30 -0700403 if (!sym) {
404 sym = ScriptCState::lookupSymbolCL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700405 }
Jason Sams6bfc1b92010-11-01 14:26:30 -0700406 if (!sym) {
407 sym = ScriptCState::lookupSymbolGL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700408 }
Jason Sams29df66f2009-07-16 15:08:06 -0700409 if (sym) {
Jason Sams6bfc1b92010-11-01 14:26:30 -0700410 s->mEnviroment.mIsThreadable &= sym->threadable;
Jason Sams29df66f2009-07-16 15:08:06 -0700411 return sym->mPtr;
412 }
Jason Sams29df66f2009-07-16 15:08:06 -0700413 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700414 return NULL;
415}
Jason Samsa4a54e42009-06-10 18:39:40 -0700416
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700417extern const char rs_runtime_lib_bc[];
418extern unsigned rs_runtime_lib_bc_size;
419
Jason Sams8c6bc692009-09-16 15:04:38 -0700420void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
Jason Sams1f526332009-06-05 17:35:09 -0700421{
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700422 {
423 StopWatch compileTimer("RenderScript compile time");
424 s->mBccScript = bccCreateScript();
425 s->mEnviroment.mIsThreadable = true;
426 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700427 //bccLinkBitcode(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700428 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
429 bccCompileScript(s->mBccScript);
430 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
431 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
432 }
Jason Samsb9077f42010-09-22 15:57:41 -0700433 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700434
Jason Sams8c6bc692009-09-16 15:04:38 -0700435 if (s->mProgram.mInit) {
436 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700437 }
438
Jason Sams8c880902010-06-15 12:15:57 -0700439 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
440 if(s->mEnviroment.mInvokeFunctionCount <= 0)
441 s->mEnviroment.mInvokeFunctions = NULL;
442 else {
443 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
444 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700445 }
446
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700447 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
448 if(s->mEnviroment.mFieldCount <= 0)
449 s->mEnviroment.mFieldAddress = NULL;
450 else {
451 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
452 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700453 s->initSlots();
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700454 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700455
Jason Sams8c6bc692009-09-16 15:04:38 -0700456 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
457 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700458 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700459 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700460
Jason Samsbe36bf32010-05-11 14:03:58 -0700461 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700462 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700463 BCCsizei pragmaCount;
464 BCCchar * str[pragmaMax];
465 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700466
Jason Sams10308932009-06-09 12:15:30 -0700467 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700468 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700469 if (!strcmp(str[ct], "version")) {
470 continue;
Jason Sams10308932009-06-09 12:15:30 -0700471 }
472
Jason Sams10308932009-06-09 12:15:30 -0700473 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700474 if (!strcmp(str[ct+1], "default")) {
475 continue;
476 }
477 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700478 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700479 continue;
480 }
Jason Sams10308932009-06-09 12:15:30 -0700481 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
482 }
483
484 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700485 if (!strcmp(str[ct+1], "default")) {
486 continue;
487 }
488 if (!strcmp(str[ct+1], "parent")) {
489 s->mEnviroment.mRaster.clear();
490 continue;
491 }
Jason Sams10308932009-06-09 12:15:30 -0700492 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
493 }
494
495 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700496 if (!strcmp(str[ct+1], "default")) {
497 continue;
498 }
499 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700500 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700501 continue;
502 }
Jason Sams10308932009-06-09 12:15:30 -0700503 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
504 }
505
Jason Samsb681c8a2009-09-28 18:12:56 -0700506 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700507 if (!strcmp(str[ct+1], "default")) {
508 continue;
509 }
510 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700511 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700512 continue;
513 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700514 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700515 }
516
517 }
518
Jason Samsd34b7252009-08-04 16:58:20 -0700519
Jason Sams10308932009-06-09 12:15:30 -0700520 } else {
521 // Deal with an error.
522 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700523}
524
Jason Sams8b2c0652009-08-12 17:54:11 -0700525
Joe Onorato57b79ce2009-08-09 22:57:44 -0700526
Jason Sams326e0dd2009-05-22 14:03:28 -0700527namespace android {
528namespace renderscript {
529
530void rsi_ScriptCBegin(Context * rsc)
531{
532 ScriptCState *ss = &rsc->mScriptC;
Stephen Hines01b7d292010-09-28 15:45:45 -0700533 ss->clear(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700534}
535
Jason Sams1f526332009-06-05 17:35:09 -0700536void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
537{
538 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800539
540 char *t = (char *)malloc(len + 1);
541 memcpy(t, text, len);
542 t[len] = 0;
543 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700544 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700545}
546
547
Jason Sams326e0dd2009-05-22 14:03:28 -0700548RsScript rsi_ScriptCCreate(Context * rsc)
549{
550 ScriptCState *ss = &rsc->mScriptC;
551
Jason Sams225afd32010-10-21 14:06:55 -0700552 ObjectBaseRef<ScriptC> s(ss->mScript);
Stephen Hines01b7d292010-09-28 15:45:45 -0700553 ss->mScript.clear();
Jason Sams225afd32010-10-21 14:06:55 -0700554 s->incUserRef();
Jason Sams1f526332009-06-05 17:35:09 -0700555
Stephen Hines01b7d292010-09-28 15:45:45 -0700556 ss->runCompiler(rsc, s.get());
Stephen Hines01b7d292010-09-28 15:45:45 -0700557 ss->clear(rsc);
558 return s.get();
Jason Sams326e0dd2009-05-22 14:03:28 -0700559}
560
Jason Sams326e0dd2009-05-22 14:03:28 -0700561}
562}