Make commitRegion a base method of TextureD3D.

Use a helper method isValidIndex to preserve ASSERT checks.

Refactoring patch only.

BUG=angle:741

Change-Id: Ie19fa21db51cd0239a3b391de362584a9fbab2df
Reviewed-on: https://chromium-review.googlesource.com/222268
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libGLESv2/renderer/d3d/TextureD3D.cpp b/src/libGLESv2/renderer/d3d/TextureD3D.cpp
index 9adfc37..bbe1261 100644
--- a/src/libGLESv2/renderer/d3d/TextureD3D.cpp
+++ b/src/libGLESv2/renderer/d3d/TextureD3D.cpp
@@ -438,6 +438,25 @@
     return (image->isRenderableFormat() && levelsComplete);
 }
 
+gl::Error TextureD3D::commitRegion(const gl::ImageIndex &index, const gl::Box &region)
+{
+    if (mTexStorage)
+    {
+        ASSERT(isValidIndex(index));
+        Image *image = getImage(index);
+        ImageD3D *imageD3D = ImageD3D::makeImageD3D(image);
+        gl::Error error = imageD3D->copyToStorage(mTexStorage, index, region);
+        if (error.isError())
+        {
+            return error;
+        }
+
+        image->markClean();
+    }
+
+    return gl::Error(GL_NO_ERROR);
+}
+
 TextureD3D_2D::TextureD3D_2D(Renderer *renderer)
     : TextureD3D(renderer)
 {
@@ -1034,26 +1053,6 @@
     }
 }
 
-gl::Error TextureD3D_2D::commitRegion(const gl::ImageIndex &index, const gl::Box &region)
-{
-    ASSERT(!index.hasLayer());
-    GLint level = index.mipIndex;
-
-    if (isValidLevel(level))
-    {
-        ImageD3D *image = mImageArray[level];
-        gl::Error error = image->copyToStorage(mTexStorage, index, region);
-        if (error.isError())
-        {
-            return error;
-        }
-
-        image->markClean();
-    }
-
-    return gl::Error(GL_NO_ERROR);
-}
-
 gl::ImageIndexIterator TextureD3D_2D::imageIterator() const
 {
     return gl::ImageIndexIterator::Make2D(0, mTexStorage->getLevelCount());
@@ -1065,6 +1064,12 @@
     return gl::ImageIndex::Make2D(mip);
 }
 
+bool TextureD3D_2D::isValidIndex(const gl::ImageIndex &index) const
+{
+    return (mTexStorage && index.type == GL_TEXTURE_2D &&
+            index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount());
+}
+
 TextureD3D_Cube::TextureD3D_Cube(Renderer *renderer)
     : TextureD3D(renderer)
 {
@@ -1594,28 +1599,6 @@
     }
 }
 
-gl::Error TextureD3D_Cube::commitRegion(const gl::ImageIndex &index, const gl::Box &region)
-{
-    ASSERT(index.hasLayer());
-
-    GLint level = index.mipIndex;
-    int faceIndex = static_cast<int>(index.layerIndex);
-
-    if (isValidFaceLevel(faceIndex, level))
-    {
-        ImageD3D *image = mImageArray[faceIndex][level];
-        gl::Error error = image->copyToStorage(mTexStorage, index, region);
-        if (error.isError())
-        {
-            return error;
-        }
-
-        image->markClean();
-    }
-
-    return gl::Error(GL_NO_ERROR);
-}
-
 gl::ImageIndexIterator TextureD3D_Cube::imageIterator() const
 {
     return gl::ImageIndexIterator::MakeCube(0, mTexStorage->getLevelCount());
@@ -1627,6 +1610,12 @@
     return gl::ImageIndex::MakeCube(gl::TextureCubeMap::layerIndexToTarget(layer), mip);
 }
 
+bool TextureD3D_Cube::isValidIndex(const gl::ImageIndex &index) const
+{
+    return (mTexStorage && gl::IsCubemapTextureTarget(index.type) &&
+            index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount());
+}
+
 TextureD3D_3D::TextureD3D_3D(Renderer *renderer)
     : TextureD3D(renderer)
 {
@@ -2173,26 +2162,6 @@
     }
 }
 
-gl::Error TextureD3D_3D::commitRegion(const gl::ImageIndex &index, const gl::Box &region)
-{
-    ASSERT(!index.hasLayer());
-    GLint level = index.mipIndex;
-
-    if (isValidLevel(level))
-    {
-        ImageD3D *image = mImageArray[level];
-        gl::Error error = image->copyToStorage(mTexStorage, index, region);
-        if (error.isError())
-        {
-            return error;
-        }
-
-        image->markClean();
-    }
-
-    return gl::Error(GL_NO_ERROR);
-}
-
 gl::ImageIndexIterator TextureD3D_3D::imageIterator() const
 {
     return gl::ImageIndexIterator::Make3D(0, mTexStorage->getLevelCount(),
@@ -2205,6 +2174,12 @@
     return gl::ImageIndex::Make3D(mip);
 }
 
+bool TextureD3D_3D::isValidIndex(const gl::ImageIndex &index) const
+{
+    return (mTexStorage && index.type == GL_TEXTURE_3D &&
+            index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount());
+}
+
 TextureD3D_2DArray::TextureD3D_2DArray(Renderer *renderer)
     : TextureD3D(renderer)
 {
@@ -2753,27 +2728,6 @@
     }
 }
 
-gl::Error TextureD3D_2DArray::commitRegion(const gl::ImageIndex &index, const gl::Box &region)
-{
-    ASSERT(index.hasLayer());
-    GLint level = index.mipIndex;
-    GLint layerTarget = index.layerIndex;
-
-    if (isValidLevel(level) && layerTarget < getLayerCount(level))
-    {
-        ImageD3D *image = mImageArray[level][layerTarget];
-        gl::Error error = image->copyToStorage(mTexStorage, index, region);
-        if (error.isError())
-        {
-            return error;
-        }
-
-        image->markClean();
-    }
-
-    return gl::Error(GL_NO_ERROR);
-}
-
 gl::ImageIndexIterator TextureD3D_2DArray::imageIterator() const
 {
     return gl::ImageIndexIterator::Make2DArray(0, mTexStorage->getLevelCount(), mLayerCounts);
@@ -2784,4 +2738,22 @@
     return gl::ImageIndex::Make2DArray(mip, layer);
 }
 
+bool TextureD3D_2DArray::isValidIndex(const gl::ImageIndex &index) const
+{
+    // Check for having a storage and the right type of index
+    if (!mTexStorage || index.type != GL_TEXTURE_2D_ARRAY)
+    {
+        return false;
+    }
+
+    // Check the mip index
+    if (index.mipIndex < 0 || index.mipIndex >= mTexStorage->getLevelCount())
+    {
+        return false;
+    }
+
+    // Check the layer index
+    return (!index.hasLayer() || (index.layerIndex >= 0 && index.layerIndex < mLayerCounts[index.mipIndex]));
+}
+
 }