Move egl texture binding to the base texture class.

It's still only valid to bind a 2D texture but the validation layer
verifies that.

BUG=angle:681

Change-Id: I744dc32f9bd0f69b1e1235b8feef7e796167d033
Reviewed-on: https://chromium-review.googlesource.com/236931
Reviewed-by: Brandon Jones <bajones@chromium.org>
Tested-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/Texture.cpp b/src/libANGLE/Texture.cpp
index e1e9d5d..cd86bee 100644
--- a/src/libANGLE/Texture.cpp
+++ b/src/libANGLE/Texture.cpp
@@ -54,12 +54,18 @@
       mTextureSerial(issueTextureSerial()),
       mUsage(GL_NONE),
       mImmutableLevelCount(0),
-      mTarget(target)
+      mTarget(target),
+      mBoundSurface(NULL)
 {
 }
 
 Texture::~Texture()
 {
+    if (mBoundSurface)
+    {
+        mBoundSurface->releaseTexImage(EGL_BACK_BUFFER);
+        mBoundSurface = NULL;
+    }
     SafeDelete(mTexture);
 }
 
@@ -134,6 +140,8 @@
         return error;
     }
 
+    releaseTexImage();
+
     setImageDesc(ImageIdentifier(target, level), ImageDesc(size, GetSizedInternalFormat(internalFormat, type)));
 
     return Error(GL_NO_ERROR);
@@ -158,6 +166,8 @@
         return error;
     }
 
+    releaseTexImage();
+
     setImageDesc(ImageIdentifier(target, level), ImageDesc(size, GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE)));
 
     return Error(GL_NO_ERROR);
@@ -182,6 +192,8 @@
         return error;
     }
 
+    releaseTexImage();
+
     setImageDesc(ImageIdentifier(target, level), ImageDesc(Extents(sourceArea.width, sourceArea.height, 1),
                                                                    GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE)));
 
@@ -206,6 +218,8 @@
         return error;
     }
 
+    releaseTexImage();
+
     mImmutableLevelCount = levels;
     clearImageDescs();
     setImageDescChain(levels, size, internalFormat);
@@ -222,6 +236,8 @@
         return error;
     }
 
+    releaseTexImage();
+
     ImageIdentifier baseLevel(mTarget == GL_TEXTURE_CUBE_MAP ? GL_TEXTURE_CUBE_MAP_POSITIVE_X : mTarget, 0);
     const ImageDesc &baseImageInfo = getImageDesc(baseLevel);
     size_t mipLevels = log2(std::max(std::max(baseImageInfo.size.width, baseImageInfo.size.height), baseImageInfo.size.depth)) + 1;
@@ -297,103 +313,29 @@
     mImageDescs.clear();
 }
 
+void Texture::bindTexImage(egl::Surface *surface)
+{
+    releaseTexImage();
+    mTexture->bindTexImage(surface);
+    mBoundSurface = surface;
+}
+
+void Texture::releaseTexImage()
+{
+    if (mBoundSurface)
+    {
+        mBoundSurface = NULL;
+        mTexture->releaseTexImage();
+    }
+}
+
 Texture2D::Texture2D(rx::TextureImpl *impl, GLuint id)
     : Texture(impl, id, GL_TEXTURE_2D)
 {
-    mSurface = NULL;
 }
 
 Texture2D::~Texture2D()
 {
-    if (mSurface)
-    {
-        mSurface->releaseTexImage(EGL_BACK_BUFFER);
-        mSurface = NULL;
-    }
-}
-
-Error Texture2D::setImage(GLenum target, size_t level, GLenum internalFormat, const Extents &size, GLenum format, GLenum type,
-                          const PixelUnpackState &unpack, const uint8_t *pixels)
-{
-    Error error = Texture::setImage(target, level, internalFormat, size, format, type, unpack, pixels);
-    if (error.isError())
-    {
-        return error;
-    }
-
-    releaseTexImage();
-
-    return Error(GL_NO_ERROR);
-}
-
-Error Texture2D::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const Extents &size,
-                                    const PixelUnpackState &unpack, const uint8_t *pixels)
-{
-    Error error = Texture::setCompressedImage(target, level, internalFormat, size, unpack, pixels);
-    if (error.isError())
-    {
-        return error;
-    }
-
-    releaseTexImage();
-
-    return Error(GL_NO_ERROR);
-}
-
-Error Texture2D::copyImage(GLenum target, size_t level, const Rectangle &sourceArea, GLenum internalFormat,
-                           const Framebuffer *source)
-{
-    Error error = Texture::copyImage(target, level, sourceArea, internalFormat, source);
-    if (error.isError())
-    {
-        return error;
-    }
-
-    releaseTexImage();
-
-    return Error(GL_NO_ERROR);
-}
-
-Error Texture2D::setStorage(GLenum target, size_t levels, GLenum internalFormat, const Extents &size)
-{
-    Error error = Texture::setStorage(target, levels, internalFormat, size);
-    if (error.isError())
-    {
-        return error;
-    }
-
-    releaseTexImage();
-
-    return Error(GL_NO_ERROR);
-}
-
-Error Texture2D::generateMipmaps()
-{
-    Error error = Texture::generateMipmaps();
-    if (error.isError())
-    {
-        return error;
-    }
-
-    releaseTexImage();
-
-    return Error(GL_NO_ERROR);
-}
-
-void Texture2D::bindTexImage(egl::Surface *surface)
-{
-    releaseTexImage();
-    mTexture->bindTexImage(surface);
-    mSurface = surface;
-}
-
-void Texture2D::releaseTexImage()
-{
-    if (mSurface)
-    {
-        mSurface = NULL;
-        mTexture->releaseTexImage();
-    }
 }
 
 // Tests for 2D texture sampling completeness. [OpenGL ES 2.0.24] section 3.8.2 page 85.