| 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 auto path = builder->CopyPath(static_cast<FillType>(fill_type));
46 std::vector<float> points;
47 if (TessellatorLibtess{}.Tessellate(
48 path, tolerance,
49 [&points](const float* vertices, size_t vertices_count,
50 const uint16_t* indices, size_t indices_count) {
51 // Results are expected to be re-duplicated.
52 std::vector<Point> raw_points;
53 for (auto i = 0u; i < vertices_count * 2; i += 2) {
54 raw_points.emplace_back(Point{vertices[i], vertices[i + 1]});
55 }
56 for (auto i = 0u; i < indices_count; i++) {
57 auto point = raw_points[indices[i]];
58 points.push_back(point.x);
59 points.push_back(point.y);
60 }
61 return true;
62 }) != TessellatorLibtess::Result::kSuccess) {
63 return nullptr;
64 }
65
66 Vertices* vertices = new Vertices();
67 vertices->points = new float[points.size()];
68 if (!vertices->points) {
69 return nullptr;
70 }
71 vertices->length = points.size();
72 std::copy(points.begin(), points.end(), vertices->points);
73 return vertices;
74 }
75
76 void DestroyVertices(Vertices* vertices) {
77 delete vertices->points;
78 delete vertices;
79 }
80
81 } // namespace impeller
impeller::PathBuilder
Definition: path_builder.h:16
impeller::PathBuilder::LineTo
PathBuilder & LineTo(Point point, bool relative=false)
Insert a line from the current position to point.
Definition: path_builder.cc:66
impeller::PathBuilder::MoveTo
PathBuilder & MoveTo(Point point, bool relative=false)
Definition: path_builder.cc:47
impeller::PathBuilder::Close
PathBuilder & Close()
Definition: path_builder.cc:54
impeller::PathBuilder::CopyPath
Path CopyPath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:21
impeller::PathBuilder::CubicCurveTo
PathBuilder & CubicCurveTo(Point controlPoint1, Point controlPoint2, Point point, bool relative=false)
Insert a cubic curve from the curren position to point using the control points controlPoint1 and con...
Definition: path_builder.cc:115
impeller::TessellatorLibtess
An extended tessellator that offers arbitrary/concave tessellation via the libtess2 library.
Definition: tessellator_libtess.h:29
impeller::TessellatorLibtess::Tessellate
TessellatorLibtess::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.
Definition: tessellator_libtess.cc:56
impeller::TessellatorLibtess::Result::kSuccess
@ kSuccess
x
int32_t x
Definition: dl_golden_unittests.cc:458
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Tessellate
struct Vertices * Tessellate(PathBuilder *builder, int fill_type, Scalar tolerance)
Definition: tessellator.cc:42
impeller::FillType
FillType
Definition: path.h:32
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::DestroyVertices
void DestroyVertices(Vertices *vertices)
Definition: tessellator.cc:76
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:22
impeller::Vertices::points
float * points
Definition: tessellator.h:23
tessellator.h
tessellator_libtess.h