Fix eglDestroyContext and glTexImage2D

1. When destroy context that is in use, EGL spec says:
   "If the EGL rendering context context is not current to any thread,
   eglDestroyContext destroys it immediately. Otherwise, context is
   destroyed when it becomes not current to any thread."
2. When calling glTexImage2D, should bind the correct texture first.

Change-Id: I6c779b71d1e6002b8a484477921ba323acbd986e
diff --git a/system/GLESv2_enc/GL2Encoder.cpp b/system/GLESv2_enc/GL2Encoder.cpp
index 0575935..80a2cda 100644
--- a/system/GLESv2_enc/GL2Encoder.cpp
+++ b/system/GLESv2_enc/GL2Encoder.cpp
@@ -121,6 +121,7 @@
     OVERRIDE(glTexParameterfv);
     OVERRIDE(glTexParameteri);
     OVERRIDE(glTexParameteriv);
+    OVERRIDE(glTexImage2D);
 }
 
 GL2Encoder::~GL2Encoder()
@@ -1211,6 +1212,23 @@
     }
 }
 
+void GL2Encoder::s_glTexImage2D(void* self, GLenum target, GLint level,
+        GLint internalformat, GLsizei width, GLsizei height, GLint border,
+        GLenum format, GLenum type, const GLvoid* pixels)
+{
+    GL2Encoder* ctx = (GL2Encoder*)self;
+    if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
+        ctx->override2DTextureTarget(target);
+        ctx->m_glTexImage2D_enc(ctx, target, level, internalformat, width,
+                height, border, format, type, pixels);
+        ctx->restore2DTextureTarget();
+    } else {
+        ctx->m_glTexImage2D_enc(ctx, target, level, internalformat, width,
+                height, border, format, type, pixels);
+    }
+}
+
+
 void GL2Encoder::s_glTexParameteriv(void* self,
         GLenum target, GLenum pname, const GLint* params)
 {