Cleanup after removal of IORefs from GrSurface

The removal of IORefs from GrSurface makes a lot of other cruft obsolete.

Change-Id: I0e02d680a17dc4f4ec705cb6ee4c294738271e28
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/239919
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrGpuResourceCacheAccess.h b/src/gpu/GrGpuResourceCacheAccess.h
index 03edec9..b18949c 100644
--- a/src/gpu/GrGpuResourceCacheAccess.h
+++ b/src/gpu/GrGpuResourceCacheAccess.h
@@ -55,7 +55,7 @@
     /** Called by the cache to assign a new unique key. */
     void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; }
 
-    /** Is the resource ref'ed (not counting pending IOs). */
+    /** Is the resource ref'ed */
     bool hasRef() const { return fResource->hasRef(); }
 
     /** Called by the cache to make the unique key invalid. */
diff --git a/src/gpu/GrOnFlushResourceProvider.cpp b/src/gpu/GrOnFlushResourceProvider.cpp
index bb63505..781df12 100644
--- a/src/gpu/GrOnFlushResourceProvider.cpp
+++ b/src/gpu/GrOnFlushResourceProvider.cpp
@@ -20,8 +20,7 @@
         sk_sp<GrSurfaceProxy> proxy, GrColorType colorType, sk_sp<SkColorSpace> colorSpace,
         const SkSurfaceProps* props) {
     // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
-    // we have to manually ensure it is allocated here. The proxy had best have been created
-    // with the kNoPendingIO flag!
+    // we have to manually ensure it is allocated here.
     if (!this->instatiateProxy(proxy.get())) {
         return nullptr;
     }
diff --git a/src/gpu/GrProxyProvider.cpp b/src/gpu/GrProxyProvider.cpp
index f65ad6f..46ea7b7 100644
--- a/src/gpu/GrProxyProvider.cpp
+++ b/src/gpu/GrProxyProvider.cpp
@@ -147,12 +147,10 @@
 
     if (SkBackingFit::kApprox == fit) {
         tex = resourceProvider->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
-                                                    isProtected,
-                                                    GrResourceProvider::Flags::kNoPendingIO);
+                                                    isProtected);
     } else {
         tex = resourceProvider->createTexture(desc, format, renderable, renderTargetSampleCnt,
-                                              budgeted, isProtected,
-                                              GrResourceProvider::Flags::kNoPendingIO);
+                                              budgeted, isProtected);
     }
     if (!tex) {
         return nullptr;
@@ -300,7 +298,7 @@
 
                 return LazyCallbackResult(resourceProvider->createTexture(
                         desc, format, GrRenderable::kNo, sampleCnt, budgeted, fit, GrProtected::kNo,
-                        ct, mipLevel, GrResourceProvider::Flags::kNoPendingIO));
+                        ct, mipLevel));
             },
             format, desc, GrRenderable::kNo, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
             GrMipMapsStatus::kNotAllocated, surfaceFlags, fit, budgeted, GrProtected::kNo,
diff --git a/src/gpu/GrResourceCache.cpp b/src/gpu/GrResourceCache.cpp
index afb2b38..e6655d2 100644
--- a/src/gpu/GrResourceCache.cpp
+++ b/src/gpu/GrResourceCache.cpp
@@ -285,8 +285,7 @@
         SkASSERT(!resource->getUniqueKey().isValid() &&
                  resource->resourcePriv().getScratchKey().isValid());
 
-        // isScratch() also tests that the resource is budgeted. TODO: why are unbudgeted
-        // scratch resouces in the scratchMap?
+        // isScratch() also tests that the resource is budgeted.
         if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
             return false;
         }
@@ -294,32 +293,10 @@
     }
 };
 
-GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
-                                                          size_t resourceSize,
-                                                          ScratchFlags flags) {
+GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
     SkASSERT(scratchKey.isValid());
 
-    GrGpuResource* resource;
-    // TODO: remove these conditions and fuse the two code paths!
-    if (flags & (ScratchFlags::kPreferNoPendingIO | ScratchFlags::kRequireNoPendingIO)) {
-        resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
-        if (resource) {
-            this->refAndMakeResourceMRU(resource);
-            this->validate();
-            return resource;
-        } else if (flags & ScratchFlags::kRequireNoPendingIO) {
-            return nullptr;
-        }
-        // We would prefer to consume more available VRAM rather than flushing
-        // immediately, but on ANGLE this can lead to starving of the GPU.
-        if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
-            // kPrefer is specified, we didn't find a resource without pending io,
-            // but there is still space in our budget for the resource so force
-            // the caller to allocate a new resource.
-            return nullptr;
-        }
-    }
-    resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
+    GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
     if (resource) {
         this->refAndMakeResourceMRU(resource);
         this->validate();
diff --git a/src/gpu/GrResourceCache.h b/src/gpu/GrResourceCache.h
index 1b800bf..efbdb49 100644
--- a/src/gpu/GrResourceCache.h
+++ b/src/gpu/GrResourceCache.h
@@ -116,19 +116,10 @@
      */
     void releaseAll();
 
-    enum class ScratchFlags {
-        kNone = 0,
-        /** Preferentially returns scratch resources with no pending IO. */
-        kPreferNoPendingIO = 0x1,
-        /** Will not return any resources that match but have pending IO. */
-        kRequireNoPendingIO = 0x2,
-    };
-
     /**
      * Find a resource that matches a scratch key.
      */
-    GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, size_t resourceSize,
-                                             ScratchFlags);
+    GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey);
 
 #ifdef SK_DEBUG
     // This is not particularly fast and only used for validation, so debug only.
@@ -377,8 +368,6 @@
     bool                                fPreferVRAMUseOverFlushes = false;
 };
 
-GR_MAKE_BITFIELD_CLASS_OPS(GrResourceCache::ScratchFlags);
-
 class GrResourceCache::ResourceAccess {
 private:
     ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index 6e964ef..214b24d 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -125,10 +125,9 @@
                                                      GrRenderable renderable,
                                                      int renderTargetSampleCnt,
                                                      SkBudgeted budgeted,
-                                                     GrProtected isProtected,
-                                                     Flags flags) {
+                                                     GrProtected isProtected) {
     sk_sp<GrTexture> tex(this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
-                                                 isProtected, flags));
+                                                 isProtected));
     if (tex && SkBudgeted::kNo == budgeted) {
         tex->resourcePriv().makeUnbudgeted();
     }
@@ -144,8 +143,7 @@
                                                    SkBackingFit fit,
                                                    GrProtected isProtected,
                                                    GrColorType srcColorType,
-                                                   const GrMipLevel& mipLevel,
-                                                   Flags flags) {
+                                                   const GrMipLevel& mipLevel) {
     ASSERT_SINGLE_OWNER
 
     if (this->isAbandoned()) {
@@ -178,9 +176,9 @@
     sk_sp<GrTexture> tex =
             (SkBackingFit::kApprox == fit)
                     ? this->createApproxTexture(desc, format, renderable, renderTargetSampleCnt,
-                                                isProtected, flags)
+                                                isProtected)
                     : this->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
-                                          isProtected, flags);
+                                          isProtected);
     if (!tex) {
         return nullptr;
     }
@@ -221,8 +219,7 @@
                                                    GrRenderable renderable,
                                                    int renderTargetSampleCnt,
                                                    SkBudgeted budgeted,
-                                                   GrProtected isProtected,
-                                                   Flags flags) {
+                                                   GrProtected isProtected) {
     ASSERT_SINGLE_OWNER
     if (this->isAbandoned()) {
         return nullptr;
@@ -236,7 +233,7 @@
     // Compressed textures are read-only so they don't support re-use for scratch.
     if (!GrPixelConfigIsCompressed(desc.fConfig)) {
         sk_sp<GrTexture> tex = this->getExactScratch(
-                desc, format, renderable, renderTargetSampleCnt, budgeted, isProtected, flags);
+                desc, format, renderable, renderTargetSampleCnt, budgeted, isProtected);
         if (tex) {
             return tex;
         }
@@ -287,10 +284,8 @@
                                                          const GrBackendFormat& format,
                                                          GrRenderable renderable,
                                                          int renderTargetSampleCnt,
-                                                         GrProtected isProtected,
-                                                         Flags flags) {
+                                                         GrProtected isProtected) {
     ASSERT_SINGLE_OWNER
-    SkASSERT(Flags::kNone == flags || Flags::kNoPendingIO == flags);
 
     if (this->isAbandoned()) {
         return nullptr;
@@ -307,7 +302,7 @@
     }
 
     if (auto tex = this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
-                                           isProtected, flags)) {
+                                           isProtected)) {
         return tex;
     }
 
