libgui: have ST::updateTexImage check the GL ctx

This change adds a check to SurfaceTexture::updateTexImage to verify
that the current GL context is the same as the one that was used for
previous updateTexImage calls.

Change-Id: If02d2f787bcfdb528046dc9ddf6665f8a90e1bf4
diff --git a/libs/gui/SurfaceTexture.cpp b/libs/gui/SurfaceTexture.cpp
index d8dd5bd..0f21255 100644
--- a/libs/gui/SurfaceTexture.cpp
+++ b/libs/gui/SurfaceTexture.cpp
@@ -115,6 +115,8 @@
     mUseFenceSync(false),
 #endif
     mTexTarget(texTarget),
+    mEglDisplay(EGL_NO_DISPLAY),
+    mEglContext(EGL_NO_CONTEXT),
     mAbandoned(false),
     mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
 {
@@ -178,6 +180,22 @@
         return NO_INIT;
     }
 
+    EGLDisplay dpy = eglGetCurrentDisplay();
+    EGLContext ctx = eglGetCurrentContext();
+
+    if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
+        ST_LOGE("updateTexImage: invalid current EGLDisplay");
+        return -EINVAL;
+    }
+
+    if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
+        ST_LOGE("updateTexImage: invalid current EGLContext");
+        return -EINVAL;
+    }
+
+    mEglDisplay = dpy;
+    mEglContext = ctx;
+
     BufferQueue::BufferItem item;
 
     // In asynchronous mode the list is guaranteed to be one buffer
@@ -188,17 +206,14 @@
         if (item.mGraphicBuffer != NULL) {
             mEGLSlots[buf].mGraphicBuffer = 0;
             if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
-                eglDestroyImageKHR(mEGLSlots[buf].mEglDisplay,
-                        mEGLSlots[buf].mEglImage);
+                eglDestroyImageKHR(dpy, mEGLSlots[buf].mEglImage);
                 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
-                mEGLSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
             }
             mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
         }
 
         // Update the GL texture object.
         EGLImageKHR image = mEGLSlots[buf].mEglImage;
-        EGLDisplay dpy = eglGetCurrentDisplay();
         if (image == EGL_NO_IMAGE_KHR) {
             if (item.mGraphicBuffer == 0) {
                 ST_LOGE("buffer at slot %d is null", buf);
@@ -206,7 +221,6 @@
             }
             image = createImage(dpy, item.mGraphicBuffer);
             mEGLSlots[buf].mEglImage = image;
-            mEGLSlots[buf].mEglDisplay = dpy;
             if (image == EGL_NO_IMAGE_KHR) {
                 // NOTE: if dpy was invalid, createImage() is guaranteed to
                 // fail. so we'd end up here.
@@ -229,8 +243,7 @@
             failed = true;
         }
         if (failed) {
-            mBufferQueue->releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
-                    mEGLSlots[buf].mFence);
+            mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence);
             return -EINVAL;
         }
 
@@ -241,7 +254,7 @@
                 if (fence == EGL_NO_SYNC_KHR) {
                     ALOGE("updateTexImage: error creating fence: %#x",
                             eglGetError());
-                    mBufferQueue->releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
+                    mBufferQueue->releaseBuffer(buf, dpy,
                             mEGLSlots[buf].mFence);
                     return -EINVAL;
                 }
@@ -256,8 +269,7 @@
                 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
 
         // release old buffer
-        mBufferQueue->releaseBuffer(mCurrentTexture,
-                mEGLSlots[mCurrentTexture].mEglDisplay,
+        mBufferQueue->releaseBuffer(mCurrentTexture, dpy,
                 mEGLSlots[mCurrentTexture].mFence);
 
         // Update the SurfaceTexture state.
@@ -459,10 +471,9 @@
     if (mEGLSlots[slotIndex].mEglImage != EGL_NO_IMAGE_KHR) {
         EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
         if (img != EGL_NO_IMAGE_KHR) {
-            eglDestroyImageKHR(mEGLSlots[slotIndex].mEglDisplay, img);
+            eglDestroyImageKHR(mEglDisplay, img);
         }
         mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
-        mEGLSlots[slotIndex].mEglDisplay = EGL_NO_DISPLAY;
     }
 }