Defer EGL init until the surface changed call comes in.  Pass w,h along with surface for verification of driver state.
diff --git a/RenderScript.h b/RenderScript.h
index 9b04393..3ca8b15 100644
--- a/RenderScript.h
+++ b/RenderScript.h
@@ -55,7 +55,7 @@
 void rsDeviceDestroy(RsDevice);
 void rsDeviceSetConfig(RsDevice, RsDeviceParam, int32_t value);
 
-RsContext rsContextCreate(RsDevice, void *, uint32_t version, bool useDepth);
+RsContext rsContextCreate(RsDevice, uint32_t version, bool useDepth);
 void rsContextDestroy(RsContext);
 void rsObjDestroyOOB(RsContext, void *);
 
diff --git a/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index b1facfc..334fd9c 100644
--- a/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
@@ -126,13 +126,14 @@
     }
 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
+        mRS.contextSetSurface(width, height, holder.getSurface());
     }
 
     public void surfaceDestroyed(SurfaceHolder holder) {
     }
         
     private Script.Invokable createScript() {
-        mRS = new RenderScript(mSurfaceView.getHolder().getSurface(), false, false);
+        mRS = new RenderScript(false, false);
         mRS.mMessageCallback = new FilterCallback();
 
         mParamsType = Type.createFromClass(mRS, Params.class, 1, "Parameters");
diff --git a/rs.spec b/rs.spec
index 865e435..da25a27 100644
--- a/rs.spec
+++ b/rs.spec
@@ -37,9 +37,15 @@
 	}
 
 ContextSetSurface {
+	param uint32_t width
+	param uint32_t height
 	param void *sur
 	}
 
+ContextSetPriority {
+	param uint32_t priority
+	}
+
 AssignName {
 	param void *obj
 	param const char *name
diff --git a/rsContext.cpp b/rsContext.cpp
index 3e4cc36..ab3809d 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -92,38 +92,12 @@
         LOGE("eglCreateContext returned EGL_NO_CONTEXT");
     }
     gGLContextCount++;
-
-    if (mWndSurface) {
-        setSurface(mWndSurface);
-    } else {
-        setSurface((Surface *)android_createDisplaySurface());
-    }
-
-    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 ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
-        LOGE("Error, OpenGL ES Lite not supported");
-    } else {
-        sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
-    }
 }
 
 void Context::deinitEGL()
 {
-    setSurface(NULL);
+    LOGV("deinitEGL");
+    setSurface(0, 0, NULL);
     eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
     checkEglError("eglDestroyContext");
 
@@ -265,9 +239,9 @@
      rsc->props.mLogScripts = getProp("debug.rs.script");
      rsc->props.mLogObjects = getProp("debug.rs.objects");
 
-     pthread_mutex_lock(&gInitMutex);
-     rsc->initEGL();
-     pthread_mutex_unlock(&gInitMutex);
+     //pthread_mutex_lock(&gInitMutex);
+     //rsc->initEGL();
+     //pthread_mutex_unlock(&gInitMutex);
 
      ScriptTLSStruct *tlsStruct = new ScriptTLSStruct;
      if (!tlsStruct) {
@@ -342,7 +316,7 @@
      return NULL;
 }
 
-Context::Context(Device *dev, Surface *sur, bool useDepth)
+Context::Context(Device *dev, bool useDepth)
 {
     pthread_mutex_lock(&gInitMutex);
 
@@ -353,6 +327,7 @@
     mUseDepth = useDepth;
     mPaused = false;
     mObjHead = NULL;
+    memset(&mEGL, 0, sizeof(mEGL));
 
     int status;
     pthread_attr_t threadAttr;
@@ -380,7 +355,7 @@
     sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
     pthread_attr_setschedparam(&threadAttr, &sparam);
 
-    mWndSurface = sur;
+    mWndSurface = NULL;
 
     objDestroyOOBInit();
     timerInit();
@@ -426,8 +401,10 @@
     objDestroyOOBDestroy();
 }
 
-void Context::setSurface(Surface *sur)
+void Context::setSurface(uint32_t w, uint32_t h, Surface *sur)
 {
+    LOGV("setSurface %i %i %p", w, h, sur);
+
     EGLBoolean ret;
     if (mEGL.mSurface != NULL) {
         ret = eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
@@ -437,10 +414,22 @@
         checkEglError("eglDestroySurface", ret);
 
         mEGL.mSurface = NULL;
+        mEGL.mWidth = 0;
+        mEGL.mHeight = 0;
+        mWidth = 0;
+        mHeight = 0;
     }
 
     mWndSurface = sur;
     if (mWndSurface != NULL) {
+        bool first = false;
+        if (!mEGL.mContext) {
+            first = true;
+            pthread_mutex_lock(&gInitMutex);
+            initEGL();
+            pthread_mutex_unlock(&gInitMutex);
+        }
+
         mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
         checkEglError("eglCreateWindowSurface");
         if (mEGL.mSurface == EGL_NO_SURFACE) {
@@ -449,6 +438,35 @@
 
         ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
         checkEglError("eglMakeCurrent", ret);
+
+        eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_WIDTH, &mEGL.mWidth);
+        eglQuerySurface(mEGL.mDisplay, mEGL.mSurface, EGL_HEIGHT, &mEGL.mHeight);
+        mWidth = w;
+        mHeight = h;
+
+        if ((int)mWidth != mEGL.mWidth || (int)mHeight != mEGL.mHeight) {
+            LOGE("EGL/Surface mismatch  EGL (%i x %i)  SF (%i x %i)", mEGL.mWidth, mEGL.mHeight, mWidth, mHeight);
+        }
+
+        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);
+
+            if ((strlen((const char *)mGL.mVersion) < 12) || memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
+                LOGE("Error, OpenGL ES Lite not supported");
+            } else {
+                sscanf((const char *)mGL.mVersion + 13, "%i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
+            }
+        }
+
     }
 }
 
