Flutter Impeller
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 
7 #include "flutter/display_list/dl_blend_mode.h"
8 #include "flutter/display_list/dl_builder.h"
9 #include "flutter/display_list/dl_color.h"
10 #include "flutter/display_list/dl_paint.h"
11 #include "flutter/display_list/effects/dl_color_filter.h"
12 #include "flutter/testing/testing.h"
13 
14 namespace impeller {
15 namespace testing {
16 
17 using namespace flutter;
18 
19 TEST_P(AiksTest, CanRenderNestedClips) {
20  DisplayListBuilder builder;
21  DlPaint paint;
22  paint.setColor(DlColor::kFuchsia());
23 
24  builder.Save();
25  builder.ClipPath(DlPath::MakeCircle(DlPoint(200, 400), 300));
26  builder.Restore();
27  builder.ClipPath(DlPath::MakeCircle(DlPoint(600, 400), 300));
28  builder.ClipPath(DlPath::MakeCircle(DlPoint(400, 600), 300));
29  builder.DrawRect(DlRect::MakeXYWH(200, 200, 400, 400), paint);
30 
31  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
32 }
33 
34 TEST_P(AiksTest, CanRenderDifferenceClips) {
35  DisplayListBuilder builder;
36  builder.Translate(400, 400);
37 
38  // Limit drawing to face circle with a clip.
39  builder.ClipPath(DlPath::MakeCircle(DlPoint(0, 0), 200));
40  builder.Save();
41 
42  // Cut away eyes/mouth using difference clips.
43  builder.ClipPath(DlPath::MakeCircle(DlPoint(-100, -50), 30),
44  DlClipOp::kDifference);
45  builder.ClipPath(DlPath::MakeCircle(DlPoint(100, -50), 30),
46  DlClipOp::kDifference);
47 
48  DlPathBuilder path_builder;
49  path_builder.MoveTo(DlPoint(-100, 50));
50  path_builder.QuadraticCurveTo(DlPoint(0, 150), DlPoint(100, 50));
51  builder.ClipPath(DlPath(path_builder), DlClipOp::kDifference);
52 
53  // Draw a huge yellow rectangle to prove the clipping works.
54  DlPaint paint;
55  paint.setColor(DlColor::kYellow());
56  builder.DrawRect(DlRect::MakeXYWH(-1000, -1000, 2000, 2000), paint);
57 
58  // Remove the difference clips and draw hair that partially covers the eyes.
59  builder.Restore();
60  paint.setColor(DlColor::kMaroon());
61  DlPathBuilder path_builder_2;
62  path_builder_2.MoveTo(DlPoint(200, -200));
63  path_builder_2.LineTo(DlPoint(-200, -200));
64  path_builder_2.LineTo(DlPoint(-200, -40));
65  path_builder_2.CubicCurveTo(DlPoint(0, -40), DlPoint(0, -80),
66  DlPoint(200, -80));
67 
68  builder.DrawPath(DlPath(path_builder_2), paint);
69 
70  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
71 }
72 
73 TEST_P(AiksTest, CanRenderWithContiguousClipRestores) {
74  DisplayListBuilder builder;
75 
76  // Cover the whole canvas with red.
77  DlPaint paint;
78  paint.setColor(DlColor::kRed());
79  builder.DrawPaint(paint);
80 
81  builder.Save();
82 
83  // Append two clips, the second resulting in empty coverage.
84  builder.ClipRect(DlRect::MakeXYWH(100, 100, 100, 100));
85  builder.ClipRect(DlRect::MakeXYWH(300, 300, 100, 100));
86 
87  // Restore to no clips.
88  builder.Restore();
89 
90  // Replace the whole canvas with green.
91  paint.setColor(DlColor::kGreen());
92  builder.DrawPaint(paint);
93 
94  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
95 }
96 
97 TEST_P(AiksTest, ClipsUseCurrentTransform) {
98  std::array<DlColor, 5> colors = {DlColor::kWhite(), DlColor::kBlack(),
99  DlColor::kSkyBlue(), DlColor::kRed(),
100  DlColor::kYellow()};
101  DisplayListBuilder builder;
102  DlPaint paint;
103 
104  builder.Translate(300, 300);
105  for (int i = 0; i < 15; i++) {
106  builder.Scale(0.8, 0.8);
107 
108  paint.setColor(colors[i % colors.size()]);
109  builder.ClipPath(DlPath::MakeCircle(DlPoint(0, 0), 300));
110  builder.DrawRect(DlRect::MakeXYWH(-300, -300, 600, 600), paint);
111  }
112  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
113 }
114 
115 /// If correct, this test should draw a green circle. If any red is visible,
116 /// there is a depth bug.
117 TEST_P(AiksTest, FramebufferBlendsRespectClips) {
118  DisplayListBuilder builder;
119 
120  // Clear the whole canvas with white.
121  DlPaint paint;
122  paint.setColor(DlColor::kWhite());
123  builder.DrawPaint(paint);
124 
125  builder.ClipPath(DlPath::MakeCircle(DlPoint(150, 150), 50),
126  DlClipOp::kIntersect);
127 
128  // Draw a red rectangle that should not show through the circle clip.
129  paint.setColor(DlColor::kRed());
130  paint.setBlendMode(DlBlendMode::kMultiply);
131  builder.DrawRect(DlRect::MakeXYWH(100, 100, 100, 100), paint);
132 
133  // Draw a green circle that shows through the clip.
134  paint.setColor(DlColor::kGreen());
135  paint.setBlendMode(DlBlendMode::kSrcOver);
136  builder.DrawCircle(DlPoint(150, 150), 50, paint);
137 
138  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
139 }
140 
141 } // namespace testing
142 } // namespace impeller
TEST_P(AiksTest, DrawAtlasNoColor)
flutter::DlPoint DlPoint
Definition: dl_dispatcher.h:24
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29