| Flutter Impeller |

tessellator_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/testing/testing.h"
6 #include "gtest/gtest.h"
7 #include "impeller/geometry/path.h"
8 #include "impeller/geometry/path_builder.h"
9 #include "impeller/tessellator/tessellator.h"
10
11 namespace impeller {
12 namespace testing {
13
14 TEST(TessellatorTest, TessellatorBuilderReturnsCorrectResultStatus) {
15 // Zero points.
16 {
17 Tessellator t;
18 auto polyline = PathBuilder{}.TakePath().CreatePolyline(1.0f);
19 Tessellator::Result result = t.Tessellate(
20 FillType::kPositive, polyline,
21 [](const float* vertices, size_t vertices_count,
22 const uint16_t* indices, size_t indices_count) { return true; });
23
24 ASSERT_EQ(polyline.points.size(), 0u);
25 ASSERT_EQ(result, Tessellator::Result::kInputError);
26 }
27
28 // One point.
29 {
30 Tessellator t;
31 auto polyline =
32 PathBuilder{}.LineTo({0, 0}).TakePath().CreatePolyline(1.0f);
33 Tessellator::Result result =
34 t.Tessellate(FillType::kPositive, polyline,
35 [](const float* vertices, size_t vertices_count,
36 const uint16_t* indices_count,
37 size_t indices_size) { return true; });
38 ASSERT_EQ(polyline.points.size(), 1u);
39 ASSERT_EQ(result, Tessellator::Result::kSuccess);
40 }
41
42 // Two points.
43 {
44 Tessellator t;
45 auto polyline =
46 PathBuilder{}.AddLine({0, 0}, {0, 1}).TakePath().CreatePolyline(1.0f);
47 Tessellator::Result result =
48 t.Tessellate(FillType::kPositive, polyline,
49 [](const float* vertices, size_t vertices_count,
50 const uint16_t* indices_count,
51 size_t indices_size) { return true; });
52
53 ASSERT_EQ(polyline.points.size(), 2u);
54 ASSERT_EQ(result, Tessellator::Result::kSuccess);
55 }
56
57 // Many points.
58 {
59 Tessellator t;
60 PathBuilder builder;
61 for (int i = 0; i < 1000; i++) {
62 auto coord = i * 1.0f;
63 builder.AddLine({coord, coord}, {coord + 1, coord + 1});
64 }
65 auto polyline = builder.TakePath().CreatePolyline(1.0f);
66 Tessellator::Result result =
67 t.Tessellate(FillType::kPositive, polyline,
68 [](const float* vertices, size_t vertices_count,
69 const uint16_t* indices_count,
70 size_t indices_size) { return true; });
71
72 ASSERT_EQ(polyline.points.size(), 2000u);
73 ASSERT_EQ(result, Tessellator::Result::kSuccess);
74 }
75
76 // Closure fails.
77 {
78 Tessellator t;
79 auto polyline =
80 PathBuilder{}.AddLine({0, 0}, {0, 1}).TakePath().CreatePolyline(1.0f);
81 Tessellator::Result result =
82 t.Tessellate(FillType::kPositive, polyline,
83 [](const float* vertices, size_t vertices_count,
84 const uint16_t* indices_count,
85 size_t indices_size) { return false; });
86
87 ASSERT_EQ(polyline.points.size(), 2u);
88 ASSERT_EQ(result, Tessellator::Result::kInputError);
89 }
90
91 // More than uint16 points, odd fill mode.
92 {
93 Tessellator t;
94 PathBuilder builder = {};
95 for (auto i = 0; i < 1000; i++) {
96 builder.AddCircle(Point(i, i), 4);
97 }
98 auto polyline = builder.TakePath(FillType::kOdd).CreatePolyline(1.0f);
99 bool no_indices = false;
100 size_t count = 0u;
101 Tessellator::Result result = t.Tessellate(
102 FillType::kOdd, polyline,
103 [&no_indices, &count](const float* vertices, size_t vertices_count,
104 const uint16_t* indices, size_t indices_count) {
105 no_indices = indices == nullptr;
106 count = vertices_count;
107 return true;
108 });
109
110 ASSERT_TRUE(no_indices);
111 ASSERT_TRUE(count >= USHRT_MAX);
112 ASSERT_EQ(result, Tessellator::Result::kSuccess);
113 }
114 }
115
116 } // namespace testing
117 } // namespace impeller
path.h
impeller::FillType::kOdd
@ kOdd
impeller::Tessellator
A utility that generates triangles of the specified fill type given a polyline. This happens on the C...
Definition: tessellator.h:35
impeller::Tessellator::Result::kInputError
@ kInputError
impeller::PathBuilder
Definition: path_builder.h:13
impeller::testing::TEST
TEST(AiksCanvasTest, EmptyCullRect)
Definition: canvas_unittests.cc:17
impeller::FillType::kPositive
@ kPositive
tessellator.h
path_builder.h
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
impeller::PathBuilder::LineTo
PathBuilder & LineTo(Point point, bool relative=false)
Insert a line from the current position to point.
Definition: path_builder.cc:46
impeller::Tessellator::Result::kSuccess
@ kSuccess
impeller::PathBuilder::AddLine
PathBuilder & AddLine(const Point &p1, const Point &p2)
Move to point p1, then insert a line from p1 to p2.
Definition: path_builder.cc:433
impeller::PathBuilder::TakePath
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:21
impeller::Tessellator::Tessellate
Tessellator::Result Tessellate(FillType fill_type, const Path::Polyline &polyline, const BuilderCallback &callback) const
Generates filled triangles from the polyline. A callback is invoked once for the entire tessellation.
Definition: tessellator.cc:61
impeller::PathBuilder::AddCircle
PathBuilder & AddCircle(const Point &center, Scalar radius)
Definition: path_builder.cc:198
impeller::Tessellator::Result
Result
Definition: tessellator.h:37
impeller
Definition: aiks_context.cc:10
impeller::Path::CreatePolyline
Polyline CreatePolyline(Scalar scale) const
Definition: path.cc:272