blob: 1f5ed7cf367d11bf6d91e25a67e9ffdc7397dbcb [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;
81 } else {
Jason Samsd7e54812010-10-10 17:58:25 -070082 if (rsc->props.mLogScripts) {
83 LOGV("ScriptC::setupScript, NULL var binding address.");
84 }
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 Sams177f8442010-10-29 10:19:21 -0700220static void wc_x(void *usr, uint32_t idx)
221{
222 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
223
224 while (1) {
225 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
226 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
227 uint32_t xEnd = xStart + mtls->mSliceSize;
228 xEnd = rsMin(xEnd, mtls->xEnd);
229 if (xEnd <= xStart) {
230 return;
231 }
232
233 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
234 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
235 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
236 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
237 for (uint32_t x = xStart; x < xEnd; x++) {
238 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
239 xPtrIn += mtls->eStrideIn;
240 xPtrOut += mtls->eStrideOut;
241 }
242 }
243
244}
245
Jason Samsace3e012010-07-15 17:11:13 -0700246void ScriptC::runForEach(Context *rsc,
247 const Allocation * ain,
248 Allocation * aout,
249 const void * usr,
250 const RsScriptCall *sc)
Jason Samsc61346b2010-05-28 18:23:22 -0700251{
Jason Sams7bf29dd2010-07-19 15:38:19 -0700252 MTLaunchStruct mtls;
253 memset(&mtls, 0, sizeof(mtls));
Jason Samsc61346b2010-05-28 18:23:22 -0700254
Jason Sams7bf29dd2010-07-19 15:38:19 -0700255 if (ain) {
256 mtls.dimX = ain->getType()->getDimX();
257 mtls.dimY = ain->getType()->getDimY();
258 mtls.dimZ = ain->getType()->getDimZ();
259 //mtls.dimArray = ain->getType()->getDimArray();
260 } else if (aout) {
261 mtls.dimX = aout->getType()->getDimX();
262 mtls.dimY = aout->getType()->getDimY();
263 mtls.dimZ = aout->getType()->getDimZ();
264 //mtls.dimArray = aout->getType()->getDimArray();
265 } else {
266 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
267 return;
268 }
Jason Samsace3e012010-07-15 17:11:13 -0700269
270 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700271 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700272 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700273 rsAssert(sc->xStart < mtls.dimX);
274 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700275 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700276 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
277 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
278 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700279 }
280
281 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700282 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700283 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700284 rsAssert(sc->yStart < mtls.dimY);
285 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700286 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700287 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
288 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
289 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700290 }
291
Jason Sams7bf29dd2010-07-19 15:38:19 -0700292 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
293 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
294 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
295 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700296
297 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700298
299 setupScript(rsc);
300 Script * oldTLS = setTLS(this);
301
Jason Samsace3e012010-07-15 17:11:13 -0700302
Jason Sams7bf29dd2010-07-19 15:38:19 -0700303 mtls.rsc = rsc;
304 mtls.ain = ain;
305 mtls.aout = aout;
306 mtls.script = this;
307 mtls.usr = usr;
308 mtls.mSliceSize = 10;
309 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700310
Jason Sams7bf29dd2010-07-19 15:38:19 -0700311 mtls.ptrIn = NULL;
312 mtls.eStrideIn = 0;
313 if (ain) {
314 mtls.ptrIn = (const uint8_t *)ain->getPtr();
315 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700316 }
317
Jason Sams7bf29dd2010-07-19 15:38:19 -0700318 mtls.ptrOut = NULL;
319 mtls.eStrideOut = 0;
320 if (aout) {
321 mtls.ptrOut = (uint8_t *)aout->getPtr();
322 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
323 }
324
Jason Sams177f8442010-10-29 10:19:21 -0700325 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) {
326 if (mtls.dimY > 1) {
327 rsc->launchThreads(wc_xy, &mtls);
328 } else {
329 rsc->launchThreads(wc_x, &mtls);
330 }
Jason Sams18133402010-07-20 15:09:00 -0700331
332 //LOGE("launch 1");
Jason Sams18133402010-07-20 15:09:00 -0700333 } else {
Jason Sams8d957fa2010-09-28 14:41:22 -0700334 //LOGE("launch 3");
Jason Sams18133402010-07-20 15:09:00 -0700335 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
336 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
337 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
338 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
339 mtls.dimX * mtls.dimY * z +
340 mtls.dimX * y;
341 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
342 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700343
Jason Sams18133402010-07-20 15:09:00 -0700344 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
345 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
346 xPtrIn += mtls.eStrideIn;
347 xPtrOut += mtls.eStrideOut;
348 }
Jason Samsace3e012010-07-15 17:11:13 -0700349 }
350 }
351 }
Jason Samsc61346b2010-05-28 18:23:22 -0700352 }
Jason Sams18133402010-07-20 15:09:00 -0700353
Jason Samsc61346b2010-05-28 18:23:22 -0700354 setTLS(oldTLS);
355}
356
Jason Sams22fa3712010-05-19 17:22:57 -0700357void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
358{
359 //LOGE("rsi_ScriptInvoke %i", slot);
360 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
361 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
362 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
363 return;
364 }
Jason Samsc61346b2010-05-28 18:23:22 -0700365 setupScript(rsc);
366 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700367
Jason Samsb9077f42010-09-22 15:57:41 -0700368 if (rsc->props.mLogScripts) {
369 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
370 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700371 ((void (*)(const void *, uint32_t))
372 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700373 if (rsc->props.mLogScripts) {
374 LOGV("%p ScriptC::Invoke complete", rsc);
375 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700376
Jason Samsc61346b2010-05-28 18:23:22 -0700377 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700378}
379
Jason Sams326e0dd2009-05-22 14:03:28 -0700380ScriptCState::ScriptCState()
381{
Stephen Hines01b7d292010-09-28 15:45:45 -0700382 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700383}
384
385ScriptCState::~ScriptCState()
386{
Stephen Hines01b7d292010-09-28 15:45:45 -0700387 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700388}
389
Stephen Hines01b7d292010-09-28 15:45:45 -0700390void ScriptCState::init(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700391{
Stephen Hines01b7d292010-09-28 15:45:45 -0700392 clear(rsc);
393}
394
395void ScriptCState::clear(Context *rsc)
396{
397 rsAssert(rsc);
Stephen Hines01b7d292010-09-28 15:45:45 -0700398 mScript.clear();
399 mScript.set(new ScriptC(rsc));
Jason Sams1f526332009-06-05 17:35:09 -0700400}
401
Jason Samsbe36bf32010-05-11 14:03:58 -0700402static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700403{
Jason Samsaeb094b2010-05-18 13:35:45 -0700404 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700405 ScriptC *s = (ScriptC *)pContext;
Jason Samsaeb094b2010-05-18 13:35:45 -0700406 sym = ScriptCState::lookupSymbol(name);
407 if (sym) {
408 return sym->mPtr;
409 }
410 sym = ScriptCState::lookupSymbolCL(name);
411 if (sym) {
412 return sym->mPtr;
413 }
Jason Sams8d957fa2010-09-28 14:41:22 -0700414 s->mEnviroment.mIsThreadable = false;
Jason Samsaeb094b2010-05-18 13:35:45 -0700415 sym = ScriptCState::lookupSymbolGL(name);
Jason Sams29df66f2009-07-16 15:08:06 -0700416 if (sym) {
417 return sym->mPtr;
418 }
Jason Sams29df66f2009-07-16 15:08:06 -0700419 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700420 return NULL;
421}
Jason Samsa4a54e42009-06-10 18:39:40 -0700422
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700423extern const char rs_runtime_lib_bc[];
424extern unsigned rs_runtime_lib_bc_size;
425
Jason Sams8c6bc692009-09-16 15:04:38 -0700426void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
Jason Sams1f526332009-06-05 17:35:09 -0700427{
Jason Samsb9077f42010-09-22 15:57:41 -0700428 LOGV("%p ScriptCState::runCompiler ", rsc);
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700429 {
430 StopWatch compileTimer("RenderScript compile time");
431 s->mBccScript = bccCreateScript();
432 s->mEnviroment.mIsThreadable = true;
433 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700434 //bccLinkBitcode(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700435 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
436 bccCompileScript(s->mBccScript);
437 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
438 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
439 }
Jason Samsb9077f42010-09-22 15:57:41 -0700440 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700441
Jason Sams8c6bc692009-09-16 15:04:38 -0700442 if (s->mProgram.mInit) {
443 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700444 }
445
Jason Sams8c880902010-06-15 12:15:57 -0700446 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
447 if(s->mEnviroment.mInvokeFunctionCount <= 0)
448 s->mEnviroment.mInvokeFunctions = NULL;
449 else {
450 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
451 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700452 }
453
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700454 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
455 if(s->mEnviroment.mFieldCount <= 0)
456 s->mEnviroment.mFieldAddress = NULL;
457 else {
458 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
459 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700460 s->initSlots();
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700461 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700462
Jason Sams8c6bc692009-09-16 15:04:38 -0700463 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
464 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700465 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700466 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700467
Jason Samsbe36bf32010-05-11 14:03:58 -0700468 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700469 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700470 BCCsizei pragmaCount;
471 BCCchar * str[pragmaMax];
472 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700473
Jason Sams10308932009-06-09 12:15:30 -0700474 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700475 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700476 if (!strcmp(str[ct], "version")) {
477 continue;
Jason Sams10308932009-06-09 12:15:30 -0700478 }
479
Jason Sams10308932009-06-09 12:15:30 -0700480 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700481 if (!strcmp(str[ct+1], "default")) {
482 continue;
483 }
484 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700485 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700486 continue;
487 }
Jason Sams10308932009-06-09 12:15:30 -0700488 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
489 }
490
491 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700492 if (!strcmp(str[ct+1], "default")) {
493 continue;
494 }
495 if (!strcmp(str[ct+1], "parent")) {
496 s->mEnviroment.mRaster.clear();
497 continue;
498 }
Jason Sams10308932009-06-09 12:15:30 -0700499 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
500 }
501
502 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700503 if (!strcmp(str[ct+1], "default")) {
504 continue;
505 }
506 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700507 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700508 continue;
509 }
Jason Sams10308932009-06-09 12:15:30 -0700510 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
511 }
512
Jason Samsb681c8a2009-09-28 18:12:56 -0700513 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700514 if (!strcmp(str[ct+1], "default")) {
515 continue;
516 }
517 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700518 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700519 continue;
520 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700521 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700522 }
523
524 }
525
Jason Samsd34b7252009-08-04 16:58:20 -0700526
Jason Sams10308932009-06-09 12:15:30 -0700527 } else {
528 // Deal with an error.
529 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700530}
531
Jason Sams8b2c0652009-08-12 17:54:11 -0700532
Joe Onorato57b79ce2009-08-09 22:57:44 -0700533
Jason Sams326e0dd2009-05-22 14:03:28 -0700534namespace android {
535namespace renderscript {
536
537void rsi_ScriptCBegin(Context * rsc)
538{
539 ScriptCState *ss = &rsc->mScriptC;
Stephen Hines01b7d292010-09-28 15:45:45 -0700540 ss->clear(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700541}
542
Jason Sams1f526332009-06-05 17:35:09 -0700543void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
544{
545 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800546
547 char *t = (char *)malloc(len + 1);
548 memcpy(t, text, len);
549 t[len] = 0;
550 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700551 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700552}
553
554
Jason Sams326e0dd2009-05-22 14:03:28 -0700555RsScript rsi_ScriptCCreate(Context * rsc)
556{
557 ScriptCState *ss = &rsc->mScriptC;
558
Jason Sams225afd32010-10-21 14:06:55 -0700559 ObjectBaseRef<ScriptC> s(ss->mScript);
Stephen Hines01b7d292010-09-28 15:45:45 -0700560 ss->mScript.clear();
Jason Sams225afd32010-10-21 14:06:55 -0700561 s->incUserRef();
Jason Sams1f526332009-06-05 17:35:09 -0700562
Stephen Hines01b7d292010-09-28 15:45:45 -0700563 ss->runCompiler(rsc, s.get());
Stephen Hines01b7d292010-09-28 15:45:45 -0700564 ss->clear(rsc);
565 return s.get();
Jason Sams326e0dd2009-05-22 14:03:28 -0700566}
567
Jason Sams326e0dd2009-05-22 14:03:28 -0700568}
569}