Factor code between PixelUnpackState and PixelPackState

BUG=angleproject:1512

Change-Id: I4c60472d216bfc5198e635d70fd197a5738dde98
Reviewed-on: https://chromium-review.googlesource.com/390133
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/formatutils.cpp b/src/libANGLE/formatutils.cpp
index 4222d72..b9c5b4e 100644
--- a/src/libANGLE/formatutils.cpp
+++ b/src/libANGLE/formatutils.cpp
@@ -858,7 +858,7 @@
     return components * typeInfo.bytes;
 }
 
-gl::ErrorOrResult<GLuint> InternalFormat::computeRowPitch(GLenum formatType,
+ErrorOrResult<GLuint> InternalFormat::computeRowPitch(GLenum formatType,
                                                           GLsizei width,
                                                           GLint alignment,
                                                           GLint rowLength) const
@@ -867,7 +867,7 @@
     if (compressed)
     {
         ASSERT(rowLength == 0);
-        return computeCompressedImageSize(formatType, gl::Extents(width, 1, 1));
+        return computeCompressedImageSize(formatType, Extents(width, 1, 1));
     }
 
     CheckedNumeric<GLuint> checkedWidth(rowLength > 0 ? rowLength : width);
@@ -880,7 +880,7 @@
     return aligned.ValueOrDie();
 }
 
