diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-02-13 16:00:51 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-02-13 16:00:51 +0000 |
commit | ea7d08e3bbc45a0a573c1ac06afa4452f8d7000a (patch) | |
tree | f9b81ad1760a3a7538e61a8f23f44a6b77081c18 | |
parent | 310ec8e32ca03757a9dec46be4f108efe01ba757 (diff) |
Add unit test for unbalanced save and restores in pictures.
R=reed@google.com
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/150653010
git-svn-id: http://skia.googlecode.com/svn/trunk@13430 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | include/core/SkCanvas.h | 8 | ||||
-rw-r--r-- | tests/PictureTest.cpp | 57 |
2 files changed, 62 insertions, 3 deletions
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h index 7235efb2f0..b1d415f724 100644 --- a/include/core/SkCanvas.h +++ b/include/core/SkCanvas.h @@ -302,13 +302,15 @@ public: virtual void restore(); /** Returns the number of matrix/clip states on the SkCanvas' private stack. - This will equal # save() calls - # restore() calls. + This will equal # save() calls - # restore() calls + 1. The save count on + a new canvas is 1. */ int getSaveCount() const; /** Efficient way to pop any calls to save() that happened after the save - count reached saveCount. It is an error for saveCount to be less than - getSaveCount() + count reached saveCount. It is an error for saveCount to be greater than + getSaveCount(). To pop all the way back to the initial matrix/clip context + pass saveCount == 1. @param saveCount The number of save() levels to restore from */ void restoreToCount(int saveCount); diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp index e540181134..89f0af43fb 100644 --- a/tests/PictureTest.cpp +++ b/tests/PictureTest.cpp @@ -680,6 +680,62 @@ static void rand_op(SkCanvas* canvas, SkRandom& rand) { } } +static void set_canvas_to_save_count_4(SkCanvas* canvas) { + canvas->restoreToCount(1); + canvas->save(); + canvas->save(); + canvas->save(); +} + +static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { + SkCanvas testCanvas(100, 100); + set_canvas_to_save_count_4(&testCanvas); + + REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); + + SkPaint paint; + SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); + + SkPicture extra_save_picture; + extra_save_picture.beginRecording(100, 100); + extra_save_picture.getRecordingCanvas()->save(); + extra_save_picture.getRecordingCanvas()->translate(10, 10); + extra_save_picture.getRecordingCanvas()->drawRect(rect, paint); + extra_save_picture.getRecordingCanvas()->save(); + extra_save_picture.getRecordingCanvas()->translate(10, 10); + extra_save_picture.getRecordingCanvas()->drawRect(rect, paint); + + testCanvas.drawPicture(extra_save_picture); + REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); + + set_canvas_to_save_count_4(&testCanvas); + + SkPicture extra_restore_picture; + extra_restore_picture.beginRecording(100, 100); + extra_restore_picture.getRecordingCanvas()->save(); + extra_restore_picture.getRecordingCanvas()->translate(10, 10); + extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint); + extra_restore_picture.getRecordingCanvas()->save(); + extra_restore_picture.getRecordingCanvas()->translate(10, 10); + extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint); + extra_restore_picture.getRecordingCanvas()->restore(); + extra_restore_picture.getRecordingCanvas()->restore(); + extra_restore_picture.getRecordingCanvas()->restore(); + extra_restore_picture.getRecordingCanvas()->restore(); + + testCanvas.drawPicture(extra_save_picture); + REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); + + SkPicture no_save_picture; + extra_restore_picture.beginRecording(100, 100); + extra_restore_picture.getRecordingCanvas()->translate(10, 10); + extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint); + + testCanvas.drawPicture(extra_save_picture); + REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); + REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); +} + static void test_peephole() { SkRandom rand; @@ -1021,6 +1077,7 @@ DEF_TEST(Picture, reporter) { #else test_bad_bitmap(); #endif + test_unbalanced_save_restores(reporter); test_peephole(); test_gatherpixelrefs(reporter); test_gatherpixelrefsandrects(reporter); |