@@ -767,19 +785,23 @@
     rsc->resume();
 }
 
-void rsi_ContextSetSurface(Context *rsc, void *sur)
+void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, void *sur)
 {
-    rsc->setSurface((Surface *)sur);
+    rsc->setSurface(w, h, (Surface *)sur);
+}
+
+void rsi_ContextSetPriority(Context *rsc, uint32_t p)
+{
 }
 
 }
 }
 
 
-RsContext rsContextCreate(RsDevice vdev, void *sur, uint32_t version, bool useDepth)
+RsContext rsContextCreate(RsDevice vdev, uint32_t version, bool useDepth)
 {
     Device * dev = static_cast<Device *>(vdev);
-    Context *rsc = new Context(dev, (Surface *)sur, useDepth);
+    Context *rsc = new Context(dev, useDepth);
     return rsc;
 }
 
diff --git a/rsContext.h b/rsContext.h
index bffc55b..f3803a5 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -49,7 +49,7 @@
 class Context
 {
 public:
-    Context(Device *, Surface *, bool useDepth);
+    Context(Device *, bool useDepth);
     ~Context();
 
     static pthread_key_t gThreadTLSKey;
@@ -94,7 +94,7 @@
 
     void pause();
     void resume();
-    void setSurface(Surface *sur);
+    void setSurface(uint32_t w, uint32_t h, Surface *sur);
 
     void assignName(ObjectBase *obj, const char *name, uint32_t len);
     void removeName(ObjectBase *obj);
@@ -189,6 +189,9 @@
 
     } mGL;
 
+    uint32_t mWidth;
+    uint32_t mHeight;
+
     bool mRunning;
     bool mExit;
     bool mUseDepth;
diff --git a/rsLocklessFifo.cpp b/rsLocklessFifo.cpp
index 085a81e..c796520 100644
--- a/rsLocklessFifo.cpp
+++ b/rsLocklessFifo.cpp
@@ -57,7 +57,7 @@
     mPut = mBuffer;
     mGet = mBuffer;
     mEnd = mBuffer + (sizeInBytes) - 1;
-    dumpState("init");
+    //dumpState("init");
     return true;
 }
 
diff --git a/rsUtils.h b/rsUtils.h
index 63d73a1..07f8933 100644
--- a/rsUtils.h
+++ b/rsUtils.h
@@ -18,7 +18,7 @@
 #define ANDROID_RS_UTILS_H
 
 #define LOG_NDEBUG 0
-#define LOG_TAG "rs"
+#define LOG_TAG "RenderScript"
 #include <utils/Log.h>
 #include <utils/Vector.h>
 #include <utils/KeyedVector.h>