-gl::ErrorOrResult<GLuint> InternalFormat::computeDepthPitch(GLenum formatType,
+ErrorOrResult<GLuint> InternalFormat::computeDepthPitch(GLenum formatType,
                                                             GLsizei width,
                                                             GLsizei height,
                                                             GLint alignment,
@@ -898,8 +898,8 @@
     return depthPitch.ValueOrDie();
 }
 
-gl::ErrorOrResult<GLuint> InternalFormat::computeCompressedImageSize(GLenum formatType,
-                                                                     const gl::Extents &size) const
+ErrorOrResult<GLuint> InternalFormat::computeCompressedImageSize(GLenum formatType,
+                                                                     const Extents &size) const
 {
     CheckedNumeric<GLuint> checkedWidth(size.width);
     CheckedNumeric<GLuint> checkedHeight(size.height);
@@ -915,21 +915,19 @@
     return bytes.ValueOrDie();
 }
 
-gl::ErrorOrResult<GLuint> InternalFormat::computeSkipBytes(GLuint rowPitch,
+ErrorOrResult<GLuint> InternalFormat::computeSkipBytes(GLuint rowPitch,
                                                            GLuint depthPitch,
-                                                           GLint skipImages,
-                                                           GLint skipRows,
-                                                           GLint skipPixels,
-                                                           bool applySkipImages) const
+                                                           const PixelStoreStateBase &state,
+                                                           bool is3D) const
 {
     CheckedNumeric<GLuint> checkedRowPitch(rowPitch);
     CheckedNumeric<GLuint> checkedDepthPitch(depthPitch);
-    CheckedNumeric<GLuint> checkedSkipImages(static_cast<GLuint>(skipImages));
-    CheckedNumeric<GLuint> checkedSkipRows(static_cast<GLuint>(skipRows));
-    CheckedNumeric<GLuint> checkedSkipPixels(static_cast<GLuint>(skipPixels));
+    CheckedNumeric<GLuint> checkedSkipImages(static_cast<GLuint>(state.skipImages));
+    CheckedNumeric<GLuint> checkedSkipRows(static_cast<GLuint>(state.skipRows));
+    CheckedNumeric<GLuint> checkedSkipPixels(static_cast<GLuint>(state.skipPixels));
     CheckedNumeric<GLuint> checkedPixelBytes(pixelBytes);
     auto checkedSkipImagesBytes = checkedSkipImages * checkedDepthPitch;
-    if (!applySkipImages)
+    if (!is3D)
     {
         checkedSkipImagesBytes = 0;
     }
@@ -939,108 +937,46 @@
     return skipBytes.ValueOrDie();
 }
 
-gl::ErrorOrResult<GLuint> InternalFormat::computePackSize(GLenum formatType,
-                                                          const gl::Extents &size,
-                                                          const gl::PixelPackState &pack) const
+ErrorOrResult<GLuint> InternalFormat::computePackUnpackEndByte(
+    GLenum formatType,
+    const Extents &size,
+    const PixelStoreStateBase &state,
+    bool is3D) const
 {
-    ASSERT(!compressed);
-
-    if (size.height == 0)
+    GLuint depthPitch = 0;
+    if (is3D)
     {
-        return 0;
+        ANGLE_TRY_RESULT(computeDepthPitch(formatType, size.width, size.height, state.alignment,
+                                           state.rowLength, state.imageHeight),
+                         depthPitch);
     }
 
-    CheckedNumeric<GLuint> rowPitch;
-    ANGLE_TRY_RESULT(computeRowPitch(formatType, size.width, pack.alignment, pack.rowLength),
+    GLuint rowPitch = 0;
+    ANGLE_TRY_RESULT(computeRowPitch(formatType, size.width, state.alignment, state.rowLength),
                      rowPitch);
 
-    CheckedNumeric<GLuint> heightMinusOne = size.height - 1;
-    CheckedNumeric<GLuint> bytes          = computePixelBytes(formatType);
-
-    CheckedNumeric<GLuint> totalSize = heightMinusOne * rowPitch;
-    totalSize += size.width * bytes;
-
-    ANGLE_TRY_CHECKED_MATH(totalSize);
-
-    return totalSize.ValueOrDie();
-}
-
-gl::ErrorOrResult<GLuint> InternalFormat::computeUnpackSize(
-    GLenum formatType,
-    const gl::Extents &size,
-    const gl::PixelUnpackState &unpack) const
-{
-    // Compressed images do not use unpack parameters.
+    CheckedNumeric<GLuint> checkedCopyBytes = 0;
     if (compressed)
     {
-        return computeCompressedImageSize(formatType, size);
+        ANGLE_TRY_RESULT(computeCompressedImageSize(formatType, size), checkedCopyBytes);
     }
-
-    if (size.height == 0 || size.depth == 0)
+    else if (size.height != 0 && (!is3D || size.depth != 0))
     {
-        return 0;
+        CheckedNumeric<GLuint> bytes = computePixelBytes(formatType);
+        checkedCopyBytes += size.width * bytes;
+
+        CheckedNumeric<GLuint> heightMinusOne = size.height - 1;
+        checkedCopyBytes += heightMinusOne * rowPitch;
+
+        if (is3D)
+        {
+            CheckedNumeric<GLuint> depthMinusOne = size.depth - 1;
+            checkedCopyBytes += depthMinusOne * depthPitch;
+        }
     }
 
-    CheckedNumeric<GLuint> rowPitch;
-    CheckedNumeric<GLuint> depthPitch;
-    ANGLE_TRY_RESULT(computeRowPitch(formatType, size.width, unpack.alignment, unpack.rowLength),
-                     rowPitch);
-    ANGLE_TRY_RESULT(computeDepthPitch(formatType, size.width, size.height, unpack.alignment,
-                                       unpack.rowLength, unpack.imageHeight),
-                     depthPitch);
-
-    CheckedNumeric<GLuint> depthMinusOne  = size.depth - 1;
-    CheckedNumeric<GLuint> heightMinusOne = size.height - 1;
-    CheckedNumeric<GLuint> bytes          = computePixelBytes(formatType);
-
-    CheckedNumeric<GLuint> totalSize = depthMinusOne * depthPitch;
-    totalSize += heightMinusOne * rowPitch;
-    totalSize += size.width * bytes;
-
-    ANGLE_TRY_CHECKED_MATH(totalSize);
-
-    return totalSize.ValueOrDie();
-}
-
-gl::ErrorOrResult<GLuint> InternalFormat::computePackEndByte(GLenum formatType,
-                                                             const gl::Extents &size,
-                                                             const gl::PixelPackState &pack) const
-{
-    GLuint rowPitch;
-    CheckedNumeric<GLuint> checkedSkipBytes;
-    CheckedNumeric<GLuint> checkedCopyBytes;
-
-    ANGLE_TRY_RESULT(computeRowPitch(formatType, size.width, pack.alignment, pack.rowLength),
-                     rowPitch);
-    ANGLE_TRY_RESULT(computeSkipBytes(rowPitch, 0, 0, pack.skipRows, pack.skipPixels, false),
-                     checkedSkipBytes);
-    ANGLE_TRY_RESULT(computePackSize(formatType, size, pack), checkedCopyBytes);
-
-    CheckedNumeric<GLuint> endByte = checkedCopyBytes + checkedSkipBytes;
-
-    ANGLE_TRY_CHECKED_MATH(endByte);
-    return endByte.ValueOrDie();
-}
-
-gl::ErrorOrResult<GLuint> InternalFormat::computeUnpackEndByte(GLenum formatType,
-                                                               const gl::Extents &size,
-                                                               const gl::PixelUnpackState &unpack,
-                                                               bool applySkipImages) const
-{
-    GLuint rowPitch;
-    GLuint depthPitch;
-    CheckedNumeric<GLuint> checkedSkipBytes;
-    CheckedNumeric<GLuint> checkedCopyBytes;
-
-    ANGLE_TRY_RESULT(computeRowPitch(formatType, size.width, unpack.alignment, unpack.rowLength),
-                     rowPitch);
-    ANGLE_TRY_RESULT(computeDepthPitch(formatType, size.width, size.height, unpack.alignment,
-                                       unpack.rowLength, unpack.imageHeight),
-                     depthPitch);
-    ANGLE_TRY_RESULT(computeSkipBytes(rowPitch, depthPitch, unpack.skipImages, unpack.skipRows,
-                                      unpack.skipPixels, applySkipImages),
-                     checkedSkipBytes);
-    ANGLE_TRY_RESULT(computeUnpackSize(formatType, size, unpack), checkedCopyBytes);
+    CheckedNumeric<GLuint> checkedSkipBytes = 0;
+    ANGLE_TRY_RESULT(computeSkipBytes(rowPitch, depthPitch, state, is3D), checkedSkipBytes);
 
     CheckedNumeric<GLuint> endByte = checkedCopyBytes + checkedSkipBytes;