Update GrClipStackClip and GrDeviceSpaceTextureDecalFP to use views.

Bug: skia:9556
Change-Id: I61346e31b67ca1dffc5132e0af47aff4fdfc6e22
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/269907
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/gm/windowrectangles.cpp b/gm/windowrectangles.cpp
index 5720efe..b14d137 100644
--- a/gm/windowrectangles.cpp
+++ b/gm/windowrectangles.cpp
@@ -174,16 +174,16 @@
  */
 class AlphaOnlyClip final : public MaskOnlyClipBase {
 public:
-    AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
+    AlphaOnlyClip(GrSurfaceProxyView mask, int x, int y) : fMask(std::move(mask)), fX(x), fY(y) {}
 
 private:
     bool apply(GrRecordingContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
                SkRect* bounds) const override {
         out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
-                fMask, SkIRect::MakeSize(fMask->dimensions()), {fX, fY}));
+                fMask, SkIRect::MakeSize(fMask.proxy()->dimensions()), {fX, fY}));
         return true;
     }
-    sk_sp<GrTextureProxy> fMask;
+    GrSurfaceProxyView fMask;
     int fX;
     int fY;
 };
@@ -250,7 +250,7 @@
     // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
     // inside window rectangles or outside the scissor should still have the initial checkerboard
     // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
-    AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
+    AlphaOnlyClip clip(maskRTC->readSurfaceView(), x, y);
     rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
                   SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
 }
diff --git a/src/gpu/GrClipStackClip.cpp b/src/gpu/GrClipStackClip.cpp
index 250a81a..283d3a8 100644
--- a/src/gpu/GrClipStackClip.cpp
+++ b/src/gpu/GrClipStackClip.cpp
@@ -77,8 +77,9 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 // set up the draw state to enable the aa clipping mask.
-static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(sk_sp<GrTextureProxy> mask,
+static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(GrSurfaceProxyView mask,
                                                                const SkIRect& devBound) {
+    SkASSERT(mask.asTextureProxy());
     SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
     return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(mask), domainTexels,
                                                             {devBound.fLeft, devBound.fTop});
@@ -270,7 +271,7 @@
     if ((renderTargetContext->numSamples() <= 1 && reducedClip.maskRequiresAA()) ||
         context->priv().caps()->avoidStencilBuffers() ||
         renderTargetContext->wrapsVkSecondaryCB()) {
-        sk_sp<GrTextureProxy> result;
+        GrSurfaceProxyView result;
         if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
             // The clip geometry is complex enough that it will be more efficient to create it
             // entirely in software
@@ -341,38 +342,39 @@
     SkDEBUGFAIL("Gen ID was not found in stack.");
 }
 
-sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
-                                                           const GrReducedClip& reducedClip) const {
+GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
+                                                        const GrReducedClip& reducedClip) const {
     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
     GrUniqueKey key;
     create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(),
                          reducedClip.numAnalyticFPs(), &key);
 
-    sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
-            key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin));
-    if (proxy) {
-        return proxy;
+    if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(
+                key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin)) {
+        GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
+                                                                   GrColorType::kAlpha_8);
+        return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
     }
 
     auto rtc = GrRenderTargetContext::MakeWithFallback(
             context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kApprox,
-            {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, proxy->isProtected(),
+            {reducedClip.width(), reducedClip.height()}, 1, GrMipMapped::kNo, GrProtected::kNo,
             kTopLeft_GrSurfaceOrigin);
     if (!rtc) {
-        return nullptr;
+        return {};
     }
 
     if (!reducedClip.drawAlphaClipMask(rtc.get())) {
-        return nullptr;
+        return {};
     }
 
-    sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
-    if (!result) {
-        return nullptr;
+    GrSurfaceProxyView result = rtc->readSurfaceView();
+    if (!result || !result.asTextureProxy()) {
+        return {};
     }
 
-    SkASSERT(result->origin() == kTopLeft_GrSurfaceOrigin);
-    proxyProvider->assignUniqueKeyToProxy(key, result.get());
+    SkASSERT(result.origin() == kTopLeft_GrSurfaceOrigin);
+    proxyProvider->assignUniqueKeyToProxy(key, result.asTextureProxy());
     add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
 
     return result;
@@ -452,7 +454,7 @@
     }
 }
 
-sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
+GrSurfaceProxyView GrClipStackClip::createSoftwareClipMask(
         GrRecordingContext* context, const GrReducedClip& reducedClip,
         GrRenderTargetContext* renderTargetContext) const {
     GrUniqueKey key;
@@ -462,10 +464,11 @@
     GrProxyProvider* proxyProvider = context->priv().proxyProvider();
     const GrCaps* caps = context->priv().caps();
 
-    sk_sp<GrTextureProxy> proxy(proxyProvider->findOrCreateProxyByUniqueKey(
-            key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin));
-    if (proxy) {
-        return proxy;
+    if (sk_sp<GrTextureProxy> proxy = proxyProvider->findOrCreateProxyByUniqueKey(
+                key, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin)) {
+        GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(proxy->backendFormat(),
+                                                                   GrColorType::kAlpha_8);
+        return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
     }
 
     // The mask texture may be larger than necessary. We round out the clip bounds and pin the top
@@ -477,6 +480,7 @@
         taskGroup = direct->priv().getTaskGroup();
     }
 
+    GrSurfaceProxyView view;
     if (taskGroup && renderTargetContext) {
         // Create our texture proxy
         GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
@@ -486,16 +490,16 @@
 
         // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt
         // to ops), so it can't have any pending IO.
-        proxy = proxyProvider->createProxy(format,
-                                           maskSpaceIBounds.size(),
-                                           swizzle,
-                                           GrRenderable::kNo,
-                                           1,
-                                           kTopLeft_GrSurfaceOrigin,
-                                           GrMipMapped::kNo,
-                                           SkBackingFit::kApprox,
-                                           SkBudgeted::kYes,
-                                           GrProtected::kNo);
+        auto proxy = proxyProvider->createProxy(format,
+                                                maskSpaceIBounds.size(),
+                                                swizzle,
+                                                GrRenderable::kNo,
+                                                1,
+                                                kTopLeft_GrSurfaceOrigin,
+                                                GrMipMapped::kNo,
+                                                SkBackingFit::kApprox,
+                                                SkBudgeted::kYes,
+                                                GrProtected::kNo);
 
         auto uploader = std::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip);
         GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get();
@@ -514,20 +518,23 @@
 
         taskGroup->add(std::move(drawAndUploadMask));
         proxy->texPriv().setDeferredUploader(std::move(uploader));
+
+        view = {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
     } else {
         GrSWMaskHelper helper;
         if (!helper.init(maskSpaceIBounds)) {
-            return nullptr;
+            return {};
         }
 
         draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(),
                                           reducedClip.initialState());
 
-        proxy = helper.toTextureView(context, SkBackingFit::kApprox).asTextureProxyRef();
+        view = helper.toTextureView(context, SkBackingFit::kApprox);
     }
 
-    SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
-    proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
+    SkASSERT(view);
+    SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin);
+    proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
     add_invalidate_on_pop_message(context, *fStack, reducedClip.maskGenID(), key);
-    return proxy;
+    return view;
 }
diff --git a/src/gpu/GrClipStackClip.h b/src/gpu/GrClipStackClip.h
index 7d16d5e..40ca7d6 100644
--- a/src/gpu/GrClipStackClip.h
+++ b/src/gpu/GrClipStackClip.h
@@ -51,11 +51,11 @@
 
     // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
     // rect specified by clipSpaceIBounds.
-    sk_sp<GrTextureProxy> createAlphaClipMask(GrRecordingContext*, const GrReducedClip&) const;
+    GrSurfaceProxyView createAlphaClipMask(GrRecordingContext*, const GrReducedClip&) const;
 
     // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
-    sk_sp<GrTextureProxy> createSoftwareClipMask(GrRecordingContext*, const GrReducedClip&,
-                                                 GrRenderTargetContext*) const;
+    GrSurfaceProxyView createSoftwareClipMask(GrRecordingContext*, const GrReducedClip&,
+                                              GrRenderTargetContext*) const;
 
     static bool UseSWOnlyPath(GrRecordingContext*,
                               bool hasUserStencilSettings,
diff --git a/src/gpu/effects/GrTextureDomain.cpp b/src/gpu/effects/GrTextureDomain.cpp
index 4713526..96472e0 100644
--- a/src/gpu/effects/GrTextureDomain.cpp
+++ b/src/gpu/effects/GrTextureDomain.cpp
@@ -318,19 +318,19 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 std::unique_ptr<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make(
-        sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) {
+        GrSurfaceProxyView view, const SkIRect& subset, const SkIPoint& deviceSpaceOffset) {
     return std::unique_ptr<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor(
-            std::move(proxy), subset, deviceSpaceOffset));
+            std::move(view), subset, deviceSpaceOffset));
 }
 
 GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
