| Flutter Impeller |

dl_golden_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 "impeller/display_list/dl_golden_unittests.h"
6
7 #include "flutter/display_list/dl_builder.h"
8 #include "flutter/testing/testing.h"
9 #include "gtest/gtest.h"
10
11 namespace flutter {
12 namespace testing {
13
14 using impeller::Degrees;
15 using impeller::PlaygroundBackend;
16 using impeller::PlaygroundTest;
17 using impeller::Point;
18 using impeller::Radians;
19 using impeller::Scalar;
20
21 INSTANTIATE_PLAYGROUND_SUITE(DlGoldenTest);
22
23 TEST_P(DlGoldenTest, CanDrawPaint) {
24 auto draw = [](DlCanvas* canvas,
25 const std::vector<std::unique_ptr<DlImage>>& images) {
26 canvas->Scale(0.2, 0.2);
27 DlPaint paint;
28 paint.setColor(DlColor::kCyan());
29 canvas->DrawPaint(paint);
30 };
31
32 DisplayListBuilder builder;
33 draw(&builder, /*images=*/{});
34
35 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
36 }
37
38 TEST_P(DlGoldenTest, CanRenderImage) {
39 auto draw = [](DlCanvas* canvas, const std::vector<sk_sp<DlImage>>& images) {
40 FML_CHECK(images.size() >= 1);
41 DlPaint paint;
42 paint.setColor(DlColor::kRed());
43 canvas->DrawImage(images[0], SkPoint::Make(100.0, 100.0),
44 DlImageSampling::kLinear, &paint);
45 };
46
47 DisplayListBuilder builder;
48 std::vector<sk_sp<DlImage>> images;
49 images.emplace_back(CreateDlImageForFixture("kalimba.jpg"));
50 draw(&builder, images);
51
52 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
53 }
54
55 // Asserts that subpass rendering of MatrixImageFilters works.
56 // https://github.com/flutter/flutter/issues/147807
57 TEST_P(DlGoldenTest, Bug147807) {
58 Point content_scale = GetContentScale();
59 auto draw = [content_scale](DlCanvas* canvas,
60 const std::vector<sk_sp<DlImage>>& images) {
61 canvas->Scale(content_scale.x, content_scale.y);
62 DlPaint paint;
63 paint.setColor(DlColor(0xfffef7ff));
64 canvas->DrawRect(SkRect::MakeLTRB(0, 0, 375, 667), paint);
65 paint.setColor(DlColor(0xffff9800));
66 canvas->DrawRect(SkRect::MakeLTRB(0, 0, 187.5, 333.5), paint);
67 paint.setColor(DlColor(0xff9c27b0));
68 canvas->DrawRect(SkRect::MakeLTRB(187.5, 0, 375, 333.5), paint);
69 paint.setColor(DlColor(0xff4caf50));
70 canvas->DrawRect(SkRect::MakeLTRB(0, 333.5, 187.5, 667), paint);
71 paint.setColor(DlColor(0xfff44336));
72 canvas->DrawRect(SkRect::MakeLTRB(187.5, 333.5, 375, 667), paint);
73
74 canvas->Save();
75 {
76 canvas->ClipRRect(
77 SkRRect::MakeOval(SkRect::MakeLTRB(201.25, 10, 361.25, 170)),
78 DlCanvas::ClipOp::kIntersect, true);
79 SkRect save_layer_bounds = SkRect::MakeLTRB(201.25, 10, 361.25, 170);
80 DlMatrixImageFilter backdrop(SkMatrix::MakeAll(3, 0, -280, //
81 0, 3, -920, //
82 0, 0, 1),
83 DlImageSampling::kLinear);
84 canvas->SaveLayer(&save_layer_bounds, /*paint=*/nullptr, &backdrop);
85 {
86 canvas->Translate(201.25, 10);
87 auto paint = DlPaint()
88 .setAntiAlias(true)
89 .setColor(DlColor(0xff2196f3))
90 .setStrokeWidth(5)
91 .setDrawStyle(DlDrawStyle::kStroke);
92 canvas->DrawCircle(SkPoint::Make(80, 80), 80, paint);
93 }
94 canvas->Restore();
95 }
96 canvas->Restore();
97 };
98
99 DisplayListBuilder builder;
100 std::vector<sk_sp<DlImage>> images;
101 draw(&builder, images);
102
103 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
104 }
105
106 namespace {
107 void DrawBlurGrid(DlCanvas* canvas) {
108 DlPaint paint;
109 paint.setColor(DlColor(0xfffef7ff));
110 Scalar width = 150;
111 Scalar height = 150;
112 Scalar gap = 80;
113 std::vector<Scalar> blur_radii = {10, 30, 50};
114 for (size_t i = 0; i < blur_radii.size(); ++i) {
115 Scalar blur_radius = blur_radii[i];
116 auto blur_filter = std::make_shared<flutter::DlBlurMaskFilter>(
117 flutter::DlBlurStyle::kNormal, blur_radius);
118 paint.setMaskFilter(blur_filter);
119 SkRRect rrect;
120 Scalar yval = gap + i * (gap + height);
121 rrect.setNinePatch(SkRect::MakeXYWH(gap, yval, width, height), 10, 10, 10,
122 10);
123 canvas->DrawRRect(rrect, paint);
124 rrect.setNinePatch(SkRect::MakeXYWH(2.0 * gap + width, yval, width, height),
125 9, 10, 10, 10);
126 canvas->DrawRRect(rrect, paint);
127 }
128 }
129 } // namespace
130
131 TEST_P(DlGoldenTest, GaussianVsRRectBlur) {
132 Point content_scale = GetContentScale();
133 auto draw = [content_scale](DlCanvas* canvas,
134 const std::vector<sk_sp<DlImage>>& images) {
135 canvas->Scale(content_scale.x, content_scale.y);
136 canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233)));
137 DrawBlurGrid(canvas);
138 };
139
140 DisplayListBuilder builder;
141 std::vector<sk_sp<DlImage>> images;
142 draw(&builder, images);
143
144 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
145 }
146
147 TEST_P(DlGoldenTest, GaussianVsRRectBlurScaled) {
148 Point content_scale = GetContentScale();
149 auto draw = [content_scale](DlCanvas* canvas,
150 const std::vector<sk_sp<DlImage>>& images) {
151 canvas->Scale(content_scale.x, content_scale.y);
152 canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233)));
153 canvas->Scale(0.33, 0.33);
154 DrawBlurGrid(canvas);
155 };
156
157 DisplayListBuilder builder;
158 std::vector<sk_sp<DlImage>> images;
159 draw(&builder, images);
160
161 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
162 }
163
164 TEST_P(DlGoldenTest, GaussianVsRRectBlurScaledRotated) {
165 Point content_scale = GetContentScale();
166 auto draw = [content_scale](DlCanvas* canvas,
167 const std::vector<sk_sp<DlImage>>& images) {
168 canvas->Scale(content_scale.x, content_scale.y);
169 canvas->Translate(200, 200);
170 canvas->DrawPaint(DlPaint().setColor(DlColor(0xff112233)));
171 canvas->Scale(0.33, 0.33);
172 canvas->Translate(300, 300);
173 canvas->Rotate(45);
174 canvas->Translate(-300, -300);
175 DrawBlurGrid(canvas);
176 };
177
178 DisplayListBuilder builder;
179 std::vector<sk_sp<DlImage>> images;
180 draw(&builder, images);
181
182 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
183 }
184
185 TEST_P(DlGoldenTest, DashedLinesTest) {
186 Point content_scale = GetContentScale();
187 auto draw = [content_scale](DlCanvas* canvas,
188 const std::vector<sk_sp<DlImage>>& images) {
189 canvas->Scale(content_scale.x, content_scale.y);
190 canvas->DrawPaint(DlPaint().setColor(DlColor::kWhite()));
191
192 auto draw_one = [canvas](DlStrokeCap cap, Scalar x, Scalar y,
193 Scalar dash_on, Scalar dash_off) {
194 Point center = Point(x, y);
195 Scalar inner = 20.0f;
196 Scalar outer = 100.0f;
197 DlPaint thick_paint = DlPaint()
198 .setColor(DlColor::kBlue())
199 .setStrokeCap(cap)
200 .setStrokeWidth(8.0f);
201 DlPaint middle_paint = DlPaint()
202 .setColor(DlColor::kGreen())
203 .setStrokeCap(cap)
204 .setStrokeWidth(5.0f);
205 DlPaint thin_paint = DlPaint()
206 .setColor(DlColor::kMagenta())
207 .setStrokeCap(cap)
208 .setStrokeWidth(2.0f);
209 for (int degrees = 0; degrees < 360; degrees += 30) {
210 Point delta = Point(1.0f, 0.0f).Rotate(Degrees(degrees));
211 canvas->DrawDashedLine(center + inner * delta, center + outer * delta,
212 dash_on, dash_off, thick_paint);
213 canvas->DrawDashedLine(center + inner * delta, center + outer * delta,
214 dash_on, dash_off, middle_paint);
215 canvas->DrawDashedLine(center + inner * delta, center + outer * delta,
216 dash_on, dash_off, thin_paint);
217 }
218 };
219
220 draw_one(DlStrokeCap::kButt, 150.0f, 150.0f, 15.0f, 10.0f);
221 draw_one(DlStrokeCap::kSquare, 400.0f, 150.0f, 15.0f, 10.0f);
222 draw_one(DlStrokeCap::kRound, 150.0f, 400.0f, 15.0f, 10.0f);
223 draw_one(DlStrokeCap::kRound, 400.0f, 400.0f, 0.0f, 11.0f);
224
225 // Make sure the rendering op responds appropriately to clipping
226 canvas->Save();
227 SkPath clip_path = SkPath();
228 clip_path.moveTo(275.0f, 225.0f);
229 clip_path.lineTo(325.0f, 275.0f);
230 clip_path.lineTo(275.0f, 325.0f);
231 clip_path.lineTo(225.0f, 275.0f);
232 canvas->ClipPath(clip_path);
233 canvas->DrawColor(DlColor::kYellow());
234 draw_one(DlStrokeCap::kRound, 275.0f, 275.0f, 15.0f, 10.0f);
235 canvas->Restore();
236 };
237
238 DisplayListBuilder builder;
239 std::vector<sk_sp<DlImage>> images;
240 draw(&builder, images);
241
242 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
243 }
244
245 } // namespace testing
246 } // namespace flutter
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::PlaygroundBackend
PlaygroundBackend
Definition: playground.h:27
flutter::testing::TEST_P
TEST_P(DlGoldenTest, TextBlurMaskFilterRespectCTM)
Definition: dl_golden_blur_unittests.cc:59
dl_golden_unittests.h
impeller::Point
TPoint< Scalar > Point
Definition: point.h:322
impeller::Radians
Definition: scalar.h:38
flutter
Definition: dl_golden_blur_unittests.cc:14
blur_radius
Vector2 blur_radius
Blur radius in source pixels based on scaled_sigma.
Definition: gaussian_blur_filter_contents.cc:89
paint
const Paint & paint
Definition: color_source.cc:38
impeller::Degrees
Definition: scalar.h:46
impeller::TPoint::Rotate
constexpr TPoint Rotate(const Radians &angle) const
Definition: point.h:226
impeller::PlaygroundTest
Definition: playground_test.h:21
flutter::testing::DlGoldenTest
impeller::DlPlayground DlGoldenTest
Definition: dl_golden_unittests.h:17
flutter::testing::INSTANTIATE_PLAYGROUND_SUITE
INSTANTIATE_PLAYGROUND_SUITE(DlGoldenTest)
impeller::DlPlayground
Definition: dl_playground.h:15