Moved some validation back to Texture, unified all TextureImpl variants

BUG=angle:688
Change-Id: Ie2df18277c84345fceaa31d4f63f5cbbb47540c6
Reviewed-on: https://chromium-review.googlesource.com/211387
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Brandon Jones <bajones@chromium.org>
diff --git a/src/libGLESv2/Texture.cpp b/src/libGLESv2/Texture.cpp
index 87e0304..eae0008 100644
--- a/src/libGLESv2/Texture.cpp
+++ b/src/libGLESv2/Texture.cpp
@@ -14,6 +14,7 @@
 #include "libGLESv2/main.h"
 #include "common/mathutil.h"
 #include "common/utilities.h"
+#include "libGLESv2/Context.h"
 #include "libGLESv2/formatutils.h"
 #include "libGLESv2/Renderbuffer.h"
 #include "libGLESv2/renderer/Image.h"
@@ -24,8 +25,31 @@
 namespace gl
 {
 
-Texture::Texture(GLuint id, GLenum target)
+bool IsMipmapFiltered(const gl::SamplerState &samplerState)
+{
+    switch (samplerState.minFilter)
+    {
+      case GL_NEAREST:
+      case GL_LINEAR:
+        return false;
+      case GL_NEAREST_MIPMAP_NEAREST:
+      case GL_LINEAR_MIPMAP_NEAREST:
+      case GL_NEAREST_MIPMAP_LINEAR:
+      case GL_LINEAR_MIPMAP_LINEAR:
+        return true;
+      default: UNREACHABLE();
+        return false;
+    }
+}
+
+bool IsPointSampled(const gl::SamplerState &samplerState)
+{
+    return (samplerState.magFilter == GL_NEAREST && (samplerState.minFilter == GL_NEAREST || samplerState.minFilter == GL_NEAREST_MIPMAP_NEAREST));
+}
+
+Texture::Texture(rx::TextureImpl *impl, GLuint id, GLenum target)
     : RefCountObject(id),
+      mTexture(impl),
       mUsage(GL_NONE),
       mImmutable(false),
       mTarget(target)
@@ -34,6 +58,7 @@
 
 Texture::~Texture()
 {
+    SafeDelete(mTexture);
 }
 
 GLenum Texture::getTarget() const
@@ -89,12 +114,6 @@
     return (baseImage ? baseImage->getInternalFormat() : GL_NONE);
 }
 
-// Tests for texture sampling completeness
-bool Texture::isSamplerComplete(const SamplerState &samplerState) const
-{
-    return getImplementation()->isSamplerComplete(samplerState);
-}
-
 rx::TextureStorageInterface *Texture::getNativeTexture()
 {
     return getImplementation()->getNativeTexture();
@@ -136,17 +155,14 @@
     return (getImplementation()->getLayerCount(0) > 0 ? getImplementation()->getImage(0, 0) : NULL);
 }
 
