Render batch bounds as stroke rects

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

Review URL: https://codereview.chromium.org/1745063002
diff --git a/include/private/GrAuditTrail.h b/include/private/GrAuditTrail.h
index 66e578c..57e0fea 100644
--- a/include/private/GrAuditTrail.h
+++ b/include/private/GrAuditTrail.h
@@ -125,6 +125,19 @@
 
     void setClientID(int clientID) { fClientID = clientID; }
 
+    // We could just return our internal bookkeeping struct if copying the data out becomes
+    // a performance issue, but until then its nice to decouple
+    struct BatchInfo {
+        SkRect fBounds;
+        struct Batch {
+            int fClientID;
+            SkRect fBounds;
+        };
+        SkTArray<Batch> fBatches;
+    };
+
+    void getBoundsByClientID(SkTArray<BatchInfo>* outInfo, int clientID);
+
     void fullReset() {
         SkASSERT(fEnabled);
         fBatchList.reset();
diff --git a/src/gpu/GrAuditTrail.cpp b/src/gpu/GrAuditTrail.cpp
index 4b90ea5..f17ada4 100644
--- a/src/gpu/GrAuditTrail.cpp
+++ b/src/gpu/GrAuditTrail.cpp
@@ -40,6 +40,40 @@
     fBatchList.emplace_back(batchNode);
 }
 
+void GrAuditTrail::getBoundsByClientID(SkTArray<BatchInfo>* outInfo, int clientID) {
+    Batches** batchesLookup = fClientIDLookup.find(clientID);
+    if (batchesLookup) {
+        // We track which batchlistID we're currently looking at.  If it changes, then we
+        // need to push back a new batch info struct.  We happen to know that batches are
+        // in sequential order in the batchlist, otherwise we'd have to do more bookkeeping
+        int currentBatchListID = kGrAuditTrailInvalidID;
+        for (int i = 0; i < (*batchesLookup)->count(); i++) {
+            const Batch* batch = (**batchesLookup)[i];
+
+            // Because we will copy out all of the batches associated with a given
+            // batch list id everytime the id changes, we only have to update our struct
+            // when the id changes.
+            if (kGrAuditTrailInvalidID == currentBatchListID ||
+                batch->fBatchListID != currentBatchListID) {
+                BatchInfo& outBatchInfo = outInfo->push_back();
+                currentBatchListID = batch->fBatchListID;
+                
+                // copy out all of the batches so the client can display them even if
+                // they have a different clientID
+                const BatchNode* bn = fBatchList[currentBatchListID];
+                outBatchInfo.fBounds = bn->fBounds;
+                for (int j = 0; j < bn->fChildren.count(); j++) {
+                    BatchInfo::Batch& outBatch = outBatchInfo.fBatches.push_back();
+                    const Batch* currentBatch = bn->fChildren[j];
+                    outBatch.fBounds = currentBatch->fBounds;
+                    outBatch.fClientID = currentBatch->fClientID;
+                }
+            }
+        }
+    }
+}
+
+
 template <typename T>
 void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const T& array,
                                  bool addComma) {
diff --git a/tools/debugger/SkDebugCanvas.cpp b/tools/debugger/SkDebugCanvas.cpp
index cc1733b..4fc6d14 100644
--- a/tools/debugger/SkDebugCanvas.cpp
+++ b/tools/debugger/SkDebugCanvas.cpp
@@ -13,6 +13,12 @@
 #include "SkPaintFilterCanvas.h"
 #include "SkOverdrawMode.h"
 
+#if SK_SUPPORT_GPU
+#include "GrAuditTrail.h"
+#include "GrContext.h"
+#include "GrRenderTarget.h"
+#endif
+
 #define SKDEBUGCANVAS_VERSION            1
 #define SKDEBUGCANVAS_ATTRIBUTE_VERSION  "version"
 #define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS "commands"
@@ -68,7 +74,8 @@
         , fOverdrawViz(false)
         , fOverrideFilterQuality(false)
         , fFilterQuality(kNone_SkFilterQuality)
-        , fClipVizColor(SK_ColorTRANSPARENT) {
+        , fClipVizColor(SK_ColorTRANSPARENT)
+        , fDrawGpuBatchBounds(true) {
     fUserMatrix.reset();
 
     // SkPicturePlayback uses the base-class' quickReject calls to cull clipped
@@ -209,16 +216,36 @@
     if (fPaintFilterCanvas) {
         fPaintFilterCanvas->addCanvas(canvas);
         canvas = fPaintFilterCanvas.get();
+    
     }
 
     if (fMegaVizMode) {
         this->markActiveCommands(index);
     }
+   
+    // 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) {
+        GrContext* ctx = rt->getContext();
+        if (ctx) {
+            at = ctx->getAuditTrail();
+        }
+    }
+#endif
 
     for (int i = 0; i <= index; i++) {
         if (i == index && fFilter) {
             canvas->clear(0xAAFFFFFF);
         }
+   
+#if SK_SUPPORT_GPU
+        GrAuditTrail::AutoCollectBatches* acb = nullptr;
+        if (at) {
+            acb = new GrAuditTrail::AutoCollectBatches(at, i);
+        }
+#endif
 
         if (fCommandVector[i]->isVisible()) {
             if (fMegaVizMode && fCommandVector[i]->active()) {
@@ -232,6 +259,11 @@
                 fCommandVector[i]->execute(canvas);
             }
         }
+#if SK_SUPPORT_GPU
+        if (at && acb) {
+            delete acb;
+        }
+#endif
     }
 
     if (SkColorGetA(fClipVizColor) != 0) {
@@ -294,6 +326,34 @@
     if (fPaintFilterCanvas) {
         fPaintFilterCanvas->removeAll();
     }
+
+#if SK_SUPPORT_GPU
+    // draw any batches if required and issue a full reset onto GrAuditTrail
+    if (at) {
+        GrAuditTrail::AutoEnable ae(at);
+        SkTArray<GrAuditTrail::BatchInfo> childrenBounds;
+        at->getBoundsByClientID(&childrenBounds, index);
+        SkPaint paint;
+        paint.setStyle(SkPaint::kStroke_Style);
+        paint.setStrokeWidth(1);
+        for (int i = 0; i < childrenBounds.count(); i++) {
+            paint.setColor(SK_ColorBLACK);
+            canvas->drawRect(childrenBounds[i].fBounds, paint);
+            for (int j = 0; j < childrenBounds[i].fBatches.count(); j++) {
+                const GrAuditTrail::BatchInfo::Batch& batch = childrenBounds[i].fBatches[j];
+                if (batch.fClientID != index) {
+                    paint.setColor(SK_ColorBLUE);
+                } else {
+                    paint.setColor(SK_ColorRED);
+                }
+                canvas->drawRect(batch.fBounds, paint);
+            }
+        }
+
+        at->fullReset();
+    }
+    
+#endif
 }
 
 void SkDebugCanvas::deleteDrawCommandAt(int index) {
diff --git a/tools/debugger/SkDebugCanvas.h b/tools/debugger/SkDebugCanvas.h
index d06637d..9549cb0 100644
--- a/tools/debugger/SkDebugCanvas.h
+++ b/tools/debugger/SkDebugCanvas.h
@@ -43,6 +43,10 @@
     void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
     SkColor getClipVizColor() const { return fClipVizColor; }
 
+    void setDrawGpuBatchBounds(bool drawGpuBatchBounds) { 
+        fDrawGpuBatchBounds = drawGpuBatchBounds; 
+    }
+
     bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
 
     void setPicture(SkPicture* picture) { fPicture = picture; }
@@ -245,6 +249,7 @@
     bool fOverrideFilterQuality;
     SkFilterQuality fFilterQuality;
     SkColor fClipVizColor;
+    bool fDrawGpuBatchBounds;
 
     SkAutoTUnref<SkNWayCanvas> fPaintFilterCanvas;
 
diff --git a/tools/skiaserve/skiaserve.cpp b/tools/skiaserve/skiaserve.cpp
index b593e72..195544e 100644
--- a/tools/skiaserve/skiaserve.cpp
+++ b/tools/skiaserve/skiaserve.cpp
@@ -39,6 +39,7 @@
         fHandlers.push_back(new DataHandler);
         fHandlers.push_back(new BreakHandler);
         fHandlers.push_back(new BatchesHandler);
+        fHandlers.push_back(new BatchBoundsHandler);
     }
 
     ~UrlManager() {
diff --git a/tools/skiaserve/urlhandlers/BatchBoundsHandler.cpp b/tools/skiaserve/urlhandlers/BatchBoundsHandler.cpp
new file mode 100644
index 0000000..4044986
--- /dev/null
+++ b/tools/skiaserve/urlhandlers/BatchBoundsHandler.cpp
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "UrlHandler.h"
+
+#include "microhttpd.h"
+#include "../Request.h"
+#include "../Response.h"
+
+using namespace Response;
+
+bool BatchBoundsHandler::canHandle(const char* method, const char* url) {
+    static const char* kBasePath = "/batchBounds/";
+    return 0 == strcmp(method, MHD_HTTP_METHOD_POST) &&
+           0 == strncmp(url, kBasePath, strlen(kBasePath));
+}
+
+int BatchBoundsHandler::handle(Request* request, MHD_Connection* connection,
+                               const char* url, const char* method,
+                               const char* upload_data, size_t* upload_data_size) {
+    SkTArray<SkString> commands;
+    SkStrSplit(url, "/", &commands);
+
+    if (!request->hasPicture() || commands.count() != 2) {
+        return MHD_NO;
+    }
+
+    int enabled;
+    sscanf(commands[1].c_str(), "%d", &enabled);
+
+    request->fDebugCanvas->setDrawGpuBatchBounds(enabled);
+    return SendOK(connection);
+}
+
diff --git a/tools/skiaserve/urlhandlers/UrlHandler.h b/tools/skiaserve/urlhandlers/UrlHandler.h
index 28d378a..3fe269a 100644
--- a/tools/skiaserve/urlhandlers/UrlHandler.h
+++ b/tools/skiaserve/urlhandlers/UrlHandler.h
@@ -112,6 +112,17 @@
                const char* upload_data, size_t* upload_data_size) override;
 };
 
+/*
+ * Enables drawing of batch bounds
+ */
+class BatchBoundsHandler : public UrlHandler {
+public:
+    bool canHandle(const char* method, const char* url) override;
+    int handle(Request* request, MHD_Connection* connection,
+               const char* url, const char* method,
+               const char* upload_data, size_t* upload_data_size) override;
+};
+
 class RootHandler : public UrlHandler {
 public:
     bool canHandle(const char* method, const char* url) override;