blob: 4b484b1931663ac17cbab7723e359f0a281d18e8 [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"
Jack Palevich1ef8b802009-05-28 15:53:04 -070022
Jason Sams1aa5a4e2009-06-22 17:15:15 -070023#include <GLES/gl.h>
24#include <GLES/glext.h>
25
Jason Sams326e0dd2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Samse5769102009-06-19 16:03:18 -070029#define GET_TLS() Context::ScriptTLSStruct * tls = \
30 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
31 Context * rsc = tls->mContext; \
32 ScriptC * sc = (ScriptC *) tls->mScript
33
Jason Sams326e0dd2009-05-22 14:03:28 -070034
Jason Samse514b452009-09-25 14:51:22 -070035ScriptC::ScriptC(Context *rsc) : Script(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070036{
Jason Samsf2649a92009-09-25 16:37:33 -070037 mAllocFile = __FILE__;
38 mAllocLine = __LINE__;
Jason Samsbe36bf32010-05-11 14:03:58 -070039 mBccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -070040 memset(&mProgram, 0, sizeof(mProgram));
Jason Sams326e0dd2009-05-22 14:03:28 -070041}
42
43ScriptC::~ScriptC()
44{
Jason Samsbe36bf32010-05-11 14:03:58 -070045 if (mBccScript) {
46 bccDeleteScript(mBccScript);
Jack Palevich1ef8b802009-05-28 15:53:04 -070047 }
Jason Samse402ed32009-11-03 11:25:42 -080048 free(mEnviroment.mScriptText);
49 mEnviroment.mScriptText = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070050}
51
Jason Samsc61346b2010-05-28 18:23:22 -070052void ScriptC::setupScript(Context *rsc)
Jason Samsada7f272009-09-24 14:55:38 -070053{
Jason Samsc61346b2010-05-28 18:23:22 -070054 setupGLState(rsc);
55 mEnviroment.mStartTimeMillis
56 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
57
Jason Samsbe36bf32010-05-11 14:03:58 -070058 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
Jason Sams900f1612010-09-16 18:18:29 -070059 if (mSlots[ct].get() && !mTypes[ct].get()) {
60 mTypes[ct].set(mSlots[ct]->getType());
61 }
62
63 if (!mTypes[ct].get())
Jason Samsbe36bf32010-05-11 14:03:58 -070064 continue;
Jason Sams900f1612010-09-16 18:18:29 -070065 void *ptr = NULL;
66 if (mSlots[ct].get()) {
67 ptr = mSlots[ct]->getPtr();
68 }
Jason Samsbe36bf32010-05-11 14:03:58 -070069 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
Jason Samsbe36bf32010-05-11 14:03:58 -070070
Jason Samsb9077f42010-09-22 15:57:41 -070071 if (rsc->props.mLogScripts) {
72 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
73
74 //const uint32_t *p32 = (const uint32_t *)ptr;
75 //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) {
76 //LOGE(" %i = 0x%08x ", ct2, p32[ct2]);
77 //}
78 }
Jason Samsbe36bf32010-05-11 14:03:58 -070079
80 if (dest) {
81 *dest = ptr;
82 } else {
83 LOGE("ScriptC::setupScript, NULL var binding address.");
Jason Samsada7f272009-09-24 14:55:38 -070084 }
85 }
86}
87
Jason Samsce92d4b2010-05-17 14:55:34 -070088const Allocation *ScriptC::ptrToAllocation(const void *ptr) const
89{
90 if (!ptr) {
91 return NULL;
92 }
93 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
94 if (!mSlots[ct].get())
95 continue;
96 if (mSlots[ct]->getPtr() == ptr) {
97 return mSlots[ct].get();
98 }
99 }
100 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
101 return NULL;
102}
103
Jason Samsc61346b2010-05-28 18:23:22 -0700104Script * ScriptC::setTLS(Script *sc)
Jason Sams22fa3712010-05-19 17:22:57 -0700105{
106 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
107 pthread_getspecific(Context::gThreadTLSKey);
108 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700109 Script *old = tls->mScript;
110 tls->mScript = sc;
111 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700112}
113
Jason Sams326e0dd2009-05-22 14:03:28 -0700114
Jason Samsc61346b2010-05-28 18:23:22 -0700115void ScriptC::setupGLState(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700116{
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700117 if (mEnviroment.mFragmentStore.get()) {
118 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
119 }
120 if (mEnviroment.mFragment.get()) {
121 rsc->setFragment(mEnviroment.mFragment.get());
122 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700123 if (mEnviroment.mVertex.get()) {
124 rsc->setVertex(mEnviroment.mVertex.get());
125 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700126 if (mEnviroment.mRaster.get()) {
127 rsc->setRaster(mEnviroment.mRaster.get());
128 }
Jason Samsc61346b2010-05-28 18:23:22 -0700129}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700130
Jason Samsc61346b2010-05-28 18:23:22 -0700131uint32_t ScriptC::run(Context *rsc)
132{
133 if (mProgram.mRoot == NULL) {
134 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
135 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700136 }
Jason Samsc61346b2010-05-28 18:23:22 -0700137
138 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700139
Jason Sams2dca84d2009-12-09 11:05:45 -0800140 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700141 Script * oldTLS = setTLS(this);
Jason Samsb9077f42010-09-22 15:57:41 -0700142
143 if (rsc->props.mLogScripts) {
144 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
145 }
146
Jason Samsbe36bf32010-05-11 14:03:58 -0700147 ret = mProgram.mRoot();
Jason Samsb9077f42010-09-22 15:57:41 -0700148
149 if (rsc->props.mLogScripts) {
150 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
151 }
152
Jason Samsc61346b2010-05-28 18:23:22 -0700153 setTLS(oldTLS);
Jason Samse45ac6e2009-07-20 14:31:06 -0700154 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700155}
156
Jason Samsc61346b2010-05-28 18:23:22 -0700157
Jason Sams7bf29dd2010-07-19 15:38:19 -0700158typedef struct {
159 Context *rsc;
160 ScriptC *script;
161 const Allocation * ain;
162 Allocation * aout;
163 const void * usr;
164
165 uint32_t mSliceSize;
166 volatile int mSliceNum;
167
168 const uint8_t *ptrIn;
169 uint32_t eStrideIn;
170 uint8_t *ptrOut;
171 uint32_t eStrideOut;
172
173 uint32_t xStart;
174 uint32_t xEnd;
175 uint32_t yStart;
176 uint32_t yEnd;
177 uint32_t zStart;
178 uint32_t zEnd;
179 uint32_t arrayStart;
180 uint32_t arrayEnd;
181
182 uint32_t dimX;
183 uint32_t dimY;
184 uint32_t dimZ;
185 uint32_t dimArray;
186} MTLaunchStruct;
187typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
188
189static void wc_xy(void *usr, uint32_t idx)
190{
191 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700192
193 while (1) {
194 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
195 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
196 uint32_t yEnd = yStart + mtls->mSliceSize;
197 yEnd = rsMin(yEnd, mtls->yEnd);
198 if (yEnd <= yStart) {
199 return;
200 }
201
202 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
203
204 for (uint32_t y = yStart; y < yEnd; y++) {
205 uint32_t offset = mtls->dimX * y;
206 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
207 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
208
209 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
210 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
211 xPtrIn += mtls->eStrideIn;
212 xPtrOut += mtls->eStrideOut;
213 }
214 }
215 }
216
217}
218
Jason Samsace3e012010-07-15 17:11:13 -0700219void ScriptC::runForEach(Context *rsc,
220 const Allocation * ain,
221 Allocation * aout,
222 const void * usr,
223 const RsScriptCall *sc)
Jason Samsc61346b2010-05-28 18:23:22 -0700224{
Jason Sams7bf29dd2010-07-19 15:38:19 -0700225 MTLaunchStruct mtls;
226 memset(&mtls, 0, sizeof(mtls));
Jason Samsc61346b2010-05-28 18:23:22 -0700227
Jason Sams7bf29dd2010-07-19 15:38:19 -0700228 if (ain) {
229 mtls.dimX = ain->getType()->getDimX();
230 mtls.dimY = ain->getType()->getDimY();
231 mtls.dimZ = ain->getType()->getDimZ();
232 //mtls.dimArray = ain->getType()->getDimArray();
233 } else if (aout) {
234 mtls.dimX = aout->getType()->getDimX();
235 mtls.dimY = aout->getType()->getDimY();
236 mtls.dimZ = aout->getType()->getDimZ();
237 //mtls.dimArray = aout->getType()->getDimArray();
238 } else {
239 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
240 return;
241 }
Jason Samsace3e012010-07-15 17:11:13 -0700242
243 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700244 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700245 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700246 rsAssert(sc->xStart < mtls.dimX);
247 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700248 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700249 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
250 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
251 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700252 }
253
254 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700255 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700256 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700257 rsAssert(sc->yStart < mtls.dimY);
258 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700259 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700260 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
261 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
262 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700263 }
264
Jason Sams7bf29dd2010-07-19 15:38:19 -0700265 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
266 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
267 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
268 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700269
270 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700271
272 setupScript(rsc);
273 Script * oldTLS = setTLS(this);
274
Jason Samsace3e012010-07-15 17:11:13 -0700275
Jason Sams7bf29dd2010-07-19 15:38:19 -0700276 mtls.rsc = rsc;
277 mtls.ain = ain;
278 mtls.aout = aout;
279 mtls.script = this;
280 mtls.usr = usr;
281 mtls.mSliceSize = 10;
282 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700283
Jason Sams7bf29dd2010-07-19 15:38:19 -0700284 mtls.ptrIn = NULL;
285 mtls.eStrideIn = 0;
286 if (ain) {
287 mtls.ptrIn = (const uint8_t *)ain->getPtr();
288 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700289 }
290
Jason Sams7bf29dd2010-07-19 15:38:19 -0700291 mtls.ptrOut = NULL;
292 mtls.eStrideOut = 0;
293 if (aout) {
294 mtls.ptrOut = (uint8_t *)aout->getPtr();
295 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
296 }
297
298
Jason Samsdd663fa2010-08-11 13:26:28 -0700299 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable &&
Jason Sams18133402010-07-20 15:09:00 -0700300 ((mtls.dimY * mtls.dimZ * mtls.dimArray) > 1)) {
301
302 //LOGE("launch 1");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700303 rsc->launchThreads(wc_xy, &mtls);
Jason Sams18133402010-07-20 15:09:00 -0700304 //LOGE("launch 2");
305 } else {
306 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
307 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
308 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
309 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
310 mtls.dimX * mtls.dimY * z +
311 mtls.dimX * y;
312 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
313 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700314
Jason Sams18133402010-07-20 15:09:00 -0700315 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
316 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
317 xPtrIn += mtls.eStrideIn;
318 xPtrOut += mtls.eStrideOut;
319 }
Jason Samsace3e012010-07-15 17:11:13 -0700320 }
321 }
322 }
Jason Samsc61346b2010-05-28 18:23:22 -0700323 }
Jason Sams18133402010-07-20 15:09:00 -0700324
Jason Samsc61346b2010-05-28 18:23:22 -0700325 setTLS(oldTLS);
326}
327
Jason Sams22fa3712010-05-19 17:22:57 -0700328void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
329{
330 //LOGE("rsi_ScriptInvoke %i", slot);
331 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
332 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
333 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
334 return;
335 }
Jason Samsc61346b2010-05-28 18:23:22 -0700336 setupScript(rsc);
337 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700338
Jason Samsb9077f42010-09-22 15:57:41 -0700339 if (rsc->props.mLogScripts) {
340 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
341 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700342 ((void (*)(const void *, uint32_t))
343 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700344 if (rsc->props.mLogScripts) {
345 LOGV("%p ScriptC::Invoke complete", rsc);
346 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700347
Jason Samsc61346b2010-05-28 18:23:22 -0700348 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700349}
350
Jason Sams326e0dd2009-05-22 14:03:28 -0700351ScriptCState::ScriptCState()
352{
Jason Sams8c6bc692009-09-16 15:04:38 -0700353 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700354 clear();
355}
356
357ScriptCState::~ScriptCState()
358{
Jason Sams8c6bc692009-09-16 15:04:38 -0700359 delete mScript;
360 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700361}
362
363void ScriptCState::clear()
364{
Jason Sams8b2c0652009-08-12 17:54:11 -0700365 for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
366 mConstantBufferTypes[ct].clear();
Jason Sams90b36a82009-08-17 13:56:09 -0700367 mSlotWritable[ct] = false;
Jason Sams8b2c0652009-08-12 17:54:11 -0700368 }
Jason Samsefb8de12009-06-08 15:20:31 -0700369
Jason Sams8c6bc692009-09-16 15:04:38 -0700370 delete mScript;
Jason Samse514b452009-09-25 14:51:22 -0700371 mScript = new ScriptC(NULL);
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 }
Jason Samsdd663fa2010-08-11 13:26:28 -0700382 s->mEnviroment.mIsThreadable = false;
Jason Samsaeb094b2010-05-18 13:35:45 -0700383 sym = ScriptCState::lookupSymbolCL(name);
384 if (sym) {
385 return sym->mPtr;
386 }
387 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);
Jason Sams1f526332009-06-05 17:35:09 -0700398
Jason Samsbe36bf32010-05-11 14:03:58 -0700399 s->mBccScript = bccCreateScript();
Jason Samsdd663fa2010-08-11 13:26:28 -0700400 s->mEnviroment.mIsThreadable = true;
Jason Samsbe36bf32010-05-11 14:03:58 -0700401 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
Jason Samsdd663fa2010-08-11 13:26:28 -0700402 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
Jason Samsbe36bf32010-05-11 14:03:58 -0700403 bccCompileScript(s->mBccScript);
Jason Samsbe36bf32010-05-11 14:03:58 -0700404 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
405 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
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);
426 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700427
Jason Sams8c6bc692009-09-16 15:04:38 -0700428 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
429 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700430 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700431 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700432
Jason Samsbe36bf32010-05-11 14:03:58 -0700433 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700434 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700435 BCCsizei pragmaCount;
436 BCCchar * str[pragmaMax];
437 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700438
Jason Sams10308932009-06-09 12:15:30 -0700439 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700440 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700441 if (!strcmp(str[ct], "version")) {
442 continue;
Jason Sams10308932009-06-09 12:15:30 -0700443 }
444
Jason Sams10308932009-06-09 12:15:30 -0700445 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700446 if (!strcmp(str[ct+1], "default")) {
447 continue;
448 }
449 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700450 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700451 continue;
452 }
Jason Sams10308932009-06-09 12:15:30 -0700453 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
454 }
455
456 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700457 if (!strcmp(str[ct+1], "default")) {
458 continue;
459 }
460 if (!strcmp(str[ct+1], "parent")) {
461 s->mEnviroment.mRaster.clear();
462 continue;
463 }
Jason Sams10308932009-06-09 12:15:30 -0700464 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
465 }
466
467 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700468 if (!strcmp(str[ct+1], "default")) {
469 continue;
470 }
471 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700472 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700473 continue;
474 }
Jason Sams10308932009-06-09 12:15:30 -0700475 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
476 }
477
Jason Samsb681c8a2009-09-28 18:12:56 -0700478 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700479 if (!strcmp(str[ct+1], "default")) {
480 continue;
481 }
482 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700483 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700484 continue;
485 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700486 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700487 }
488
489 }
490
Jason Samsd34b7252009-08-04 16:58:20 -0700491
Jason Sams10308932009-06-09 12:15:30 -0700492 } else {
493 // Deal with an error.
494 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700495}
496
Jason Sams8b2c0652009-08-12 17:54:11 -0700497
Joe Onorato57b79ce2009-08-09 22:57:44 -0700498
Jason Sams326e0dd2009-05-22 14:03:28 -0700499namespace android {
500namespace renderscript {
501
502void rsi_ScriptCBegin(Context * rsc)
503{
504 ScriptCState *ss = &rsc->mScriptC;
505 ss->clear();
506}
507
Jason Sams1f526332009-06-05 17:35:09 -0700508void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
509{
510 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800511
512 char *t = (char *)malloc(len + 1);
513 memcpy(t, text, len);
514 t[len] = 0;
515 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700516 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700517}
518
519
Jason Sams326e0dd2009-05-22 14:03:28 -0700520RsScript rsi_ScriptCCreate(Context * rsc)
521{
522 ScriptCState *ss = &rsc->mScriptC;
523
Jason Sams8c6bc692009-09-16 15:04:38 -0700524 ScriptC *s = ss->mScript;
525 ss->mScript = NULL;
Jason Sams1f526332009-06-05 17:35:09 -0700526
Jason Sams8c6bc692009-09-16 15:04:38 -0700527 ss->runCompiler(rsc, s);
Jason Sams9397e302009-08-27 20:23:34 -0700528 s->incUserRef();
Jason Samse514b452009-09-25 14:51:22 -0700529 s->setContext(rsc);
Jason Samsfa517192009-08-13 12:59:04 -0700530 for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
531 s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get());
Jason Sams90b36a82009-08-17 13:56:09 -0700532 s->mSlotWritable[ct] = ss->mSlotWritable[ct];
Jason Samsfa517192009-08-13 12:59:04 -0700533 }
Jason Sams10308932009-06-09 12:15:30 -0700534
Jason Samsfa517192009-08-13 12:59:04 -0700535 ss->clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700536 return s;
537}
538
Jason Sams326e0dd2009-05-22 14:03:28 -0700539}
540}
541
542