Flutter Engine
 
Loading...
Searching...
No Matches
aiks_dl_clip_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
14
15namespace impeller {
16namespace testing {
17
18using namespace flutter;
19
20TEST_P(AiksTest, CanRenderNestedClips) {
21 DisplayListBuilder builder;
22 DlPaint paint;
24
25 builder.Save();
26 builder.ClipPath(DlPath::MakeCircle(DlPoint(200, 400), 300));
27 builder.Restore();
28 builder.ClipPath(DlPath::MakeCircle(DlPoint(600, 400), 300));
29 builder.ClipPath(DlPath::MakeCircle(DlPoint(400, 600), 300));
30 builder.DrawRect(DlRect::MakeXYWH(200, 200, 400, 400), paint);
31
32 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
33}
34
35TEST_P(AiksTest, CanRenderDifferenceClips) {
36 DisplayListBuilder builder;
37 builder.Translate(400, 400);
38
39 // Limit drawing to face circle with a clip.
40 builder.ClipPath(DlPath::MakeCircle(DlPoint(0, 0), 200));
41 builder.Save();
42
43 // Cut away eyes/mouth using difference clips.
44 builder.ClipPath(DlPath::MakeCircle(DlPoint(-100, -50), 30),
45 DlClipOp::kDifference);
46 builder.ClipPath(DlPath::MakeCircle(DlPoint(100, -50), 30),
47 DlClipOp::kDifference);
48
49 DlPathBuilder path_builder;
50 path_builder.MoveTo(DlPoint(-100, 50));
51 path_builder.QuadraticCurveTo(DlPoint(0, 150), DlPoint(100, 50));
52 builder.ClipPath(path_builder.TakePath(), DlClipOp::kDifference);
53
54 // Draw a huge yellow rectangle to prove the clipping works.
55 DlPaint paint;
57 builder.DrawRect(DlRect::MakeXYWH(-1000, -1000, 2000, 2000), paint);
58
59 // Remove the difference clips and draw hair that partially covers the eyes.
60 builder.Restore();
62 DlPathBuilder path_builder_2;
63 path_builder_2.MoveTo(DlPoint(200, -200));
64 path_builder_2.LineTo(DlPoint(-200, -200));
65 path_builder_2.LineTo(DlPoint(-200, -40));
66 path_builder_2.CubicCurveTo(DlPoint(0, -40), DlPoint(0, -80),
67 DlPoint(200, -80));
68
69 builder.DrawPath(path_builder_2.TakePath(), paint);
70
71 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
72}
73
74TEST_P(AiksTest, CanRenderWithContiguousClipRestores) {
75 DisplayListBuilder builder;
76
77 // Cover the whole canvas with red.
78 DlPaint paint;
79 paint.setColor(DlColor::kRed());
80 builder.DrawPaint(paint);
81
82 builder.Save();
83
84 // Append two clips, the second resulting in empty coverage.
85 builder.ClipRect(DlRect::MakeXYWH(100, 100, 100, 100));
86 builder.ClipRect(DlRect::MakeXYWH(300, 300, 100, 100));
87
88 // Restore to no clips.
89 builder.Restore();
90
91 // Replace the whole canvas with green.
93 builder.DrawPaint(paint);
94
95 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
96}
97
98TEST_P(AiksTest, ClipsUseCurrentTransform) {
99 std::array<DlColor, 5> colors = {DlColor::kWhite(), DlColor::kBlack(),
102 DisplayListBuilder builder;
103 DlPaint paint;
104
105 builder.Translate(300, 300);
106 for (int i = 0; i < 15; i++) {
107 builder.Scale(0.8, 0.8);
108
109 paint.setColor(colors[i % colors.size()]);
110 builder.ClipPath(DlPath::MakeCircle(DlPoint(0, 0), 300));
111 builder.DrawRect(DlRect::MakeXYWH(-300, -300, 600, 600), paint);
112 }
113 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
114}
115
116/// If correct, this test should draw a green circle. If any red is visible,
117/// there is a depth bug.
118TEST_P(AiksTest, FramebufferBlendsRespectClips) {
119 DisplayListBuilder builder;
120
121 // Clear the whole canvas with white.
122 DlPaint paint;
123 paint.setColor(DlColor::kWhite());
124 builder.DrawPaint(paint);
125
126 builder.ClipPath(DlPath::MakeCircle(DlPoint(150, 150), 50),
127 DlClipOp::kIntersect);
128
129 // Draw a red rectangle that should not show through the circle clip.
130 paint.setColor(DlColor::kRed());
131 paint.setBlendMode(DlBlendMode::kMultiply);
132 builder.DrawRect(DlRect::MakeXYWH(100, 100, 100, 100), paint);
133
134 // Draw a green circle that shows through the clip.
135 paint.setColor(DlColor::kGreen());
136 paint.setBlendMode(DlBlendMode::kSrcOver);
137 builder.DrawCircle(DlPoint(150, 150), 50, paint);
138
139 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
140}
141
142} // namespace testing
143} // namespace impeller
void ClipRect(const DlRect &rect, DlClipOp clip_op=DlClipOp::kIntersect, bool is_aa=false) override
void DrawCircle(const DlPoint &center, DlScalar radius, const DlPaint &paint) override
void Scale(DlScalar sx, DlScalar sy) override
void Translate(DlScalar tx, DlScalar ty) override
void DrawPaint(const DlPaint &paint) override
sk_sp< DisplayList > Build()
Definition dl_builder.cc:66
void DrawPath(const DlPath &path, const DlPaint &paint) override
void ClipPath(const DlPath &path, DlClipOp clip_op=DlClipOp::kIntersect, bool is_aa=false) override
void DrawRect(const DlRect &rect, const DlPaint &paint) override
DlPaint & setColor(DlColor color)
Definition dl_paint.h:70
DlPaint & setBlendMode(DlBlendMode mode)
Definition dl_paint.h:85
DlPathBuilder & LineTo(DlPoint p2)
Draw a line from the current point to the indicated point p2.
DlPathBuilder & MoveTo(DlPoint p2)
Start a new contour that will originate at the indicated point p2.
const DlPath TakePath()
Returns the path constructed by this path builder and resets its internal state to the default state ...
DlPathBuilder & QuadraticCurveTo(DlPoint cp, DlPoint p2)
Draw a quadratic bezier curve from the current point to the indicated point p2, using the indicated p...
DlPathBuilder & CubicCurveTo(DlPoint cp1, DlPoint cp2, DlPoint p2)
Draw a cubic bezier curve from the current point to the indicated point p2, using the indicated point...
static DlPath MakeCircle(const DlPoint center, DlScalar radius)
Definition dl_path.cc:68
impeller::Point DlPoint
TEST_P(AiksTest, DrawAtlasNoColor)
static constexpr DlColor kWhite()
Definition dl_color.h:70
static constexpr DlColor kBlack()
Definition dl_color.h:69
static constexpr DlColor kMaroon()
Definition dl_color.h:82
static constexpr DlColor kYellow()
Definition dl_color.h:76
static constexpr DlColor kFuchsia()
Definition dl_color.h:81
static constexpr DlColor kRed()
Definition dl_color.h:71
static constexpr DlColor kGreen()
Definition dl_color.h:72
static constexpr DlColor kSkyBlue()
Definition dl_color.h:83
static constexpr TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition rect.h:136