| Flutter Impeller |

Public Types | Public Member Functions | List of all members
impeller::TessellatorLibtess Class Reference

An extended tessellator that offers arbitrary/concave tessellation via the libtess2 library. More...

`#include <tessellator_libtess.h>`

## Public Types
enum class | Result { kSuccess , kInputError , kTessellationError }
using | BuilderCallback = std::function< bool(const float *vertices, size_t vertices_count, const uint16_t *indices, size_t indices_count)>
| A callback that returns the results of the tessellation. More...

## Public Member Functions
| TessellatorLibtess ()
| ~TessellatorLibtess ()
TessellatorLibtess::Result | Tessellate (const PathSource &source, Scalar tolerance, const BuilderCallback &callback)
| Generates filled triangles from the path. A callback is invoked once for the entire tessellation. More...

## Detailed Description

An extended tessellator that offers arbitrary/concave tessellation via the libtess2 library.

This object is not thread safe, and its methods must not be called from multiple threads.

Definition at line 29 of file tessellator_libtess.h.

## Member Typedef Documentation

## ◆ BuilderCallback

using impeller::TessellatorLibtess::BuilderCallback = std::function<bool(const float* vertices, size_t vertices_count, const uint16_t* indices, size_t indices_count)>

A callback that returns the results of the tessellation.

       The index buffer may not be populated, in which case [indices] will
       be nullptr and indices_count will be 0.

Definition at line 45 of file tessellator_libtess.h.

## Member Enumeration Documentation

## ◆ Result

enum impeller::TessellatorLibtess::Result
| strong

Enumerator
kSuccess |
kInputError |
kTessellationError |

Definition at line 35 of file tessellator_libtess.h.

35 {
36 kSuccess,
37 kInputError,
38 kTessellationError,
39 };

## Constructor & Destructor Documentation

## ◆ TessellatorLibtess()

impeller::TessellatorLibtess::TessellatorLibtess | ( |  | ) |

Definition at line 35 of file tessellator_libtess.cc.

36 : c_tessellator_(nullptr, &DestroyTessellator) {
37 TESSalloc alloc = kAlloc;
38 {
39 // libTess2 copies the TESSalloc despite the non-const argument.
40 CTessellator tessellator(::tessNewTess(&alloc), &DestroyTessellator);
41 c_tessellator_ = std::move(tessellator);
42 }
43 }
impeller::DestroyTessellator
void DestroyTessellator(TESStesselator *tessellator)
Definition: tessellator_libtess.cc:186
impeller::CTessellator
std::unique_ptr< TESStesselator, decltype(&DestroyTessellator)> CTessellator
Definition: tessellator_libtess.h:20
impeller::kAlloc
static const TESSalloc kAlloc
Definition: tessellator_libtess.cc:25

References impeller::DestroyTessellator(), and impeller::kAlloc.

## ◆ ~TessellatorLibtess()

impeller::TessellatorLibtess::~TessellatorLibtess | ( |  | ) |
| default

## Member Function Documentation

## ◆ Tessellate()

TessellatorLibtess::Result impeller::TessellatorLibtess::Tessellate | ( | const PathSource & | source,
|  | Scalar | tolerance,
|  | const BuilderCallback & | callback
| ) |  |

Generates filled triangles from the path. A callback is invoked once for the entire tessellation.

Parameters

  [in] | source | The path source to tessellate.
  [in] | tolerance | The tolerance value for conversion of the path to a polyline. This value is often derived from the Matrix::GetMaxBasisLength of the CTM applied to the path for rendering.
  [in] | callback | The callback, return false to indicate failure.

Returns
  The result status of the tessellation.

Feed contour information to the tessellator.

Let's tessellate.

Definition at line 85 of file tessellator_libtess.cc.