-Texture2D::Texture2D(rx::Texture2DImpl *impl, GLuint id)
-    : Texture(id, GL_TEXTURE_2D),
-      mTexture(impl)
+Texture2D::Texture2D(rx::TextureImpl *impl, GLuint id)
+    : Texture(impl, id, GL_TEXTURE_2D)
 {
     mSurface = NULL;
 }
 
 Texture2D::~Texture2D()
 {
-    SafeDelete(mTexture);
-
     if (mSurface)
     {
         mSurface->setBoundTexture(NULL);
@@ -245,6 +261,70 @@
     mTexture->storage(GL_TEXTURE_2D, levels, internalformat, width, height, 1);
 }
 
+// Tests for 2D texture sampling completeness. [OpenGL ES 2.0.24] section 3.8.2 page 85.
+bool Texture2D::isSamplerComplete(const SamplerState &samplerState, const TextureCapsMap &textureCaps, const Extensions &extensions, int clientVersion) const
+{
+    GLsizei width = getBaseLevelWidth();
+    GLsizei height = getBaseLevelHeight();
+
+    if (width <= 0 || height <= 0)
+    {
+        return false;
+    }
+
+    if (!textureCaps.get(getInternalFormat(0)).filterable && !IsPointSampled(samplerState))
+    {
+        return false;
+    }
+
+    bool npotSupport = extensions.textureNPOT;
+
+    if (!npotSupport)
+    {
+        if ((samplerState.wrapS != GL_CLAMP_TO_EDGE && !gl::isPow2(width)) ||
+            (samplerState.wrapT != GL_CLAMP_TO_EDGE && !gl::isPow2(height)))
+        {
+            return false;
+        }
+    }
+
+    if (IsMipmapFiltered(samplerState))
+    {
+        if (!npotSupport)
+        {
+            if (!gl::isPow2(width) || !gl::isPow2(height))
+            {
+                return false;
+            }
+        }
+
+        if (!isMipmapComplete())
+        {
+            return false;
+        }
+    }
+
+    // OpenGLES 3.0.2 spec section 3.8.13 states that a texture is not mipmap complete if:
+    // The internalformat specified for the texture arrays is a sized internal depth or
+    // depth and stencil format (see table 3.13), the value of TEXTURE_COMPARE_-
+    // MODE is NONE, and either the magnification filter is not NEAREST or the mini-
+    // fication filter is neither NEAREST nor NEAREST_MIPMAP_NEAREST.
+    const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(getInternalFormat(0));
+    if (formatInfo.depthBits > 0 && clientVersion > 2)
+    {
+        if (samplerState.compareMode == GL_NONE)
+        {
+            if ((samplerState.minFilter != GL_NEAREST && samplerState.minFilter != GL_NEAREST_MIPMAP_NEAREST) ||
+                samplerState.magFilter != GL_NEAREST)
+            {
+                return false;
+            }
+        }
+    }
+
+    return true;
+}
+
 bool Texture2D::isCompressed(GLint level) const
 {
     return GetInternalFormatInfo(getInternalFormat(level)).compressed;
@@ -277,15 +357,73 @@
     return mTexture->getDepthStencil(level, 0);
 }
 
-TextureCubeMap::TextureCubeMap(rx::TextureCubeImpl *impl, GLuint id)
-    : Texture(id, GL_TEXTURE_CUBE_MAP),
-      mTexture(impl)
+// Tests for 2D texture (mipmap) completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
+bool Texture2D::isMipmapComplete() const
+{
+    int levelCount = mipLevels();
+
+    for (int level = 0; level < levelCount; level++)
+    {
+        if (!isLevelComplete(level))
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool Texture2D::isLevelComplete(int level) const
+{
+    if (isImmutable())
+    {
+        return true;
+    }
+
+    const rx::Image *baseImage = getBaseLevelImage();
+
+    GLsizei width = baseImage->getWidth();
+    GLsizei height = baseImage->getHeight();
+
+    if (width <= 0 || height <= 0)
+    {
+        return false;
+    }
+
+    // The base image level is complete if the width and height are positive
+    if (level == 0)
+    {
+        return true;
+    }
+
+    ASSERT(level >= 1 && level < IMPLEMENTATION_MAX_TEXTURE_LEVELS && mTexture->getImage(level, 0) != NULL);
+    rx::Image *image = mTexture->getImage(level, 0);
+
+    if (image->getInternalFormat() != baseImage->getInternalFormat())
+    {
+        return false;
+    }
+
+    if (image->getWidth() != std::max(1, width >> level))
+    {
+        return false;
+    }
+
+    if (image->getHeight() != std::max(1, height >> level))
+    {
+        return false;
+    }
+
+    return true;
+}
+
+TextureCubeMap::TextureCubeMap(rx::TextureImpl *impl, GLuint id)
+    : Texture(impl, id, GL_TEXTURE_CUBE_MAP)
 {
 }
 
 TextureCubeMap::~TextureCubeMap()
 {
-    SafeDelete(mTexture);
 }
 
 GLsizei TextureCubeMap::getWidth(GLenum target, GLint level) const
@@ -368,7 +506,28 @@
 // Tests for cube texture completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
 bool TextureCubeMap::isCubeComplete() const
 {
-    return mTexture->isCubeComplete();
+    int    baseWidth  = getBaseLevelWidth();
+    int    baseHeight = getBaseLevelHeight();
+    GLenum baseFormat = getBaseLevelInternalFormat();
+
+    if (baseWidth <= 0 || baseWidth != baseHeight)
+    {
+        return false;
+    }
+
+    for (int faceIndex = 1; faceIndex < 6; faceIndex++)
+    {
+        const rx::Image *faceBaseImage = mTexture->getImage(0, faceIndex);
+
+        if (faceBaseImage->getWidth()          != baseWidth  ||
+            faceBaseImage->getHeight()         != baseHeight ||
+            faceBaseImage->getInternalFormat() != baseFormat )
+        {
+            return false;
+        }
+    }
+
+    return true;
 }
 
 bool TextureCubeMap::isCompressed(GLenum target, GLint level) const
@@ -393,6 +552,44 @@
     mTexture->storage(GL_TEXTURE_CUBE_MAP, levels, internalformat, size, size, 1);
 }
 
+// Tests for texture sampling completeness
+bool TextureCubeMap::isSamplerComplete(const SamplerState &samplerState, const TextureCapsMap &textureCaps, const Extensions &extensions, int clientVersion) const
+{
+    int size = getBaseLevelWidth();
+
+    bool mipmapping = IsMipmapFiltered(samplerState);
+
+    if (!textureCaps.get(getInternalFormat(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0)).filterable && !IsPointSampled(samplerState))
+    {
+        return false;
+    }
+
+    if (!gl::isPow2(size) && !extensions.textureNPOT)
+    {
+        if (samplerState.wrapS != GL_CLAMP_TO_EDGE || samplerState.wrapT != GL_CLAMP_TO_EDGE || mipmapping)
+        {
+            return false;
+        }
+    }
+
+    if (!mipmapping)
+    {
+        if (!isCubeComplete())
+        {
+            return false;
+        }
+    }
+    else
+    {
+        if (!isMipmapComplete())   // Also tests for isCubeComplete()
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
 unsigned int TextureCubeMap::getRenderTargetSerial(GLenum target, GLint level)
 {
     return mTexture->getRenderTargetSerial(level, targetToLayerIndex(target));
@@ -430,15 +627,82 @@
     return mTexture->getDepthStencil(level, targetToLayerIndex(target));
 }
 
-Texture3D::Texture3D(rx::Texture3DImpl *impl, GLuint id)
-    : Texture(id, GL_TEXTURE_3D),
-      mTexture(impl)
+bool TextureCubeMap::isMipmapComplete() const
+{
+    if (isImmutable())
+    {
+        return true;
+    }
+
+    if (!isCubeComplete())
+    {
+        return false;
+    }
+
+    int levelCount = mipLevels();
+
+    for (int face = 0; face < 6; face++)
+    {
+        for (int level = 1; level < levelCount; level++)
+        {
+            if (!isFaceLevelComplete(face, level))
+            {
+                return false;
+            }
+        }
+    }
+
+    return true;
+}
+
+bool TextureCubeMap::isFaceLevelComplete(int faceIndex, int level) const
+{
+    ASSERT(level >= 0 && faceIndex < 6 && level < IMPLEMENTATION_MAX_TEXTURE_LEVELS && mTexture->getImage(level, faceIndex) != NULL);
+
+    if (isImmutable())
+    {
+        return true;
+    }
+
+    int baseSize = getBaseLevelWidth();
+
+    if (baseSize <= 0)
+    {
+        return false;
+    }
+
+    // "isCubeComplete" checks for base level completeness and we must call that
+    // to determine if any face at level 0 is complete. We omit that check here
+    // to avoid re-checking cube-completeness for every face at level 0.
+    if (level == 0)
+    {
+        return true;
+    }
+
+    // Check that non-zero levels are consistent with the base level.
+    const rx::Image *faceLevelImage = mTexture->getImage(level, faceIndex);
+
+    if (faceLevelImage->getInternalFormat() != getBaseLevelInternalFormat())
+    {
+        return false;
+    }
+
+    if (faceLevelImage->getWidth() != std::max(1, baseSize >> level))
+    {
+        return false;
+    }
+
+    return true;
+}
+
+
+Texture3D::Texture3D(rx::TextureImpl *impl, GLuint id)
+    : Texture(impl, id, GL_TEXTURE_3D)
 {
 }
 
 Texture3D::~Texture3D()
 {
-    SafeDelete(mTexture);
 }
 
 GLsizei Texture3D::getWidth(GLint level) const
@@ -503,6 +767,30 @@
     mTexture->storage(GL_TEXTURE_3D, levels, internalformat, width, height, depth);
 }
 
+bool Texture3D::isSamplerComplete(const SamplerState &samplerState, const TextureCapsMap &textureCaps, const Extensions &extensions, int clientVersion) const
+{
+    GLsizei width = getBaseLevelWidth();
+    GLsizei height = getBaseLevelHeight();
+    GLsizei depth = getBaseLevelDepth();
+
+    if (width <= 0 || height <= 0 || depth <= 0)
+    {
+        return false;
+    }
+
+    if (!textureCaps.get(getInternalFormat(0)).filterable && !IsPointSampled(samplerState))
+    {
+        return false;
+    }
+
+    if (IsMipmapFiltered(samplerState) && !isMipmapComplete())
+    {
+        return false;
+    }
+
+    return true;
+}
+
 unsigned int Texture3D::getRenderTargetSerial(GLint level, GLint layer)
 {
     return mTexture->getRenderTargetSerial(level, layer);
@@ -519,15 +807,76 @@
     return mTexture->getDepthStencil(level, layer);
 }
 
-Texture2DArray::Texture2DArray(rx::Texture2DArrayImpl *impl, GLuint id)
-    : Texture(id, GL_TEXTURE_2D_ARRAY),
-      mTexture(impl)
+bool Texture3D::isMipmapComplete() const
+{
+    int levelCount = mipLevels();
+
+    for (int level = 0; level < levelCount; level++)
+    {
+        if (!isLevelComplete(level))
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool Texture3D::isLevelComplete(int level) const
+{
+    ASSERT(level >= 0 && level < IMPLEMENTATION_MAX_TEXTURE_LEVELS && mTexture->getImage(level, 0) != NULL);
+
+    if (isImmutable())
+    {
+        return true;
+    }
+
+    GLsizei width = getBaseLevelWidth();
+    GLsizei height = getBaseLevelHeight();
+    GLsizei depth = getBaseLevelDepth();
+
+    if (width <= 0 || height <= 0 || depth <= 0)
+    {
+        return false;
+    }
+
+    if (level == 0)
+    {
+        return true;
+    }
+
+    rx::Image *levelImage = mTexture->getImage(level, 0);
+
+    if (levelImage->getInternalFormat() != getBaseLevelInternalFormat())
+    {
+        return false;
+    }
+
+    if (levelImage->getWidth() != std::max(1, width >> level))
+    {
+        return false;
+    }
+
+    if (levelImage->getHeight() != std::max(1, height >> level))
+    {
+        return false;
+    }
+
+    if (levelImage->getDepth() != std::max(1, depth >> level))
+    {
+        return false;
+    }
+
+    return true;
+}
+
+Texture2DArray::Texture2DArray(rx::TextureImpl *impl, GLuint id)
+    : Texture(impl, id, GL_TEXTURE_2D_ARRAY)
 {
 }
 
 Texture2DArray::~Texture2DArray()
 {
-    SafeDelete(mTexture);
 }
 
 GLsizei Texture2DArray::getWidth(GLint level) const
@@ -592,6 +941,30 @@
     mTexture->storage(GL_TEXTURE_2D_ARRAY, levels, internalformat, width, height, depth);
 }
 
+bool Texture2DArray::isSamplerComplete(const SamplerState &samplerState, const TextureCapsMap &textureCaps, const Extensions &extensions, int clientVersion) const
+{
+    GLsizei width = getBaseLevelWidth();
+    GLsizei height = getBaseLevelHeight();
+    GLsizei depth = getLayers(0);
+
+    if (width <= 0 || height <= 0 || depth <= 0)
+    {
+        return false;
+    }
+
+    if (!textureCaps.get(getBaseLevelInternalFormat()).filterable && !IsPointSampled(samplerState))
+    {
+        return false;
+    }
+
+    if (IsMipmapFiltered(samplerState) && !isMipmapComplete())
+    {
+        return false;
+    }
+
+    return true;
+}
+
 unsigned int Texture2DArray::getRenderTargetSerial(GLint level, GLint layer)
 {
     return mTexture->getRenderTargetSerial(level, layer);
@@ -607,4 +980,65 @@
     return mTexture->getDepthStencil(level, layer);
 }
 
+bool Texture2DArray::isMipmapComplete() const
+{
+    int levelCount = mipLevels();
+
+    for (int level = 1; level < levelCount; level++)
+    {
+        if (!isLevelComplete(level))
+        {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool Texture2DArray::isLevelComplete(int level) const
+{
+    ASSERT(level >= 0 && level < IMPLEMENTATION_MAX_TEXTURE_LEVELS);
+
+    if (isImmutable())
+    {
+        return true;
+    }
+
+    GLsizei width = getBaseLevelWidth();
+    GLsizei height = getBaseLevelHeight();
+    GLsizei layers = getLayers(0);
+
+    if (width <= 0 || height <= 0 || layers <= 0)
+    {
+        return false;
+    }
+
+    if (level == 0)
+    {
+        return true;
+    }
+
+    if (getInternalFormat(level) != getInternalFormat(0))
+    {
+        return false;
+    }
+
+    if (getWidth(level) != std::max(1, width >> level))
+    {
+        return false;
+    }
+
+    if (getHeight(level) != std::max(1, height >> level))
+    {
+        return false;
+    }
+
+    if (getLayers(level) != layers)
+    {
+        return false;
+    }
+
+    return true;
+}
+
 }