blob: 44cf59a889e4ee802e31a475aa7c1452d2ec9217 [file] [log] [blame]
scroggo@google.comd614c6a2012-09-14 17:26:37 +00001/*
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"
reed@google.com21b519d2012-10-02 17:42:15 +00008#include "SkCanvas.h"
9#include "SkPaint.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000010#include "SkPicture.h"
reed@google.com21b519d2012-10-02 17:42:15 +000011#include "SkRandom.h"
scroggo@google.comd614c6a2012-09-14 17:26:37 +000012#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
reed@google.com21b519d2012-10-02 17:42:15 +000037static 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);
skia.committer@gmail.com52c24372012-10-03 02:01:13 +000076
reed@google.com21b519d2012-10-02 17:42:15 +000077 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
scroggo@google.comd614c6a2012-09-14 17:26:37 +000088static void TestPicture(skiatest::Reporter* reporter) {
89#ifdef SK_DEBUG
90 test_deleting_empty_playback();
91 test_serializing_empty_picture();
92#endif
reed@google.com21b519d2012-10-02 17:42:15 +000093 test_peephole(reporter);
scroggo@google.comd614c6a2012-09-14 17:26:37 +000094}
95
96#include "TestClassDef.h"
reed@google.com21b519d2012-10-02 17:42:15 +000097DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture)