blob: 6d0701d1872001b3159a516f45f0d60a08f60d69 [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"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070019#include "utils/Timers.h"
Alex Sakhartchoukb26fb042010-09-24 15:18:12 -070020#include "utils/StopWatch.h"
Jack Palevich1ef8b802009-05-28 15:53:04 -070021
Jason Sams1aa5a4e2009-06-22 17:15:15 -070022#include <GLES/gl.h>
23#include <GLES/glext.h>
24
Alex Sakhartchouk02000b32011-02-25 09:34:33 -080025#include <bcc/bcc.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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080035ScriptC::ScriptC(Context *rsc) : Script(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070036}
37
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038ScriptC::~ScriptC() {
Jason Samsbad80742011-03-16 16:29:28 -070039 mRSC->mHal.funcs.script.destroy(mRSC, this);
Jason Sams326e0dd2009-05-22 14:03:28 -070040}
41
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080042void ScriptC::setupScript(Context *rsc) {
Jason Samsc61346b2010-05-28 18:23:22 -070043 mEnviroment.mStartTimeMillis
44 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
45
Jason Samsbad80742011-03-16 16:29:28 -070046 for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) {
Jason Sams900f1612010-09-16 18:18:29 -070047 if (mSlots[ct].get() && !mTypes[ct].get()) {
48 mTypes[ct].set(mSlots[ct]->getType());
49 }
50
51 if (!mTypes[ct].get())
Jason Samsbe36bf32010-05-11 14:03:58 -070052 continue;
Jason Sams900f1612010-09-16 18:18:29 -070053 void *ptr = NULL;
54 if (mSlots[ct].get()) {
55 ptr = mSlots[ct]->getPtr();
56 }
Jason Samsbe36bf32010-05-11 14:03:58 -070057
Jason Samsbad80742011-03-16 16:29:28 -070058 rsc->mHal.funcs.script.setGlobalBind(rsc, this, ct, ptr);
Jason Samsada7f272009-09-24 14:55:38 -070059 }
60}
61
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080062const Allocation *ScriptC::ptrToAllocation(const void *ptr) const {
Jason Samsbad80742011-03-16 16:29:28 -070063 //LOGE("ptr to alloc %p", ptr);
Jason Samsce92d4b2010-05-17 14:55:34 -070064 if (!ptr) {
65 return NULL;
66 }
Jason Samsbad80742011-03-16 16:29:28 -070067 for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) {
Jason Samsce92d4b2010-05-17 14:55:34 -070068 if (!mSlots[ct].get())
69 continue;
70 if (mSlots[ct]->getPtr() == ptr) {
71 return mSlots[ct].get();
72 }
73 }
74 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
75 return NULL;
76}
77
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080078void ScriptC::setupGLState(Context *rsc) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -070079 if (mEnviroment.mFragmentStore.get()) {
Jason Sams60709252010-11-17 15:29:32 -080080 rsc->setProgramStore(mEnviroment.mFragmentStore.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -070081 }
82 if (mEnviroment.mFragment.get()) {
Jason Sams60709252010-11-17 15:29:32 -080083 rsc->setProgramFragment(mEnviroment.mFragment.get());
Jason Samsa0a1b6f2009-06-10 15:04:38 -070084 }
Jason Sams8ce125b2009-06-17 16:52:59 -070085 if (mEnviroment.mVertex.get()) {
Jason Sams60709252010-11-17 15:29:32 -080086 rsc->setProgramVertex(mEnviroment.mVertex.get());
Jason Sams8ce125b2009-06-17 16:52:59 -070087 }
Jason Samsb681c8a2009-09-28 18:12:56 -070088 if (mEnviroment.mRaster.get()) {
Jason Sams60709252010-11-17 15:29:32 -080089 rsc->setProgramRaster(mEnviroment.mRaster.get());
Jason Samsb681c8a2009-09-28 18:12:56 -070090 }
Jason Samsc61346b2010-05-28 18:23:22 -070091}
Jason Samsa0a1b6f2009-06-10 15:04:38 -070092
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080093uint32_t ScriptC::run(Context *rsc) {
Jason Samsbad80742011-03-16 16:29:28 -070094 if (mHal.info.root == NULL) {
Jason Samsc61346b2010-05-28 18:23:22 -070095 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
96 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070097 }
Jason Samsc61346b2010-05-28 18:23:22 -070098
Jason Sams1f24db42010-12-11 17:42:30 -080099 setupGLState(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700100 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700101
Jason Sams2dca84d2009-12-09 11:05:45 -0800102 uint32_t ret = 0;
Jason Samsb9077f42010-09-22 15:57:41 -0700103
104 if (rsc->props.mLogScripts) {
Jason Samsbad80742011-03-16 16:29:28 -0700105 LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mHal.info.root);
Jason Samsb9077f42010-09-22 15:57:41 -0700106 }
107
Jason Samscdfdb8f2011-03-17 16:12:47 -0700108 ret = rsc->mHal.funcs.script.invokeRoot(rsc, this);
Jason Samsb9077f42010-09-22 15:57:41 -0700109
110 if (rsc->props.mLogScripts) {
111 LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
112 }
113
Jason Samse45ac6e2009-07-20 14:31:06 -0700114 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Jason Sams177f8442010-10-29 10:19:21 -0700117
Jason Samsace3e012010-07-15 17:11:13 -0700118void ScriptC::runForEach(Context *rsc,
119 const Allocation * ain,
120 Allocation * aout,
121 const void * usr,
Jason Sams87fe59a2011-04-20 15:09:01 -0700122 size_t usrBytes,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800123 const RsScriptCall *sc) {
Jason Samscdfdb8f2011-03-17 16:12:47 -0700124
Jason Sams60709252010-11-17 15:29:32 -0800125 Context::PushState ps(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700126
Jason Sams1f24db42010-12-11 17:42:30 -0800127 setupGLState(rsc);
Jason Samsc61346b2010-05-28 18:23:22 -0700128 setupScript(rsc);
Jason Sams87fe59a2011-04-20 15:09:01 -0700129 rsc->mHal.funcs.script.invokeForEach(rsc, this, ain, aout, usr, usrBytes, sc);
Jason Samsc61346b2010-05-28 18:23:22 -0700130}
131
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800132void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) {
Jason Samsbad80742011-03-16 16:29:28 -0700133 if (slot >= mHal.info.exportedFunctionCount) {
Jason Sams22fa3712010-05-19 17:22:57 -0700134 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
135 return;
136 }
Jason Samsc61346b2010-05-28 18:23:22 -0700137 setupScript(rsc);
Jason Sams22fa3712010-05-19 17:22:57 -0700138
Jason Samsb9077f42010-09-22 15:57:41 -0700139 if (rsc->props.mLogScripts) {
Jason Samsbad80742011-03-16 16:29:28 -0700140 LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, this);
Jason Samsb9077f42010-09-22 15:57:41 -0700141 }
Jason Samsbad80742011-03-16 16:29:28 -0700142 rsc->mHal.funcs.script.invokeFunction(rsc, this, slot, data, len);
Jason Sams22fa3712010-05-19 17:22:57 -0700143}
144
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800145ScriptCState::ScriptCState() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700146}
147
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800148ScriptCState::~ScriptCState() {
Jason Sams1f526332009-06-05 17:35:09 -0700149}
150
Jason Sams87fe59a2011-04-20 15:09:01 -0700151/*
Shih-wei Liao2b2e6212011-01-14 06:21:28 -0800152static void* symbolLookup(void* pContext, char const* name) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700153 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700154 ScriptC *s = (ScriptC *)pContext;
Shih-wei Liaof17b13b2010-12-07 13:44:10 -0800155 if (!strcmp(name, "__isThreadable")) {
Jason Samsbad80742011-03-16 16:29:28 -0700156 return (void*) s->mHal.info.isThreadable;
Shih-wei Liaof17b13b2010-12-07 13:44:10 -0800157 } else if (!strcmp(name, "__clearThreadable")) {
Jason Samsbad80742011-03-16 16:29:28 -0700158 s->mHal.info.isThreadable = false;
Shih-wei Liaof17b13b2010-12-07 13:44:10 -0800159 return NULL;
160 }
Jason Samsaeb094b2010-05-18 13:35:45 -0700161 sym = ScriptCState::lookupSymbol(name);
Jason Sams6bfc1b92010-11-01 14:26:30 -0700162 if (!sym) {
163 sym = ScriptCState::lookupSymbolCL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700164 }
Jason Sams6bfc1b92010-11-01 14:26:30 -0700165 if (!sym) {
166 sym = ScriptCState::lookupSymbolGL(name);
Jason Samsaeb094b2010-05-18 13:35:45 -0700167 }
Jason Sams29df66f2009-07-16 15:08:06 -0700168 if (sym) {
Jason Samsbad80742011-03-16 16:29:28 -0700169 s->mHal.info.isThreadable &= sym->threadable;
Jason Sams29df66f2009-07-16 15:08:06 -0700170 return sym->mPtr;
171 }
Jason Sams29df66f2009-07-16 15:08:06 -0700172 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700173 return NULL;
174}
Jason Sams87fe59a2011-04-20 15:09:01 -0700175*/
Jason Samsa4a54e42009-06-10 18:39:40 -0700176
Shih-wei Liao80761ec2011-01-16 02:23:04 -0800177#if 0
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700178extern const char rs_runtime_lib_bc[];
179extern unsigned rs_runtime_lib_bc_size;
Shih-wei Liao80761ec2011-01-16 02:23:04 -0800180#endif
Shih-wei Liao1f9ba732010-10-23 02:15:57 -0700181
Jason Samsbad80742011-03-16 16:29:28 -0700182bool ScriptC::runCompiler(Context *rsc,
183 const char *resName,
184 const char *cacheDir,
185 const uint8_t *bitcode,
186 size_t bitcodeLen) {
Shih-wei Liao37150de2011-01-07 18:17:07 -0800187
Jason Samsbad80742011-03-16 16:29:28 -0700188 //LOGE("runCompiler %p %p %p %p %p %i", rsc, this, resName, cacheDir, bitcode, bitcodeLen);
Shih-wei Liao37150de2011-01-07 18:17:07 -0800189
Jason Sams87fe59a2011-04-20 15:09:01 -0700190 rsc->mHal.funcs.script.init(rsc, this, resName, cacheDir, bitcode, bitcodeLen, 0);
Shih-wei Liao37150de2011-01-07 18:17:07 -0800191
Jason Samsbad80742011-03-16 16:29:28 -0700192 mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
193 mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
194 mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
195 mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Shih-wei Liao37150de2011-01-07 18:17:07 -0800196
Jason Samsbad80742011-03-16 16:29:28 -0700197 rsc->mHal.funcs.script.invokeInit(rsc, this);
Stephen Hines7b337b12011-01-17 17:31:58 -0800198
Jason Samsbad80742011-03-16 16:29:28 -0700199 for (size_t i=0; i < mHal.info.exportedPragmaCount; ++i) {
200 const char * key = mHal.info.exportedPragmaKeyList[i];
201 const char * value = mHal.info.exportedPragmaValueList[i];
Stephen Hines7b337b12011-01-17 17:31:58 -0800202 //LOGE("pragma %s %s", keys[i], values[i]);
Jason Samsbad80742011-03-16 16:29:28 -0700203 if (!strcmp(key, "version")) {
204 if (!strcmp(value, "1")) {
Stephen Hinesb5dc6af2011-01-18 14:10:44 -0800205 continue;
206 }
Jason Samsbad80742011-03-16 16:29:28 -0700207 LOGE("Invalid version pragma value: %s\n", value);
Jason Sams26b2c9f2011-01-19 16:14:21 -0800208 return false;
Stephen Hines7b337b12011-01-17 17:31:58 -0800209 }
210
Jason Samsbad80742011-03-16 16:29:28 -0700211 if (!strcmp(key, "stateVertex")) {
212 if (!strcmp(value, "default")) {
Jason Sams10308932009-06-09 12:15:30 -0700213 continue;
Jason Sams10308932009-06-09 12:15:30 -0700214 }
Jason Samsbad80742011-03-16 16:29:28 -0700215 if (!strcmp(value, "parent")) {
216 mEnviroment.mVertex.clear();
Stephen Hines7b337b12011-01-17 17:31:58 -0800217 continue;
Jason Sams10308932009-06-09 12:15:30 -0700218 }
Jason Samsbad80742011-03-16 16:29:28 -0700219 LOGE("Unrecognized value %s passed to stateVertex", value);
Jason Sams26b2c9f2011-01-19 16:14:21 -0800220 return false;
Jason Sams10308932009-06-09 12:15:30 -0700221 }
Stephen Hines7b337b12011-01-17 17:31:58 -0800222
Jason Samsbad80742011-03-16 16:29:28 -0700223 if (!strcmp(key, "stateRaster")) {
224 if (!strcmp(value, "default")) {
Stephen Hines7b337b12011-01-17 17:31:58 -0800225 continue;
226 }
Jason Samsbad80742011-03-16 16:29:28 -0700227 if (!strcmp(value, "parent")) {
228 mEnviroment.mRaster.clear();
Stephen Hines7b337b12011-01-17 17:31:58 -0800229 continue;
230 }
Jason Samsbad80742011-03-16 16:29:28 -0700231 LOGE("Unrecognized value %s passed to stateRaster", value);
Jason Sams26b2c9f2011-01-19 16:14:21 -0800232 return false;
Stephen Hines7b337b12011-01-17 17:31:58 -0800233 }
234
Jason Samsbad80742011-03-16 16:29:28 -0700235 if (!strcmp(key, "stateFragment")) {
236 if (!strcmp(value, "default")) {
Stephen Hines7b337b12011-01-17 17:31:58 -0800237 continue;
238 }
Jason Samsbad80742011-03-16 16:29:28 -0700239 if (!strcmp(value, "parent")) {
240 mEnviroment.mFragment.clear();
Stephen Hines7b337b12011-01-17 17:31:58 -0800241 continue;
242 }
Jason Samsbad80742011-03-16 16:29:28 -0700243 LOGE("Unrecognized value %s passed to stateFragment", value);
Jason Sams26b2c9f2011-01-19 16:14:21 -0800244 return false;
Stephen Hines7b337b12011-01-17 17:31:58 -0800245 }
246
Jason Samsbad80742011-03-16 16:29:28 -0700247 if (!strcmp(key, "stateStore")) {
248 if (!strcmp(value, "default")) {
Stephen Hines7b337b12011-01-17 17:31:58 -0800249 continue;
250 }
Jason Samsbad80742011-03-16 16:29:28 -0700251 if (!strcmp(value, "parent")) {
252 mEnviroment.mFragmentStore.clear();
Stephen Hines7b337b12011-01-17 17:31:58 -0800253 continue;
254 }
Jason Samsbad80742011-03-16 16:29:28 -0700255 LOGE("Unrecognized value %s passed to stateStore", value);
Jason Sams26b2c9f2011-01-19 16:14:21 -0800256 return false;
Stephen Hines7b337b12011-01-17 17:31:58 -0800257 }
Jason Sams10308932009-06-09 12:15:30 -0700258 }
Jason Sams2e8665d2011-01-27 00:14:13 -0800259
Jason Samsbad80742011-03-16 16:29:28 -0700260 mSlots = new ObjectBaseRef<Allocation>[mHal.info.exportedVariableCount];
261 mTypes = new ObjectBaseRef<const Type>[mHal.info.exportedVariableCount];
Jason Sams2e8665d2011-01-27 00:14:13 -0800262
Jason Sams26b2c9f2011-01-19 16:14:21 -0800263 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700264}
265
266namespace android {
267namespace renderscript {
268
Shih-wei Liaoce8a0792010-12-20 20:45:56 +0800269RsScript rsi_ScriptCCreate(Context *rsc,
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700270 const char *resName, size_t resName_length,
271 const char *cacheDir, size_t cacheDir_length,
272 const char *text, uint32_t text_length)
Shih-wei Liao9503b662010-11-08 01:33:59 -0800273{
Jason Sams249d4532011-01-23 17:48:45 -0800274 ScriptC *s = new ScriptC(rsc);
Jason Sams1f526332009-06-05 17:35:09 -0700275
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700276 if (!s->runCompiler(rsc, resName, cacheDir, (uint8_t *)text, text_length)) {
Jason Sams26b2c9f2011-01-19 16:14:21 -0800277 // Error during compile, destroy s and return null.
Jason Sams249d4532011-01-23 17:48:45 -0800278 delete s;
Jason Sams26b2c9f2011-01-19 16:14:21 -0800279 return NULL;
280 }
Jason Samsbad80742011-03-16 16:29:28 -0700281
282 s->incUserRef();
Jason Sams249d4532011-01-23 17:48:45 -0800283 return s;
Jason Sams326e0dd2009-05-22 14:03:28 -0700284}
285
Jason Sams326e0dd2009-05-22 14:03:28 -0700286}
287}