2009-05-22 14:03:28 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rsContext.h"
|
|
|
|
#include "rsScriptC.h"
|
2009-08-09 11:39:02 -07:00
|
|
|
#include "utils/Timers.h"
|
2010-09-24 15:18:12 -07:00
|
|
|
#include "utils/StopWatch.h"
|
2009-05-28 15:53:04 -07:00
|
|
|
|
2011-08-01 15:02:34 -07:00
|
|
|
#ifndef ANDROID_RS_SERIALIZE
|
|
|
|
#include <bcinfo/BitcodeTranslator.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
using namespace android;
|
|
|
|
using namespace android::renderscript;
|
|
|
|
|
2009-06-19 16:03:18 -07:00
|
|
|
#define GET_TLS() Context::ScriptTLSStruct * tls = \
|
|
|
|
(Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
|
|
|
|
Context * rsc = tls->mContext; \
|
|
|
|
ScriptC * sc = (ScriptC *) tls->mScript
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptC::ScriptC(Context *rsc) : Script(rsc) {
|
2011-08-01 15:02:34 -07:00
|
|
|
#ifndef ANDROID_RS_SERIALIZE
|
|
|
|
BT = NULL;
|
|
|
|
#endif
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptC::~ScriptC() {
|
2011-08-01 15:02:34 -07:00
|
|
|
#ifndef ANDROID_RS_SERIALIZE
|
|
|
|
if (BT) {
|
|
|
|
delete BT;
|
|
|
|
BT = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-01 18:07:11 -07:00
|
|
|
mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
|
2011-03-16 16:29:28 -07:00
|
|
|
mRSC->mHal.funcs.script.destroy(mRSC, this);
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ScriptC::setupScript(Context *rsc) {
|
2010-05-28 18:23:22 -07:00
|
|
|
mEnviroment.mStartTimeMillis
|
|
|
|
= nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
|
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) {
|
2010-09-16 18:18:29 -07:00
|
|
|
if (mSlots[ct].get() && !mTypes[ct].get()) {
|
|
|
|
mTypes[ct].set(mSlots[ct]->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTypes[ct].get())
|
2010-05-11 14:03:58 -07:00
|
|
|
continue;
|
2010-09-16 18:18:29 -07:00
|
|
|
void *ptr = NULL;
|
|
|
|
if (mSlots[ct].get()) {
|
|
|
|
ptr = mSlots[ct]->getPtr();
|
|
|
|
}
|
2010-05-11 14:03:58 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
rsc->mHal.funcs.script.setGlobalBind(rsc, this, ct, ptr);
|
2009-09-24 14:55:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
const Allocation *ScriptC::ptrToAllocation(const void *ptr) const {
|
2011-03-16 16:29:28 -07:00
|
|
|
//LOGE("ptr to alloc %p", ptr);
|
2010-05-17 14:55:34 -07:00
|
|
|
if (!ptr) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) {
|
2010-05-17 14:55:34 -07:00
|
|
|
if (!mSlots[ct].get())
|
|
|
|
continue;
|
|
|
|
if (mSlots[ct]->getPtr() == ptr) {
|
|
|
|
return mSlots[ct].get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ScriptC::setupGLState(Context *rsc) {
|
2009-06-10 15:04:38 -07:00
|
|
|
if (mEnviroment.mFragmentStore.get()) {
|
2010-11-17 15:29:32 -08:00
|
|
|
rsc->setProgramStore(mEnviroment.mFragmentStore.get());
|
2009-06-10 15:04:38 -07:00
|
|
|
}
|
|
|
|
if (mEnviroment.mFragment.get()) {
|
2010-11-17 15:29:32 -08:00
|
|
|
rsc->setProgramFragment(mEnviroment.mFragment.get());
|
2009-06-10 15:04:38 -07:00
|
|
|
}
|
2009-06-17 16:52:59 -07:00
|
|
|
if (mEnviroment.mVertex.get()) {
|
2010-11-17 15:29:32 -08:00
|
|
|
rsc->setProgramVertex(mEnviroment.mVertex.get());
|
2009-06-17 16:52:59 -07:00
|
|
|
}
|
2009-09-28 18:12:56 -07:00
|
|
|
if (mEnviroment.mRaster.get()) {
|
2010-11-17 15:29:32 -08:00
|
|
|
rsc->setProgramRaster(mEnviroment.mRaster.get());
|
2009-09-28 18:12:56 -07:00
|
|
|
}
|
2010-05-28 18:23:22 -07:00
|
|
|
}
|
2009-06-10 15:04:38 -07:00
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
uint32_t ScriptC::run(Context *rsc) {
|
2011-03-16 16:29:28 -07:00
|
|
|
if (mHal.info.root == NULL) {
|
2010-05-28 18:23:22 -07:00
|
|
|
rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
|
|
|
|
return 0;
|
2009-08-09 11:39:02 -07:00
|
|
|
}
|
2010-05-28 18:23:22 -07:00
|
|
|
|
2010-12-11 17:42:30 -08:00
|
|
|
setupGLState(rsc);
|
2010-05-28 18:23:22 -07:00
|
|
|
setupScript(rsc);
|
2009-09-03 15:43:13 -07:00
|
|
|
|
2009-12-09 11:05:45 -08:00
|
|
|
uint32_t ret = 0;
|
2010-09-22 15:57:41 -07:00
|
|
|
|
|
|
|
if (rsc->props.mLogScripts) {
|
2011-03-16 16:29:28 -07:00
|
|
|
LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mHal.info.root);
|
2010-09-22 15:57:41 -07:00
|
|
|
}
|
|
|
|
|
2011-03-17 16:12:47 -07:00
|
|
|
ret = rsc->mHal.funcs.script.invokeRoot(rsc, this);
|
2010-09-22 15:57:41 -07:00
|
|
|
|
|
|
|
if (rsc->props.mLogScripts) {
|
|
|
|
LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
|
|
|
|
}
|
|
|
|
|
2009-07-20 14:31:06 -07:00
|
|
|
return ret;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-10-29 10:19:21 -07:00
|
|
|
|
2010-07-15 17:11:13 -07:00
|
|
|
void ScriptC::runForEach(Context *rsc,
|
|
|
|
const Allocation * ain,
|
|
|
|
Allocation * aout,
|
|
|
|
const void * usr,
|
2011-04-20 15:09:01 -07:00
|
|
|
size_t usrBytes,
|
2010-11-09 17:00:54 -08:00
|
|
|
const RsScriptCall *sc) {
|
2010-07-19 15:38:19 -07:00
|
|
|
|
2011-03-17 16:12:47 -07:00
|
|
|
Context::PushState ps(rsc);
|
2010-05-28 18:23:22 -07:00
|
|
|
|
2010-12-11 17:42:30 -08:00
|
|
|
setupGLState(rsc);
|
2010-05-28 18:23:22 -07:00
|
|
|
setupScript(rsc);
|
2011-07-13 11:26:26 -07:00
|
|
|
rsc->mHal.funcs.script.invokeForEach(rsc, this, 0, ain, aout, usr, usrBytes, sc);
|
2010-05-28 18:23:22 -07:00
|
|
|
}
|
|
|
|
|
2011-06-03 10:18:01 -07:00
|
|
|
void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, size_t len) {
|
2011-03-16 16:29:28 -07:00
|
|
|
if (slot >= mHal.info.exportedFunctionCount) {
|
2010-05-19 17:22:57 -07:00
|
|
|
rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-28 18:23:22 -07:00
|
|
|
setupScript(rsc);
|
2010-05-19 17:22:57 -07:00
|
|
|
|
2010-09-22 15:57:41 -07:00
|
|
|
if (rsc->props.mLogScripts) {
|
2011-03-16 16:29:28 -07:00
|
|
|
LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, this);
|
2010-09-22 15:57:41 -07:00
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
rsc->mHal.funcs.script.invokeFunction(rsc, this, slot, data, len);
|
2010-05-19 17:22:57 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptCState::ScriptCState() {
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptCState::~ScriptCState() {
|
2009-06-05 17:35:09 -07:00
|
|
|
}
|
|
|
|
|
2011-04-20 15:09:01 -07:00
|
|
|
/*
|
2011-01-14 06:21:28 -08:00
|
|
|
static void* symbolLookup(void* pContext, char const* name) {
|
2010-05-18 13:35:45 -07:00
|
|
|
const ScriptCState::SymbolTable_t *sym;
|
2010-08-11 13:26:28 -07:00
|
|
|
ScriptC *s = (ScriptC *)pContext;
|
2010-12-07 13:44:10 -08:00
|
|
|
if (!strcmp(name, "__isThreadable")) {
|
2011-03-16 16:29:28 -07:00
|
|
|
return (void*) s->mHal.info.isThreadable;
|
2010-12-07 13:44:10 -08:00
|
|
|
} else if (!strcmp(name, "__clearThreadable")) {
|
2011-03-16 16:29:28 -07:00
|
|
|
s->mHal.info.isThreadable = false;
|
2010-12-07 13:44:10 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-18 13:35:45 -07:00
|
|
|
sym = ScriptCState::lookupSymbol(name);
|
2010-11-01 14:26:30 -07:00
|
|
|
if (!sym) {
|
|
|
|
sym = ScriptCState::lookupSymbolCL(name);
|
2010-05-18 13:35:45 -07:00
|
|
|
}
|
2010-11-01 14:26:30 -07:00
|
|
|
if (!sym) {
|
|
|
|
sym = ScriptCState::lookupSymbolGL(name);
|
2010-05-18 13:35:45 -07:00
|
|
|
}
|
2009-07-16 15:08:06 -07:00
|
|
|
if (sym) {
|
2011-03-16 16:29:28 -07:00
|
|
|
s->mHal.info.isThreadable &= sym->threadable;
|
2009-07-16 15:08:06 -07:00
|
|
|
return sym->mPtr;
|
|
|
|
}
|
|
|
|
LOGE("ScriptC sym lookup failed for %s", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-20 15:09:01 -07:00
|
|
|
*/
|
2009-06-10 18:39:40 -07:00
|
|
|
|
2011-01-16 02:23:04 -08:00
|
|
|
#if 0
|
2010-10-23 02:15:57 -07:00
|
|
|
extern const char rs_runtime_lib_bc[];
|
|
|
|
extern unsigned rs_runtime_lib_bc_size;
|
2011-01-16 02:23:04 -08:00
|
|
|
#endif
|
2010-10-23 02:15:57 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
bool ScriptC::runCompiler(Context *rsc,
|
|
|
|
const char *resName,
|
|
|
|
const char *cacheDir,
|
|
|
|
const uint8_t *bitcode,
|
|
|
|
size_t bitcodeLen) {
|
2009-07-16 17:47:40 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
//LOGE("runCompiler %p %p %p %p %p %i", rsc, this, resName, cacheDir, bitcode, bitcodeLen);
|
2011-08-01 15:02:34 -07:00
|
|
|
#ifndef ANDROID_RS_SERIALIZE
|
|
|
|
uint32_t sdkVersion = rsc->getTargetSdkVersion();
|
|
|
|
if (BT) {
|
|
|
|
delete BT;
|
|
|
|
}
|
|
|
|
BT = new bcinfo::BitcodeTranslator((const char *)bitcode, bitcodeLen,
|
|
|
|
sdkVersion);
|
|
|
|
if (!BT->translate()) {
|
|
|
|
LOGE("Failed to translate bitcode from version: %u", sdkVersion);
|
|
|
|
delete BT;
|
|
|
|
BT = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bitcode = (const uint8_t *) BT->getTranslatedBitcode();
|
|
|
|
bitcodeLen = BT->getTranslatedBitcodeSize();
|
|
|
|
#endif
|
2009-09-03 15:43:13 -07:00
|
|
|
|
2011-04-20 15:09:01 -07:00
|
|
|
rsc->mHal.funcs.script.init(rsc, this, resName, cacheDir, bitcode, bitcodeLen, 0);
|
2010-06-15 12:15:57 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
|
|
|
|
mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
|
|
|
|
mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
|
|
|
|
mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
|
2009-09-03 15:43:13 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
rsc->mHal.funcs.script.invokeInit(rsc, this);
|
2009-06-10 18:39:40 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
for (size_t i=0; i < mHal.info.exportedPragmaCount; ++i) {
|
|
|
|
const char * key = mHal.info.exportedPragmaKeyList[i];
|
|
|
|
const char * value = mHal.info.exportedPragmaValueList[i];
|
2011-01-17 17:31:58 -08:00
|
|
|
//LOGE("pragma %s %s", keys[i], values[i]);
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(key, "version")) {
|
|
|
|
if (!strcmp(value, "1")) {
|
2011-01-18 14:10:44 -08:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
LOGE("Invalid version pragma value: %s\n", value);
|
2011-01-19 16:14:21 -08:00
|
|
|
return false;
|
2011-01-17 17:31:58 -08:00
|
|
|
}
|
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(key, "stateVertex")) {
|
|
|
|
if (!strcmp(value, "default")) {
|
2009-06-09 12:15:30 -07:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(value, "parent")) {
|
|
|
|
mEnviroment.mVertex.clear();
|
2011-01-17 17:31:58 -08:00
|
|
|
continue;
|
2009-06-09 12:15:30 -07:00
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
LOGE("Unrecognized value %s passed to stateVertex", value);
|
2011-01-19 16:14:21 -08:00
|
|
|
return false;
|
2011-01-17 17:31:58 -08:00
|
|
|
}
|
2009-06-09 12:15:30 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(key, "stateRaster")) {
|
|
|
|
if (!strcmp(value, "default")) {
|
2011-01-17 17:31:58 -08:00
|
|
|
continue;
|
2009-06-09 12:15:30 -07:00
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(value, "parent")) {
|
|
|
|
mEnviroment.mRaster.clear();
|
2011-01-17 17:31:58 -08:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
LOGE("Unrecognized value %s passed to stateRaster", value);
|
2011-01-19 16:14:21 -08:00
|
|
|
return false;
|
2011-01-17 17:31:58 -08:00
|
|
|
}
|
2009-06-09 12:15:30 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(key, "stateFragment")) {
|
|
|
|
if (!strcmp(value, "default")) {
|
2011-01-17 17:31:58 -08:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(value, "parent")) {
|
|
|
|
mEnviroment.mFragment.clear();
|
2011-01-17 17:31:58 -08:00
|
|
|
continue;
|
2009-06-09 12:15:30 -07:00
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
LOGE("Unrecognized value %s passed to stateFragment", value);
|
2011-01-19 16:14:21 -08:00
|
|
|
return false;
|
2011-01-17 17:31:58 -08:00
|
|
|
}
|
2009-06-09 12:15:30 -07:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(key, "stateStore")) {
|
|
|
|
if (!strcmp(value, "default")) {
|
2011-01-17 17:31:58 -08:00
|
|
|
continue;
|
2009-06-09 12:15:30 -07:00
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
if (!strcmp(value, "parent")) {
|
|
|
|
mEnviroment.mFragmentStore.clear();
|
2011-01-17 17:31:58 -08:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
LOGE("Unrecognized value %s passed to stateStore", value);
|
2011-01-19 16:14:21 -08:00
|
|
|
return false;
|
2009-06-09 12:15:30 -07:00
|
|
|
}
|
|
|
|
}
|
2011-01-27 00:14:13 -08:00
|
|
|
|
2011-03-16 16:29:28 -07:00
|
|
|
mSlots = new ObjectBaseRef<Allocation>[mHal.info.exportedVariableCount];
|
|
|
|
mTypes = new ObjectBaseRef<const Type>[mHal.info.exportedVariableCount];
|
2011-01-27 00:14:13 -08:00
|
|
|
|
2011-01-19 16:14:21 -08:00
|
|
|
return true;
|
2009-08-09 22:57:44 -07:00
|
|
|
}
|
2009-06-09 12:15:30 -07:00
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
namespace android {
|
|
|
|
namespace renderscript {
|
|
|
|
|
2010-12-20 20:45:56 +08:00
|
|
|
RsScript rsi_ScriptCCreate(Context *rsc,
|
2011-04-06 10:57:51 -07:00
|
|
|
const char *resName, size_t resName_length,
|
|
|
|
const char *cacheDir, size_t cacheDir_length,
|
2011-06-03 10:18:01 -07:00
|
|
|
const char *text, size_t text_length)
|
2010-11-08 01:33:59 -08:00
|
|
|
{
|
2011-01-23 17:48:45 -08:00
|
|
|
ScriptC *s = new ScriptC(rsc);
|
2009-06-05 17:35:09 -07:00
|
|
|
|
2011-04-06 10:57:51 -07:00
|
|
|
if (!s->runCompiler(rsc, resName, cacheDir, (uint8_t *)text, text_length)) {
|
2011-01-19 16:14:21 -08:00
|
|
|
// Error during compile, destroy s and return null.
|
2011-01-23 17:48:45 -08:00
|
|
|
delete s;
|
2011-01-19 16:14:21 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-16 16:29:28 -07:00
|
|
|
|
|
|
|
s->incUserRef();
|
2011-01-23 17:48:45 -08:00
|
|
|
return s;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|