| Flutter Impeller |

paint_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 "flutter/impeller/display_list/paint.h"
6
7 #include "gtest/gtest.h"
8
9 #include "flutter/display_list/dl_color.h"
10 #include "flutter/display_list/dl_tile_mode.h"
11 #include "flutter/display_list/effects/dl_color_source.h"
12 #include "flutter/impeller/geometry/scalar.h"
13
14 namespace impeller {
15 namespace testing {
16
17 TEST(PaintTest, GradientStopConversion) {
18 // Typical gradient.
19 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
20 flutter::DlColor::kRed(),
21 flutter::DlColor::kGreen()};
22 std::vector<float> stops = {0.0, 0.5, 1.0};
23 const auto gradient =
24 flutter::DlColorSource::MakeLinear(flutter::DlPoint(0, 0), //
25 flutter::DlPoint(1.0, 1.0), //
26 3, //
27 colors.data(), //
28 stops.data(), //
29 flutter::DlTileMode::kClamp, //
30 nullptr //
31 );
32
33 std::vector<Color> converted_colors;
34 std::vector<Scalar> converted_stops;
35 Paint::ConvertStops(gradient->asLinearGradient(), converted_colors,
36 converted_stops);
37
38 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
39 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
40 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
41 }
42
43 TEST(PaintTest, GradientMissing0) {
44 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
45 flutter::DlColor::kRed()};
46 std::vector<float> stops = {0.5, 1.0};
47 const auto gradient =
48 flutter::DlColorSource::MakeLinear(flutter::DlPoint(0, 0), //
49 flutter::DlPoint(1.0, 1.0), //
50 2, //
51 colors.data(), //
52 stops.data(), //
53 flutter::DlTileMode::kClamp, //
54 nullptr //
55 );
56
57 std::vector<Color> converted_colors;
58 std::vector<Scalar> converted_stops;
59 Paint::ConvertStops(gradient->asLinearGradient(), converted_colors,
60 converted_stops);
61
62 // First color is inserted as blue.
63 ASSERT_TRUE(ScalarNearlyEqual(converted_colors[0].blue, 1.0f));
64 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
65 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
66 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
67 }
68
69 TEST(PaintTest, GradientMissingLastValue) {
70 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
71 flutter::DlColor::kRed()};
72 std::vector<float> stops = {0.0, .5};
73 const auto gradient =
74 flutter::DlColorSource::MakeLinear(flutter::DlPoint(0, 0), //
75 flutter::DlPoint(1.0, 1.0), //
76 2, //
77 colors.data(), //
78 stops.data(), //
79 flutter::DlTileMode::kClamp, //
80 nullptr //
81 );
82
83 std::vector<Color> converted_colors;
84 std::vector<Scalar> converted_stops;
85 Paint::ConvertStops(gradient->asLinearGradient(), converted_colors,
86 converted_stops);
87
88 // Last color is inserted as red.
89 ASSERT_TRUE(ScalarNearlyEqual(converted_colors[2].red, 1.0f));
90 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
91 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
92 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
93 }
94
95 TEST(PaintTest, GradientStopGreaterThan1) {
96 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
97 flutter::DlColor::kGreen(),
98 flutter::DlColor::kRed()};
99 std::vector<float> stops = {0.0, 100, 1.0};
100 const auto gradient =
101 flutter::DlColorSource::MakeLinear(flutter::DlPoint(0, 0), //
102 flutter::DlPoint(1.0, 1.0), //
103 3, //
104 colors.data(), //
105 stops.data(), //
106 flutter::DlTileMode::kClamp, //
107 nullptr //
108 );
109
110 std::vector<Color> converted_colors;
111 std::vector<Scalar> converted_stops;
112 Paint::ConvertStops(gradient->asLinearGradient(), converted_colors,
113 converted_stops);
114
115 // Value is clamped to 1.0
116 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
117 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 1.0f));
118 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 1.0f));
119 }
120
121 TEST(PaintTest, GradientConversionNonMonotonic) {
122 std::vector<flutter::DlColor> colors = {
123 flutter::DlColor::kBlue(), flutter::DlColor::kGreen(),
124 flutter::DlColor::kGreen(), flutter::DlColor::kRed()};
125 std::vector<float> stops = {0.0, 0.5, 0.4, 1.0};
126 const auto gradient =
127 flutter::DlColorSource::MakeLinear(flutter::DlPoint(0, 0), //
128 flutter::DlPoint(1.0, 1.0), //
129 4, //
130 colors.data(), //
131 stops.data(), //
132 flutter::DlTileMode::kClamp, //
133 nullptr //
134 );
135
136 std::vector<Color> converted_colors;
137 std::vector<Scalar> converted_stops;
138 Paint::ConvertStops(gradient->asLinearGradient(), converted_colors,
139 converted_stops);
140
141 // Value is clamped to 0.5
142 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[0], 0.0f));
143 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[1], 0.5f));
144 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[2], 0.5f));
145 ASSERT_TRUE(ScalarNearlyEqual(converted_stops[3], 1.0f));
146 }
147
148 } // namespace testing
149 } // namespace impeller
paint.h
impeller::testing::TEST
TEST(AllocationSizeTest, CanCreateTypedAllocations)
Definition: allocation_size_unittests.cc:10
impeller
Definition: allocation.cc:12
impeller::DlPoint
flutter::DlPoint DlPoint
Definition: dl_dispatcher.h:24
impeller::ScalarNearlyEqual
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:35
scalar.h
impeller::Paint::ConvertStops
static 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: paint.cc:37