Consolidate read/write-Pixels testing code - take 2

Change-Id: I0de058a9eec749a7086138ac2eb79732f06ce55e
Reviewed-on: https://skia-review.googlesource.com/7650
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/gn/tests.gni b/gn/tests.gni
index 483f574..6a836f6 100644
--- a/gn/tests.gni
+++ b/gn/tests.gni
@@ -234,6 +234,8 @@
   "$_tests/Test.cpp",
   "$_tests/TestConfigParsing.cpp",
   "$_tests/TestTest.cpp",
+  "$_tests/TestUtils.h",
+  "$_tests/TestUtils.cpp",
   "$_tests/TextBlobCacheTest.cpp",
   "$_tests/TextBlobTest.cpp",
   "$_tests/TextureCompressionTest.cpp",
diff --git a/tests/EGLImageTest.cpp b/tests/EGLImageTest.cpp
index 79d3261..02ba4a8 100644
--- a/tests/EGLImageTest.cpp
+++ b/tests/EGLImageTest.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "Test.h"
+#include "TestUtils.h"
 #if SK_SUPPORT_GPU
 #include "GrContext.h"
 #include "GrContextPriv.h"
@@ -41,58 +42,6 @@
     }
 }
 
-static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
-                             GrSurfaceContext* externalTextureContext,
-                             uint32_t expectedPixelValues[]) {
-    int pixelCnt = externalTextureContext->width() * externalTextureContext->height();
-    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
-    memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
-
-    SkImageInfo ii = SkImageInfo::Make(externalTextureContext->width(), 
-                                       externalTextureContext->height(),
-                                       kRGBA_8888_SkColorType, kPremul_SkAlphaType);
-    bool read = externalTextureContext->readPixels(ii, pixels.get(), 0, 0, 0);
-    if (!read) {
-        ERRORF(reporter, "Error reading external texture.");
-    }
-    for (int i = 0; i < pixelCnt; ++i) {
-        if (pixels.get()[i] != expectedPixelValues[i]) {
-            ERRORF(reporter, "Error, external texture pixel value %d should be 0x%08x,"
-                             " got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]);
-            break;
-        }
-    }
-}
-
-static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
-                              GrSurfaceContext* externalTextureContext) {
-    int pixelCnt = externalTextureContext->width() * externalTextureContext->height();
-    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
-    memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
-
-    SkImageInfo ii = SkImageInfo::Make(externalTextureContext->width(),
-                                       externalTextureContext->height(),
-                                       kRGBA_8888_SkColorType, kPremul_SkAlphaType);
-    bool write = externalTextureContext->writePixels(ii, pixels.get(), 0, 0, 0);
-    REPORTER_ASSERT_MESSAGE(reporter, !write, "Should not be able to write to a EXTERNAL"
-                                              " texture.");
-}
-
-static void test_copy_surface(skiatest::Reporter* reporter, GrContext* context,
-                              GrSurfaceProxy* externalTextureProxy,
-                              uint32_t expectedPixelValues[]) {
-    GrSurfaceDesc copyDesc;
-    copyDesc.fConfig = kRGBA_8888_GrPixelConfig;
-    copyDesc.fWidth = externalTextureProxy->width();
-    copyDesc.fHeight = externalTextureProxy->height();
-    copyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
-
-    sk_sp<GrSurfaceContext> copyContext(GrSurfaceProxy::TestCopy(context, copyDesc,
-                                                                 externalTextureProxy));
-
-    test_read_pixels(reporter, context, copyContext.get(), expectedPixelValues);
-}
-
 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
     GrContext* context0 = ctxInfo.grContext();
     sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
@@ -223,12 +172,17 @@
         externalDesc.fSampleCnt = 0;
     }
 
-    test_read_pixels(reporter, context0, externalTextureContext.get(), pixels.get());
+    test_read_pixels(reporter, context0, externalTextureContext.get(), pixels.get(),
+                     "EGLImageTest-read");
 
-    test_write_pixels(reporter, context0, externalTextureContext.get());
+    // We should not be able to write to a EXTERNAL texture
+    test_write_pixels(reporter, context0, externalTextureContext.get(), false,
+                      "EGLImageTest-write");
 
