Remove Skia-internal uses of SkImage::makeWithFilter

Bug: b/293326072
Bug: b/293475819
Change-Id: I5eb34a9c42bd0595fb910e01a946b84fefe4f5ab
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/729257
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/bench/ImageFilterDAGBench.cpp b/bench/ImageFilterDAGBench.cpp
index 1325be5..4592bbe 100644
--- a/bench/ImageFilterDAGBench.cpp
+++ b/bench/ImageFilterDAGBench.cpp
@@ -9,10 +9,16 @@
 #include "include/core/SkCanvas.h"
 #include "include/core/SkImage.h"
 #include "include/effects/SkImageFilters.h"
-#include "include/gpu/GrDirectContext.h"
+#include "tools/Resources.h"
+
+#if defined(SK_GANESH)
 #include "include/gpu/GrRecordingContext.h"
 #include "include/gpu/ganesh/SkImageGanesh.h"
-#include "tools/Resources.h"
+#endif
+
+#if defined(SK_GRAPHITE)
+#include "include/gpu/graphite/Image.h"
+#endif
 
 // Exercise a blur filter connected to 5 inputs of the same merge filter.
 // This bench shows an improvement in performance once cacheing of re-used
@@ -74,13 +80,6 @@
         SkIPoint offset = SkIPoint::Make(0, 0);
         SkIRect discardSubset;
 
-        auto dContext = GrAsDirectContext(canvas->recordingContext());
-        // makeWithFilter will only use the GPU backend if the image is already a texture
-        sk_sp<SkImage> image = SkImages::TextureFromImage(dContext, fImage);
-        if (!image) {
-            image = fImage;
-        }
-
         // Set up the filters once so the allocation cost isn't included per-loop
         sk_sp<SkImageFilter> blur(SkImageFilters::Blur(20.0f, 20.0f, nullptr));
         sk_sp<SkImageFilter> inputs[kNumInputs];
@@ -91,8 +90,24 @@
 
         // But measure makeWithFilter() per loop since that's the focus of this benchmark
         for (int j = 0; j < loops; j++) {
-            image = image->makeWithFilter(dContext, mergeFilter.get(), subset, subset,
-                                          &discardSubset, &offset);
+            sk_sp<SkImage> image;
+
+#if defined(SK_GANESH)
+            if (auto rContext = canvas->recordingContext()) {
+                image = SkImages::MakeWithFilter(rContext, fImage, mergeFilter.get(),
+                                                 subset, subset, &discardSubset, &offset);
+            } else
+#endif
+#if defined(SK_GRAPHITE)
+            if (auto recorder = canvas->recorder()) {
+                image = SkImages::MakeWithFilter(recorder, fImage, mergeFilter.get(),
+                                                 subset, subset, &discardSubset, &offset);
+            } else
+#endif
+            {
+                image = SkImages::MakeWithFilter(fImage, mergeFilter.get(),
+                                                 subset, subset, &discardSubset, &offset);
+            }
             SkASSERT(image && image->dimensions() == fImage->dimensions());
         }
     }
diff --git a/docs/examples/Image_makeWithFilter.cpp b/docs/examples/Image_makeWithFilter.cpp
index 9ccba49..94942f8 100644
--- a/docs/examples/Image_makeWithFilter.cpp
+++ b/docs/examples/Image_makeWithFilter.cpp
@@ -12,8 +12,16 @@
     clipBounds.outset(60, 60);
     SkIRect outSubset;
     SkIPoint offset;
-    sk_sp<SkImage> filtered(image->makeWithFilter(canvas->recordingContext(), offsetFilter.get(),
-                                                  subset, clipBounds, &outSubset, &offset));
+    sk_sp<SkImage> filtered;
+
+    if (auto rContext = canvas->recordingContext()) {
+        filtered = SkImages::MakeWithFilter(rContext, image, offsetFilter.get(),
+                                            subset, clipBounds, &outSubset, &offset);
+    } else {
+        filtered = SkImages::MakeWithFilter(image, offsetFilter.get(),
+                                            subset, clipBounds, &outSubset, &offset);
+    }
+
     SkPaint paint;
     paint.setAntiAlias(true);
     paint.setStyle(SkPaint::kStroke_Style);
