Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
checkerboard_layertree_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/flow/layers/clip_path_layer.h"
6#include "flutter/flow/layers/clip_rect_layer.h"
7#include "flutter/flow/layers/clip_rrect_layer.h"
8#include "flutter/flow/testing/layer_test.h"
9#include "flutter/flow/testing/mock_layer.h"
10#include "flutter/fml/macros.h"
11#include "flutter/testing/mock_canvas.h"
12
13namespace flutter {
14namespace testing {
15
17
19
20#ifndef NDEBUG
21TEST_F(CheckerBoardLayerTest, ClipRectSaveLayerCheckBoard) {
22 const SkMatrix initial_matrix = SkMatrix::Translate(0.5f, 1.0f);
23 const SkRect child_bounds = SkRect::MakeXYWH(1.0, 2.0, 2.0, 2.0);
24 const SkRect layer_bounds = SkRect::MakeXYWH(0.5, 1.0, 5.0, 6.0);
25 const SkPath child_path = SkPath().addRect(child_bounds);
26 const DlPaint child_paint = DlPaint(DlColor::kYellow());
27 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
28 auto layer = std::make_shared<ClipRectLayer>(layer_bounds,
30 layer->Add(mock_layer);
31
32 preroll_context()->state_stack.set_preroll_delegate(initial_matrix);
33 layer->Preroll(preroll_context());
34
35 // Untouched
36 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(), kGiantRect);
37 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
38
39 EXPECT_EQ(mock_layer->paint_bounds(), child_bounds);
40 EXPECT_EQ(layer->paint_bounds(), mock_layer->paint_bounds());
41 EXPECT_EQ(layer->child_paint_bounds(), child_bounds);
42 EXPECT_TRUE(mock_layer->needs_painting(paint_context()));
43 EXPECT_TRUE(layer->needs_painting(paint_context()));
44 EXPECT_EQ(mock_layer->parent_cull_rect(), layer_bounds);
45 EXPECT_EQ(mock_layer->parent_matrix(), initial_matrix);
46 EXPECT_EQ(mock_layer->parent_mutators(),
47 std::vector({Mutator(layer_bounds)}));
48
49 layer->Paint(display_list_paint_context());
50 {
51 DisplayListBuilder expected_builder;
52 /* (ClipRect)layer::Paint */ {
53 expected_builder.Save();
54 {
55 expected_builder.ClipRect(layer_bounds, DlCanvas::ClipOp::kIntersect,
56 true);
57 expected_builder.SaveLayer(&child_bounds);
58 {
59 /* mock_layer::Paint */ {
60 expected_builder.DrawPath(child_path, child_paint);
61 }
62 }
63 expected_builder.Restore();
64 }
65 expected_builder.Restore();
66 }
68 DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
69 }
70
71 reset_display_list();
72 layer->Paint(checkerboard_context());
73 {
74 DisplayListBuilder expected_builder;
75 /* (ClipRect)layer::Paint */ {
76 expected_builder.Save();
77 {
78 expected_builder.ClipRect(layer_bounds, DlCanvas::ClipOp::kIntersect,
79 true);
80 expected_builder.SaveLayer(&child_bounds);
81 {
82 /* mock_layer::Paint */ {
83 expected_builder.DrawPath(child_path, child_paint);
84 }
85 expected_builder.DrawRect(child_path.getBounds(),
86 checkerboard_paint());
87 }
88 expected_builder.Restore();
89 }
90 expected_builder.Restore();
91 }
93 DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
94 }
95}
96
97TEST_F(CheckerBoardLayerTest, ClipPathSaveLayerCheckBoard) {
98 const SkMatrix initial_matrix = SkMatrix::Translate(0.5f, 1.0f);
99 const SkRect child_bounds = SkRect::MakeXYWH(1.0, 2.0, 2.0, 2.0);
100 const SkRect layer_bounds = SkRect::MakeXYWH(0.5, 1.0, 5.0, 6.0);
101 const SkPath child_path = SkPath().addRect(child_bounds);
102 const SkPath layer_path =
103 SkPath().addRect(layer_bounds).addRect(layer_bounds);
104 const DlPaint child_paint = DlPaint(DlColor::kYellow());
105 const DlPaint clip_paint;
106 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
107 auto layer = std::make_shared<ClipPathLayer>(layer_path,
109 layer->Add(mock_layer);
110
111 preroll_context()->state_stack.set_preroll_delegate(initial_matrix);
112 layer->Preroll(preroll_context());
113
114 // Untouched
115 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(), kGiantRect);
116 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
117
118 EXPECT_EQ(mock_layer->paint_bounds(), child_bounds);
119 EXPECT_EQ(layer->paint_bounds(), mock_layer->paint_bounds());
120 EXPECT_TRUE(mock_layer->needs_painting(paint_context()));
121 EXPECT_TRUE(layer->needs_painting(paint_context()));
122 EXPECT_EQ(mock_layer->parent_cull_rect(), layer_bounds);
123 EXPECT_EQ(mock_layer->parent_matrix(), initial_matrix);
124 EXPECT_EQ(mock_layer->parent_mutators(), std::vector({Mutator(layer_path)}));
125
126 layer->Paint(display_list_paint_context());
127 {
128 DisplayListBuilder expected_builder;
129 /* (ClipRect)layer::Paint */ {
130 expected_builder.Save();
131 {
132 expected_builder.ClipPath(layer_path, DlCanvas::ClipOp::kIntersect,
133 true);
134 expected_builder.SaveLayer(&child_bounds);
135 {
136 /* mock_layer::Paint */ {
137 expected_builder.DrawPath(child_path, child_paint);
138 }
139 }
140 expected_builder.Restore();
141 }
142 expected_builder.Restore();
143 }
145 DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
146 }
147
148 reset_display_list();
149 layer->Paint(checkerboard_context());
150 {
151 DisplayListBuilder expected_builder;
152 /* (ClipRect)layer::Paint */ {
153 expected_builder.Save();
154 {
155 expected_builder.ClipPath(layer_path, DlCanvas::ClipOp::kIntersect,
156 true);
157 expected_builder.SaveLayer(&child_bounds);
158 {
159 /* mock_layer::Paint */ {
160 expected_builder.DrawPath(child_path, child_paint);
161 }
162 expected_builder.DrawRect(child_path.getBounds(),
163 checkerboard_paint());
164 }
165 expected_builder.Restore();
166 }
167 expected_builder.Restore();
168 }
170 DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
171 }
172}
173
174TEST_F(CheckerBoardLayerTest, ClipRRectSaveLayerCheckBoard) {
175 const SkMatrix initial_matrix = SkMatrix::Translate(0.5f, 1.0f);
176 const SkRect child_bounds = SkRect::MakeXYWH(1.0, 2.0, 2.0, 2.0);
177 const SkRect layer_bounds = SkRect::MakeXYWH(0.5, 1.0, 5.0, 6.0);
178 const SkPath child_path = SkPath().addRect(child_bounds);
179 const SkRRect layer_rrect = SkRRect::MakeRectXY(layer_bounds, .1, .1);
180 const DlPaint child_paint = DlPaint(DlColor::kYellow());
181 const DlPaint clip_paint;
182 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
183 auto layer = std::make_shared<ClipRRectLayer>(layer_rrect,
185 layer->Add(mock_layer);
186
187 preroll_context()->state_stack.set_preroll_delegate(initial_matrix);
188 layer->Preroll(preroll_context());
189
190 // Untouched
191 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(), kGiantRect);
192 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
193
194 EXPECT_EQ(mock_layer->paint_bounds(), child_bounds);
195 EXPECT_EQ(layer->paint_bounds(), mock_layer->paint_bounds());
196 EXPECT_TRUE(mock_layer->needs_painting(paint_context()));
197 EXPECT_TRUE(layer->needs_painting(paint_context()));
198 EXPECT_EQ(mock_layer->parent_cull_rect(), layer_bounds);
199 EXPECT_EQ(mock_layer->parent_matrix(), initial_matrix);
200 EXPECT_EQ(mock_layer->parent_mutators(), std::vector({Mutator(layer_rrect)}));
201
202 layer->Paint(display_list_paint_context());
203 {
204 DisplayListBuilder expected_builder;
205 /* (ClipRect)layer::Paint */ {
206 expected_builder.Save();
207 {
208 expected_builder.ClipRRect(layer_rrect, DlCanvas::ClipOp::kIntersect,
209 true);
210 expected_builder.SaveLayer(&child_bounds);
211 {
212 /* mock_layer::Paint */ {
213 expected_builder.DrawPath(child_path, child_paint);
214 }
215 }
216 expected_builder.Restore();
217 }
218 expected_builder.Restore();
219 }
221 DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
222 }
223
224 reset_display_list();
225 layer->Paint(checkerboard_context());
226 {
227 DisplayListBuilder expected_builder;
228 /* (ClipRect)layer::Paint */ {
229 expected_builder.Save();
230 {
231 expected_builder.ClipRRect(layer_rrect, DlCanvas::ClipOp::kIntersect,
232 true);
233 expected_builder.SaveLayer(&child_bounds);
234 {
235 /* mock_layer::Paint */ {
236 expected_builder.DrawPath(child_path, child_paint);
237 }
238 expected_builder.DrawRect(child_path.getBounds(),
239 checkerboard_paint());
240 }
241 expected_builder.Restore();
242 }
243 expected_builder.Restore();
244 }
246 DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
247 }
248}
249
250#endif
251} // namespace testing
252} // namespace flutter
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
const SkRect & getBounds() const
Definition SkPath.cpp:420
SkPath & addRect(const SkRect &rect, SkPathDirection dir, unsigned start)
Definition SkPath.cpp:854
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition SkRRect.h:180
void DrawRect(const SkRect &rect, const DlPaint &paint) override
void ClipRRect(const SkRRect &rrect, ClipOp clip_op=ClipOp::kIntersect, bool is_aa=false) override
void DrawPath(const SkPath &path, const DlPaint &paint) override
void SaveLayer(const SkRect *bounds, const DlPaint *paint=nullptr, const DlImageFilter *backdrop=nullptr) override
sk_sp< DisplayList > Build()
Definition dl_builder.cc:67
void ClipRect(const SkRect &rect, ClipOp clip_op=ClipOp::kIntersect, bool is_aa=false) override
void ClipPath(const SkPath &path, ClipOp clip_op=ClipOp::kIntersect, bool is_aa=false) override
TEST_F(DisplayListTest, Defaults)
LayerTestBase<::testing::Test > LayerTest
Definition layer_test.h:240
bool DisplayListsEQ_Verbose(const DisplayList *a, const DisplayList *b)
@ kAntiAliasWithSaveLayer
Definition layer.h:52
static constexpr SkRect kGiantRect
Definition layer.h:49
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr DlColor kYellow()
Definition dl_color.h:29
#define EXPECT_TRUE(handle)
Definition unit_test.h:685