| 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 namespace impeller {
10 PathBuilder* CreatePathBuilder() {
11 return new PathBuilder();
12 }
13
14 void DestroyPathBuilder(PathBuilder* builder) {
15 delete builder;
16 }
17
18 void MoveTo(PathBuilder* builder, Scalar x, Scalar y) {
19 builder->MoveTo(Point(x, y));
20 }
21
22 void LineTo(PathBuilder* builder, Scalar x, Scalar y) {
23 builder->LineTo(Point(x, y));
24 }
25
26 void CubicTo(PathBuilder* builder,
27 Scalar x1,
28 Scalar y1,
29 Scalar x2,
30 Scalar y2,
31 Scalar x3,
32 Scalar y3) {
33 builder->CubicCurveTo(Point(x1, y1), Point(x2, y2), Point(x3, y3));
34 }
35
36 void Close(PathBuilder* builder) {
37 builder->Close();
38 }
39
40 struct Vertices* Tessellate(PathBuilder* builder,
41 int fill_type,
42 Scalar tolerance) {
43 auto path = builder->CopyPath(static_cast<FillType>(fill_type));
44 auto polyline = path.CreatePolyline(tolerance);
45 std::vector<float> points;
46 if (Tessellator{}.Tessellate(
47 path.GetFillType(), polyline,
48 [&points](const float* vertices, size_t vertices_count,
49 const uint16_t* indices, size_t indices_count) {
50 // Results are expected to be re-duplicated.
51 std::vector<Point> raw_points;
52 for (auto i = 0u; i < vertices_count * 2; i += 2) {
53 raw_points.emplace_back(Point{vertices[i], vertices[i + 1]});
54 }
55 for (auto i = 0u; i < indices_count; i++) {
56 auto point = raw_points[indices[i]];
57 points.push_back(point.x);
58 points.push_back(point.y);
59 }
60 return true;
61 }) != Tessellator::Result::kSuccess) {
62 return nullptr;
63 }
64
65 Vertices* vertices = new Vertices();
66 vertices->points = new float[points.size()];
67 if (!vertices->points) {
68 return nullptr;
69 }
70 vertices->length = points.size();
71 std::copy(points.begin(), points.end(), vertices->points);
72 return vertices;
73 }
74
75 void DestroyVertices(Vertices* vertices) {
76 delete vertices->points;
77 delete vertices;
78 }
79
80 } // namespace impeller
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::DestroyVertices
void DestroyVertices(Vertices *vertices)
Definition: tessellator.cc:75
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::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:116
impeller::PathBuilder
Definition: path_builder.h:13
impeller::MoveTo
void MoveTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:18
impeller::Vertices
Definition: tessellator.h:20
impeller::Vertices::points
float * points
Definition: tessellator.h:21
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
impeller::CreatePathBuilder
PathBuilder * CreatePathBuilder()
Definition: tessellator.cc:10
impeller::CubicTo
void CubicTo(PathBuilder *builder, Scalar x1, Scalar y1, Scalar x2, Scalar y2, Scalar x3, Scalar y3)
Definition: tessellator.cc:26
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::FillType
FillType
Definition: path.h:29
impeller::Close
void Close(PathBuilder *builder)
Definition: tessellator.cc:36
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::Close
PathBuilder & Close()
Definition: path_builder.cc:39
impeller::PathBuilder::CopyPath
Path CopyPath(FillType fill=FillType::kNonZero) const
Definition: path_builder.cc:15
impeller::LineTo
void LineTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:22
impeller::PathBuilder::MoveTo
PathBuilder & MoveTo(Point point, bool relative=false)
Definition: path_builder.cc:32
impeller::DestroyPathBuilder
void DestroyPathBuilder(PathBuilder *builder)
Definition: tessellator.cc:14
tessellator.h
impeller::Tessellate
struct Vertices * Tessellate(PathBuilder *builder, int fill_type, Scalar tolerance)
Definition: tessellator.cc:40
impeller
Definition: aiks_context.cc:10
impeller::Path::CreatePolyline
Polyline CreatePolyline(Scalar scale) const
Definition: path.cc:272