Flutter Engine
The Flutter Engine
geometry_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 <memory>
6#include "flutter/testing/testing.h"
7#include "gtest/gtest.h"
15
16inline ::testing::AssertionResult SolidVerticesNear(
17 std::vector<impeller::SolidFillVertexShader::PerVertexData> a,
18 std::vector<impeller::SolidFillVertexShader::PerVertexData> b) {
19 if (a.size() != b.size()) {
20 return ::testing::AssertionFailure() << "Colors length does not match";
21 }
22 for (auto i = 0u; i < b.size(); i++) {
23 if (!PointNear(a[i].position, b[i].position)) {
24 return ::testing::AssertionFailure() << "Positions are not equal.";
25 }
26 }
27 return ::testing::AssertionSuccess();
28}
29
30inline ::testing::AssertionResult TextureVerticesNear(
31 std::vector<impeller::TextureFillVertexShader::PerVertexData> a,
32 std::vector<impeller::TextureFillVertexShader::PerVertexData> b) {
33 if (a.size() != b.size()) {
34 return ::testing::AssertionFailure() << "Colors length does not match";
35 }
36 for (auto i = 0u; i < b.size(); i++) {
37 if (!PointNear(a[i].position, b[i].position)) {
38 return ::testing::AssertionFailure() << "Positions are not equal.";
39 }
40 if (!PointNear(a[i].texture_coords, b[i].texture_coords)) {
41 return ::testing::AssertionFailure() << "Texture coords are not equal.";
42 }
43 }
44 return ::testing::AssertionSuccess();
45}
46
47#define EXPECT_SOLID_VERTICES_NEAR(a, b) \
48 EXPECT_PRED2(&::SolidVerticesNear, a, b)
49#define EXPECT_TEXTURE_VERTICES_NEAR(a, b) \
50 EXPECT_PRED2(&::TextureVerticesNear, a, b)
51
52namespace impeller {
53
55 public:
56 static std::vector<SolidFillVertexShader::PerVertexData>
59 Scalar miter_limit,
60 Join stroke_join,
61 Cap stroke_cap,
62 Scalar scale) {
63 return StrokePathGeometry::GenerateSolidStrokeVertices(
64 polyline, stroke_width, miter_limit, stroke_join, stroke_cap, scale);
65 }
66};
67
68namespace testing {
69
70TEST(EntityGeometryTest, RectGeometryCoversArea) {
71 auto geometry = Geometry::MakeRect(Rect::MakeLTRB(0, 0, 100, 100));
72 ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(0, 0, 100, 100)));
73 ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(-1, 0, 100, 100)));
74 ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(1, 1, 100, 100)));
75 ASSERT_TRUE(geometry->CoversArea({}, Rect()));
76}
77
78TEST(EntityGeometryTest, FillPathGeometryCoversArea) {
79 auto path = PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath();
80 auto geometry = Geometry::MakeFillPath(
81 path, /* inner rect */ Rect::MakeLTRB(0, 0, 100, 100));
82 ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(0, 0, 100, 100)));
83 ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(-1, 0, 100, 100)));
84 ASSERT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(1, 1, 100, 100)));
85 ASSERT_TRUE(geometry->CoversArea({}, Rect()));
86}
87
88TEST(EntityGeometryTest, FillPathGeometryCoversAreaNoInnerRect) {
89 auto path = PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath();
90 auto geometry = Geometry::MakeFillPath(path);
91 ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(0, 0, 100, 100)));
92 ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(-1, 0, 100, 100)));
93 ASSERT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(1, 1, 100, 100)));
94 ASSERT_FALSE(geometry->CoversArea({}, Rect()));
95}
96
97TEST(EntityGeometryTest, LineGeometryCoverage) {
98 {
99 auto geometry = Geometry::MakeLine({10, 10}, {20, 10}, 2, Cap::kButt);
100 EXPECT_EQ(geometry->GetCoverage({}), Rect::MakeLTRB(10, 9, 20, 11));
101 EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(10, 9, 20, 11)));
102 }
103
104 {
105 auto geometry = Geometry::MakeLine({10, 10}, {20, 10}, 2, Cap::kSquare);
106 EXPECT_EQ(geometry->GetCoverage({}), Rect::MakeLTRB(9, 9, 21, 11));
107 EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(9, 9, 21, 11)));
108 }
109
110 {
111 auto geometry = Geometry::MakeLine({10, 10}, {10, 20}, 2, Cap::kButt);
112 EXPECT_EQ(geometry->GetCoverage({}), Rect::MakeLTRB(9, 10, 11, 20));
113 EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(9, 10, 11, 20)));
114 }
115
116 {
117 auto geometry = Geometry::MakeLine({10, 10}, {10, 20}, 2, Cap::kSquare);
118 EXPECT_EQ(geometry->GetCoverage({}), Rect::MakeLTRB(9, 9, 11, 21));
119 EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(9, 9, 11, 21)));
120 }
121}
122
123TEST(EntityGeometryTest, RoundRectGeometryCoversArea) {
124 auto geometry =
125 Geometry::MakeRoundRect(Rect::MakeLTRB(0, 0, 100, 100), Size(20, 20));
126 EXPECT_FALSE(geometry->CoversArea({}, Rect::MakeLTRB(15, 15, 85, 85)));
127 EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(20, 20, 80, 80)));
128 EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(30, 1, 70, 99)));
129 EXPECT_TRUE(geometry->CoversArea({}, Rect::MakeLTRB(1, 30, 99, 70)));
130}
131
132TEST(EntityGeometryTest, GeometryResultHasReasonableDefaults) {
134 EXPECT_EQ(result.type, PrimitiveType::kTriangleStrip);
135 EXPECT_EQ(result.transform, Matrix());
136 EXPECT_EQ(result.mode, GeometryResult::Mode::kNormal);
137}
138
139TEST(EntityGeometryTest, AlphaCoverageStrokePaths) {
140 Entity entity;
141 entity.SetTransform(Matrix::MakeScale(Vector2{3.0, 3.0}));
142 EXPECT_EQ(Geometry::MakeStrokePath({}, 0.5)->ComputeAlphaCoverage(entity), 1);
143 EXPECT_EQ(Geometry::MakeStrokePath({}, 0.1)->ComputeAlphaCoverage(entity), 1);
144 EXPECT_EQ(Geometry::MakeStrokePath({}, 0.05)->ComputeAlphaCoverage(entity),
145 1);
146 EXPECT_NEAR(Geometry::MakeStrokePath({}, 0.01)->ComputeAlphaCoverage(entity),
147 0.6, 0.1);
148 EXPECT_NEAR(
149 Geometry::MakeStrokePath({}, 0.0000005)->ComputeAlphaCoverage(entity),
150 1e-05, 0.001);
151 EXPECT_EQ(Geometry::MakeStrokePath({}, 0)->ComputeAlphaCoverage(entity), 1);
152 EXPECT_EQ(Geometry::MakeStrokePath({}, 40)->ComputeAlphaCoverage(entity), 1);
153}
154
155} // namespace testing
156} // namespace impeller
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:62
static std::shared_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, Scalar width, Cap cap)
Definition: geometry.cc:97
static std::shared_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:89
static std::shared_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:115
static std::shared_ptr< Geometry > MakeStrokePath(const Path &path, Scalar stroke_width=0.0, Scalar miter_limit=4.0, Cap stroke_cap=Cap::kButt, Join stroke_join=Join::kMiter)
Definition: geometry.cc:72
static std::shared_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:60
static std::vector< SolidFillVertexShader::PerVertexData > GenerateSolidStrokeVertices(const Path::Polyline &polyline, Scalar stroke_width, Scalar miter_limit, Join stroke_join, Cap stroke_cap, Scalar scale)
PathBuilder & AddRect(Rect rect)
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:22
static bool b
struct MyStruct a[10]
GAsyncResult * result
inline ::testing::AssertionResult PointNear(impeller::Point a, impeller::Point b)
inline ::testing::AssertionResult SolidVerticesNear(std::vector< impeller::SolidFillVertexShader::PerVertexData > a, std::vector< impeller::SolidFillVertexShader::PerVertexData > b)
inline ::testing::AssertionResult TextureVerticesNear(std::vector< impeller::TextureFillVertexShader::PerVertexData > a, std::vector< impeller::TextureFillVertexShader::PerVertexData > b)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
TEST(AiksCanvasTest, EmptyCullRect)
Join
Definition: path.h:24
float Scalar
Definition: scalar.h:18
Cap
Definition: path.h:18
TSize< Scalar > Size
Definition: size.h:137
const Scalar stroke_width
const Scalar scale
const Path::Polyline & polyline
@ kNormal
The geometry has no overlapping triangles.
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678