Flutter Impeller
uber_sdf_contents_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 "gtest/gtest.h"
10 #include "impeller/geometry/rect.h"
11 #include "impeller/geometry/size.h"
12 
13 namespace impeller {
14 namespace testing {
15 
16 TEST(UberSDFContentsTest, ApplyColorFilter) {
17  auto rect = Rect::MakeXYWH(100, 100, 200, 200);
18  auto params =
19  UberSDFParameters::MakeRect(Color::Red(), rect, /*stroke=*/std::nullopt);
20  auto geometry = std::make_unique<UberSDFGeometry>(params);
21  auto contents = UberSDFContents::Make(params, std::move(geometry));
22 
23  EXPECT_EQ(contents->GetColor(), Color::Red());
24 
25  bool result =
26  contents->ApplyColorFilter([](Color color) { return Color::Blue(); });
27 
28  EXPECT_TRUE(result);
29  EXPECT_EQ(contents->GetColor(), Color::Blue());
30 }
31 
32 TEST(UberSDFContentsTest, AsBackgroundColor) {
33  auto rect = Rect::MakeXYWH(-2, -2, 504, 504);
34  auto params =
35  UberSDFParameters::MakeRect(Color::Red(), rect, /*stroke=*/std::nullopt);
36  auto geometry = std::make_unique<UberSDFGeometry>(params);
37  auto contents = UberSDFContents::Make(params, std::move(geometry));
38 
39  Entity entity;
40  entity.SetTransform(Matrix());
41 
42  EXPECT_EQ(contents->AsBackgroundColor(entity, ISize(500, 500)), Color::Red());
43 
44  auto small_bg_color = contents->AsBackgroundColor(entity, ISize(400, 400));
45  EXPECT_TRUE(small_bg_color.has_value());
46  if (small_bg_color.has_value()) {
47  EXPECT_EQ(small_bg_color.value(), Color::Red());
48  }
49 
50  auto huge_bg_color = contents->AsBackgroundColor(entity, ISize(600, 600));
51  EXPECT_FALSE(huge_bg_color.has_value());
52 }
53 
54 TEST(UberSDFContentsTest, AsBackgroundColorExactSize) {
55  auto rect = Rect::MakeXYWH(0, 0, 500, 500);
56  auto params =
57  UberSDFParameters::MakeRect(Color::Red(), rect, /*stroke=*/std::nullopt);
58  auto geometry = std::make_unique<UberSDFGeometry>(params);
59  auto contents = UberSDFContents::Make(params, std::move(geometry));
60 
61  Entity entity;
62  entity.SetTransform(Matrix());
63 
64  auto bg_color = contents->AsBackgroundColor(entity, ISize(500, 500));
65  // The exact size now returns true because over-conservative AA insets are
66  // removed
67  EXPECT_TRUE(bg_color.has_value());
68  if (bg_color.has_value()) {
69  EXPECT_EQ(bg_color.value(), Color::Red());
70  }
71 }
72 
73 TEST(UberSDFContentsTest, AsBackgroundColorNonRect) {
74  Entity entity;
75  entity.SetTransform(Matrix());
76 
77  // Non-rect shape (Circle) should return nullopt
78  auto circle_params = UberSDFParameters::MakeCircle(
79  Color::Red(), Point(250, 250), 250.0f, /*stroke=*/std::nullopt);
80  auto circle_geometry = std::make_unique<UberSDFGeometry>(circle_params);
81  auto circle_contents =
82  UberSDFContents::Make(circle_params, std::move(circle_geometry));
83 
84  auto circle_bg_color =
85  circle_contents->AsBackgroundColor(entity, ISize(500, 500));
86  EXPECT_FALSE(circle_bg_color.has_value());
87 }
88 
89 TEST(UberSDFContentsTest, AsBackgroundColorStrokedRect) {
90  auto rect = Rect::MakeXYWH(-2, -2, 504, 504);
91  auto params = UberSDFParameters::MakeRect(Color::Red(), rect,
92  StrokeParameters{.width = 2.0f});
93  auto geometry = std::make_unique<UberSDFGeometry>(params);
94  auto contents = UberSDFContents::Make(params, std::move(geometry));
95 
96  Entity entity;
97  entity.SetTransform(Matrix());
98 
99  auto bg_color = contents->AsBackgroundColor(entity, ISize(500, 500));
100  EXPECT_FALSE(bg_color.has_value());
101 }
102 
103 } // namespace testing
104 } // namespace impeller
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:62
static std::unique_ptr< UberSDFContents > Make(const UberSDFParameters &params, std::unique_ptr< Geometry > geometry)
TEST(AllocationSizeTest, CanCreateTypedAllocations)
TPoint< Scalar > Point
Definition: point.h:426
ISize64 ISize
Definition: size.h:162
static constexpr Color Red()
Definition: color.h:277
static constexpr Color Blue()
Definition: color.h:281
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
A structure to store all of the parameters related to stroking a path or basic geometry object.
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
static UberSDFParameters MakeCircle(Color color, const Point &center, Scalar radius, std::optional< StrokeParameters > stroke)
Creates UberSDFParameters for a circle.
static UberSDFParameters MakeRect(Color color, const Rect &rect, std::optional< StrokeParameters > stroke)
Creates UberSDFParameters for a rectangle.