blob: 975b7045cf33a0ac93560e03e73e202089053506 [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"
Jason Samsbe36bf32010-05-11 14:03:58 -070020#include "../../../external/llvm/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++) {
59 if (!mSlots[ct].get())
60 continue;
61 void *ptr = mSlots[ct]->getPtr();
62 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
63 //LOGE("setupScript %i %p = %p %p %i", ct, dest, ptr, mSlots[ct]->getType(), mSlots[ct]->getType()->getDimX());
64
65 //const uint32_t *p32 = (const uint32_t *)ptr;
66 //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) {
67 //LOGE(" %i = 0x%08x ", ct2, p32[ct2]);
68 //}
69
70 if (dest) {
71 *dest = ptr;
72 } else {
73 LOGE("ScriptC::setupScript, NULL var binding address.");
Jason Samsada7f272009-09-24 14:55:38 -070074 }
75 }
76}
77
Jason Samsce92d4b2010-05-17 14:55:34 -070078const Allocation *ScriptC::ptrToAllocation(const void *ptr) const
79{
80 if (!ptr) {
81 return NULL;
82 }
83 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
84 if (!mSlots[ct].get())
85 continue;
86 if (mSlots[ct]->getPtr() == ptr) {
87 return mSlots[ct].get();
88 }
89 }
90 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
91 return NULL;
92}
93
Jason Samsc61346b2010-05-28 18:23:22 -070094Script * ScriptC::setTLS(Script *sc)
Jason Sams22fa3712010-05-19 17:22:57 -070095{
96 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
97 pthread_getspecific(Context::gThreadTLSKey);
98 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -070099 Script *old = tls->mScript;
100 tls->mScript = sc;
101 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700102}
103
Jason Sams326e0dd2009-05-22 14:03:28 -0700104
Jason Samsc61346b2010-05-28 18:23:22 -0700105void ScriptC::setupGLState(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700106{
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700107 if (mEnviroment.mFragmentStore.get()) {
108 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
109 }
110 if (mEnviroment.mFragment.get()) {
111 rsc->setFragment(mEnviroment.mFragment.get());
112 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700113 if (mEnviroment.mVertex.get()) {
114 rsc->setVertex(mEnviroment.mVertex.get());
115 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700116 if (mEnviroment.mRaster.get()) {
117 rsc->setRaster(mEnviroment.mRaster.get());
118 }
Jason Samsc61346b2010-05-28 18:23:22 -0700119}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700120
Jason Samsc61346b2010-05-28 18:23:22 -0700121uint32_t ScriptC::run(Context *rsc)
122{
123 if (mProgram.mRoot == NULL) {
124 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
125 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700126 }
Jason Samsc61346b2010-05-28 18:23:22 -0700127
128 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700129
Jason Sams2dca84d2009-12-09 11:05:45 -0800130 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700131 Script * oldTLS = setTLS(this);
Jason Samsbe36bf32010-05-11 14:03:58 -0700132 //LOGE("ScriptC::run %p", mProgram.mRoot);
133 ret = mProgram.mRoot();
Jason Samsc61346b2010-05-28 18:23:22 -0700134 setTLS(oldTLS);
Jason Samsbe36bf32010-05-11 14:03:58 -0700135 //LOGE("ScriptC::run ret %i", ret);
Jason Samse45ac6e2009-07-20 14:31:06 -0700136 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700137}
138
Jason Samsc61346b2010-05-28 18:23:22 -0700139void ScriptC::runForEach(Context *rsc, const Allocation *ain, Allocation *aout,
140 uint32_t xStart, uint32_t yStart, uint32_t xEnd, uint32_t yEnd)
141{
142 LOGE("ScriptC::runForEach not implemented");
143}
144
145void ScriptC::runForEach(Context *rsc, const Allocation *ain, Allocation *aout, uint32_t xStart, uint32_t xEnd)
146{
147 uint32_t dimX = ain->getType()->getDimX();
148 rsAssert(xStart < dimX);
149 rsAssert(xEnd <= dimX);
150 rsAssert(ain->getType()->getDimY() == 0);
151 rsAssert(ain->getType()->getDimZ() == 0);
152
153 if (xStart >= dimX) xStart = dimX - 1;
154 if (xEnd >= dimX) xEnd = dimX - 1;
155 if (xStart > xEnd) return;
156
157 setupScript(rsc);
158 Script * oldTLS = setTLS(this);
159
160 typedef int (*rs_t)(const void *, void *, uint32_t);
161 const uint8_t *ptrIn = (const uint8_t *)ain->getPtr();
162 uint32_t strideIn = ain->getType()->getElementSizeBytes();
163
164 uint8_t *ptrOut = NULL;
165 uint32_t strideOut = 0;
166 if (aout) {
167 ptrOut = (uint8_t *)aout->getPtr();
168 strideOut = aout->getType()->getElementSizeBytes();
169 }
170
171 for (uint32_t ct=xStart; ct < xEnd; ct++) {
172 ((rs_t)mProgram.mRoot) (ptrIn + (strideIn * ct), ptrOut + (strideOut * ct), ct);
173 }
174
175 setTLS(oldTLS);
176}
177
178void ScriptC::runForEach(Context *rsc, const Allocation *ain, Allocation *aout)
179{
180 if (ain->getType()->getDimY()) {
181 runForEach(rsc, ain, aout, 0, 0, 0xffffffff, 0xffffffff);
182 } else {
183 runForEach(rsc, ain, aout, 0, 0xffffffff);
184 }
185}
186
187
Jason Sams22fa3712010-05-19 17:22:57 -0700188void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
189{
190 //LOGE("rsi_ScriptInvoke %i", slot);
191 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
192 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
193 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
194 return;
195 }
Jason Samsc61346b2010-05-28 18:23:22 -0700196 setupScript(rsc);
197 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700198
Jason Sams2a63bf62010-06-08 15:40:48 -0700199 ((void (*)(const void *, uint32_t))
200 mEnviroment.mInvokeFunctions[slot])(data, len);
201
Jason Samsc61346b2010-05-28 18:23:22 -0700202 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700203}
204
Jason Sams326e0dd2009-05-22 14:03:28 -0700205ScriptCState::ScriptCState()
206{
Jason Sams8c6bc692009-09-16 15:04:38 -0700207 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700208 clear();
209}
210
211ScriptCState::~ScriptCState()
212{
Jason Sams8c6bc692009-09-16 15:04:38 -0700213 delete mScript;
214 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700215}
216
217void ScriptCState::clear()
218{
Jason Sams8b2c0652009-08-12 17:54:11 -0700219 for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
220 mConstantBufferTypes[ct].clear();
Jason Sams90b36a82009-08-17 13:56:09 -0700221 mSlotWritable[ct] = false;
Jason Sams8b2c0652009-08-12 17:54:11 -0700222 }
Jason Samsefb8de12009-06-08 15:20:31 -0700223
Jason Sams8c6bc692009-09-16 15:04:38 -0700224 delete mScript;
Jason Samse514b452009-09-25 14:51:22 -0700225 mScript = new ScriptC(NULL);
Jason Sams1f526332009-06-05 17:35:09 -0700226}
227
Jason Samsbe36bf32010-05-11 14:03:58 -0700228static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700229{
Jason Samsaeb094b2010-05-18 13:35:45 -0700230 const ScriptCState::SymbolTable_t *sym;
231 sym = ScriptCState::lookupSymbol(name);
232 if (sym) {
233 return sym->mPtr;
234 }
235 sym = ScriptCState::lookupSymbolCL(name);
236 if (sym) {
237 return sym->mPtr;
238 }
239 sym = ScriptCState::lookupSymbolGL(name);
Jason Sams29df66f2009-07-16 15:08:06 -0700240 if (sym) {
241 return sym->mPtr;
242 }
Jason Sams29df66f2009-07-16 15:08:06 -0700243 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700244 return NULL;
245}
Jason Samsa4a54e42009-06-10 18:39:40 -0700246
Jason Sams8c6bc692009-09-16 15:04:38 -0700247void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
Jason Sams1f526332009-06-05 17:35:09 -0700248{
Jason Samsbe36bf32010-05-11 14:03:58 -0700249 LOGE("ScriptCState::runCompiler ");
Jason Sams1f526332009-06-05 17:35:09 -0700250
Jason Samsbe36bf32010-05-11 14:03:58 -0700251 s->mBccScript = bccCreateScript();
252 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
253 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, NULL);
Jason Samsbe36bf32010-05-11 14:03:58 -0700254 bccCompileScript(s->mBccScript);
Jason Samsbe36bf32010-05-11 14:03:58 -0700255 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
256 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
257 LOGE("root %p, init %p", s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700258
Jason Sams8c6bc692009-09-16 15:04:38 -0700259 if (s->mProgram.mInit) {
260 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700261 }
262
Jason Sams8c880902010-06-15 12:15:57 -0700263 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
264 if(s->mEnviroment.mInvokeFunctionCount <= 0)
265 s->mEnviroment.mInvokeFunctions = NULL;
266 else {
267 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
268 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700269 }
270
Jason Sams8c880902010-06-15 12:15:57 -0700271// s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t *)calloc(100, sizeof(void *));
272// BCCchar **labels = new char*[100];
273// bccGetFunctions(s->mBccScript, (BCCsizei *)&s->mEnviroment.mInvokeFunctionCount,
274// 100, (BCCchar **)labels);
275 //LOGE("func count %i", s->mEnviroment.mInvokeFunctionCount);
276// for (uint32_t i=0; i < s->mEnviroment.mInvokeFunctionCount; i++) {
277// BCCsizei length;
278// bccGetFunctionBinary(s->mBccScript, labels[i], (BCCvoid **)&(s->mEnviroment.mInvokeFunctions[i]), &length);
279 //LOGE("func %i %p", i, s->mEnviroment.mInvokeFunctions[i]);
280 // }
281
Jason Samsbe36bf32010-05-11 14:03:58 -0700282 s->mEnviroment.mFieldAddress = (void **)calloc(100, sizeof(void *));
283 bccGetExportVars(s->mBccScript, (BCCsizei *)&s->mEnviroment.mFieldCount,
284 100, s->mEnviroment.mFieldAddress);
285 //LOGE("var count %i", s->mEnviroment.mFieldCount);
286 for (uint32_t i=0; i < s->mEnviroment.mFieldCount; i++) {
287 //LOGE("var %i %p", i, s->mEnviroment.mFieldAddress[i]);
Jason Sams8c6bc692009-09-16 15:04:38 -0700288 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700289
Jason Sams8c6bc692009-09-16 15:04:38 -0700290 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
291 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700292 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700293 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700294
Jason Samsbe36bf32010-05-11 14:03:58 -0700295 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700296 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700297 BCCsizei pragmaCount;
298 BCCchar * str[pragmaMax];
299 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700300
Jason Sams10308932009-06-09 12:15:30 -0700301 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700302 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700303 if (!strcmp(str[ct], "version")) {
304 continue;
Jason Sams10308932009-06-09 12:15:30 -0700305 }
306
Jason Sams10308932009-06-09 12:15:30 -0700307 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700308 if (!strcmp(str[ct+1], "default")) {
309 continue;
310 }
311 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700312 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700313 continue;
314 }
Jason Sams10308932009-06-09 12:15:30 -0700315 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
316 }
317
318 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700319 if (!strcmp(str[ct+1], "default")) {
320 continue;
321 }
322 if (!strcmp(str[ct+1], "parent")) {
323 s->mEnviroment.mRaster.clear();
324 continue;
325 }
Jason Sams10308932009-06-09 12:15:30 -0700326 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
327 }
328
329 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700330 if (!strcmp(str[ct+1], "default")) {
331 continue;
332 }
333 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700334 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700335 continue;
336 }
Jason Sams10308932009-06-09 12:15:30 -0700337 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
338 }
339
Jason Samsb681c8a2009-09-28 18:12:56 -0700340 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700341 if (!strcmp(str[ct+1], "default")) {
342 continue;
343 }
344 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700345 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700346 continue;
347 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700348 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700349 }
350
351 }
352
Jason Samsd34b7252009-08-04 16:58:20 -0700353
Jason Sams10308932009-06-09 12:15:30 -0700354 } else {
355 // Deal with an error.
356 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700357}
358
Jason Sams8b2c0652009-08-12 17:54:11 -0700359
Joe Onorato57b79ce2009-08-09 22:57:44 -0700360
Jason Sams326e0dd2009-05-22 14:03:28 -0700361namespace android {
362namespace renderscript {
363
364void rsi_ScriptCBegin(Context * rsc)
365{
366 ScriptCState *ss = &rsc->mScriptC;
367 ss->clear();
368}
369
Jason Samsefb8de12009-06-08 15:20:31 -0700370void rsi_ScriptCSetScript(Context * rsc, void *vp)
Jason Sams326e0dd2009-05-22 14:03:28 -0700371{
Jason Sams8c6bc692009-09-16 15:04:38 -0700372 rsAssert(0);
373 //ScriptCState *ss = &rsc->mScriptC;
374 //ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp);
Jason Sams326e0dd2009-05-22 14:03:28 -0700375}
376
Jason Sams1f526332009-06-05 17:35:09 -0700377void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
378{
379 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800380
381 char *t = (char *)malloc(len + 1);
382 memcpy(t, text, len);
383 t[len] = 0;
384 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700385 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700386}
387
388
Jason Sams326e0dd2009-05-22 14:03:28 -0700389RsScript rsi_ScriptCCreate(Context * rsc)
390{
391 ScriptCState *ss = &rsc->mScriptC;
392
Jason Sams8c6bc692009-09-16 15:04:38 -0700393 ScriptC *s = ss->mScript;
394 ss->mScript = NULL;
Jason Sams1f526332009-06-05 17:35:09 -0700395
Jason Sams8c6bc692009-09-16 15:04:38 -0700396 ss->runCompiler(rsc, s);
Jason Sams9397e302009-08-27 20:23:34 -0700397 s->incUserRef();
Jason Samse514b452009-09-25 14:51:22 -0700398 s->setContext(rsc);
Jason Samsfa517192009-08-13 12:59:04 -0700399 for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
400 s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get());
Jason Sams90b36a82009-08-17 13:56:09 -0700401 s->mSlotWritable[ct] = ss->mSlotWritable[ct];
Jason Samsfa517192009-08-13 12:59:04 -0700402 }
Jason Sams10308932009-06-09 12:15:30 -0700403
Jason Samsfa517192009-08-13 12:59:04 -0700404 ss->clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700405 return s;
406}
407
Jason Sams326e0dd2009-05-22 14:03:28 -0700408}
409}
410
411