Begin hooking up SurfaceConfig.

Change-Id: I328138f29affbed11fcfb5e9ed0872d4ba22d241
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 0f9ed87..97ce7dc 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -69,9 +69,22 @@
 
     // Methods below are wrapped to protect the non-threadsafe
     // lockless fifo.
-    native int  rsnContextCreateGL(int dev, int ver, boolean useDepth);
-    synchronized int nContextCreateGL(int dev, int ver, boolean useDepth) {
-        return rsnContextCreateGL(dev, ver, useDepth);
+    native int  rsnContextCreateGL(int dev, int ver,
+                 int colorMin, int colorPref,
+                 int alphaMin, int alphaPref,
+                 int depthMin, int depthPref,
+                 int stencilMin, int stencilPref,
+                 int samplesMin, int samplesPref, float samplesQ);
+    synchronized int nContextCreateGL(int dev, int ver,
+                 int colorMin, int colorPref,
+                 int alphaMin, int alphaPref,
+                 int depthMin, int depthPref,
+                 int stencilMin, int stencilPref,
+                 int samplesMin, int samplesPref, float samplesQ) {
+        return rsnContextCreateGL(dev, ver, colorMin, colorPref,
+                                  alphaMin, alphaPref, depthMin, depthPref,
+                                  stencilMin, stencilPref,
+                                  samplesMin, samplesPref, samplesQ);
     }
     native int  rsnContextCreate(int dev, int ver);
     synchronized int nContextCreate(int dev, int ver) {
diff --git a/graphics/java/android/renderscript/RenderScriptGL.java b/graphics/java/android/renderscript/RenderScriptGL.java
index b60c689..0477d75 100644
--- a/graphics/java/android/renderscript/RenderScriptGL.java
+++ b/graphics/java/android/renderscript/RenderScriptGL.java
@@ -18,6 +18,7 @@
 
 import java.lang.reflect.Field;
 
+import android.graphics.PixelFormat;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.util.Config;
@@ -103,7 +104,11 @@
     SurfaceConfig mSurfaceConfig;
 
     public void configureSurface(SurfaceHolder sh) {
-        //getHolder().setFormat(PixelFormat.TRANSLUCENT);
+        if (mSurfaceConfig.mAlphaMin > 1) {
+            sh.setFormat(PixelFormat.RGBA_8888);
+        } else {
+            sh.setFormat(PixelFormat.RGBX_8888);
+        }
     }
 
     public void checkSurface(SurfaceHolder sh) {
@@ -112,13 +117,17 @@
     public RenderScriptGL(SurfaceConfig sc) {
         mSurfaceConfig = new SurfaceConfig(sc);
 
-
-
         mSurface = null;
         mWidth = 0;
         mHeight = 0;
         mDev = nDeviceCreate();
-        mContext = nContextCreateGL(mDev, 0, mSurfaceConfig.mDepthMin > 0);
+        mContext = nContextCreateGL(mDev, 0,
+                                    mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
+                                    mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
+                                    mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
+                                    mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
+                                    mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
+                                    mSurfaceConfig.mSamplesQ);
         mMessageThread = new MessageThread(this);
         mMessageThread.start();
         Element.initPredefined(this);
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 014f03b..ce2a40c 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -158,10 +158,26 @@
 }
 
 static jint
-nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver, jboolean useDepth)
+nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver,
+                 int colorMin, int colorPref,
+                 int alphaMin, int alphaPref,
+                 int depthMin, int depthPref,
+                 int stencilMin, int stencilPref,
+                 int samplesMin, int samplesPref, float samplesQ)
 {
+    RsSurfaceConfig sc;
+    sc.alphaMin = alphaMin;
+    sc.alphaPref = alphaPref;
+    sc.colorMin = colorMin;
+    sc.colorPref = colorPref;
+    sc.depthMin = depthMin;
+    sc.depthPref = depthPref;
+    sc.samplesMin = samplesMin;
+    sc.samplesPref = samplesPref;
+    sc.samplesQ = samplesQ;
+
     LOG_API("nContextCreateGL");
-    return (jint)rsContextCreateGL((RsDevice)dev, ver, useDepth);
+    return (jint)rsContextCreateGL((RsDevice)dev, ver, sc);
 }
 
 static void
@@ -309,7 +325,7 @@
 
     rsElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
 
-    for(jint i = 0; i < dataSize; i ++) {
+    for(jint i = 0; i < dataSize; i++) {
         _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
         _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
     }
@@ -1220,7 +1236,7 @@
 
 // All methods below are thread protected in java.
 {"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
-{"rsnContextCreateGL",               "(IIZ)I",                                (void*)nContextCreateGL },
+{"rsnContextCreateGL",               "(IIIIIIIIIIIIF)I",                      (void*)nContextCreateGL },
 {"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
 {"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
 {"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h
index 66e27f3..da8edbe 100644
--- a/libs/rs/RenderScript.h
+++ b/libs/rs/RenderScript.h
@@ -55,12 +55,26 @@
     RS_DEVICE_PARAM_COUNT
 };
 
+typedef struct {
+    uint32_t colorMin;
+    uint32_t colorPref;
+    uint32_t alphaMin;
+    uint32_t alphaPref;
+    uint32_t depthMin;
+    uint32_t depthPref;
+    uint32_t stencilMin;
+    uint32_t stencilPref;
+    uint32_t samplesMin;
+    uint32_t samplesPref;
+    float samplesQ;
+} RsSurfaceConfig;
+
 RsDevice rsDeviceCreate();
 void rsDeviceDestroy(RsDevice);
 void rsDeviceSetConfig(RsDevice, RsDeviceParam, int32_t value);
 
 RsContext rsContextCreate(RsDevice, uint32_t version);
-RsContext rsContextCreateGL(RsDevice, uint32_t version, bool useDepth);
+RsContext rsContextCreateGL(RsDevice, uint32_t version, RsSurfaceConfig sc);
 void rsContextDestroy(RsContext);
 
 uint32_t rsContextGetMessage(RsContext, void *data, size_t *receiveLen, size_t bufferLen, bool wait);
@@ -270,6 +284,7 @@
 
 } RsScriptCall;
 
+
 #ifndef NO_RS_FUNCS
 #include "rsgApiFuncDecl.h"
 #endif
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index 30add62..be8fb0e 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -18,6 +18,7 @@
 #include "rsContext.h"
 #include "rsThreadIO.h"
 #include <ui/FramebufferNativeWindow.h>
+#include <ui/PixelFormat.h>
 #include <ui/EGLUtils.h>
 #include <ui/egl/android_natives.h>
 
@@ -56,8 +57,61 @@
     }
 }
 
-void Context::initEGL(bool useGL2)
+void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
+
+#define X(VAL) {VAL, #VAL}
+    struct {EGLint attribute; const char* name;} names[] = {
+    X(EGL_BUFFER_SIZE),
+    X(EGL_ALPHA_SIZE),
+    X(EGL_BLUE_SIZE),
+    X(EGL_GREEN_SIZE),
+    X(EGL_RED_SIZE),
+    X(EGL_DEPTH_SIZE),
+    X(EGL_STENCIL_SIZE),
+    X(EGL_CONFIG_CAVEAT),
+    X(EGL_CONFIG_ID),
+    X(EGL_LEVEL),
+    X(EGL_MAX_PBUFFER_HEIGHT),
+    X(EGL_MAX_PBUFFER_PIXELS),
+    X(EGL_MAX_PBUFFER_WIDTH),
+    X(EGL_NATIVE_RENDERABLE),
+    X(EGL_NATIVE_VISUAL_ID),
+    X(EGL_NATIVE_VISUAL_TYPE),
+    X(EGL_SAMPLES),
+    X(EGL_SAMPLE_BUFFERS),
+    X(EGL_SURFACE_TYPE),
+    X(EGL_TRANSPARENT_TYPE),
+    X(EGL_TRANSPARENT_RED_VALUE),
+    X(EGL_TRANSPARENT_GREEN_VALUE),
+    X(EGL_TRANSPARENT_BLUE_VALUE),
+    X(EGL_BIND_TO_TEXTURE_RGB),
+    X(EGL_BIND_TO_TEXTURE_RGBA),
+    X(EGL_MIN_SWAP_INTERVAL),
+    X(EGL_MAX_SWAP_INTERVAL),
+    X(EGL_LUMINANCE_SIZE),
+    X(EGL_ALPHA_MASK_SIZE),
+    X(EGL_COLOR_BUFFER_TYPE),
+    X(EGL_RENDERABLE_TYPE),
+    X(EGL_CONFORMANT),
+   };
+#undef X
+
+    for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
+        EGLint value = -1;
+        EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
+        EGLint error = eglGetError();
+        if (returnVal && error == EGL_SUCCESS) {
+            LOGV(" %s: %d (0x%x)", names[j].name, value, value);
+        }
+    }
+}
+
+
+void Context::initGLThread()
 {
+    pthread_mutex_lock(&gInitMutex);
+    LOGV("initGLThread start %p", this);
+
     mEGL.mNumConfigs = -1;
     EGLint configAttribs[128];
     EGLint *configAttribsPtr = configAttribs;
@@ -69,15 +123,13 @@
     configAttribsPtr[1] = EGL_WINDOW_BIT;
     configAttribsPtr += 2;
 
-    if (useGL2) {
-        configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
-        configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
-        configAttribsPtr += 2;
-    }
+    configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
+    configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
+    configAttribsPtr += 2;
 
-    if (mUseDepth) {
+    if (mUserSurfaceConfig.depthMin > 0) {
         configAttribsPtr[0] = EGL_DEPTH_SIZE;
-        configAttribsPtr[1] = 16;
+        configAttribsPtr[1] = mUserSurfaceConfig.depthMin;
         configAttribsPtr += 2;
     }
 
@@ -97,29 +149,94 @@
     eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
     checkEglError("eglInitialize");
 
-    status_t err = EGLUtils::selectConfigForNativeWindow(mEGL.mDisplay, configAttribs, mWndSurface, &mEGL.mConfig);
+#if 1
+    PixelFormat pf = PIXEL_FORMAT_RGBA_8888;
+    if (mUserSurfaceConfig.alphaMin == 0) {
+        pf = PIXEL_FORMAT_RGBX_8888;
+    }
+
+    status_t err = EGLUtils::selectConfigForPixelFormat(mEGL.mDisplay, configAttribs, pf, &mEGL.mConfig);
     if (err) {
        LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
     }
-    //eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
-
-
-    if (useGL2) {
-        mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
-    } else {
-        mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, NULL);
+    if (props.mLogVisual) {
+        printEGLConfiguration(mEGL.mDisplay, mEGL.mConfig);
     }
+#else
+    eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
+#endif
+
+    mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
     checkEglError("eglCreateContext");
     if (mEGL.mContext == EGL_NO_CONTEXT) {
         LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
     }
     gGLContextCount++;
+
+
+    EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
+    mEGL.mSurfaceDefault = eglCreatePbufferSurface(mEGL.mDisplay, mEGL.mConfig, pbuffer_attribs);
+    checkEglError("eglCreatePbufferSurface");
+    if (mEGL.mSurfaceDefault == EGL_NO_SURFACE) {
+        LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
+    }
+
+    EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
+    checkEglError("eglMakeCurrent", ret);
+
+    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);
+
+    const char *verptr = NULL;
+    if (strlen((const char *)mGL.mVersion) > 9) {
+        if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
+            verptr = (const char *)mGL.mVersion + 12;
+        }
+        if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
+            verptr = (const char *)mGL.mVersion + 9;
+        }
+    }
+
+    if (!verptr) {
+        LOGE("Error, OpenGL ES Lite not supported");
+    } else {
+        sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
+    }
+
+    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
+    glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
+    glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
+
+    glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
+    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
+
+    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
+    glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
+
+    mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
+    mGL.EXT_texture_max_aniso = 1.0f;
+    bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
+    if(hasAniso) {
+        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
+    }
+
+    LOGV("initGLThread end %p", this);
+    pthread_mutex_unlock(&gInitMutex);
 }
 
 void Context::deinitEGL()
 {
     LOGV("%p, deinitEGL", this);
-    setSurface(0, 0, NULL);
+
+    eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEGL.mContext);
     eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
     checkEglError("eglDestroyContext");
 
