Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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>

Inheritance diagram for impeller::TessellatorLibtess:
impeller::Tessellator

Public Types

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.
 
- Public Types inherited from impeller::Tessellator
enum class  Result { kSuccess , kInputError , kTessellationError }
 
using TessellatedVertexProc = std::function< void(const Point &p)>
 A callback function for a |VertexGenerator| to deliver the vertices it computes as |Point| objects.
 

Public Member Functions

 TessellatorLibtess ()
 
 ~TessellatorLibtess ()
 
Tessellator::Result Tessellate (const Path &path, Scalar tolerance, const BuilderCallback &callback)
 Generates filled triangles from the path. A callback is invoked once for the entire tessellation.
 
- Public Member Functions inherited from impeller::Tessellator
 Tessellator ()
 
virtual ~Tessellator ()
 
std::vector< PointTessellateConvex (const Path &path, Scalar tolerance)
 Given a convex path, create a triangle fan structure.
 
Path::Polyline CreateTempPolyline (const Path &path, Scalar tolerance)
 Create a temporary polyline. Only one per-process can exist at a time.
 
EllipticalVertexGenerator FilledCircle (const Matrix &view_transform, const Point &center, Scalar radius)
 Create a |VertexGenerator| that can produce vertices for a filled circle of the given radius around the given center with enough polygon sub-divisions to provide reasonable fidelity when viewed under the given view transform.
 
EllipticalVertexGenerator StrokedCircle (const Matrix &view_transform, const Point &center, Scalar radius, Scalar half_width)
 Create a |VertexGenerator| that can produce vertices for a stroked circle of the given radius and half_width around the given shared center with enough polygon sub-divisions to provide reasonable fidelity when viewed under the given view transform. The outer edge of the stroked circle is generated at (radius + half_width) and the inner edge is generated at (radius - half_width).
 
EllipticalVertexGenerator RoundCapLine (const Matrix &view_transform, const Point &p0, const Point &p1, Scalar radius)
 Create a |VertexGenerator| that can produce vertices for a line with round end caps of the given radius with enough polygon sub-divisions to provide reasonable fidelity when viewed under the given view transform.
 
EllipticalVertexGenerator FilledEllipse (const Matrix &view_transform, const Rect &bounds)
 Create a |VertexGenerator| that can produce vertices for a filled ellipse inscribed within the given bounds with enough polygon sub-divisions to provide reasonable fidelity when viewed under the given view transform.
 
EllipticalVertexGenerator FilledRoundRect (const Matrix &view_transform, const Rect &bounds, const Size &radii)
 Create a |VertexGenerator| that can produce vertices for a filled round rect within the given bounds and corner radii with enough polygon sub-divisions to provide reasonable fidelity when viewed under the given view transform.
 

Additional Inherited Members

- Static Public Attributes inherited from impeller::Tessellator
static constexpr Scalar kCircleTolerance = 0.1f
 The pixel tolerance used by the algorighm to determine how many divisions to create for a circle.
 
- Protected Attributes inherited from impeller::Tessellator
std::unique_ptr< std::vector< Point > > point_buffer_
 Used for polyline generation.
 

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 30 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 40 of file tessellator_libtess.h.

Constructor & Destructor Documentation

◆ TessellatorLibtess()

impeller::TessellatorLibtess::TessellatorLibtess ( )

Definition at line 34 of file tessellator_libtess.cc.

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

◆ ~TessellatorLibtess()

impeller::TessellatorLibtess::~TessellatorLibtess ( )
default

Member Function Documentation

◆ Tessellate()

TessellatorLibtess::Result impeller::TessellatorLibtess::Tessellate ( const Path path,
Scalar  tolerance,
const BuilderCallback callback 
)

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

Parameters
[in]pathThe path to tessellate.
[in]toleranceThe 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]callbackThe 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 56 of file tessellator_libtess.cc.

