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 
10 
11 namespace impeller {
13  return new PathBuilder();
14 }
15 
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;
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;
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
An extended tessellator that offers arbitrary/concave tessellation via the libtess2 library.
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.
int32_t x
float Scalar
Definition: scalar.h:19
struct Vertices * Tessellate(PathBuilder *builder, int fill_type, Scalar tolerance)
Definition: tessellator.cc:42
TPoint< Scalar > Point
Definition: point.h:327
PathBuilder * CreatePathBuilder()
Definition: tessellator.cc:12
void DestroyPathBuilder(PathBuilder *builder)
Definition: tessellator.cc:16
flutter::DlPathBuilder PathBuilder
Definition: tessellator.h:22
void DestroyVertices(Vertices *vertices)
Definition: tessellator.cc:77
void MoveTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:20
void LineTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:24
void CubicTo(PathBuilder *builder, Scalar x1, Scalar y1, Scalar x2, Scalar y2, Scalar x3, Scalar y3)
Definition: tessellator.cc:28
void Close(PathBuilder *builder)
Definition: tessellator.cc:38
std::vector< Point > points