Revise GrRingBuffer to be more stable with Metal GPU capture.

After switching to use GrRingBuffer for Metal uniforms, it was no longer
possible to use GPU Capture properly -- the command buffer information
was not showing up.

I tracked this down to a few causes:
* Initially CurrentBuffer is NULL. We always added this in startSubmit()
to the TrackedBuffers list, which would then get passed to
Gpu->takeOwnershipOfBuffer().
* GrRingBuffer maintained its own ownership of CurrentBuffer while still
adding it to the TrackedBuffers list and hence to takeOwnershipOfBuffer()
* Even if no new allocation is created, we added SubmitData.

It's unclear why these were affecting GPU Capture, but addressing these
allows it to work now, and it's cleaner.

Bug: skia:12110
Change-Id: Ie947c635ea690f91e1862bc7f443115a2adacfdc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/419359
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/GrRingBuffer.cpp b/src/gpu/GrRingBuffer.cpp
index a64b799..d33a123 100644
--- a/src/gpu/GrRingBuffer.cpp
+++ b/src/gpu/GrRingBuffer.cpp
@@ -55,6 +55,7 @@
 }
 
 GrRingBuffer::Slice GrRingBuffer::suballocate(size_t size) {
+    fNewAllocation = true;
     if (fCurrentBuffer) {
         size_t offset = this->getAllocationOffset(size);
         if (offset < fTotalSize) {
@@ -63,13 +64,14 @@
 
         // Try to grow allocation (old allocation will age out).
         fTotalSize *= 2;
+        // Add current buffer to be tracked for next submit.
+        fPreviousBuffers.push_back(std::move(fCurrentBuffer));
     }
 
     GrResourceProvider* resourceProvider = fGpu->getContext()->priv().resourceProvider();
     fCurrentBuffer = resourceProvider->createBuffer(fTotalSize, fType, kDynamic_GrAccessPattern);
 
     SkASSERT(fCurrentBuffer);
-    fTrackedBuffers.push_back(fCurrentBuffer);
     fHead = 0;
     fTail = 0;
     fGenID++;
@@ -80,18 +82,20 @@
 
 // used when current command buffer/command list is submitted
 void GrRingBuffer::startSubmit(GrGpu* gpu) {
-    for (unsigned int i = 0; i < fTrackedBuffers.size(); ++i) {
-        gpu->takeOwnershipOfBuffer(std::move(fTrackedBuffers[i]));
+    for (unsigned int i = 0; i < fPreviousBuffers.size(); ++i) {
+        fPreviousBuffers[i]->unmap();
+        gpu->takeOwnershipOfBuffer(std::move(fPreviousBuffers[i]));
     }
-    fTrackedBuffers.clear();
-    // add current buffer to be tracked for next submit
-    fTrackedBuffers.push_back(fCurrentBuffer);
+    fPreviousBuffers.clear();
 
-    SubmitData* submitData = new SubmitData();
-    submitData->fOwner = this;
-    submitData->fLastHead = fHead;
-    submitData->fGenID = fGenID;
-    gpu->addFinishedProc(FinishSubmit, submitData);
+    if (fNewAllocation) {
+        SubmitData* submitData = new SubmitData();
+        submitData->fOwner = this;
+        submitData->fLastHead = fHead;
+        submitData->fGenID = fGenID;
+        gpu->addFinishedProc(FinishSubmit, submitData);
+        fNewAllocation = false;
+    }
 }
 
 // used when current command buffer/command list is completed