add SkDrawPictureCallback optional parameter to drawPicture(), which can abort the picture drawing.

R=bsalomon@google.com

Review URL: https://codereview.chromium.org/14598023

git-svn-id: http://skia.googlecode.com/svn/trunk@9197 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h
index b2c2b62..83aee4b 100644
--- a/include/core/SkPicture.h
+++ b/include/core/SkPicture.h
@@ -15,6 +15,7 @@
 
 class SkBBoxHierarchy;
 class SkCanvas;
+class SkDrawPictureCallback;
 class SkPicturePlayback;
 class SkPictureRecord;
 class SkStream;
@@ -150,9 +151,9 @@
 
     /** Replays the drawing commands on the specified canvas. This internally
         calls endRecording() if that has not already been called.
-        @param surface the canvas receiving the drawing commands.
+        @param canvas the canvas receiving the drawing commands.
     */
-    void draw(SkCanvas* surface);
+    void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL);
 
     /** Return the width of the picture's recording canvas. This
         value reflects what was passed to setSize(), and does not necessarily
@@ -246,5 +247,22 @@
     SkCanvas*   fCanvas;
 };
 
+/**
+ *  Subclasses of this can be passed to canvas.drawPicture. During the drawing
+ *  of the picture, this callback will periodically be invoked. If its
+ *  abortDrawing() returns true, then picture playback will be interrupted.
+ *
+ *  The resulting drawing is undefined, as there is no guarantee how often the
+ *  callback will be invoked. If the abort happens inside some level of nested
+ *  calls to save(), restore will automatically be called to return the state
+ *  to the same level it was before the drawPicture call was made.
+ */
+class SkDrawPictureCallback {
+public:
+    SkDrawPictureCallback() {}
+    virtual ~SkDrawPictureCallback() {}
+    
+    virtual bool abortDrawing() = 0;
+};
 
 #endif
diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp
index 1ff5865..ab2faea 100644
--- a/src/core/SkPicture.cpp
+++ b/src/core/SkPicture.cpp
@@ -253,10 +253,10 @@
     SkASSERT(NULL == fRecord);
 }
 
-void SkPicture::draw(SkCanvas* surface) {
+void SkPicture::draw(SkCanvas* surface, SkDrawPictureCallback* callback) {
     this->endRecording();
     if (fPlayback) {
-        fPlayback->draw(*surface);
+        fPlayback->draw(*surface, callback);
     }
 }
 
diff --git a/src/core/SkPicturePlayback.cpp b/src/core/SkPicturePlayback.cpp
index d54a6c5..e1c9a3a 100644
--- a/src/core/SkPicturePlayback.cpp
+++ b/src/core/SkPicturePlayback.cpp
@@ -656,7 +656,7 @@
     return (DrawType) op;
 }
 
-void SkPicturePlayback::draw(SkCanvas& canvas) {
+void SkPicturePlayback::draw(SkCanvas& canvas, SkDrawPictureCallback* callback) {
 #ifdef ENABLE_TIME_DRAW
     SkAutoTime  at("SkPicture::draw", 50);
 #endif
@@ -706,12 +706,17 @@
 
     // Record this, so we can concat w/ it if we encounter a setMatrix()
     SkMatrix initialMatrix = canvas.getTotalMatrix();
+    int originalSaveCount = canvas.getSaveCount();
 
 #ifdef SK_BUILD_FOR_ANDROID
     fAbortCurrentPlayback = false;
 #endif
 
     while (!reader.eof()) {
+        if (callback && callback->abortDrawing()) {
+            canvas.restoreToCount(originalSaveCount);
+            return;
+        }
 #ifdef SK_BUILD_FOR_ANDROID
         if (fAbortCurrentPlayback) {
             return;
diff --git a/src/core/SkPicturePlayback.h b/src/core/SkPicturePlayback.h
index b0af694..8019001 100644
--- a/src/core/SkPicturePlayback.h
+++ b/src/core/SkPicturePlayback.h
@@ -66,7 +66,7 @@
 
     virtual ~SkPicturePlayback();
 
-    void draw(SkCanvas& canvas);
+    void draw(SkCanvas& canvas, SkDrawPictureCallback*);
 
     void serialize(SkWStream*, SkPicture::EncodeBitmap) const;