Add support for selecting the color bit depth and if the application used a depth buffer.

This commit is contained in:
Jason Sams
2009-08-25 11:34:49 -07:00
parent dbade9d6a0
commit b13ada5071
19 changed files with 134 additions and 106 deletions

View File

@ -141,15 +141,12 @@ public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback
// ----------------------------------------------------------------------
public RenderScript createRenderScript() {
Log.v(RenderScript.LOG_TAG, "createRenderScript 1");
public RenderScript createRenderScript(boolean useDepth) {
Surface sur = null;
while ((sur == null) || (mSurfaceHolder == null)) {
sur = getHolder().getSurface();
}
Log.v(RenderScript.LOG_TAG, "createRenderScript 2");
RenderScript rs = new RenderScript(sur);
Log.v(RenderScript.LOG_TAG, "createRenderScript 3 rs");
RenderScript rs = new RenderScript(sur, useDepth);
return rs;
}

View File

@ -60,7 +60,7 @@ public class RenderScript {
native int nDeviceCreate();
native void nDeviceDestroy(int dev);
native int nContextCreate(int dev, Surface sur, int ver);
native int nContextCreate(int dev, Surface sur, int ver, boolean useDepth);
native void nContextDestroy(int con);
//void rsContextBindSampler (uint32_t slot, RsSampler sampler);
@ -194,10 +194,10 @@ public class RenderScript {
///////////////////////////////////////////////////////////////////////////////////
//
public RenderScript(Surface sur) {
public RenderScript(Surface sur, boolean useDepth) {
mSurface = sur;
mDev = nDeviceCreate();
mContext = nContextCreate(mDev, mSurface, 0);
mContext = nContextCreate(mDev, mSurface, 0, useDepth);
// TODO: This should be protected by a lock
if(!mElementsInitialized) {

View File

@ -126,7 +126,7 @@ nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
}
static jint
nContextCreate(JNIEnv *_env, jobject _this, jint dev, jobject wnd, jint ver)
nContextCreate(JNIEnv *_env, jobject _this, jint dev, jobject wnd, jint ver, jboolean useDepth)
{
LOG_API("nContextCreate");
@ -142,7 +142,7 @@ nContextCreate(JNIEnv *_env, jobject _this, jint dev, jobject wnd, jint ver)
if (window == NULL)
goto not_valid_surface;
return (jint)rsContextCreate((RsDevice)dev, window, ver);
return (jint)rsContextCreate((RsDevice)dev, window, ver, useDepth);
}
static void
@ -1206,7 +1206,7 @@ static JNINativeMethod methods[] = {
{"_nInit", "()V", (void*)_nInit },
{"nDeviceCreate", "()I", (void*)nDeviceCreate },
{"nDeviceDestroy", "(I)V", (void*)nDeviceDestroy },
{"nContextCreate", "(ILandroid/view/Surface;I)I", (void*)nContextCreate },
{"nContextCreate", "(ILandroid/view/Surface;IZ)I", (void*)nContextCreate },
{"nContextDestroy", "(I)V", (void*)nContextDestroy },
{"nAssignName", "(I[B)V", (void*)nAssignName },
{"nObjDestroy", "(I)V", (void*)nObjDestroy },

View File

@ -49,7 +49,7 @@ typedef void * RsProgramFragmentStore;
RsDevice rsDeviceCreate();
void rsDeviceDestroy(RsDevice);
RsContext rsContextCreate(RsDevice, void *, uint32_t version);
RsContext rsContextCreate(RsDevice, void *, uint32_t version, bool useDepth);
void rsContextDestroy(RsContext);
void rsObjDestroyOOB(RsContext, void *);

View File

@ -36,7 +36,7 @@ class FallView extends RSSurfaceView {
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
RenderScript RS = createRenderScript();
RenderScript RS = createRenderScript(false);
mRender = new FallRS(w, h);
mRender.init(RS, getResources());
}

View File

@ -52,7 +52,7 @@ public class FilmView extends RSSurfaceView {
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
mRS = createRenderScript();
mRS = createRenderScript(true);
mRender = new FilmRS();
mRender.init(mRS, getResources(), w, h);
}

View File

@ -52,7 +52,7 @@ public class FountainView extends RSSurfaceView {
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
mRS = createRenderScript();
mRS = createRenderScript(false);
mRender = new FountainRS();
mRender.init(mRS, getResources(), w, h);
}

View File

@ -54,7 +54,7 @@ public class RolloView extends RSSurfaceView {
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
mRS = createRenderScript();
mRS = createRenderScript(false);
mRender = new RolloRS();
mRender.init(mRS, getResources(), w, h);
}

View File

@ -18,6 +18,7 @@
#include "rsContext.h"
#include "rsThreadIO.h"
#include <ui/FramebufferNativeWindow.h>
#include <ui/EGLUtils.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
@ -29,41 +30,64 @@ pthread_key_t Context::gThreadTLSKey = 0;
void Context::initEGL()
{
mNumConfigs = -1;
mEGL.mNumConfigs = -1;
EGLint configAttribs[128];
EGLint *configAttribsPtr = configAttribs;
EGLint s_configAttribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
#if 1
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
#else
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
#endif
EGL_DEPTH_SIZE, 16,
EGL_NONE
};
memset(configAttribs, 0, sizeof(configAttribs));
mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
configAttribsPtr[0] = EGL_SURFACE_TYPE;
configAttribsPtr[1] = EGL_WINDOW_BIT;
configAttribsPtr += 2;
if (mUseDepth) {
configAttribsPtr[0] = EGL_DEPTH_SIZE;
configAttribsPtr[1] = 16;
configAttribsPtr += 2;
}
configAttribsPtr[0] = EGL_NONE;
rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
if (err) {
LOGE("couldn't find an EGLConfig matching the screen format\n");
}
//eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
if (mWndSurface) {
mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
} else {
mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig,
android_createDisplaySurface(),
NULL);
}
mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, NULL, NULL);
eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
mGL.mVersion = glGetString(GL_VERSION);
mGL.mVendor = glGetString(GL_VENDOR);
mGL.mRenderer = glGetString(GL_RENDERER);
mGL.mExtensions = glGetString(GL_EXTENSIONS);
LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
LOGV("GL Version %s", mGL.mVersion);
LOGV("GL Vendor %s", mGL.mVendor);
LOGV("GL Renderer %s", mGL.mRenderer);
LOGV("GL Extensions %s", mGL.mExtensions);
if (memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
LOGE("Error, OpenGL ES Lite not supported");
}
sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
if (mWndSurface) {
mSurface = eglCreateWindowSurface(mDisplay, mConfig, mWndSurface,
NULL);
} else {
mSurface = eglCreateWindowSurface(mDisplay, mConfig,
android_createDisplaySurface(),
NULL);
}
mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
}
bool Context::runScript(Script *s, uint32_t launchID)
@ -90,19 +114,22 @@ bool Context::runRootScript()
//glColor4f(1,1,1,1);
//glEnable(GL_LIGHT0);
glViewport(0, 0, mWidth, mHeight);
glDepthMask(GL_TRUE);
glViewport(0, 0, mEGL.mWidth, mEGL.mHeight);
#if 1
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glClearColor(mRootScript->mEnviroment.mClearColor[0],
mRootScript->mEnviroment.mClearColor[1],
mRootScript->mEnviroment.mClearColor[2],
mRootScript->mEnviroment.mClearColor[3]);
glClearDepthf(mRootScript->mEnviroment.mClearDepth);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
if (mUseDepth) {
glDepthMask(GL_TRUE);
glClearDepthf(mRootScript->mEnviroment.mClearDepth);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} else {
glClear(GL_COLOR_BUFFER_BIT);
}
#endif
#if RS_LOG_TIMES
timerSet(RS_TIMER_SCRIPT);
#endif
@ -156,13 +183,13 @@ void Context::timerPrint()
void Context::setupCheck()
{
if (mFragmentStore.get()) {
mFragmentStore->setupGL(&mStateFragmentStore);
mFragmentStore->setupGL(this, &mStateFragmentStore);
}
if (mFragment.get()) {
mFragment->setupGL(&mStateFragment);
mFragment->setupGL(this, &mStateFragment);
}
if (mVertex.get()) {
mVertex->setupGL(&mStateVertex);
mVertex->setupGL(this, &mStateVertex);
}
}
@ -186,11 +213,11 @@ void * Context::threadProc(void *vrsc)
LOGE("pthread_setspecific %i", status);
}
rsc->mStateVertex.init(rsc, rsc->mWidth, rsc->mHeight);
rsc->mStateVertex.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
rsc->setVertex(NULL);
rsc->mStateFragment.init(rsc, rsc->mWidth, rsc->mHeight);
rsc->mStateFragment.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
rsc->setFragment(NULL);
rsc->mStateFragmentStore.init(rsc, rsc->mWidth, rsc->mHeight);
rsc->mStateFragmentStore.init(rsc, rsc->mEGL.mWidth, rsc->mEGL.mHeight);
rsc->setFragmentStore(NULL);
rsc->mRunning = true;
@ -204,7 +231,7 @@ void * Context::threadProc(void *vrsc)
#if RS_LOG_TIMES
rsc->timerSet(RS_TIMER_CLEAR_SWAP);
#endif
eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
#if RS_LOG_TIMES
rsc->timerSet(RS_TIMER_INTERNAL);
rsc->timerPrint();
@ -218,18 +245,19 @@ void * Context::threadProc(void *vrsc)
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
eglTerminate(rsc->mDisplay);
eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
eglTerminate(rsc->mEGL.mDisplay);
rsc->objDestroyOOBRun();
return NULL;
}
Context::Context(Device *dev, Surface *sur)
Context::Context(Device *dev, Surface *sur, bool useDepth)
{
dev->addContext(this);
mDev = dev;
mRunning = false;
mExit = false;
mUseDepth = useDepth;
int status;
pthread_attr_t threadAttr;
@ -284,17 +312,6 @@ Context::~Context()
objDestroyOOBDestroy();
}
void Context::swapBuffers()
{
eglSwapBuffers(mDisplay, mSurface);
}
void rsContextSwap(RsContext vrsc)
{
Context *rsc = static_cast<Context *>(vrsc);
rsc->swapBuffers();
}
void Context::setRootScript(Script *s)
{
mRootScript.set(s);
@ -520,10 +537,10 @@ void rsi_ContextSetDefineI32(Context *rsc, const char* name, int32_t value)
}
RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version)
RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
{
Device * dev = static_cast<Device *>(vdev);
Context *rsc = new Context(dev, (Surface *)sur);
Context *rsc = new Context(dev, (Surface *)sur, useDepth);
return rsc;
}

