Centralize CreateLazyView helper in GrThreadSafeUniquelyKeyedProxyViewCache

We'll also be needing this helper for HW-generated blur mask caching

Bug: 1108408
Change-Id: I60d91ae8864239f0cf68830d0a5b4266d27545d3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323109
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp b/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp
index d467d2b..898ad8e 100644
--- a/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp
+++ b/src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.cpp
@@ -7,6 +7,10 @@
 
 #include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h"
 
+#include "include/gpu/GrDirectContext.h"
+#include "src/gpu/GrContextPriv.h"
+#include "src/gpu/GrProxyProvider.h"
+#include "src/gpu/GrRenderTargetContext.h"
 #include "src/gpu/GrResourceCache.h"
 
 GrThreadSafeUniquelyKeyedProxyViewCache::GrThreadSafeUniquelyKeyedProxyViewCache()
@@ -222,3 +226,54 @@
         this->recycleEntry(tmp);
     }
 }
+
+std::tuple<GrSurfaceProxyView, sk_sp<GrThreadSafeUniquelyKeyedProxyViewCache::Trampoline>>
+GrThreadSafeUniquelyKeyedProxyViewCache::CreateLazyView(GrDirectContext* dContext,
+                                                        SkISize dimensions,
+                                                        GrColorType origCT,
+                                                        GrSurfaceOrigin origin) {
+    GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
+
+    constexpr int kSampleCnt = 1;
+    auto [newCT, format] = GrRenderTargetContext::GetFallbackColorTypeAndFormat(
+            dContext, origCT, kSampleCnt);
+
+    if (newCT == GrColorType::kUnknown) {
+        return {GrSurfaceProxyView(nullptr), nullptr};
+    }
+
+    sk_sp<Trampoline> trampoline(new Trampoline);
+
+    GrProxyProvider::TextureInfo texInfo{ GrMipMapped::kNo, GrTextureType::k2D };
+
+    sk_sp<GrRenderTargetProxy> proxy = proxyProvider->createLazyRenderTargetProxy(
+            [trampoline](
+                    GrResourceProvider* resourceProvider,
+                    const GrSurfaceProxy::LazySurfaceDesc&) -> GrSurfaceProxy::LazyCallbackResult {
+                if (!resourceProvider || !trampoline->fProxy ||
+                    !trampoline->fProxy->isInstantiated()) {
+                    return GrSurfaceProxy::LazyCallbackResult(nullptr, true);
+                }
+
+                SkASSERT(!trampoline->fProxy->peekTexture()->getUniqueKey().isValid());
+                return GrSurfaceProxy::LazyCallbackResult(
+                        sk_ref_sp(trampoline->fProxy->peekTexture()));
+            },
+            format,
+            dimensions,
+            kSampleCnt,
+            GrInternalSurfaceFlags::kNone,
+            &texInfo,
+            GrMipmapStatus::kNotAllocated,
+            SkBackingFit::kExact,
+            SkBudgeted::kYes,
+            GrProtected::kNo,
+            /* wrapsVkSecondaryCB */ false,
+            GrSurfaceProxy::UseAllocator::kYes);
+
+    // TODO: It seems like this 'newCT' usage should be 'origCT' but this is
+    // what GrRenderTargetContext::MakeWithFallback does
+    GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, newCT);
+
+    return {{std::move(proxy), origin, swizzle}, std::move(trampoline)};
+}