-        sk_sp<GrSurfaceProxy> proxy, const SkIRect& subset, const SkIPoint& deviceSpaceOffset)
+        GrSurfaceProxyView view, const SkIRect& subset, const SkIPoint& deviceSpaceOffset)
         : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
                     kCompatibleWithCoverageAsAlpha_OptimizationFlag)
-        , fTextureSampler(proxy, GrSamplerState::Filter::kNearest)
-        , fTextureDomain(proxy.get(),
+        , fTextureDomain(view.proxy(),
                          GrTextureDomain::MakeTexelDomain(subset, GrTextureDomain::kDecal_Mode),
-                         GrTextureDomain::kDecal_Mode, GrTextureDomain::kDecal_Mode) {
+                         GrTextureDomain::kDecal_Mode, GrTextureDomain::kDecal_Mode)
+        , fTextureSampler(std::move(view), GrSamplerState::Filter::kNearest) {
     this->setTextureSamplerCnt(1);
     fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft;
     fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop;
@@ -340,8 +340,8 @@
         const GrDeviceSpaceTextureDecalFragmentProcessor& that)
         : INHERITED(kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
                     kCompatibleWithCoverageAsAlpha_OptimizationFlag)
-        , fTextureSampler(that.fTextureSampler)
         , fTextureDomain(that.fTextureDomain)
+        , fTextureSampler(that.fTextureSampler)
         , fDeviceSpaceOffset(that.fDeviceSpaceOffset) {
     this->setTextureSamplerCnt(1);
 }
@@ -430,6 +430,11 @@
     SkIPoint pt;
     pt.fX = d->fRandom->nextULessThan(2048);
     pt.fY = d->fRandom->nextULessThan(2048);
-    return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(proxy), subset, pt);
+
+    GrSurfaceOrigin origin = proxy->origin();
+    GrSwizzle swizzle = proxy->textureSwizzle();
+    GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
+
+    return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(view), subset, pt);
 }
 #endif
diff --git a/src/gpu/effects/GrTextureDomain.h b/src/gpu/effects/GrTextureDomain.h
index 70e6766..c9a563a 100644
--- a/src/gpu/effects/GrTextureDomain.h
+++ b/src/gpu/effects/GrTextureDomain.h
@@ -233,7 +233,7 @@
 
 class GrDeviceSpaceTextureDecalFragmentProcessor : public GrFragmentProcessor {
 public:
-    static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrSurfaceProxy>,
+    static std::unique_ptr<GrFragmentProcessor> Make(GrSurfaceProxyView,
                                                      const SkIRect& subset,
                                                      const SkIPoint& deviceSpaceOffset);
 
@@ -254,12 +254,11 @@
     std::unique_ptr<GrFragmentProcessor> clone() const override;
 
 private:
-    TextureSampler fTextureSampler;
     GrTextureDomain fTextureDomain;
+    TextureSampler fTextureSampler;
     SkIPoint fDeviceSpaceOffset;
 
-    GrDeviceSpaceTextureDecalFragmentProcessor(sk_sp<GrSurfaceProxy>,
-                                               const SkIRect&, const SkIPoint&);
+    GrDeviceSpaceTextureDecalFragmentProcessor(GrSurfaceProxyView, const SkIRect&, const SkIPoint&);
     GrDeviceSpaceTextureDecalFragmentProcessor(const GrDeviceSpaceTextureDecalFragmentProcessor&);
 
     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
diff --git a/tests/ClipStackTest.cpp b/tests/ClipStackTest.cpp
index 1f0887a..168dc0a 100644
--- a/tests/ClipStackTest.cpp
+++ b/tests/ClipStackTest.cpp
@@ -1521,7 +1521,7 @@
 
 sk_sp<GrTextureProxy> GrClipStackClip::testingOnly_createClipMask(GrContext* context) const {
     const GrReducedClip reducedClip(*fStack, SkRect::MakeWH(512, 512), 0);
-    return this->createSoftwareClipMask(context, reducedClip, nullptr);
+    return this->createSoftwareClipMask(context, reducedClip, nullptr).asTextureProxyRef();
 }
 
 // Verify that clip masks are freed up when the clip state that generated them goes away.