Fix some bugs and performance issues with skiaserve

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1777203003

Review URL: https://codereview.chromium.org/1777203003
diff --git a/tools/debugger/SkDebugCanvas.cpp b/tools/debugger/SkDebugCanvas.cpp
index 7bb70c2..cf2652c 100644
--- a/tools/debugger/SkDebugCanvas.cpp
+++ b/tools/debugger/SkDebugCanvas.cpp
@@ -226,16 +226,10 @@
     }
    
     // If we have a GPU backend we can also visualize the batching information
-#if SK_SUPPORT_GPU
     GrAuditTrail* at = nullptr;
-    GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
-    if (rt && (fDrawGpuBatchBounds || m != -1)) {
-        GrContext* ctx = rt->getContext();
-        if (ctx) {
-            at = ctx->getAuditTrail();
-        }
+    if (fDrawGpuBatchBounds || m != -1) {
+        at = this->getAuditTrail(canvas);
     }
-#endif
 
     for (int i = 0; i <= index; i++) {
         if (i == index && fFilter) {
@@ -375,11 +369,9 @@
                 canvas->drawRect(batch.fBounds, paint);
             }
         }
-
-        at->fullReset();
     }
-    
 #endif
+    this->cleanupAuditTrail(canvas);
 }
 
 void SkDebugCanvas::deleteDrawCommandAt(int index) {
@@ -417,32 +409,55 @@
     return fCommandVector;
 }
 
-Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
+GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
+    GrAuditTrail* at = nullptr;
 #if SK_SUPPORT_GPU
     GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
-    GrAuditTrail* at = nullptr;
     if (rt) {
         GrContext* ctx = rt->getContext();
-        if(ctx) {
+        if (ctx) {
             at = ctx->getAuditTrail();
-
-            // loop over all of the commands and draw them, this is to collect reordering
-            // information
-            for (int i = 0; i < this->getSize() && i <= n; i++) {
-                GrAuditTrail::AutoCollectBatches enable(at, i);
-                fCommandVector[i]->execute(canvas);
-            }
-
-            // in case there is some kind of global reordering
-            {
-                GrAuditTrail::AutoEnable ae(at);
-                canvas->flush();
-            }
         }
     }
 #endif
+    return at;
+}
+
+void SkDebugCanvas::drawAndCollectBatches(int n, SkCanvas* canvas) {
+    GrAuditTrail* at = this->getAuditTrail(canvas);
+    if (at) {
+#if SK_SUPPORT_GPU
+        // loop over all of the commands and draw them, this is to collect reordering
+        // information
+        for (int i = 0; i < this->getSize() && i <= n; i++) {
+            GrAuditTrail::AutoCollectBatches enable(at, i);
+            fCommandVector[i]->execute(canvas);
+        }
+
+        // in case there is some kind of global reordering
+        {
+            GrAuditTrail::AutoEnable ae(at);
+            canvas->flush();
+        }
+#endif
+    }
+}
+
+void SkDebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
+    GrAuditTrail* at = this->getAuditTrail(canvas);
+    if (at) {
+#if SK_SUPPORT_GPU
+        GrAuditTrail::AutoEnable ae(at);
+        at->fullReset();
+#endif
+    }
+}
+
+Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
+    this->drawAndCollectBatches(n, canvas);
     
     // now collect json
+    GrAuditTrail* at = this->getAuditTrail(canvas);
     Json::Value result = Json::Value(Json::objectValue);
     result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
     Json::Value commands = Json::Value(Json::arrayValue);
@@ -460,16 +475,27 @@
         }
 #endif
     }
-#if SK_SUPPORT_GPU
-    if (at) {
-        GrAuditTrail::AutoEnable ae(at);
-        at->fullReset();
-    }
-#endif
+    this->cleanupAuditTrail(canvas);
     result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
     return result;
 }
 
+Json::Value SkDebugCanvas::toJSONBatchList(int n, SkCanvas* canvas) {
+    this->drawAndCollectBatches(n, canvas);
+
+    Json::Value parsedFromString;
+    GrAuditTrail* at = this->getAuditTrail(canvas);
+#if SK_SUPPORT_GPU
+    if (at) {
+        GrAuditTrail::AutoManageBatchList enable(at);
+        Json::Reader reader;
+        SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
+    }
+#endif
+    this->cleanupAuditTrail(canvas);
+    return parsedFromString;
+}
+
 void SkDebugCanvas::updatePaintFilterCanvas() {
     if (!fOverdrawViz && !fOverrideFilterQuality) {
         fPaintFilterCanvas.reset(nullptr);
diff --git a/tools/debugger/SkDebugCanvas.h b/tools/debugger/SkDebugCanvas.h
index 7379698..fa02874 100644
--- a/tools/debugger/SkDebugCanvas.h
+++ b/tools/debugger/SkDebugCanvas.h
@@ -19,6 +19,7 @@
 #include "SkTArray.h"
 #include "UrlDataManager.h"
 
+class GrAuditTrail;
 class SkNWayCanvas;
 
 class SK_API SkDebugCanvas : public SkCanvas {
@@ -160,6 +161,8 @@
      */
     Json::Value toJSON(UrlDataManager& urlDataManager, int n, SkCanvas*);
 
+    Json::Value toJSONBatchList(int n, SkCanvas*);
+
 ////////////////////////////////////////////////////////////////////////////////
 // Inherited from SkCanvas
 ////////////////////////////////////////////////////////////////////////////////
@@ -284,7 +287,11 @@
     void outputPointsCommon(const SkPoint* pts, int count);
     void outputScalar(SkScalar num);
 
+    GrAuditTrail* getAuditTrail(SkCanvas*);
+
     void updatePaintFilterCanvas();
+    void drawAndCollectBatches(int n, SkCanvas*);
+    void cleanupAuditTrail(SkCanvas*);
 
     typedef SkCanvas INHERITED;
 };