Flutter Engine
The Flutter Engine
PictureBBHTest.cpp
Go to the documentation of this file.
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
15#include "include/core/SkRect.h"
18#include "tests/Test.h"
19
21public:
22 PictureBBHTestBase(int playbackWidth, int playbackHeight,
23 int recordWidth, int recordHeight) {
24
25 fResultBitmap.allocN32Pixels(playbackWidth, playbackHeight);
26 fPictureWidth = recordWidth;
27 fPictureHeight = recordHeight;
28 }
29
30 virtual ~PictureBBHTestBase() { }
31
32 virtual void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) = 0;
33
35 // No BBH
36 this->run(nullptr, reporter);
37
38 // With an R-Tree
39 SkRTreeFactory RTreeFactory;
40 this->run(&RTreeFactory, reporter);
41 }
42
43private:
45 SkCanvas playbackCanvas(fResultBitmap);
46 playbackCanvas.clear(SK_ColorGREEN);
47 SkPictureRecorder recorder;
48 SkCanvas* recordCanvas = recorder.beginRecording(SkIntToScalar(fPictureWidth),
49 SkIntToScalar(fPictureHeight),
50 factory);
51 this->doTest(playbackCanvas, *recordCanvas);
53 playbackCanvas.drawPicture(picture);
54 REPORTER_ASSERT(reporter, SK_ColorGREEN == fResultBitmap.getColor(0, 0));
55 }
56
57 SkBitmap fResultBitmap;
58 int fPictureWidth, fPictureHeight;
59};
60
61// Test to verify the playback of an empty picture
62//
64public:
66 : PictureBBHTestBase(2, 2, 1, 1) {}
68
69 void doTest(SkCanvas&, SkCanvas&) override {}
70};
71
72// Test to verify the playback of a picture into a canvas that has
73// an empty clip.
74//
76public:
78 : PictureBBHTestBase(2, 2, 3, 3) {}
79
80 void doTest(SkCanvas& playbackCanvas, SkCanvas& recordingCanvas) override {
81 // intersect with out of bounds rect -> empty clip.
82 playbackCanvas.clipRect(SkRect::MakeXYWH(10, 10, 1, 1));
84 recordingCanvas.drawRect(SkRect::MakeWH(3, 3), paint);
85 }
86
88};
89
90DEF_TEST(PictureBBH, reporter) {
91
92 DrawEmptyPictureBBHTest emptyPictureTest;
93 emptyPictureTest.run(reporter);
94
95 EmptyClipPictureBBHTest emptyClipPictureTest;
96 emptyClipPictureTest.run(reporter);
97}
98
99DEF_TEST(PictureNegativeSpace, r) {
100 SkRTreeFactory factory;
101 SkPictureRecorder recorder;
102
103 SkRect cull = {-200,-200,+200,+200};
104
105 {
106 sk_sp<SkBBoxHierarchy> bbh = factory();
107 auto canvas = recorder.beginRecording(cull, bbh);
108 canvas->save();
109 canvas->clipRect(cull);
110 canvas->drawRect({-20,-20,-10,-10}, SkPaint{});
111 canvas->drawRect({-20,-20,-10,-10}, SkPaint{});
112 canvas->restore();
113 auto pic = recorder.finishRecordingAsPicture();
114 REPORTER_ASSERT(r, pic->approximateOpCount() == 5);
115 REPORTER_ASSERT(r, pic->cullRect() == (SkRect{-20,-20,-10,-10}));
116 }
117
118 {
119 auto canvas = recorder.beginRecording(cull, &factory);
120 canvas->clipRect(cull);
121 canvas->drawRect({-20,-20,-10,-10}, SkPaint{});
122 canvas->drawRect({-20,-20,-10,-10}, SkPaint{});
123 auto pic = recorder.finishRecordingAsPicture();
124 REPORTER_ASSERT(r, pic->approximateOpCount() == 3);
125 REPORTER_ASSERT(r, pic->cullRect() == (SkRect{-20,-20,-10,-10}));
126 }
127}
reporter
Definition: FontMgrTest.cpp:39
DEF_TEST(PictureBBH, reporter)
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
~DrawEmptyPictureBBHTest() override
void doTest(SkCanvas &, SkCanvas &) override
void doTest(SkCanvas &playbackCanvas, SkCanvas &recordingCanvas) override
~EmptyClipPictureBBHTest() override
virtual ~PictureBBHTestBase()
void run(skiatest::Reporter *reporter)
PictureBBHTestBase(int playbackWidth, int playbackHeight, int recordWidth, int recordHeight)
virtual void doTest(SkCanvas &playbackCanvas, SkCanvas &recordingCanvas)=0
SkColor getColor(int x, int y) const
Definition: SkBitmap.h:874
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
int save()
Definition: SkCanvas.cpp:447
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
const Paint & paint
Definition: color_source.cc:38
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609