blob: d961fed819a7dd62706e129016131035f538f105 [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 {
84 LOGE("ScriptC::setupScript, NULL var binding address.");
Jason Samsada7f272009-09-24 14:55:38 -070085 }
86 }
87}
88
Jason Samsce92d4b2010-05-17 14:55:34 -070089const Allocation *ScriptC::ptrToAllocation(const void *ptr) const
90{
91 if (!ptr) {
92 return NULL;
93 }
94 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
95 if (!mSlots[ct].get())
96 continue;
97 if (mSlots[ct]->getPtr() == ptr) {
98 return mSlots[ct].get();
99 }
100 }
101 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
102 return NULL;
103}
104
Jason Samsc61346b2010-05-28 18:23:22 -0700105Script * ScriptC::setTLS(Script *sc)
Jason Sams22fa3712010-05-19 17:22:57 -0700106{
107 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
108 pthread_getspecific(Context::gThreadTLSKey);
109 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700110 Script *old = tls->mScript;
111 tls->mScript = sc;
112 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700113}
114
Jason Sams326e0dd2009-05-22 14:03:28 -0700115
Jason Samsc61346b2010-05-28 18:23:22 -0700116void ScriptC::setupGLState(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700117{
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700118 if (mEnviroment.mFragmentStore.get()) {
119 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
120 }
121 if (mEnviroment.mFragment.get()) {
122 rsc->setFragment(mEnviroment.mFragment.get());
123 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700124 if (mEnviroment.mVertex.get()) {
125 rsc->setVertex(mEnviroment.mVertex.get());
126 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700127 if (mEnviroment.mRaster.get()) {
128 rsc->setRaster(mEnviroment.mRaster.get());
129 }
Jason Samsc61346b2010-05-28 18:23:22 -0700130}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700131
Jason Samsc61346b2010-05-28 18:23:22 -0700132uint32_t ScriptC::run(Context *rsc)
133{
134 if (mProgram.mRoot == NULL) {
135 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
136 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700137 }
Jason Samsc61346b2010-05-28 18:23:22 -0700138
139 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700140
Jason Sams2dca84d2009-12-09 11:05:45 -0800141 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700142 Script * oldTLS = setTLS(this);
Jason Samsb9077f42010-09-22 15:57:41 -0700143
144 if (rsc->props.mLogScripts) {
145 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
146 }
147
Jason Samsbe36bf32010-05-11 14:03:58 -0700148 ret = mProgram.mRoot();
Jason Samsb9077f42010-09-22 15:57:41 -0700149
150 if (rsc->props.mLogScripts) {
151 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
152 }
153
Jason Samsc61346b2010-05-28 18:23:22 -0700154 setTLS(oldTLS);
Jason Samse45ac6e2009-07-20 14:31:06 -0700155 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700156}
157
Jason Samsc61346b2010-05-28 18:23:22 -0700158
Jason Sams7bf29dd2010-07-19 15:38:19 -0700159typedef struct {
160 Context *rsc;
161 ScriptC *script;
162 const Allocation * ain;
163 Allocation * aout;
164 const void * usr;
165
166 uint32_t mSliceSize;
167 volatile int mSliceNum;
168
169 const uint8_t *ptrIn;
170 uint32_t eStrideIn;
171 uint8_t *ptrOut;
172 uint32_t eStrideOut;
173
174 uint32_t xStart;
175 uint32_t xEnd;
176 uint32_t yStart;
177 uint32_t yEnd;
178 uint32_t zStart;
179 uint32_t zEnd;
180 uint32_t arrayStart;
181 uint32_t arrayEnd;
182
183 uint32_t dimX;
184 uint32_t dimY;
185 uint32_t dimZ;
186 uint32_t dimArray;
187} MTLaunchStruct;
188typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
189
190static void wc_xy(void *usr, uint32_t idx)
191{
192 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700193
194 while (1) {
195 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
196 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
197 uint32_t yEnd = yStart + mtls->mSliceSize;
198 yEnd = rsMin(yEnd, mtls->yEnd);
199 if (yEnd <= yStart) {
200 return;
201 }
202
203 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
Jason Sams8d957fa2010-09-28 14:41:22 -0700204 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700205 for (uint32_t y = yStart; y < yEnd; y++) {
206 uint32_t offset = mtls->dimX * y;
207 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
208 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
209
210 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
211 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
212 xPtrIn += mtls->eStrideIn;
213 xPtrOut += mtls->eStrideOut;
214 }
215 }
216 }
217
218}
219
Jason Samsace3e012010-07-15 17:11:13 -0700220void ScriptC::runForEach(Context *rsc,
221 const Allocation * ain,
222 Allocation * aout,
223 const void * usr,
224 const RsScriptCall *sc)
Jason Samsc61346b2010-05-28 18:23:22 -0700225{
Jason Sams7bf29dd2010-07-19 15:38:19 -0700226 MTLaunchStruct mtls;
227 memset(&mtls, 0, sizeof(mtls));
Jason Samsc61346b2010-05-28 18:23:22 -0700228
Jason Sams7bf29dd2010-07-19 15:38:19 -0700229 if (ain) {
230 mtls.dimX = ain->getType()->getDimX();
231 mtls.dimY = ain->getType()->getDimY();
232 mtls.dimZ = ain->getType()->getDimZ();
233 //mtls.dimArray = ain->getType()->getDimArray();
234 } else if (aout) {
235 mtls.dimX = aout->getType()->getDimX();
236 mtls.dimY = aout->getType()->getDimY();
237 mtls.dimZ = aout->getType()->getDimZ();
238 //mtls.dimArray = aout->getType()->getDimArray();
239 } else {
240 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
241 return;
242 }
Jason Samsace3e012010-07-15 17:11:13 -0700243
244 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700245 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700246 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700247 rsAssert(sc->xStart < mtls.dimX);
248 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700249 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700250 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
251 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
252 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700253 }
254
255 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700256 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700257 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700258 rsAssert(sc->yStart < mtls.dimY);
259 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700260 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700261 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
262 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
263 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700264 }
265
Jason Sams7bf29dd2010-07-19 15:38:19 -0700266 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
267 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
268 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
269 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700270
271 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700272
273 setupScript(rsc);
274 Script * oldTLS = setTLS(this);
275
Jason Samsace3e012010-07-15 17:11:13 -0700276
Jason Sams7bf29dd2010-07-19 15:38:19 -0700277 mtls.rsc = rsc;
278 mtls.ain = ain;
279 mtls.aout = aout;
280 mtls.script = this;
281 mtls.usr = usr;
282 mtls.mSliceSize = 10;
283 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700284
Jason Sams7bf29dd2010-07-19 15:38:19 -0700285 mtls.ptrIn = NULL;
286 mtls.eStrideIn = 0;
287 if (ain) {
288 mtls.ptrIn = (const uint8_t *)ain->getPtr();
289 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700290 }
291
Jason Sams7bf29dd2010-07-19 15:38:19 -0700292 mtls.ptrOut = NULL;
293 mtls.eStrideOut = 0;
294 if (aout) {
295 mtls.ptrOut = (uint8_t *)aout->getPtr();
296 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
297 }
298
Jason Sams8d957fa2010-09-28 14:41:22 -0700299 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable && (mtls.dimY > 1)) {
Jason Sams18133402010-07-20 15:09:00 -0700300
301 //LOGE("launch 1");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700302 rsc->launchThreads(wc_xy, &mtls);
Jason Sams18133402010-07-20 15:09:00 -0700303 } else {
Jason Sams8d957fa2010-09-28 14:41:22 -0700304 //LOGE("launch 3");
Jason Sams18133402010-07-20 15:09:00 -0700305 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
306 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
307 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
308 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
309 mtls.dimX * mtls.dimY * z +
310 mtls.dimX * y;
311 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
312 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700313
Jason Sams18133402010-07-20 15:09:00 -0700314 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
315 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
316 xPtrIn += mtls.eStrideIn;
317 xPtrOut += mtls.eStrideOut;
318 }
Jason Samsace3e012010-07-15 17:11:13 -0700319 }
320 }
321 }
Jason Samsc61346b2010-05-28 18:23:22 -0700322 }
Jason Sams18133402010-07-20 15:09:00 -0700323
Jason Samsc61346b2010-05-28 18:23:22 -0700324 setTLS(oldTLS);
325}
326
Jason Sams22fa3712010-05-19 17:22:57 -0700327void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
328{
329 //LOGE("rsi_ScriptInvoke %i", slot);
330 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
331 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
332 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
333 return;
334 }
Jason Samsc61346b2010-05-28 18:23:22 -0700335 setupScript(rsc);
336 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700337
Jason Samsb9077f42010-09-22 15:57:41 -0700338 if (rsc->props.mLogScripts) {
339 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
340 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700341 ((void (*)(const void *, uint32_t))
342 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700343 if (rsc->props.mLogScripts) {
344 LOGV("%p ScriptC::Invoke complete", rsc);
345 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700346
Jason Samsc61346b2010-05-28 18:23:22 -0700347 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700348}
349
Jason Sams326e0dd2009-05-22 14:03:28 -0700350ScriptCState::ScriptCState()
351{
Stephen Hines01b7d292010-09-28 15:45:45 -0700352 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700353}
354
355ScriptCState::~ScriptCState()
356{
Stephen Hines01b7d292010-09-28 15:45:45 -0700357 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700358}
359
Stephen Hines01b7d292010-09-28 15:45:45 -0700360void ScriptCState::init(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700361{
Stephen Hines01b7d292010-09-28 15:45:45 -0700362 clear(rsc);
363}
364
365void ScriptCState::clear(Context *rsc)
366{
367 rsAssert(rsc);
Stephen Hines01b7d292010-09-28 15:45:45 -0700368 mScript.clear();
369 mScript.set(new ScriptC(rsc));
Jason Sams1f526332009-06-05 17:35:09 -0700370}
371
Jason Samsbe36bf32010-05-11 14:03:58 -0700372static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700373{
Jason Samsaeb094b2010-05-18 13:35:45 -0700374 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700375 ScriptC *s = (ScriptC *)pContext;
Jason Samsaeb094b2010-05-18 13:35:45 -0700376 sym = ScriptCState::lookupSymbol(name);
377 if (sym) {
378 return sym->mPtr;
379 }
380 sym = ScriptCState::lookupSymbolCL(name);
381 if (sym) {
382 return sym->mPtr;
383 }
Jason Sams8d957fa2010-09-28 14:41:22 -0700384 s->mEnviroment.mIsThreadable = false;
Jason Samsaeb094b2010-05-18 13:35:45 -0700385 sym = ScriptCState::lookupSymbolGL(name);
Jason Sams29df66f2009-07-16 15:08:06 -0700386 if (sym) {
387 return sym->mPtr;
388 }
Jason Sams29df66f2009-07-16 15:08:06 -0700389 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700390 return NULL;
391}
Jason Samsa4a54e42009-06-10 18:39:40 -0700392
Jason Sams8c6bc692009-09-16 15:04:38 -0700393void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
Jason Sams1f526332009-06-05 17:35:09 -0700394{
Jason Samsb9077f42010-09-22 15:57:41 -0700395 LOGV("%p ScriptCState::runCompiler ", rsc);
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700396 {
397 StopWatch compileTimer("RenderScript compile time");
398 s->mBccScript = bccCreateScript();
399 s->mEnviroment.mIsThreadable = true;
400 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
401 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
402 bccCompileScript(s->mBccScript);
403 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
404 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
405 }
Jason Samsb9077f42010-09-22 15:57:41 -0700406 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700407
Jason Sams8c6bc692009-09-16 15:04:38 -0700408 if (s->mProgram.mInit) {
409 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700410 }
411
Jason Sams8c880902010-06-15 12:15:57 -0700412 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
413 if(s->mEnviroment.mInvokeFunctionCount <= 0)
414 s->mEnviroment.mInvokeFunctions = NULL;
415 else {
416 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
417 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700418 }
419
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700420 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
421 if(s->mEnviroment.mFieldCount <= 0)
422 s->mEnviroment.mFieldAddress = NULL;
423 else {
424 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
425 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700426 s->initSlots();
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700427 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700428
Jason Sams8c6bc692009-09-16 15:04:38 -0700429 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
430 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700431 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700432 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700433
Jason Samsbe36bf32010-05-11 14:03:58 -0700434 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700435 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700436 BCCsizei pragmaCount;
437 BCCchar * str[pragmaMax];
438 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700439
Jason Sams10308932009-06-09 12:15:30 -0700440 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700441 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700442 if (!strcmp(str[ct], "version")) {
443 continue;
Jason Sams10308932009-06-09 12:15:30 -0700444 }
445
Jason Sams10308932009-06-09 12:15:30 -0700446 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700447 if (!strcmp(str[ct+1], "default")) {
448 continue;
449 }
450 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700451 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700452 continue;
453 }
Jason Sams10308932009-06-09 12:15:30 -0700454 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
455 }
456
457 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700458 if (!strcmp(str[ct+1], "default")) {
459 continue;
460 }
461 if (!strcmp(str[ct+1], "parent")) {
462 s->mEnviroment.mRaster.clear();
463 continue;
464 }
Jason Sams10308932009-06-09 12:15:30 -0700465 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
466 }
467
468 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700469 if (!strcmp(str[ct+1], "default")) {
470 continue;
471 }
472 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700473 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700474 continue;
475 }
Jason Sams10308932009-06-09 12:15:30 -0700476 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
477 }
478
Jason Samsb681c8a2009-09-28 18:12:56 -0700479 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700480 if (!strcmp(str[ct+1], "default")) {
481 continue;
482 }
483 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700484 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700485 continue;
486 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700487 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700488 }
489
490 }
491
Jason Samsd34b7252009-08-04 16:58:20 -0700492
Jason Sams10308932009-06-09 12:15:30 -0700493 } else {
494 // Deal with an error.
495 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700496}
497
Jason Sams8b2c0652009-08-12 17:54:11 -0700498
Joe Onorato57b79ce2009-08-09 22:57:44 -0700499
Jason Sams326e0dd2009-05-22 14:03:28 -0700500namespace android {
501namespace renderscript {
502
503void rsi_ScriptCBegin(Context * rsc)
504{
505 ScriptCState *ss = &rsc->mScriptC;
Stephen Hines01b7d292010-09-28 15:45:45 -0700506 ss->clear(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700507}
508
Jason Sams1f526332009-06-05 17:35:09 -0700509void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
510{
511 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800512
513 char *t = (char *)malloc(len + 1);
514 memcpy(t, text, len);
515 t[len] = 0;
516 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700517 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700518}
519
520
Jason Sams326e0dd2009-05-22 14:03:28 -0700521RsScript rsi_ScriptCCreate(Context * rsc)
522{
523 ScriptCState *ss = &rsc->mScriptC;
524
Stephen Hines01b7d292010-09-28 15:45:45 -0700525 ObjectBaseRef<ScriptC> s = ss->mScript.get();
526 ss->mScript.clear();
Jason Sams1f526332009-06-05 17:35:09 -0700527
Stephen Hines01b7d292010-09-28 15:45:45 -0700528 ss->runCompiler(rsc, s.get());
Jason Sams9397e302009-08-27 20:23:34 -0700529 s->incUserRef();
Jason Samse514b452009-09-25 14:51:22 -0700530 s->setContext(rsc);
Jason Sams10308932009-06-09 12:15:30 -0700531
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