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