PictureTest.cpp revision 52c2437c73065094e183d875672c5c34aef34ba6
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "Test.h"
8#include "SkCanvas.h"
9#include "SkPaint.h"
10#include "SkPicture.h"
11#include "SkRandom.h"
12#include "SkStream.h"
13
14#ifdef SK_DEBUG
15// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
16// run in debug mode.
17static void test_deleting_empty_playback() {
18    SkPicture picture;
19    // Creates an SkPictureRecord
20    picture.beginRecording(0, 0);
21    // Turns that into an SkPicturePlayback
22    picture.endRecording();
23    // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
24    picture.beginRecording(0, 0);
25}
26
27// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
28static void test_serializing_empty_picture() {
29    SkPicture picture;
30    picture.beginRecording(0, 0);
31    picture.endRecording();
32    SkDynamicMemoryWStream stream;
33    picture.serialize(&stream);
34}
35#endif
36
37static void rand_op(SkCanvas* canvas, SkRandom& rand) {
38    SkPaint paint;
39    SkRect rect = SkRect::MakeWH(50, 50);
40
41    SkScalar unit = rand.nextUScalar1();
42    if (unit <= 0.3) {
43//        SkDebugf("save\n");
44        canvas->save();
45    } else if (unit <= 0.6) {
46//        SkDebugf("restore\n");
47        canvas->restore();
48    } else if (unit <= 0.9) {
49//        SkDebugf("clip\n");
50        canvas->clipRect(rect);
51    } else {
52//        SkDebugf("draw\n");
53        canvas->drawPaint(paint);
54    }
55}
56
57static void test_peephole(skiatest::Reporter* reporter) {
58    SkRandom rand;
59
60    for (int j = 0; j < 100; j++) {
61        SkRandom rand2(rand.getSeed()); // remember the seed
62
63        SkPicture picture;
64        SkCanvas* canvas = picture.beginRecording(100, 100);
65
66        for (int i = 0; i < 1000; ++i) {
67            rand_op(canvas, rand);
68        }
69        picture.endRecording();
70    }
71
72    {
73        SkPicture picture;
74        SkCanvas* canvas = picture.beginRecording(100, 100);
75        SkRect rect = SkRect::MakeWH(50, 50);
76
77        for (int i = 0; i < 100; ++i) {
78            canvas->save();
79        }
80        while (canvas->getSaveCount() > 1) {
81            canvas->clipRect(rect);
82            canvas->restore();
83        }
84        picture.endRecording();
85    }
86}
87
88static void TestPicture(skiatest::Reporter* reporter) {
89#ifdef SK_DEBUG
90    test_deleting_empty_playback();
91    test_serializing_empty_picture();
92#endif
93    test_peephole(reporter);
94}
95
96#include "TestClassDef.h"
97DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture)
98