Cleanup restore offsets at end of SkPicture recording

Review URL: http://codereview.appspot.com/6355099/
TEST=skia unit test CanvasTest, test step TwoClipOps
BUG=https://code.google.com/p/chromium/issues/detail?id=133432



git-svn-id: http://skia.googlecode.com/svn/trunk@4577 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp
index 3507845..58098e7 100644
--- a/src/core/SkPicture.cpp
+++ b/src/core/SkPicture.cpp
@@ -180,6 +180,7 @@
 void SkPicture::endRecording() {
     if (NULL == fPlayback) {
         if (NULL != fRecord) {
+            fRecord->endRecording();
             fPlayback = SkNEW_ARGS(SkPicturePlayback, (*fRecord));
             fRecord->unref();
             fRecord = NULL;
diff --git a/src/core/SkPictureRecord.cpp b/src/core/SkPictureRecord.cpp
index f961bc4..630a1bb 100644
--- a/src/core/SkPictureRecord.cpp
+++ b/src/core/SkPictureRecord.cpp
@@ -170,6 +170,14 @@
     }
 }
 
+void SkPictureRecord::endRecording() {
+    // clear any remaining unhandled restore offset placeholders
+    while (fRestoreOffsetStack.count()) {
+        this->fillRestoreOffsetPlaceholdersForCurrentStackLevel(0);
+        fRestoreOffsetStack.pop();
+    }
+}
+
 void SkPictureRecord::recordRestoreOffsetPlaceholder(SkRegion::Op op) {
     if (regionOpExpands(op)) {
         // Run back through any previous clip ops, and mark their offset to
diff --git a/src/core/SkPictureRecord.h b/src/core/SkPictureRecord.h
index 7e5f086..c970696 100644
--- a/src/core/SkPictureRecord.h
+++ b/src/core/SkPictureRecord.h
@@ -95,6 +95,8 @@
     }
 
     bool shouldFlattenPixels(const SkBitmap&) const;
+
+    void endRecording();
 private:
     struct PixelRefDictionaryEntry {
         uint32_t fKey; // SkPixelRef GenerationID.
diff --git a/tests/CanvasTest.cpp b/tests/CanvasTest.cpp
index 0677ce8..a1ca236 100644
--- a/tests/CanvasTest.cpp
+++ b/tests/CanvasTest.cpp
@@ -327,6 +327,18 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Complex test steps
 
+static void TwoClipOpsStep(SkCanvas* canvas, 
+                              skiatest::Reporter* reporter,
+                              CanvasTestStep* testStep) {
+    // This test exercises a functionality in SkPicture that leads to the
+    // recording of restore offset placeholders.  This test will trigger an 
+    // assertion at playback time if the placeholders are not properly
+    // filled when the recording ends.
+    canvas->clipRect(kTestRect);
+    canvas->clipRegion(kTestRegion);
+}
+TEST_STEP(TwoClipOps, TwoClipOpsStep);
+
 // exercise fix for http://code.google.com/p/skia/issues/detail?id=560
 // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
 static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,