Flutter Engine
display_list_color_filter_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/display_list/display_list_attributes_testing.h"
6 #include "flutter/display_list/display_list_builder.h"
7 #include "flutter/display_list/display_list_color_filter.h"
8 #include "flutter/display_list/types.h"
9 
10 namespace flutter {
11 namespace testing {
12 
13 static const float kMatrix[20] = {
14  1, 2, 3, 4, 5, //
15  6, 7, 8, 9, 10, //
16  11, 12, 13, 14, 15, //
17  16, 17, 18, 19, 20, //
18 };
19 
20 TEST(DisplayListColorFilter, BuilderSetGet) {
22  DisplayListBuilder builder;
23  ASSERT_EQ(builder.getColorFilter(), nullptr);
24  builder.setColorFilter(&filter);
25  ASSERT_NE(builder.getColorFilter(), nullptr);
26  ASSERT_TRUE(
27  Equals(builder.getColorFilter(), static_cast<DlColorFilter*>(&filter)));
28  builder.setColorFilter(nullptr);
29  ASSERT_EQ(builder.getColorFilter(), nullptr);
30 }
31 
32 TEST(DisplayListColorFilter, FromSkiaNullFilter) {
33  std::shared_ptr<DlColorFilter> filter = DlColorFilter::From(nullptr);
34  ASSERT_EQ(filter, nullptr);
35  ASSERT_EQ(filter.get(), nullptr);
36 }
37 
38 TEST(DisplayListColorFilter, FromSkiaBlendFilter) {
39  sk_sp<SkColorFilter> sk_filter =
40  SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kDstATop);
41  std::shared_ptr<DlColorFilter> filter = DlColorFilter::From(sk_filter);
43  ASSERT_EQ(filter->type(), DlColorFilterType::kBlend);
44  ASSERT_EQ(*filter->asBlend(), dl_filter);
45  ASSERT_EQ(filter->asBlend()->color(), DlColor::kRed());
46  ASSERT_EQ(filter->asBlend()->mode(), DlBlendMode::kDstATop);
47 
48  ASSERT_EQ(filter->asMatrix(), nullptr);
49 }
50 
51 TEST(DisplayListColorFilter, FromSkiaMatrixFilter) {
52  sk_sp<SkColorFilter> sk_filter = SkColorFilters::Matrix(kMatrix);
53  std::shared_ptr<DlColorFilter> filter = DlColorFilter::From(sk_filter);
54  DlMatrixColorFilter dl_filter(kMatrix);
55  ASSERT_EQ(filter->type(), DlColorFilterType::kMatrix);
56  ASSERT_EQ(*filter->asMatrix(), dl_filter);
57  const DlMatrixColorFilter* matrix_filter = filter->asMatrix();
58  for (int i = 0; i < 20; i++) {
59  ASSERT_EQ((*matrix_filter)[i], kMatrix[i]);
60  }
61 
62  ASSERT_EQ(filter->asBlend(), nullptr);
63 }
64 
65 TEST(DisplayListColorFilter, FromSkiaSrgbToLinearFilter) {
66  sk_sp<SkColorFilter> sk_filter = SkColorFilters::SRGBToLinearGamma();
67  std::shared_ptr<DlColorFilter> filter = DlColorFilter::From(sk_filter);
68  ASSERT_EQ(filter->type(), DlColorFilterType::kSrgbToLinearGamma);
69 
70  ASSERT_EQ(filter->asBlend(), nullptr);
71  ASSERT_EQ(filter->asMatrix(), nullptr);
72 }
73 
74 TEST(DisplayListColorFilter, FromSkiaLinearToSrgbFilter) {
75  sk_sp<SkColorFilter> sk_filter = SkColorFilters::LinearToSRGBGamma();
76  std::shared_ptr<DlColorFilter> filter = DlColorFilter::From(sk_filter);
77  ASSERT_EQ(filter->type(), DlColorFilterType::kLinearToSrgbGamma);
78 
79  ASSERT_EQ(filter->asBlend(), nullptr);
80  ASSERT_EQ(filter->asMatrix(), nullptr);
81 }
82 
83 TEST(DisplayListColorFilter, FromSkiaUnrecognizedFilter) {
84  sk_sp<SkColorFilter> sk_inputA =
85  SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kOverlay);
86  sk_sp<SkColorFilter> sk_inputB =
87  SkColorFilters::Blend(SK_ColorBLUE, SkBlendMode::kScreen);
88  sk_sp<SkColorFilter> sk_filter =
89  SkColorFilters::Compose(sk_inputA, sk_inputB);
90  std::shared_ptr<DlColorFilter> filter = DlColorFilter::From(sk_filter);
91  ASSERT_EQ(filter->type(), DlColorFilterType::kUnknown);
92  ASSERT_EQ(filter->skia_object(), sk_filter);
93 
94  ASSERT_EQ(filter->asBlend(), nullptr);
95  ASSERT_EQ(filter->asMatrix(), nullptr);
96 }
97 
98 TEST(DisplayListColorFilter, BlendConstructor) {
100 }
101 
102 TEST(DisplayListColorFilter, BlendShared) {
104  ASSERT_NE(filter.shared().get(), &filter);
105  ASSERT_EQ(*filter.shared(), filter);
106 }
107 
108 TEST(DisplayListColorFilter, BlendAsBlend) {
110  ASSERT_NE(filter.asBlend(), nullptr);
111  ASSERT_EQ(filter.asBlend(), &filter);
112 }
113 
114 TEST(DisplayListColorFilter, BlendContents) {
116  ASSERT_EQ(filter.color(), DlColor::kRed());
117  ASSERT_EQ(filter.mode(), DlBlendMode::kDstATop);
118 }
119 
120 TEST(DisplayListColorFilter, BlendEquals) {
123  TestEquals(filter1, filter2);
124 }
125 
126 TEST(DisplayListColorFilter, BlendNotEquals) {
130  TestNotEquals(filter1, filter2, "Color differs");
131  TestNotEquals(filter1, filter3, "Blend mode differs");
132 }
133 
134 TEST(DisplayListColorFilter, NopBlendShouldNotCrash) {
136  ASSERT_FALSE(filter.modifies_transparent_black());
137 }
138 
139 TEST(DisplayListColorFilter, MatrixConstructor) {
140  DlMatrixColorFilter filter(kMatrix);
141 }
142 
143 TEST(DisplayListColorFilter, MatrixShared) {
144  DlMatrixColorFilter filter(kMatrix);
145  ASSERT_NE(filter.shared().get(), &filter);
146  ASSERT_EQ(*filter.shared(), filter);
147 }
148 
149 TEST(DisplayListColorFilter, MatrixAsMatrix) {
150  DlMatrixColorFilter filter(kMatrix);
151  ASSERT_NE(filter.asMatrix(), nullptr);
152  ASSERT_EQ(filter.asMatrix(), &filter);
153 }
154 
155 TEST(DisplayListColorFilter, MatrixContents) {
156  float matrix_[20];
157  memcpy(matrix_, kMatrix, sizeof(matrix_));
158  DlMatrixColorFilter filter(matrix_);
159 
160  // Test deref operator []
161  for (int i = 0; i < 20; i++) {
162  ASSERT_EQ(filter[i], matrix_[i]);
163  }
164 
165  // Test get_matrix
166  float matrix2[20];
167  filter.get_matrix(matrix2);
168  for (int i = 0; i < 20; i++) {
169  ASSERT_EQ(matrix2[i], matrix_[i]);
170  }
171 
172  // Test perturbing original array does not affect filter
173  float original_value = matrix_[4];
174  matrix_[4] += 101;
175  ASSERT_EQ(filter[4], original_value);
176 }
177 
178 TEST(DisplayListColorFilter, MatrixEquals) {
179  DlMatrixColorFilter filter1(kMatrix);
180  DlMatrixColorFilter filter2(kMatrix);
181  TestEquals(filter1, filter2);
182 }
183 
184 TEST(DisplayListColorFilter, MatrixNotEquals) {
185  float matrix_[20];
186  memcpy(matrix_, kMatrix, sizeof(matrix_));
187  DlMatrixColorFilter filter1(matrix_);
188  matrix_[4] += 101;
189  DlMatrixColorFilter filter2(matrix_);
190  TestNotEquals(filter1, filter2, "Matrix differs");
191 }
192 
193 TEST(DisplayListColorFilter, NopMatrixShouldNotCrash) {
194  float matrix[20] = {
195  1, 0, 0, 0, 0, //
196  0, 1, 0, 0, 0, //
197  0, 0, 1, 0, 0, //
198  0, 0, 0, 1, 0, //
199  };
200  DlMatrixColorFilter filter(matrix);
201  ASSERT_FALSE(filter.modifies_transparent_black());
202 }
203 
204 TEST(DisplayListColorFilter, SrgbToLinearConstructor) {
206 }
207 
208 TEST(DisplayListColorFilter, SrgbToLinearShared) {
210  ASSERT_NE(filter.shared().get(), &filter);
211  ASSERT_EQ(*filter.shared(), filter);
212 }
213 
214 TEST(DisplayListColorFilter, SrgbToLinearEquals) {
217  TestEquals(filter1, filter2);
219 }
220 
221 TEST(DisplayListColorFilter, LinearToSrgbConstructor) {
223 }
224 
225 TEST(DisplayListColorFilter, LinearToSrgbShared) {
227  ASSERT_NE(filter.shared().get(), &filter);
228  ASSERT_EQ(*filter.shared(), filter);
229 }
230 
231 TEST(DisplayListColorFilter, LinearToSrgbEquals) {
234  TestEquals(filter1, filter2);
236 }
237 
238 TEST(DisplayListColorFilter, UnknownConstructor) {
239  DlUnknownColorFilter filter(SkColorFilters::LinearToSRGBGamma());
240 }
241 
242 TEST(DisplayListColorFilter, UnknownShared) {
243  DlUnknownColorFilter filter(SkColorFilters::LinearToSRGBGamma());
244  ASSERT_NE(filter.shared().get(), &filter);
245  ASSERT_EQ(*filter.shared(), filter);
246 }
247 
248 TEST(DisplayListColorFilter, UnknownContents) {
249  sk_sp<SkColorFilter> sk_filter = SkColorFilters::LinearToSRGBGamma();
250  DlUnknownColorFilter filter(sk_filter);
251  ASSERT_EQ(sk_filter, filter.skia_object());
252  ASSERT_EQ(sk_filter.get(), filter.skia_object().get());
253 }
254 
255 TEST(DisplayListColorFilter, UnknownEquals) {
256  sk_sp<SkColorFilter> sk_filter = SkColorFilters::LinearToSRGBGamma();
257  DlUnknownColorFilter filter1(sk_filter);
258  DlUnknownColorFilter filter2(sk_filter);
259  TestEquals(filter1, filter2);
260 }
261 
262 TEST(DisplayListColorFilter, UnknownNotEquals) {
263  // Even though the filter is the same, it is a different instance
264  // and we cannot currently tell them apart because the Skia
265  // ColorFilter objects do not implement ==
266  DlUnknownColorFilter filter1(
267  SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kDstATop));
268  DlUnknownColorFilter filter2(
269  SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kDstATop));
270  TestNotEquals(filter1, filter2, "SkColorFilter instance differs");
271 }
272 
273 } // namespace testing
274 } // namespace flutter
void get_matrix(float matrix[20]) const
bool Equals(const T *a, const T *b)
r = d*sa + s*(1-da)
static constexpr DlColor kRed()
std::shared_ptr< const DlColorFilter > getColorFilter() const
TEST(DisplayListColorFilter, BuilderSetGet)
static void TestNotEquals(T &source1, T &source2, std::string label)
static constexpr DlColor kBlue()
std::shared_ptr< DlColorFilter > shared() const override
multiply or screen, depending on destination
static const std::shared_ptr< DlLinearToSrgbGammaColorFilter > instance
void setColorFilter(const DlColorFilter *filter) override
static std::shared_ptr< DlColorFilter > From(SkColorFilter *sk_filter)
bool modifies_transparent_black() const override
const DlBlendColorFilter * asBlend() const override
static constexpr DlColor kTransparent()
sk_sp< SkColorFilter > skia_object() const override
std::shared_ptr< DlColorFilter > shared() const override
static const std::shared_ptr< DlSrgbToLinearGammaColorFilter > instance
const DlMatrixColorFilter * asMatrix() const override
std::shared_ptr< DlColorFilter > shared() const override
static void TestEquals(T &source1, T &source2)
bool modifies_transparent_black() const override
std::shared_ptr< DlColorFilter > shared() const override
std::shared_ptr< DlColorFilter > shared() const override