Be more consistent about calling release procs in SkImage factories (take 2)

I will follow this up with a Chrome-side CL to fix the call sites of the following factories:
   MakeFromCompressedTexture
   MakeFromTexture
   MakeFromYUVATexturesCopyWithExternalBackend
   MakeFromYUVTexturesCopyWithExternalBackend
   MakeFromNV12TexturesCopyWithExternalBackend

Here is the Chrome-side CL: https://chromium-review.googlesource.com/c/chromium/src/+/2264598/ (Skia's SkImage::Make* factories now guarantee cleanup)

Here is the Chrome-side CL that adds the guard flag:
https://chromium-review.googlesource.com/c/chromium/src/+/2273067/ (Add flag in order to roll a Skia CL into Chrome)

TBR=bsalomon@google.com
Bug: 1097484
Change-Id: Ic2fcdc116f0f866b33d752b6d5abc784c7f65be6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/299663
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrProxyProvider.cpp b/src/gpu/GrProxyProvider.cpp
index e5bd752..e2bfd76 100644
--- a/src/gpu/GrProxyProvider.cpp
+++ b/src/gpu/GrProxyProvider.cpp
@@ -523,11 +523,11 @@
                                                     this->isDDLProvider()));
 }
 
-sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(const GrBackendTexture& beTex,
-                                                                    GrWrapOwnership ownership,
-                                                                    GrWrapCacheable cacheable,
-                                                                    ReleaseProc releaseProc,
-                                                                    ReleaseContext releaseCtx) {
+sk_sp<GrTextureProxy> GrProxyProvider::wrapCompressedBackendTexture(
+        const GrBackendTexture& beTex,
+        GrWrapOwnership ownership,
+        GrWrapCacheable cacheable,
+        sk_sp<GrRefCntedCallback> releaseHelper) {
     if (this->isAbandoned()) {
         return nullptr;
     }
@@ -546,8 +546,8 @@
         return nullptr;
     }
 
-    if (releaseProc) {
-        tex->setRelease(releaseProc, releaseCtx);
+    if (releaseHelper) {
+        tex->setRelease(std::move(releaseHelper));
     }
 
     SkASSERT(!tex->asRenderTarget());  // Strictly a GrTexture
@@ -563,8 +563,7 @@
         int sampleCnt,
         GrWrapOwnership ownership,
         GrWrapCacheable cacheable,
-        ReleaseProc releaseProc,
-        ReleaseContext releaseCtx) {
+        sk_sp<GrRefCntedCallback> releaseHelper) {
     if (this->isAbandoned()) {
         return nullptr;
     }
@@ -588,8 +587,8 @@
         return nullptr;
     }
 
-    if (releaseProc) {
-        tex->setRelease(releaseProc, releaseCtx);
+    if (releaseHelper) {
+        tex->setRelease(std::move(releaseHelper));
     }
 
     SkASSERT(tex->asRenderTarget());  // A GrTextureRenderTarget
@@ -602,8 +601,7 @@
 
 sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
         const GrBackendRenderTarget& backendRT,
-        ReleaseProc releaseProc,
-        ReleaseContext releaseCtx) {
+        sk_sp<GrRefCntedCallback> releaseHelper) {
     if (this->isAbandoned()) {
         return nullptr;
     }
@@ -621,8 +619,8 @@
         return nullptr;
     }
 
-    if (releaseProc) {
-        rt->setRelease(releaseProc, releaseCtx);
+    if (releaseHelper) {
+        rt->setRelease(std::move(releaseHelper));
     }
 
     SkASSERT(!rt->asTexture());  // A GrRenderTarget that's not textureable
diff --git a/src/gpu/GrProxyProvider.h b/src/gpu/GrProxyProvider.h
index 63f0335..e1f681f 100644
--- a/src/gpu/GrProxyProvider.h
+++ b/src/gpu/GrProxyProvider.h
@@ -117,9 +117,10 @@
                                              GrIOType,
                                              sk_sp<GrRefCntedCallback> = nullptr);
 
-    sk_sp<GrTextureProxy> wrapCompressedBackendTexture(const GrBackendTexture&, GrWrapOwnership,
-                                                       GrWrapCacheable, ReleaseProc = nullptr,
-                                                       ReleaseContext = nullptr);
+    sk_sp<GrTextureProxy> wrapCompressedBackendTexture(const GrBackendTexture&,
+                                                       GrWrapOwnership,
+                                                       GrWrapCacheable,
+                                                       sk_sp<GrRefCntedCallback> releaseHelper);
 
     /*
      * Create a texture proxy that wraps a backend texture and is both texture-able and renderable
@@ -128,15 +129,13 @@
                                                        int sampleCnt,
                                                        GrWrapOwnership,
                                                        GrWrapCacheable,
-                                                       ReleaseProc = nullptr,
-                                                       ReleaseContext = nullptr);
+                                                       sk_sp<GrRefCntedCallback> releaseHelper);
 
     /*
      * Create a render target proxy that wraps a backend render target
      */
     sk_sp<GrSurfaceProxy> wrapBackendRenderTarget(const GrBackendRenderTarget&,
-                                                  ReleaseProc = nullptr,
-                                                  ReleaseContext = nullptr);
+                                                  sk_sp<GrRefCntedCallback> releaseHelper);
 
     /*
      * Create a render target proxy that wraps a backend texture
diff --git a/src/gpu/GrRenderTargetContext.cpp b/src/gpu/GrRenderTargetContext.cpp
index 6201245..e5c7bac 100644
--- a/src/gpu/GrRenderTargetContext.cpp
+++ b/src/gpu/GrRenderTargetContext.cpp
@@ -289,12 +289,11 @@
         int sampleCnt,
         GrSurfaceOrigin origin,
         const SkSurfaceProps* surfaceProps,
-        ReleaseProc releaseProc,
-        ReleaseContext releaseCtx) {
+        sk_sp<GrRefCntedCallback> releaseHelper) {
     SkASSERT(sampleCnt > 0);
     sk_sp<GrTextureProxy> proxy(context->priv().proxyProvider()->wrapRenderableBackendTexture(
-            tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseProc,
-            releaseCtx));
+            tex, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
+            std::move(releaseHelper)));
     if (!proxy) {
         return nullptr;
     }
@@ -331,8 +330,13 @@
         const SkSurfaceProps* surfaceProps,
         ReleaseProc releaseProc,
         ReleaseContext releaseCtx) {
+    sk_sp<GrRefCntedCallback> releaseHelper;
+    if (releaseProc) {
+        releaseHelper.reset(new GrRefCntedCallback(releaseProc, releaseCtx));
+    }
+
     sk_sp<GrSurfaceProxy> proxy(
-            context->priv().proxyProvider()->wrapBackendRenderTarget(rt, releaseProc, releaseCtx));
+            context->priv().proxyProvider()->wrapBackendRenderTarget(rt, std::move(releaseHelper)));
     if (!proxy) {
         return nullptr;
     }
diff --git a/src/gpu/GrRenderTargetContext.h b/src/gpu/GrRenderTargetContext.h
index 9d77829..4aa6cde 100644
--- a/src/gpu/GrRenderTargetContext.h
+++ b/src/gpu/GrRenderTargetContext.h
@@ -118,8 +118,8 @@
     // Creates a GrRenderTargetContext that wraps the passed in GrBackendTexture.
     static std::unique_ptr<GrRenderTargetContext> MakeFromBackendTexture(
             GrRecordingContext*, GrColorType, sk_sp<SkColorSpace>, const GrBackendTexture&,
-            int sampleCnt, GrSurfaceOrigin, const SkSurfaceProps*, ReleaseProc releaseProc,
-            ReleaseContext releaseCtx);
+            int sampleCnt, GrSurfaceOrigin, const SkSurfaceProps*,
+            sk_sp<GrRefCntedCallback> releaseHelper);
 
     static std::unique_ptr<GrRenderTargetContext> MakeFromBackendTextureAsRenderTarget(
             GrRecordingContext*, GrColorType, sk_sp<SkColorSpace>, const GrBackendTexture&,
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index 1ce084f..2fac0b1 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -197,6 +197,11 @@
                                                   sk_sp<SkColorSpace> cs,
                                                   TextureReleaseProc releaseP,
                                                   ReleaseContext releaseC) {
+    sk_sp<GrRefCntedCallback> releaseHelper;
+    if (releaseP) {
+        releaseHelper.reset(new GrRefCntedCallback(releaseP, releaseC));
+    }
+
     if (!ctx) {
         return nullptr;
     }
@@ -209,7 +214,7 @@
 
     GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
     sk_sp<GrTextureProxy> proxy = proxyProvider->wrapCompressedBackendTexture(
-            tex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, releaseP, releaseC);
+            tex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, std::move(releaseHelper));
     if (!proxy) {
         return nullptr;
     }
@@ -226,6 +231,13 @@
                                         const GrBackendTexture& tex, GrSurfaceOrigin origin,
                                         SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
                                         TextureReleaseProc releaseP, ReleaseContext releaseC) {
+#ifndef SK_LEGACY_MAKEFROMTEXTURE_BEHAVIOR
+    sk_sp<GrRefCntedCallback> releaseHelper;
+    if (releaseP) {
+        releaseHelper.reset(new GrRefCntedCallback(releaseP, releaseC));
+    }
+#endif
+
     if (!ctx) {
         return nullptr;
     }
@@ -241,10 +253,12 @@
         return nullptr;
     }
 
+#ifdef SK_LEGACY_MAKEFROMTEXTURE_BEHAVIOR
     sk_sp<GrRefCntedCallback> releaseHelper;
     if (releaseP) {
         releaseHelper.reset(new GrRefCntedCallback(releaseP, releaseC));
     }
+#endif
 
     return new_wrapped_texture_common(ctx, tex, grColorType, origin, at, std::move(cs),
                                       kBorrow_GrWrapOwnership, std::move(releaseHelper));
@@ -368,6 +382,11 @@
         ReleaseContext releaseContext) {
     const GrCaps* caps = ctx->priv().caps();
 
+    sk_sp<GrRefCntedCallback> releaseHelper;
+    if (textureReleaseProc) {
+        releaseHelper.reset(new GrRefCntedCallback(textureReleaseProc, releaseContext));
+    }
+
     GrColorType grColorType = SkColorTypeAndFormatToGrColorType(caps, kRGBA_8888_SkColorType,
                                                                 backendTexture.getBackendFormat());
     if (GrColorType::kUnknown == grColorType) {
@@ -384,7 +403,7 @@
     // in order to draw to it for the yuv->rgb conversion.
     auto renderTargetContext = GrRenderTargetContext::MakeFromBackendTexture(
             ctx, grColorType, std::move(imageColorSpace), backendTexture, 1, imageOrigin,
-            nullptr, textureReleaseProc, releaseContext);
+            nullptr, std::move(releaseHelper));
     if (!renderTargetContext) {
         return nullptr;
     }
diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp
index bbadfe8..f445f03 100644
--- a/src/image/SkSurface_Gpu.cpp
+++ b/src/image/SkSurface_Gpu.cpp
@@ -422,11 +422,10 @@
                                                    const GrBackendTexture& backendTexture,
                                                    TextureReleaseProc textureReleaseProc,
                                                    ReleaseContext releaseContext) {
-    SkScopeExit callProc([&] {
-        if (textureReleaseProc) {
-            textureReleaseProc(releaseContext);
-        }
-    });
+    sk_sp<GrRefCntedCallback> releaseHelper;
+    if (textureReleaseProc) {
+        releaseHelper.reset(new GrRefCntedCallback(textureReleaseProc, releaseContext));
+    }
 
     if (!context || !c.isValid()) {
         return nullptr;
@@ -454,11 +453,10 @@
 
     auto rtc = GrRenderTargetContext::MakeFromBackendTexture(
             context, grCT, c.refColorSpace(), backendTexture, c.sampleCount(), c.origin(),
-            &c.surfaceProps(), textureReleaseProc, releaseContext);
+            &c.surfaceProps(), std::move(releaseHelper));
     if (!rtc) {
         return nullptr;
     }
-    callProc.clear();
 
     auto device = SkGpuDevice::Make(context, std::move(rtc), SkGpuDevice::kUninit_InitContents);
     if (!device) {
@@ -519,11 +517,10 @@
                                                    const SkSurfaceProps* props,
                                                    SkSurface::TextureReleaseProc textureReleaseProc,
                                                    SkSurface::ReleaseContext releaseContext) {
-    SkScopeExit callProc([&] {
-        if (textureReleaseProc) {
-            textureReleaseProc(releaseContext);
-        }
-    });
+    sk_sp<GrRefCntedCallback> releaseHelper;
+    if (textureReleaseProc) {
+        releaseHelper.reset(new GrRefCntedCallback(textureReleaseProc, releaseContext));
+    }
 
     if (!context) {
         return nullptr;
@@ -542,11 +539,10 @@
 
     auto rtc = GrRenderTargetContext::MakeFromBackendTexture(
             context, grColorType, std::move(colorSpace), tex, sampleCnt, origin, props,
-            textureReleaseProc, releaseContext);
+            std::move(releaseHelper));
     if (!rtc) {
         return nullptr;
     }
-    callProc.clear();
 
     auto device = SkGpuDevice::Make(context, std::move(rtc), SkGpuDevice::kUninit_InitContents);
     if (!device) {
@@ -560,11 +556,10 @@
                                             ContentChangeMode mode,
                                             TextureReleaseProc releaseProc,
                                             ReleaseContext releaseContext) {
-    SkScopeExit callProc([&] {
-        if (releaseProc) {
-            releaseProc(releaseContext);
-        }
-    });
+    sk_sp<GrRefCntedCallback> releaseHelper;
+    if (releaseProc) {
+        releaseHelper.reset(new GrRefCntedCallback(releaseProc, releaseContext));
+    }
 
     auto context = this->fDevice->context();
     if (context->abandoned()) {
@@ -604,11 +599,10 @@
     }
     auto rtc = GrRenderTargetContext::MakeFromBackendTexture(
             context, oldRTC->colorInfo().colorType(), std::move(colorSpace), backendTexture,
-            sampleCnt, origin, &this->props(), releaseProc, releaseContext);
+            sampleCnt, origin, &this->props(), std::move(releaseHelper));
     if (!rtc) {
         return false;
     }
-    callProc.clear();
     fDevice->replaceRenderTargetContext(std::move(rtc), mode);
     return true;
 }
diff --git a/tests/EGLImageTest.cpp b/tests/EGLImageTest.cpp
index 54c9e8b..cd19c92 100644
--- a/tests/EGLImageTest.cpp
+++ b/tests/EGLImageTest.cpp
@@ -193,7 +193,7 @@
     // Should not be able to wrap as a RT
     {
         auto temp = GrRenderTargetContext::MakeFromBackendTexture(
-                context0, colorType, nullptr, backendTex, 1, origin, nullptr, nullptr, nullptr);
+                context0, colorType, nullptr, backendTex, 1, origin, nullptr, nullptr);
         if (temp) {
             ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
         }
diff --git a/tests/GrTestingBackendTextureUploadTest.cpp b/tests/GrTestingBackendTextureUploadTest.cpp
index a4ba4c3..9b8f250 100644
--- a/tests/GrTestingBackendTextureUploadTest.cpp
+++ b/tests/GrTestingBackendTextureUploadTest.cpp
@@ -65,7 +65,7 @@
     sk_sp<GrTextureProxy> wrappedProxy;
     if (GrRenderable::kYes == renderable) {
         wrappedProxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
-                backendTex, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo);
+                backendTex, 1, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, nullptr);
     } else {
         wrappedProxy = context->priv().proxyProvider()->wrapBackendTexture(
                 backendTex, kAdopt_GrWrapOwnership, GrWrapCacheable::kNo, GrIOType::kRW_GrIOType);
diff --git a/tests/MtlCopySurfaceTest.mm b/tests/MtlCopySurfaceTest.mm
index 9b1e1ee..1bbfaea 100644
--- a/tests/MtlCopySurfaceTest.mm
+++ b/tests/MtlCopySurfaceTest.mm
@@ -42,7 +42,7 @@
     GrBackendRenderTarget backendRT(kWidth, kHeight, 1, fbInfo);
 
     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
-    sk_sp<GrSurfaceProxy> srcProxy = proxyProvider->wrapBackendRenderTarget(backendRT);
+    sk_sp<GrSurfaceProxy> srcProxy = proxyProvider->wrapBackendRenderTarget(backendRT, nullptr);
 
     auto dstProxy = GrSurfaceProxy::Copy(context,
                                          srcProxy.get(),
diff --git a/tests/ProxyConversionTest.cpp b/tests/ProxyConversionTest.cpp
index e5bdfda..5a06c72 100644
--- a/tests/ProxyConversionTest.cpp
+++ b/tests/ProxyConversionTest.cpp
@@ -26,7 +26,7 @@
                                              GrColorType colorType) {
     auto backendRT =
             gpu->createTestingOnlyBackendRenderTarget(size.width(), size.height(), colorType);
-    return provider->wrapBackendRenderTarget(backendRT, nullptr, nullptr);
+    return provider->wrapBackendRenderTarget(backendRT, nullptr);
 }
 
 void clean_up_wrapped_rt(GrGpu* gpu, sk_sp<GrSurfaceProxy> proxy) {
diff --git a/tests/ProxyTest.cpp b/tests/ProxyTest.cpp
index 778c4c6..d9c58cf 100644
--- a/tests/ProxyTest.cpp
+++ b/tests/ProxyTest.cpp
@@ -225,7 +225,7 @@
             GrBackendRenderTarget backendRT = gpu->createTestingOnlyBackendRenderTarget(
                     kWidthHeight, kWidthHeight, grColorType);
             sk_sp<GrSurfaceProxy> sProxy(
-                    proxyProvider->wrapBackendRenderTarget(backendRT, nullptr, nullptr));
+                    proxyProvider->wrapBackendRenderTarget(backendRT, nullptr));
             check_surface(reporter, sProxy.get(), kWidthHeight, kWidthHeight, SkBudgeted::kNo);
             static constexpr int kExpectedNumSamples = 1;
             check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
@@ -253,7 +253,7 @@
                 GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples,
                                                 kStencilBits, fboInfo);
                 sk_sp<GrSurfaceProxy> sProxy(
-                        proxyProvider->wrapBackendRenderTarget(backendRT, nullptr, nullptr));
+                        proxyProvider->wrapBackendRenderTarget(backendRT, nullptr));
                 check_surface(reporter, sProxy.get(), kWidthHeight, kWidthHeight, SkBudgeted::kNo);
                 check_rendertarget(reporter, caps, resourceProvider, sProxy->asRenderTargetProxy(),
                                    supportedNumSamples, SkBackingFit::kExact, 0);
@@ -290,7 +290,7 @@
 
                 sk_sp<GrSurfaceProxy> sProxy = proxyProvider->wrapRenderableBackendTexture(
                         backendTex, supportedNumSamples, kBorrow_GrWrapOwnership,
-                        GrWrapCacheable::kNo, nullptr, nullptr);
+                        GrWrapCacheable::kNo, nullptr);
                 if (!sProxy) {
                     context->deleteBackendTexture(backendTex);
                     continue;  // This can fail on Mesa