diff --git a/gm/imagemakewithfilter.cpp b/gm/imagemakewithfilter.cpp
index 970054c..f84dcca 100644
--- a/gm/imagemakewithfilter.cpp
+++ b/gm/imagemakewithfilter.cpp
@@ -25,14 +25,19 @@
 #include "include/core/SkString.h"
 #include "include/core/SkSurface.h"
 #include "include/core/SkTypes.h"
-
 #include "include/effects/SkImageFilters.h"
-
-#include "include/gpu/GrDirectContext.h"
-
 #include "tools/Resources.h"
 #include "tools/ToolUtils.h"
 
+#if defined(SK_GANESH)
+#include "include/gpu/GrDirectContext.h"
+#include "include/gpu/ganesh/SkImageGanesh.h"
+#endif
+
+#if defined(SK_GRAPHITE)
+#include "include/gpu/graphite/Image.h"
+#endif
+
 #include <utility>
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -367,9 +372,23 @@
             SkIRect outSubset;
             SkIPoint offset;
 
-            auto rContext = canvas->recordingContext();
-            result = mainImage->makeWithFilter(rContext, filter.get(), subset, clip,
-                                               &outSubset, &offset);
+#if defined(SK_GANESH)
+            if (auto rContext = canvas->recordingContext()) {
+                result = SkImages::MakeWithFilter(rContext, mainImage, filter.get(),
+                                                  subset, clip, &outSubset, &offset);
+            } else
+#endif
+#if defined(SK_GRAPHITE)
+            if (auto recorder = canvas->recorder()){
+                result = SkImages::MakeWithFilter(recorder, mainImage, filter.get(),
+                                                  subset, clip, &outSubset, &offset);
+            } else
+#endif
+            {
+                result = SkImages::MakeWithFilter(mainImage, filter.get(),
+                                                  subset, clip, &outSubset, &offset);
+            }
+
             if (!result) {
                 return;
             }
diff --git a/tests/ImageFilterTest.cpp b/tests/ImageFilterTest.cpp
index 2eee40f..9d73774 100644
--- a/tests/ImageFilterTest.cpp
+++ b/tests/ImageFilterTest.cpp
@@ -39,10 +39,7 @@
 #include "include/effects/SkImageFilters.h"
 #include "include/effects/SkPerlinNoiseShader.h"
 #include "include/gpu/GpuTypes.h"
-#include "include/gpu/GrDirectContext.h"
-#include "include/gpu/GrRecordingContext.h"
 #include "include/gpu/GrTypes.h"
-#include "include/gpu/ganesh/SkSurfaceGanesh.h"
 #include "include/private/base/SkTArray.h"
 #include "include/private/base/SkTo.h"
 #include "src/core/SkImageFilterTypes.h"
@@ -52,15 +49,30 @@
 #include "src/core/SkSpecialSurface.h"
 #include "src/effects/colorfilters/SkColorFilterBase.h"
 #include "src/effects/imagefilters/SkCropImageFilter.h"
-#include "src/gpu/ganesh/GrCaps.h"
-#include "src/gpu/ganesh/GrRecordingContextPriv.h"
-#include "src/gpu/ganesh/image/GrImageUtils.h"
-#include "src/gpu/ganesh/image/SkSpecialImage_Ganesh.h"
+#include "src/image/SkImage_Base.h"
 #include "tests/CtsEnforcement.h"
 #include "tests/Test.h"
 #include "tools/Resources.h"
 #include "tools/ToolUtils.h"
 
+#if defined(SK_GANESH)
+#include "include/gpu/GrDirectContext.h"
+#include "include/gpu/GrRecordingContext.h"
+#include "include/gpu/ganesh/SkImageGanesh.h"
+#include "include/gpu/ganesh/SkSurfaceGanesh.h"
+#include "src/gpu/ganesh/GrCaps.h"
+#include "src/gpu/ganesh/GrRecordingContextPriv.h"
+#include "src/gpu/ganesh/image/GrImageUtils.h"
+#include "src/gpu/ganesh/image/SkImage_GaneshBase.h"
+#include "src/gpu/ganesh/image/SkSpecialImage_Ganesh.h"
+#endif
+
+#if defined(SK_GRAPHITE)
+#include "include/gpu/graphite/Context.h"
+#include "include/gpu/graphite/Image.h"
+#include "include/gpu/graphite/Surface.h"
+#endif
+
 #include <algorithm>
 #include <cstdint>
 #include <cstring>
@@ -355,16 +367,6 @@
     }
 }
 
