blob: 2e7f2134fbaabeedfcfc6d1e2f0eaee8f2e3067f [file] [log] [blame]
Jason Samsd19f10d2009-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"
Joe Onorato3370ec92009-08-09 11:39:02 -070019#include "utils/Timers.h"
Alex Sakhartchoukeb1a73c2010-09-24 15:18:12 -070020#include "utils/StopWatch.h"
Jack Palevichec5a20b2009-05-28 15:53:04 -070021
Stephen Hines4382467a2011-08-01 15:02:34 -070022#ifndef ANDROID_RS_SERIALIZE
23#include <bcinfo/BitcodeTranslator.h>
24#endif
25
Jason Samsd19f10d2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Sams462d11b2009-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
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080034ScriptC::ScriptC(Context *rsc) : Script(rsc) {
Stephen Hines4382467a2011-08-01 15:02:34 -070035#ifndef ANDROID_RS_SERIALIZE
36 BT = NULL;
37#endif
Jason Samsd19f10d2009-05-22 14:03:28 -070038}
39
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080040ScriptC::~ScriptC() {
Stephen Hines4382467a2011-08-01 15:02:34 -070041#ifndef ANDROID_RS_SERIALIZE
42 if (BT) {
43 delete BT;
44 BT = NULL;
45 }
46#endif
Stephen Hines466bdc42011-09-01 18:07:11 -070047 mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
Jason Samse4a06c52011-03-16 16:29:28 -070048 mRSC->mHal.funcs.script.destroy(mRSC, this);
Jason Samsd19f10d2009-05-22 14:03:28 -070049}
50
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080051void ScriptC::setupScript(Context *rsc) {
Jason Samsf17bccc2010-05-28 18:23:22 -070052 mEnviroment.mStartTimeMillis
53 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
54
Jason Samse4a06c52011-03-16 16:29:28 -070055 for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) {
Jason Samsd081fff2010-09-16 18:18:29 -070056 if (mSlots[ct].get() && !mTypes[ct].get()) {
57 mTypes[ct].set(mSlots[ct]->getType());
58 }
59
60 if (!mTypes[ct].get())
Jason Sams4d339932010-05-11 14:03:58 -070061 continue;
Jason Samsd081fff2010-09-16 18:18:29 -070062 void *ptr = NULL;
63 if (mSlots[ct].get()) {
64 ptr = mSlots[ct]->getPtr();
65 }
Jason Sams4d339932010-05-11 14:03:58 -070066
Jason Samse4a06c52011-03-16 16:29:28 -070067 rsc->mHal.funcs.script.setGlobalBind(rsc, this, ct, ptr);
Jason Samse60446b2009-09-24 14:55:38 -070068 }
69}
70
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080071const Allocation *ScriptC::ptrToAllocation(const void *ptr) const {
Jason Samse4a06c52011-03-16 16:29:28 -070072 //LOGE("ptr to alloc %p", ptr);
Jason Sams1de0b872010-05-17 14:55:34 -070073 if (!ptr) {
74 return NULL;
75 }
Jason Samse4a06c52011-03-16 16:29:28 -070076 for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) {
Jason Sams1de0b872010-05-17 14:55:34 -070077 if (!mSlots[ct].get())
78 continue;
79 if (mSlots[ct]->getPtr() == ptr) {
80 return mSlots[ct].get();
81 }
82 }
83 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
84 return NULL;
85}
86
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080087void ScriptC::setupGLState(Context *rsc) {
Jason Sams3eaa3382009-06-10 15:04:38 -070088 if (mEnviroment.mFragmentStore.get()) {
Jason Samsa17af042010-11-17 15:29:32 -080089 rsc->setProgramStore(mEnviroment.mFragmentStore.get());
Jason Sams3eaa3382009-06-10 15:04:38 -070090 }
91 if (mEnviroment.mFragment.get()) {
Jason Samsa17af042010-11-17 15:29:32 -080092 rsc->setProgramFragment(mEnviroment.mFragment.get());
Jason Sams3eaa3382009-06-10 15:04:38 -070093 }
Jason Sams9c54bdb2009-06-17 16:52:59 -070094 if (mEnviroment.mVertex.get()) {
Jason Samsa17af042010-11-17 15:29:32 -080095 rsc->setProgramVertex(mEnviroment.mVertex.get());
Jason Sams9c54bdb2009-06-17 16:52:59 -070096 }
Jason Sams5235cf32009-09-28 18:12:56 -070097 if (mEnviroment.mRaster.get()) {
Jason Samsa17af042010-11-17 15:29:32 -080098 rsc->setProgramRaster(mEnviroment.mRaster.get());
Jason Sams5235cf32009-09-28 18:12:56 -070099 }
Jason Samsf17bccc2010-05-28 18:23:22 -0700100}
Jason Sams3eaa3382009-06-10 15:04:38 -0700101
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800102uint32_t ScriptC::run(Context *rsc) {
Jason Samse4a06c52011-03-16 16:29:28 -0700103 if (mHal.info.root == NULL) {
Jason Samsf17bccc2010-05-28 18:23:22 -0700104 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
105 return 0;
Joe Onorato3370ec92009-08-09 11:39:02 -0700106 }
Jason Samsf17bccc2010-05-28 18:23:22 -0700107
Jason Sams4859f522010-12-11 17:42:30 -0800108 setupGLState(rsc);
Jason Samsf17bccc2010-05-28 18:23:22 -0700109 setupScript(rsc);
Jason Sams2525a812009-09-03 15:43:13 -0700110
Jason Samsb9d5c572009-12-09 11:05:45 -0800111 uint32_t ret = 0;
Jason Samsf119b912010-09-22 15:57:41 -0700112
113 if (rsc->props.mLogScripts) {
Jason Samse4a06c52011-03-16 16:29:28 -0700114 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mHal.info.root);
Jason Samsf119b912010-09-22 15:57:41 -0700115 }
116
Jason Sams55d2a252011-03-17 16:12:47 -0700117 ret = rsc->mHal.funcs.script.invokeRoot(rsc, this);
Jason Samsf119b912010-09-22 15:57:41 -0700118
119 if (rsc->props.mLogScripts) {
120 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
121 }
122
Jason Samsc97bb882009-07-20 14:31:06 -0700123 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700124}
125
Jason Sams5585e362010-10-29 10:19:21 -0700126
Jason Sams8f8a5722010-07-15 17:11:13 -0700127void ScriptC::runForEach(Context *rsc,
128 const Allocation * ain,
129 Allocation * aout,
130 const void * usr,
Jason Samsfcf72312011-04-20 15:09:01 -0700131 size_t usrBytes,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800132 const RsScriptCall *sc) {
Jason Sams55d2a252011-03-17 16:12:47 -0700133
Jason Samsa17af042010-11-17 15:29:32 -0800134 Context::PushState ps(rsc);
Jason Samsf17bccc2010-05-28 18:23:22 -0700135
Jason Sams4859f522010-12-11 17:42:30 -0800136 setupGLState(rsc);
Jason Samsf17bccc2010-05-28 18:23:22 -0700137 setupScript(rsc);
Jason Sams92b0eab2011-07-13 11:26:26 -0700138 rsc->mHal.funcs.script.invokeForEach(rsc, this, 0, ain, aout, usr, usrBytes, sc);
Jason Samsf17bccc2010-05-28 18:23:22 -0700139}
140
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700141void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, size_t len) {
Jason Samse4a06c52011-03-16 16:29:28 -0700142 if (slot >= mHal.info.exportedFunctionCount) {
Jason Samsd79b2e92010-05-19 17:22:57 -0700143 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
144 return;
145 }
Jason Samsf17bccc2010-05-28 18:23:22 -0700146 setupScript(rsc);
Jason Samsd79b2e92010-05-19 17:22:57 -0700147
Jason Samsf119b912010-09-22 15:57:41 -0700148 if (rsc->props.mLogScripts) {
Jason Samse4a06c52011-03-16 16:29:28 -0700149 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, this);
Jason Samsf119b912010-09-22 15:57:41 -0700150 }
Jason Samse4a06c52011-03-16 16:29:28 -0700151 rsc->mHal.funcs.script.invokeFunction(rsc, this, slot, data, len);
Jason Samsd79b2e92010-05-19 17:22:57 -0700152}
153
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800154ScriptCState::ScriptCState() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700155}
156
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800157ScriptCState::~ScriptCState() {
Jason Sams39ddc9502009-06-05 17:35:09 -0700158}
159
Jason Samsfcf72312011-04-20 15:09:01 -0700160/*
Shih-wei Liao076b7702011-01-14 06:21:28 -0800161static void* symbolLookup(void* pContext, char const* name) {
Jason Sams536923d2010-05-18 13:35:45 -0700162 const ScriptCState::SymbolTable_t *sym;
Jason Sams2cbd2982010-08-11 13:26:28 -0700163 ScriptC *s = (ScriptC *)pContext;
Shih-wei Liaob7282bd2010-12-07 13:44:10 -0800164 if (!strcmp(name, "__isThreadable")) {
Jason Samse4a06c52011-03-16 16:29:28 -0700165 return (void*) s->mHal.info.isThreadable;
Shih-wei Liaob7282bd2010-12-07 13:44:10 -0800166 } else if (!strcmp(name, "__clearThreadable")) {
Jason Samse4a06c52011-03-16 16:29:28 -0700167 s->mHal.info.isThreadable = false;
Shih-wei Liaob7282bd2010-12-07 13:44:10 -0800168 return NULL;
169 }
Jason Sams536923d2010-05-18 13:35:45 -0700170 sym = ScriptCState::lookupSymbol(name);
Jason Sams8f0adba2010-11-01 14:26:30 -0700171 if (!sym) {
172 sym = ScriptCState::lookupSymbolCL(name);
Jason Sams536923d2010-05-18 13:35:45 -0700173 }
Jason Sams8f0adba2010-11-01 14:26:30 -0700174 if (!sym) {
175 sym = ScriptCState::lookupSymbolGL(name);
Jason Sams536923d2010-05-18 13:35:45 -0700176 }
Jason Sams54440a02009-07-16 15:08:06 -0700177 if (sym) {
Jason Samse4a06c52011-03-16 16:29:28 -0700178 s->mHal.info.isThreadable &= sym->threadable;
Jason Sams54440a02009-07-16 15:08:06 -0700179 return sym->mPtr;
180 }
Jason Sams54440a02009-07-16 15:08:06 -0700181 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams54440a02009-07-16 15:08:06 -0700182 return NULL;
183}
Jason Samsfcf72312011-04-20 15:09:01 -0700184*/
Jason Samsd5680f92009-06-10 18:39:40 -0700185
Shih-wei Liao71a2e132011-01-16 02:23:04 -0800186#if 0
Shih-wei Liao0175e012010-10-23 02:15:57 -0700187extern const char rs_runtime_lib_bc[];
188extern unsigned rs_runtime_lib_bc_size;
Shih-wei Liao71a2e132011-01-16 02:23:04 -0800189#endif
Shih-wei Liao0175e012010-10-23 02:15:57 -0700190
Jason Samse4a06c52011-03-16 16:29:28 -0700191bool ScriptC::runCompiler(Context *rsc,
192 const char *resName,
193 const char *cacheDir,
194 const uint8_t *bitcode,
195 size_t bitcodeLen) {
Shih-wei Liao9d4024d2011-01-07 18:17:07 -0800196
Jason Samse4a06c52011-03-16 16:29:28 -0700197 //LOGE("runCompiler %p %p %p %p %p %i", rsc, this, resName, cacheDir, bitcode, bitcodeLen);
Stephen Hines4382467a2011-08-01 15:02:34 -0700198#ifndef ANDROID_RS_SERIALIZE
199 uint32_t sdkVersion = rsc->getTargetSdkVersion();
200 if (BT) {
201 delete BT;
202 }
203 BT = new bcinfo::BitcodeTranslator((const char *)bitcode, bitcodeLen,
204 sdkVersion);
205 if (!BT->translate()) {
206 LOGE("Failed to translate bitcode from version: %u", sdkVersion);
207 delete BT;
208 BT = NULL;
209 return false;
210 }
211 bitcode = (const uint8_t *) BT->getTranslatedBitcode();
212 bitcodeLen = BT->getTranslatedBitcodeSize();
213#endif
Shih-wei Liao9d4024d2011-01-07 18:17:07 -0800214
Jason Samsfcf72312011-04-20 15:09:01 -0700215 rsc->mHal.funcs.script.init(rsc, this, resName, cacheDir, bitcode, bitcodeLen, 0);
Shih-wei Liao9d4024d2011-01-07 18:17:07 -0800216
Jason Samse4a06c52011-03-16 16:29:28 -0700217 mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
218 mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
219 mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
220 mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Shih-wei Liao9d4024d2011-01-07 18:17:07 -0800221
Jason Samse4a06c52011-03-16 16:29:28 -0700222 rsc->mHal.funcs.script.invokeInit(rsc, this);
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800223
Jason Samse4a06c52011-03-16 16:29:28 -0700224 for (size_t i=0; i < mHal.info.exportedPragmaCount; ++i) {
225 const char * key = mHal.info.exportedPragmaKeyList[i];
226 const char * value = mHal.info.exportedPragmaValueList[i];
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800227 //LOGE("pragma %s %s", keys[i], values[i]);
Jason Samse4a06c52011-03-16 16:29:28 -0700228 if (!strcmp(key, "version")) {
229 if (!strcmp(value, "1")) {
Stephen Hines5dd60be2011-01-18 14:10:44 -0800230 continue;
231 }
Jason Samse4a06c52011-03-16 16:29:28 -0700232 LOGE("Invalid version pragma value: %s\n", value);
Jason Samsfdc54a92011-01-19 16:14:21 -0800233 return false;
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800234 }
235
Jason Samse4a06c52011-03-16 16:29:28 -0700236 if (!strcmp(key, "stateVertex")) {
237 if (!strcmp(value, "default")) {
Jason Samsda423d82009-06-09 12:15:30 -0700238 continue;
Jason Samsda423d82009-06-09 12:15:30 -0700239 }
Jason Samse4a06c52011-03-16 16:29:28 -0700240 if (!strcmp(value, "parent")) {
241 mEnviroment.mVertex.clear();
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800242 continue;
Jason Samsda423d82009-06-09 12:15:30 -0700243 }
Jason Samse4a06c52011-03-16 16:29:28 -0700244 LOGE("Unrecognized value %s passed to stateVertex", value);
Jason Samsfdc54a92011-01-19 16:14:21 -0800245 return false;
Jason Samsda423d82009-06-09 12:15:30 -0700246 }
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800247
Jason Samse4a06c52011-03-16 16:29:28 -0700248 if (!strcmp(key, "stateRaster")) {
249 if (!strcmp(value, "default")) {
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800250 continue;
251 }
Jason Samse4a06c52011-03-16 16:29:28 -0700252 if (!strcmp(value, "parent")) {
253 mEnviroment.mRaster.clear();
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800254 continue;
255 }
Jason Samse4a06c52011-03-16 16:29:28 -0700256 LOGE("Unrecognized value %s passed to stateRaster", value);
Jason Samsfdc54a92011-01-19 16:14:21 -0800257 return false;
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800258 }
259
Jason Samse4a06c52011-03-16 16:29:28 -0700260 if (!strcmp(key, "stateFragment")) {
261 if (!strcmp(value, "default")) {
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800262 continue;
263 }
Jason Samse4a06c52011-03-16 16:29:28 -0700264 if (!strcmp(value, "parent")) {
265 mEnviroment.mFragment.clear();
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800266 continue;
267 }
Jason Samse4a06c52011-03-16 16:29:28 -0700268 LOGE("Unrecognized value %s passed to stateFragment", value);
Jason Samsfdc54a92011-01-19 16:14:21 -0800269 return false;
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800270 }
271
Jason Samse4a06c52011-03-16 16:29:28 -0700272 if (!strcmp(key, "stateStore")) {
273 if (!strcmp(value, "default")) {
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800274 continue;
275 }
Jason Samse4a06c52011-03-16 16:29:28 -0700276 if (!strcmp(value, "parent")) {
277 mEnviroment.mFragmentStore.clear();
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800278 continue;
279 }
Jason Samse4a06c52011-03-16 16:29:28 -0700280 LOGE("Unrecognized value %s passed to stateStore", value);
Jason Samsfdc54a92011-01-19 16:14:21 -0800281 return false;
Stephen Hines5ef3ddd2011-01-17 17:31:58 -0800282 }
Jason Samsda423d82009-06-09 12:15:30 -0700283 }
Jason Sams38f8d9d2011-01-27 00:14:13 -0800284
Jason Samse4a06c52011-03-16 16:29:28 -0700285 mSlots = new ObjectBaseRef<Allocation>[mHal.info.exportedVariableCount];
286 mTypes = new ObjectBaseRef<const Type>[mHal.info.exportedVariableCount];
Jason Sams38f8d9d2011-01-27 00:14:13 -0800287
Jason Samsfdc54a92011-01-19 16:14:21 -0800288 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700289}
290
291namespace android {
292namespace renderscript {
293
Shih-wei Liaoeeca4352010-12-20 20:45:56 +0800294RsScript rsi_ScriptCCreate(Context *rsc,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700295 const char *resName, size_t resName_length,
296 const char *cacheDir, size_t cacheDir_length,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700297 const char *text, size_t text_length)
Shih-wei Liaoa914f342010-11-08 01:33:59 -0800298{
Jason Samsc55de662011-01-23 17:48:45 -0800299 ScriptC *s = new ScriptC(rsc);
Jason Sams39ddc9502009-06-05 17:35:09 -0700300
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700301 if (!s->runCompiler(rsc, resName, cacheDir, (uint8_t *)text, text_length)) {
Jason Samsfdc54a92011-01-19 16:14:21 -0800302 // Error during compile, destroy s and return null.
Jason Samsc55de662011-01-23 17:48:45 -0800303 delete s;
Jason Samsfdc54a92011-01-19 16:14:21 -0800304 return NULL;
305 }
Jason Samse4a06c52011-03-16 16:29:28 -0700306
307 s->incUserRef();
Jason Samsc55de662011-01-23 17:48:45 -0800308 return s;
Jason Samsd19f10d2009-05-22 14:03:28 -0700309}
310
Jason Samsd19f10d2009-05-22 14:03:28 -0700311}
312}