-    test_copy_surface(reporter, context0, externalTextureContext->asDeferredSurface(),
-                      pixels.get());
+    // Only test RT-config
+    // TODO: why do we always need to draw to copy from an external texture?
+    test_copy_from_surface(reporter, context0, externalTextureContext->asDeferredSurface(),
+                           pixels.get(), true, "EGLImageTest-copy");
 
     cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
 }
diff --git a/tests/ImageTest.cpp b/tests/ImageTest.cpp
index 4f9d944..66f19fa 100644
--- a/tests/ImageTest.cpp
+++ b/tests/ImageTest.cpp
@@ -541,7 +541,7 @@
     return true;
 }
 
-static void test_read_pixels(skiatest::Reporter* reporter, SkImage* image) {
+static void image_test_read_pixels(skiatest::Reporter* reporter, SkImage* image) {
     if (!image) {
         ERRORF(reporter, "Failed to create image!");
         return;
@@ -591,23 +591,23 @@
 }
 DEF_TEST(ImageReadPixels, reporter) {
     sk_sp<SkImage> image(create_image());
-    test_read_pixels(reporter, image.get());
+    image_test_read_pixels(reporter, image.get());
 
     image = create_data_image();
-    test_read_pixels(reporter, image.get());
+    image_test_read_pixels(reporter, image.get());
 
     RasterDataHolder dataHolder;
     image = create_rasterproc_image(&dataHolder);
-    test_read_pixels(reporter, image.get());
+    image_test_read_pixels(reporter, image.get());
     image.reset();
     REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
 
     image = create_codec_image();
-    test_read_pixels(reporter, image.get());
+    image_test_read_pixels(reporter, image.get());
 }
 #if SK_SUPPORT_GPU
 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
-    test_read_pixels(reporter, create_gpu_image(ctxInfo.grContext()).get());
+    image_test_read_pixels(reporter, create_gpu_image(ctxInfo.grContext()).get());
 }
 #endif
 
diff --git a/tests/RectangleTextureTest.cpp b/tests/RectangleTextureTest.cpp
index 523c2e3..d55b882 100644
--- a/tests/RectangleTextureTest.cpp
+++ b/tests/RectangleTextureTest.cpp
@@ -6,6 +6,8 @@
  */
 
 #include "Test.h"
+#include "TestUtils.h"
+
 #if SK_SUPPORT_GPU
 #include "GrContext.h"
 #include "GrContextPriv.h"
@@ -14,92 +16,6 @@
 #include "gl/GrGLUtil.h"
 #include "gl/GLTestContext.h"
 
-static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
-                             GrSurfaceContext* srcContext, uint32_t expectedPixelValues[]) {
-    int pixelCnt = srcContext->width() * srcContext->height();
-    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
-    memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
-
-    SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
-                                       kRGBA_8888_SkColorType, kPremul_SkAlphaType);
-    bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0);
-    if (!read) {
-        ERRORF(reporter, "Error reading rectangle texture.");
-    }
-
-    for (int i = 0; i < pixelCnt; ++i) {
-        if (pixels.get()[i] != expectedPixelValues[i]) {
-            ERRORF(reporter, "Error, pixel value %d should be 0x%08x, got 0x%08x.", i,
-                   expectedPixelValues[i], pixels.get()[i]);
-            break;
-        }
-    }
-}
-
-static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
-                              GrSurfaceContext* rectSurfaceContext) {
-    int pixelCnt = rectSurfaceContext->width() * rectSurfaceContext->height();
-    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
-    for (int y = 0; y < rectSurfaceContext->width(); ++y) {
-        for (int x = 0; x < rectSurfaceContext->height(); ++x) {
-            pixels.get()[y * rectSurfaceContext->width() + x] = GrColorPackRGBA(x, y, x + y, x * y);
-        }
-    }
-
-    SkImageInfo ii = SkImageInfo::Make(rectSurfaceContext->width(), rectSurfaceContext->height(),
-                                       kRGBA_8888_SkColorType, kPremul_SkAlphaType);
-    bool write = rectSurfaceContext->writePixels(ii, pixels.get(), 0, 0, 0);
-    if (!write) {
-        ERRORF(reporter, "Error writing to rectangle texture.");
-    }
-
-    test_read_pixels(reporter, context, rectSurfaceContext, pixels.get());
-}
-
-static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context,
-                                  GrSurfaceProxy* rectProxy, uint32_t expectedPixelValues[]) {
-    GrSurfaceDesc copyDstDesc;
-    copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
-    copyDstDesc.fWidth = rectProxy->width();
-    copyDstDesc.fHeight = rectProxy->height();
-
-    for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
-        copyDstDesc.fFlags = flags;
-
-        sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc,
-                                                                    rectProxy));
-
-        test_read_pixels(reporter, context, dstContext.get(), expectedPixelValues);
-    }
-}
-
-static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context,
-                                  GrSurfaceContext* rectContext) {
-
-    int pixelCnt = rectContext->width() * rectContext->height();
-    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
-    for (int y = 0; y < rectContext->width(); ++y) {
-        for (int x = 0; x < rectContext->height(); ++x) {
-            pixels.get()[y * rectContext->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
-        }
-    }
-    for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
-        GrSurfaceDesc copySrcDesc;
-        copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
-        copySrcDesc.fWidth = rectContext->width();
-        copySrcDesc.fHeight = rectContext->height();
-        copySrcDesc.fFlags = flags;
-
-        sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(*context->caps(),
-                                                               context->textureProvider(),
-                                                               copySrcDesc,
-                                                               SkBudgeted::kYes, pixels.get(), 0));
-        rectContext->copy(src.get());
-
-        test_read_pixels(reporter, context, rectContext, pixels.get());
-    }
-}
-
 // skbug.com/5932
 static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
                                    sk_sp<GrSurfaceProxy> rectProxy, uint32_t expectedPixelValues[]) {
@@ -120,7 +36,8 @@
         paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
         paint.addColorFragmentProcessor(std::move(fp));
         rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
-        test_read_pixels(reporter, context, rtContext.get(), expectedPixelValues);
+        test_read_pixels(reporter, context, rtContext.get(), expectedPixelValues,
+                         "RectangleTexture-basic-draw");
     }
 }
 
