Flutter Engine
The Flutter Engine
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
7#include "flutter/testing/testing.h"
11
12namespace impeller {
13namespace testing {
14
15TEST(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
24TEST(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
33TEST(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
47TEST(SkiaConversionsTest, GradientStopConversion) {
48 // Typical gradient.
49 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
52 std::vector<float> stops = {0.0, 0.5, 1.0};
53 const auto gradient =
55 SkPoint::Make(1.0, 1.0), //
56 3, //
57 colors.data(), //
58 stops.data(), //
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
73TEST(SkiaConversionsTest, GradientMissing0) {
74 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
76 std::vector<float> stops = {0.5, 1.0};
77 const auto gradient =
79 SkPoint::Make(1.0, 1.0), //
80 2, //
81 colors.data(), //
82 stops.data(), //
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
99TEST(SkiaConversionsTest, GradientMissingLastValue) {
100 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
102 std::vector<float> stops = {0.0, .5};
103 const auto gradient =
105 SkPoint::Make(1.0, 1.0), //
106 2, //
107 colors.data(), //
108 stops.data(), //
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
125TEST(SkiaConversionsTest, GradientStopGreaterThan1) {
126 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
129 std::vector<float> stops = {0.0, 100, 1.0};
130 const auto gradient =
132 SkPoint::Make(1.0, 1.0), //
133 3, //
134 colors.data(), //
135 stops.data(), //
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
151TEST(SkiaConversionsTest, GradientConversionNonMonotonic) {
152 std::vector<flutter::DlColor> colors = {
155 std::vector<float> stops = {0.0, 0.5, 0.4, 1.0};
156 const auto gradient =
158 SkPoint::Make(1.0, 1.0), //
159 4, //
160 colors.data(), //
161 stops.data(), //
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
178TEST(SkiaConversionsTest, IsNearlySimpleRRect) {
180 SkRRect::MakeRectXY(SkRect::MakeLTRB(0, 0, 10, 10), 10, 10)));
182 SkRRect::MakeRectXY(SkRect::MakeLTRB(0, 0, 10, 10), 10, 9.999)));
184 SkRRect::MakeRectXY(SkRect::MakeLTRB(0, 0, 10, 10), 10, 9)));
185}
186
187} // namespace testing
188} // namespace impeller
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition: SkRRect.h:180
static std::shared_ptr< DlLinearGradientColorSource > MakeLinear(const SkPoint start_point, const SkPoint end_point, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const SkMatrix *matrix=nullptr)
DlColor color
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
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...
Point ToPoint(const SkPoint &point)
Size ToSize(const SkPoint &point)
bool IsNearlySimpleRRect(const SkRRect &rr)
Like SkRRect.isSimple, but allows the corners to differ by kEhCloseEnough.
Color ToColor(const flutter::DlColor &color)
TEST(AiksCanvasTest, EmptyCullRect)
TPoint< Scalar > Point
Definition: point.h:322
TSize< Scalar > Size
Definition: size.h:137
constexpr bool ScalarNearlyEqual(Scalar x, Scalar y, Scalar tolerance=kEhCloseEnough)
Definition: scalar.h:30
flutter::DlColor DlColor
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
static constexpr DlColor kBlue()
Definition: dl_color.h:26
static constexpr DlColor kRed()
Definition: dl_color.h:24
static constexpr DlColor kGreen()
Definition: dl_color.h:25
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678