@@ -321,7 +316,7 @@
     }
 
     if (auto tex = this->refScratchTexture(*copyDesc, format, renderable, renderTargetSampleCnt,
-                                           isProtected, flags)) {
+                                           isProtected)) {
         return tex;
     }
 
@@ -343,8 +338,7 @@
                                                        const GrBackendFormat& format,
                                                        GrRenderable renderable,
                                                        int renderTargetSampleCnt,
-                                                       GrProtected isProtected,
-                                                       Flags flags) {
+                                                       GrProtected isProtected) {
     ASSERT_SINGLE_OWNER
     SkASSERT(!this->isAbandoned());
     SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
@@ -356,17 +350,7 @@
     if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
         GrScratchKey key;
         GrTexturePriv::ComputeScratchKey(desc, renderable, renderTargetSampleCnt, &key);
-        auto scratchFlags = GrResourceCache::ScratchFlags::kNone;
-        if (Flags::kNoPendingIO & flags) {
-            scratchFlags |= GrResourceCache::ScratchFlags::kRequireNoPendingIO;
-        } else if (renderable == GrRenderable::kNo) {
-            // If it is not a render target then it will most likely be populated by
-            // writePixels() which will trigger a flush if the texture has pending IO.
-            scratchFlags |= GrResourceCache::ScratchFlags::kPreferNoPendingIO;
-        }
-        GrGpuResource* resource = fCache->findAndRefScratchResource(
-                key, GrSurface::WorstCaseSize(desc, renderable, renderTargetSampleCnt),
-                scratchFlags);
+        GrGpuResource* resource = fCache->findAndRefScratchResource(key);
         if (resource) {
             fGpu->stats()->incNumScratchTexturesReused();
             GrSurface* surface = static_cast<GrSurface*>(resource);
@@ -458,7 +442,6 @@
                                                                         const GrUniqueKey* key) {
     size_t bufferSize = patternSize * reps * sizeof(uint16_t);
 
-    // This is typically used in GrMeshDrawOps, so we assume kNoPendingIO.
     sk_sp<GrGpuBuffer> buffer(
             this->createBuffer(bufferSize, GrGpuBufferType::kIndex, kStatic_GrAccessPattern));
     if (!buffer) {
@@ -527,7 +510,7 @@
     GrGpuBuffer::ComputeScratchKeyForDynamicVBO(allocSize, intendedType, &key);
     auto buffer =
             sk_sp<GrGpuBuffer>(static_cast<GrGpuBuffer*>(this->cache()->findAndRefScratchResource(
-                    key, allocSize, GrResourceCache::ScratchFlags::kNone)));
+                    key)));
     if (!buffer) {
         buffer = this->gpu()->createBuffer(allocSize, intendedType, kDynamic_GrAccessPattern);
         if (!buffer) {
diff --git a/src/gpu/GrResourceProvider.h b/src/gpu/GrResourceProvider.h
index d3264d3..7d77bbf 100644
--- a/src/gpu/GrResourceProvider.h
+++ b/src/gpu/GrResourceProvider.h
@@ -33,26 +33,10 @@
 class SkTypeface;
 
 /**
- * A factory for arbitrary resource types. This class is intended for use within the Gr code base.
- *
- * Some members force callers to make a flags (pendingIO) decision. This can be relaxed once
- * https://bug.skia.org/4156 is fixed.
+ * A factory for arbitrary resource types.
  */
 class GrResourceProvider {
 public:
-    /** These flags govern which scratch resources we are allowed to return */
-    enum class Flags {
-        kNone            = 0x0,
-
-        /** If the caller intends to do direct reads/writes to/from the CPU then this flag must be
-         *  set when accessing resources during a GrOpsTask flush. This includes the execution of
-         *  GrOp objects. The reason is that these memory operations are done immediately and
-         *  will occur out of order WRT the operations being flushed.
-         *  Make this automatic: https://bug.skia.org/4156
-         */
-        kNoPendingIO     = 0x1,
-    };
-
     GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*);
 
     /**
@@ -79,8 +63,7 @@
                                          const GrBackendFormat& format,
                                          GrRenderable renderable,
                                          int renderTargetSampleCnt,
-                                         GrProtected isProtected,
-                                         Flags flags);
+                                         GrProtected isProtected);
 
     /** Create an exact fit texture with no initial data to upload. */
     sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc,
@@ -88,8 +71,7 @@
                                    GrRenderable renderable,
                                    int renderTargetSampleCnt,
                                    SkBudgeted budgeted,
-                                   GrProtected isProtected,
-                                   Flags flags = Flags::kNone);
+                                   GrProtected isProtected);
 
     sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc,
                                    const GrBackendFormat& format,