@@ -165,7 +82,7 @@
             }
         }
 
-        test_read_pixels(reporter, context, rtc, expectedPixels.get());
+        test_read_pixels(reporter, context, rtc, expectedPixels.get(), "RectangleTexture-clear");
     }
 }
 
@@ -237,17 +154,19 @@
 
         test_basic_draw_as_src(reporter, context, rectProxy, refPixels);
 
-        test_copy_surface_src(reporter, context, rectProxy.get(), refPixels);
+        // Test copy to both a texture and RT
+        test_copy_from_surface(reporter, context, rectProxy.get(), refPixels,
+                               false, "RectangleTexture-copy-from");
 
         sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext(
                                                                     std::move(rectProxy), nullptr);
         SkASSERT(rectContext);
 
-        test_read_pixels(reporter, context, rectContext.get(), refPixels);
+        test_read_pixels(reporter, context, rectContext.get(), refPixels, "RectangleTexture-read");
 
-        test_copy_surface_dst(reporter, context, rectContext.get());
+        test_copy_to_surface(reporter, context, rectContext.get(), "RectangleTexture-copy-to");
 
-        test_write_pixels(reporter, context, rectContext.get());
+        test_write_pixels(reporter, context, rectContext.get(), true, "RectangleTexture-write");
 
         test_clear(reporter, context, rectContext.get());
 
