Ensure that textures exported via SkImage::MakeBackendTextureFromSkImage
have consistent content in their mip map levels.

Bug= chromium:850617

Change-Id: I3ad918aa453bd8e4e625eb145de6ba2a5dab7b0c
Reviewed-on: https://skia-review.googlesource.com/136230
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/tests/GrTextureMipMapInvalidationTest.cpp b/tests/GrTextureMipMapInvalidationTest.cpp
index 8da79c9..ea2d0de 100644
--- a/tests/GrTextureMipMapInvalidationTest.cpp
+++ b/tests/GrTextureMipMapInvalidationTest.cpp
@@ -5,9 +5,8 @@
  * found in the LICENSE file.
  */
 
-#include "SkTypes.h"
-
 #include "GrContext.h"
+#include "GrContextPriv.h"
 #include "GrTexturePriv.h"
 #include "SkCanvas.h"
 #include "SkImage_Base.h"
@@ -40,18 +39,65 @@
         REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
 
         // Painting with downscale and medium filter quality should result in mipmap creation
+        // Flush the context rather than the canvas as flushing the canvas triggers MIP level
+        // generation.
         SkPaint paint;
         paint.setFilterQuality(kMedium_SkFilterQuality);
         surf2->getCanvas()->scale(0.2f, 0.2f);
         surf2->getCanvas()->drawImage(surf1->makeImageSnapshot(), 0, 0, &paint);
-        surf2->getCanvas()->flush();
+        context->flush();
         REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
         REPORTER_ASSERT(reporter, !allocateMips || !mipsAreDirty(surf1.get()));
 
         // Changing the contents of the surface should invalidate the mipmap, but not de-allocate
         surf1->getCanvas()->drawCircle(128, 128, 100, SkPaint());
-        surf1->getCanvas()->flush();
+        context->flush();
         REPORTER_ASSERT(reporter, isMipped(surf1.get()) == allocateMips);
         REPORTER_ASSERT(reporter, mipsAreDirty(surf1.get()));
     }
 }
+
+DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReimportImageTextureWithMipLevels, reporter, ctxInfo) {
+    auto* ctx = ctxInfo.grContext();
+    if (!ctx->contextPriv().caps()->mipMapSupport()) {
+        return;
+    }
+    static constexpr auto kCreateWithMipMaps = true;
+    auto surf = SkSurface::MakeRenderTarget(
+            ctx, SkBudgeted::kYes,
+            SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
+            kTopLeft_GrSurfaceOrigin, nullptr, kCreateWithMipMaps);
+    if (!surf) {
+        return;
+    }
+    surf->getCanvas()->drawColor(SK_ColorDKGRAY);
+    auto img = surf->makeImageSnapshot();
+    if (!img) {
+        return;
+    }
+    surf.reset();
+    GrBackendTexture btex;
+    SkImage::BackendTextureReleaseProc texRelease;
+    if (!SkImage::MakeBackendTextureFromSkImage(ctx, std::move(img), &btex, &texRelease)) {
+        // Not all backends support stealing textures yet.
+        // ERRORF(reporter, "Could not turn image into texture");
+        return;
+    }
+    REPORTER_ASSERT(reporter, btex.hasMipMaps());
+    // Reimport the texture as an image and perform a downsampling draw with medium quality which
+    // should use the upper MIP levels.
+    img = SkImage::MakeFromTexture(ctx, btex, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
+                                   kPremul_SkAlphaType, nullptr);
+    const auto singlePixelInfo =
+            SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
+    surf = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kYes, singlePixelInfo, 1,
+                                       kTopLeft_GrSurfaceOrigin, nullptr);
+    SkPaint paint;
+    paint.setFilterQuality(kMedium_SkFilterQuality);
+    surf->getCanvas()->drawImageRect(img, SkRect::MakeWH(1, 1), &paint);
+    uint32_t pixel;
+    surf->readPixels(singlePixelInfo, &pixel, sizeof(uint32_t), 0, 0);
+    REPORTER_ASSERT(reporter, pixel == SkPreMultiplyColor(SK_ColorDKGRAY));
+    img.reset();
+    texRelease(btex);
+}