@@ -312,6 +429,8 @@
          LOGE("pthread_setspecific %i", status);
      }
 
+     rsc->initGLThread();
+
      rsc->mScriptC.init(rsc);
      if (rsc->mIsGraphicsContext) {
          rsc->mStateRaster.init(rsc);
@@ -460,7 +579,7 @@
 #endif
 }
 
-Context::Context(Device *dev, bool isGraphics, bool useDepth)
+Context::Context(Device *dev, const RsSurfaceConfig *sc)
 {
     pthread_mutex_lock(&gInitMutex);
 
@@ -468,15 +587,19 @@
     mDev = dev;
     mRunning = false;
     mExit = false;
-    mUseDepth = useDepth;
     mPaused = false;
     mObjHead = NULL;
     mError = RS_ERROR_NONE;
     mErrorMsg = NULL;
+    if (sc) {
+        mUserSurfaceConfig = *sc;
+    } else {
+        memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
+    }
 
     memset(&mEGL, 0, sizeof(mEGL));
     memset(&mGL, 0, sizeof(mGL));
-    mIsGraphicsContext = isGraphics;
+    mIsGraphicsContext = sc != NULL;
 
     int status;
     pthread_attr_t threadAttr;
@@ -490,6 +613,7 @@
         }
     }
     gThreadTLSKeyCount++;
