blob: 8fbeea369055a8a36e0e9fce3322f8e829c3cf23 [file] [log] [blame]
junova41d3c32014-10-30 11:44:19 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkRectPriv.h"
junova41d3c32014-10-30 11:44:19 -070014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
junova41d3c32014-10-30 11:44:19 -070016
17class PictureBBHTestBase {
18public:
19 PictureBBHTestBase(int playbackWidth, int playbackHeight,
20 int recordWidth, int recordHeight) {
21
22 fResultBitmap.allocN32Pixels(playbackWidth, playbackHeight);
23 fPictureWidth = recordWidth;
24 fPictureHeight = recordHeight;
25 }
26
27 virtual ~PictureBBHTestBase() { }
28
29 virtual void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) = 0;
30
31 void run(skiatest::Reporter* reporter) {
32 // No BBH
halcanary96fcdcc2015-08-27 07:41:13 -070033 this->run(nullptr, reporter);
junova41d3c32014-10-30 11:44:19 -070034
junova41d3c32014-10-30 11:44:19 -070035 // With an R-Tree
36 SkRTreeFactory RTreeFactory;
37 this->run(&RTreeFactory, reporter);
38 }
39
40private:
41 void run(SkBBHFactory* factory, skiatest::Reporter* reporter) {
42 SkCanvas playbackCanvas(fResultBitmap);
43 playbackCanvas.clear(SK_ColorGREEN);
44 SkPictureRecorder recorder;
mtklein7cc1a342014-11-20 08:01:09 -080045 SkCanvas* recordCanvas = recorder.beginRecording(SkIntToScalar(fPictureWidth),
46 SkIntToScalar(fPictureHeight),
47 factory);
junova41d3c32014-10-30 11:44:19 -070048 this->doTest(playbackCanvas, *recordCanvas);
reedca2622b2016-03-18 07:25:55 -070049 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
junova41d3c32014-10-30 11:44:19 -070050 playbackCanvas.drawPicture(picture);
51 REPORTER_ASSERT(reporter, SK_ColorGREEN == fResultBitmap.getColor(0, 0));
52 }
53
54 SkBitmap fResultBitmap;
55 int fPictureWidth, fPictureHeight;
56};
57
58// Test to verify the playback of an empty picture
mtklein49aabde2015-01-05 07:02:45 -080059//
junova41d3c32014-10-30 11:44:19 -070060class DrawEmptyPictureBBHTest : public PictureBBHTestBase {
61public:
62 DrawEmptyPictureBBHTest()
Brian Salomond3b65972017-03-22 12:05:03 -040063 : PictureBBHTestBase(2, 2, 1, 1) {}
64 ~DrawEmptyPictureBBHTest() override {}
junova41d3c32014-10-30 11:44:19 -070065
Brian Salomond3b65972017-03-22 12:05:03 -040066 void doTest(SkCanvas&, SkCanvas&) override {}
junova41d3c32014-10-30 11:44:19 -070067};
68
69// Test to verify the playback of a picture into a canvas that has
70// an empty clip.
71//
72class EmptyClipPictureBBHTest : public PictureBBHTestBase {
73public:
mtklein49aabde2015-01-05 07:02:45 -080074 EmptyClipPictureBBHTest()
Brian Salomond3b65972017-03-22 12:05:03 -040075 : PictureBBHTestBase(2, 2, 3, 3) {}
junova41d3c32014-10-30 11:44:19 -070076
mtklein36352bf2015-03-25 18:17:31 -070077 void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) override {
junova41d3c32014-10-30 11:44:19 -070078 // intersect with out of bounds rect -> empty clip.
reed73603f32016-09-20 08:42:38 -070079 playbackCanvas.clipRect(SkRect::MakeXYWH(10, 10, 1, 1));
junova41d3c32014-10-30 11:44:19 -070080 SkPaint paint;
reed73603f32016-09-20 08:42:38 -070081 recordingCanvas.drawRect(SkRect::MakeWH(3, 3), paint);
junova41d3c32014-10-30 11:44:19 -070082 }
83
Brian Salomond3b65972017-03-22 12:05:03 -040084 ~EmptyClipPictureBBHTest() override {}
junova41d3c32014-10-30 11:44:19 -070085};
86
87DEF_TEST(PictureBBH, reporter) {
88
89 DrawEmptyPictureBBHTest emptyPictureTest;
90 emptyPictureTest.run(reporter);
91
92 EmptyClipPictureBBHTest emptyClipPictureTest;
93 emptyClipPictureTest.run(reporter);
94}
Mike Klein1dd161c2017-04-07 10:46:39 -040095
Mike Klein738b80d2018-05-04 13:51:11 -040096DEF_TEST(PictureNegativeSpace, r) {
97 SkRTreeFactory factory;
98 SkPictureRecorder recorder;
99
100 SkRect cull = {-200,-200,+200,+200};
101
102 {
Mike Klein196e9fb2020-01-21 10:48:46 -0600103 sk_sp<SkBBoxHierarchy> bbh = factory();
104 auto canvas = recorder.beginRecording(cull, bbh);
Mike Klein738b80d2018-05-04 13:51:11 -0400105 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 Kleinad67c662018-05-07 10:56:40 -0400115 {
Mike Klein738b80d2018-05-04 13:51:11 -0400116 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}