Add findOrMakeStaticBuffer method to GrResourceProvider

Bug: skia:
Change-Id: Ie47f00bf8542462d719df0d08972794861ec4a2b
Reviewed-on: https://skia-review.googlesource.com/86283
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/gpu/GrGpuResourcePriv.h b/src/gpu/GrGpuResourcePriv.h
index a1e207d..affc753 100644
--- a/src/gpu/GrGpuResourcePriv.h
+++ b/src/gpu/GrGpuResourcePriv.h
@@ -16,6 +16,8 @@
  */
 class GrGpuResource::ResourcePriv {
 public:
+    SkDEBUGCODE(bool hasPendingIO_debugOnly() const { return fResource->internalHasPendingIO(); })
+
     /**
      * Sets a unique key for the resource. If the resource was previously cached as scratch it will
      * be converted to a uniquely-keyed resource. If the key is invalid then this is equivalent to
diff --git a/src/gpu/GrOnFlushResourceProvider.cpp b/src/gpu/GrOnFlushResourceProvider.cpp
index c9fc77e..e3b5eeb 100644
--- a/src/gpu/GrOnFlushResourceProvider.cpp
+++ b/src/gpu/GrOnFlushResourceProvider.cpp
@@ -88,19 +88,14 @@
                                             data));
 }
 
-sk_sp<GrBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(const GrUniqueKey& key,
-                                                                  GrBufferType intendedType,
-                                                                  size_t size, const void* data) {
+sk_sp<const GrBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
+                                                                        size_t size,
+                                                                        const void* data,
+                                                                        const GrUniqueKey& key) {
     GrResourceProvider* rp = fDrawingMgr->getContext()->resourceProvider();
-    sk_sp<GrBuffer> buffer(rp->findByUniqueKey<GrBuffer>(key));
-    if (!buffer) {
-        buffer.reset(rp->createBuffer(size, intendedType, kStatic_GrAccessPattern, 0, data));
-        if (!buffer) {
-            return nullptr;
-        }
-        SkASSERT(buffer->sizeInBytes() == size); // rp shouldn't bin and/or cache static buffers.
-        buffer->resourcePriv().setUniqueKey(key);
-    }
+    sk_sp<const GrBuffer> buffer = rp->findOrMakeStaticBuffer(intendedType, size, data, key);
+    // Static buffers should never have pending IO.
+    SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
     return buffer;
 }
 
diff --git a/src/gpu/GrOnFlushResourceProvider.h b/src/gpu/GrOnFlushResourceProvider.h
index e16bb57..f4e4459 100644
--- a/src/gpu/GrOnFlushResourceProvider.h
+++ b/src/gpu/GrOnFlushResourceProvider.h
@@ -83,8 +83,8 @@
     sk_sp<GrBuffer> makeBuffer(GrBufferType, size_t, const void* data = nullptr);
 
     // Either finds and refs, or creates a static GPU buffer with the given data.
-    sk_sp<GrBuffer> findOrMakeStaticBuffer(const GrUniqueKey&, GrBufferType,
-                                           size_t, const void* data);
+    sk_sp<const GrBuffer> findOrMakeStaticBuffer(GrBufferType, size_t, const void* data,
+                                                 const GrUniqueKey&);
 
     const GrCaps* caps() const;
 
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index 8b36bad..7fef350 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -312,6 +312,25 @@
     return this->isAbandoned() ? nullptr : fCache->findOrCreateProxyByUniqueKey(key, origin);
 }
 
+sk_sp<const GrBuffer> GrResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
+                                                                 size_t size,
+                                                                 const void* data,
+                                                                 const GrUniqueKey& key) {
+    if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
+        return buffer;
+    }
+    if (auto buffer = this->createBuffer(size, intendedType, kStatic_GrAccessPattern, 0,
+                                         data)) {
+        // We shouldn't bin and/or cachestatic buffers.
+        SkASSERT(buffer->sizeInBytes() == size);
+        SkASSERT(!buffer->resourcePriv().getScratchKey().isValid());
+        SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
+        buffer->resourcePriv().setUniqueKey(key);
+        return sk_sp<const GrBuffer>(buffer);
+    }
+    return nullptr;
+}
+
 sk_sp<const GrBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
                                                                      int patternSize,
                                                                      int reps,
diff --git a/src/gpu/GrResourceProvider.h b/src/gpu/GrResourceProvider.h
index 4cd34da..f7f7850 100644
--- a/src/gpu/GrResourceProvider.h
+++ b/src/gpu/GrResourceProvider.h
@@ -133,6 +133,19 @@
     static const uint32_t kMinScratchTextureSize;
 
     /**
+     * Either finds and refs, or creates a static buffer with the given parameters and contents.
+     *
+     * @param intendedType    hint to the graphics subsystem about what the buffer will be used for.
+     * @param size            minimum size of buffer to return.
+     * @param data            optional data with which to initialize the buffer.
+     * @param key             Key to be assigned to the buffer.
+     *
+     * @return The buffer if successful, otherwise nullptr.
+     */
+    sk_sp<const GrBuffer> findOrMakeStaticBuffer(GrBufferType intendedType, size_t size,
+                                                 const void* data, const GrUniqueKey& key);
+
+    /**
      * Either finds and refs, or creates an index buffer with a repeating pattern for drawing
      * contiguous vertices of a repeated mesh. If the return is non-null, the caller owns a ref on
      * the returned GrBuffer.
diff --git a/src/gpu/ccpr/GrCCPRPathProcessor.cpp b/src/gpu/ccpr/GrCCPRPathProcessor.cpp
index c99a010..2f0c023 100644
--- a/src/gpu/ccpr/GrCCPRPathProcessor.cpp
+++ b/src/gpu/ccpr/GrCCPRPathProcessor.cpp
@@ -36,6 +36,12 @@
 
 GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
 
+sk_sp<const GrBuffer> GrCCPRPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
+    GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
+    return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms),
+                                             kOctoEdgeNorms, gVertexBufferKey);
+}
+
 // Index buffer for the octagon defined above.
 static uint16_t kOctoIndices[GrCCPRPathProcessor::kPerInstanceIndexCount] = {
     0, 4, 2,
@@ -48,6 +54,12 @@
 
 GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
 
+sk_sp<const GrBuffer> GrCCPRPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
+    GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
+    return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndices),
+                                             kOctoIndices, gIndexBufferKey);
+}
+
 GrCCPRPathProcessor::GrCCPRPathProcessor(GrResourceProvider* rp, sk_sp<GrTextureProxy> atlas,
                                          SkPath::FillType fillType, const GrShaderCaps& shaderCaps)
         : INHERITED(kGrCCPRPathProcessor_ClassID)
@@ -190,15 +202,3 @@
         f->codeAppendf("%s = half4(1 - abs(t - 1));", args.fOutputCoverage);
     }
 }
-
-sk_sp<GrBuffer> GrCCPRPathProcessor::FindOrMakeIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
-    GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
-    return onFlushRP->findOrMakeStaticBuffer(gIndexBufferKey, kIndex_GrBufferType,
-                                             sizeof(kOctoIndices), kOctoIndices);
-}
-
-sk_sp<GrBuffer> GrCCPRPathProcessor::FindOrMakeVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
-    GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
-    return onFlushRP->findOrMakeStaticBuffer(gVertexBufferKey, kVertex_GrBufferType,
-                                             sizeof(kOctoEdgeNorms), kOctoEdgeNorms);
-}
diff --git a/src/gpu/ccpr/GrCCPRPathProcessor.h b/src/gpu/ccpr/GrCCPRPathProcessor.h
index 7ad314e..97daefe 100644
--- a/src/gpu/ccpr/GrCCPRPathProcessor.h
+++ b/src/gpu/ccpr/GrCCPRPathProcessor.h
@@ -27,8 +27,6 @@
 class GrCCPRPathProcessor : public GrGeometryProcessor {
 public:
     static constexpr int kPerInstanceIndexCount = 6 * 3;
-    static sk_sp<GrBuffer> FindOrMakeIndexBuffer(GrOnFlushResourceProvider*);
-    static sk_sp<GrBuffer> FindOrMakeVertexBuffer(GrOnFlushResourceProvider*);
 
     enum class InstanceAttribs {
         kDevBounds,
@@ -54,6 +52,9 @@
 
     GR_STATIC_ASSERT(4 * 16 == sizeof(Instance));
 
+    static sk_sp<const GrBuffer> FindIndexBuffer(GrOnFlushResourceProvider*);
+    static sk_sp<const GrBuffer> FindVertexBuffer(GrOnFlushResourceProvider*);
+
     GrCCPRPathProcessor(GrResourceProvider*, sk_sp<GrTextureProxy> atlas, SkPath::FillType,
                        const GrShaderCaps&);
 
diff --git a/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp b/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp
index 937fd48..a239569 100644
--- a/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp
+++ b/src/gpu/ccpr/GrCoverageCountingPathRenderer.cpp
@@ -324,13 +324,13 @@
     }
 
     // Allocate GPU buffers.
-    fPerFlushIndexBuffer = GrCCPRPathProcessor::FindOrMakeIndexBuffer(onFlushRP);
+    fPerFlushIndexBuffer = GrCCPRPathProcessor::FindIndexBuffer(onFlushRP);
     if (!fPerFlushIndexBuffer) {
         SkDebugf("WARNING: failed to allocate ccpr path index buffer.\n");
         return;
     }
 
-    fPerFlushVertexBuffer = GrCCPRPathProcessor::FindOrMakeVertexBuffer(onFlushRP);
+    fPerFlushVertexBuffer = GrCCPRPathProcessor::FindVertexBuffer(onFlushRP);
     if (!fPerFlushVertexBuffer) {
         SkDebugf("WARNING: failed to allocate ccpr path vertex buffer.\n");
         return;
diff --git a/src/gpu/ccpr/GrCoverageCountingPathRenderer.h b/src/gpu/ccpr/GrCoverageCountingPathRenderer.h
index 1d08f38..8446af6 100644
--- a/src/gpu/ccpr/GrCoverageCountingPathRenderer.h
+++ b/src/gpu/ccpr/GrCoverageCountingPathRenderer.h
@@ -217,8 +217,8 @@
     std::map<uint32_t, RTPendingPaths> fRTPendingPathsMap;
     SkDEBUGCODE(int fPendingDrawOpsCount = 0;)
 
-    sk_sp<GrBuffer> fPerFlushIndexBuffer;
-    sk_sp<GrBuffer> fPerFlushVertexBuffer;
+    sk_sp<const GrBuffer> fPerFlushIndexBuffer;
+    sk_sp<const GrBuffer> fPerFlushVertexBuffer;
     sk_sp<GrBuffer> fPerFlushInstanceBuffer;
     GrSTAllocator<4, GrCCPRAtlas> fPerFlushAtlases;
     bool fPerFlushResourcesAreValid;