Creates helper function for common subimage param checking

TRAC #18802
Signed-off-by: Nicolas Capens
Signed-off-by: Daniel Koch
Author: Shannon Woods

git-svn-id: https://angleproject.googlecode.com/svn/trunk@901 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/libGLESv2.cpp b/src/libGLESv2/libGLESv2.cpp
index f75d798..bad6f41 100644
--- a/src/libGLESv2/libGLESv2.cpp
+++ b/src/libGLESv2/libGLESv2.cpp
@@ -53,6 +53,41 @@
     return false;
 }
 
+bool validateSubImageParams(bool compressed, GLsizei width, GLsizei height, GLint xoffset, GLint yoffset, GLint level, GLenum format, gl::Texture *texture)
+{
+    if (!texture)
+    {
+        return error(GL_INVALID_OPERATION, false);
+    }
+
+    if (compressed != texture->isCompressed())
+    {
+        return error(GL_INVALID_OPERATION, false);
+    }
+
+    if (format != GL_NONE && format != texture->getInternalFormat())
+    {
+        return error(GL_INVALID_OPERATION, false);
+    }
+
+    if (compressed)
+    {
+        if ((width % 4 != 0 && width != texture->getWidth(0)) || 
+            (height % 4 != 0 && height != texture->getHeight(0)))
+        {
+            return error(GL_INVALID_OPERATION, false);
+        }
+    }
+
+    if (xoffset + width > texture->getWidth(level) ||
+        yoffset + height > texture->getHeight(level))
+    {
+        return error(GL_INVALID_VALUE, false);
+    }
+
+    return true;
+}
+
 // check for combinations of format and type that are valid for ReadPixels
 bool validReadFormatType(GLenum format, GLenum type)
 {
@@ -1025,58 +1060,18 @@
             if (target == GL_TEXTURE_2D)
             {
                 gl::Texture2D *texture = context->getTexture2D();
-
-                if (!texture)
+                if (validateSubImageParams(true, width, height, xoffset, yoffset, level, GL_NONE, texture))
                 {
-                    return error(GL_INVALID_OPERATION);
+                    texture->subImageCompressed(level, xoffset, yoffset, width, height, format, imageSize, data);
                 }
-
-                if (!texture->isCompressed())
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if ((width % 4 != 0 && width != texture->getWidth(0)) || 
-                    (height % 4 != 0 && height != texture->getHeight(0)))
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if (xoffset + width > texture->getWidth(level) ||
-                    yoffset + height > texture->getHeight(level))
-                {
-                    return error(GL_INVALID_VALUE);
-                }
-
-                texture->subImageCompressed(level, xoffset, yoffset, width, height, format, imageSize, data);
             }
             else if (gl::IsCubemapTextureTarget(target))
             {
                 gl::TextureCubeMap *texture = context->getTextureCubeMap();
-
-                if (!texture)
+                if (validateSubImageParams(true, width, height, xoffset, yoffset, level, GL_NONE, texture))
                 {
-                    return error(GL_INVALID_OPERATION);
+                    texture->subImageCompressed(target, level, xoffset, yoffset, width, height, format, imageSize, data);
                 }
-
-                if (!texture->isCompressed())
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if ((width % 4 != 0 && width != texture->getWidth(0)) || 
-                    (height % 4 != 0 && height != texture->getHeight(0)))
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if (xoffset + width > texture->getWidth(level) ||
-                    yoffset + height > texture->getHeight(level))
-                {
-                    return error(GL_INVALID_VALUE);
-                }
-
-                texture->subImageCompressed(target, level, xoffset, yoffset, width, height, format, imageSize, data);
             }
             else
             {
@@ -1337,15 +1332,9 @@
             }
             else UNREACHABLE();
 
-            if (!texture)
+            if (!validateSubImageParams(false, width, height, xoffset, yoffset, level, GL_NONE, texture))
             {
-                return error(GL_INVALID_OPERATION);
-            }
-
-            if (xoffset + width > texture->getWidth(level) ||
-                yoffset + height > texture->getHeight(level))
-            {
-                return error(GL_INVALID_VALUE);
+                return; // error already registered by validateSubImageParams
             }
 
             GLenum textureFormat = texture->getInternalFormat();
@@ -5234,56 +5223,18 @@
             if (target == GL_TEXTURE_2D)
             {
                 gl::Texture2D *texture = context->getTexture2D();
-
-                if (!texture)
+                if (validateSubImageParams(false, width, height, xoffset, yoffset, level, format, texture))
                 {
-                    return error(GL_INVALID_OPERATION);
+                    texture->subImage(level, xoffset, yoffset, width, height, format, type, context->getUnpackAlignment(), pixels);
                 }
-
-                if (texture->isCompressed())
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if (format != texture->getInternalFormat())
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if (xoffset + width > texture->getWidth(level) ||
-                    yoffset + height > texture->getHeight(level))
-                {
-                    return error(GL_INVALID_VALUE);
-                }
-
-                texture->subImage(level, xoffset, yoffset, width, height, format, type, context->getUnpackAlignment(), pixels);
             }
             else if (gl::IsCubemapTextureTarget(target))
             {
                 gl::TextureCubeMap *texture = context->getTextureCubeMap();
-
-                if (!texture)
+                if (validateSubImageParams(false, width, height, xoffset, yoffset, level, format, texture))
                 {
-                    return error(GL_INVALID_OPERATION);
+                    texture->subImage(target, level, xoffset, yoffset, width, height, format, type, context->getUnpackAlignment(), pixels);
                 }
-
-                if (texture->isCompressed())
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if (format != texture->getInternalFormat())
-                {
-                    return error(GL_INVALID_OPERATION);
-                }
-
-                if (xoffset + width > texture->getWidth(level) ||
-                    yoffset + height > texture->getHeight(level))
-                {
-                    return error(GL_INVALID_VALUE);
-                }
-
-                texture->subImage(target, level, xoffset, yoffset, width, height, format, type, context->getUnpackAlignment(), pixels);
             }
             else
             {