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"
|
|
|
|
#include "rsMatrix.h"
|
2010-09-07 17:33:01 -07:00
|
|
|
#include "../../compile/libbcc/include/bcc/bcc.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
|
|
|
|
2009-06-22 17:15:15 -07:00
|
|
|
#include <GLES/gl.h>
|
|
|
|
#include <GLES/glext.h>
|
|
|
|
|
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
|
|
|
|
|
2009-05-22 14:03:28 -07:00
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptC::ScriptC(Context *rsc) : Script(rsc) {
|
2010-05-11 14:03:58 -07:00
|
|
|
mBccScript = NULL;
|
2009-06-08 15:20:31 -07:00
|
|
|
memset(&mProgram, 0, sizeof(mProgram));
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptC::~ScriptC() {
|
2010-05-11 14:03:58 -07:00
|
|
|
if (mBccScript) {
|
|
|
|
bccDeleteScript(mBccScript);
|
2009-05-28 15:53:04 -07:00
|
|
|
}
|
2009-11-03 11:25:42 -08:00
|
|
|
free(mEnviroment.mScriptText);
|
|
|
|
mEnviroment.mScriptText = NULL;
|
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
|
|
|
setupGLState(rsc);
|
|
|
|
mEnviroment.mStartTimeMillis
|
|
|
|
= nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
|
|
|
|
|
2010-05-11 14:03:58 -07:00
|
|
|
for (uint32_t ct=0; ct < mEnviroment.mFieldCount; 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
|
|
|
void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
|
|
|
|
|
2010-09-22 15:57:41 -07:00
|
|
|
if (rsc->props.mLogScripts) {
|
2010-11-16 12:19:26 -08:00
|
|
|
if (mSlots[ct].get() != NULL) {
|
|
|
|
LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=%p", rsc, ct, dest, ptr, mSlots[ct]->getType());
|
|
|
|
} else {
|
|
|
|
LOGV("%p ScriptC::setupScript slot=%i dst=%p src=%p type=null", rsc, ct, dest, ptr);
|
|
|
|
}
|
2010-09-22 15:57:41 -07:00
|
|
|
}
|
2010-05-11 14:03:58 -07:00
|
|
|
|
|
|
|
if (dest) {
|
|
|
|
*dest = 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 {
|
2010-05-17 14:55:34 -07:00
|
|
|
if (!ptr) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
|
|
|
|
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
|
|
|
Script * ScriptC::setTLS(Script *sc) {
|
2010-05-19 17:22:57 -07:00
|
|
|
Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
|
|
|
|
pthread_getspecific(Context::gThreadTLSKey);
|
|
|
|
rsAssert(tls);
|
2010-05-28 18:23:22 -07:00
|
|
|
Script *old = tls->mScript;
|
|
|
|
tls->mScript = sc;
|
|
|
|
return old;
|
2010-05-19 17:22:57 -07:00
|
|
|
}
|
|
|
|
|
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) {
|
2010-05-28 18:23:22 -07:00
|
|
|
if (mProgram.mRoot == NULL) {
|
|
|
|
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
|
|
|
|
|
|
|
setupScript(rsc);
|
2009-09-03 15:43:13 -07:00
|
|
|
|
2009-12-09 11:05:45 -08:00
|
|
|
uint32_t ret = 0;
|
2010-05-28 18:23:22 -07:00
|
|
|
Script * oldTLS = setTLS(this);
|
2010-09-22 15:57:41 -07:00
|
|
|
|
|
|
|
if (rsc->props.mLogScripts) {
|
|
|
|
LOGV("%p ScriptC::run invoking root, ptr %p", rsc, mProgram.mRoot);
|
|
|
|
}
|
|
|
|
|
2010-05-11 14:03:58 -07:00
|
|
|
ret = mProgram.mRoot();
|
2010-09-22 15:57:41 -07:00
|
|
|
|
|
|
|
if (rsc->props.mLogScripts) {
|
|
|
|
LOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
|
|
|
|
}
|
|
|
|
|
2010-05-28 18:23:22 -07:00
|
|
|
setTLS(oldTLS);
|
2009-07-20 14:31:06 -07:00
|
|
|
return ret;
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-07-19 15:38:19 -07:00
|
|
|
typedef struct {
|
|
|
|
Context *rsc;
|
|
|
|
ScriptC *script;
|
|
|
|
const Allocation * ain;
|
|
|
|
Allocation * aout;
|
|
|
|
const void * usr;
|
|
|
|
|
|
|
|
uint32_t mSliceSize;
|
|
|
|
volatile int mSliceNum;
|
|
|
|
|
|
|
|
const uint8_t *ptrIn;
|
|
|
|
uint32_t eStrideIn;
|
|
|
|
uint8_t *ptrOut;
|
|
|
|
uint32_t eStrideOut;
|
|
|
|
|
|
|
|
uint32_t xStart;
|
|
|
|
uint32_t xEnd;
|
|
|
|
uint32_t yStart;
|
|
|
|
uint32_t yEnd;
|
|
|
|
uint32_t zStart;
|
|
|
|
uint32_t zEnd;
|
|
|
|
uint32_t arrayStart;
|
|
|
|
uint32_t arrayEnd;
|
|
|
|
|
|
|
|
uint32_t dimX;
|
|
|
|
uint32_t dimY;
|
|
|
|
uint32_t dimZ;
|
|
|
|
uint32_t dimArray;
|
|
|
|
} MTLaunchStruct;
|
|
|
|
typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
static void wc_xy(void *usr, uint32_t idx) {
|
2010-07-19 15:38:19 -07:00
|
|
|
MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
|
|
|
|
uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
|
|
|
|
uint32_t yEnd = yStart + mtls->mSliceSize;
|
|
|
|
yEnd = rsMin(yEnd, mtls->yEnd);
|
|
|
|
if (yEnd <= yStart) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
|
2010-09-28 14:41:22 -07:00
|
|
|
//LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
|
2010-07-19 15:38:19 -07:00
|
|
|
for (uint32_t y = yStart; y < yEnd; y++) {
|
|
|
|
uint32_t offset = mtls->dimX * y;
|
|
|
|
uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
|
|
|
|
const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
|
|
|
|
|
|
|
|
for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
|
|
|
|
((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
|
|
|
|
xPtrIn += mtls->eStrideIn;
|
|
|
|
xPtrOut += mtls->eStrideOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
static void wc_x(void *usr, uint32_t idx) {
|
2010-10-29 10:19:21 -07:00
|
|
|
MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
|
|
|
|
uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
|
|
|
|
uint32_t xEnd = xStart + mtls->mSliceSize;
|
|
|
|
xEnd = rsMin(xEnd, mtls->xEnd);
|
|
|
|
if (xEnd <= xStart) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
|
|
|
|
//LOGE("usr ptr in %p, out %p", mtls->ptrIn, mtls->ptrOut);
|
|
|
|
uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * xStart);
|
|
|
|
const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * xStart);
|
|
|
|
for (uint32_t x = xStart; x < xEnd; x++) {
|
|
|
|
((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, 0, 0, 0);
|
|
|
|
xPtrIn += mtls->eStrideIn;
|
|
|
|
xPtrOut += mtls->eStrideOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-15 17:11:13 -07:00
|
|
|
void ScriptC::runForEach(Context *rsc,
|
|
|
|
const Allocation * ain,
|
|
|
|
Allocation * aout,
|
|
|
|
const void * usr,
|
2010-11-09 17:00:54 -08:00
|
|
|
const RsScriptCall *sc) {
|
2010-07-19 15:38:19 -07:00
|
|
|
MTLaunchStruct mtls;
|
|
|
|
memset(&mtls, 0, sizeof(mtls));
|
2010-11-17 15:29:32 -08:00
|
|
|
Context::PushState ps(rsc);
|
2010-07-19 15:38:19 -07:00
|
|
|
|
|
|
|
if (ain) {
|
|
|
|
mtls.dimX = ain->getType()->getDimX();
|
|
|
|
mtls.dimY = ain->getType()->getDimY();
|
|
|
|
mtls.dimZ = ain->getType()->getDimZ();
|
|
|
|
//mtls.dimArray = ain->getType()->getDimArray();
|
|
|
|
} else if (aout) {
|
|
|
|
mtls.dimX = aout->getType()->getDimX();
|
|
|
|
mtls.dimY = aout->getType()->getDimY();
|
|
|
|
mtls.dimZ = aout->getType()->getDimZ();
|
|
|
|
//mtls.dimArray = aout->getType()->getDimArray();
|
|
|
|
} else {
|
|
|
|
rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
|
|
|
|
return;
|
|
|
|
}
|
2010-07-15 17:11:13 -07:00
|
|
|
|
|
|
|
if (!sc || (sc->xEnd == 0)) {
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.xEnd = mtls.dimX;
|
2010-07-15 17:11:13 -07:00
|
|
|
} else {
|
2010-07-19 15:38:19 -07:00
|
|
|
rsAssert(sc->xStart < mtls.dimX);
|
|
|
|
rsAssert(sc->xEnd <= mtls.dimX);
|
2010-07-15 17:11:13 -07:00
|
|
|
rsAssert(sc->xStart < sc->xEnd);
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.xStart = rsMin(mtls.dimX, sc->xStart);
|
|
|
|
mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
|
|
|
|
if (mtls.xStart >= mtls.xEnd) return;
|
2010-07-15 17:11:13 -07:00
|
|
|
}
|
2010-05-28 18:23:22 -07:00
|
|
|
|
2010-07-15 17:11:13 -07:00
|
|
|
if (!sc || (sc->yEnd == 0)) {
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.yEnd = mtls.dimY;
|
2010-07-15 17:11:13 -07:00
|
|
|
} else {
|
2010-07-19 15:38:19 -07:00
|
|
|
rsAssert(sc->yStart < mtls.dimY);
|
|
|
|
rsAssert(sc->yEnd <= mtls.dimY);
|
2010-07-15 17:11:13 -07:00
|
|
|
rsAssert(sc->yStart < sc->yEnd);
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.yStart = rsMin(mtls.dimY, sc->yStart);
|
|
|
|
mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
|
|
|
|
if (mtls.yStart >= mtls.yEnd) return;
|
2010-07-15 17:11:13 -07:00
|
|
|
}
|
|
|
|
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
|
|
|
|
mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
|
|
|
|
mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
|
|
|
|
mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
|
2010-07-15 17:11:13 -07:00
|
|
|
|
|
|
|
rsAssert(ain->getType()->getDimZ() == 0);
|
2010-05-28 18:23:22 -07:00
|
|
|
|
|
|
|
setupScript(rsc);
|
|
|
|
Script * oldTLS = setTLS(this);
|
|
|
|
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.rsc = rsc;
|
|
|
|
mtls.ain = ain;
|
|
|
|
mtls.aout = aout;
|
|
|
|
mtls.script = this;
|
|
|
|
mtls.usr = usr;
|
|
|
|
mtls.mSliceSize = 10;
|
|
|
|
mtls.mSliceNum = 0;
|
|
|
|
|
|
|
|
mtls.ptrIn = NULL;
|
|
|
|
mtls.eStrideIn = 0;
|
|
|
|
if (ain) {
|
|
|
|
mtls.ptrIn = (const uint8_t *)ain->getPtr();
|
|
|
|
mtls.eStrideIn = ain->getType()->getElementSizeBytes();
|
|
|
|
}
|
2010-05-28 18:23:22 -07:00
|
|
|
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.ptrOut = NULL;
|
|
|
|
mtls.eStrideOut = 0;
|
2010-05-28 18:23:22 -07:00
|
|
|
if (aout) {
|
2010-07-19 15:38:19 -07:00
|
|
|
mtls.ptrOut = (uint8_t *)aout->getPtr();
|
|
|
|
mtls.eStrideOut = aout->getType()->getElementSizeBytes();
|
2010-05-28 18:23:22 -07:00
|
|
|
}
|
|
|
|
|
2010-10-29 10:19:21 -07:00
|
|
|
if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable) {
|
|
|
|
if (mtls.dimY > 1) {
|
|
|
|
rsc->launchThreads(wc_xy, &mtls);
|
|
|
|
} else {
|
|
|
|
rsc->launchThreads(wc_x, &mtls);
|
|
|
|
}
|
2010-07-19 15:38:19 -07:00
|
|
|
|
2010-07-20 15:09:00 -07:00
|
|
|
//LOGE("launch 1");
|
|
|
|
} else {
|
2010-09-28 14:41:22 -07:00
|
|
|
//LOGE("launch 3");
|
2010-07-20 15:09:00 -07:00
|
|
|
for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
|
|
|
|
for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
|
|
|
|
for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
|
|
|
|
uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
|
|
|
|
mtls.dimX * mtls.dimY * z +
|
|
|
|
mtls.dimX * y;
|
|
|
|
uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
|
|
|
|
const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
|
|
|
|
|
|
|
|
for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
|
|
|
|
((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
|
|
|
|
xPtrIn += mtls.eStrideIn;
|
|
|
|
xPtrOut += mtls.eStrideOut;
|
|
|
|
}
|
2010-07-15 17:11:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-28 18:23:22 -07:00
|
|
|
}
|
2010-07-20 15:09:00 -07:00
|
|
|
|
2010-05-28 18:23:22 -07:00
|
|
|
setTLS(oldTLS);
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) {
|
2010-05-19 17:22:57 -07:00
|
|
|
//LOGE("rsi_ScriptInvoke %i", slot);
|
|
|
|
if ((slot >= mEnviroment.mInvokeFunctionCount) ||
|
|
|
|
(mEnviroment.mInvokeFunctions[slot] == NULL)) {
|
|
|
|
rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-28 18:23:22 -07:00
|
|
|
setupScript(rsc);
|
|
|
|
Script * oldTLS = setTLS(this);
|
2010-05-19 17:22:57 -07:00
|
|
|
|
2010-09-22 15:57:41 -07:00
|
|
|
if (rsc->props.mLogScripts) {
|
|
|
|
LOGV("%p ScriptC::Invoke invoking slot %i, ptr %p", rsc, slot, mEnviroment.mInvokeFunctions[slot]);
|
|
|
|
}
|
2010-06-08 15:40:48 -07:00
|
|
|
((void (*)(const void *, uint32_t))
|
|
|
|
mEnviroment.mInvokeFunctions[slot])(data, len);
|
2010-09-22 15:57:41 -07:00
|
|
|
if (rsc->props.mLogScripts) {
|
|
|
|
LOGV("%p ScriptC::Invoke complete", rsc);
|
|
|
|
}
|
2010-06-08 15:40:48 -07:00
|
|
|
|
2010-05-28 18:23:22 -07:00
|
|
|
setTLS(oldTLS);
|
2010-05-19 17:22:57 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptCState::ScriptCState() {
|
2010-09-28 15:45:45 -07:00
|
|
|
mScript.clear();
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
ScriptCState::~ScriptCState() {
|
2010-09-28 15:45:45 -07:00
|
|
|
mScript.clear();
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ScriptCState::init(Context *rsc) {
|
2010-09-28 15:45:45 -07:00
|
|
|
clear(rsc);
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void ScriptCState::clear(Context *rsc) {
|
2010-09-28 15:45:45 -07:00
|
|
|
rsAssert(rsc);
|
|
|
|
mScript.clear();
|
|
|
|
mScript.set(new ScriptC(rsc));
|
2009-06-05 17:35:09 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* 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-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) {
|
2010-11-01 14:26:30 -07:00
|
|
|
s->mEnviroment.mIsThreadable &= sym->threadable;
|
2009-07-16 15:08:06 -07:00
|
|
|
return sym->mPtr;
|
|
|
|
}
|
|
|
|
LOGE("ScriptC sym lookup failed for %s", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-06-10 18:39:40 -07:00
|
|
|
|
2010-10-23 02:15:57 -07:00
|
|
|
extern const char rs_runtime_lib_bc[];
|
|
|
|
extern unsigned rs_runtime_lib_bc_size;
|
|
|
|
|
2010-11-08 01:33:59 -08:00
|
|
|
void ScriptCState::runCompiler(Context *rsc, ScriptC *s, const char *resName) {
|
2010-09-24 15:18:12 -07:00
|
|
|
{
|
|
|
|
s->mBccScript = bccCreateScript();
|
|
|
|
s->mEnviroment.mIsThreadable = true;
|
|
|
|
bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
|
2010-11-08 01:33:59 -08:00
|
|
|
// bccReadBC() reads in the BitCode, if no cache file corresponding to
|
|
|
|
// the resName is found. Otherwise, bccReadBC() returns a negative value
|
|
|
|
// and the "else" branch will be taken.
|
|
|
|
if (bccReadBC(s->mBccScript,
|
|
|
|
s->mEnviroment.mScriptText,
|
|
|
|
s->mEnviroment.mScriptTextLength,
|
|
|
|
resName) >= 0) {
|
|
|
|
//bccLinkBC(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
|
|
|
|
bccCompileBC(s->mBccScript);
|
|
|
|
} else {
|
|
|
|
// bccReadBC returns a neagative value: Didn't read any script,
|
|
|
|
// So, use cached binary instead
|
|
|
|
bccLoadBinary(s->mBccScript);
|
|
|
|
}
|
2010-09-24 15:18:12 -07:00
|
|
|
bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
|
|
|
|
bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
|
|
|
|
}
|
2010-09-22 15:57:41 -07:00
|
|
|
LOGV("%p ScriptCState::runCompiler root %p, init %p", rsc, s->mProgram.mRoot, s->mProgram.mInit);
|
2009-07-16 17:47:40 -07:00
|
|
|
|
2009-09-16 15:04:38 -07:00
|
|
|
if (s->mProgram.mInit) {
|
|
|
|
s->mProgram.mInit();
|
2009-09-03 15:43:13 -07:00
|
|
|
}
|
|
|
|
|
2010-06-15 12:15:57 -07:00
|
|
|
bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
|
2010-11-09 17:00:54 -08:00
|
|
|
if (s->mEnviroment.mInvokeFunctionCount <= 0)
|
2010-06-15 12:15:57 -07:00
|
|
|
s->mEnviroment.mInvokeFunctions = NULL;
|
|
|
|
else {
|
|
|
|
s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
|
|
|
|
bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
|
|
|
|
}
|
|
|
|
|
2010-07-20 16:43:25 -07:00
|
|
|
bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
|
2010-11-09 17:00:54 -08:00
|
|
|
if (s->mEnviroment.mFieldCount <= 0)
|
2010-07-20 16:43:25 -07:00
|
|
|
s->mEnviroment.mFieldAddress = NULL;
|
|
|
|
else {
|
|
|
|
s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
|
|
|
|
bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
|
2010-10-08 15:00:05 -07:00
|
|
|
s->initSlots();
|
2010-07-20 16:43:25 -07:00
|
|
|
}
|
2009-09-03 15:43:13 -07:00
|
|
|
|
2009-09-16 15:04:38 -07:00
|
|
|
s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
|
|
|
|
s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
|
2010-05-13 18:30:11 -07:00
|
|
|
s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
|
2009-09-28 18:12:56 -07:00
|
|
|
s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
|
2009-06-10 18:39:40 -07:00
|
|
|
|
2010-05-11 14:03:58 -07:00
|
|
|
if (s->mProgram.mRoot) {
|
2009-06-09 12:15:30 -07:00
|
|
|
const static int pragmaMax = 16;
|
2010-05-11 14:03:58 -07:00
|
|
|
BCCsizei pragmaCount;
|
|
|
|
BCCchar * str[pragmaMax];
|
|
|
|
bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
|
2009-06-09 12:15:30 -07:00
|
|
|
|
|
|
|
for (int ct=0; ct < pragmaCount; ct+=2) {
|
2010-05-18 13:35:45 -07:00
|
|
|
//LOGE("pragme %s %s", str[ct], str[ct+1]);
|
2009-06-09 12:15:30 -07:00
|
|
|
if (!strcmp(str[ct], "version")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(str[ct], "stateVertex")) {
|
2009-06-17 16:52:59 -07:00
|
|
|
if (!strcmp(str[ct+1], "default")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(str[ct+1], "parent")) {
|
2009-09-16 15:04:38 -07:00
|
|
|
s->mEnviroment.mVertex.clear();
|
2009-06-17 16:52:59 -07:00
|
|
|
continue;
|
|
|
|
}
|
2009-06-09 12:15:30 -07:00
|
|
|
LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(str[ct], "stateRaster")) {
|
2009-09-28 18:12:56 -07:00
|
|
|
if (!strcmp(str[ct+1], "default")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(str[ct+1], "parent")) {
|
|
|
|
s->mEnviroment.mRaster.clear();
|
|
|
|
continue;
|
|
|
|
}
|
2009-06-09 12:15:30 -07:00
|
|
|
LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(str[ct], "stateFragment")) {
|
2009-06-17 16:52:59 -07:00
|
|
|
if (!strcmp(str[ct+1], "default")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(str[ct+1], "parent")) {
|
2009-09-16 15:04:38 -07:00
|
|
|
s->mEnviroment.mFragment.clear();
|
2009-06-17 16:52:59 -07:00
|
|
|
continue;
|
|
|
|
}
|
2009-06-09 12:15:30 -07:00
|
|
|
LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
|
|
|
|
}
|
|
|
|
|
2009-09-28 18:12:56 -07:00
|
|
|
if (!strcmp(str[ct], "stateStore")) {
|
2009-06-17 16:52:59 -07:00
|
|
|
if (!strcmp(str[ct+1], "default")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(str[ct+1], "parent")) {
|
2009-09-16 15:04:38 -07:00
|
|
|
s->mEnviroment.mFragmentStore.clear();
|
2009-06-17 16:52:59 -07:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-28 18:12:56 -07:00
|
|
|
LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
|
2009-06-09 12:15:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-08-04 16:58:20 -07:00
|
|
|
|
2009-06-09 12:15:30 -07:00
|
|
|
} else {
|
|
|
|
// Deal with an error.
|
|
|
|
}
|
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-11-09 17:00:54 -08:00
|
|
|
void rsi_ScriptCBegin(Context * rsc) {
|
2009-05-22 14:03:28 -07:00
|
|
|
ScriptCState *ss = &rsc->mScriptC;
|
2010-09-28 15:45:45 -07:00
|
|
|
ss->clear(rsc);
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:00:54 -08:00
|
|
|
void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) {
|
2009-06-05 17:35:09 -07:00
|
|
|
ScriptCState *ss = &rsc->mScriptC;
|
2009-11-03 11:25:42 -08:00
|
|
|
|
|
|
|
char *t = (char *)malloc(len + 1);
|
|
|
|
memcpy(t, text, len);
|
|
|
|
t[len] = 0;
|
|
|
|
ss->mScript->mEnviroment.mScriptText = t;
|
2009-09-16 15:04:38 -07:00
|
|
|
ss->mScript->mEnviroment.mScriptTextLength = len;
|
2009-06-05 17:35:09 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 01:33:59 -08:00
|
|
|
RsScript rsi_ScriptCCreate(Context * rsc, const char *resName)
|
|
|
|
{
|
2009-05-22 14:03:28 -07:00
|
|
|
ScriptCState *ss = &rsc->mScriptC;
|
|
|
|
|
2010-10-21 14:06:55 -07:00
|
|
|
ObjectBaseRef<ScriptC> s(ss->mScript);
|
2010-09-28 15:45:45 -07:00
|
|
|
ss->mScript.clear();
|
2010-10-21 14:06:55 -07:00
|
|
|
s->incUserRef();
|
2009-06-05 17:35:09 -07:00
|
|
|
|
2010-11-08 01:33:59 -08:00
|
|
|
ss->runCompiler(rsc, s.get(), resName);
|
2010-09-28 15:45:45 -07:00
|
|
|
ss->clear(rsc);
|
|
|
|
return s.get();
|
2009-05-22 14:03:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|