blob: 64b09b1e0333b802d82bd4f739b890d34af76bbc [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
8#include "SkCanvas.h"
9#include "SkBBoxHierarchy.h"
10#include "SkPaint.h"
11#include "SkPicture.h"
12#include "SkPictureRecorder.h"
13
14#include "Test.h"
15
16class PictureBBHTestBase {
17public:
18 PictureBBHTestBase(int playbackWidth, int playbackHeight,
19 int recordWidth, int recordHeight) {
20
21 fResultBitmap.allocN32Pixels(playbackWidth, playbackHeight);
22 fPictureWidth = recordWidth;
23 fPictureHeight = recordHeight;
24 }
25
26 virtual ~PictureBBHTestBase() { }
27
28 virtual void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) = 0;
29
30 void run(skiatest::Reporter* reporter) {
31 // No BBH
halcanary96fcdcc2015-08-27 07:41:13 -070032 this->run(nullptr, reporter);
junova41d3c32014-10-30 11:44:19 -070033
junova41d3c32014-10-30 11:44:19 -070034 // With an R-Tree
35 SkRTreeFactory RTreeFactory;
36 this->run(&RTreeFactory, reporter);
37 }
38
39private:
40 void run(SkBBHFactory* factory, skiatest::Reporter* reporter) {
41 SkCanvas playbackCanvas(fResultBitmap);
42 playbackCanvas.clear(SK_ColorGREEN);
43 SkPictureRecorder recorder;
mtklein7cc1a342014-11-20 08:01:09 -080044 SkCanvas* recordCanvas = recorder.beginRecording(SkIntToScalar(fPictureWidth),
45 SkIntToScalar(fPictureHeight),
46 factory);
junova41d3c32014-10-30 11:44:19 -070047 this->doTest(playbackCanvas, *recordCanvas);
reedca2622b2016-03-18 07:25:55 -070048 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
junova41d3c32014-10-30 11:44:19 -070049 playbackCanvas.drawPicture(picture);
50 REPORTER_ASSERT(reporter, SK_ColorGREEN == fResultBitmap.getColor(0, 0));
51 }
52
53 SkBitmap fResultBitmap;
54 int fPictureWidth, fPictureHeight;
55};
56
57// Test to verify the playback of an empty picture
mtklein49aabde2015-01-05 07:02:45 -080058//
junova41d3c32014-10-30 11:44:19 -070059class DrawEmptyPictureBBHTest : public PictureBBHTestBase {
60public:
61 DrawEmptyPictureBBHTest()
Brian Salomond3b65972017-03-22 12:05:03 -040062 : PictureBBHTestBase(2, 2, 1, 1) {}
63 ~DrawEmptyPictureBBHTest() override {}
junova41d3c32014-10-30 11:44:19 -070064
Brian Salomond3b65972017-03-22 12:05:03 -040065 void doTest(SkCanvas&, SkCanvas&) override {}
junova41d3c32014-10-30 11:44:19 -070066};
67
68// Test to verify the playback of a picture into a canvas that has
69// an empty clip.
70//
71class EmptyClipPictureBBHTest : public PictureBBHTestBase {
72public:
mtklein49aabde2015-01-05 07:02:45 -080073 EmptyClipPictureBBHTest()
Brian Salomond3b65972017-03-22 12:05:03 -040074 : PictureBBHTestBase(2, 2, 3, 3) {}
junova41d3c32014-10-30 11:44:19 -070075
mtklein36352bf2015-03-25 18:17:31 -070076 void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) override {
junova41d3c32014-10-30 11:44:19 -070077 // intersect with out of bounds rect -> empty clip.
reed73603f32016-09-20 08:42:38 -070078 playbackCanvas.clipRect(SkRect::MakeXYWH(10, 10, 1, 1));
junova41d3c32014-10-30 11:44:19 -070079 SkPaint paint;
reed73603f32016-09-20 08:42:38 -070080 recordingCanvas.drawRect(SkRect::MakeWH(3, 3), paint);
junova41d3c32014-10-30 11:44:19 -070081 }
82
Brian Salomond3b65972017-03-22 12:05:03 -040083 ~EmptyClipPictureBBHTest() override {}
junova41d3c32014-10-30 11:44:19 -070084};
85
86DEF_TEST(PictureBBH, reporter) {
87
88 DrawEmptyPictureBBHTest emptyPictureTest;
89 emptyPictureTest.run(reporter);
90
91 EmptyClipPictureBBHTest emptyClipPictureTest;
92 emptyClipPictureTest.run(reporter);
93}
Mike Klein1dd161c2017-04-07 10:46:39 -040094
95DEF_TEST(RTreeMakeLargest, r) {
96 // A call to insert() with 2 or more rects and a bounds of SkRect::MakeLargest()
97 // used to fall into an infinite loop.
98
99 SkRTreeFactory factory;
100 std::unique_ptr<SkBBoxHierarchy> bbh{ factory(SkRect::MakeLargest()) };
101
102 SkRect rects[] = { {0,0, 10,10}, {5,5,15,15} };
103 bbh->insert(rects, SK_ARRAY_COUNT(rects));
104 REPORTER_ASSERT(r, bbh->getRootBound() == SkRect::MakeWH(15,15));
105}