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/Surface.cpp b/src/libANGLE/Surface.cpp
index 70c44d1..7f8525f 100644
--- a/src/libANGLE/Surface.cpp
+++ b/src/libANGLE/Surface.cpp
@@ -134,7 +134,7 @@
     return mImplementation->getHeight();
 }
 
-Error Surface::bindTexImage(gl::Texture2D *texture, EGLint buffer)
+Error Surface::bindTexImage(gl::Texture *texture, EGLint buffer)
 {
     ASSERT(!mTexture);
 
@@ -146,7 +146,7 @@
 Error Surface::releaseTexImage(EGLint buffer)
 {
     ASSERT(mTexture);
-    gl::Texture2D *boundTexture = mTexture;
+    gl::Texture *boundTexture = mTexture;
     mTexture = NULL;
 
     boundTexture->releaseTexImage();
diff --git a/src/libANGLE/Surface.h b/src/libANGLE/Surface.h
index 70f94ae..a49343b 100644
--- a/src/libANGLE/Surface.h
+++ b/src/libANGLE/Surface.h
@@ -18,7 +18,7 @@
 
 namespace gl
 {
-class Texture2D;
+class Texture;
 }
 
 namespace rx
@@ -43,7 +43,7 @@
     Error swap();
     Error postSubBuffer(EGLint x, EGLint y, EGLint width, EGLint height);
     Error querySurfacePointerANGLE(EGLint attribute, void **value);
-    Error bindTexImage(gl::Texture2D *texture, EGLint buffer);
+    Error bindTexImage(gl::Texture *texture, EGLint buffer);
     Error releaseTexImage(EGLint buffer);
 
     EGLNativeWindowType getWindowHandle() const;
@@ -64,7 +64,7 @@
     EGLenum getTextureTarget() const;
     EGLenum getFormat() const;
 
-    gl::Texture2D *getBoundTexture() const { return mTexture; }
+    gl::Texture *getBoundTexture() const { return mTexture; }
 
     EGLint isFixedSize() const;
 
@@ -77,7 +77,7 @@
     EGLenum mRenderBuffer;         // Render buffer
     EGLenum mSwapBehavior;         // Buffer swap behavior
 
-    gl::Texture2D *mTexture;
+    gl::Texture *mTexture;
 };
 
 }
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.
diff --git a/src/libANGLE/Texture.h b/src/libANGLE/Texture.h
index 7fc5091..3ee199d 100644
--- a/src/libANGLE/Texture.h
+++ b/src/libANGLE/Texture.h
@@ -83,6 +83,9 @@
     bool isImmutable() const;
     GLsizei immutableLevelCount();
 
+    void bindTexImage(egl::Surface *surface);
+    void releaseTexImage();
+
     rx::TextureImpl *getImplementation() { return mTexture; }
     const rx::TextureImpl *getImplementation() const { return mTexture; }
 
@@ -133,6 +136,8 @@
 
     typedef std::map<ImageIdentifier, ImageDesc> ImageDescMap;
     ImageDescMap mImageDescs;
+
+    egl::Surface *mBoundSurface;
 };
 
 class Texture2D : public Texture
@@ -142,30 +147,13 @@
 
     virtual ~Texture2D();
 
-    Error setImage(GLenum target, size_t level, GLenum internalFormat, const Extents &size, GLenum format, GLenum type,
-                   const PixelUnpackState &unpack, const uint8_t *pixels) override;
-
-    Error setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const Extents &size,
-                             const PixelUnpackState &unpack, const uint8_t *pixels) override;
-
-    Error copyImage(GLenum target, size_t level, const Rectangle &sourceArea, GLenum internalFormat,
-                    const Framebuffer *source) override;
-
-    Error setStorage(GLenum target, size_t levels, GLenum internalFormat, const Extents &size) override;
-
-    Error generateMipmaps() override;
-
     virtual bool isSamplerComplete(const SamplerState &samplerState, const Data &data) const;
-    virtual void bindTexImage(egl::Surface *surface);
-    virtual void releaseTexImage();
 
   private:
     DISALLOW_COPY_AND_ASSIGN(Texture2D);
 
     bool isMipmapComplete() const;
     bool isLevelComplete(size_t level) const;
-
-    egl::Surface *mSurface;
 };
 
 class TextureCubeMap : public Texture
diff --git a/src/libGLESv2/entry_points_egl.cpp b/src/libGLESv2/entry_points_egl.cpp
index 0a63057..8f25c2e 100644
--- a/src/libGLESv2/entry_points_egl.cpp
+++ b/src/libGLESv2/entry_points_egl.cpp
@@ -864,7 +864,7 @@
     gl::Context *context = GetGlobalContext();
     if (context)
     {
-        gl::Texture2D *textureObject = context->getTexture2D();
+        gl::Texture *textureObject = context->getTargetTexture(GL_TEXTURE_2D);
         ASSERT(textureObject != NULL);
 
         if (textureObject->isImmutable())
@@ -929,7 +929,7 @@
         return EGL_FALSE;
     }
 
-    gl::Texture2D *texture = eglSurface->getBoundTexture();
+    gl::Texture *texture = eglSurface->getBoundTexture();
 
     if (texture)
     {