blob: a2910d7e75d14159cb498ab83ec62d83d0635c3c [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 Samsf2649a92009-09-25 16:37:33 -070038 mAllocFile = __FILE__;
39 mAllocLine = __LINE__;
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
44ScriptC::~ScriptC()
45{
Jason Samsbe36bf32010-05-11 14:03:58 -070046 if (mBccScript) {
47 bccDeleteScript(mBccScript);
Jack Palevich1ef8b802009-05-28 15:53:04 -070048 }
Jason Samse402ed32009-11-03 11:25:42 -080049 free(mEnviroment.mScriptText);
50 mEnviroment.mScriptText = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070051}
52
Jason Samsc61346b2010-05-28 18:23:22 -070053void ScriptC::setupScript(Context *rsc)
Jason Samsada7f272009-09-24 14:55:38 -070054{
Jason Samsc61346b2010-05-28 18:23:22 -070055 setupGLState(rsc);
56 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) {
73 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
74
75 //const uint32_t *p32 = (const uint32_t *)ptr;
76 //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) {
77 //LOGE(" %i = 0x%08x ", ct2, p32[ct2]);
78 //}
79 }
Jason Samsbe36bf32010-05-11 14:03:58 -070080
81 if (dest) {
82 *dest = ptr;
83 } else {
Jason Samsd7e54812010-10-10 17:58:25 -070084 if (rsc->props.mLogScripts) {
85 LOGV("ScriptC::setupScript, NULL var binding address.");
86 }
Jason Samsada7f272009-09-24 14:55:38 -070087 }
88 }
89}
90
Jason Samsce92d4b2010-05-17 14:55:34 -070091const Allocation *ScriptC::ptrToAllocation(const void *ptr) const
92{
93 if (!ptr) {
94 return NULL;
95 }
96 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
97 if (!mSlots[ct].get())
98 continue;
99 if (mSlots[ct]->getPtr() == ptr) {
100 return mSlots[ct].get();
101 }
102 }
103 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
104 return NULL;
105}
106
Jason Samsc61346b2010-05-28 18:23:22 -0700107Script * ScriptC::setTLS(Script *sc)
Jason Sams22fa3712010-05-19 17:22:57 -0700108{
109 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
110 pthread_getspecific(Context::gThreadTLSKey);
111 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700112 Script *old = tls->mScript;
113 tls->mScript = sc;
114 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700115}
116
Jason Sams326e0dd2009-05-22 14:03:28 -0700117
Jason Samsc61346b2010-05-28 18:23:22 -0700118void ScriptC::setupGLState(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700119{
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700120 if (mEnviroment.mFragmentStore.get()) {
121 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
122 }
123 if (mEnviroment.mFragment.get()) {
124 rsc->setFragment(mEnviroment.mFragment.get());
125 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700126 if (mEnviroment.mVertex.get()) {
127 rsc->setVertex(mEnviroment.mVertex.get());
128 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700129 if (mEnviroment.mRaster.get()) {
130 rsc->setRaster(mEnviroment.mRaster.get());
131 }
Jason Samsc61346b2010-05-28 18:23:22 -0700132}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700133
Jason Samsc61346b2010-05-28 18:23:22 -0700134uint32_t ScriptC::run(Context *rsc)
135{
136 if (mProgram.mRoot == NULL) {
137 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
138 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700139 }
Jason Samsc61346b2010-05-28 18:23:22 -0700140
141 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700142
Jason Sams2dca84d2009-12-09 11:05:45 -0800143 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700144 Script * oldTLS = setTLS(this);
Jason Samsb9077f42010-09-22 15:57:41 -0700145
146 if (rsc->props.mLogScripts) {
147 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
148 }
149
Jason Samsbe36bf32010-05-11 14:03:58 -0700150 ret = mProgram.mRoot();
Jason Samsb9077f42010-09-22 15:57:41 -0700151
152 if (rsc->props.mLogScripts) {
153 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
154 }
155
Jason Samsc61346b2010-05-28 18:23:22 -0700156 setTLS(oldTLS);
Jason Samse45ac6e2009-07-20 14:31:06 -0700157 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700158}
159
Jason Samsc61346b2010-05-28 18:23:22 -0700160
Jason Sams7bf29dd2010-07-19 15:38:19 -0700161typedef struct {
162 Context *rsc;
163 ScriptC *script;
164 const Allocation * ain;
165 Allocation * aout;
166 const void * usr;
167
168 uint32_t mSliceSize;
169 volatile int mSliceNum;
170
171 const uint8_t *ptrIn;
172 uint32_t eStrideIn;
173 uint8_t *ptrOut;
174 uint32_t eStrideOut;
175
176 uint32_t xStart;
177 uint32_t xEnd;
178 uint32_t yStart;
179 uint32_t yEnd;
180 uint32_t zStart;
181 uint32_t zEnd;
182 uint32_t arrayStart;
183 uint32_t arrayEnd;
184
185 uint32_t dimX;
186 uint32_t dimY;
187 uint32_t dimZ;
188 uint32_t dimArray;
189} MTLaunchStruct;
190typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
191
192static void wc_xy(void *usr, uint32_t idx)
193{
194 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700195
196 while (1) {
197 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
198 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
199 uint32_t yEnd = yStart + mtls->mSliceSize;
200 yEnd = rsMin(yEnd, mtls->yEnd);
201 if (yEnd <= yStart) {
202 return;
203 }
204
205 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
Jason Sams8d957fa2010-09-28 14:41:22 -0700206 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700207 for (uint32_t y = yStart; y < yEnd; y++) {
208 uint32_t offset = mtls->dimX * y;
209 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
210 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
211
212 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
213 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
214 xPtrIn += mtls->eStrideIn;
215 xPtrOut += mtls->eStrideOut;
216 }
217 }
218 }
219
220}
221
Jason Samsace3e012010-07-15 17:11:13 -0700222void ScriptC::runForEach(Context *rsc,
223 const Allocation * ain,
224 Allocation * aout,
225 const void * usr,
226 const RsScriptCall *sc)
Jason Samsc61346b2010-05-28 18:23:22 -0700227{
Jason Sams7bf29dd2010-07-19 15:38:19 -0700228 MTLaunchStruct mtls;
229 memset(&mtls, 0, sizeof(mtls));
Jason Samsc61346b2010-05-28 18:23:22 -0700230
Jason Sams7bf29dd2010-07-19 15:38:19 -0700231 if (ain) {
232 mtls.dimX = ain->getType()->getDimX();
233 mtls.dimY = ain->getType()->getDimY();
234 mtls.dimZ = ain->getType()->getDimZ();
235 //mtls.dimArray = ain->getType()->getDimArray();
236 } else if (aout) {
237 mtls.dimX = aout->getType()->getDimX();
238 mtls.dimY = aout->getType()->getDimY();
239 mtls.dimZ = aout->getType()->getDimZ();
240 //mtls.dimArray = aout->getType()->getDimArray();
241 } else {
242 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
243 return;
244 }
Jason Samsace3e012010-07-15 17:11:13 -0700245
246 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700247 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700248 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700249 rsAssert(sc->xStart < mtls.dimX);
250 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700251 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700252 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
253 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
254 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700255 }
256
257 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700258 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700259 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700260 rsAssert(sc->yStart < mtls.dimY);
261 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700262 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700263 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
264 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
265 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700266 }
267
Jason Sams7bf29dd2010-07-19 15:38:19 -0700268 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
269 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
270 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
271 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700272
273 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700274
275 setupScript(rsc);
276 Script * oldTLS = setTLS(this);
277
Jason Samsace3e012010-07-15 17:11:13 -0700278
Jason Sams7bf29dd2010-07-19 15:38:19 -0700279 mtls.rsc = rsc;
280 mtls.ain = ain;
281 mtls.aout = aout;
282 mtls.script = this;
283 mtls.usr = usr;
284 mtls.mSliceSize = 10;
285 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700286
Jason Sams7bf29dd2010-07-19 15:38:19 -0700287 mtls.ptrIn = NULL;
288 mtls.eStrideIn = 0;
289 if (ain) {
290 mtls.ptrIn = (const uint8_t *)ain->getPtr();
291 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700292 }
293
Jason Sams7bf29dd2010-07-19 15:38:19 -0700294 mtls.ptrOut = NULL;
295 mtls.eStrideOut = 0;
296 if (aout) {
297 mtls.ptrOut = (uint8_t *)aout->getPtr();
298 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
299 }
300
Jason Sams8d957fa2010-09-28 14:41:22 -0700301 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable && (mtls.dimY > 1)) {
Jason Sams18133402010-07-20 15:09:00 -0700302
303 //LOGE("launch 1");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700304 rsc->launchThreads(wc_xy, &mtls);
Jason Sams18133402010-07-20 15:09:00 -0700305 } else {
Jason Sams8d957fa2010-09-28 14:41:22 -0700306 //LOGE("launch 3");
Jason Sams18133402010-07-20 15:09:00 -0700307 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
308 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
309 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
310 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
311 mtls.dimX * mtls.dimY * z +
312 mtls.dimX * y;
313 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
314 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700315
Jason Sams18133402010-07-20 15:09:00 -0700316 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
317 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
318 xPtrIn += mtls.eStrideIn;
319 xPtrOut += mtls.eStrideOut;
320 }
Jason Samsace3e012010-07-15 17:11:13 -0700321 }
322 }
323 }
Jason Samsc61346b2010-05-28 18:23:22 -0700324 }
Jason Sams18133402010-07-20 15:09:00 -0700325
Jason Samsc61346b2010-05-28 18:23:22 -0700326 setTLS(oldTLS);
327}
328
Jason Sams22fa3712010-05-19 17:22:57 -0700329void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
330{
331 //LOGE("rsi_ScriptInvoke %i", slot);
332 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
333 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
334 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
335 return;
336 }
Jason Samsc61346b2010-05-28 18:23:22 -0700337 setupScript(rsc);
338 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700339
Jason Samsb9077f42010-09-22 15:57:41 -0700340 if (rsc->props.mLogScripts) {
341 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
342 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700343 ((void (*)(const void *, uint32_t))
344 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700345 if (rsc->props.mLogScripts) {
346 LOGV("%p ScriptC::Invoke complete", rsc);
347 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700348
Jason Samsc61346b2010-05-28 18:23:22 -0700349 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700350}
351
Jason Sams326e0dd2009-05-22 14:03:28 -0700352ScriptCState::ScriptCState()
353{
Stephen Hines01b7d292010-09-28 15:45:45 -0700354 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700355}
356
357ScriptCState::~ScriptCState()
358{
Stephen Hines01b7d292010-09-28 15:45:45 -0700359 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700360}
361
Stephen Hines01b7d292010-09-28 15:45:45 -0700362void ScriptCState::init(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700363{
Stephen Hines01b7d292010-09-28 15:45:45 -0700364 clear(rsc);
365}
366
367void ScriptCState::clear(Context *rsc)
368{
369 rsAssert(rsc);
Stephen Hines01b7d292010-09-28 15:45:45 -0700370 mScript.clear();
371 mScript.set(new ScriptC(rsc));
Jason Sams1f526332009-06-05 17:35:09 -0700372}
373
Jason Samsbe36bf32010-05-11 14:03:58 -0700374static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700375{
Jason Samsaeb094b2010-05-18 13:35:45 -0700376 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700377 ScriptC *s = (ScriptC *)pContext;
Jason Samsaeb094b2010-05-18 13:35:45 -0700378 sym = ScriptCState::lookupSymbol(name);
379 if (sym) {
380 return sym->mPtr;
381 }
382 sym = ScriptCState::lookupSymbolCL(name);
383 if (sym) {
384 return sym->mPtr;
385 }
Jason Sams8d957fa2010-09-28 14:41:22 -0700386 s->mEnviroment.mIsThreadable = false;
Jason Samsaeb094b2010-05-18 13:35:45 -0700387 sym = ScriptCState::lookupSymbolGL(name);
Jason Sams29df66f2009-07-16 15:08:06 -0700388 if (sym) {
389 return sym->mPtr;
390 }
Jason Sams29df66f2009-07-16 15:08:06 -0700391 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700392 return NULL;
393}
Jason Samsa4a54e42009-06-10 18:39:40 -0700394
Jason Sams8c6bc692009-09-16 15:04:38 -0700395void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
Jason Sams1f526332009-06-05 17:35:09 -0700396{
Jason Samsb9077f42010-09-22 15:57:41 -0700397 LOGV("%p ScriptCState::runCompiler ", rsc);
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700398 {
399 StopWatch compileTimer("RenderScript compile time");
400 s->mBccScript = bccCreateScript();
401 s->mEnviroment.mIsThreadable = true;
402 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
403 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
404 bccCompileScript(s->mBccScript);
405 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
406 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
407 }
Jason Samsb9077f42010-09-22 15:57:41 -0700408 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700409
Jason Sams8c6bc692009-09-16 15:04:38 -0700410 if (s->mProgram.mInit) {
411 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700412 }
413
Jason Sams8c880902010-06-15 12:15:57 -0700414 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
415 if(s->mEnviroment.mInvokeFunctionCount <= 0)
416 s->mEnviroment.mInvokeFunctions = NULL;
417 else {
418 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
419 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700420 }
421
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700422 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
423 if(s->mEnviroment.mFieldCount <= 0)
424 s->mEnviroment.mFieldAddress = NULL;
425 else {
426 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
427 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700428 s->initSlots();
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700429 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700430
Jason Sams8c6bc692009-09-16 15:04:38 -0700431 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
432 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700433 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700434 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700435
Jason Samsbe36bf32010-05-11 14:03:58 -0700436 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700437 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700438 BCCsizei pragmaCount;
439 BCCchar * str[pragmaMax];
440 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700441
Jason Sams10308932009-06-09 12:15:30 -0700442 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700443 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700444 if (!strcmp(str[ct], "version")) {
445 continue;
Jason Sams10308932009-06-09 12:15:30 -0700446 }
447
Jason Sams10308932009-06-09 12:15:30 -0700448 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700449 if (!strcmp(str[ct+1], "default")) {
450 continue;
451 }
452 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700453 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700454 continue;
455 }
Jason Sams10308932009-06-09 12:15:30 -0700456 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
457 }
458
459 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700460 if (!strcmp(str[ct+1], "default")) {
461 continue;
462 }
463 if (!strcmp(str[ct+1], "parent")) {
464 s->mEnviroment.mRaster.clear();
465 continue;
466 }
Jason Sams10308932009-06-09 12:15:30 -0700467 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
468 }
469
470 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700471 if (!strcmp(str[ct+1], "default")) {
472 continue;
473 }
474 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700475 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700476 continue;
477 }
Jason Sams10308932009-06-09 12:15:30 -0700478 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
479 }
480
Jason Samsb681c8a2009-09-28 18:12:56 -0700481 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700482 if (!strcmp(str[ct+1], "default")) {
483 continue;
484 }
485 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700486 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700487 continue;
488 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700489 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700490 }
491
492 }
493
Jason Samsd34b7252009-08-04 16:58:20 -0700494
Jason Sams10308932009-06-09 12:15:30 -0700495 } else {
496 // Deal with an error.
497 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700498}
499
Jason Sams8b2c0652009-08-12 17:54:11 -0700500
Joe Onorato57b79ce2009-08-09 22:57:44 -0700501
Jason Sams326e0dd2009-05-22 14:03:28 -0700502namespace android {
503namespace renderscript {
504
505void rsi_ScriptCBegin(Context * rsc)
506{
507 ScriptCState *ss = &rsc->mScriptC;
Stephen Hines01b7d292010-09-28 15:45:45 -0700508 ss->clear(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700509}
510
Jason Sams1f526332009-06-05 17:35:09 -0700511void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
512{
513 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800514
515 char *t = (char *)malloc(len + 1);
516 memcpy(t, text, len);
517 t[len] = 0;
518 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700519 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700520}
521
522
Jason Sams326e0dd2009-05-22 14:03:28 -0700523RsScript rsi_ScriptCCreate(Context * rsc)
524{
525 ScriptCState *ss = &rsc->mScriptC;
526
Stephen Hines01b7d292010-09-28 15:45:45 -0700527 ObjectBaseRef<ScriptC> s = ss->mScript.get();
528 ss->mScript.clear();
Jason Sams1f526332009-06-05 17:35:09 -0700529
Stephen Hines01b7d292010-09-28 15:45:45 -0700530 ss->runCompiler(rsc, s.get());
Jason Sams9397e302009-08-27 20:23:34 -0700531 s->incUserRef();
Stephen Hines01b7d292010-09-28 15:45:45 -0700532 ss->clear(rsc);
533 return s.get();
Jason Sams326e0dd2009-05-22 14:03:28 -0700534}
535
Jason Sams326e0dd2009-05-22 14:03:28 -0700536}
537}
538
539