Make lazy proxies have 2 modes for proxy/surface key management.

kSynced: Proxy and GrSurface key kept in sync.

kUnsynced: Proxy and GrSurface keys are unrelated.

This will allow cross-context image generators' lazy instantiation
callbacks to use unique keys to find any pre-existing backing GrTexture
rather than keeping an unref'ed bare pointer to the GrTexture.

Bug: skia:8927

Change-Id: Id15e2a64e8d2e56c4ce70b9399eb1d8bcea6ac9a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/204723
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/image/SkImage_GpuBase.cpp b/src/image/SkImage_GpuBase.cpp
index 12c4b54..3d53e8d 100644
--- a/src/image/SkImage_GpuBase.cpp
+++ b/src/image/SkImage_GpuBase.cpp
@@ -453,11 +453,16 @@
             }
         }
 
-        sk_sp<GrSurface> operator()(GrResourceProvider* resourceProvider) {
+        GrSurfaceProxy::LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) {
+            // We use the unique key in a way that is unrelated to the SkImage-based key that the
+            // proxy may receive, hence kUnsynced.
+            static constexpr auto kKeySyncMode =
+                    GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
+
             // Our proxy is getting instantiated for the second+ time. We are only allowed to call
             // Fulfill once. So return our cached result.
             if (fTexture) {
-                return sk_ref_sp(fTexture);
+                return {sk_ref_sp(fTexture), kKeySyncMode};
             }
             SkASSERT(fDoneCallback);
             PromiseImageTextureContext textureContext = fDoneCallback->context();
@@ -466,13 +471,13 @@
             // the return from fulfill was invalid or we fail for some other reason.
             auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
             if (!promiseTexture) {
-                return sk_sp<GrTexture>();
+                return {};
             }
 
             auto backendTexture = promiseTexture->backendTexture();
             backendTexture.fConfig = fConfig;
             if (!backendTexture.isValid()) {
-                return sk_sp<GrTexture>();
+                return {};
             }
 
             sk_sp<GrTexture> tex;
@@ -494,7 +499,7 @@
                              kRead_GrIOType))) {
                     tex->resourcePriv().setUniqueKey(key);
                 } else {
-                    return sk_sp<GrTexture>();
+                    return {};
                 }
             }
             auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
@@ -511,7 +516,7 @@
             GrContext* context = fTexture->getContext();
             context->priv().getResourceCache()->insertDelayedResourceUnref(fTexture);
             fTextureContextID = context->priv().contextID();
-            return std::move(tex);
+            return {std::move(tex), kKeySyncMode};
         }
 
     private: