Migrate MakeFromAdoptedTexture to GrDirectContext

More recontexting for SkImage. Chrome flag in CL 2323135.
Flutter migration landed in https://github.com/flutter/engine/pull/19962

Bug: skia:104662
Change-Id: Id725eb130310639457ba90f378ecdb334dd5f3cd
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/306182
Auto-Submit: Adlai Holler <adlai@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/docs/examples/Image_MakeFromAdoptedTexture.cpp b/docs/examples/Image_MakeFromAdoptedTexture.cpp
index 16ae645..55fb508 100644
--- a/docs/examples/Image_MakeFromAdoptedTexture.cpp
+++ b/docs/examples/Image_MakeFromAdoptedTexture.cpp
@@ -4,7 +4,9 @@
 // HASH=b034517e39394b7543f06ec885e36d7d
 REG_FIDDLE(Image_MakeFromAdoptedTexture, 256, 256, false, 5) {
 void draw(SkCanvas* canvas) {
-    if (!canvas->getGrContext()) {
+    GrDirectContext* dContext = GrAsDirectContext(canvas->recordingContext());
+    // Example does not support DDL.
+    if (!dContext) {
         return;
     }
     canvas->scale(.5f, .5f);
@@ -12,7 +14,7 @@
     int x = 0, y = 0;
     for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
         for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
-            sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
+            sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(dContext,
                                                                    backEndTexture, origin,
                                                                    kRGBA_8888_SkColorType, alpha);
             canvas->drawImage(image, x, y);
diff --git a/gm/rectangletexture.cpp b/gm/rectangletexture.cpp
index abdd2c4..949e9b5 100644
--- a/gm/rectangletexture.cpp
+++ b/gm/rectangletexture.cpp
@@ -94,11 +94,11 @@
         SkUNREACHABLE;
     }
 
-    sk_sp<SkImage> createRectangleTextureImg(GrDirectContext* context, GrSurfaceOrigin origin,
+    sk_sp<SkImage> createRectangleTextureImg(GrDirectContext* dContext, GrSurfaceOrigin origin,
                                              const SkBitmap content) {
         SkASSERT(content.colorType() == kRGBA_8888_SkColorType);
         auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
-        auto bet = context->createBackendTexture(content.width(), content.height(), format,
+        auto bet = dContext->createBackendTexture(content.width(), content.height(), format,
                                                  GrMipmapped::kNo, GrRenderable::kNo);
         if (!bet.isValid()) {
             return nullptr;
@@ -116,10 +116,10 @@
             }
             pm = &tempPM;
         }
-        if (!context->updateBackendTexture(bet, pm, 1, nullptr, nullptr)) {
-            context->deleteBackendTexture(bet);
+        if (!dContext->updateBackendTexture(bet, pm, 1, nullptr, nullptr)) {
+            dContext->deleteBackendTexture(bet);
         }
-        return SkImage::MakeFromAdoptedTexture(context, bet, origin, kRGBA_8888_SkColorType);
+        return SkImage::MakeFromAdoptedTexture(dContext, bet, origin, kRGBA_8888_SkColorType);
     }
 
     DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
diff --git a/include/core/SkImage.h b/include/core/SkImage.h
index e7a2edc..13cf37c 100644
--- a/include/core/SkImage.h
+++ b/include/core/SkImage.h
@@ -400,12 +400,21 @@
         @param colorSpace      range of colors; may be nullptr
         @return                created SkImage, or nullptr
     */
+    static sk_sp<SkImage> MakeFromAdoptedTexture(GrDirectContext* context,
+                                                 const GrBackendTexture& backendTexture,
+                                                 GrSurfaceOrigin surfaceOrigin,
+                                                 SkColorType colorType,
+                                                 SkAlphaType alphaType = kPremul_SkAlphaType,
+                                                 sk_sp<SkColorSpace> colorSpace = nullptr);
+
+#ifdef SK_IMAGE_MAKE_FROM_ADOPTED_TEXTURE_LEGACY_API
     static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
                                                  const GrBackendTexture& backendTexture,
                                                  GrSurfaceOrigin surfaceOrigin,
                                                  SkColorType colorType,
                                                  SkAlphaType alphaType = kPremul_SkAlphaType,
                                                  sk_sp<SkColorSpace> colorSpace = nullptr);
+#endif
 
     /** Creates an SkImage by flattening the specified YUVA planes into a single, interleaved RGBA
         image.
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 1870ef9..a17271b 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -566,13 +566,22 @@
     return false;
 }
 
-sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
-                                               const GrBackendTexture& tex, GrSurfaceOrigin origin,
-                                               SkColorType ct, SkAlphaType at,
-                                               sk_sp<SkColorSpace> cs) {
+sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrDirectContext*,
+                                               const GrBackendTexture&, GrSurfaceOrigin,
+                                               SkColorType, SkAlphaType,
+                                               sk_sp<SkColorSpace>) {
     return nullptr;
 }
 
+#ifdef SK_IMAGE_MAKE_FROM_ADOPTED_TEXTURE_LEGACY_API
+sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext*,
+                                               const GrBackendTexture&, GrSurfaceOrigin,
+                                               SkColorType, SkAlphaType,
+                                               sk_sp<SkColorSpace>) {
+    return nullptr;
+}
+#endif
+
 sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopy(GrRecordingContext*,
                                                  SkYUVColorSpace,
                                                  const GrBackendTexture[],
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index 29aa56b..00e725d 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -251,16 +251,16 @@
                                       kBorrow_GrWrapOwnership, std::move(releaseHelper));
 }
 
-sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
+sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrDirectContext* dContext,
                                                const GrBackendTexture& tex, GrSurfaceOrigin origin,
                                                SkColorType ct, SkAlphaType at,
                                                sk_sp<SkColorSpace> cs) {
-    if (!ctx || !ctx->priv().resourceProvider()) {
+    if (!dContext || !dContext->priv().resourceProvider()) {
         // We have a DDL context and we don't support adopted textures for them.
         return nullptr;
     }
 
-    const GrCaps* caps = ctx->priv().caps();
+    const GrCaps* caps = dContext->priv().caps();
 
     GrColorType grColorType = SkColorTypeAndFormatToGrColorType(caps, ct, tex.getBackendFormat());
     if (GrColorType::kUnknown == grColorType) {
@@ -271,10 +271,20 @@
         return nullptr;
     }
 
-    return new_wrapped_texture_common(ctx, tex, grColorType, origin, at, std::move(cs),
+    return new_wrapped_texture_common(dContext, tex, grColorType, origin, at, std::move(cs),
                                       kAdopt_GrWrapOwnership, nullptr);
 }
 
+#ifdef SK_IMAGE_MAKE_FROM_ADOPTED_TEXTURE_LEGACY_API
+sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
+                                               const GrBackendTexture& tex, GrSurfaceOrigin origin,
+                                               SkColorType ct, SkAlphaType at,
+                                               sk_sp<SkColorSpace> cs) {
+    return SkImage::MakeFromAdoptedTexture(GrAsDirectContext(ctx), tex, origin, ct,
+                                           at, std::move(cs));
+}
+#endif
+
 sk_sp<SkImage> SkImage::MakeTextureFromCompressed(GrDirectContext* direct, sk_sp<SkData> data,
                                                   int width, int height, CompressionType type,
                                                   GrMipmapped mipMapped,
diff --git a/tests/DeferredDisplayListTest.cpp b/tests/DeferredDisplayListTest.cpp
index 3e6e801..2375d51 100644
--- a/tests/DeferredDisplayListTest.cpp
+++ b/tests/DeferredDisplayListTest.cpp
@@ -917,13 +917,9 @@
     }
 
     // Wrapped Backend Textures are not supported in DDL
-    sk_sp<SkImage> image =
-            SkImage::MakeFromAdoptedTexture(deferredContext, backendTex, kTopLeft_GrSurfaceOrigin,
-                                            kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
-    REPORTER_ASSERT(reporter, !image);
-
     TextureReleaseChecker releaseChecker;
-    image = SkImage::MakeFromTexture(deferredContext, backendTex, kTopLeft_GrSurfaceOrigin,
+    sk_sp<SkImage> image =
+            SkImage::MakeFromTexture(deferredContext, backendTex, kTopLeft_GrSurfaceOrigin,
                                      kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr,
                                      TextureReleaseChecker::Release, &releaseChecker);
     REPORTER_ASSERT(reporter, !image);