View File

@ -49,7 +49,7 @@ namespace renderscript {
class Context
{
public:
Context(Device *, Surface *);
Context(Device *, Surface *, bool useDepth);
~Context();
static pthread_key_t gThreadTLSKey;
@ -111,8 +111,8 @@ public:
mFloatDefines.add(String8(name), value);
}
uint32_t getWidth() const {return mWidth;}
uint32_t getHeight() const {return mHeight;}
uint32_t getWidth() const {return mEGL.mWidth;}
uint32_t getHeight() const {return mEGL.mHeight;}
ThreadIO mIO;
@ -132,21 +132,38 @@ public:
void timerSet(Timers);
void timerPrint();
bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
protected:
Device *mDev;
EGLint mNumConfigs;
EGLint mMajorVersion;
EGLint mMinorVersion;
EGLConfig mConfig;
EGLContext mContext;
EGLSurface mSurface;
EGLint mWidth;
EGLint mHeight;
EGLDisplay mDisplay;
struct {
EGLint mNumConfigs;
EGLint mMajorVersion;
EGLint mMinorVersion;
EGLConfig mConfig;
EGLContext mContext;
EGLSurface mSurface;
EGLint mWidth;
EGLint mHeight;
EGLDisplay mDisplay;
} mEGL;
struct {
const uint8_t * mVendor;
const uint8_t * mRenderer;
const uint8_t * mVersion;
const uint8_t * mExtensions;
uint32_t mMajorVersion;
uint32_t mMinorVersion;
} mGL;
bool mRunning;
bool mExit;
bool mUseDepth;
pthread_t mThreadId;

View File

@ -43,6 +43,11 @@ void ObjectBase::decRef() const
mRefCount --;
//LOGV("ObjectBase %p dec ref %i", this, mRefCount);
if (!mRefCount) {
if (mName) {
LOGV("Deleting RS object %p, name %s", this, mName);
} else {
LOGV("Deleting RS object %p, no name", this);
}
delete this;
}
}

