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