Add check_mipmaps subtest to BackendAllocation test

This is setting up the test for SkPixmap-initialized backend texture testing

Change-Id: I667a0e6d622c9408d7fb42fc4b5792b0efa2ffb4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/243736
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/tests/BackendAllocationTest.cpp b/tests/BackendAllocationTest.cpp
index 0b843bb..eaabcf1 100644
--- a/tests/BackendAllocationTest.cpp
+++ b/tests/BackendAllocationTest.cpp
@@ -20,7 +20,7 @@
 #include "src/gpu/gl/GrGLUtil.h"
 #endif
 
-// Test wrapping of GrBackendObjects in SkSurfaces and SkImages
+// Test wrapping of GrBackendObjects in SkSurfaces and SkImages (non-static since used in Mtl test)
 void test_wrapping(GrContext* context, skiatest::Reporter* reporter,
                    std::function<GrBackendTexture (GrContext*,
                                                    GrMipMapped,
@@ -140,7 +140,11 @@
     return { r, g, b, a };
 }
 
-// Test initialization of GrBackendObjects to a specific color
+static void check_mipmaps(GrContext* context, const GrBackendTexture& backendTex,
+                          SkColorType skColorType, const SkColor4f expectedColors[6],
+                          skiatest::Reporter* reporter);
+
+// Test initialization of GrBackendObjects to a specific color (non-static since used in Mtl test)
 void test_color_init(GrContext* context, skiatest::Reporter* reporter,
                      std::function<GrBackendTexture (GrContext*,
                                                      const SkColor4f&,
@@ -215,70 +219,88 @@
                                        "SkImage::readPixels");
                 }
             }
-
-            // Draw the wrapped image into an RGBA surface attempting to access all the
-            // mipMap levels.
-            {
-#ifdef SK_GL
-                // skbug.com/9141 (RGBA_F32 mipmaps appear to be broken on some Mali devices)
-                if (GrBackendApi::kOpenGL == context->backend()) {
-                    GrGLGpu* glGPU = static_cast<GrGLGpu*>(context->priv().getGpu());
-
-                    if (kRGBA_F32_SkColorType == skColorType && GrMipMapped::kYes == mipMapped &&
-                        kGLES_GrGLStandard == glGPU->ctxInfo().standard()) {
-                        context->deleteBackendTexture(backendTex);
-                        return;
-                    }
-                }
-#endif
-
-                SkImageInfo newII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
-                                                      kPremul_SkAlphaType);
-
-                sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context,
-                                                                    SkBudgeted::kNo,
-                                                                    newII, 1,
-                                                                    kTopLeft_GrSurfaceOrigin,
-                                                                    nullptr);
-                if (!surf) {
-                    context->deleteBackendTexture(backendTex);
-                    return;
-                }
-
-                SkCanvas* canvas = surf->getCanvas();
-
-                SkPaint p;
-                p.setFilterQuality(kHigh_SkFilterQuality);
-
-                int numMipLevels = (GrMipMapped::kYes == mipMapped) ? 6 : 1;
-
-                for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) {
-                    SkASSERT(rectSize >= 1);
-
-                    SkRect r = SkRect::MakeWH(rectSize, rectSize);
-                    canvas->clear(SK_ColorTRANSPARENT);
-                    canvas->drawImageRect(img, r, &p);
-
-                    SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize,
-                                                               kRGBA_8888_SkColorType,
-                                                               kPremul_SkAlphaType);
-                    SkAutoPixmapStorage actual2;
-                    SkAssertResult(actual2.tryAlloc(readbackII));
-                    actual2.erase(SkColors::kTransparent);
-
-                    bool result = surf->readPixels(actual2, 0, 0);
-                    REPORTER_ASSERT(reporter, result);
-
-                    check_solid_pixmap(reporter, expectedColor, actual2, skColorType,
-                                       "mip-level failure");
-                }
-            }
         }
     }
 
+    if (mipMapped == GrMipMapped::kYes) {
+        SkColor4f expectedColor = get_expected_color(color, skColorType);
+        SkColor4f expectedColors[6] = { expectedColor, expectedColor, expectedColor,
+                                        expectedColor, expectedColor, expectedColor };
+        check_mipmaps(context, backendTex, skColorType, expectedColors, reporter);
+    }
+
     context->deleteBackendTexture(backendTex);
 }
 
+// Draw the backend texture (wrapped in an SkImage) into an RGBA surface, attempting to access
+// all the mipMap levels.
+static void check_mipmaps(GrContext* context, const GrBackendTexture& backendTex,
+                          SkColorType skColorType, const SkColor4f expectedColors[6],
+                          skiatest::Reporter* reporter) {
+
+#ifdef SK_GL
+    // skbug.com/9141 (RGBA_F32 mipmaps appear to be broken on some Mali devices)
+    if (GrBackendApi::kOpenGL == context->backend()) {
+        GrGLGpu* glGPU = static_cast<GrGLGpu*>(context->priv().getGpu());
+
+        if (kRGBA_F32_SkColorType == skColorType &&
+            kGLES_GrGLStandard == glGPU->ctxInfo().standard()) {
+            return;
+        }
+    }
+#endif
+
+    SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType
+                                                            : kPremul_SkAlphaType;
+    sk_sp<SkImage> img = SkImage::MakeFromTexture(context,
+                                                  backendTex,
+                                                  kTopLeft_GrSurfaceOrigin,
+                                                  skColorType,
+                                                  at,
+                                                  nullptr);
+    if (!img) {
+        return;
+    }
+
+    SkImageInfo newII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
+
+    sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context,
+                                                        SkBudgeted::kNo,
+                                                        newII, 1,
+                                                        kTopLeft_GrSurfaceOrigin,
+                                                        nullptr);
+    if (!surf) {
+        return;
+    }
+
+    SkCanvas* canvas = surf->getCanvas();
+
+    SkPaint p;
+    p.setFilterQuality(kHigh_SkFilterQuality);
+
+    int numMipLevels = 6;
+
+    for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) {
+        SkASSERT(rectSize >= 1);
+
+        SkRect r = SkRect::MakeWH(rectSize, rectSize);
+        canvas->clear(SK_ColorTRANSPARENT);
+        canvas->drawImageRect(img, r, &p);
+
+        SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize,
+                                                   kRGBA_8888_SkColorType,
+                                                   kPremul_SkAlphaType);
+        SkAutoPixmapStorage actual2;
+        SkAssertResult(actual2.tryAlloc(readbackII));
+        actual2.erase(SkColors::kTransparent);
+
+        bool result = surf->readPixels(actual2, 0, 0);
+        REPORTER_ASSERT(reporter, result);
+
+        check_solid_pixmap(reporter, expectedColors[i], actual2, skColorType, "mip-level failure");
+    }
+}
+
 enum class VkLayout {
     kUndefined,
     kReadOnlyOptimal,