diff --git a/tests/TestUtils.cpp b/tests/TestUtils.cpp
new file mode 100644
index 0000000..b809a67
--- /dev/null
+++ b/tests/TestUtils.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "TestUtils.h"
+
+#if SK_SUPPORT_GPU
+
+#include "GrSurfaceContext.h"
+#include "GrSurfaceProxy.h"
+
+void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
+                      GrSurfaceContext* srcContext, uint32_t expectedPixelValues[],
+                      const char* testName) {
+    int pixelCnt = srcContext->width() * srcContext->height();
+    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
+    memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
+
+    SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
+                                       kRGBA_8888_SkColorType, kPremul_SkAlphaType);
+    bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0);
+    if (!read) {
+        ERRORF(reporter, "%s: Error reading from texture.", testName);
+    }
+
+    for (int i = 0; i < pixelCnt; ++i) {
+        if (pixels.get()[i] != expectedPixelValues[i]) {
+            ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
+                   testName, i, expectedPixelValues[i], pixels.get()[i]);
+            break;
+        }
+    }
+}
+
+void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
+                       GrSurfaceContext* dstContext, bool expectedToWork,
+                       const char* testName) {
+    int pixelCnt = dstContext->width() * dstContext->height();
+    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
+    for (int y = 0; y < dstContext->width(); ++y) {
+        for (int x = 0; x < dstContext->height(); ++x) {
+            pixels.get()[y * dstContext->width() + x] =
+                GrPremulColor(GrColorPackRGBA(x, y, x + y, 2*y));
+        }
+    }
+
+    SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(),
+                                       kRGBA_8888_SkColorType, kPremul_SkAlphaType);
+    bool write = dstContext->writePixels(ii, pixels.get(), 0, 0, 0);
+    if (!write) {
+        if (expectedToWork) {
+            ERRORF(reporter, "%s: Error writing to texture.", testName);
+        }
+        return;
+    }
+
+    if (write && !expectedToWork) {
+        ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
+        return;
+    }
+
+    test_read_pixels(reporter, context, dstContext, pixels.get(), testName);
+}
+
+void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context,
+                            GrSurfaceProxy* proxy, uint32_t expectedPixelValues[],
+                            bool onlyTestRTConfig, const char* testName) {
+    GrSurfaceDesc copyDstDesc;
+    copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
+    copyDstDesc.fWidth = proxy->width();
+    copyDstDesc.fHeight = proxy->height();
+
+    for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
+        if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) {
+            continue;
+        }
+
+        copyDstDesc.fFlags = flags;
+
+        sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc, proxy));
+
+        test_read_pixels(reporter, context, dstContext.get(), expectedPixelValues, testName);
+    }
+}
+
+void test_copy_to_surface(skiatest::Reporter* reporter, GrContext* context,
+                          GrSurfaceContext* dstContext, const char* testName) {
+
+    int pixelCnt = dstContext->width() * dstContext->height();
+    SkAutoTMalloc<uint32_t> pixels(pixelCnt);
+    for (int y = 0; y < dstContext->width(); ++y) {
+        for (int x = 0; x < dstContext->height(); ++x) {
+            pixels.get()[y * dstContext->width() + x] =
+                GrPremulColor(GrColorPackRGBA(y, x, x * y, 2*y));
+        }
+    }
+
+    GrSurfaceDesc copySrcDesc;
+    copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
+    copySrcDesc.fWidth = dstContext->width();
+    copySrcDesc.fHeight = dstContext->height();
+
+    for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) {
+        copySrcDesc.fFlags = flags;
+
+        sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(*context->caps(),
+                                                               context->textureProvider(),
+                                                               copySrcDesc,
+                                                               SkBudgeted::kYes, pixels.get(), 0));
+        dstContext->copy(src.get());
+
+        test_read_pixels(reporter, context, dstContext, pixels.get(), testName);
+    }
+}
+
+#endif
diff --git a/tests/TestUtils.h b/tests/TestUtils.h
new file mode 100644
index 0000000..7c3370e
--- /dev/null
+++ b/tests/TestUtils.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Test.h"
+
+#if SK_SUPPORT_GPU
+
+class GrSurfaceContext;
+class GrSurfaceProxy;
+
+// Ensure that reading back from 'srcContext' as RGBA 8888 matches 'expectedPixelValues
+void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
+                      GrSurfaceContext* srcContext, uint32_t expectedPixelValues[],
+                      const char* testName);
+
+// See if trying to write RGBA 8888 pixels to 'dstContext' matches matches the
+// expectation ('expectedToWork')
+void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
+                       GrSurfaceContext* srcContext, bool expectedToWork, const char* testName);
+
+// Ensure that the pixels can be copied from 'proxy' to an RGBA 8888 destination (both
+// texture-backed and rendertarget-backed).
+void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context,
+                            GrSurfaceProxy* proxy, uint32_t expectedPixelValues[],
+                            bool onlyTestRTConfig, const char* testName);
+
+// Ensure that RGBA 8888 pixels can be copied into 'dstContext'
+void test_copy_to_surface(skiatest::Reporter* reporter, GrContext* context,
+                          GrSurfaceContext* dstContext, const char* testName);
+#endif