junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkPicture.h" |
| 12 | #include "include/core/SkPictureRecorder.h" |
| 13 | #include "src/core/SkBBoxHierarchy.h" |
| 14 | #include "src/core/SkRectPriv.h" |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "tests/Test.h" |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 17 | |
| 18 | class PictureBBHTestBase { |
| 19 | public: |
| 20 | PictureBBHTestBase(int playbackWidth, int playbackHeight, |
| 21 | int recordWidth, int recordHeight) { |
| 22 | |
| 23 | fResultBitmap.allocN32Pixels(playbackWidth, playbackHeight); |
| 24 | fPictureWidth = recordWidth; |
| 25 | fPictureHeight = recordHeight; |
| 26 | } |
| 27 | |
| 28 | virtual ~PictureBBHTestBase() { } |
| 29 | |
| 30 | virtual void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) = 0; |
| 31 | |
| 32 | void run(skiatest::Reporter* reporter) { |
| 33 | // No BBH |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | this->run(nullptr, reporter); |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 35 | |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 36 | // With an R-Tree |
| 37 | SkRTreeFactory RTreeFactory; |
| 38 | this->run(&RTreeFactory, reporter); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | void run(SkBBHFactory* factory, skiatest::Reporter* reporter) { |
| 43 | SkCanvas playbackCanvas(fResultBitmap); |
| 44 | playbackCanvas.clear(SK_ColorGREEN); |
| 45 | SkPictureRecorder recorder; |
mtklein | 7cc1a34 | 2014-11-20 08:01:09 -0800 | [diff] [blame] | 46 | SkCanvas* recordCanvas = recorder.beginRecording(SkIntToScalar(fPictureWidth), |
| 47 | SkIntToScalar(fPictureHeight), |
| 48 | factory); |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 49 | this->doTest(playbackCanvas, *recordCanvas); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 50 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 51 | playbackCanvas.drawPicture(picture); |
| 52 | REPORTER_ASSERT(reporter, SK_ColorGREEN == fResultBitmap.getColor(0, 0)); |
| 53 | } |
| 54 | |
| 55 | SkBitmap fResultBitmap; |
| 56 | int fPictureWidth, fPictureHeight; |
| 57 | }; |
| 58 | |
| 59 | // Test to verify the playback of an empty picture |
mtklein | 49aabde | 2015-01-05 07:02:45 -0800 | [diff] [blame] | 60 | // |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 61 | class DrawEmptyPictureBBHTest : public PictureBBHTestBase { |
| 62 | public: |
| 63 | DrawEmptyPictureBBHTest() |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 64 | : PictureBBHTestBase(2, 2, 1, 1) {} |
| 65 | ~DrawEmptyPictureBBHTest() override {} |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 66 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 67 | void doTest(SkCanvas&, SkCanvas&) override {} |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // Test to verify the playback of a picture into a canvas that has |
| 71 | // an empty clip. |
| 72 | // |
| 73 | class EmptyClipPictureBBHTest : public PictureBBHTestBase { |
| 74 | public: |
mtklein | 49aabde | 2015-01-05 07:02:45 -0800 | [diff] [blame] | 75 | EmptyClipPictureBBHTest() |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 76 | : PictureBBHTestBase(2, 2, 3, 3) {} |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) override { |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 79 | // intersect with out of bounds rect -> empty clip. |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 80 | playbackCanvas.clipRect(SkRect::MakeXYWH(10, 10, 1, 1)); |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 81 | SkPaint paint; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 82 | recordingCanvas.drawRect(SkRect::MakeWH(3, 3), paint); |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 85 | ~EmptyClipPictureBBHTest() override {} |
junov | a41d3c3 | 2014-10-30 11:44:19 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | DEF_TEST(PictureBBH, reporter) { |
| 89 | |
| 90 | DrawEmptyPictureBBHTest emptyPictureTest; |
| 91 | emptyPictureTest.run(reporter); |
| 92 | |
| 93 | EmptyClipPictureBBHTest emptyClipPictureTest; |
| 94 | emptyClipPictureTest.run(reporter); |
| 95 | } |
Mike Klein | 1dd161c | 2017-04-07 10:46:39 -0400 | [diff] [blame] | 96 | |
Mike Klein | 738b80d | 2018-05-04 13:51:11 -0400 | [diff] [blame] | 97 | DEF_TEST(PictureNegativeSpace, r) { |
| 98 | SkRTreeFactory factory; |
| 99 | SkPictureRecorder recorder; |
| 100 | |
| 101 | SkRect cull = {-200,-200,+200,+200}; |
| 102 | |
| 103 | { |
| 104 | auto canvas = recorder.beginRecording(cull, &factory); |
| 105 | canvas->save(); |
| 106 | canvas->clipRect(cull); |
| 107 | canvas->drawRect({-20,-20,-10,-10}, SkPaint{}); |
| 108 | canvas->drawRect({-20,-20,-10,-10}, SkPaint{}); |
| 109 | canvas->restore(); |
| 110 | auto pic = recorder.finishRecordingAsPicture(); |
| 111 | REPORTER_ASSERT(r, pic->approximateOpCount() == 5); |
| 112 | REPORTER_ASSERT(r, pic->cullRect() == (SkRect{-20,-20,-10,-10})); |
| 113 | } |
| 114 | |
Mike Klein | ad67c66 | 2018-05-07 10:56:40 -0400 | [diff] [blame] | 115 | { |
Mike Klein | 738b80d | 2018-05-04 13:51:11 -0400 | [diff] [blame] | 116 | auto canvas = recorder.beginRecording(cull, &factory); |
| 117 | canvas->clipRect(cull); |
| 118 | canvas->drawRect({-20,-20,-10,-10}, SkPaint{}); |
| 119 | canvas->drawRect({-20,-20,-10,-10}, SkPaint{}); |
| 120 | auto pic = recorder.finishRecordingAsPicture(); |
| 121 | REPORTER_ASSERT(r, pic->approximateOpCount() == 3); |
| 122 | REPORTER_ASSERT(r, pic->cullRect() == (SkRect{-20,-20,-10,-10})); |
| 123 | } |
| 124 | } |