Fix SkiaServe gpu JSON to work with any reordering algorithm

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

Review URL: https://codereview.chromium.org/1761003004
diff --git a/tools/debugger/SkDebugCanvas.cpp b/tools/debugger/SkDebugCanvas.cpp
index a58db14..9915c1b 100644
--- a/tools/debugger/SkDebugCanvas.cpp
+++ b/tools/debugger/SkDebugCanvas.cpp
@@ -20,9 +20,10 @@
 #include "SkGpuDevice.h"
 #endif
 
-#define SKDEBUGCANVAS_VERSION            1
-#define SKDEBUGCANVAS_ATTRIBUTE_VERSION  "version"
-#define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS "commands"
+#define SKDEBUGCANVAS_VERSION                     1
+#define SKDEBUGCANVAS_ATTRIBUTE_VERSION           "version"
+#define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS          "commands"
+#define SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL        "auditTrail"
 
 class DebugPaintFilterCanvas : public SkPaintFilterCanvas {
 public:
@@ -331,6 +332,10 @@
 #if SK_SUPPORT_GPU
     // draw any batches if required and issue a full reset onto GrAuditTrail
     if (at) {
+        // just in case there is global reordering, we flush the canvas before querying
+        // GrAuditTrail
+        canvas->flush();
+
         // we pick three colorblind-safe colors, 75% alpha
         static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
         static const SkColor kOpBatchBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
@@ -413,13 +418,50 @@
 }
 
 Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
+#if SK_SUPPORT_GPU
+    GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
+    GrAuditTrail* at = nullptr;
+    if (rt) {
+        GrContext* ctx = rt->getContext();
+        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
+            canvas->flush();
+        }
+    }
+#endif
+    
+    // now collect json
     Json::Value result = Json::Value(Json::objectValue);
     result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
     Json::Value commands = Json::Value(Json::arrayValue);
     for (int i = 0; i < this->getSize() && i <= n; i++) {
-        commands[i] = this->getDrawCommandAt(i)->drawToAndCollectJSON(canvas, urlDataManager,
-                                                                      i);
+        commands[i] = this->getDrawCommandAt(i)->toJSON(urlDataManager);
+#if SK_SUPPORT_GPU
+        if (at) {
+            // TODO if this is inefficient we could add a method to GrAuditTrail which takes
+            // a Json::Value and is only compiled in this file
+            Json::Value parsedFromString;
+            Json::Reader reader;
+            SkAssertResult(reader.parse(at->toJson(i).c_str(), parsedFromString));
+
+            commands[i][SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
+        }
+#endif
     }
+#if SK_SUPPORT_GPU
+    if (at) {
+        at->fullReset();
+    }
+#endif
     result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
     return result;
 }
diff --git a/tools/debugger/SkDrawCommand.cpp b/tools/debugger/SkDrawCommand.cpp
index 34cbee7..1790536 100644
--- a/tools/debugger/SkDrawCommand.cpp
+++ b/tools/debugger/SkDrawCommand.cpp
@@ -25,14 +25,8 @@
 #include "SkValidatingReadBuffer.h"
 #include "SkWriteBuffer.h"
 
-#if SK_SUPPORT_GPU
-#include "GrContext.h"
-#include "GrRenderTarget.h"
-#endif
-
 #define SKDEBUGCANVAS_ATTRIBUTE_COMMAND           "command"
 #define SKDEBUGCANVAS_ATTRIBUTE_VISIBLE           "visible"
-#define SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL        "auditTrail"
 #define SKDEBUGCANVAS_ATTRIBUTE_MATRIX            "matrix"
 #define SKDEBUGCANVAS_ATTRIBUTE_COORDS            "coords"
 #define SKDEBUGCANVAS_ATTRIBUTE_BOUNDS            "bounds"
@@ -227,37 +221,6 @@
     return result;
 }
 
-Json::Value SkDrawCommand::drawToAndCollectJSON(SkCanvas* canvas,
-                                                UrlDataManager& urlDataManager,
-                                                int opIndex) const {
-    Json::Value result = this->toJSON(urlDataManager);
-
-    SkASSERT(canvas);
-
-#if SK_SUPPORT_GPU
-    GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
-    if (rt) {
-        GrContext* ctx = rt->getContext();
-        if(ctx) {
-            GrAuditTrail* at = ctx->getAuditTrail();
-            GrAuditTrail::AutoCollectBatches enable(at, opIndex);
-            this->execute(canvas);
-
-            // TODO if this is inefficient we could add a method to GrAuditTrail which takes
-            // a Json::Value and is only compiled in this file
-            Json::Value parsedFromString;
-            Json::Reader reader;
-            SkDEBUGCODE(bool parsingSuccessful = )reader.parse(at->toJson(opIndex).c_str(),
-                                                               parsedFromString);
-            SkASSERT(parsingSuccessful);
-
-            result[SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
-        }
-    }
-#endif
-    return result;
-}
-
 #define INSTALL_FACTORY(name) factories.set(SkString(GetCommandString(k ## name ##_OpType)), \
                                             (FROM_JSON) Sk ## name ## Command::fromJSON)
 SkDrawCommand* SkDrawCommand::fromJSON(Json::Value& command, UrlDataManager& urlDataManager) {
diff --git a/tools/debugger/SkDrawCommand.h b/tools/debugger/SkDrawCommand.h
index a2835dc..44682b5 100644
--- a/tools/debugger/SkDrawCommand.h
+++ b/tools/debugger/SkDrawCommand.h
@@ -102,9 +102,6 @@
 
     virtual Json::Value toJSON(UrlDataManager& urlDataManager) const;
 
-    Json::Value drawToAndCollectJSON(SkCanvas*, UrlDataManager& urlDataManager,
-                                     int opIndex) const;
-
     /* Converts a JSON representation of a command into a newly-allocated SkDrawCommand object. It
      * is the caller's responsibility to delete this object. This method may return null if an error
      * occurs.
diff --git a/tools/skiaserve/skiaserve.cpp b/tools/skiaserve/skiaserve.cpp
index 0d099e7..889eaaf 100644
--- a/tools/skiaserve/skiaserve.cpp
+++ b/tools/skiaserve/skiaserve.cpp
@@ -87,12 +87,16 @@
     printf("Visit http://%s:%d in your browser.\n", FLAGS_address[0], FLAGS_port);
 
     struct MHD_Daemon* daemon;
-    // TODO Add option to bind this strictly to an address, e.g. localhost, for security.
-    daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, FLAGS_port, nullptr, nullptr,
+    daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY
+#ifdef SK_DEBUG
+                              | MHD_USE_DEBUG
+#endif  
+                              , FLAGS_port, nullptr, nullptr,
                               &answer_to_connection, &request,
                               MHD_OPTION_SOCK_ADDR, &address,
                               MHD_OPTION_END);
     if (NULL == daemon) {
+        SkDebugf("Could not initialize daemon\n");
         return 1;
     }