+
     pthread_mutex_unlock(&gInitMutex);
 
     // Global init done at this point.
@@ -534,8 +658,6 @@
             break;
         }
     }
-
-
     pthread_attr_destroy(&threadAttr);
 }
 
@@ -568,28 +690,21 @@
 
     EGLBoolean ret;
     if (mEGL.mSurface != NULL) {
-        ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+        ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
         checkEglError("eglMakeCurrent", ret);
 
         ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
         checkEglError("eglDestroySurface", ret);
 
         mEGL.mSurface = NULL;
-        mWidth = 0;
-        mHeight = 0;
+        mWidth = 1;
+        mHeight = 1;
     }
 
     mWndSurface = sur;
     if (mWndSurface != NULL) {
         mWidth = w;
         mHeight = h;
-        bool first = false;
-        if (!mEGL.mContext) {
-            first = true;
-            pthread_mutex_lock(&gInitMutex);
-            initEGL(true);
-            pthread_mutex_unlock(&gInitMutex);
-        }
 
         mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
         checkEglError("eglCreateWindowSurface");
@@ -601,53 +716,6 @@
         checkEglError("eglMakeCurrent", ret);
 
         mStateVertex.updateSize(this);
-
-        if (first) {
-            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);
-
-            const char *verptr = NULL;
-            if (strlen((const char *)mGL.mVersion) > 9) {
-                if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
-                    verptr = (const char *)mGL.mVersion + 12;
-                }
-                if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
-                    verptr = (const char *)mGL.mVersion + 9;
-                }
-            }
-
-            if (!verptr) {
-                LOGE("Error, OpenGL ES Lite not supported");
-            } else {
-                sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
-            }
-
-            glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
-            glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
-            glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
-
-            glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
-            glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
-
-            glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
-            glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
-
-            mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
-            mGL.EXT_texture_max_aniso = 1.0f;
-            bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
-            if(hasAniso) {
-                glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
-            }
-        }
-
     }
 }
 
