blob: 3217e64b17b5b04769a48aa35e2ad539893d7125 [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 Samsada7f272009-09-24 14:55:38 -070052void ScriptC::setupScript()
53{
Jason Samsbe36bf32010-05-11 14:03:58 -070054 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
55 if (!mSlots[ct].get())
56 continue;
57 void *ptr = mSlots[ct]->getPtr();
58 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
59 //LOGE("setupScript %i %p = %p %p %i", ct, dest, ptr, mSlots[ct]->getType(), mSlots[ct]->getType()->getDimX());
60
61 //const uint32_t *p32 = (const uint32_t *)ptr;
62 //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) {
63 //LOGE(" %i = 0x%08x ", ct2, p32[ct2]);
64 //}
65
66 if (dest) {
67 *dest = ptr;
68 } else {
69 LOGE("ScriptC::setupScript, NULL var binding address.");
Jason Samsada7f272009-09-24 14:55:38 -070070 }
71 }
72}
73
Jason Samsce92d4b2010-05-17 14:55:34 -070074const Allocation *ScriptC::ptrToAllocation(const void *ptr) const
75{
76 if (!ptr) {
77 return NULL;
78 }
79 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
80 if (!mSlots[ct].get())
81 continue;
82 if (mSlots[ct]->getPtr() == ptr) {
83 return mSlots[ct].get();
84 }
85 }
86 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
87 return NULL;
88}
89
Jason Sams22fa3712010-05-19 17:22:57 -070090void ScriptC::setTLS()
91{
92 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
93 pthread_getspecific(Context::gThreadTLSKey);
94 rsAssert(tls);
95 tls->mScript = this;
96}
97
98void ScriptC::clearTLS()
99{
100 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
101 pthread_getspecific(Context::gThreadTLSKey);
102 rsAssert(tls);
103 tls->mScript = NULL;
104}
Jason Sams326e0dd2009-05-22 14:03:28 -0700105
Jason Sams2dca84d2009-12-09 11:05:45 -0800106uint32_t ScriptC::run(Context *rsc, uint32_t launchIndex)
Jason Sams326e0dd2009-05-22 14:03:28 -0700107{
Jason Samsbe36bf32010-05-11 14:03:58 -0700108 if (mProgram.mRoot == NULL) {
Jason Samsa2cf7552010-03-03 13:03:18 -0800109 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
110 return 0;
111 }
112
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700113 if (mEnviroment.mFragmentStore.get()) {
114 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
115 }
116 if (mEnviroment.mFragment.get()) {
117 rsc->setFragment(mEnviroment.mFragment.get());
118 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700119 if (mEnviroment.mVertex.get()) {
120 rsc->setVertex(mEnviroment.mVertex.get());
121 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700122 if (mEnviroment.mRaster.get()) {
123 rsc->setRaster(mEnviroment.mRaster.get());
124 }
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700125
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700126 if (launchIndex == 0) {
127 mEnviroment.mStartTimeMillis
128 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
129 }
Jason Samsada7f272009-09-24 14:55:38 -0700130 setupScript();
Jason Sams1d54f102009-09-03 15:43:13 -0700131
Jason Sams2dca84d2009-12-09 11:05:45 -0800132 uint32_t ret = 0;
Jason Sams22fa3712010-05-19 17:22:57 -0700133 setTLS();
Jason Samsbe36bf32010-05-11 14:03:58 -0700134 //LOGE("ScriptC::run %p", mProgram.mRoot);
135 ret = mProgram.mRoot();
Jason Sams22fa3712010-05-19 17:22:57 -0700136 clearTLS();
Jason Samsbe36bf32010-05-11 14:03:58 -0700137 //LOGE("ScriptC::run ret %i", ret);
Jason Samse45ac6e2009-07-20 14:31:06 -0700138 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700139}
140
Jason Sams22fa3712010-05-19 17:22:57 -0700141void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
142{
143 //LOGE("rsi_ScriptInvoke %i", slot);
144 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
145 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
146 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
147 return;
148 }
149 setupScript();
150 setTLS();
151
152 const uint32_t * dPtr = (const uint32_t *)data;
153 switch(len) {
154 case 0:
155 mEnviroment.mInvokeFunctions[slot]();
156 break;
157 case 4:
158 ((void (*)(uint32_t))
159 mEnviroment.mInvokeFunctions[slot])(dPtr[0]);
160 break;
161 case 8:
162 ((void (*)(uint32_t, uint32_t))
163 mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1]);
164 break;
165 case 12:
166 ((void (*)(uint32_t, uint32_t, uint32_t))
167 mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1], dPtr[2]);
168 break;
169 case 16:
170 ((void (*)(uint32_t, uint32_t, uint32_t, uint32_t))
171 mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1], dPtr[2], dPtr[3]);
172 break;
173 case 20:
174 ((void (*)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))
175 mEnviroment.mInvokeFunctions[slot])(dPtr[0], dPtr[1], dPtr[2], dPtr[3], dPtr[4]);
176 break;
177 }
178 clearTLS();
179}
180
Jason Sams326e0dd2009-05-22 14:03:28 -0700181ScriptCState::ScriptCState()
182{
Jason Sams8c6bc692009-09-16 15:04:38 -0700183 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700184 clear();
185}
186
187ScriptCState::~ScriptCState()
188{
Jason Sams8c6bc692009-09-16 15:04:38 -0700189 delete mScript;
190 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700191}
192
193void ScriptCState::clear()
194{
Jason Sams8b2c0652009-08-12 17:54:11 -0700195 for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
196 mConstantBufferTypes[ct].clear();
Jason Sams90b36a82009-08-17 13:56:09 -0700197 mSlotWritable[ct] = false;
Jason Sams8b2c0652009-08-12 17:54:11 -0700198 }
Jason Samsefb8de12009-06-08 15:20:31 -0700199
Jason Sams8c6bc692009-09-16 15:04:38 -0700200 delete mScript;
Jason Samse514b452009-09-25 14:51:22 -0700201 mScript = new ScriptC(NULL);
Jason Sams1f526332009-06-05 17:35:09 -0700202}
203
Jason Samsbe36bf32010-05-11 14:03:58 -0700204static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700205{
Jason Samsaeb094b2010-05-18 13:35:45 -0700206 const ScriptCState::SymbolTable_t *sym;
207 sym = ScriptCState::lookupSymbol(name);
208 if (sym) {
209 return sym->mPtr;
210 }
211 sym = ScriptCState::lookupSymbolCL(name);
212 if (sym) {
213 return sym->mPtr;
214 }
215 sym = ScriptCState::lookupSymbolGL(name);
Jason Sams29df66f2009-07-16 15:08:06 -0700216 if (sym) {
217 return sym->mPtr;
218 }
Jason Sams29df66f2009-07-16 15:08:06 -0700219 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700220 return NULL;
221}
Jason Samsa4a54e42009-06-10 18:39:40 -0700222
Jason Sams8c6bc692009-09-16 15:04:38 -0700223void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
Jason Sams1f526332009-06-05 17:35:09 -0700224{
Jason Samsbe36bf32010-05-11 14:03:58 -0700225 LOGE("ScriptCState::runCompiler ");
Jason Sams1f526332009-06-05 17:35:09 -0700226
Jason Samsbe36bf32010-05-11 14:03:58 -0700227 s->mBccScript = bccCreateScript();
228 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
229 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, NULL);
Jason Samsbe36bf32010-05-11 14:03:58 -0700230 bccCompileScript(s->mBccScript);
Jason Samsbe36bf32010-05-11 14:03:58 -0700231 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
232 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
233 LOGE("root %p, init %p", s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700234
Jason Sams8c6bc692009-09-16 15:04:38 -0700235 if (s->mProgram.mInit) {
236 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700237 }
238
Jason Samsbe36bf32010-05-11 14:03:58 -0700239 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t *)calloc(100, sizeof(void *));
240 BCCchar **labels = new char*[100];
241 bccGetFunctions(s->mBccScript, (BCCsizei *)&s->mEnviroment.mInvokeFunctionCount,
242 100, (BCCchar **)labels);
243 //LOGE("func count %i", s->mEnviroment.mInvokeFunctionCount);
244 for (uint32_t i=0; i < s->mEnviroment.mInvokeFunctionCount; i++) {
245 BCCsizei length;
246 bccGetFunctionBinary(s->mBccScript, labels[i], (BCCvoid **)&(s->mEnviroment.mInvokeFunctions[i]), &length);
247 //LOGE("func %i %p", i, s->mEnviroment.mInvokeFunctions[i]);
Jason Sams1d54f102009-09-03 15:43:13 -0700248 }
249
Jason Samsbe36bf32010-05-11 14:03:58 -0700250 s->mEnviroment.mFieldAddress = (void **)calloc(100, sizeof(void *));
251 bccGetExportVars(s->mBccScript, (BCCsizei *)&s->mEnviroment.mFieldCount,
252 100, s->mEnviroment.mFieldAddress);
253 //LOGE("var count %i", s->mEnviroment.mFieldCount);
254 for (uint32_t i=0; i < s->mEnviroment.mFieldCount; i++) {
255 //LOGE("var %i %p", i, s->mEnviroment.mFieldAddress[i]);
Jason Sams8c6bc692009-09-16 15:04:38 -0700256 }
Jason Samsa4a54e42009-06-10 18:39:40 -0700257
Jason Sams8c6bc692009-09-16 15:04:38 -0700258 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
259 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700260 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700261 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700262
Jason Samsbe36bf32010-05-11 14:03:58 -0700263 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700264 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700265 BCCsizei pragmaCount;
266 BCCchar * str[pragmaMax];
267 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700268
Jason Sams10308932009-06-09 12:15:30 -0700269 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700270 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700271 if (!strcmp(str[ct], "version")) {
272 continue;
Jason Sams10308932009-06-09 12:15:30 -0700273 }
274
Jason Sams10308932009-06-09 12:15:30 -0700275 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700276 if (!strcmp(str[ct+1], "default")) {
277 continue;
278 }
279 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700280 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700281 continue;
282 }
Jason Sams10308932009-06-09 12:15:30 -0700283 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
284 }
285
286 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700287 if (!strcmp(str[ct+1], "default")) {
288 continue;
289 }
290 if (!strcmp(str[ct+1], "parent")) {
291 s->mEnviroment.mRaster.clear();
292 continue;
293 }
Jason Sams10308932009-06-09 12:15:30 -0700294 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
295 }
296
297 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700298 if (!strcmp(str[ct+1], "default")) {
299 continue;
300 }
301 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700302 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700303 continue;
304 }
Jason Sams10308932009-06-09 12:15:30 -0700305 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
306 }
307
Jason Samsb681c8a2009-09-28 18:12:56 -0700308 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700309 if (!strcmp(str[ct+1], "default")) {
310 continue;
311 }
312 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700313 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700314 continue;
315 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700316 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700317 }
318
319 }
320
Jason Samsd34b7252009-08-04 16:58:20 -0700321
Jason Sams10308932009-06-09 12:15:30 -0700322 } else {
323 // Deal with an error.
324 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700325}
326
Jason Sams8b2c0652009-08-12 17:54:11 -0700327
Joe Onorato57b79ce2009-08-09 22:57:44 -0700328
Jason Sams326e0dd2009-05-22 14:03:28 -0700329namespace android {
330namespace renderscript {
331
332void rsi_ScriptCBegin(Context * rsc)
333{
334 ScriptCState *ss = &rsc->mScriptC;
335 ss->clear();
336}
337
Jason Samsefb8de12009-06-08 15:20:31 -0700338void rsi_ScriptCSetScript(Context * rsc, void *vp)
Jason Sams326e0dd2009-05-22 14:03:28 -0700339{
Jason Sams8c6bc692009-09-16 15:04:38 -0700340 rsAssert(0);
341 //ScriptCState *ss = &rsc->mScriptC;
342 //ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp);
Jason Sams326e0dd2009-05-22 14:03:28 -0700343}
344
Jason Sams1f526332009-06-05 17:35:09 -0700345void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
346{
347 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800348
349 char *t = (char *)malloc(len + 1);
350 memcpy(t, text, len);
351 t[len] = 0;
352 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700353 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700354}
355
356
Jason Sams326e0dd2009-05-22 14:03:28 -0700357RsScript rsi_ScriptCCreate(Context * rsc)
358{
359 ScriptCState *ss = &rsc->mScriptC;
360
Jason Sams8c6bc692009-09-16 15:04:38 -0700361 ScriptC *s = ss->mScript;
362 ss->mScript = NULL;
Jason Sams1f526332009-06-05 17:35:09 -0700363
Jason Sams8c6bc692009-09-16 15:04:38 -0700364 ss->runCompiler(rsc, s);
Jason Sams9397e302009-08-27 20:23:34 -0700365 s->incUserRef();
Jason Samse514b452009-09-25 14:51:22 -0700366 s->setContext(rsc);
Jason Samsfa517192009-08-13 12:59:04 -0700367 for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
368 s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get());
Jason Sams90b36a82009-08-17 13:56:09 -0700369 s->mSlotWritable[ct] = ss->mSlotWritable[ct];
Jason Samsfa517192009-08-13 12:59:04 -0700370 }
Jason Sams10308932009-06-09 12:15:30 -0700371
Jason Samsfa517192009-08-13 12:59:04 -0700372 ss->clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700373 return s;
374}
375
Jason Sams326e0dd2009-05-22 14:03:28 -0700376}
377}
378
379