blob: b2b2af31add48c45483256cb1e1781d2c9723306 [file] [log] [blame]
ethannicholase9709e82016-01-07 13:34:16 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Chris Dalton17dc4182020-03-25 16:18:16 -06008#include "src/gpu/GrTriangulator.h"
ethannicholase9709e82016-01-07 13:34:16 -08009
Chris Daltond081dce2020-01-23 12:09:04 -070010#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040012#include "src/gpu/geometry/GrPathUtils.h"
ethannicholase9709e82016-01-07 13:34:16 -080013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPath.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040015#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkGeometry.h"
17#include "src/core/SkPointPriv.h"
ethannicholase9709e82016-01-07 13:34:16 -080018
Stephen White94b7e542018-01-04 14:01:10 -050019#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040020#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040021#include <queue>
22#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040023#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080024
25/*
senorblancof57372d2016-08-31 10:36:19 -070026 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080027 *
28 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
29 * 2) Build a mesh of edges connecting the vertices (build_edges()).
30 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
31 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
32 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
33 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
34 *
senorblancof57372d2016-08-31 10:36:19 -070035 * For screenspace antialiasing, the algorithm is modified as follows:
36 *
37 * Run steps 1-5 above to produce polygons.
38 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040039 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070040 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
41 * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
Stephen Whitebda29c02017-03-13 15:10:13 -040042 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070043 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
44 *
ethannicholase9709e82016-01-07 13:34:16 -080045 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
46 * of vertices (and the necessity of inserting new vertices on intersection).
47 *
Stephen Whitebda29c02017-03-13 15:10:13 -040048 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080049 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
50 * left-to-right based on the point where both edges are active (when both top vertices
51 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
52 * (shared), it's sorted based on the last point where both edges are active, so the
53 * "upper" bottom vertex.
54 *
55 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
56 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
57 * not exact and may violate the mesh topology or active edge list ordering. We
58 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040059 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080060 *
61 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
62 * neighbouring edges at the top or bottom vertex. This is handled by merging the
63 * edges (merge_collinear_edges()).
64 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen Whitec03e6982020-02-06 16:32:14 -050065 * active edge list. This is handled by detecting potential violations and rewinding
66 * the active edge list to the vertex before they occur (rewind() during merging,
67 * rewind_if_necessary() during splitting).
ethannicholase9709e82016-01-07 13:34:16 -080068 *
69 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
70 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
71 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
72 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
73 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
74 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
75 * linked list implementation. With the latter, all removals are O(1), and most insertions
76 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
77 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
78 * frequent. There may be other data structures worth investigating, however.
79 *
80 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
81 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
82 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
83 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
84 * that the "left" and "right" orientation in the code remains correct (edges to the left are
85 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
86 * degrees counterclockwise, rather that transposing.
87 */
88
89#define LOGGING_ENABLED 0
90
91#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -040092#define TESS_LOG printf
ethannicholase9709e82016-01-07 13:34:16 -080093#else
Brian Salomon120e7d62019-09-11 10:29:22 -040094#define TESS_LOG(...)
ethannicholase9709e82016-01-07 13:34:16 -080095#endif
96
ethannicholase9709e82016-01-07 13:34:16 -080097namespace {
98
Chris Dalton17dc4182020-03-25 16:18:16 -060099using GrTriangulator::Mode;
Chris Daltondcc8c542020-01-28 17:55:56 -0700100
Stephen White11f65e02017-02-16 19:00:39 -0500101const int kArenaChunkSize = 16 * 1024;
Stephen Whitee260c462017-12-19 18:09:54 -0500102const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
Stephen White11f65e02017-02-16 19:00:39 -0500103
ethannicholase9709e82016-01-07 13:34:16 -0800104struct Vertex;
105struct Edge;
Stephen Whitee260c462017-12-19 18:09:54 -0500106struct Event;
ethannicholase9709e82016-01-07 13:34:16 -0800107struct Poly;
108
109template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800110void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800111 t->*Prev = prev;
112 t->*Next = next;
113 if (prev) {
114 prev->*Next = t;
115 } else if (head) {
116 *head = t;
117 }
118 if (next) {
119 next->*Prev = t;
120 } else if (tail) {
121 *tail = t;
122 }
123}
124
125template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800126void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800127 if (t->*Prev) {
128 t->*Prev->*Next = t->*Next;
129 } else if (head) {
130 *head = t->*Next;
131 }
132 if (t->*Next) {
133 t->*Next->*Prev = t->*Prev;
134 } else if (tail) {
135 *tail = t->*Prev;
136 }
137 t->*Prev = t->*Next = nullptr;
138}
139
140/**
141 * Vertices are used in three ways: first, the path contours are converted into a
142 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
143 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
144 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
145 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
146 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
147 * an individual Vertex from the path mesh may belong to multiple
148 * MonotonePolys, so the original Vertices cannot be re-used.
149 */
150
151struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700152 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800153 : fPoint(point), fPrev(nullptr), fNext(nullptr)
154 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
155 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -0400156 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -0400157 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -0700158 , fAlpha(alpha)
Stephen Whitec4dbc372019-05-22 10:50:14 -0400159 , fSynthetic(false)
ethannicholase9709e82016-01-07 13:34:16 -0800160#if LOGGING_ENABLED
161 , fID (-1.0f)
162#endif
163 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400164 SkPoint fPoint; // Vertex position
165 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
166 Vertex* fNext; // "
167 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
168 Edge* fLastEdgeAbove; // "
169 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
170 Edge* fLastEdgeBelow; // "
171 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
172 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
173 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700174 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400175 bool fSynthetic; // Is this a synthetic vertex?
ethannicholase9709e82016-01-07 13:34:16 -0800176#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400177 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800178#endif
179};
180
181/***************************************************************************************/
182
183typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
184
ethannicholase9709e82016-01-07 13:34:16 -0800185bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500186 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800187}
188
189bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500190 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800191}
192
Stephen White16a40cb2017-02-23 11:10:01 -0500193struct Comparator {
194 enum class Direction { kVertical, kHorizontal };
195 Comparator(Direction direction) : fDirection(direction) {}
196 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
197 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
198 }
Stephen White16a40cb2017-02-23 11:10:01 -0500199 Direction fDirection;
200};
201
Brian Osman0995fd52019-01-09 09:52:25 -0500202inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500203 GrVertexWriter verts{data};
204 verts.write(v->fPoint);
205
Brian Osman80879d42019-01-07 16:15:27 -0500206 if (emitCoverage) {
207 verts.write(GrNormalizeByteToFloat(v->fAlpha));
208 }
Brian Osman0995fd52019-01-09 09:52:25 -0500209
Brian Osmanf9aabff2018-11-13 16:11:38 -0500210 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800211}
212
Brian Osman0995fd52019-01-09 09:52:25 -0500213void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400214 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
215 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
216 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700217#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500218 data = emit_vertex(v0, emitCoverage, data);
219 data = emit_vertex(v1, emitCoverage, data);
220 data = emit_vertex(v1, emitCoverage, data);
221 data = emit_vertex(v2, emitCoverage, data);
222 data = emit_vertex(v2, emitCoverage, data);
223 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800224#else
Brian Osman0995fd52019-01-09 09:52:25 -0500225 data = emit_vertex(v0, emitCoverage, data);
226 data = emit_vertex(v1, emitCoverage, data);
227 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800228#endif
229 return data;
230}
231
senorblancoe6eaa322016-03-08 09:06:44 -0800232struct VertexList {
233 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500234 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800235 Vertex* fHead;
236 Vertex* fTail;
237 void insert(Vertex* v, Vertex* prev, Vertex* next) {
238 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
239 }
240 void append(Vertex* v) {
241 insert(v, fTail, nullptr);
242 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400243 void append(const VertexList& list) {
244 if (!list.fHead) {
245 return;
246 }
247 if (fTail) {
248 fTail->fNext = list.fHead;
249 list.fHead->fPrev = fTail;
250 } else {
251 fHead = list.fHead;
252 }
253 fTail = list.fTail;
254 }
senorblancoe6eaa322016-03-08 09:06:44 -0800255 void prepend(Vertex* v) {
256 insert(v, nullptr, fHead);
257 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500258 void remove(Vertex* v) {
259 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
260 }
senorblancof57372d2016-08-31 10:36:19 -0700261 void close() {
262 if (fHead && fTail) {
263 fTail->fNext = fHead;
264 fHead->fPrev = fTail;
265 }
266 }
senorblancoe6eaa322016-03-08 09:06:44 -0800267};
268
senorblancof57372d2016-08-31 10:36:19 -0700269// Round to nearest quarter-pixel. This is used for screenspace tessellation.
270
271inline void round(SkPoint* p) {
272 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
273 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
274}
275
Stephen White94b7e542018-01-04 14:01:10 -0500276inline SkScalar double_to_clamped_scalar(double d) {
277 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
278}
279
senorblanco49df8d12016-10-07 08:36:56 -0700280// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
281struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500282 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700283 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
284 Line(const SkPoint& p, const SkPoint& q)
285 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
286 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
287 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
288 static_cast<double>(p.fX) * q.fY) {}
289 double dist(const SkPoint& p) const {
290 return fA * p.fX + fB * p.fY + fC;
291 }
Stephen Whitee260c462017-12-19 18:09:54 -0500292 Line operator*(double v) const {
293 return Line(fA * v, fB * v, fC * v);
294 }
senorblanco49df8d12016-10-07 08:36:56 -0700295 double magSq() const {
296 return fA * fA + fB * fB;
297 }
Stephen Whitee260c462017-12-19 18:09:54 -0500298 void normalize() {
299 double len = sqrt(this->magSq());
300 if (len == 0.0) {
301 return;
302 }
303 double scale = 1.0f / len;
304 fA *= scale;
305 fB *= scale;
306 fC *= scale;
307 }
308 bool nearParallel(const Line& o) const {
309 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
310 }
senorblanco49df8d12016-10-07 08:36:56 -0700311
312 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500313 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700314 double denom = fA * other.fB - fB * other.fA;
315 if (denom == 0.0) {
316 return false;
317 }
Stephen White94b7e542018-01-04 14:01:10 -0500318 double scale = 1.0 / denom;
319 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
320 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500321 round(point);
Stephen White9b7f1432019-06-08 08:56:58 -0400322 return point->isFinite();
senorblanco49df8d12016-10-07 08:36:56 -0700323 }
324 double fA, fB, fC;
325};
326
ethannicholase9709e82016-01-07 13:34:16 -0800327/**
328 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
329 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
330 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whitec03e6982020-02-06 16:32:14 -0500331 * point). For speed, that case is only tested by the callers that require it (e.g.,
332 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
333 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
334 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
335 * a lot faster in the "not found" case.
ethannicholase9709e82016-01-07 13:34:16 -0800336 *
337 * The coefficients of the line equation stored in double precision to avoid catastrphic
338 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
339 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
340 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
341 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
342 * this file).
343 */
344
345struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500346 enum class Type { kInner, kOuter, kConnector };
347 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800348 : fWinding(winding)
349 , fTop(top)
350 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500351 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800352 , fLeft(nullptr)
353 , fRight(nullptr)
354 , fPrevEdgeAbove(nullptr)
355 , fNextEdgeAbove(nullptr)
356 , fPrevEdgeBelow(nullptr)
357 , fNextEdgeBelow(nullptr)
358 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700359 , fRightPoly(nullptr)
360 , fLeftPolyPrev(nullptr)
361 , fLeftPolyNext(nullptr)
362 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700363 , fRightPolyNext(nullptr)
364 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700365 , fUsedInRightPoly(false)
366 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800367 }
368 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
369 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
370 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500371 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800372 Edge* fLeft; // The linked list of edges in the active edge list.
373 Edge* fRight; // "
374 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
375 Edge* fNextEdgeAbove; // "
376 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
377 Edge* fNextEdgeBelow; // "
378 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
379 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700380 Edge* fLeftPolyPrev;
381 Edge* fLeftPolyNext;
382 Edge* fRightPolyPrev;
383 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700384 bool fUsedInLeftPoly;
385 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700386 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800387 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700388 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800389 }
390 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700391 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800392 }
393 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700394 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800395 }
396 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700397 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800398 }
Stephen White95152e12017-12-18 10:52:44 -0500399 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
Brian Salomon120e7d62019-09-11 10:29:22 -0400400 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
401 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800402 if (fTop == other.fTop || fBottom == other.fBottom) {
403 return false;
404 }
senorblanco49df8d12016-10-07 08:36:56 -0700405 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800406 if (denom == 0.0) {
407 return false;
408 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500409 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
410 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
411 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
412 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800413 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
414 // This saves us doing the divide below unless absolutely necessary.
415 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
416 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
417 return false;
418 }
419 double s = sNumer / denom;
420 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700421 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
422 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500423 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500424 if (fType == Type::kConnector) {
425 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
426 } else if (other.fType == Type::kConnector) {
427 double t = tNumer / denom;
428 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500429 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
430 *alpha = 0;
431 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500432 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500433 }
434 }
ethannicholase9709e82016-01-07 13:34:16 -0800435 return true;
436 }
senorblancof57372d2016-08-31 10:36:19 -0700437};
438
Stephen Whitec4dbc372019-05-22 10:50:14 -0400439struct SSEdge;
440
441struct SSVertex {
442 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
443 Vertex* fVertex;
444 SSEdge* fPrev;
445 SSEdge* fNext;
446};
447
448struct SSEdge {
449 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
450 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
451 }
452 Edge* fEdge;
453 Event* fEvent;
454 SSVertex* fPrev;
455 SSVertex* fNext;
456};
457
458typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
459typedef std::vector<SSEdge*> SSEdgeList;
460
senorblancof57372d2016-08-31 10:36:19 -0700461struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500462 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700463 Edge* fHead;
464 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700465 void insert(Edge* edge, Edge* prev, Edge* next) {
466 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700467 }
468 void append(Edge* e) {
469 insert(e, fTail, nullptr);
470 }
471 void remove(Edge* edge) {
472 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700473 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400474 void removeAll() {
475 while (fHead) {
476 this->remove(fHead);
477 }
478 }
senorblancof57372d2016-08-31 10:36:19 -0700479 void close() {
480 if (fHead && fTail) {
481 fTail->fRight = fHead;
482 fHead->fLeft = fTail;
483 }
484 }
485 bool contains(Edge* edge) const {
486 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800487 }
488};
489
Stephen Whitec4dbc372019-05-22 10:50:14 -0400490struct EventList;
491
Stephen Whitee260c462017-12-19 18:09:54 -0500492struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400493 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
494 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500495 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400496 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500497 SkPoint fPoint;
498 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400499 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500500};
501
Stephen Whitec4dbc372019-05-22 10:50:14 -0400502struct EventComparator {
503 enum class Op { kLessThan, kGreaterThan };
504 EventComparator(Op op) : fOp(op) {}
505 bool operator() (Event* const &e1, Event* const &e2) {
506 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
507 : e1->fAlpha > e2->fAlpha;
508 }
509 Op fOp;
510};
Stephen Whitee260c462017-12-19 18:09:54 -0500511
Stephen Whitec4dbc372019-05-22 10:50:14 -0400512typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500513
Stephen Whitec4dbc372019-05-22 10:50:14 -0400514struct EventList : EventPQ {
515 EventList(EventComparator comparison) : EventPQ(comparison) {
516 }
517};
518
519void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
520 Vertex* prev = e->fPrev->fVertex;
521 Vertex* next = e->fNext->fVertex;
522 if (prev == next || !prev->fPartner || !next->fPartner) {
523 return;
524 }
525 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
526 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500527 SkPoint p;
528 uint8_t alpha;
529 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400530 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
531 "will collapse to %g,%g alpha %d\n",
532 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
533 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400534 e->fEvent = alloc.make<Event>(e, p, alpha);
535 events->push(e->fEvent);
536 }
537}
538
539void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
540 Comparator& c, SkArenaAlloc& alloc) {
541 if (!v->fPartner) {
542 return;
543 }
Stephen White8a3c0592019-05-29 11:26:16 -0400544 Vertex* top = edge->fEdge->fTop;
545 Vertex* bottom = edge->fEdge->fBottom;
546 if (!top || !bottom ) {
547 return;
548 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400549 Line line = edge->fEdge->fLine;
550 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
551 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
552 SkPoint p;
553 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400554 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
555 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400556 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
557 "will collapse to %g,%g alpha %d\n",
558 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400559 edge->fEvent = alloc.make<Event>(edge, p, alpha);
560 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500561 }
562}
Stephen Whitee260c462017-12-19 18:09:54 -0500563
ethannicholase9709e82016-01-07 13:34:16 -0800564/***************************************************************************************/
565
566struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700567 Poly(Vertex* v, int winding)
568 : fFirstVertex(v)
569 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800570 , fHead(nullptr)
571 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800572 , fNext(nullptr)
573 , fPartner(nullptr)
574 , fCount(0)
575 {
576#if LOGGING_ENABLED
577 static int gID = 0;
578 fID = gID++;
Brian Salomon120e7d62019-09-11 10:29:22 -0400579 TESS_LOG("*** created Poly %d\n", fID);
ethannicholase9709e82016-01-07 13:34:16 -0800580#endif
581 }
senorblanco531237e2016-06-02 11:36:48 -0700582 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800583 struct MonotonePoly {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700584 MonotonePoly(Edge* edge, Side side, int winding)
senorblanco531237e2016-06-02 11:36:48 -0700585 : fSide(side)
586 , fFirstEdge(nullptr)
587 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800588 , fPrev(nullptr)
Chris Dalton022bd3b2020-01-24 13:48:53 -0700589 , fNext(nullptr)
590 , fWinding(winding) {
senorblanco531237e2016-06-02 11:36:48 -0700591 this->addEdge(edge);
592 }
ethannicholase9709e82016-01-07 13:34:16 -0800593 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700594 Edge* fFirstEdge;
595 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800596 MonotonePoly* fPrev;
597 MonotonePoly* fNext;
Chris Dalton022bd3b2020-01-24 13:48:53 -0700598 int fWinding;
senorblanco531237e2016-06-02 11:36:48 -0700599 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800600 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700601 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700602 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
603 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700604 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800605 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700606 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700607 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
608 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700609 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800610 }
ethannicholase9709e82016-01-07 13:34:16 -0800611 }
612
Brian Osman0995fd52019-01-09 09:52:25 -0500613 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700614 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700615 VertexList vertices;
616 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500617 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700618 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700619 if (kRight_Side == fSide) {
620 vertices.append(e->fBottom);
621 e = e->fRightPolyNext;
622 } else {
623 vertices.prepend(e->fBottom);
624 e = e->fLeftPolyNext;
625 }
Stephen White651cbe92017-03-03 12:24:16 -0500626 count++;
senorblanco531237e2016-06-02 11:36:48 -0700627 }
628 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800629 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700630 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800631 SkASSERT(v && v->fPrev && v->fNext);
632 Vertex* prev = v->fPrev;
633 Vertex* curr = v;
634 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500635 if (count == 3) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700636 return this->emitTriangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500637 }
ethannicholase9709e82016-01-07 13:34:16 -0800638 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
639 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
640 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
641 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
642 if (ax * by - ay * bx >= 0.0) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700643 data = this->emitTriangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800644 v->fPrev->fNext = v->fNext;
645 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500646 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800647 if (v->fPrev == first) {
648 v = v->fNext;
649 } else {
650 v = v->fPrev;
651 }
652 } else {
653 v = v->fNext;
654 }
655 }
656 return data;
657 }
Chris Dalton022bd3b2020-01-24 13:48:53 -0700658 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, bool emitCoverage,
659 void* data) const {
660 if (fWinding < 0) {
661 // Ensure our triangles always wind in the same direction as if the path had been
662 // triangulated as a simple fan (a la red book).
663 std::swap(prev, next);
664 }
665 return emit_triangle(next, curr, prev, emitCoverage, data);
666 }
ethannicholase9709e82016-01-07 13:34:16 -0800667 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500668 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400669 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
670 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800671 Poly* partner = fPartner;
672 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700673 if (side == kRight_Side) {
674 if (e->fUsedInRightPoly) {
675 return this;
676 }
677 } else {
678 if (e->fUsedInLeftPoly) {
679 return this;
680 }
681 }
ethannicholase9709e82016-01-07 13:34:16 -0800682 if (partner) {
683 fPartner = partner->fPartner = nullptr;
684 }
senorblanco531237e2016-06-02 11:36:48 -0700685 if (!fTail) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700686 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700687 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700688 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
689 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700690 } else if (side == fTail->fSide) {
691 fTail->addEdge(e);
692 fCount++;
693 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500694 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700695 fTail->addEdge(e);
696 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800697 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700698 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800699 poly = partner;
700 } else {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700701 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700702 m->fPrev = fTail;
703 fTail->fNext = m;
704 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800705 }
706 }
ethannicholase9709e82016-01-07 13:34:16 -0800707 return poly;
708 }
Brian Osman0995fd52019-01-09 09:52:25 -0500709 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800710 if (fCount < 3) {
711 return data;
712 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400713 TESS_LOG("emit() %d, size %d\n", fID, fCount);
ethannicholase9709e82016-01-07 13:34:16 -0800714 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500715 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800716 }
717 return data;
718 }
senorblanco531237e2016-06-02 11:36:48 -0700719 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
720 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800721 int fWinding;
722 MonotonePoly* fHead;
723 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800724 Poly* fNext;
725 Poly* fPartner;
726 int fCount;
727#if LOGGING_ENABLED
728 int fID;
729#endif
730};
731
732/***************************************************************************************/
733
734bool coincident(const SkPoint& a, const SkPoint& b) {
735 return a == b;
736}
737
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500738Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
739 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800740 poly->fNext = *head;
741 *head = poly;
742 return poly;
743}
744
Stephen White3a9aab92017-03-07 14:07:18 -0500745void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500746 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800747#if LOGGING_ENABLED
748 static float gID = 0.0f;
749 v->fID = gID++;
750#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500751 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800752}
753
Stephen White36e4f062017-03-27 16:11:31 -0400754SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
755 SkQuadCoeff quad(pts);
756 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
757 SkPoint mid = to_point(quad.eval(t));
758 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400759 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
760 return 0;
761 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500762 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400763}
764
765void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
766 SkArenaAlloc& alloc) {
767 SkQuadCoeff quad(pts);
768 Sk2s aa = quad.fA * quad.fA;
769 SkScalar denom = 2.0f * (aa[0] + aa[1]);
770 Sk2s ab = quad.fA * quad.fB;
771 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
772 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500773 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400774 // Test possible subdivision values only at the point of maximum curvature.
775 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500776 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400777 u = 1.0f / nPoints;
778 if (quad_error_at(pts, t, u) < toleranceSqd) {
779 break;
780 }
781 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800782 }
Stephen White36e4f062017-03-27 16:11:31 -0400783 for (int j = 1; j <= nPoints; j++) {
784 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
785 }
ethannicholase9709e82016-01-07 13:34:16 -0800786}
787
Stephen White3a9aab92017-03-07 14:07:18 -0500788void generate_cubic_points(const SkPoint& p0,
789 const SkPoint& p1,
790 const SkPoint& p2,
791 const SkPoint& p3,
792 SkScalar tolSqd,
793 VertexList* contour,
794 int pointsLeft,
795 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500796 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
797 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800798 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
799 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500800 append_point_to_contour(p3, contour, alloc);
801 return;
ethannicholase9709e82016-01-07 13:34:16 -0800802 }
803 const SkPoint q[] = {
804 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
805 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
806 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
807 };
808 const SkPoint r[] = {
809 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
810 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
811 };
812 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
813 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500814 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
815 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800816}
817
818// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
819
820void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton8e2b6942020-04-22 15:55:00 -0600821 VertexList* contours, SkArenaAlloc& alloc, Mode mode, int* numCountedCurves) {
ethannicholase9709e82016-01-07 13:34:16 -0800822 SkScalar toleranceSqd = tolerance * tolerance;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700823 bool innerPolygons = (Mode::kSimpleInnerPolygons == mode);
ethannicholase9709e82016-01-07 13:34:16 -0800824
825 SkPoint pts[4];
Chris Dalton8e2b6942020-04-22 15:55:00 -0600826 int localCurveCount = 0;
Stephen White3a9aab92017-03-07 14:07:18 -0500827 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800828 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800829 if (path.isInverseFillType()) {
830 SkPoint quad[4];
831 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700832 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500833 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800834 }
Stephen White3a9aab92017-03-07 14:07:18 -0500835 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800836 }
837 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500838 SkPath::Verb verb;
Mike Reedba7e9a62019-08-16 13:30:34 -0400839 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800840 switch (verb) {
841 case SkPath::kConic_Verb: {
Chris Dalton8e2b6942020-04-22 15:55:00 -0600842 ++localCurveCount;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700843 if (innerPolygons) {
844 append_point_to_contour(pts[2], contour, alloc);
845 break;
846 }
ethannicholase9709e82016-01-07 13:34:16 -0800847 SkScalar weight = iter.conicWeight();
848 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
849 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400850 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800851 quadPts += 2;
852 }
ethannicholase9709e82016-01-07 13:34:16 -0800853 break;
854 }
855 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500856 if (contour->fHead) {
857 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800858 }
Stephen White3a9aab92017-03-07 14:07:18 -0500859 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800860 break;
861 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500862 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800863 break;
864 }
865 case SkPath::kQuad_Verb: {
Chris Dalton8e2b6942020-04-22 15:55:00 -0600866 ++localCurveCount;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700867 if (innerPolygons) {
868 append_point_to_contour(pts[2], contour, alloc);
869 break;
870 }
871 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800872 break;
873 }
874 case SkPath::kCubic_Verb: {
Chris Dalton8e2b6942020-04-22 15:55:00 -0600875 ++localCurveCount;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700876 if (innerPolygons) {
877 append_point_to_contour(pts[3], contour, alloc);
878 break;
879 }
ethannicholase9709e82016-01-07 13:34:16 -0800880 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500881 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
882 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800883 break;
884 }
885 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800886 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800887 break;
888 }
889 }
Chris Dalton8e2b6942020-04-22 15:55:00 -0600890 *numCountedCurves = localCurveCount;
ethannicholase9709e82016-01-07 13:34:16 -0800891}
892
Mike Reed7d34dc72019-11-26 12:17:17 -0500893inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800894 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500895 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800896 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500897 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800898 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500899 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700900 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500901 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800902 return (winding & 1) == 1;
903 default:
904 SkASSERT(false);
905 return false;
906 }
907}
908
Mike Reed7d34dc72019-11-26 12:17:17 -0500909inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500910 return poly && apply_fill_type(fillType, poly->fWinding);
911}
912
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500913Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500914 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800915 Vertex* top = winding < 0 ? next : prev;
916 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500917 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800918}
919
920void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400921 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700922 SkASSERT(edges->contains(edge));
923 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800924}
925
926void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400927 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700928 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800929 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700930 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800931}
932
933void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500934 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800935 *left = v->fFirstEdgeAbove->fLeft;
936 *right = v->fLastEdgeAbove->fRight;
937 return;
938 }
939 Edge* next = nullptr;
940 Edge* prev;
941 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
942 if (prev->isLeftOf(v)) {
943 break;
944 }
945 next = prev;
946 }
947 *left = prev;
948 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800949}
950
ethannicholase9709e82016-01-07 13:34:16 -0800951void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
952 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500953 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800954 return;
955 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400956 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
957 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800958 Edge* prev = nullptr;
959 Edge* next;
960 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
961 if (next->isRightOf(edge->fTop)) {
962 break;
963 }
964 prev = next;
965 }
senorblancoe6eaa322016-03-08 09:06:44 -0800966 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800967 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
968}
969
970void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
971 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500972 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800973 return;
974 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400975 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
976 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800977 Edge* prev = nullptr;
978 Edge* next;
979 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
980 if (next->isRightOf(edge->fBottom)) {
981 break;
982 }
983 prev = next;
984 }
senorblancoe6eaa322016-03-08 09:06:44 -0800985 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800986 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
987}
988
989void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400990 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400991 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
992 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800993 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800994 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
995}
996
997void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400998 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400999 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
1000 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -08001001 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -08001002 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
1003}
1004
Stephen Whitee7a364d2017-01-11 16:19:26 -05001005void disconnect(Edge* edge)
1006{
ethannicholase9709e82016-01-07 13:34:16 -08001007 remove_edge_above(edge);
1008 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -05001009}
1010
Stephen White3b5a3fa2017-06-06 14:51:19 -04001011void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
1012
1013void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
1014 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
1015 return;
1016 }
1017 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -04001018 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001019 while (v != dst) {
1020 v = v->fPrev;
1021 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1022 remove_edge(e, activeEdges);
1023 }
1024 Edge* leftEdge = v->fLeftEnclosingEdge;
1025 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1026 insert_edge(e, leftEdge, activeEdges);
1027 leftEdge = e;
Stephen Whitec03e6982020-02-06 16:32:14 -05001028 Vertex* top = e->fTop;
1029 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
1030 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
1031 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
1032 dst = top;
1033 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001034 }
1035 }
1036 *current = v;
1037}
1038
Stephen Whitec03e6982020-02-06 16:32:14 -05001039void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
1040 if (!activeEdges || !current) {
1041 return;
1042 }
1043 Vertex* top = edge->fTop;
1044 Vertex* bottom = edge->fBottom;
1045 if (edge->fLeft) {
1046 Vertex* leftTop = edge->fLeft->fTop;
1047 Vertex* leftBottom = edge->fLeft->fBottom;
1048 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
1049 rewind(activeEdges, current, leftTop, c);
1050 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
1051 rewind(activeEdges, current, top, c);
1052 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
1053 !edge->fLeft->isLeftOf(bottom)) {
1054 rewind(activeEdges, current, leftTop, c);
1055 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
1056 rewind(activeEdges, current, top, c);
1057 }
1058 }
1059 if (edge->fRight) {
1060 Vertex* rightTop = edge->fRight->fTop;
1061 Vertex* rightBottom = edge->fRight->fBottom;
1062 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
1063 rewind(activeEdges, current, rightTop, c);
1064 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
1065 rewind(activeEdges, current, top, c);
1066 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1067 !edge->fRight->isRightOf(bottom)) {
1068 rewind(activeEdges, current, rightTop, c);
1069 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1070 !edge->isLeftOf(rightBottom)) {
1071 rewind(activeEdges, current, top, c);
1072 }
1073 }
1074}
1075
Stephen White3b5a3fa2017-06-06 14:51:19 -04001076void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001077 remove_edge_below(edge);
1078 edge->fTop = v;
1079 edge->recompute();
1080 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001081 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001082 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001083}
1084
Stephen White3b5a3fa2017-06-06 14:51:19 -04001085void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001086 remove_edge_above(edge);
1087 edge->fBottom = v;
1088 edge->recompute();
1089 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001090 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001091 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001092}
1093
Stephen White3b5a3fa2017-06-06 14:51:19 -04001094void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1095 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001096 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001097 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1098 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1099 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001100 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001101 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001102 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001103 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001104 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001105 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001106 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001107 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001108 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001109 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001110 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001111 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001112 }
1113}
1114
Stephen White3b5a3fa2017-06-06 14:51:19 -04001115void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1116 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001117 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001118 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1119 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1120 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001121 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001122 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001123 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001124 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001125 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001126 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001127 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001128 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001129 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001130 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001131 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001132 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001133 }
1134}
1135
Stephen Whited26b4d82018-07-26 10:02:27 -04001136bool top_collinear(Edge* left, Edge* right) {
1137 if (!left || !right) {
1138 return false;
1139 }
1140 return left->fTop->fPoint == right->fTop->fPoint ||
1141 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1142}
1143
1144bool bottom_collinear(Edge* left, Edge* right) {
1145 if (!left || !right) {
1146 return false;
1147 }
1148 return left->fBottom->fPoint == right->fBottom->fPoint ||
1149 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1150}
1151
Stephen White3b5a3fa2017-06-06 14:51:19 -04001152void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001153 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001154 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001155 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001156 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001157 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001158 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001159 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001160 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001161 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001162 } else {
1163 break;
1164 }
ethannicholase9709e82016-01-07 13:34:16 -08001165 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001166 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1167 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1168 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1169 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001170}
1171
Stephen White89042d52018-06-08 12:18:22 -04001172bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001173 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001174 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001175 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001176 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001177 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1178 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001179 Vertex* top;
1180 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001181 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001182 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001183 top = v;
1184 bottom = edge->fTop;
1185 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001186 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001187 top = edge->fBottom;
1188 bottom = v;
1189 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001190 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001191 top = v;
1192 bottom = edge->fBottom;
1193 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001194 }
Stephen White531a48e2018-06-01 09:49:39 -04001195 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001196 insert_edge_below(newEdge, top, c);
1197 insert_edge_above(newEdge, bottom, c);
1198 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001199 return true;
1200}
1201
1202bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1203 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1204 return false;
1205 }
Stephen White1c5fd182018-07-12 15:54:05 -04001206 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1207 return false;
1208 }
Stephen White89042d52018-06-08 12:18:22 -04001209 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1210 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001211 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001212 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1213 }
1214 } else {
1215 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001216 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001217 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1218 }
1219 }
1220 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1221 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001222 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001223 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1224 }
1225 } else {
1226 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001227 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001228 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1229 }
1230 }
1231 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001232}
1233
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001234Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001235 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001236 if (!prev || !next || prev->fPoint == next->fPoint) {
1237 return nullptr;
1238 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001239 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001240 insert_edge_below(edge, edge->fTop, c);
1241 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001242 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001243 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001244 return edge;
1245}
1246
Stephen Whitebf6137e2017-01-04 15:43:26 -05001247void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001248 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001249 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1250 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -05001251 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001252 if (src->fPartner) {
1253 src->fPartner->fPartner = dst;
1254 }
Stephen White7b376942018-05-22 11:51:32 -04001255 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001256 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001257 }
Stephen White7b376942018-05-22 11:51:32 -04001258 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001259 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001260 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001261 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001262 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001263}
1264
Stephen White95152e12017-12-18 10:52:44 -05001265Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1266 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1267 Vertex* prevV = reference;
1268 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1269 prevV = prevV->fPrev;
1270 }
1271 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1272 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1273 prevV = nextV;
1274 nextV = nextV->fNext;
1275 }
1276 Vertex* v;
1277 if (prevV && coincident(prevV->fPoint, p)) {
1278 v = prevV;
1279 } else if (nextV && coincident(nextV->fPoint, p)) {
1280 v = nextV;
1281 } else {
1282 v = alloc.make<Vertex>(p, alpha);
1283#if LOGGING_ENABLED
1284 if (!prevV) {
1285 v->fID = mesh->fHead->fID - 1.0f;
1286 } else if (!nextV) {
1287 v->fID = mesh->fTail->fID + 1.0f;
1288 } else {
1289 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1290 }
1291#endif
1292 mesh->insert(v, prevV, nextV);
1293 }
1294 return v;
1295}
1296
Stephen White53a02982018-05-30 22:47:46 -04001297// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1298// sort criterion, it may not be possible to split correctly, since there is no point which is
1299// below the top and above the bottom. This function detects that case.
1300bool nearly_flat(Comparator& c, Edge* edge) {
1301 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1302 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001303 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001304}
1305
Stephen Whitee62999f2018-06-05 18:45:07 -04001306SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1307 if (c.sweep_lt(p, min)) {
1308 return min;
1309 } else if (c.sweep_lt(max, p)) {
1310 return max;
1311 } else {
1312 return p;
1313 }
1314}
1315
Stephen Whitec4dbc372019-05-22 10:50:14 -04001316void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1317 Line line1 = edge1->fLine;
1318 Line line2 = edge2->fLine;
1319 line1.normalize();
1320 line2.normalize();
1321 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1322 if (cosAngle > 0.999) {
1323 return;
1324 }
1325 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1326 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1327 SkPoint p;
1328 if (line1.intersect(line2, &p)) {
1329 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1330 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001331 TESS_LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001332 }
1333}
1334
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001335bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001336 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001337 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001338 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001339 }
Stephen White56158ae2017-01-30 14:31:31 -05001340 SkPoint p;
1341 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001342 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001343 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001344 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001345 Vertex* top = *current;
1346 // If the intersection point is above the current vertex, rewind to the vertex above the
1347 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001348 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001349 top = top->fPrev;
1350 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001351 if (!nearly_flat(c, left)) {
1352 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001353 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001354 if (!nearly_flat(c, right)) {
1355 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001356 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001357 if (p == left->fTop->fPoint) {
1358 v = left->fTop;
1359 } else if (p == left->fBottom->fPoint) {
1360 v = left->fBottom;
1361 } else if (p == right->fTop->fPoint) {
1362 v = right->fTop;
1363 } else if (p == right->fBottom->fPoint) {
1364 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001365 } else {
Stephen White95152e12017-12-18 10:52:44 -05001366 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001367 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001368 v->fSynthetic = true;
1369 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001370 }
ethannicholase9709e82016-01-07 13:34:16 -08001371 }
Stephen White0cb31672017-06-08 14:41:01 -04001372 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001373 split_edge(left, v, activeEdges, current, c, alloc);
1374 split_edge(right, v, activeEdges, current, c, alloc);
Brian Osman788b9162020-02-07 10:36:46 -05001375 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001376 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001377 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001378 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001379}
1380
Chris Daltondcc8c542020-01-28 17:55:56 -07001381void sanitize_contours(VertexList* contours, int contourCnt, Mode mode) {
1382 bool approximate = (Mode::kEdgeAntialias == mode);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001383 bool removeCollinearVertices = (Mode::kSimpleInnerPolygons != mode);
Stephen White3a9aab92017-03-07 14:07:18 -05001384 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1385 SkASSERT(contour->fHead);
1386 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001387 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001388 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001389 }
Stephen White3a9aab92017-03-07 14:07:18 -05001390 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001391 if (approximate) {
1392 round(&v->fPoint);
1393 }
Stephen White3a9aab92017-03-07 14:07:18 -05001394 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001395 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001396 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001397 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001398 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001399 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001400 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001401 contour->remove(v);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001402 } else if (removeCollinearVertices &&
1403 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001404 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001405 contour->remove(v);
1406 } else {
1407 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001408 }
Stephen White3a9aab92017-03-07 14:07:18 -05001409 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001410 }
1411 }
1412}
1413
Stephen Whitee260c462017-12-19 18:09:54 -05001414bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001415 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001416 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001417 }
Stephen Whitee260c462017-12-19 18:09:54 -05001418 bool merged = false;
1419 for (Vertex* v = mesh->fHead->fNext; v;) {
1420 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001421 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1422 v->fPoint = v->fPrev->fPoint;
1423 }
1424 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001425 merge_vertices(v, v->fPrev, mesh, c, alloc);
1426 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001427 }
Stephen Whitee260c462017-12-19 18:09:54 -05001428 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001429 }
Stephen Whitee260c462017-12-19 18:09:54 -05001430 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001431}
1432
1433// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1434
Stephen White3a9aab92017-03-07 14:07:18 -05001435void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001436 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001437 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1438 Vertex* prev = contour->fTail;
1439 for (Vertex* v = contour->fHead; v;) {
1440 Vertex* next = v->fNext;
1441 connect(prev, v, Edge::Type::kInner, c, alloc);
1442 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001443 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001444 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001445 }
1446 }
ethannicholase9709e82016-01-07 13:34:16 -08001447}
1448
Stephen Whitee260c462017-12-19 18:09:54 -05001449void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1450 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001451 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001452 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1453 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1454 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1455 // number.
1456 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1457 inner->fPartner = outer->fPartner = nullptr;
1458 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001459 }
1460 }
1461}
1462
1463template <CompareFunc sweep_lt>
1464void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1465 Vertex* a = front->fHead;
1466 Vertex* b = back->fHead;
1467 while (a && b) {
1468 if (sweep_lt(a->fPoint, b->fPoint)) {
1469 front->remove(a);
1470 result->append(a);
1471 a = front->fHead;
1472 } else {
1473 back->remove(b);
1474 result->append(b);
1475 b = back->fHead;
1476 }
1477 }
1478 result->append(*front);
1479 result->append(*back);
1480}
1481
1482void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1483 if (c.fDirection == Comparator::Direction::kHorizontal) {
1484 sorted_merge<sweep_lt_horiz>(front, back, result);
1485 } else {
1486 sorted_merge<sweep_lt_vert>(front, back, result);
1487 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001488#if LOGGING_ENABLED
1489 float id = 0.0f;
1490 for (Vertex* v = result->fHead; v; v = v->fNext) {
1491 v->fID = id++;
1492 }
1493#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001494}
1495
ethannicholase9709e82016-01-07 13:34:16 -08001496// Stage 3: sort the vertices by increasing sweep direction.
1497
Stephen White16a40cb2017-02-23 11:10:01 -05001498template <CompareFunc sweep_lt>
1499void merge_sort(VertexList* vertices) {
1500 Vertex* slow = vertices->fHead;
1501 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001502 return;
1503 }
Stephen White16a40cb2017-02-23 11:10:01 -05001504 Vertex* fast = slow->fNext;
1505 if (!fast) {
1506 return;
1507 }
1508 do {
1509 fast = fast->fNext;
1510 if (fast) {
1511 fast = fast->fNext;
1512 slow = slow->fNext;
1513 }
1514 } while (fast);
1515 VertexList front(vertices->fHead, slow);
1516 VertexList back(slow->fNext, vertices->fTail);
1517 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001518
Stephen White16a40cb2017-02-23 11:10:01 -05001519 merge_sort<sweep_lt>(&front);
1520 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001521
Stephen White16a40cb2017-02-23 11:10:01 -05001522 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001523 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001524}
1525
Stephen White95152e12017-12-18 10:52:44 -05001526void dump_mesh(const VertexList& mesh) {
1527#if LOGGING_ENABLED
1528 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001529 TESS_LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001530 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001531 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1532 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001533 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001534 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001535 }
1536 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001537 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001538 }
1539 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001540 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001541 }
1542 }
1543#endif
1544}
1545
Stephen Whitec4dbc372019-05-22 10:50:14 -04001546void dump_skel(const SSEdgeList& ssEdges) {
1547#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001548 for (SSEdge* edge : ssEdges) {
1549 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001550 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001551 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001552 edge->fNext->fVertex->fID);
1553 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001554 TESS_LOG(" (original %g -> %g)\n",
1555 edge->fEdge->fTop->fID,
1556 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001557 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001558 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001559 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001560 }
1561 }
1562#endif
1563}
1564
Stephen White89042d52018-06-08 12:18:22 -04001565#ifdef SK_DEBUG
1566void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1567 if (!left || !right) {
1568 return;
1569 }
1570 if (left->fTop == right->fTop) {
1571 SkASSERT(left->isLeftOf(right->fBottom));
1572 SkASSERT(right->isRightOf(left->fBottom));
1573 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1574 SkASSERT(left->isLeftOf(right->fTop));
1575 } else {
1576 SkASSERT(right->isRightOf(left->fTop));
1577 }
1578 if (left->fBottom == right->fBottom) {
1579 SkASSERT(left->isLeftOf(right->fTop));
1580 SkASSERT(right->isRightOf(left->fTop));
1581 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1582 SkASSERT(left->isLeftOf(right->fBottom));
1583 } else {
1584 SkASSERT(right->isRightOf(left->fBottom));
1585 }
1586}
1587
1588void validate_edge_list(EdgeList* edges, Comparator& c) {
1589 Edge* left = edges->fHead;
1590 if (!left) {
1591 return;
1592 }
1593 for (Edge* right = left->fRight; right; right = right->fRight) {
1594 validate_edge_pair(left, right, c);
1595 left = right;
1596 }
1597}
1598#endif
1599
ethannicholase9709e82016-01-07 13:34:16 -08001600// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1601
Stephen Whitec4dbc372019-05-22 10:50:14 -04001602bool connected(Vertex* v) {
1603 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1604}
1605
Chris Dalton6ccc0322020-01-29 11:38:16 -07001606enum class SimplifyResult {
1607 kAlreadySimple,
1608 kFoundSelfIntersection,
1609 kAbort
1610};
1611
1612SimplifyResult simplify(Mode mode, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001613 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001614 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001615 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001616 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001617 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001618 continue;
1619 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001620 Edge* leftEnclosingEdge;
1621 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001622 bool restartChecks;
1623 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001624 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1625 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001626 restartChecks = false;
1627 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001628 v->fLeftEnclosingEdge = leftEnclosingEdge;
1629 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001630 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001631 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001632 if (check_for_intersection(
1633 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c, alloc) ||
1634 check_for_intersection(
1635 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c, alloc)) {
1636 if (Mode::kSimpleInnerPolygons == mode) {
1637 return SimplifyResult::kAbort;
1638 }
1639 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001640 restartChecks = true;
1641 break;
1642 }
1643 }
1644 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001645 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001646 &activeEdges, &v, mesh, c, alloc)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001647 if (Mode::kSimpleInnerPolygons == mode) {
1648 return SimplifyResult::kAbort;
1649 }
1650 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001651 restartChecks = true;
1652 }
1653
1654 }
1655 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001656#ifdef SK_DEBUG
1657 validate_edge_list(&activeEdges, c);
1658#endif
ethannicholase9709e82016-01-07 13:34:16 -08001659 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1660 remove_edge(e, &activeEdges);
1661 }
1662 Edge* leftEdge = leftEnclosingEdge;
1663 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1664 insert_edge(e, leftEdge, &activeEdges);
1665 leftEdge = e;
1666 }
ethannicholase9709e82016-01-07 13:34:16 -08001667 }
Stephen Whitee260c462017-12-19 18:09:54 -05001668 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001669 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001670}
1671
1672// Stage 5: Tessellate the simplified mesh into monotone polygons.
1673
Chris Dalton6ccc0322020-01-29 11:38:16 -07001674Poly* tessellate(SkPathFillType fillType, Mode mode, const VertexList& vertices,
1675 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001676 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001677 int maxWindMagnitude = std::numeric_limits<int>::max();
1678 if (Mode::kSimpleInnerPolygons == mode && !SkPathFillType_IsEvenOdd(fillType)) {
1679 maxWindMagnitude = 1;
1680 }
ethannicholase9709e82016-01-07 13:34:16 -08001681 EdgeList activeEdges;
1682 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001683 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001684 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001685 continue;
1686 }
1687#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001688 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001689#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001690 Edge* leftEnclosingEdge;
1691 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001692 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001693 Poly* leftPoly;
1694 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001695 if (v->fFirstEdgeAbove) {
1696 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1697 rightPoly = v->fLastEdgeAbove->fRightPoly;
1698 } else {
1699 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1700 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1701 }
1702#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001703 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001704 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001705 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1706 e->fTop->fID, e->fBottom->fID,
1707 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1708 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001709 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001710 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001711 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001712 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1713 e->fTop->fID, e->fBottom->fID,
1714 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1715 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001716 }
1717#endif
1718 if (v->fFirstEdgeAbove) {
1719 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001720 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001721 }
1722 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001723 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001724 }
1725 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001726 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001727 remove_edge(e, &activeEdges);
1728 if (e->fRightPoly) {
1729 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001730 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001731 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001732 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001733 }
1734 }
1735 remove_edge(v->fLastEdgeAbove, &activeEdges);
1736 if (!v->fFirstEdgeBelow) {
1737 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1738 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1739 rightPoly->fPartner = leftPoly;
1740 leftPoly->fPartner = rightPoly;
1741 }
1742 }
1743 }
1744 if (v->fFirstEdgeBelow) {
1745 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001746 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001747 if (leftPoly == rightPoly) {
1748 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1749 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1750 leftPoly->fWinding, alloc);
1751 leftEnclosingEdge->fRightPoly = leftPoly;
1752 } else {
1753 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1754 rightPoly->fWinding, alloc);
1755 rightEnclosingEdge->fLeftPoly = rightPoly;
1756 }
ethannicholase9709e82016-01-07 13:34:16 -08001757 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001758 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001759 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1760 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001761 }
1762 }
1763 Edge* leftEdge = v->fFirstEdgeBelow;
1764 leftEdge->fLeftPoly = leftPoly;
1765 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1766 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1767 rightEdge = rightEdge->fNextEdgeBelow) {
1768 insert_edge(rightEdge, leftEdge, &activeEdges);
1769 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1770 winding += leftEdge->fWinding;
1771 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001772 if (abs(winding) > maxWindMagnitude) {
1773 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1774 }
ethannicholase9709e82016-01-07 13:34:16 -08001775 Poly* poly = new_poly(&polys, v, winding, alloc);
1776 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1777 }
1778 leftEdge = rightEdge;
1779 }
1780 v->fLastEdgeBelow->fRightPoly = rightPoly;
1781 }
1782#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001783 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001784 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001785 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1786 e->fTop->fID, e->fBottom->fID,
1787 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1788 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001789 }
1790#endif
1791 }
1792 return polys;
1793}
1794
Mike Reed7d34dc72019-11-26 12:17:17 -05001795void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001796 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001797 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001798 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001799 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001800 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001801 continue;
1802 }
1803 Edge* leftEnclosingEdge;
1804 Edge* rightEnclosingEdge;
1805 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1806 bool prevFilled = leftEnclosingEdge &&
1807 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1808 for (Edge* e = v->fFirstEdgeAbove; e;) {
1809 Edge* next = e->fNextEdgeAbove;
1810 remove_edge(e, &activeEdges);
1811 bool filled = apply_fill_type(fillType, e->fWinding);
1812 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001813 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001814 }
Stephen White49789062017-02-21 10:35:49 -05001815 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001816 e = next;
1817 }
Stephen White49789062017-02-21 10:35:49 -05001818 Edge* prev = leftEnclosingEdge;
1819 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1820 if (prev) {
1821 e->fWinding += prev->fWinding;
1822 }
1823 insert_edge(e, prev, &activeEdges);
1824 prev = e;
1825 }
senorblancof57372d2016-08-31 10:36:19 -07001826 }
senorblancof57372d2016-08-31 10:36:19 -07001827}
1828
Stephen White66412122017-03-01 11:48:27 -05001829// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001830void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001831 normal->set(SkDoubleToScalar(e->fLine.fA),
1832 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001833}
1834
1835// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1836// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1837// invert on stroking.
1838
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001839void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001840 Edge* prevEdge = boundary->fTail;
1841 SkVector prevNormal;
1842 get_edge_normal(prevEdge, &prevNormal);
1843 for (Edge* e = boundary->fHead; e != nullptr;) {
1844 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1845 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001846 double distPrev = e->dist(prev->fPoint);
1847 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001848 SkVector normal;
1849 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001850 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001851 if (prev == next) {
1852 remove_edge(prevEdge, boundary);
1853 remove_edge(e, boundary);
1854 prevEdge = boundary->fTail;
1855 e = boundary->fHead;
1856 if (prevEdge) {
1857 get_edge_normal(prevEdge, &prevNormal);
1858 }
1859 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001860 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001861 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001862 if (prev->fPoint != next->fPoint) {
1863 join->fLine.normalize();
1864 join->fLine = join->fLine * join->fWinding;
1865 }
senorblancof57372d2016-08-31 10:36:19 -07001866 insert_edge(join, e, boundary);
1867 remove_edge(prevEdge, boundary);
1868 remove_edge(e, boundary);
1869 if (join->fLeft && join->fRight) {
1870 prevEdge = join->fLeft;
1871 e = join;
1872 } else {
1873 prevEdge = boundary->fTail;
1874 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1875 }
1876 get_edge_normal(prevEdge, &prevNormal);
1877 } else {
1878 prevEdge = e;
1879 prevNormal = normal;
1880 e = e->fRight;
1881 }
1882 }
1883}
1884
Stephen Whitec4dbc372019-05-22 10:50:14 -04001885void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1886 if (v == dest) {
1887 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001888 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001889 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001890 if (v->fSynthetic) {
1891 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1892 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001893 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1894 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001895 v->fPartner->fPartner = dest;
1896 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001897 }
1898}
1899
Stephen Whitec4dbc372019-05-22 10:50:14 -04001900void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1901 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001902 return;
1903 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001904 Vertex* prev = fEdge->fPrev->fVertex;
1905 Vertex* next = fEdge->fNext->fVertex;
1906 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1907 SSEdge* nextEdge = fEdge->fNext->fNext;
1908 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1909 return;
Stephen White77169c82018-06-05 09:15:59 -04001910 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001911 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1912 dest->fSynthetic = true;
1913 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001914 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1915 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1916 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001917 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001918
Stephen Whitec4dbc372019-05-22 10:50:14 -04001919 ss_connect(prev, dest, c, alloc);
1920 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001921
Stephen Whitec4dbc372019-05-22 10:50:14 -04001922 prevEdge->fNext = nextEdge->fPrev = ssv;
1923 ssv->fPrev = prevEdge;
1924 ssv->fNext = nextEdge;
1925 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1926 return;
1927 }
1928 if (prevEdge->fEvent) {
1929 prevEdge->fEvent->fEdge = nullptr;
1930 }
1931 if (nextEdge->fEvent) {
1932 nextEdge->fEvent->fEdge = nullptr;
1933 }
1934 if (prevEdge->fPrev == nextEdge->fNext) {
1935 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1936 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1937 } else {
1938 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1939 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1940 if (dest->fPartner) {
1941 create_event(prevEdge, events, alloc);
1942 create_event(nextEdge, events, alloc);
1943 } else {
1944 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1945 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1946 }
1947 }
Stephen Whitee260c462017-12-19 18:09:54 -05001948}
1949
1950bool is_overlap_edge(Edge* e) {
1951 if (e->fType == Edge::Type::kOuter) {
1952 return e->fWinding != 0 && e->fWinding != 1;
1953 } else if (e->fType == Edge::Type::kInner) {
1954 return e->fWinding != 0 && e->fWinding != -2;
1955 } else {
1956 return false;
1957 }
1958}
1959
1960// This is a stripped-down version of tessellate() which computes edges which
1961// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001962bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1963 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001964 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001965 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001966 EventList events(comp);
1967 SSVertexMap ssVertices;
1968 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001969 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001970 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001971 continue;
1972 }
1973 Edge* leftEnclosingEdge;
1974 Edge* rightEnclosingEdge;
1975 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001976 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001977 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1978 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001979 bool leftOverlap = prev && is_overlap_edge(prev);
1980 bool rightOverlap = is_overlap_edge(e);
1981 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1982 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001983 if (prev) {
1984 e->fWinding -= prev->fWinding;
1985 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001986 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001987 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1988 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001989 disconnect(e);
1990 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001991 TESS_LOG("found overlap edge %g -> %g%s\n",
1992 e->fTop->fID, e->fBottom->fID,
1993 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001994 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1995 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1996 SSVertex* ssPrev = ssVertices[prevVertex];
1997 if (!ssPrev) {
1998 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1999 }
2000 SSVertex* ssNext = ssVertices[nextVertex];
2001 if (!ssNext) {
2002 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
2003 }
2004 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
2005 ssEdges.push_back(ssEdge);
2006// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
2007 ssPrev->fNext = ssNext->fPrev = ssEdge;
2008 create_event(ssEdge, &events, alloc);
2009 if (!isOuterBoundary) {
2010 disconnect(e);
2011 }
2012 }
2013 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05002014 }
2015 Edge* prev = leftEnclosingEdge;
2016 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2017 if (prev) {
2018 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05002019 }
2020 insert_edge(e, prev, &activeEdges);
2021 prev = e;
2022 }
2023 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04002024 bool complex = events.size() > 0;
2025
Brian Salomon120e7d62019-09-11 10:29:22 -04002026 TESS_LOG("\ncollapsing overlap regions\n");
2027 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04002028 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002029 while (events.size() > 0) {
2030 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05002031 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04002032 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002033 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002034 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002035 dump_skel(ssEdges);
2036 for (SSEdge* edge : ssEdges) {
2037 if (Edge* e = edge->fEdge) {
2038 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
2039 }
2040 }
2041 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002042}
2043
2044bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
2045 if (!prev || !next) {
2046 return true;
2047 }
2048 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
2049 return winding != origEdge->fWinding;
2050}
Stephen White92eba8a2017-02-06 09:50:27 -05002051
senorblancof57372d2016-08-31 10:36:19 -07002052// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
2053// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
2054// new antialiased mesh from those vertices.
2055
Stephen Whitee260c462017-12-19 18:09:54 -05002056void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
2057 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002058 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002059 // A boundary with fewer than 3 edges is degenerate.
2060 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
2061 return;
2062 }
2063 Edge* prevEdge = boundary->fTail;
2064 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
2065 SkVector prevNormal;
2066 get_edge_normal(prevEdge, &prevNormal);
2067 double radius = 0.5;
2068 Line prevInner(prevEdge->fLine);
2069 prevInner.fC -= radius;
2070 Line prevOuter(prevEdge->fLine);
2071 prevOuter.fC += radius;
2072 VertexList innerVertices;
2073 VertexList outerVertices;
2074 bool innerInversion = true;
2075 bool outerInversion = true;
2076 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
2077 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2078 SkVector normal;
2079 get_edge_normal(e, &normal);
2080 Line inner(e->fLine);
2081 inner.fC -= radius;
2082 Line outer(e->fLine);
2083 outer.fC += radius;
2084 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04002085 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002086 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2087 prevOuter.intersect(outer, &outerPoint)) {
2088 float cosAngle = normal.dot(prevNormal);
2089 if (cosAngle < -kCosMiterAngle) {
2090 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2091
2092 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2093 Line bisector(innerPoint, outerPoint);
2094 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2095 if (tangent.fA == 0 && tangent.fB == 0) {
2096 continue;
2097 }
2098 tangent.normalize();
2099 Line innerTangent(tangent);
2100 Line outerTangent(tangent);
2101 innerTangent.fC -= 0.5;
2102 outerTangent.fC += 0.5;
2103 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2104 if (prevNormal.cross(normal) > 0) {
2105 // Miter inner points
2106 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2107 !innerTangent.intersect(inner, &innerPoint2) ||
2108 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002109 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002110 }
2111 Line prevTangent(prevV->fPoint,
2112 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2113 Line nextTangent(nextV->fPoint,
2114 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002115 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002116 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002117 }
2118 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002119 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002120 }
2121 outerPoint1 = outerPoint2 = outerPoint;
2122 } else {
2123 // Miter outer points
2124 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2125 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002126 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002127 }
2128 Line prevTangent(prevV->fPoint,
2129 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2130 Line nextTangent(nextV->fPoint,
2131 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002132 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002133 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002134 }
2135 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002136 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002137 }
2138 innerPoint1 = innerPoint2 = innerPoint;
2139 }
Stephen Whiteea495232018-04-03 11:28:15 -04002140 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2141 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2142 continue;
2143 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002144 TESS_LOG("inner (%g, %g), (%g, %g), ",
2145 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2146 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2147 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002148 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2149 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2150 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2151 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2152 innerVertex1->fPartner = outerVertex1;
2153 innerVertex2->fPartner = outerVertex2;
2154 outerVertex1->fPartner = innerVertex1;
2155 outerVertex2->fPartner = innerVertex2;
2156 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2157 innerInversion = false;
2158 }
2159 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2160 outerInversion = false;
2161 }
2162 innerVertices.append(innerVertex1);
2163 innerVertices.append(innerVertex2);
2164 outerVertices.append(outerVertex1);
2165 outerVertices.append(outerVertex2);
2166 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002167 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2168 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002169 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2170 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2171 innerVertex->fPartner = outerVertex;
2172 outerVertex->fPartner = innerVertex;
2173 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2174 innerInversion = false;
2175 }
2176 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2177 outerInversion = false;
2178 }
2179 innerVertices.append(innerVertex);
2180 outerVertices.append(outerVertex);
2181 }
2182 }
2183 prevInner = inner;
2184 prevOuter = outer;
2185 prevV = v;
2186 prevEdge = e;
2187 prevNormal = normal;
2188 }
2189 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2190 innerInversion = false;
2191 }
2192 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2193 outerInversion = false;
2194 }
2195 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2196 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2197 // interior inverts).
2198 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2199 // so it will be detected during collapse_overlap_regions().
2200 int innerWinding = innerInversion ? 2 : -2;
2201 int outerWinding = outerInversion ? -1 : 1;
2202 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2203 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2204 }
2205 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2206 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2207 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2208 }
2209 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2210 innerMesh->append(innerVertices);
2211 outerMesh->append(outerVertices);
2212}
senorblancof57372d2016-08-31 10:36:19 -07002213
Mike Reed7d34dc72019-11-26 12:17:17 -05002214void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002215 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002216 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002217 Vertex* start = down ? e->fTop : e->fBottom;
2218 do {
senorblancof57372d2016-08-31 10:36:19 -07002219 e->fWinding = down ? 1 : -1;
2220 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002221 e->fLine.normalize();
2222 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002223 boundary->append(e);
2224 if (down) {
2225 // Find outgoing edge, in clockwise order.
2226 if ((next = e->fNextEdgeAbove)) {
2227 down = false;
2228 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2229 down = true;
2230 } else if ((next = e->fPrevEdgeAbove)) {
2231 down = false;
2232 }
2233 } else {
2234 // Find outgoing edge, in counter-clockwise order.
2235 if ((next = e->fPrevEdgeBelow)) {
2236 down = true;
2237 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2238 down = false;
2239 } else if ((next = e->fNextEdgeBelow)) {
2240 down = true;
2241 }
2242 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002243 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002244 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002245 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002246}
2247
Stephen White5ad721e2017-02-23 16:50:47 -05002248// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002249
Stephen Whitebda29c02017-03-13 15:10:13 -04002250void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Mike Reed7d34dc72019-11-26 12:17:17 -05002251 VertexList* outerVertices, SkPathFillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002252 Comparator& c, SkArenaAlloc& alloc) {
2253 remove_non_boundary_edges(inMesh, fillType, alloc);
2254 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002255 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002256 EdgeList boundary;
2257 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2258 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002259 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002260 }
2261 }
senorblancof57372d2016-08-31 10:36:19 -07002262}
2263
Stephen Whitebda29c02017-03-13 15:10:13 -04002264// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002265
Chris Daltondcc8c542020-01-28 17:55:56 -07002266void contours_to_mesh(VertexList* contours, int contourCnt, Mode mode,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002267 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002268#if LOGGING_ENABLED
2269 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002270 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002271 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002272 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002273 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002274 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002275 }
2276 }
2277#endif
Chris Daltondcc8c542020-01-28 17:55:56 -07002278 sanitize_contours(contours, contourCnt, mode);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002279 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002280}
2281
Stephen Whitebda29c02017-03-13 15:10:13 -04002282void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002283 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002284 return;
ethannicholase9709e82016-01-07 13:34:16 -08002285 }
2286
2287 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002288 if (c.fDirection == Comparator::Direction::kHorizontal) {
2289 merge_sort<sweep_lt_horiz>(vertices);
2290 } else {
2291 merge_sort<sweep_lt_vert>(vertices);
2292 }
ethannicholase9709e82016-01-07 13:34:16 -08002293#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002294 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002295 static float gID = 0.0f;
2296 v->fID = gID++;
2297 }
2298#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002299}
2300
Mike Reed7d34dc72019-11-26 12:17:17 -05002301Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPathFillType fillType,
Chris Daltondcc8c542020-01-28 17:55:56 -07002302 const SkRect& pathBounds, Mode mode, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002303 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002304 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2305 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002306 VertexList mesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002307 contours_to_mesh(contours, contourCnt, mode, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002308 sort_mesh(&mesh, c, alloc);
2309 merge_coincident_vertices(&mesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002310 if (SimplifyResult::kAbort == simplify(mode, &mesh, c, alloc)) {
2311 return nullptr;
2312 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002313 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002314 dump_mesh(mesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002315 if (Mode::kEdgeAntialias == mode) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002316 VertexList innerMesh;
2317 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2318 sort_mesh(&innerMesh, c, alloc);
2319 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002320 merge_coincident_vertices(&innerMesh, c, alloc);
2321 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002322 auto result = simplify(mode, &innerMesh, c, alloc);
2323 SkASSERT(SimplifyResult::kAbort != result);
2324 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
2325 result = simplify(mode, outerMesh, c, alloc);
2326 SkASSERT(SimplifyResult::kAbort != result);
2327 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002328 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002329 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002330 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002331 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002332 EventComparator eventLT(EventComparator::Op::kLessThan);
2333 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2334 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2335 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002336 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002337 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002338 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002339 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002340 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002341 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002342 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002343 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002344 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002345 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2346 merge_coincident_vertices(&aaMesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002347 result = simplify(mode, &aaMesh, c, alloc);
2348 SkASSERT(SimplifyResult::kAbort != result);
Brian Salomon120e7d62019-09-11 10:29:22 -04002349 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002350 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002351 outerMesh->fHead = outerMesh->fTail = nullptr;
Chris Dalton6ccc0322020-01-29 11:38:16 -07002352 return tessellate(fillType, mode, aaMesh, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002353 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002354 TESS_LOG("no complex polygons; taking fast path\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07002355 return tessellate(fillType, mode, innerMesh, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002356 }
Stephen White49789062017-02-21 10:35:49 -05002357 } else {
Chris Dalton6ccc0322020-01-29 11:38:16 -07002358 return tessellate(fillType, mode, mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002359 }
senorblancof57372d2016-08-31 10:36:19 -07002360}
2361
2362// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Daltondcc8c542020-01-28 17:55:56 -07002363void* polys_to_triangles(Poly* polys, SkPathFillType fillType, Mode mode, void* data) {
2364 bool emitCoverage = (Mode::kEdgeAntialias == mode);
senorblancof57372d2016-08-31 10:36:19 -07002365 for (Poly* poly = polys; poly; poly = poly->fNext) {
2366 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002367 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002368 }
2369 }
2370 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002371}
2372
halcanary9d524f22016-03-29 09:03:52 -07002373Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002374 int contourCnt, SkArenaAlloc& alloc, Mode mode, int* numCountedCurves,
Stephen Whitebda29c02017-03-13 15:10:13 -04002375 VertexList* outerMesh) {
Mike Reedcf0e3c62019-12-03 16:26:15 -05002376 SkPathFillType fillType = path.getFillType();
Mike Reed7d34dc72019-11-26 12:17:17 -05002377 if (SkPathFillType_IsInverse(fillType)) {
ethannicholase9709e82016-01-07 13:34:16 -08002378 contourCnt++;
2379 }
Stephen White3a9aab92017-03-07 14:07:18 -05002380 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002381
Chris Dalton8e2b6942020-04-22 15:55:00 -06002382 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, mode, numCountedCurves);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002383 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Chris Daltondcc8c542020-01-28 17:55:56 -07002384 mode, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002385}
2386
Stephen White11f65e02017-02-16 19:00:39 -05002387int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002388 // We could theoretically be more aggressive about not counting empty contours, but we need to
2389 // actually match the exact number of contour linked lists the tessellator will create later on.
2390 int contourCnt = 1;
2391 bool hasPoints = false;
2392
2393 SkPath::Iter iter(path, false);
2394 SkPath::Verb verb;
2395 SkPoint pts[4];
2396 bool first = true;
2397 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
2398 switch (verb) {
2399 case SkPath::kMove_Verb:
2400 if (!first) {
2401 ++contourCnt;
2402 }
2403 // fallthru.
2404 case SkPath::kLine_Verb:
2405 case SkPath::kConic_Verb:
2406 case SkPath::kQuad_Verb:
2407 case SkPath::kCubic_Verb:
2408 hasPoints = true;
2409 // fallthru to break.
2410 default:
2411 break;
2412 }
2413 first = false;
2414 }
2415 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002416 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002417 }
Stephen White11f65e02017-02-16 19:00:39 -05002418 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002419}
2420
Mike Reed7d34dc72019-11-26 12:17:17 -05002421int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002422 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002423 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002424 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002425 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
ethannicholase9709e82016-01-07 13:34:16 -08002426 }
2427 }
2428 return count;
2429}
2430
Greg Danield5b45932018-06-07 13:15:10 -04002431int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2432 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002433 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2434 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002435 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002436 }
2437 }
2438 return count;
2439}
2440
Brian Osman0995fd52019-01-09 09:52:25 -05002441void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002442 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2443 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2444 Vertex* v0 = e->fTop;
2445 Vertex* v1 = e->fBottom;
2446 Vertex* v2 = e->fBottom->fPartner;
2447 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002448 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2449 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002450 }
2451 }
2452 return data;
2453}
2454
ethannicholase9709e82016-01-07 13:34:16 -08002455} // namespace
2456
Chris Dalton17dc4182020-03-25 16:18:16 -06002457namespace GrTriangulator {
ethannicholase9709e82016-01-07 13:34:16 -08002458
2459// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2460
halcanary9d524f22016-03-29 09:03:52 -07002461int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002462 GrEagerVertexAllocator* vertexAllocator, Mode mode, int* numCountedCurves) {
Stephen White11f65e02017-02-16 19:00:39 -05002463 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002464 if (contourCnt <= 0) {
Chris Dalton8e2b6942020-04-22 15:55:00 -06002465 *numCountedCurves = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002466 return 0;
2467 }
Stephen White11f65e02017-02-16 19:00:39 -05002468 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002469 VertexList outerMesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002470 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, mode,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002471 numCountedCurves, &outerMesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002472 SkPathFillType fillType = (Mode::kEdgeAntialias == mode) ?
2473 SkPathFillType::kWinding : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002474 int64_t count64 = count_points(polys, fillType);
Chris Daltondcc8c542020-01-28 17:55:56 -07002475 if (Mode::kEdgeAntialias == mode) {
Greg Danield5b45932018-06-07 13:15:10 -04002476 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002477 }
Greg Danield5b45932018-06-07 13:15:10 -04002478 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002479 return 0;
2480 }
Greg Danield5b45932018-06-07 13:15:10 -04002481 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002482
Chris Daltondcc8c542020-01-28 17:55:56 -07002483 size_t vertexStride = GetVertexStride(mode);
Chris Daltond081dce2020-01-23 12:09:04 -07002484 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002485 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002486 SkDebugf("Could not allocate vertices\n");
2487 return 0;
2488 }
senorblancof57372d2016-08-31 10:36:19 -07002489
Brian Salomon120e7d62019-09-11 10:29:22 -04002490 TESS_LOG("emitting %d verts\n", count);
Chris Daltondcc8c542020-01-28 17:55:56 -07002491 void* end = polys_to_triangles(polys, fillType, mode, verts);
Brian Osman80879d42019-01-07 16:15:27 -05002492 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002493
senorblancof57372d2016-08-31 10:36:19 -07002494 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002495 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002496 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002497 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002498 return actualCount;
2499}
2500
halcanary9d524f22016-03-29 09:03:52 -07002501int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton17dc4182020-03-25 16:18:16 -06002502 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002503 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002504 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002505 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002506 return 0;
2507 }
Stephen White11f65e02017-02-16 19:00:39 -05002508 SkArenaAlloc alloc(kArenaChunkSize);
Chris Dalton8e2b6942020-04-22 15:55:00 -06002509 int numCountedCurves;
Chris Daltondcc8c542020-01-28 17:55:56 -07002510 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, Mode::kNormal,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002511 &numCountedCurves, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002512 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002513 int64_t count64 = count_points(polys, fillType);
2514 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002515 *verts = nullptr;
2516 return 0;
2517 }
Greg Danield5b45932018-06-07 13:15:10 -04002518 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002519
Chris Dalton17dc4182020-03-25 16:18:16 -06002520 *verts = new WindingVertex[count];
2521 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002522 SkPoint* points = new SkPoint[count];
2523 SkPoint* pointsEnd = points;
2524 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002525 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002526 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002527 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002528 while (start != pointsEnd) {
2529 vertsEnd->fPos = *start;
2530 vertsEnd->fWinding = poly->fWinding;
2531 ++start;
2532 ++vertsEnd;
2533 }
2534 }
2535 }
2536 int actualCount = static_cast<int>(vertsEnd - *verts);
2537 SkASSERT(actualCount <= count);
2538 SkASSERT(pointsEnd - points == actualCount);
2539 delete[] points;
2540 return actualCount;
2541}
2542
2543} // namespace