Change refPinnedTextureProxy to return a view instead.

Bug: skia:9556
Change-Id: Iad3fbadb893d029fc39f0b682c9f8fc223c010a5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/268843
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/src/gpu/GrSurfaceProxyView.h b/src/gpu/GrSurfaceProxyView.h
index ed438a3..5e891a6 100644
--- a/src/gpu/GrSurfaceProxyView.h
+++ b/src/gpu/GrSurfaceProxyView.h
@@ -29,6 +29,8 @@
     GrSurfaceProxyView(GrSurfaceProxyView&& view) = default;
     GrSurfaceProxyView(const GrSurfaceProxyView&) = default;
 
+    operator bool() const { return SkToBool(fProxy.get()); }
+
     GrSurfaceProxyView& operator=(const GrSurfaceProxyView&) = default;
 
     bool operator==(const GrSurfaceProxyView& view) const {
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index dc2699e..3daafbf 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -919,9 +919,8 @@
     SkASSERT(!samplerState.isRepeated());
 
     SkScalar scales[2] = {1.f, 1.f};
-    sk_sp<GrTextureProxy> proxy =
-            GrRefCachedBitmapTextureProxy(fContext.get(), bitmap, samplerState, scales);
-    if (!proxy) {
+    GrSurfaceProxyView view = GrRefCachedBitmapView(fContext.get(), bitmap, samplerState, scales);
+    if (!view) {
         return;
     }
 
@@ -935,10 +934,6 @@
     // the rest from the SkPaint.
     std::unique_ptr<GrFragmentProcessor> fp;
 
-    GrSurfaceOrigin origin = proxy->origin();
-    GrSwizzle swizzle = proxy->textureSwizzle();
-    GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
-
     const auto& caps = *this->caps();
     if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
         if (bicubic) {
@@ -1311,11 +1306,7 @@
     ASSERT_SINGLE_OWNER
     uint32_t pinnedUniqueID;
     auto iter = std::make_unique<SkLatticeIter>(image->width(), image->height(), center, dst);
-    if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
-                                                                          &pinnedUniqueID)) {
-        GrSurfaceOrigin origin = proxy->origin();
-        GrSwizzle swizzle = proxy->textureSwizzle();
-        GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
+    if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
         GrTextureAdjuster adjuster(this->context(), std::move(view),
                                    image->imageInfo().colorInfo(), pinnedUniqueID);
         this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
@@ -1374,11 +1365,7 @@
     ASSERT_SINGLE_OWNER
     uint32_t pinnedUniqueID;
     auto iter = std::make_unique<SkLatticeIter>(lattice, dst);
-    if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
-                                                                          &pinnedUniqueID)) {
-        GrSurfaceOrigin origin = proxy->origin();
-        GrSwizzle swizzle = proxy->textureSwizzle();
-        GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
+    if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
         GrTextureAdjuster adjuster(this->context(), std::move(view),
                                    image->imageInfo().colorInfo(), pinnedUniqueID);
         this->drawProducerLattice(&adjuster, std::move(iter), dst, paint);
diff --git a/src/gpu/SkGpuDevice_drawTexture.cpp b/src/gpu/SkGpuDevice_drawTexture.cpp
index acd458a..23cad01 100644
--- a/src/gpu/SkGpuDevice_drawTexture.cpp
+++ b/src/gpu/SkGpuDevice_drawTexture.cpp
@@ -245,7 +245,7 @@
         // We've done enough checks above to allow us to pass ClampNearest() and not check for
         // scaling adjustments.
         auto[view, ct] = producer->view(GrMipMapped::kNo);
-        if (!view.proxy()) {
+        if (!view) {
             return;
         }
 
@@ -402,13 +402,12 @@
     // Pinned texture proxies can be rendered directly as textures, or with relatively simple
     // adjustments applied to the image content (scaling, mipmaps, color space, etc.)
     uint32_t pinnedUniqueID;
-    if (sk_sp<GrTextureProxy> proxy = as_IB(image)->refPinnedTextureProxy(this->context(),
-                                                                          &pinnedUniqueID)) {
+    if (GrSurfaceProxyView view = as_IB(image)->refPinnedView(this->context(), &pinnedUniqueID)) {
         SK_HISTOGRAM_BOOLEAN("DrawTiled", false);
         LogDrawScaleFactor(ctm, srcToDst, paint.getFilterQuality());
 
         GrColorInfo colorInfo;
-        if (fContext->priv().caps()->isFormatSRGB(proxy->backendFormat())) {
+        if (fContext->priv().caps()->isFormatSRGB(view.proxy()->backendFormat())) {
             SkASSERT(image->imageInfo().colorType() == kRGBA_8888_SkColorType);
             colorInfo = GrColorInfo(GrColorType::kRGBA_8888_SRGB, image->imageInfo().alphaType(),
                                     image->imageInfo().refColorSpace());
@@ -417,13 +416,10 @@
         }
 
         if (attemptDrawTexture && can_use_draw_texture(paint)) {
-            draw_texture(fRenderTargetContext.get(), this->clip(), ctm, paint, src,  dst,
-                         dstClip, aa, aaFlags, constraint, std::move(proxy), colorInfo);
+            draw_texture(fRenderTargetContext.get(), this->clip(), ctm, paint, src, dst, dstClip,
+                         aa, aaFlags, constraint, view.asTextureProxyRef(), colorInfo);
             return;
         }
-        GrSurfaceOrigin origin = proxy->origin();
-        GrSwizzle swizzle = proxy->textureSwizzle();
-        GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
         GrTextureAdjuster adjuster(fContext.get(), std::move(view), colorInfo, pinnedUniqueID,
                                    useDecal);
         draw_texture_producer(fContext.get(), fRenderTargetContext.get(), this->clip(), ctm,
@@ -536,20 +532,23 @@
             continue;
         }
 
-        sk_sp<GrTextureProxy> proxy;
+        GrSurfaceProxyView view;
         const SkImage_Base* image = as_IB(set[i].fImage.get());
-        // Extract proxy from image, but skip YUV images so they get processed through
+        // Extract view from image, but skip YUV images so they get processed through
         // drawImageQuad and the proper effect to dynamically sample their planes.
         if (!image->isYUVA()) {
             uint32_t uniqueID;
-            proxy = image->refPinnedTextureProxy(this->context(), &uniqueID);
-            if (!proxy) {
-                proxy = image->asTextureProxyRef(this->context(), GrSamplerState::Filter::kBilerp,
-                                                 nullptr);
+            view = image->refPinnedView(this->context(), &uniqueID);
+            if (!view) {
+                auto proxy = image->asTextureProxyRef(
+                        this->context(), GrSamplerState::Filter::kBilerp, nullptr);
+                GrSurfaceOrigin origin = proxy->origin();
+                const GrSwizzle& swizzle = proxy->textureSwizzle();
+                view = GrSurfaceProxyView(std::move(proxy), origin, swizzle);
             }
         }
 
-        if (!proxy) {
+        if (!view) {
             // This image can't go through the texture op, send through general image pipeline
             // after flushing current batch.
             draw(i + 1);
@@ -566,10 +565,7 @@
             continue;
         }
 
-        // TODO: have refPinnedTextureProxy and asTextureProxyRef return GrSurfaceProxyViews.
-        GrSurfaceOrigin origin = proxy->origin();
-        const GrSwizzle& swizzle = proxy->textureSwizzle();
-        textures[i].fProxyView = {std::move(proxy), origin, swizzle};
+        textures[i].fProxyView = std::move(view);
         textures[i].fSrcAlphaType = image->alphaType();
         textures[i].fSrcRect = set[i].fSrcRect;
         textures[i].fDstRect = set[i].fDstRect;
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index e051326..7579e28 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -140,12 +140,10 @@
     return view;
 }
 
-sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext* ctx,
-                                                    const SkBitmap& bitmap,
-                                                    GrSamplerState params,
-                                                    SkScalar scaleAdjust[2]) {
+GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext* ctx, const SkBitmap& bitmap,
+                                         GrSamplerState params, SkScalar scaleAdjust[2]) {
     GrBitmapTextureMaker maker(ctx, bitmap, GrBitmapTextureMaker::Cached::kYes);
-    return maker.viewForParams(params, scaleAdjust).asTextureProxyRef();
+    return maker.viewForParams(params, scaleAdjust);
 }
 
 GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext* context, const SkBitmap& bitmap,
diff --git a/src/gpu/SkGr.h b/src/gpu/SkGr.h
index 840d36f..9613d4a 100644
--- a/src/gpu/SkGr.h
+++ b/src/gpu/SkGr.h
@@ -165,17 +165,15 @@
 ////////////////////////////////////////////////////////////////////////////////
 // Texture management
 
-/** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
- *  texture is inserted into the cache (unless the bitmap is marked volatile) and can be
- *  retrieved again via this function.
+/** Returns a view that wraps a texture representing the bitmap that is compatible with the
+ *  GrSamplerState. The texture is inserted into the cache (unless the bitmap is marked volatile)
+ *  and can be retrieved again via this function.
  *  The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
  *  performed on the absolute texture coordinates (e.g., if the texture is resized out to
  *  the next power of two). It can be null if the caller is sure the bitmap won't be resized.
  */
-sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext*,
-                                                    const SkBitmap&,
-                                                    GrSamplerState,
-                                                    SkScalar scaleAdjust[2]);
+GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext*, const SkBitmap&, GrSamplerState,
+                                         SkScalar scaleAdjust[2]);
 
 /**
  * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h
index 6b1a2fe..332884b 100644
--- a/src/image/SkImage_Base.h
+++ b/src/image/SkImage_Base.h
@@ -67,9 +67,8 @@
 
     virtual sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*, GrSamplerState,
                                                     SkScalar scaleAdjust[2]) const = 0;
-    virtual sk_sp<GrTextureProxy> refPinnedTextureProxy(GrRecordingContext*,
-                                                        uint32_t* uniqueID) const {
-        return nullptr;
+    virtual GrSurfaceProxyView refPinnedView(GrRecordingContext*, uint32_t* uniqueID) const {
+        return {};
     }
     virtual bool isYUVA() const { return false; }
     virtual GrTexture* onGetTexture() const { return nullptr; }
diff --git a/src/image/SkImage_GpuBase.h b/src/image/SkImage_GpuBase.h
index 950fb7c..db5a26f 100644
--- a/src/image/SkImage_GpuBase.h
+++ b/src/image/SkImage_GpuBase.h
@@ -37,10 +37,10 @@
     sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*, GrSamplerState,
                                             SkScalar scaleAdjust[2]) const final;
 
-    sk_sp<GrTextureProxy> refPinnedTextureProxy(GrRecordingContext* context,
-                                                uint32_t* uniqueID) const final {
+    GrSurfaceProxyView refPinnedView(GrRecordingContext* context, uint32_t* uniqueID) const final {
         *uniqueID = this->uniqueID();
-        return this->asTextureProxyRef(context);
+        SkASSERT(this->view(context));
+        return *this->view(context);
     }
 
     GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp
index cdf3b45..f2fc42e 100644
--- a/src/image/SkImage_Raster.cpp
+++ b/src/image/SkImage_Raster.cpp
@@ -111,8 +111,8 @@
     }
 
 #if SK_SUPPORT_GPU
-    sk_sp<GrTextureProxy> refPinnedTextureProxy(GrRecordingContext*,
-                                                uint32_t* uniqueID) const override;
+    GrSurfaceProxyView refPinnedView(GrRecordingContext* context,
+                                     uint32_t* uniqueID) const override;
     bool onPinAsTexture(GrContext*) const override;
     void onUnpinAsTexture(GrContext*) const override;
 #endif
@@ -121,7 +121,7 @@
     SkBitmap fBitmap;
 
 #if SK_SUPPORT_GPU
-    mutable sk_sp<GrTextureProxy> fPinnedProxy;
+    mutable GrSurfaceProxyView fPinnedView;
     mutable int32_t fPinnedCount = 0;
     mutable uint32_t fPinnedUniqueID = 0;
 #endif
@@ -147,7 +147,7 @@
 
 SkImage_Raster::~SkImage_Raster() {
 #if SK_SUPPORT_GPU
-    SkASSERT(nullptr == fPinnedProxy.get());  // want the caller to have manually unpinned
+    SkASSERT(!fPinnedView);  // want the caller to have manually unpinned
 #endif
 }
 
@@ -175,45 +175,41 @@
     }
 
     uint32_t uniqueID;
-    sk_sp<GrTextureProxy> tex = this->refPinnedTextureProxy(context, &uniqueID);
-    if (tex) {
-        GrSurfaceOrigin origin = tex->origin();
-        GrSwizzle swizzle = tex->textureSwizzle();
-        GrSurfaceProxyView view(std::move(tex), origin, swizzle);
+    if (GrSurfaceProxyView view = this->refPinnedView(context, &uniqueID)) {
         GrTextureAdjuster adjuster(context, std::move(view), fBitmap.info().colorInfo(),
                                    fPinnedUniqueID);
         return adjuster.viewForParams(params, scaleAdjust).asTextureProxyRef();
     }
 
-    return GrRefCachedBitmapTextureProxy(context, fBitmap, params, scaleAdjust);
+    return GrRefCachedBitmapView(context, fBitmap, params, scaleAdjust).asTextureProxyRef();
 }
 #endif
 
 #if SK_SUPPORT_GPU
 
-sk_sp<GrTextureProxy> SkImage_Raster::refPinnedTextureProxy(GrRecordingContext*,
-                                                            uint32_t* uniqueID) const {
-    if (fPinnedProxy) {
+GrSurfaceProxyView SkImage_Raster::refPinnedView(GrRecordingContext*, uint32_t* uniqueID) const {
+    if (fPinnedView) {
         SkASSERT(fPinnedCount > 0);
         SkASSERT(fPinnedUniqueID != 0);
         *uniqueID = fPinnedUniqueID;
-        return fPinnedProxy;
+        return fPinnedView;
     }
-    return nullptr;
+    return {};
 }
 
 bool SkImage_Raster::onPinAsTexture(GrContext* ctx) const {
-    if (fPinnedProxy) {
+    if (fPinnedView) {
         SkASSERT(fPinnedCount > 0);
         SkASSERT(fPinnedUniqueID != 0);
     } else {
         SkASSERT(fPinnedCount == 0);
         SkASSERT(fPinnedUniqueID == 0);
-        fPinnedProxy = GrRefCachedBitmapTextureProxy(ctx, fBitmap, GrSamplerState::Filter::kNearest,
-                                                     nullptr);
-        if (!fPinnedProxy) {
+        fPinnedView =
+                GrRefCachedBitmapView(ctx, fBitmap, GrSamplerState::Filter::kNearest, nullptr);
+        if (!fPinnedView) {
             return false;
         }
+        SkASSERT(fPinnedView.asTextureProxy());
         fPinnedUniqueID = fBitmap.getGenerationID();
     }
     // Note: we only increment if the texture was successfully pinned
@@ -227,7 +223,7 @@
     SkASSERT(fPinnedUniqueID != 0);
 
     if (0 == --fPinnedCount) {
-        fPinnedProxy.reset(nullptr);
+        fPinnedView = GrSurfaceProxyView();
         fPinnedUniqueID = 0;
     }
 }