| Flutter Impeller |

tessellator.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 "tessellator.h"
6
7 #include <vector>
8
9 #include "impeller/tessellator/tessellator_libtess.h"
10
11 namespace impeller {
12 PathBuilder* CreatePathBuilder() {
13 return new PathBuilder();
14 }
15
16 void DestroyPathBuilder(PathBuilder* builder) {
17 delete builder;
18 }
19
20 void MoveTo(PathBuilder* builder, Scalar x, Scalar y) {
21 builder->MoveTo(Point(x, y));
22 }
23
24 void LineTo(PathBuilder* builder, Scalar x, Scalar y) {
25 builder->LineTo(Point(x, y));
26 }
27
28 void CubicTo(PathBuilder* builder,
29 Scalar x1,
30 Scalar y1,
31 Scalar x2,
32 Scalar y2,
33 Scalar x3,
34 Scalar y3) {
35 builder->CubicCurveTo(Point(x1, y1), Point(x2, y2), Point(x3, y3));
36 }
37
38 void Close(PathBuilder* builder) {
39 builder->Close();
40 }
41
42 struct Vertices* Tessellate(PathBuilder* builder,
43 int fill_type,
44 Scalar tolerance) {
45 builder->SetFillType(static_cast<FillType>(fill_type));
46 auto path = builder->CopyPath();
47 std::vector<float> points;
48 if (TessellatorLibtess{}.Tessellate(
49 path, tolerance,
50 [&points](const float* vertices, size_t vertices_count,
51 const uint16_t* indices, size_t indices_count) {
52 // Results are expected to be re-duplicated.
53 std::vector<Point> raw_points;
54 for (auto i = 0u; i < vertices_count * 2; i += 2) {
55 raw_points.emplace_back(Point{vertices[i], vertices[i + 1]});
56 }
57 for (auto i = 0u; i < indices_count; i++) {
58 auto point = raw_points[indices[i]];
59 points.push_back(point.x);
60 points.push_back(point.y);
61 }
62 return true;
63 }) != TessellatorLibtess::Result::kSuccess) {
64 return nullptr;
65 }
66
67 Vertices* vertices = new Vertices();
68 vertices->points = new float[points.size()];
69 if (!vertices->points) {
70 return nullptr;
71 }
72 vertices->length = points.size();
73 std::copy(points.begin(), points.end(), vertices->points);
74 return vertices;
75 }
76
77 void DestroyVertices(Vertices* vertices) {
78 delete vertices->points;
79 delete vertices;
80 }
81
82 } // namespace impeller
impeller::TessellatorLibtess
An extended tessellator that offers arbitrary/concave tessellation via the libtess2 library.
Definition: tessellator_libtess.h:29
impeller::TessellatorLibtess::Result::kSuccess
@ kSuccess
impeller::TessellatorLibtess::Tessellate
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.
Definition: tessellator_libtess.cc:85
x
int32_t x
Definition: dl_golden_unittests.cc:458
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::Tessellate
struct Vertices * Tessellate(PathBuilder *builder, int fill_type, Scalar tolerance)
Definition: tessellator.cc:42
impeller::FillType
FillType
Definition: path_source.h:13
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::CreatePathBuilder
PathBuilder * CreatePathBuilder()
Definition: tessellator.cc:12
impeller::DestroyPathBuilder
void DestroyPathBuilder(PathBuilder *builder)
Definition: tessellator.cc:16
impeller::PathBuilder
flutter::DlPathBuilder PathBuilder
Definition: tessellator.h:22
impeller::DestroyVertices
void DestroyVertices(Vertices *vertices)
Definition: tessellator.cc:77
impeller::MoveTo
void MoveTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:20
impeller::LineTo
void LineTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:24
impeller::CubicTo
void CubicTo(PathBuilder *builder, Scalar x1, Scalar y1, Scalar x2, Scalar y2, Scalar x3, Scalar y3)
Definition: tessellator.cc:28
impeller::Close
void Close(PathBuilder *builder)
Definition: tessellator.cc:38
impeller::TPoint< Scalar >
impeller::Vertices
Definition: tessellator.h:24
impeller::Vertices::points
float * points
Definition: tessellator.h:25
tessellator.h
points
std::vector< Point > points
Definition: tessellator_libtess.cc:76
tessellator_libtess.h