Fixed up SkTimedPicturePlayback

This was more motivated to remove the preDraw and postDraw virtuals from SkPicturePlayback.

R=mtklein@google.com, reed@google.com

Author: robertphillips@google.com

Review URL: https://codereview.chromium.org/375943002
diff --git a/debugger/QT/SkDebuggerGUI.cpp b/debugger/QT/SkDebuggerGUI.cpp
index b6920a7..1a324fd 100644
--- a/debugger/QT/SkDebuggerGUI.cpp
+++ b/debugger/QT/SkDebuggerGUI.cpp
@@ -156,9 +156,9 @@
     }
 }
 
-// The timed picture playback uses the SkPictureData's profiling stubs
-// to time individual commands. The offsets are needed to map SkPicture
-// offsets to individual commands.
+// The timed picture playback just steps through every operation timing
+// each one individually. Note that each picture should be replayed multiple 
+// times (via calls to 'draw') before each command's time is accessed via 'time'.
 class SkTimedPicturePlayback : public SkPicturePlayback {
 public:
 
@@ -172,6 +172,47 @@
         this->resetTimes();
     }
 
+    virtual void draw(SkCanvas* canvas, SkDrawPictureCallback* callback) SK_OVERRIDE {
+        AutoResetOpID aroi(this);
+        SkASSERT(0 == fCurOffset);
+
+        SkReader32 reader(fPictureData->opData()->bytes(), fPictureData->opData()->size());
+
+        // Record this, so we can concat w/ it if we encounter a setMatrix()
+        SkMatrix initialMatrix = canvas->getTotalMatrix();
+
+        SkAutoCanvasRestore acr(canvas, false);
+
+        int opIndex = -1;
+
+        while (!reader.eof()) {
+            if (NULL != callback && callback->abortDrawing()) {
+                return;
+            }
+
+            fCurOffset = reader.offset();
+            uint32_t size;
+            DrawType op = ReadOpAndSize(&reader, &size);
+            if (NOOP == op) {
+                // NOOPs are to be ignored - do not propagate them any further
+                reader.setOffset(fCurOffset + size);
+                continue;
+            }
+
+            opIndex++;
+
+            if (this->preDraw(opIndex, op)) {
+                // This operation is disabled in the debugger's GUI
+                reader.setOffset(fCurOffset + size);
+                continue;
+            }
+
+            this->handleOp(&reader, op, size, canvas, initialMatrix);
+
+            this->postDraw(opIndex);
+        }
+    }
+
     void resetTimes() {
         for (int i = 0; i < fTimes.count(); ++i) {
             fTimes[i] = 0.0;
@@ -184,6 +225,7 @@
 
     int count() const { return fTimes.count(); }
 
+    // Return the fraction of the total time consumed by the index-th operation
     double time(int index) const { return fTimes[index] / fTot; }
 
     const SkTDArray<double>* typeTimes() const { return &fTypeTimes; }
@@ -196,11 +238,11 @@
     SkTDArray<double> fTimes;   // sum of time consumed for each command
     SkTDArray<double> fTypeTimes; // sum of time consumed for each type of command (e.g., drawPath)
     double fTot;                // total of all times in 'fTimes'
+
     int fCurType;
     int fCurCommand;            // the current command being executed/timed
 
-#ifdef SK_DEVELOPER
-    virtual bool preDraw(int opIndex, int type) SK_OVERRIDE {
+    bool preDraw(int opIndex, int type) {
         fCurCommand = opIndex;
 
         if (fSkipCommands[fCurCommand]) {
@@ -227,7 +269,7 @@
         return false;
     }
 
-    virtual void postDraw(int opIndex) SK_OVERRIDE {
+    void postDraw(int opIndex) {
 #if defined(SK_BUILD_FOR_WIN32)
         // CPU timer doesn't work well on Windows
         double time = fTimer.endWall();
@@ -242,7 +284,6 @@
         fTypeTimes[fCurType] += time;
         fTot += time;
     }
-#endif
 
 private:
     typedef SkPicturePlayback INHERITED;
diff --git a/src/core/SkPicturePlayback.cpp b/src/core/SkPicturePlayback.cpp
index d8ed082..734a54a 100644
--- a/src/core/SkPicturePlayback.cpp
+++ b/src/core/SkPicturePlayback.cpp
@@ -152,10 +152,6 @@
 
     SkAutoCanvasRestore acr(canvas, false);
 
-#ifdef SK_DEVELOPER
-    int opIndex = -1;
-#endif
-
     while (!reader.eof()) {
         if (callback && callback->abortDrawing()) {
             return;
@@ -232,13 +228,6 @@
         if (NOOP == op) {
             // NOOPs are to be ignored - do not propagate them any further
             skipTo = fCurOffset + size;
-#ifdef SK_DEVELOPER
-        } else {
-            opIndex++;
-            if (this->preDraw(opIndex, op)) {
-                skipTo = fCurOffset + size;
-            }
-#endif
         }
 
         if (0 != skipTo) {
@@ -260,10 +249,6 @@
 
         this->handleOp(&reader, op, size, canvas, initialMatrix);
 
-#ifdef SK_DEVELOPER
-        this->postDraw(opIndex);
-#endif
-
         if (it.isValid()) {
             uint32_t skipTo = it.nextDraw();
             if (kDrawComplete == skipTo) {
diff --git a/src/core/SkPicturePlayback.h b/src/core/SkPicturePlayback.h
index 7966670..4248e2d 100644
--- a/src/core/SkPicturePlayback.h
+++ b/src/core/SkPicturePlayback.h
@@ -96,11 +96,6 @@
                   SkCanvas* canvas,
                   const SkMatrix& initialMatrix);
 
-#ifdef SK_DEVELOPER
-    virtual bool preDraw(int opIndex, int type) { return false; }
-    virtual void postDraw(int opIndex) { }
-#endif
-
     static DrawType ReadOpAndSize(SkReader32* reader, uint32_t* size);
 
     class AutoResetOpID {
diff --git a/src/core/SkPictureRangePlayback.cpp b/src/core/SkPictureRangePlayback.cpp
index 16473db..b5e36d6 100644
--- a/src/core/SkPictureRangePlayback.cpp
+++ b/src/core/SkPictureRangePlayback.cpp
@@ -29,7 +29,7 @@
     SkAutoCanvasRestore acr(canvas, false);
 
     while (!reader.eof()) {
-        if (callback && callback->abortDrawing()) {
+        if (NULL != callback && callback->abortDrawing()) {
             return;
         }