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"
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);
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 30 contours, non-zero fill mode.
92  {
93  Tessellator t;
94  PathBuilder builder = {};
95  for (auto i = 0u; i < Tessellator::kMultiContourThreshold + 1; i++) {
96  builder.AddCircle(Point(i, i), 4);
97  }
98  auto polyline = builder.TakePath().CreatePolyline(1.0f);
99  bool no_indices = false;
100  Tessellator::Result result = t.Tessellate(
101  FillType::kNonZero, polyline,
102  [&no_indices](const float* vertices, size_t vertices_count,
103  const uint16_t* indices, size_t indices_count) {
104  no_indices = indices == nullptr;
105  return true;
106  });
107 
108  ASSERT_TRUE(no_indices);
109  ASSERT_EQ(result, Tessellator::Result::kSuccess);
110  }
111 
112  // More than uint16 points, odd fill mode.
113  {
114  Tessellator t;
115  PathBuilder builder = {};
116  for (auto i = 0; i < 1000; i++) {
117  builder.AddCircle(Point(i, i), 4);
118  }
119  auto polyline = builder.TakePath(FillType::kOdd).CreatePolyline(1.0f);
120  bool no_indices = false;
121  size_t count = 0u;
122  Tessellator::Result result = t.Tessellate(
123  FillType::kOdd, polyline,
124  [&no_indices, &count](const float* vertices, size_t vertices_count,
125  const uint16_t* indices, size_t indices_count) {
126  no_indices = indices == nullptr;
127  count = vertices_count;
128  return true;
129  });
130 
131  ASSERT_TRUE(no_indices);
132  ASSERT_TRUE(count >= USHRT_MAX);
133  ASSERT_EQ(result, Tessellator::Result::kSuccess);
134  }
135 }
136 
137 } // namespace testing
138 } // 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::Tessellator::kMultiContourThreshold
static constexpr size_t kMultiContourThreshold
An arbitrary value to determine when a multi-contour non-zero fill path should be split into multiple...
Definition: tessellator.h:49
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::FillType::kNonZero
@ kNonZero
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