blob: ec7780e13a6307110577dc966e6e024dcabb4771 [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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080036ScriptC::ScriptC(Context *rsc) : Script(rsc) {
Jason Samsbe36bf32010-05-11 14:03:58 -070037 mBccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -070038 memset(&mProgram, 0, sizeof(mProgram));
Jason Sams326e0dd2009-05-22 14:03:28 -070039}
40
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080041ScriptC::~ScriptC() {
Jason Samsbe36bf32010-05-11 14:03:58 -070042 if (mBccScript) {
43 bccDeleteScript(mBccScript);
Jack Palevich1ef8b802009-05-28 15:53:04 -070044 }
Jason Samse402ed32009-11-03 11:25:42 -080045 free(mEnviroment.mScriptText);
46 mEnviroment.mScriptText = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070047}
48
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049void ScriptC::setupScript(Context *rsc) {
Jason Samsc61346b2010-05-28 18:23:22 -070050 setupGLState(rsc);
51 mEnviroment.mStartTimeMillis
52 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
53
Jason Samsbe36bf32010-05-11 14:03:58 -070054 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
Jason Sams900f1612010-09-16 18:18:29 -070055 if (mSlots[ct].get() && !mTypes[ct].get()) {
56 mTypes[ct].set(mSlots[ct]->getType());
57 }
58
59 if (!mTypes[ct].get())
Jason Samsbe36bf32010-05-11 14:03:58 -070060 continue;
Jason Sams900f1612010-09-16 18:18:29 -070061 void *ptr = NULL;
62 if (mSlots[ct].get()) {
63 ptr = mSlots[ct]->getPtr();
64 }
Jason Samsbe36bf32010-05-11 14:03:58 -070065 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
Jason Samsbe36bf32010-05-11 14:03:58 -070066
Jason Samsb9077f42010-09-22 15:57:41 -070067 if (rsc->props.mLogScripts) {
Jason Sams2fad7e42010-11-16 12:19:26 -080068 if (mSlots[ct].get() != NULL) {
69 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
70 } else {
71 LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=null", rsc, ct, dest, ptr);
72 }
Jason Samsb9077f42010-09-22 15:57:41 -070073 }
Jason Samsbe36bf32010-05-11 14:03:58 -070074
75 if (dest) {
76 *dest = ptr;
Jason Samsada7f272009-09-24 14:55:38 -070077 }
78 }
79}
80
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080081const Allocation *ScriptC::ptrToAllocation(const void *ptr) const {
Jason Samsce92d4b2010-05-17 14:55:34 -070082 if (!ptr) {
83 return NULL;
84 }
85 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
86 if (!mSlots[ct].get())
87 continue;
88 if (mSlots[ct]->getPtr() == ptr) {
89 return mSlots[ct].get();
90 }
91 }
92 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
93 return NULL;
94}
95
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080096Script * ScriptC::setTLS(Script *sc) {
Jason Sams22fa3712010-05-19 17:22:57 -070097 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
98 pthread_getspecific(Context::gThreadTLSKey);
99 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700100 Script *old = tls->mScript;
101 tls->mScript = sc;
102 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700103}
104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105void ScriptC::setupGLState(Context *rsc) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700106 if (mEnviroment.mFragmentStore.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800107 rsc->setProgramStore(mEnviroment.mFragmentStore.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700108 }
109 if (mEnviroment.mFragment.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800110 rsc->setProgramFragment(mEnviroment.mFragment.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700111 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700112 if (mEnviroment.mVertex.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800113 rsc->setProgramVertex(mEnviroment.mVertex.get());
Jason Sams8ce125b2009-06-17 16:52:59 -0700114 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700115 if (mEnviroment.mRaster.get()) {
Jason Sams60709252010-11-17 15:29:32 -0800116 rsc->setProgramRaster(mEnviroment.mRaster.get());
Jason Samsb681c8a2009-09-28 18:12:56 -0700117 }
Jason Samsc61346b2010-05-28 18:23:22 -0700118}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700119
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800120uint32_t ScriptC::run(Context *rsc) {
Jason Samsc61346b2010-05-28 18:23:22 -0700121 if (mProgram.mRoot == NULL) {
122 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
123 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700124 }
Jason Samsc61346b2010-05-28 18:23:22 -0700125
126 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700127
Jason Sams2dca84d2009-12-09 11:05:45 -0800128 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700129 Script * oldTLS = setTLS(this);
Jason Samsb9077f42010-09-22 15:57:41 -0700130
131 if (rsc->props.mLogScripts) {
132 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
133 }
134
Jason Samsbe36bf32010-05-11 14:03:58 -0700135 ret = mProgram.mRoot();
Jason Samsb9077f42010-09-22 15:57:41 -0700136
137 if (rsc->props.mLogScripts) {
138 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
139 }
140
Jason Samsc61346b2010-05-28 18:23:22 -0700141 setTLS(oldTLS);
Jason Samse45ac6e2009-07-20 14:31:06 -0700142 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700143}
144
Jason Sams7bf29dd2010-07-19 15:38:19 -0700145typedef struct {
146 Context *rsc;
147 ScriptC *script;
148 const Allocation * ain;
149 Allocation * aout;
150 const void * usr;
151
152 uint32_t mSliceSize;
153 volatile int mSliceNum;
154
155 const uint8_t *ptrIn;
156 uint32_t eStrideIn;
157 uint8_t *ptrOut;
158 uint32_t eStrideOut;
159
160 uint32_t xStart;
161 uint32_t xEnd;
162 uint32_t yStart;
163 uint32_t yEnd;
164 uint32_t zStart;
165 uint32_t zEnd;
166 uint32_t arrayStart;
167 uint32_t arrayEnd;
168
169 uint32_t dimX;
170 uint32_t dimY;
171 uint32_t dimZ;
172 uint32_t dimArray;
173} MTLaunchStruct;
174typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
175
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800176static void wc_xy(void *usr, uint32_t idx) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700177 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700178
179 while (1) {
180 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
181 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
182 uint32_t yEnd = yStart + mtls->mSliceSize;
183 yEnd = rsMin(yEnd, mtls->yEnd);
184 if (yEnd <= yStart) {
185 return;
186 }
187
188 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
Jason Sams8d957fa2010-09-28 14:41:22 -0700189 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700190 for (uint32_t y = yStart; y < yEnd; y++) {
191 uint32_t offset = mtls->dimX * y;
192 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
193 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
194
195 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
196 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
197 xPtrIn += mtls->eStrideIn;
198 xPtrOut += mtls->eStrideOut;
199 }
200 }
201 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700202}
203
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800204static void wc_x(void *usr, uint32_t idx) {
Jason Sams177f8442010-10-29 10:19:21 -0700205 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
206
207 while (1) {
208 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
209 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
210 uint32_t xEnd = xStart + mtls->mSliceSize;
211 xEnd = rsMin(xEnd, mtls->xEnd);
212 if (xEnd <= xStart) {
213 return;
214 }
215
216 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
217 //LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
218 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
219 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
220 for (uint32_t x = xStart; x < xEnd; x++) {
221 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
222 xPtrIn += mtls->eStrideIn;
223 xPtrOut += mtls->eStrideOut;
224 }
225 }
Jason Sams177f8442010-10-29 10:19:21 -0700226}
227
Jason Samsace3e012010-07-15 17:11:13 -0700228void ScriptC::runForEach(Context *rsc,
229 const Allocation * ain,
230 Allocation * aout,
231 const void * usr,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800232 const RsScriptCall *sc) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700233 MTLaunchStruct mtls;
234 memset(&mtls, 0, sizeof(mtls));
Jason Sams60709252010-11-17 15:29:32 -0800235 Context::PushState ps(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700236
Jason Sams7bf29dd2010-07-19 15:38:19 -0700237 if (ain) {
238 mtls.dimX = ain->getType()->getDimX();
239 mtls.dimY = ain->getType()->getDimY();
240 mtls.dimZ = ain->getType()->getDimZ();
241 //mtls.dimArray = ain->getType()->getDimArray();
242 } else if (aout) {
243 mtls.dimX = aout->getType()->getDimX();
244 mtls.dimY = aout->getType()->getDimY();
245 mtls.dimZ = aout->getType()->getDimZ();
246 //mtls.dimArray = aout->getType()->getDimArray();
247 } else {
248 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
249 return;
250 }
Jason Samsace3e012010-07-15 17:11:13 -0700251
252 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700253 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700254 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700255 rsAssert(sc->xStart < mtls.dimX);
256 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700257 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700258 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
259 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
260 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700261 }
262
263 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700264 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700265 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700266 rsAssert(sc->yStart < mtls.dimY);
267 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700268 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700269 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
270 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
271 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700272 }
273
Jason Sams7bf29dd2010-07-19 15:38:19 -0700274 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
275 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
276 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
277 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700278
279 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700280
281 setupScript(rsc);
282 Script * oldTLS = setTLS(this);
283
Jason Sams7bf29dd2010-07-19 15:38:19 -0700284 mtls.rsc = rsc;
285 mtls.ain = ain;
286 mtls.aout = aout;
287 mtls.script = this;
288 mtls.usr = usr;
289 mtls.mSliceSize = 10;
290 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700291
Jason Sams7bf29dd2010-07-19 15:38:19 -0700292 mtls.ptrIn = NULL;
293 mtls.eStrideIn = 0;
294 if (ain) {
295 mtls.ptrIn = (const uint8_t *)ain->getPtr();
296 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700297 }
298
Jason Sams7bf29dd2010-07-19 15:38:19 -0700299 mtls.ptrOut = NULL;
300 mtls.eStrideOut = 0;
301 if (aout) {
302 mtls.ptrOut = (uint8_t *)aout->getPtr();
303 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
304 }
305
Jason Sams177f8442010-10-29 10:19:21 -0700306 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) {
307 if (mtls.dimY > 1) {
308 rsc->launchThreads(wc_xy, &mtls);
309 } else {
310 rsc->launchThreads(wc_x, &mtls);
311 }
Jason Sams18133402010-07-20 15:09:00 -0700312
313 //LOGE("launch 1");
Jason Sams18133402010-07-20 15:09:00 -0700314 } else {
Jason Sams8d957fa2010-09-28 14:41:22 -0700315 //LOGE("launch 3");
Jason Sams18133402010-07-20 15:09:00 -0700316 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
317 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
318 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
319 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
320 mtls.dimX * mtls.dimY * z +
321 mtls.dimX * y;
322 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
323 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700324
Jason Sams18133402010-07-20 15:09:00 -0700325 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
326 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
327 xPtrIn += mtls.eStrideIn;
328 xPtrOut += mtls.eStrideOut;
329 }
Jason Samsace3e012010-07-15 17:11:13 -0700330 }
331 }
332 }
Jason Samsc61346b2010-05-28 18:23:22 -0700333 }
Jason Sams18133402010-07-20 15:09:00 -0700334
Jason Samsc61346b2010-05-28 18:23:22 -0700335 setTLS(oldTLS);
336}
337
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800338void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) {
Jason Sams22fa3712010-05-19 17:22:57 -0700339 //LOGE("rsi_ScriptInvoke %i", slot);
340 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
341 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
342 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
343 return;
344 }
Jason Samsc61346b2010-05-28 18:23:22 -0700345 setupScript(rsc);
346 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700347
Jason Samsb9077f42010-09-22 15:57:41 -0700348 if (rsc->props.mLogScripts) {
349 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
350 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700351 ((void (*)(const void *, uint32_t))
352 mEnviroment.mInvokeFunctions[slot])(data, len);
Jason Samsb9077f42010-09-22 15:57:41 -0700353 if (rsc->props.mLogScripts) {
354 LOGV("%p ScriptC::Invoke complete", rsc);
355 }
Jason Sams2a63bf62010-06-08 15:40:48 -0700356
Jason Samsc61346b2010-05-28 18:23:22 -0700357 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700358}
359
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800360ScriptCState::ScriptCState() {
Stephen Hines01b7d292010-09-28 15:45:45 -0700361 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700362}
363
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800364ScriptCState::~ScriptCState() {
Stephen Hines01b7d292010-09-28 15:45:45 -0700365 mScript.clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700366}
367
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800368void ScriptCState::init(Context *rsc) {
Stephen Hines01b7d292010-09-28 15:45:45 -0700369 clear(rsc);
370}
371
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800372void ScriptCState::clear(Context *rsc) {
Stephen Hines01b7d292010-09-28 15:45:45 -0700373 rsAssert(rsc);
Stephen Hines01b7d292010-09-28 15:45:45 -0700374 mScript.clear();
375 mScript.set(new ScriptC(rsc));
Jason Sams1f526332009-06-05 17:35:09 -0700376}
377
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800378static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700379 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700380 ScriptC *s = (ScriptC *)pContext;
Jason Samsaeb094b2010-05-18 13:35:45 -0700381 sym = ScriptCState::lookupSymbol(name);
Jason Sams6bfc1b92010-11-01 14:26:30 -0700382 if (!sym) {
383 sym = ScriptCState::lookupSymbolCL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700384 }
Jason Sams6bfc1b92010-11-01 14:26:30 -0700385 if (!sym) {
386 sym = ScriptCState::lookupSymbolGL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700387 }
Jason Sams29df66f2009-07-16 15:08:06 -0700388 if (sym) {
Jason Sams6bfc1b92010-11-01 14:26:30 -0700389 s->mEnviroment.mIsThreadable &= sym->threadable;
Jason Sams29df66f2009-07-16 15:08:06 -0700390 return sym->mPtr;
391 }
Jason Sams29df66f2009-07-16 15:08:06 -0700392 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700393 return NULL;
394}
Jason Samsa4a54e42009-06-10 18:39:40 -0700395
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700396extern const char rs_runtime_lib_bc[];
397extern unsigned rs_runtime_lib_bc_size;
398
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800399void ScriptCState::runCompiler(Context *rsc, ScriptC *s) {
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700400 {
401 StopWatch compileTimer("RenderScript compile time");
402 s->mBccScript = bccCreateScript();
403 s->mEnviroment.mIsThreadable = true;
404 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700405 //bccLinkBitcode(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -0700406 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
407 bccCompileScript(s->mBccScript);
408 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
409 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
410 }
Jason Samsb9077f42010-09-22 15:57:41 -0700411 LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700412
Jason Sams8c6bc692009-09-16 15:04:38 -0700413 if (s->mProgram.mInit) {
414 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700415 }
416
Jason Sams8c880902010-06-15 12:15:57 -0700417 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800418 if (s->mEnviroment.mInvokeFunctionCount <= 0)
Jason Sams8c880902010-06-15 12:15:57 -0700419 s->mEnviroment.mInvokeFunctions = NULL;
420 else {
421 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
422 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700423 }
424
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700425 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800426 if (s->mEnviroment.mFieldCount <= 0)
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700427 s->mEnviroment.mFieldAddress = NULL;
428 else {
429 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
430 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700431 s->initSlots();
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700432 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700433
Jason Sams8c6bc692009-09-16 15:04:38 -0700434 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
435 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700436 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700437 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700438
Jason Samsbe36bf32010-05-11 14:03:58 -0700439 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700440 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700441 BCCsizei pragmaCount;
442 BCCchar * str[pragmaMax];
443 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700444
Jason Sams10308932009-06-09 12:15:30 -0700445 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700446 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700447 if (!strcmp(str[ct], "version")) {
448 continue;
Jason Sams10308932009-06-09 12:15:30 -0700449 }
450
Jason Sams10308932009-06-09 12:15:30 -0700451 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700452 if (!strcmp(str[ct+1], "default")) {
453 continue;
454 }
455 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700456 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700457 continue;
458 }
Jason Sams10308932009-06-09 12:15:30 -0700459 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
460 }
461
462 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700463 if (!strcmp(str[ct+1], "default")) {
464 continue;
465 }
466 if (!strcmp(str[ct+1], "parent")) {
467 s->mEnviroment.mRaster.clear();
468 continue;
469 }
Jason Sams10308932009-06-09 12:15:30 -0700470 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
471 }
472
473 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700474 if (!strcmp(str[ct+1], "default")) {
475 continue;
476 }
477 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700478 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700479 continue;
480 }
Jason Sams10308932009-06-09 12:15:30 -0700481 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
482 }
483
Jason Samsb681c8a2009-09-28 18:12:56 -0700484 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700485 if (!strcmp(str[ct+1], "default")) {
486 continue;
487 }
488 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700489 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700490 continue;
491 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700492 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700493 }
494
495 }
496
Jason Samsd34b7252009-08-04 16:58:20 -0700497
Jason Sams10308932009-06-09 12:15:30 -0700498 } else {
499 // Deal with an error.
500 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700501}
502
503namespace android {
504namespace renderscript {
505
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800506void rsi_ScriptCBegin(Context * rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700507 ScriptCState *ss = &rsc->mScriptC;
Stephen Hines01b7d292010-09-28 15:45:45 -0700508 ss->clear(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700509}
510
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800511void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) {
Jason Sams1f526332009-06-05 17:35:09 -0700512 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800513
514 char *t = (char *)malloc(len + 1);
515 memcpy(t, text, len);
516 t[len] = 0;
517 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700518 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700519}
520
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800521RsScript rsi_ScriptCCreate(Context * rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700522 ScriptCState *ss = &rsc->mScriptC;
523
Jason Sams225afd32010-10-21 14:06:55 -0700524 ObjectBaseRef<ScriptC> s(ss->mScript);
Stephen Hines01b7d292010-09-28 15:45:45 -0700525 ss->mScript.clear();
Jason Sams225afd32010-10-21 14:06:55 -0700526 s->incUserRef();
Jason Sams1f526332009-06-05 17:35:09 -0700527
Stephen Hines01b7d292010-09-28 15:45:45 -0700528 ss->runCompiler(rsc, s.get());
Stephen Hines01b7d292010-09-28 15:45:45 -0700529 ss->clear(rsc);
530 return s.get();
Jason Sams326e0dd2009-05-22 14:03:28 -0700531}
532
Jason Sams326e0dd2009-05-22 14:03:28 -0700533}
534}