Revert "Add async rescale and read APIs to SkImage."

This reverts commit 7ac9b5fdb6ee226601ab468cdc957c1650f96bbf.

Reason for revert: abandon context bots breaking

Original change's description:
> Add async rescale and read APIs to SkImage.
> 
> These function the same as the already existing
> SkSurface APIs.
> 
> Bug: skia:10431
> 
> Change-Id: I4f1e842d8d4b72ee27bae5f8a85e499e130d420c
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/299281
> Reviewed-by: Brian Osman <brianosman@google.com>
> Reviewed-by: Greg Daniel <egdaniel@google.com>
> Commit-Queue: Brian Salomon <bsalomon@google.com>

TBR=egdaniel@google.com,bsalomon@google.com,brianosman@google.com

Change-Id: I351795274245fc9f553cd210d82178f497f22660
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: skia:10431
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/299376
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/gm/asyncrescaleandread.cpp b/gm/asyncrescaleandread.cpp
index 754e32d..f119144 100644
--- a/gm/asyncrescaleandread.cpp
+++ b/gm/asyncrescaleandread.cpp
@@ -23,14 +23,14 @@
 namespace {
 struct AsyncContext {
     bool fCalled = false;
-    std::unique_ptr<const SkImage::AsyncReadResult> fResult;
+    std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
 };
 }  // anonymous namespace
 
 // Making this a lambda in the test functions caused:
 //   "error: cannot compile this forwarded non-trivially copyable parameter yet"
 // on x86/Win/Clang bot, referring to 'result'.
