scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 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" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
| 9 | #include "SkPaint.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 10 | #include "SkPicture.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 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. |
| 17 | static 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. |
| 28 | static 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.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 37 | static 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 | |
| 57 | static 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.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 76 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 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 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 88 | static void TestPicture(skiatest::Reporter* reporter) { |
| 89 | #ifdef SK_DEBUG |
| 90 | test_deleting_empty_playback(); |
| 91 | test_serializing_empty_picture(); |
| 92 | #endif |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 93 | test_peephole(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | #include "TestClassDef.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 97 | DEFINE_TESTCLASS("Pictures", PictureTestClass, TestPicture) |