@@ -829,7 +897,7 @@
     LOGE(" GL Extensions: %s", mGL.mExtensions);
     LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
     LOGE(" RS width %i, height %i", mWidth, mHeight);
-    LOGE(" RS running %i, exit %i, useDepth %i, paused %i", mRunning, mExit, mUseDepth, mPaused);
+    LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
     LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
 
     LOGV("MAX Textures %i, %i  %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
@@ -957,15 +1025,15 @@
 {
     LOGV("rsContextCreate %p", vdev);
     Device * dev = static_cast<Device *>(vdev);
-    Context *rsc = new Context(dev, false, false);
+    Context *rsc = new Context(dev, NULL);
     return rsc;
 }
 
-RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, bool useDepth)
+RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc)
 {
-    LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
+    LOGV("rsContextCreateGL %p", vdev);
     Device * dev = static_cast<Device *>(vdev);
-    Context *rsc = new Context(dev, true, useDepth);
+    Context *rsc = new Context(dev, &sc);
     LOGV("rsContextCreateGL ret %p ", rsc);
     return rsc;
 }
diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h
index 8a8b8a8..dbe2c79 100644
--- a/libs/rs/rsContext.h
+++ b/libs/rs/rsContext.h
@@ -69,7 +69,7 @@
 class Context
 {
 public:
-    Context(Device *, bool isGraphics, bool useDepth);
+    Context(Device *, const RsSurfaceConfig *sc);
     ~Context();
 
     static pthread_key_t gThreadTLSKey;
@@ -82,6 +82,7 @@
         Script * mScript;
     };
     ScriptTLSStruct *mTlsStruct;
