Have GrMatrixConvolutionEffect use the thread-safe view cache

No discernable perf improvement for OOP-R/DDL-style rendering of the matrixconvolution* GMs.

Bug: 1108408
Change-Id: Iea016feb2a8a0e5303a64ee8166c3ef089bb2649
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/321137
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/effects/GrMatrixConvolutionEffect.cpp b/src/gpu/effects/GrMatrixConvolutionEffect.cpp
index f9c8a94..0e65ede 100644
--- a/src/gpu/effects/GrMatrixConvolutionEffect.cpp
+++ b/src/gpu/effects/GrMatrixConvolutionEffect.cpp
@@ -13,6 +13,7 @@
 #include "src/gpu/GrRecordingContextPriv.h"
 #include "src/gpu/GrTexture.h"
 #include "src/gpu/GrTextureProxy.h"
+#include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h"
 #include "src/gpu/effects/GrTextureEffect.h"
 #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
@@ -43,13 +44,14 @@
 };
 
 GrMatrixConvolutionEffect::KernelWrapper::MakeResult
-GrMatrixConvolutionEffect::KernelWrapper::Make(GrRecordingContext* context,
+GrMatrixConvolutionEffect::KernelWrapper::Make(GrRecordingContext* rContext,
                                                SkISize size,
                                                const GrCaps& caps,
                                                const SkScalar* values) {
-    if (!context || !values || size.isEmpty()) {
+    if (!rContext || !values || size.isEmpty()) {
         return {};
     }
+
     const int length = size.area();
     // Small kernel -> just fill the array.
     KernelWrapper result(size);
@@ -62,7 +64,7 @@
 
     BiasAndGain& scalableSampler = result.fBiasAndGain;
     bool useA16 =
-        context->defaultBackendFormat(kA16_float_SkColorType, GrRenderable::kNo).isValid();
+        rContext->defaultBackendFormat(kA16_float_SkColorType, GrRenderable::kNo).isValid();
     SkScalar min = values[0];
     if (!useA16) {
         // Determine min and max values to figure out inner gain & bias.
@@ -100,39 +102,43 @@
     }
 
     // Find or create a texture.
-    GrProxyProvider* proxyProvider = context->priv().proxyProvider();
-    GrSurfaceProxyView view;
+    auto threadSafeViewCache = rContext->priv().threadSafeViewCache();
+
     SkColorType colorType = useA16 ? kA16_float_SkColorType : kAlpha_8_SkColorType;
-    sk_sp<GrTextureProxy> cachedKernel;
-    if (kCacheKernelTexture && (cachedKernel = proxyProvider->findOrCreateProxyByUniqueKey(key))) {
-        GrSwizzle swizzle =
-            context->priv().caps()->getReadSwizzle(cachedKernel->backendFormat(),
-                                                   SkColorTypeToGrColorType(colorType));
-        view = {std::move(cachedKernel), kTopLeft_GrSurfaceOrigin, swizzle};
-    } else {
-        SkBitmap bm;
-        auto info = SkImageInfo::Make({length, 1}, colorType, kPremul_SkAlphaType, nullptr);
-        if (!bm.tryAllocPixels(info)) {
-            return {};
-        }
-        for (int i = 0; i < length; i++) {
-            if (useA16) {
-                *bm.getAddr16(i, 0) = SkFloatToHalf(values[i]);
-            } else {
-                *bm.getAddr8(i, 0) =
-                    SkScalarRoundToInt((values[i] - min) / scalableSampler.fGain * 255);
-            }
-        }
-        bm.setImmutable();
-        GrBitmapTextureMaker maker(context, bm, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
-        view = maker.view(GrMipmapped::kNo);
-        if (!view) {
-            return {};
-        }
-        if (kCacheKernelTexture) {
-            proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
+
+    GrSurfaceProxyView view;
+    if (kCacheKernelTexture && (view = threadSafeViewCache->find(key))) {
+        SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
+        auto kernelFP = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
+        return {result, std::move(kernelFP)};
+    }
+
+    SkBitmap bm;
+    auto info = SkImageInfo::Make({length, 1}, colorType, kPremul_SkAlphaType, nullptr);
+    if (!bm.tryAllocPixels(info)) {
+        return {};
+    }
+    for (int i = 0; i < length; i++) {
+        if (useA16) {
+            *bm.getAddr16(i, 0) = SkFloatToHalf(values[i]);
+        } else {
+            *bm.getAddr8(i, 0) =
+                SkScalarRoundToInt((values[i] - min) / scalableSampler.fGain * 255);
         }
     }
+    bm.setImmutable();
+
+    GrBitmapTextureMaker maker(rContext, bm, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
+    view = maker.view(GrMipmapped::kNo);
+    if (!view) {
+        return {};
+    }
+
+    if (kCacheKernelTexture) {
+        view = threadSafeViewCache->add(key, view);
+    }
+
+    SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
     auto kernelFP = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
     return {result, std::move(kernelFP)};
 }