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
This commit is contained in:
commit-bot@chromium.org 2014-02-13 16:00:51 +00:00
Родитель 310ec8e32c
Коммит ea7d08e3bb
2 изменённых файлов: 62 добавлений и 3 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);