+    RsSurfaceConfig mUserSurfaceConfig;
 
     typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
 
@@ -207,6 +208,7 @@
         EGLConfig mConfig;
         EGLContext mContext;
         EGLSurface mSurface;
+        EGLSurface mSurfaceDefault;
         EGLDisplay mDisplay;
     } mEGL;
 
@@ -240,7 +242,6 @@
 
     bool mRunning;
     bool mExit;
-    bool mUseDepth;
     bool mPaused;
     RsError mError;
     const char *mErrorMsg;
@@ -274,7 +275,8 @@
 private:
     Context();
 
-    void initEGL(bool useGL2);
+    void initEGL();
+    void initGLThread();
     void deinitEGL();
 
     uint32_t runRootScript();
diff --git a/libs/rs/rsProgramStore.cpp b/libs/rs/rsProgramStore.cpp
index 3f90d7a..586a89f 100644
--- a/libs/rs/rsProgramStore.cpp
+++ b/libs/rs/rsProgramStore.cpp
@@ -76,14 +76,25 @@
 
     //LOGE("pfs  %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
 
-    glDepthMask(mDepthWriteEnable);
-    if(mDepthTestEnable || mDepthWriteEnable) {
-        glEnable(GL_DEPTH_TEST);
-        glDepthFunc(mDepthFunc);
+    if (rsc->mUserSurfaceConfig.depthMin > 0) {
+        glDepthMask(mDepthWriteEnable);
+        if(mDepthTestEnable || mDepthWriteEnable) {
+            glEnable(GL_DEPTH_TEST);
+            glDepthFunc(mDepthFunc);
+        } else {
+            glDisable(GL_DEPTH_TEST);
+        }
     } else {
+        glDepthMask(false);
         glDisable(GL_DEPTH_TEST);
     }
 
+    if (rsc->mUserSurfaceConfig.stencilMin > 0) {
+    } else {
+        glStencilMask(0);
+        glDisable(GL_STENCIL_TEST);
+    }
+
     if (mDitherEnable) {
         glEnable(GL_DITHER);
     } else {