View File

@ -47,9 +47,3 @@ void Program::checkUpdatedAllocation(const Allocation *alloc)
}
}
void Program::setupGL()
{
}

View File

@ -32,11 +32,7 @@ public:
Program(Element *in, Element *out);
virtual ~Program();
void bindAllocation(Allocation *);
virtual void setupGL();
void checkUpdatedAllocation(const Allocation *);
protected:

View File

@ -40,7 +40,7 @@ ProgramFragment::~ProgramFragment()
{
}
void ProgramFragment::setupGL(ProgramFragmentState *state)
void ProgramFragment::setupGL(const Context *rsc, ProgramFragmentState *state)
{
if ((state->mLast.get() == this) && !mDirty) {
return;
@ -55,7 +55,9 @@ void ProgramFragment::setupGL(ProgramFragmentState *state)
}
glEnable(GL_TEXTURE_2D);
//glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, mPointSpriteEnable);
if (rsc->checkVersion1_1()) {
glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, mPointSpriteEnable);
}
glBindTexture(GL_TEXTURE_2D, mTextures[ct]->getTextureID());
switch(mEnvModes[ct]) {

View File

@ -35,7 +35,7 @@ public:
ProgramFragment(Element *in, Element *out, bool pointSpriteEnable);
virtual ~ProgramFragment();
virtual void setupGL(ProgramFragmentState *);
virtual void setupGL(const Context *, ProgramFragmentState *);

View File

@ -48,7 +48,7 @@ ProgramFragmentStore::~ProgramFragmentStore()
{
}
void ProgramFragmentStore::setupGL(ProgramFragmentStoreState *state)
void ProgramFragmentStore::setupGL(const Context *rsc, ProgramFragmentStoreState *state)
{
if (state->mLast.get() == this) {
return;

View File

@ -31,7 +31,7 @@ public:
ProgramFragmentStore(Element *in, Element *out);
virtual ~ProgramFragmentStore();
virtual void setupGL(ProgramFragmentStoreState *);
virtual void setupGL(const Context *, ProgramFragmentStoreState *);
void setDepthFunc(RsDepthFunc);
void setDepthMask(bool);

View File

@ -44,7 +44,7 @@ static void logMatrix(const char *txt, const float *f)
LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]);
}
void ProgramVertex::setupGL(ProgramVertexState *state)
void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state)
{
if ((state->mLast.get() == this) && !mDirty) {
return;

View File

@ -33,7 +33,7 @@ public:
ProgramVertex(Element *in, Element *out);
virtual ~ProgramVertex();
virtual void setupGL(ProgramVertexState *state);
virtual void setupGL(const Context *rsc, ProgramVertexState *state);
void setTextureMatrixEnable(bool e) {mTextureMatrixEnable = e;}