88 {
89 if (!callback) {
90 return TessellatorLibtess::Result::kInputError;
91 }
92
93 Polyline polyline;
94 PathTessellator::PathToFilledVertices(source, polyline, tolerance);
95
96 auto fill_type = source.GetFillType();
97
98 if (polyline.points.empty()) {
99 return TessellatorLibtess::Result::kInputError;
100 }
101
102 auto tessellator = c_tessellator_.get();
103 if (!tessellator) {
104 return TessellatorLibtess::Result::kTessellationError;
105 }
106
107 constexpr int kVertexSize = 2;
108 constexpr int kPolygonSize = 3;
109
110 //----------------------------------------------------------------------------
111 /// Feed contour information to the tessellator.
112 ///
113 static_assert(sizeof(Point) == 2 * sizeof(float));
114 for (auto contour : polyline.contours) {
115 ::tessAddContour(tessellator, // the C tessellator
116 kVertexSize, //
117 polyline.points.data() + contour.start, //
118 sizeof(Point), //
119 contour.size() //
120 );
121 }
122
123 //----------------------------------------------------------------------------
124 /// Let's tessellate.
125 ///
126 auto result = ::tessTesselate(tessellator, // tessellator
127 ToTessWindingRule(fill_type), // winding
128 TESS_POLYGONS, // element type
129 kPolygonSize, // polygon size
130 kVertexSize, // vertex size
131 nullptr // normal (null is automatic)
132 );
133
134 if (result != 1) {
135 return TessellatorLibtess::Result::kTessellationError;
136 }
137
138 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
139
140 // We default to using a 16bit index buffer, but in cases where we generate
141 // more tessellated data than this can contain we need to fall back to
142 // dropping the index buffer entirely. Instead code could instead switch to
143 // a uint32 index buffer, but this is done for simplicity with the other
144 // fast path above.
145 if (element_item_count < USHRT_MAX) {
146 int vertex_item_count = tessGetVertexCount(tessellator);
147 auto vertices = tessGetVertices(tessellator);
148 auto elements = tessGetElements(tessellator);
149
150 // libtess uses an int index internally due to usage of -1 as a sentinel
151 // value.
152 std::vector<uint16_t> indices(element_item_count);
153 for (int i = 0; i < element_item_count; i++) {
154 indices[i] = static_cast<uint16_t>(elements[i]);
155 }
156 if (!callback(vertices, vertex_item_count, indices.data(),
157 element_item_count)) {
158 return TessellatorLibtess::Result::kInputError;
159 }
160 } else {
161 std::vector<Point> points;
162 std::vector<float> data;
163
164 int vertex_item_count = tessGetVertexCount(tessellator) * kVertexSize;
165 auto vertices = tessGetVertices(tessellator);
166 points.reserve(vertex_item_count);
167 for (int i = 0; i < vertex_item_count; i += 2) {
168 points.emplace_back(vertices[i], vertices[i + 1]);
169 }
170
171 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
172 auto elements = tessGetElements(tessellator);
173 data.reserve(element_item_count);
174 for (int i = 0; i < element_item_count; i++) {
175 data.emplace_back(points[elements[i]].x);
176 data.emplace_back(points[elements[i]].y);
177 }
178 if (!callback(data.data(), element_item_count, nullptr, 0u)) {
179 return TessellatorLibtess::Result::kInputError;
180 }
181 }
182
183 return TessellatorLibtess::Result::kSuccess;
184 }
impeller::PathTessellator::PathToFilledVertices
static void PathToFilledVertices(const PathSource &source, VertexWriter &writer, Scalar scale)
Definition: path_tessellator.cc:307
impeller::TessellatorLibtess::Result::kSuccess
@ kSuccess
impeller::TessellatorLibtess::Result::kInputError
@ kInputError
impeller::TessellatorLibtess::Result::kTessellationError
@ kTessellationError
x
int32_t x
Definition: dl_golden_unittests.cc:458
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::ToTessWindingRule
static int ToTessWindingRule(FillType fill_type)
Definition: tessellator_libtess.cc:47
points
std::vector< Point > points
Definition: tessellator_libtess.cc:76
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68

References data, impeller::PathSource::GetFillType(), kInputError, kSuccess, kTessellationError, impeller::PathTessellator::PathToFilledVertices(), points, impeller::ToTessWindingRule(), and x.

Referenced by impeller::Tessellate(), and impeller::testing::TEST().

The documentation for this class was generated from the following files:

- impeller/tessellator/tessellator_libtess.h
- impeller/tessellator/tessellator_libtess.cc