-static void async_callback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) {
+static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
     auto context = static_cast<AsyncContext*>(c);
     context->fResult = std::move(result);
     context->fCalled = true;
@@ -38,70 +38,61 @@
 
 // Draws the image to a surface, does a asyncRescaleAndReadPixels of the image, and then sticks
 // the result in a raster image.
-template <typename Src>
-static sk_sp<SkImage> do_read_and_scale(Src* src,
-                                        GrContext* context,
-                                        const SkIRect& srcRect,
-                                        const SkImageInfo& ii,
-                                        SkImage::RescaleGamma rescaleGamma,
+static sk_sp<SkImage> do_read_and_scale(SkSurface* surface, const SkIRect& srcRect,
+                                        const SkImageInfo& ii, SkSurface::RescaleGamma rescaleGamma,
                                         SkFilterQuality quality) {
-    auto* asyncContext = new AsyncContext();
-    src->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, async_callback,
-                                   asyncContext);
-    if (context) {
-        context->submit();
+    auto* context = new AsyncContext();
+    surface->asyncRescaleAndReadPixels(ii, srcRect, rescaleGamma, quality, async_callback, context);
+    if (auto ctx = surface->getContext()) {
+        ctx->submit();
     }
-    while (!asyncContext->fCalled) {
+    while (!context->fCalled) {
         // Only GPU should actually be asynchronous.
-        SkASSERT(context);
-        context->checkAsyncWorkCompletion();
+        SkASSERT(surface->getCanvas()->getGrContext());
+        surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
     }
-    if (!asyncContext->fResult) {
+    if (!context->fResult) {
         return nullptr;
     }
-    SkPixmap pixmap(ii, asyncContext->fResult->data(0), asyncContext->fResult->rowBytes(0));
+    SkPixmap pixmap(ii, context->fResult->data(0), context->fResult->rowBytes(0));
     auto releasePixels = [](const void*, void* c) { delete static_cast<AsyncContext*>(c); };
-    return SkImage::MakeFromRaster(pixmap, releasePixels, asyncContext);
+    return SkImage::MakeFromRaster(pixmap, releasePixels, context);
 }
 
-template <typename Src>
-static sk_sp<SkImage> do_read_and_scale_yuv(Src* src,
-                                            GrContext* context,
-                                            SkYUVColorSpace yuvCS,
-                                            const SkIRect& srcRect,
-                                            SkISize size,
-                                            SkImage::RescaleGamma rescaleGamma,
-                                            SkFilterQuality quality,
-                                            SkScopeExit* cleanup) {
+static sk_sp<SkImage> do_read_and_scale_yuv(SkSurface* surface, SkYUVColorSpace yuvCS,
+                                            const SkIRect& srcRect, SkISize size,
+                                            SkSurface::RescaleGamma rescaleGamma,
+                                            SkFilterQuality quality, SkScopeExit* cleanup) {
     SkASSERT(!(size.width() & 0b1) && !(size.height() & 0b1));
 
     SkISize uvSize = {size.width()/2, size.height()/2};
     SkImageInfo yII  = SkImageInfo::Make(size,   kGray_8_SkColorType, kPremul_SkAlphaType);
     SkImageInfo uvII = SkImageInfo::Make(uvSize, kGray_8_SkColorType, kPremul_SkAlphaType);
 
-    AsyncContext asyncContext;
-    src->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size,
-                                         rescaleGamma, quality, async_callback, &asyncContext);
-    if (context) {
-        context->submit();
+    AsyncContext context;
+    surface->asyncRescaleAndReadPixelsYUV420(yuvCS, SkColorSpace::MakeSRGB(), srcRect, size,
+                                             rescaleGamma, quality, async_callback, &context);
+    if (auto ctx = surface->getContext()) {
+        ctx->submit();
     }
-    while (!asyncContext.fCalled) {
+    while (!context.fCalled) {
         // Only GPU should actually be asynchronous.
-        SkASSERT(context);
-        context->checkAsyncWorkCompletion();
+        SkASSERT(surface->getCanvas()->getGrContext());
+        surface->getCanvas()->getGrContext()->checkAsyncWorkCompletion();
     }
-    if (!asyncContext.fResult) {
+    if (!context.fResult) {
         return nullptr;
     }
+    auto* gr = surface->getCanvas()->getGrContext();
     GrBackendTexture backendTextures[3];
 
-    SkPixmap yPM(yII, asyncContext.fResult->data(0), asyncContext.fResult->rowBytes(0));
-    SkPixmap uPM(uvII, asyncContext.fResult->data(1), asyncContext.fResult->rowBytes(1));
-    SkPixmap vPM(uvII, asyncContext.fResult->data(2), asyncContext.fResult->rowBytes(2));
+    SkPixmap yPM(yII,  context.fResult->data(0), context.fResult->rowBytes(0));
+    SkPixmap uPM(uvII, context.fResult->data(1), context.fResult->rowBytes(1));
+    SkPixmap vPM(uvII, context.fResult->data(2), context.fResult->rowBytes(2));
 
-    backendTextures[0] = context->createBackendTexture(yPM, GrRenderable::kNo, GrProtected::kNo);
-    backendTextures[1] = context->createBackendTexture(uPM, GrRenderable::kNo, GrProtected::kNo);
-    backendTextures[2] = context->createBackendTexture(vPM, GrRenderable::kNo, GrProtected::kNo);
+    backendTextures[0] = gr->createBackendTexture(yPM, GrRenderable::kNo, GrProtected::kNo);
+    backendTextures[1] = gr->createBackendTexture(uPM, GrRenderable::kNo, GrProtected::kNo);
+    backendTextures[2] = gr->createBackendTexture(vPM, GrRenderable::kNo, GrProtected::kNo);
 
     SkYUVAIndex indices[4] = {
         { 0, SkColorChannel::kR},
@@ -110,31 +101,25 @@
         {-1, SkColorChannel::kR}
     };
 
-    *cleanup = {[context, backendTextures] {
+    *cleanup = {[gr, backendTextures] {
         GrFlushInfo flushInfo;
         flushInfo.fFlags = kSyncCpu_GrFlushFlag;
-        context->flush(flushInfo);
-        context->submit(true);
-        context->deleteBackendTexture(backendTextures[0]);
-        context->deleteBackendTexture(backendTextures[1]);
-        context->deleteBackendTexture(backendTextures[2]);
+        gr->flush(flushInfo);
+        gr->submit(true);
+        gr->deleteBackendTexture(backendTextures[0]);
+        gr->deleteBackendTexture(backendTextures[1]);
+        gr->deleteBackendTexture(backendTextures[2]);
     }};
 
-    return SkImage::MakeFromYUVATextures(context, yuvCS, backendTextures, indices, size,
+    return SkImage::MakeFromYUVATextures(gr, yuvCS, backendTextures, indices, size,
                                          kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB());
 }
 
 // Draws a grid of rescales. The columns are none, low, and high filter quality. The rows are
 // rescale in src gamma and rescale in linear gamma.
-template <typename Src>
-static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas,
-                                          Src* src,
-                                          GrContext* context,
-                                          const SkIRect& srcRect,
-                                          SkISize newSize,
-                                          bool doYUV420,
-                                          SkString* errorMsg,
-                                          int pad = 0) {
+static skiagm::DrawResult do_rescale_grid(SkCanvas* canvas, SkSurface* surface,
+                                          const SkIRect& srcRect, SkISize newSize, bool doYUV420,
+                                          SkString* errorMsg, int pad = 0) {
     if (doYUV420) {
         if (!canvas->getGrContext() || !canvas->getGrContext()->priv().asDirectContext()) {
             errorMsg->printf("YUV420 only supported on direct GPU for now.");
@@ -149,13 +134,13 @@
 
     SkYUVColorSpace yuvColorSpace = kRec601_SkYUVColorSpace;
     canvas->save();
-    for (auto gamma : {SkImage::RescaleGamma::kSrc, SkImage::RescaleGamma::kLinear}) {
+    for (auto gamma : {SkSurface::RescaleGamma::kSrc, SkSurface::RescaleGamma::kLinear}) {
         canvas->save();
         for (auto quality : {kNone_SkFilterQuality, kLow_SkFilterQuality, kHigh_SkFilterQuality}) {
             SkScopeExit cleanup;
             sk_sp<SkImage> result;
             if (doYUV420) {
-                result = do_read_and_scale_yuv(src, context, yuvColorSpace, srcRect, newSize, gamma,
+                result = do_read_and_scale_yuv(surface, yuvColorSpace, srcRect, newSize, gamma,
                                                quality, &cleanup);
                 if (!result) {
                     errorMsg->printf("YUV420 async call failed. Allowed for now.");
@@ -164,7 +149,7 @@
                 int nextCS = static_cast<int>(yuvColorSpace + 1) % (kLastEnum_SkYUVColorSpace + 1);
                 yuvColorSpace = static_cast<SkYUVColorSpace>(nextCS);
             } else {
-                result = do_read_and_scale(src, context, srcRect, ii, gamma, quality);
+                result = do_read_and_scale(surface, srcRect, ii, gamma, quality);
                 if (!result) {
                     errorMsg->printf("async read call failed.");
                     return skiagm::DrawResult::kFail;
@@ -180,13 +165,9 @@
     return skiagm::DrawResult::kOk;
 }
 
-static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas,
-                                                const char* imageFile,
-                                                const SkIRect& srcRect,
-                                                SkISize newSize,
-                                                bool doSurface,
-                                                bool doYUV420,
-                                                SkString* errorMsg) {
+static skiagm::DrawResult do_rescale_image_grid(SkCanvas* canvas, const char* imageFile,
+                                                const SkIRect& srcRect, SkISize newSize,
+                                                bool doYUV420, SkString* errorMsg) {
     auto image = GetResourceAsImage(imageFile);
     if (!image) {
         errorMsg->printf("Could not load image file %s.", imageFile);
@@ -196,81 +177,54 @@
         *errorMsg = "Not supported on recording/vector backends.";
         return skiagm::DrawResult::kSkip;
     }
-    if (doSurface) {
-        // Turn the image into a surface in order to call the read and rescale API
-        auto surfInfo = image->imageInfo().makeDimensions(image->dimensions());
-        auto surface = canvas->makeSurface(surfInfo);
-        if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) {
-            surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType);
-            surface = canvas->makeSurface(surfInfo);
-        }
-        if (!surface) {
-            *errorMsg = "Could not create surface for image.";
-            // When testing abandoned GrContext we expect surface creation to fail.
-            if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
-                return skiagm::DrawResult::kSkip;
-            }
-            return skiagm::DrawResult::kFail;
-        }
-        SkPaint paint;
-        paint.setBlendMode(SkBlendMode::kSrc);
-        surface->getCanvas()->drawImage(image, 0, 0, &paint);
-        return do_rescale_grid(canvas, surface.get(), canvas->getGrContext(), srcRect, newSize,
-                               doYUV420, errorMsg);
-    } else if (auto ctx = canvas->getGrContext()) {
-        image = image->makeTextureImage(ctx);
+    // Turn the image into a surface in order to call the read and rescale API
+    auto surfInfo = image->imageInfo().makeDimensions(image->dimensions());
+    auto surface = canvas->makeSurface(surfInfo);
+    if (!surface && surfInfo.colorType() == kBGRA_8888_SkColorType) {
+        surfInfo = surfInfo.makeColorType(kRGBA_8888_SkColorType);
+        surface = canvas->makeSurface(surfInfo);
     }
-    return do_rescale_grid(canvas, image.get(), canvas->getGrContext(), srcRect, newSize, doYUV420,
-                           errorMsg);
+    if (!surface) {
+        *errorMsg = "Could not create surface for image.";
+        // When testing abandoned GrContext we expect surface creation to fail.
+        if (canvas->getGrContext() && canvas->getGrContext()->abandoned()) {
+            return skiagm::DrawResult::kSkip;
+        }
+        return skiagm::DrawResult::kFail;
+    }
+    SkPaint paint;
+    paint.setBlendMode(SkBlendMode::kSrc);
+    surface->getCanvas()->drawImage(image, 0, 0, &paint);
+    return do_rescale_grid(canvas, surface.get(), srcRect, newSize, doYUV420, errorMsg);
 }
 
-#define DEF_RESCALE_AND_READ_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                      \
-    DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
-        ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);          \
-        return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, false,   \
-                                     errorMsg);                                            \
+#define DEF_RESCALE_AND_READ_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                              \
+    DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) {    \
+        ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);             \
+        return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, errorMsg); \
     }
 
-#define DEF_RESCALE_AND_READ_YUV_SURF_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                          \
-    DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) {  \
-        ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);                  \
-        return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \
+#define DEF_RESCALE_AND_READ_YUV_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                              \
+    DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
+        ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);                 \
+        return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, errorMsg);      \
     }
 
-#define DEF_RESCALE_AND_READ_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                       \
-    DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_##TAG, canvas, errorMsg, 3 * W, 2 * H) { \
-        ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);          \
-        return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, false, false,  \
-                                     errorMsg);                                            \
-    }
+DEF_RESCALE_AND_READ_YUV_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150),
+                            410, 376)
 
-#define DEF_RESCALE_AND_READ_YUV_IMG_GM(IMAGE_FILE, TAG, SRC_RECT, W, H)                           \
-    DEF_SIMPLE_GM_CAN_FAIL(async_rescale_and_read_yuv420_##TAG, canvas, errorMsg, 3 * W, 2 * H) {  \
-        ToolUtils::draw_checkerboard(canvas, SK_ColorDKGRAY, SK_ColorLTGRAY, 25);                  \
-        return do_rescale_image_grid(canvas, #IMAGE_FILE, SRC_RECT, {W, H}, true, true, errorMsg); \
-    }
+DEF_RESCALE_AND_READ_GM(images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100),
+                        410, 410)
 
-DEF_RESCALE_AND_READ_YUV_SURF_GM(
-        images/yellow_rose.webp, rose, SkIRect::MakeXYWH(50, 5, 200, 150), 410, 376)
+DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
+DEF_RESCALE_AND_READ_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
 
-DEF_RESCALE_AND_READ_YUV_IMG_GM(
-        images/yellow_rose.webp, rose_down, SkIRect::MakeXYWH(50, 5, 200, 150), 106, 60)
-
-DEF_RESCALE_AND_READ_SURF_GM(
-        images/yellow_rose.webp, rose, SkIRect::MakeXYWH(100, 20, 100, 100), 410, 410)
-
-DEF_RESCALE_AND_READ_SURF_GM(images/dog.jpg, dog_down, SkIRect::MakeXYWH(0, 10, 180, 150), 45, 45)
-DEF_RESCALE_AND_READ_IMG_GM(images/dog.jpg, dog_up, SkIRect::MakeWH(180, 180), 800, 400)
-
-DEF_RESCALE_AND_READ_IMG_GM(
-        images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637), (int)(0.7 * 105))
-DEF_RESCALE_AND_READ_SURF_GM(
-        images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637), (int)(1.2 * 105))
-DEF_RESCALE_AND_READ_IMG_GM(images/text.png,
-                            text_up_large,
-                            SkIRect::MakeXYWH(300, 0, 300, 105),
-                            (int)(2.4 * 300),
-                            (int)(2.4 * 105))
+DEF_RESCALE_AND_READ_GM(images/text.png, text_down, SkIRect::MakeWH(637, 105), (int)(0.7 * 637),
+                        (int)(0.7 * 105))
+DEF_RESCALE_AND_READ_GM(images/text.png, text_up, SkIRect::MakeWH(637, 105), (int)(1.2 * 637),
+                        (int)(1.2 * 105))
+DEF_RESCALE_AND_READ_GM(images/text.png, text_up_large, SkIRect::MakeXYWH(300, 0, 300, 105),
+                        (int)(2.4 * 300), (int)(2.4 * 105))
 
 // Exercises non-scaling YUV420. Reads from the original canvas's surface in order to
 // exercise case where source surface is not a texture (in glbert config).
@@ -290,8 +244,8 @@
 
     SkScopeExit scopeExit;
     auto yuvImage = do_read_and_scale_yuv(
-            surface, surface->getContext(), kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300),
-            {400, 300}, SkImage::RescaleGamma::kSrc, kNone_SkFilterQuality, &scopeExit);
+            surface, kRec601_SkYUVColorSpace, SkIRect::MakeWH(400, 300), {400, 300},
+            SkSurface::RescaleGamma::kSrc, kNone_SkFilterQuality, &scopeExit);
 
     canvas->clear(SK_ColorWHITE);
     canvas->drawImage(yuvImage.get(), 0, 0);
@@ -329,17 +283,13 @@
     canvas->translate(kPad, kPad);
     skiagm::DrawResult result;
     SkISize downSize = {static_cast<int>(kInner/2),  static_cast<int>(kInner / 2)};
-    GrContext* context = canvas->getGrContext();
-    result = do_rescale_grid(canvas, surface.get(), context, srcRect, downSize, false, errorMsg,
-                             kPad);
-
+    result = do_rescale_grid(canvas, surface.get(), srcRect, downSize, false, errorMsg, kPad);
     if (result != skiagm::DrawResult::kOk) {
         return result;
     }
     canvas->translate(0, 4 * downSize.height());
     SkISize upSize = {static_cast<int>(kInner * 3.5), static_cast<int>(kInner * 4.6)};
-    result =
-            do_rescale_grid(canvas, surface.get(), context, srcRect, upSize, false, errorMsg, kPad);
+    result = do_rescale_grid(canvas, surface.get(), srcRect, upSize, false, errorMsg, kPad);
     if (result != skiagm::DrawResult::kOk) {
         return result;
     }