59 {
60 if (!callback) {
62 }
63
64 point_buffer_->clear();
65 auto polyline =
66 path.CreatePolyline(tolerance, std::move(point_buffer_),
67 [this](Path::Polyline::PointBufferPtr point_buffer) {
68 point_buffer_ = std::move(point_buffer);
69 });
70
71 auto fill_type = path.GetFillType();
72
73 if (polyline.points->empty()) {
75 }
76
77 auto tessellator = c_tessellator_.get();
78 if (!tessellator) {
80 }
81
82 constexpr int kVertexSize = 2;
83 constexpr int kPolygonSize = 3;
84
85 //----------------------------------------------------------------------------
86 /// Feed contour information to the tessellator.
87 ///
88 static_assert(sizeof(Point) == 2 * sizeof(float));
89 for (size_t contour_i = 0; contour_i < polyline.contours.size();
90 contour_i++) {
91 size_t start_point_index, end_point_index;
92 std::tie(start_point_index, end_point_index) =
93 polyline.GetContourPointBounds(contour_i);
94
95 ::tessAddContour(tessellator, // the C tessellator
96 kVertexSize, //
97 polyline.points->data() + start_point_index, //
98 sizeof(Point), //
99 end_point_index - start_point_index //
100 );
101 }
102
103 //----------------------------------------------------------------------------
104 /// Let's tessellate.
105 ///
106 auto result = ::tessTesselate(tessellator, // tessellator
107 ToTessWindingRule(fill_type), // winding
108 TESS_POLYGONS, // element type
109 kPolygonSize, // polygon size
110 kVertexSize, // vertex size
111 nullptr // normal (null is automatic)
112 );
113
114 if (result != 1) {
116 }
117
118 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
119
120 // We default to using a 16bit index buffer, but in cases where we generate
121 // more tessellated data than this can contain we need to fall back to
122 // dropping the index buffer entirely. Instead code could instead switch to
123 // a uint32 index buffer, but this is done for simplicity with the other
124 // fast path above.
125 if (element_item_count < USHRT_MAX) {
126 int vertex_item_count = tessGetVertexCount(tessellator);
127 auto vertices = tessGetVertices(tessellator);
128 auto elements = tessGetElements(tessellator);
129
130 // libtess uses an int index internally due to usage of -1 as a sentinel
131 // value.
132 std::vector<uint16_t> indices(element_item_count);
133 for (int i = 0; i < element_item_count; i++) {
134 indices[i] = static_cast<uint16_t>(elements[i]);
135 }
136 if (!callback(vertices, vertex_item_count, indices.data(),
137 element_item_count)) {
138 return Result::kInputError;
139 }
140 } else {
141 std::vector<Point> points;
142 std::vector<float> data;
143
144 int vertex_item_count = tessGetVertexCount(tessellator) * kVertexSize;
145 auto vertices = tessGetVertices(tessellator);
146 points.reserve(vertex_item_count);
147 for (int i = 0; i < vertex_item_count; i += 2) {
148 points.emplace_back(vertices[i], vertices[i + 1]);
149 }
150
151 int element_item_count = tessGetElementCount(tessellator) * kPolygonSize;
152 auto elements = tessGetElements(tessellator);
153 data.reserve(element_item_count);
154 for (int i = 0; i < element_item_count; i++) {
155 data.emplace_back(points[elements[i]].x);
156 data.emplace_back(points[elements[i]].y);
157 }
158 if (!callback(data.data(), element_item_count, nullptr, 0u)) {
159 return Result::kInputError;
160 }
161 }
162
163 return Result::kSuccess;
164}
static const int points[]
std::unique_ptr< std::vector< Point > > point_buffer_
Used for polyline generation.
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
double y
double x
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
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
TPoint< Scalar > Point
Definition point.h:316
static int ToTessWindingRule(FillType fill_type)
const Path::Polyline & polyline
std::unique_ptr< std::vector< Point > > PointBufferPtr
Definition path.h:97

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