Add a writer class for indirect draw commands

This enforces write-only access to the mapped buffers, will enable
chaining of indirect strokes, and gives us the ability to reorder the
fields for Metal.

Bug: chromium:1172543
Bug: skia:11291
Bug: skia:10419
Change-Id: I4449ff85dd0019f6d6d6781ede52bcf26dee8b02
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/367416
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/gpu/GrOpsRenderPass.cpp b/src/gpu/GrOpsRenderPass.cpp
index e48c14c..88468af 100644
--- a/src/gpu/GrOpsRenderPass.cpp
+++ b/src/gpu/GrOpsRenderPass.cpp
@@ -10,6 +10,7 @@
 #include "include/core/SkRect.h"
 #include "src/gpu/GrCaps.h"
 #include "src/gpu/GrCpuBuffer.h"
+#include "src/gpu/GrDrawIndirectCommand.h"
 #include "src/gpu/GrGpu.h"
 #include "src/gpu/GrPrimitiveProcessor.h"
 #include "src/gpu/GrProgramInfo.h"
@@ -284,13 +285,13 @@
     if (!this->gpu()->caps()->nativeDrawIndirectSupport()) {
         // Polyfill indirect draws with looping instanced calls.
         SkASSERT(drawIndirectBuffer->isCpuBuffer());
-        auto cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer);
-        auto cmd = reinterpret_cast<const GrDrawIndirectCommand*>(
+        auto* cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer);
+        auto* cmds = reinterpret_cast<const GrDrawIndirectCommand*>(
                 cpuIndirectBuffer->data() + bufferOffset);
-        auto end = cmd + drawCount;
-        for (; cmd != end; ++cmd) {
-            this->onDrawInstanced(cmd->fInstanceCount, cmd->fBaseInstance, cmd->fVertexCount,
-                                  cmd->fBaseVertex);
+        for (int i = 0; i < drawCount; ++i) {
+            // TODO: SkASSERT(caps.drawIndirectSignature() == standard);
+            auto [vertexCount, instanceCount, baseVertex, baseInstance] = cmds[i];
+            this->onDrawInstanced(instanceCount, baseInstance, vertexCount, baseVertex);
         }
         return;
     }
@@ -312,13 +313,14 @@
         this->gpu()->caps()->nativeDrawIndexedIndirectIsBroken()) {
         // Polyfill indexedIndirect draws with looping indexedInstanced calls.
         SkASSERT(drawIndirectBuffer->isCpuBuffer());
-        auto cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer);
-        auto cmd = reinterpret_cast<const GrDrawIndexedIndirectCommand*>(
+        auto* cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer);
+        auto* cmds = reinterpret_cast<const GrDrawIndexedIndirectCommand*>(
                 cpuIndirectBuffer->data() + bufferOffset);
-        auto end = cmd + drawCount;
-        for (; cmd != end; ++cmd) {
-            this->onDrawIndexedInstanced(cmd->fIndexCount, cmd->fBaseIndex, cmd->fInstanceCount,
-                                         cmd->fBaseInstance, cmd->fBaseVertex);
+        for (int i = 0; i < drawCount; ++i) {
+            // TODO: SkASSERT(caps.drawIndirectSignature() == standard);
+            auto [indexCount, instanceCount, baseIndex, baseVertex, baseInstance] = cmds[i];
+            this->onDrawIndexedInstanced(indexCount, baseIndex, instanceCount, baseInstance,
+                                         baseVertex);
         }
         return;
     }