@@ -109,8 +91,7 @@
                                    SkBackingFit fit,
                                    GrProtected isProtected,
                                    GrColorType srcColorType,
-                                   const GrMipLevel& mipLevel,
-                                   Flags flags);
+                                   const GrMipLevel& mipLevel);
 
     /**
      * Creates a compressed texture. The GrGpu must support the SkImageImage::Compression type.
@@ -296,8 +277,7 @@
                                        const GrBackendFormat& format,
                                        GrRenderable renderable,
                                        int renderTargetSampleCnt,
-                                       GrProtected isProtected,
-                                       Flags flags);
+                                       GrProtected isProtected);
 
     /*
      * Try to find an existing scratch texture that exactly matches 'desc'. If successful
@@ -308,8 +288,7 @@
                                      GrRenderable renderable,
                                      int renderTargetSampleCnt,
                                      SkBudgeted budgeted,
-                                     GrProtected isProtected,
-                                     Flags flags);
+                                     GrProtected isProtected);
 
     GrResourceCache* cache() { return fCache; }
     const GrResourceCache* cache() const { return fCache; }
@@ -342,6 +321,4 @@
     SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
 };
 
-GR_MAKE_BITFIELD_CLASS_OPS(GrResourceProvider::Flags);
-
 #endif
diff --git a/src/gpu/GrSurface.cpp b/src/gpu/GrSurface.cpp
index 3541b33..3144837 100644
--- a/src/gpu/GrSurface.cpp
+++ b/src/gpu/GrSurface.cpp
@@ -15,45 +15,6 @@
 #include "src/core/SkMathPriv.h"
 #include "src/gpu/SkGr.h"
 
-size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, GrRenderable renderable,
-                                int renderTargetSampleCnt, bool binSize) {
-    size_t size;
-
-    int width  = binSize ? GrResourceProvider::MakeApprox(desc.fWidth)  : desc.fWidth;
-    int height = binSize ? GrResourceProvider::MakeApprox(desc.fHeight) : desc.fHeight;
-
-    if (renderable == GrRenderable::kYes) {
-        // We own one color value for each MSAA sample.
-        SkASSERT(renderTargetSampleCnt >= 1);
-        int colorValuesPerPixel = renderTargetSampleCnt;
-        if (renderTargetSampleCnt > 1) {
-            // Worse case, we own the resolve buffer so that is one more sample per pixel.
-            colorValuesPerPixel += 1;
-        }
-        SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
-        SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
-        size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
-
-        // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces).
-        // Unfortunately Chromium seems to want to do this.
-        //SkASSERT(colorBytes > 0);
-
-        size = colorValuesPerPixel * colorBytes;
-        size += colorBytes/3; // in case we have to mipmap
-    } else {
-        SkASSERT(renderTargetSampleCnt == 1);
-        if (GrPixelConfigIsCompressed(desc.fConfig)) {
-            size = GrCompressedFormatDataSize(desc.fConfig, width, height);
-        } else {
-            size = (size_t)width * height * GrBytesPerPixel(desc.fConfig);
-        }
-
-        size += size/3;  // in case we have to mipmap
-    }
-
-    return size;
-}
-
 size_t GrSurface::ComputeSize(GrPixelConfig config,
                               int width,
                               int height,
diff --git a/src/gpu/GrSurfacePriv.h b/src/gpu/GrSurfacePriv.h
index 6df6284..bd7aa53 100644
--- a/src/gpu/GrSurfacePriv.h
+++ b/src/gpu/GrSurfacePriv.h
@@ -17,8 +17,6 @@
     implemented privately in GrSurface with a inline public method here). */
 class GrSurfacePriv {
 public:
-    bool hasUniqueRef() const { return fSurface->internalHasUniqueRef(); }
-
     GrInternalSurfaceFlags flags() const { return fSurface->fSurfaceFlags; }
 
 private:
diff --git a/src/gpu/GrSurfaceProxy.cpp b/src/gpu/GrSurfaceProxy.cpp
index a6f595d..e79953e 100644
--- a/src/gpu/GrSurfaceProxy.cpp
+++ b/src/gpu/GrSurfaceProxy.cpp
@@ -174,10 +174,6 @@
     desc.fHeight = fHeight;
     desc.fConfig = fConfig;
 
-    // The explicit resource allocator requires that any resources it pulls out of the
-    // cache have no pending IO.
-    GrResourceProvider::Flags resourceProviderFlags = GrResourceProvider::Flags::kNoPendingIO;
-
     sk_sp<GrSurface> surface;
     if (GrMipMapped::kYes == mipMapped) {
         SkASSERT(SkBackingFit::kExact == fFit);
@@ -211,11 +207,11 @@
     } else {
         if (SkBackingFit::kApprox == fFit) {
             surface = resourceProvider->createApproxTexture(desc, fFormat, renderable, sampleCnt,
-                                                            fIsProtected, resourceProviderFlags);
+                                                            fIsProtected);
         } else {
             surface =
                     resourceProvider->createTexture(desc, fFormat, renderable, sampleCnt, fBudgeted,
-                                                    fIsProtected, resourceProviderFlags);
+                                                    fIsProtected);
         }
     }
     if (!surface) {
diff --git a/src/gpu/GrTexture.cpp b/src/gpu/GrTexture.cpp
index e6a9f15..371aa8e 100644
--- a/src/gpu/GrTexture.cpp
+++ b/src/gpu/GrTexture.cpp
@@ -51,7 +51,7 @@
 bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
                                     GrBackendTexture* backendTexture,
                                     SkImage::BackendTextureReleaseProc* releaseProc) {
-    if (!texture->surfacePriv().hasUniqueRef()) {
+    if (!texture->unique()) {
         return false;
     }
 
diff --git a/src/gpu/ccpr/GrCCAtlas.cpp b/src/gpu/ccpr/GrCCAtlas.cpp
index 5db2f2f..92fca8e 100644
--- a/src/gpu/ccpr/GrCCAtlas.cpp
+++ b/src/gpu/ccpr/GrCCAtlas.cpp
@@ -123,7 +123,7 @@
                     desc.fConfig = pixelConfig;
                     fBackingTexture = resourceProvider->createTexture(
                             desc, format, GrRenderable::kYes, sampleCount, SkBudgeted::kYes,
-                            GrProtected::kNo, GrResourceProvider::Flags::kNoPendingIO);
+                            GrProtected::kNo);
                 }
                 return fBackingTexture;
             },
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index baae2b3..4730194 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -652,7 +652,7 @@
 
     // We must make a copy of the image if the image is not unique, if the GrTexture owned by the
     // image is not unique, or if the texture wraps an external object.
-    if (!image->unique() || !texture->surfacePriv().hasUniqueRef() ||
+    if (!image->unique() || !texture->unique() ||
         texture->resourcePriv().refsWrappedObjects()) {
         // onMakeSubset will always copy the image.
         image = as_IB(image)->onMakeSubset(ctx, image->bounds());
@@ -670,7 +670,7 @@
     }
 
     SkASSERT(!texture->resourcePriv().refsWrappedObjects());
-    SkASSERT(texture->surfacePriv().hasUniqueRef());
+    SkASSERT(texture->unique());
     SkASSERT(image->unique());
 
     // Take a reference to the GrTexture and release the image.