Change GrRingBuffer::finishSubmit to be a finishedProc

Change-Id: I307669ee928152ea3616f3814716da7476a03c46
Bug: skia:10530
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/305716
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h
index 23704aa..6883d73 100644
--- a/src/gpu/GrGpu.h
+++ b/src/gpu/GrGpu.h
@@ -382,9 +382,11 @@
     virtual void insertSemaphore(GrSemaphore* semaphore) = 0;
     virtual void waitSemaphore(GrSemaphore* semaphore) = 0;
 
+    virtual void addFinishedProc(GrGpuFinishedProc finishedProc,
+                                 GrGpuFinishedContext finishedContext) = 0;
     virtual void checkFinishProcs() = 0;
 
-    virtual void takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer>) {}
+    virtual void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) {}
 
     /**
      * Checks if we detected an OOM from the underlying 3D API and if so returns true and resets
@@ -820,9 +822,6 @@
     virtual bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
                                const SkIPoint& dstPoint) = 0;
 
-    virtual void addFinishedProc(GrGpuFinishedProc finishedProc,
-                                 GrGpuFinishedContext finishedContext) = 0;
-
     virtual void prepareSurfacesForBackendAccessAndStateUpdates(
             GrSurfaceProxy* proxies[],
             int numProxies,
diff --git a/src/gpu/GrRingBuffer.cpp b/src/gpu/GrRingBuffer.cpp
index a911c20..86b7904 100644
--- a/src/gpu/GrRingBuffer.cpp
+++ b/src/gpu/GrRingBuffer.cpp
@@ -79,17 +79,27 @@
 }
 
 // used when current command buffer/command list is submitted
-void GrRingBuffer::startSubmit(GrRingBuffer::SubmitData* submitData) {
-    submitData->fTrackedBuffers = std::move(fTrackedBuffers);
-    submitData->fLastHead = fHead;
-    submitData->fGenID = fGenID;
+void GrRingBuffer::startSubmit(GrGpu* gpu) {
+    for (unsigned int i = 0; i < fTrackedBuffers.size(); ++i) {
+        gpu->takeOwnershipOfBuffer(std::move(fTrackedBuffers[i]));
+    }
+    fTrackedBuffers.clear();
     // add current buffer to be tracked for next submit
     fTrackedBuffers.push_back(fCurrentBuffer);
+
+    SubmitData* submitData = new SubmitData();
+    submitData->fOwner = this;
+    submitData->fLastHead = fHead;
+    submitData->fGenID = fGenID;
+    gpu->addFinishedProc(FinishSubmit, submitData);
 }
 
 // used when current command buffer/command list is completed
-void GrRingBuffer::finishSubmit(const GrRingBuffer::SubmitData& submitData) {
-    if (submitData.fGenID == fGenID) {
-        fTail = submitData.fLastHead;
+void GrRingBuffer::FinishSubmit(void* finishedContext) {
+    GrRingBuffer::SubmitData* submitData = (GrRingBuffer::SubmitData*)finishedContext;
+    if (submitData && submitData->fOwner && submitData->fGenID == submitData->fOwner->fGenID) {
+        submitData->fOwner->fTail = submitData->fLastHead;
+        submitData->fOwner = nullptr;
     }
+    delete submitData;
 }
diff --git a/src/gpu/GrRingBuffer.h b/src/gpu/GrRingBuffer.h
index caa2311..5aeaad6 100644
--- a/src/gpu/GrRingBuffer.h
+++ b/src/gpu/GrRingBuffer.h
@@ -41,24 +41,19 @@
     };
     Slice suballocate(size_t size);
 
-    class SubmitData {
-    public:
-        const GrRingBuffer* fRingBuffer;
-        std::vector<sk_sp<GrGpuBuffer>> fTrackedBuffers;
-    private:
-        friend class GrRingBuffer;
-        size_t fLastHead;
-        size_t fGenID;
-    };
-    // Backends should call startSubmit() at submit time, and finishSubmit() when the
-    // command buffer/list finishes.
-    void startSubmit(SubmitData*);
-    void finishSubmit(const SubmitData&);
+    // Backends should call startSubmit() at submit time
+    void startSubmit(GrGpu*);
 
     size_t size() const { return fTotalSize; }
 
 private:
     size_t getAllocationOffset(size_t size);
+    struct SubmitData {
+        GrRingBuffer* fOwner;
+        size_t fLastHead;
+        size_t fGenID;
+    };
+    static void FinishSubmit(void*);
 
     GrGpu* fGpu;
     sk_sp<GrGpuBuffer> fCurrentBuffer;
diff --git a/src/gpu/GrStagingBufferManager.cpp b/src/gpu/GrStagingBufferManager.cpp
index e85f704..8c2d6e1 100644
--- a/src/gpu/GrStagingBufferManager.cpp
+++ b/src/gpu/GrStagingBufferManager.cpp
@@ -54,7 +54,7 @@
 void GrStagingBufferManager::detachBuffers() {
     for (size_t i = 0; i < fBuffers.size(); ++i) {
         fBuffers[i].fBuffer->unmap();
-        fGpu->takeOwnershipOfStagingBuffer(std::move(fBuffers[i].fBuffer));
+        fGpu->takeOwnershipOfBuffer(std::move(fBuffers[i].fBuffer));
     }
     fBuffers.clear();
 }
diff --git a/src/gpu/GrStagingBufferManager.h b/src/gpu/GrStagingBufferManager.h
index 514ec32..d2d78ed 100644
--- a/src/gpu/GrStagingBufferManager.h
+++ b/src/gpu/GrStagingBufferManager.h
@@ -30,9 +30,9 @@
     Slice allocateStagingBufferSlice(size_t size, size_t requiredAlignment = 1);
 
     // This call is used to move all the buffers off of the manager and to backend gpu by calling
-    // the virtual GrGpu::takeOwnershipOfStagingBuffer on each buffer. This is called during
+    // the virtual GrGpu::takeOwnershipOfBuffer on each buffer. This is called during
     // submitToGpu. It is up to the backend to take refs to the buffers in their implemented
-    // takeOwnershipOfStagingBuffer implementation if they need to. After this call returns the
+    // takeOwnershipOfBuffer implementation if they need to. After this call returns the
     // manager will have released all refs to its buffers.
     void detachBuffers();
 
diff --git a/src/gpu/d3d/GrD3DCommandList.cpp b/src/gpu/d3d/GrD3DCommandList.cpp
index 6a5de21..bc607b3 100644
--- a/src/gpu/d3d/GrD3DCommandList.cpp
+++ b/src/gpu/d3d/GrD3DCommandList.cpp
@@ -223,7 +223,6 @@
     , fCurrentInstanceBuffer(nullptr)
     , fCurrentInstanceStride(0)
     , fCurrentIndexBuffer(nullptr)
-    , fCurrentConstantRingBuffer(nullptr)
     , fCurrentConstantBufferAddress(0)
     , fCurrentSRVCRVDescriptorHeap(nullptr)
     , fCurrentSamplerDescriptorHeap(nullptr) {
@@ -238,10 +237,6 @@
     fCurrentInstanceBuffer = nullptr;
     fCurrentInstanceStride = 0;
     fCurrentIndexBuffer = nullptr;
-    if (fCurrentConstantRingBuffer) {
-        fCurrentConstantRingBuffer->finishSubmit(fConstantRingBufferSubmitData);
-        fCurrentConstantRingBuffer = nullptr;
-    }
     fCurrentConstantBufferAddress = 0;
     sk_bzero(fCurrentRootDescriptorTable, sizeof(fCurrentRootDescriptorTable));
     fCurrentSRVCRVDescriptorHeap = nullptr;
@@ -257,18 +252,6 @@
     }
 }
 
-void GrD3DDirectCommandList::setCurrentConstantBuffer(GrRingBuffer* constantsRingBuffer) {
-    fCurrentConstantRingBuffer = constantsRingBuffer;
-    if (fCurrentConstantRingBuffer) {
-        constantsRingBuffer->startSubmit(&fConstantRingBufferSubmitData);
-        for (unsigned int i = 0; i < fConstantRingBufferSubmitData.fTrackedBuffers.size(); ++i) {
-            this->addGrBuffer(std::move(fConstantRingBufferSubmitData.fTrackedBuffers[i]));
-        }
-        // we don't need these any more so clear this copy out
-        fConstantRingBufferSubmitData.fTrackedBuffers.clear();
-    }
-}
-
 void GrD3DDirectCommandList::setStencilRef(unsigned int stencilRef) {
     SkASSERT(fIsActive);
     fCommandList->OMSetStencilRef(stencilRef);
diff --git a/src/gpu/d3d/GrD3DCommandList.h b/src/gpu/d3d/GrD3DCommandList.h
index 41e4672..3293fb6 100644
--- a/src/gpu/d3d/GrD3DCommandList.h
+++ b/src/gpu/d3d/GrD3DCommandList.h
@@ -152,8 +152,6 @@
 
     void setPipelineState(sk_sp<GrD3DPipelineState> pipelineState);
 
-    void setCurrentConstantBuffer(GrRingBuffer* constantsRingBuffer);
-
     void setStencilRef(unsigned int stencilRef);
     void setBlendFactor(const float blendFactor[4]);
     void setPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY primitiveTopology);
@@ -207,9 +205,6 @@
     size_t fCurrentInstanceStride;
     const GrBuffer* fCurrentIndexBuffer;
 
-    GrRingBuffer* fCurrentConstantRingBuffer;
-    GrRingBuffer::SubmitData fConstantRingBufferSubmitData;
-
     D3D12_GPU_VIRTUAL_ADDRESS fCurrentConstantBufferAddress;
     D3D12_GPU_DESCRIPTOR_HANDLE fCurrentRootDescriptorTable[GrD3DRootSignature::kParamIndexCount];
     const ID3D12DescriptorHeap* fCurrentSRVCRVDescriptorHeap;
diff --git a/src/gpu/d3d/GrD3DGpu.cpp b/src/gpu/d3d/GrD3DGpu.cpp
index 13fd18c..07b8a08 100644
--- a/src/gpu/d3d/GrD3DGpu.cpp
+++ b/src/gpu/d3d/GrD3DGpu.cpp
@@ -121,7 +121,7 @@
     SkASSERT(fCurrentDirectCommandList);
 
     // set up constant data
-    fCurrentDirectCommandList->setCurrentConstantBuffer(&fConstantsRingBuffer);
+    fConstantsRingBuffer.startSubmit(this);
 
     fResourceProvider.prepForSubmit();
 
@@ -1290,7 +1290,7 @@
     }
 }
 
-void GrD3DGpu::takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer> buffer) {
+void GrD3DGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
     fCurrentDirectCommandList->addGrBuffer(std::move(buffer));
 }
 
diff --git a/src/gpu/d3d/GrD3DGpu.h b/src/gpu/d3d/GrD3DGpu.h
index f790624..2b93b80 100644
--- a/src/gpu/d3d/GrD3DGpu.h
+++ b/src/gpu/d3d/GrD3DGpu.h
@@ -46,7 +46,7 @@
     GrD3DDirectCommandList* currentCommandList() const { return fCurrentDirectCommandList.get(); }
 
     GrStagingBufferManager* stagingBufferManager() override { return &fStagingBufferManager; }
-    void takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer>) override;
+    void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) override;
 
     // TODO: hoist up to GrGpu
     GrRingBuffer* constantsRingBuffer() { return &fConstantsRingBuffer; }
diff --git a/src/gpu/dawn/GrDawnGpu.cpp b/src/gpu/dawn/GrDawnGpu.cpp
index e368bd6..dbbc251 100644
--- a/src/gpu/dawn/GrDawnGpu.cpp
+++ b/src/gpu/dawn/GrDawnGpu.cpp
@@ -497,7 +497,7 @@
     }
 }
 
-void GrDawnGpu::takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer> buffer) {
+void GrDawnGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
     fSubmittedStagingBuffers.push_back(std::move(buffer));
 }
 
diff --git a/src/gpu/dawn/GrDawnGpu.h b/src/gpu/dawn/GrDawnGpu.h
index 47a2b38..e1c4c64 100644
--- a/src/gpu/dawn/GrDawnGpu.h
+++ b/src/gpu/dawn/GrDawnGpu.h
@@ -38,7 +38,7 @@
     void disconnect(DisconnectType) override;
 
     GrStagingBufferManager* stagingBufferManager() override { return &fStagingBufferManager; }
-    void takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer>) override;
+    void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) override;
 
     const wgpu::Device& device() const { return fDevice; }
     const wgpu::Queue&  queue() const { return fQueue; }
diff --git a/src/gpu/vk/GrVkGpu.cpp b/src/gpu/vk/GrVkGpu.cpp
index 3b70ea1..ba1fcca 100644
--- a/src/gpu/vk/GrVkGpu.cpp
+++ b/src/gpu/vk/GrVkGpu.cpp
@@ -2042,7 +2042,7 @@
     fResourceProvider.addFinishedProcToActiveCommandBuffers(std::move(finishedCallback));
 }
 
-void GrVkGpu::takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer> buffer) {
+void GrVkGpu::takeOwnershipOfBuffer(sk_sp<GrGpuBuffer> buffer) {
     this->currentCommandBuffer()->addGrBuffer(std::move(buffer));
 }
 
diff --git a/src/gpu/vk/GrVkGpu.h b/src/gpu/vk/GrVkGpu.h
index 2e71638..eca9279 100644
--- a/src/gpu/vk/GrVkGpu.h
+++ b/src/gpu/vk/GrVkGpu.h
@@ -50,7 +50,7 @@
     const GrVkCaps& vkCaps() const { return *fVkCaps; }
 
     GrStagingBufferManager* stagingBufferManager() override { return &fStagingBufferManager; }
-    void takeOwnershipOfStagingBuffer(sk_sp<GrGpuBuffer>) override;
+    void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) override;
 
     bool isDeviceLost() const override { return fDeviceIsLost; }
     void setDeviceLost() { fDeviceIsLost = true; }