-static sk_sp<SkSurface> create_surface(GrRecordingContext* rContext, int width, int height) {
-    const SkImageInfo info = SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType);
-    if (rContext) {
-        return SkSurfaces::RenderTarget(
-                rContext, skgpu::Budgeted::kNo, info, 0, kTestSurfaceOrigin, nullptr);
-    } else {
-        return SkSurfaces::Raster(info);
-    }
-}
-
 static sk_sp<SkSpecialImage> create_empty_special_image(GrRecordingContext* rContext,
                                                         int widthHeight) {
     sk_sp<SkSpecialSurface> surf(create_empty_special_surface(rContext, widthHeight));
@@ -1712,8 +1714,16 @@
     test_large_blur_input(reporter, surface->getCanvas());
 }
 
-static void test_make_with_filter(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
-    sk_sp<SkSurface> surface(create_surface(rContext, 192, 128));
+static void test_make_with_filter(
+        skiatest::Reporter* reporter,
+        const std::function<sk_sp<SkSurface>(int width, int height)>& createSurface,
+        const std::function<sk_sp<SkImage>(sk_sp<SkImage> src,
+                                           const SkImageFilter* filter,
+                                           const SkIRect& subset,
+                                           const SkIRect& clipBounds,
+                                           SkIRect* outSubset,
+                                           SkIPoint* offset)>& makeWithFilter) {
+    sk_sp<SkSurface> surface(createSurface(192, 128));
     surface->getCanvas()->clear(SK_ColorRED);
     SkPaint bluePaint;
     bluePaint.setColor(SK_ColorBLUE);
@@ -1727,44 +1737,36 @@
     SkIPoint offset;
     sk_sp<SkImage> result;
 
-    result = sourceImage->makeWithFilter(rContext, nullptr, subset, clipBounds,
-                                         &outSubset, &offset);
-    REPORTER_ASSERT(reporter, !result);
+    result = makeWithFilter(sourceImage, nullptr, subset, clipBounds, &outSubset, &offset);
+    REPORTER_ASSERT(reporter, !result);  // filter is required
 
-    result = sourceImage->makeWithFilter(rContext, filter.get(), subset, clipBounds,
-                                         nullptr, &offset);
-    REPORTER_ASSERT(reporter, !result);
+    result = makeWithFilter(sourceImage, filter.get(), subset, clipBounds, nullptr, &offset);
+    REPORTER_ASSERT(reporter, !result);  // outSubset is required
 
-    result = sourceImage->makeWithFilter(rContext, filter.get(), subset, clipBounds,
-                                         &outSubset, nullptr);
-    REPORTER_ASSERT(reporter, !result);
+    result = makeWithFilter(sourceImage, filter.get(), subset, clipBounds, &outSubset, nullptr);
+    REPORTER_ASSERT(reporter, !result);  // offset is required
 
     SkIRect bigSubset = SkIRect::MakeXYWH(-10000, -10000, 20000, 20000);
-    result = sourceImage->makeWithFilter(rContext, filter.get(), bigSubset, clipBounds,
-                                         &outSubset, &offset);
+    result = makeWithFilter(sourceImage, filter.get(), bigSubset, clipBounds, &outSubset, &offset);
+    REPORTER_ASSERT(reporter, !result);  // subset needs to be w/in source's bounds
+
+    const SkIRect kEmpty = SkIRect::MakeEmpty();
+    result = makeWithFilter(sourceImage, filter.get(), kEmpty, clipBounds, &outSubset, &offset);
+    REPORTER_ASSERT(reporter, !result);  // subset can't be empty
+
+    result = makeWithFilter(sourceImage, filter.get(), subset, kEmpty, &outSubset, &offset);
+    REPORTER_ASSERT(reporter, !result);  // clipBounds can't be empty
+
+    const SkIRect kLeftField = SkIRect::MakeXYWH(-1000, 0, 100, 100);
+    result = makeWithFilter(sourceImage, filter.get(), subset, kLeftField, &outSubset, &offset);
     REPORTER_ASSERT(reporter, !result);
 
-    SkIRect empty = SkIRect::MakeEmpty();
-    result = sourceImage->makeWithFilter(rContext, filter.get(), empty, clipBounds,
-                                         &outSubset, &offset);
-    REPORTER_ASSERT(reporter, !result);
-
-    result = sourceImage->makeWithFilter(rContext, filter.get(), subset, empty,
-                                         &outSubset, &offset);
-    REPORTER_ASSERT(reporter, !result);
-
-    SkIRect leftField = SkIRect::MakeXYWH(-1000, 0, 100, 100);
-    result = sourceImage->makeWithFilter(rContext, filter.get(), subset, leftField,
-                                         &outSubset, &offset);
-    REPORTER_ASSERT(reporter, !result);
-
-    result = sourceImage->makeWithFilter(rContext, filter.get(), subset, clipBounds,
-                                         &outSubset, &offset);
+    result = makeWithFilter(sourceImage, filter.get(), subset, clipBounds, &outSubset, &offset);
 
     REPORTER_ASSERT(reporter, result);
     REPORTER_ASSERT(reporter, result->bounds().contains(outSubset));
     SkIRect destRect = SkIRect::MakeXYWH(offset.x(), offset.y(),
-                                          outSubset.width(), outSubset.height());
+                                         outSubset.width(), outSubset.height());
     REPORTER_ASSERT(reporter, clipBounds.contains(destRect));
 
     // In GPU-mode, this case creates a special image with a backing size that differs from
@@ -1774,30 +1776,149 @@
         subset.setXYWH(0, 0, 160, 90);
 
         filter = SkImageFilters::Blend(SkBlendMode::kSrcOver, nullptr);
-        result = sourceImage->makeWithFilter(rContext, filter.get(), subset, clipBounds,
-                                             &outSubset, &offset);
+        result = makeWithFilter(sourceImage, filter.get(), subset, clipBounds, &outSubset, &offset);
         REPORTER_ASSERT(reporter, result);
 
-        // In GPU-mode, we want the result image (and all intermediate steps) to have used the same
+        // In Ganesh, we want the result image (and all intermediate steps) to have used the same
         // origin as the original surface.
-        if (rContext) {
-            auto [proxyView, _] = skgpu::ganesh::AsView(rContext, result, GrMipmapped::kNo);
-            REPORTER_ASSERT(reporter, proxyView && proxyView.origin() == kTestSurfaceOrigin);
+        if (result && as_IB(result)->isGaneshBacked()) {
+            SkImage_GaneshBase* base = static_cast<SkImage_GaneshBase*>(result.get());
+            REPORTER_ASSERT(reporter, base->origin() == kTestSurfaceOrigin);
         }
     }
 }
 
 DEF_TEST(ImageFilterMakeWithFilter, reporter) {
-    test_make_with_filter(reporter, nullptr);
+    auto createRasterSurface = [](int width, int height) -> sk_sp<SkSurface> {
+        const SkImageInfo info = SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType);
+        return SkSurfaces::Raster(info);
+    };
+
+    auto raster = [](sk_sp<SkImage> src,
+                     const SkImageFilter* filter,
+                     const SkIRect& subset,
+                     const SkIRect& clipBounds,
+                     SkIRect* outSubset,
+                     SkIPoint* offset) -> sk_sp<SkImage> {
+                         return SkImages::MakeWithFilter(std::move(src),
+                                                         filter,
+                                                         subset,
+                                                         clipBounds,
+                                                         outSubset,
+                                                         offset);
+                     };
+
+    test_make_with_filter(reporter, createRasterSurface, raster);
 }
 
-DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageFilterMakeWithFilter_Gpu,
+// TODO(b/293326072): remove when SkImage::makeWithFilter is removed
+DEF_TEST(ImageFilterMakeWithFilter_LegacyRaster, reporter) {
+    auto createRasterSurface = [](int width, int height) -> sk_sp<SkSurface> {
+        const SkImageInfo info = SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType);
+        return SkSurfaces::Raster(info);
+    };
+
+    auto legacy = [](sk_sp<SkImage> src,
+                     const SkImageFilter* filter,
+                     const SkIRect& subset,
+                     const SkIRect& clipBounds,
+                     SkIRect* outSubset,
+                     SkIPoint* offset) -> sk_sp<SkImage> {
+        return src->makeWithFilter(nullptr, filter, subset, clipBounds, outSubset, offset);
+    };
+
+    test_make_with_filter(reporter, createRasterSurface, legacy);
+}
+
+// TODO(b/293326072): remove when SkImage::makeWithFilter is removed
+DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageFilterMakeWithFilter_LegacyGanesh,
                                        reporter,
                                        ctxInfo,
                                        CtsEnforcement::kNever) {
-    test_make_with_filter(reporter, ctxInfo.directContext());
+    GrRecordingContext* rContext = ctxInfo.directContext();
+
+    auto createGaneshSurface = [rContext](int width, int height) -> sk_sp<SkSurface> {
+        const SkImageInfo info = SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType);
+        return SkSurfaces::RenderTarget(
+                rContext, skgpu::Budgeted::kNo, info, 0, kTestSurfaceOrigin, nullptr);
+    };
+
+    auto legacy = [rContext](sk_sp<SkImage> src,
+                             const SkImageFilter* filter,
+                             const SkIRect& subset,
+                             const SkIRect& clipBounds,
+                             SkIRect* outSubset,
+                             SkIPoint* offset) -> sk_sp<SkImage> {
+        return src->makeWithFilter(rContext, filter, subset, clipBounds, outSubset, offset);
+    };
+
+    test_make_with_filter(reporter, createGaneshSurface, legacy);
 }
 
+DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageFilterMakeWithFilter_Ganesh,
+                                       reporter,
+                                       ctxInfo,
+                                       CtsEnforcement::kNever) {
+    GrRecordingContext* rContext = ctxInfo.directContext();
+
+    auto createGaneshSurface = [rContext](int width, int height) -> sk_sp<SkSurface> {
+        const SkImageInfo info = SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType);
+        return SkSurfaces::RenderTarget(
+                rContext, skgpu::Budgeted::kNo, info, 0, kTestSurfaceOrigin, nullptr);
+    };
+
+    auto ganesh = [rContext](sk_sp<SkImage> src,
+                             const SkImageFilter* filter,
+                             const SkIRect& subset,
+                             const SkIRect& clipBounds,
+                             SkIRect* outSubset,
+                             SkIPoint* offset) -> sk_sp<SkImage> {
+         return SkImages::MakeWithFilter(rContext,
+                                         std::move(src),
+                                         filter,
+                                         subset,
+                                         clipBounds,
+                                         outSubset,
+                                         offset);
+    };
+
+    test_make_with_filter(reporter, createGaneshSurface, ganesh);
+}
+
+#if defined(SK_GRAPHITE)
+
+DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(ImageFilterMakeWithFilter_Graphite,
+                                         reporter,
+                                         context,
+                                         CtsEnforcement::kNextRelease) {
+    std::unique_ptr<skgpu::graphite::Recorder> recorder =
+            context->makeRecorder(ToolUtils::CreateTestingRecorderOptions());
+
+    auto createGraphiteSurface = [r = recorder.get()](int width, int height) -> sk_sp<SkSurface> {
+        const SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
+        return SkSurfaces::RenderTarget(r, info);
+    };
+
+    auto graphite = [r = recorder.get()](sk_sp<SkImage> src,
+                                         const SkImageFilter* filter,
+                                         const SkIRect& subset,
+                                         const SkIRect& clipBounds,
+                                         SkIRect* outSubset,
+                                         SkIPoint* offset) -> sk_sp<SkImage> {
+        return SkImages::MakeWithFilter(r,
+                                        std::move(src),
+                                        filter,
+                                        subset,
+                                        clipBounds,
+                                        outSubset,
+                                        offset);
+    };
+
+    test_make_with_filter(reporter, createGraphiteSurface, graphite);
+}
+
+#endif
+
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageFilterHugeBlur_Gpu,
                                        reporter,
                                        ctxInfo,
diff --git a/tests/RepeatedClippedBlurTest.cpp b/tests/RepeatedClippedBlurTest.cpp
index bafbce2..f13edd2 100644
--- a/tests/RepeatedClippedBlurTest.cpp
+++ b/tests/RepeatedClippedBlurTest.cpp
@@ -116,8 +116,8 @@
 
         SkIRect outSubset;
         SkIPoint offset;
-        sk_sp<SkImage> filteredImg = smImg->makeWithFilter(dContext, blur.get(), subset, clip,
-                                                           &outSubset, &offset);
+        sk_sp<SkImage> filteredImg = SkImages::MakeWithFilter(dContext, smImg, blur.get(), subset,
+                                                              clip, &outSubset, &offset);
 
         SkRect dstRect = SkRect::MakeXYWH(offset.fX, offset.fY,
                                           outSubset.width(), outSubset.height());