| Flutter Impeller |

skia_conversions_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 "display_list/dl_color.h"
6 #include "display_list/dl_tile_mode.h"
7 #include "flutter/testing/testing.h"
8 #include "impeller/display_list/skia_conversions.h"
9 #include "impeller/geometry/scalar.h"
10 #include "include/core/SkRRect.h"
11
12 namespace impeller {
13 namespace testing {
14
15 TEST(SkiaConversionsTest, SkPointToPoint) {
16 for (int x = -100; x < 100; x += 4) {
17 for (int y = -100; y < 100; y += 4) {
18 EXPECT_EQ(skia_conversions::ToPoint(SkPoint::Make(x * 0.25f, y * 0.25f)),
19 Point(x * 0.25f, y * 0.25f));
20 }
21 }
22 }
23
24 TEST(SkiaConversionsTest, SkPointToSize) {
25 for (int x = -100; x < 100; x += 4) {
26 for (int y = -100; y < 100; y += 4) {
27 EXPECT_EQ(skia_conversions::ToSize(SkPoint::Make(x * 0.25f, y * 0.25f)),
28 Size(x * 0.25f, y * 0.25f));
29 }
30 }
31 }
32
33 TEST(SkiaConversionsTest, ToColor) {
34 // Create a color with alpha, red, green, and blue values that are all
35 // trivially divisible by 255 so that we can test the conversion results in
36 // correct scalar values.
37 // AARRGGBB
38 const flutter::DlColor color = flutter::DlColor(0x8040C020);
39 auto converted_color = skia_conversions::ToColor(color);
40
41 ASSERT_TRUE(ScalarNearlyEqual(converted_color.alpha, 0x80 * (1.0f / 255)));
42 ASSERT_TRUE(ScalarNearlyEqual(converted_color.red, 0x40 * (1.0f / 255)));
43 ASSERT_TRUE(ScalarNearlyEqual(converted_color.green, 0xC0 * (1.0f / 255)));
44 ASSERT_TRUE(ScalarNearlyEqual(converted_color.blue, 0x20 * (1.0f / 255)));
45 }
46
47 TEST(SkiaConversionsTest, GradientStopConversion) {
48 // Typical gradient.
49 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
50 flutter::DlColor::kRed(),
51 flutter::DlColor::kGreen()};
52 std::vector<float> stops = {0.0, 0.5, 1.0};
53 const auto gradient =
54 flutter::DlColorSource::MakeLinear(SkPoint::Make(0, 0), //
55 SkPoint::Make(1.0, 1.0), //
56 3, //
57 colors.data(), //
58 stops.data(), //
59 flutter::DlTileMode::kClamp, //
60 nullptr //
61 );
62
63 std::vector<Color> converted_colors;
64 std::vector<Scalar> converted_stops;
65 skia_conversions::ConvertStops(gradient.get(), converted_colors,
66 converted_stops);
67
68 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
69 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
70 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
71 }
72
73 TEST(SkiaConversionsTest, GradientMissing0) {
74 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
75 flutter::DlColor::kRed()};
76 std::vector<float> stops = {0.5, 1.0};
77 const auto gradient =
78 flutter::DlColorSource::MakeLinear(SkPoint::Make(0, 0), //
79 SkPoint::Make(1.0, 1.0), //
80 2, //
81 colors.data(), //
82 stops.data(), //
83 flutter::DlTileMode::kClamp, //
84 nullptr //
85 );
86
87 std::vector<Color> converted_colors;
88 std::vector<Scalar> converted_stops;
89 skia_conversions::ConvertStops(gradient.get(), converted_colors,
90 converted_stops);
91
92 // First color is inserted as blue.
93 ASSERT_TRUE(ScalarNearlyEqual(converted_colors[0].blue, 1.0f));
94 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
95 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
96 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
97 }
98
99 TEST(SkiaConversionsTest, GradientMissingLastValue) {
100 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
101 flutter::DlColor::kRed()};
102 std::vector<float> stops = {0.0, .5};
103 const auto gradient =
104 flutter::DlColorSource::MakeLinear(SkPoint::Make(0, 0), //
105 SkPoint::Make(1.0, 1.0), //
106 2, //
107 colors.data(), //
108 stops.data(), //
109 flutter::DlTileMode::kClamp, //
110 nullptr //
111 );
112
113 std::vector<Color> converted_colors;
114 std::vector<Scalar> converted_stops;
115 skia_conversions::ConvertStops(gradient.get(), converted_colors,
116 converted_stops);
117
118 // Last color is inserted as red.
119 ASSERT_TRUE(ScalarNearlyEqual(converted_colors[2].red, 1.0f));
120 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
121 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
122 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
123 }
124
125 TEST(SkiaConversionsTest, GradientStopGreaterThan1) {
126 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
127 flutter::DlColor::kGreen(),
128 flutter::DlColor::kRed()};
129 std::vector<float> stops = {0.0, 100, 1.0};
130 const auto gradient =
131 flutter::DlColorSource::MakeLinear(SkPoint::Make(0, 0), //
132 SkPoint::Make(1.0, 1.0), //
133 3, //
134 colors.data(), //
135 stops.data(), //
136 flutter::DlTileMode::kClamp, //
137 nullptr //
138 );
139
140 std::vector<Color> converted_colors;
141 std::vector<Scalar> converted_stops;
142 skia_conversions::ConvertStops(gradient.get(), converted_colors,
143 converted_stops);
144
145 // Value is clamped to 1.0
146 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
147 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 1.0f));
148 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
149 }
150
151 TEST(SkiaConversionsTest, GradientConversionNonMonotonic) {
152 std::vector<flutter::DlColor> colors = {
153 flutter::DlColor::kBlue(), flutter::DlColor::kGreen(),
154 flutter::DlColor::kGreen(), flutter::DlColor::kRed()};
155 std::vector<float> stops = {0.0, 0.5, 0.4, 1.0};
156 const auto gradient =
157 flutter::DlColorSource::MakeLinear(SkPoint::Make(0, 0), //
158 SkPoint::Make(1.0, 1.0), //
159 4, //
160 colors.data(), //
161 stops.data(), //
162 flutter::DlTileMode::kClamp, //
163 nullptr //
164 );
165
166 std::vector<Color> converted_colors;
167 std::vector<Scalar> converted_stops;
168 skia_conversions::ConvertStops(gradient.get(), converted_colors,
169 converted_stops);
170
171 // Value is clamped to 0.5
172 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
173 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
174 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 0.5f));
175 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[3], 1.0f));
176 }
177
178 TEST(SkiaConversionsTest, IsNearlySimpleRRect) {
179 EXPECT_TRUE(skia_conversions::IsNearlySimpleRRect(
180 SkRRect::MakeRectXY(SkRect::MakeLTRB(0, 0, 10, 10), 10, 10)));
181 EXPECT_TRUE(skia_conversions::IsNearlySimpleRRect(
182 SkRRect::MakeRectXY(SkRect::MakeLTRB(0, 0, 10, 10), 10, 9.999)));
183 EXPECT_FALSE(skia_conversions::IsNearlySimpleRRect(
184 SkRRect::MakeRectXY(SkRect::MakeLTRB(0, 0, 10, 10), 10, 9)));
185 }
186
187 } // namespace testing
188 } // namespace impeller
impeller::skia_conversions::IsNearlySimpleRRect
bool IsNearlySimpleRRect(const SkRRect &rr)
Like SkRRect.isSimple, but allows the corners to differ by kEhCloseEnough.
Definition: skia_conversions.cc:12
impeller::skia_conversions::ConvertStops
void ConvertStops(const flutter::DlGradientColorSourceBase *gradient, std::vector< Color > &colors, std::vector< float > &stops)
Convert display list colors + stops into impeller colors and stops, taking care to ensure that the st...
Definition: skia_conversions.cc:212
impeller::skia_conversions::ToSize
Size ToSize(const SkPoint &point)
Definition: skia_conversions.cc:158
impeller::testing::TEST
TEST(AiksCanvasTest, EmptyCullRect)
Definition: canvas_unittests.cc:18
impeller::skia_conversions::ToColor
Color ToColor(const flutter::DlColor &color)
Definition: skia_conversions.cc:162
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::Point
TPoint< Scalar > Point
Definition: point.h:322
skia_conversions.h
impeller::skia_conversions::ToPoint
Point ToPoint(const SkPoint &point)
Definition: skia_conversions.cc:154
scalar.h
impeller::ScalarNearlyEqual
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:30
color
DlColor color
Definition: dl_golden_blur_unittests.cc:23
impeller
Definition: aiks_blend_unittests.cc:18