Flutter Engine
The Flutter Engine
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
6
7#include "flutter/display_list/dl_builder.h"
8#include "flutter/testing/testing.h"
9#include "gtest/gtest.h"
10
11namespace flutter {
12namespace testing {
13
17using impeller::Point;
20
22
23TEST_P(DlGoldenTest, CanDrawPaint) {
24 auto draw = [](DlCanvas* canvas,
25 const std::vector<std::unique_ptr<DlImage>>& images) {
26 canvas->Scale(0.2, 0.2);
28 paint.setColor(DlColor::kCyan());
29 canvas->DrawPaint(paint);
30 };
31
33 draw(&builder, /*images=*/{});
34
35 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
36}
37
38TEST_P(DlGoldenTest, CanRenderImage) {
39 auto draw = [](DlCanvas* canvas, const std::vector<sk_sp<DlImage>>& images) {
40 FML_CHECK(images.size() >= 1);
42 paint.setColor(DlColor::kRed());
43 canvas->DrawImage(images[0], SkPoint::Make(100.0, 100.0),
45 };
46
48 std::vector<sk_sp<DlImage>> images;
49 images.emplace_back(CreateDlImageForFixture("kalimba.jpg"));
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
57TEST_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);
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)),
79 SkRect save_layer_bounds = SkRect::MakeLTRB(201.25, 10, 361.25, 170);
81 0, 3, -920, //
82 0, 0, 1),
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))
92 canvas->DrawCircle(SkPoint::Make(80, 80), 80, paint);
93 }
94 canvas->Restore();
95 }
96 canvas->Restore();
97 };
98
100 std::vector<sk_sp<DlImage>> images;
102
103 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
104}
105
106namespace {
107void DrawBlurGrid(DlCanvas* canvas) {
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);
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);
125 9, 10, 10, 10);
126 canvas->DrawRRect(rrect, paint);
127 }
128}
129} // namespace
130
131TEST_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
141 std::vector<sk_sp<DlImage>> images;
143
144 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
145}
146
147TEST_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
158 std::vector<sk_sp<DlImage>> images;
160
161 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
162}
163
164TEST_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
179 std::vector<sk_sp<DlImage>> images;
181
182 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
183}
184
185TEST_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);
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()
199 .setStrokeCap(cap)
200 .setStrokeWidth(8.0f);
201 DlPaint middle_paint = DlPaint()
203 .setStrokeCap(cap)
204 .setStrokeWidth(5.0f);
205 DlPaint thin_paint = DlPaint()
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
239 std::vector<sk_sp<DlImage>> images;
241
242 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
243}
244
245} // namespace testing
246} // namespace flutter
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
static SkMatrix MakeAll(SkScalar scaleX, SkScalar skewX, SkScalar transX, SkScalar skewY, SkScalar scaleY, SkScalar transY, SkScalar pers0, SkScalar pers1, SkScalar pers2)
Definition: SkMatrix.h:179
Definition: SkPath.h:59
SkPath & moveTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:688
SkPath & lineTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:728
static SkRRect MakeOval(const SkRect &oval)
Definition: SkRRect.h:162
void setNinePatch(const SkRect &rect, SkScalar leftRad, SkScalar topRad, SkScalar rightRad, SkScalar bottomRad)
Definition: SkRRect.cpp:115
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
virtual void DrawPaint(const DlPaint &paint)=0
virtual void Rotate(SkScalar degrees)=0
virtual void DrawRect(const SkRect &rect, const DlPaint &paint)=0
virtual void Scale(SkScalar sx, SkScalar sy)=0
virtual void DrawCircle(const SkPoint &center, SkScalar radius, const DlPaint &paint)=0
virtual void SaveLayer(const SkRect *bounds, const DlPaint *paint=nullptr, const DlImageFilter *backdrop=nullptr)=0
virtual void DrawRRect(const SkRRect &rrect, const DlPaint &paint)=0
virtual void DrawColor(DlColor color, DlBlendMode mode=DlBlendMode::kSrcOver)=0
virtual void DrawDashedLine(const DlPoint &p0, const DlPoint &p1, DlScalar on_length, DlScalar off_length, const DlPaint &paint)=0
virtual void Restore()=0
virtual void Translate(SkScalar tx, SkScalar ty)=0
virtual void DrawImage(const sk_sp< DlImage > &image, const SkPoint point, DlImageSampling sampling, const DlPaint *paint=nullptr)=0
virtual void ClipPath(const SkPath &path, ClipOp clip_op=ClipOp::kIntersect, bool is_aa=false)=0
virtual void ClipRRect(const SkRRect &rrect, ClipOp clip_op=ClipOp::kIntersect, bool is_aa=false)=0
virtual void Save()=0
DlPaint & setColor(DlColor color)
Definition: dl_paint.h:70
DlPaint & setAntiAlias(bool isAntiAlias)
Definition: dl_paint.h:58
DlPaint & setStrokeCap(DlStrokeCap cap)
Definition: dl_paint.h:102
DlPaint & setStrokeWidth(float width)
Definition: dl_paint.h:116
DlPaint & setDrawStyle(DlDrawStyle style)
Definition: dl_paint.h:94
const Paint & paint
Definition: color_source.cc:38
#define FML_CHECK(condition)
Definition: logging.h:85
std::array< MockImage, 3 > images
Definition: mock_vulkan.cc:41
double y
double x
sk_sp< const SkImageFilter > backdrop
Definition: SkRecords.h:191
SkRRect rrect
Definition: SkRecords.h:232
TEST_P(DlGoldenTest, TextBlurMaskFilterRespectCTM)
INSTANTIATE_PLAYGROUND_SUITE(DlGoldenTest)
DlStrokeCap
Definition: dl_paint.h:28
@ kRound
adds circle
@ kButt
no stroke extension
@ kSquare
adds square
@ kStroke
strokes boundary of shapes
@ kNormal
fuzzy inside and outside
float Scalar
Definition: scalar.h:18
TPoint< Scalar > Point
Definition: point.h:322
PlaygroundBackend
Definition: playground.h:27
flutter::DlColor DlColor
flutter::DlPaint DlPaint
int32_t height
int32_t width
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
static constexpr DlColor kMagenta()
Definition: dl_color.h:28
static constexpr DlColor kWhite()
Definition: dl_color.h:23
static constexpr DlColor kBlue()
Definition: dl_color.h:26
static constexpr DlColor kYellow()
Definition: dl_color.h:29
static constexpr DlColor kRed()
Definition: dl_color.h:24
static constexpr DlColor kGreen()
Definition: dl_color.h:25
static constexpr DlColor kCyan()
Definition: dl_color.h:27
constexpr TPoint Rotate(const Radians &angle) const
Definition: point.h:226