blob: a9697327633215ca63bcd4bcdb10797cdd60741f [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
Chris Dalton8e2b6942020-04-22 15:55:00 -0600825 int localCurveCount = 0;
Stephen White3a9aab92017-03-07 14:07:18 -0500826 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800827 if (path.isInverseFillType()) {
828 SkPoint quad[4];
829 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700830 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500831 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800832 }
Stephen White3a9aab92017-03-07 14:07:18 -0500833 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800834 }
835 SkAutoConicToQuads converter;
Chris Dalton64964bb2020-05-01 16:01:46 -0600836 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
ethannicholase9709e82016-01-07 13:34:16 -0800837 switch (verb) {
Chris Dalton64964bb2020-05-01 16:01:46 -0600838 case SkPathVerb::kConic: {
Chris Dalton8e2b6942020-04-22 15:55:00 -0600839 ++localCurveCount;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700840 if (innerPolygons) {
841 append_point_to_contour(pts[2], contour, alloc);
842 break;
843 }
Chris Dalton64964bb2020-05-01 16:01:46 -0600844 const SkPoint* quadPts = converter.computeQuads(pts, *w, toleranceSqd);
ethannicholase9709e82016-01-07 13:34:16 -0800845 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400846 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800847 quadPts += 2;
848 }
ethannicholase9709e82016-01-07 13:34:16 -0800849 break;
850 }
Chris Dalton64964bb2020-05-01 16:01:46 -0600851 case SkPathVerb::kMove:
Stephen White3a9aab92017-03-07 14:07:18 -0500852 if (contour->fHead) {
853 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800854 }
Stephen White3a9aab92017-03-07 14:07:18 -0500855 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800856 break;
Chris Dalton64964bb2020-05-01 16:01:46 -0600857 case SkPathVerb::kLine: {
Stephen White3a9aab92017-03-07 14:07:18 -0500858 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800859 break;
860 }
Chris Dalton64964bb2020-05-01 16:01:46 -0600861 case SkPathVerb::kQuad: {
Chris Dalton8e2b6942020-04-22 15:55:00 -0600862 ++localCurveCount;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700863 if (innerPolygons) {
864 append_point_to_contour(pts[2], contour, alloc);
865 break;
866 }
867 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800868 break;
869 }
Chris Dalton64964bb2020-05-01 16:01:46 -0600870 case SkPathVerb::kCubic: {
Chris Dalton8e2b6942020-04-22 15:55:00 -0600871 ++localCurveCount;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700872 if (innerPolygons) {
873 append_point_to_contour(pts[3], contour, alloc);
874 break;
875 }
ethannicholase9709e82016-01-07 13:34:16 -0800876 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500877 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
878 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800879 break;
880 }
Chris Dalton64964bb2020-05-01 16:01:46 -0600881 case SkPathVerb::kClose:
ethannicholase9709e82016-01-07 13:34:16 -0800882 break;
883 }
884 }
Chris Dalton8e2b6942020-04-22 15:55:00 -0600885 *numCountedCurves = localCurveCount;
ethannicholase9709e82016-01-07 13:34:16 -0800886}
887
Mike Reed7d34dc72019-11-26 12:17:17 -0500888inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800889 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500890 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800891 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500892 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800893 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500894 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700895 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500896 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800897 return (winding & 1) == 1;
898 default:
899 SkASSERT(false);
900 return false;
901 }
902}
903
Mike Reed7d34dc72019-11-26 12:17:17 -0500904inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500905 return poly && apply_fill_type(fillType, poly->fWinding);
906}
907
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500908Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500909 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800910 Vertex* top = winding < 0 ? next : prev;
911 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500912 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800913}
914
915void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400916 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700917 SkASSERT(edges->contains(edge));
918 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800919}
920
921void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400922 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700923 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800924 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700925 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800926}
927
928void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500929 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800930 *left = v->fFirstEdgeAbove->fLeft;
931 *right = v->fLastEdgeAbove->fRight;
932 return;
933 }
934 Edge* next = nullptr;
935 Edge* prev;
936 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
937 if (prev->isLeftOf(v)) {
938 break;
939 }
940 next = prev;
941 }
942 *left = prev;
943 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800944}
945
ethannicholase9709e82016-01-07 13:34:16 -0800946void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
947 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500948 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800949 return;
950 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400951 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
952 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800953 Edge* prev = nullptr;
954 Edge* next;
955 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
956 if (next->isRightOf(edge->fTop)) {
957 break;
958 }
959 prev = next;
960 }
senorblancoe6eaa322016-03-08 09:06:44 -0800961 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800962 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
963}
964
965void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
966 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500967 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800968 return;
969 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400970 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
971 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800972 Edge* prev = nullptr;
973 Edge* next;
974 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
975 if (next->isRightOf(edge->fBottom)) {
976 break;
977 }
978 prev = next;
979 }
senorblancoe6eaa322016-03-08 09:06:44 -0800980 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800981 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
982}
983
984void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400985 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400986 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
987 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800988 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800989 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
990}
991
992void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400993 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400994 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
995 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800996 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800997 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
998}
999
Stephen Whitee7a364d2017-01-11 16:19:26 -05001000void disconnect(Edge* edge)
1001{
ethannicholase9709e82016-01-07 13:34:16 -08001002 remove_edge_above(edge);
1003 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -05001004}
1005
Stephen White3b5a3fa2017-06-06 14:51:19 -04001006void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
1007
1008void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
1009 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
1010 return;
1011 }
1012 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -04001013 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001014 while (v != dst) {
1015 v = v->fPrev;
1016 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1017 remove_edge(e, activeEdges);
1018 }
1019 Edge* leftEdge = v->fLeftEnclosingEdge;
1020 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1021 insert_edge(e, leftEdge, activeEdges);
1022 leftEdge = e;
Stephen Whitec03e6982020-02-06 16:32:14 -05001023 Vertex* top = e->fTop;
1024 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
1025 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
1026 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
1027 dst = top;
1028 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001029 }
1030 }
1031 *current = v;
1032}
1033
Stephen Whitec03e6982020-02-06 16:32:14 -05001034void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
1035 if (!activeEdges || !current) {
1036 return;
1037 }
1038 Vertex* top = edge->fTop;
1039 Vertex* bottom = edge->fBottom;
1040 if (edge->fLeft) {
1041 Vertex* leftTop = edge->fLeft->fTop;
1042 Vertex* leftBottom = edge->fLeft->fBottom;
1043 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
1044 rewind(activeEdges, current, leftTop, c);
1045 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
1046 rewind(activeEdges, current, top, c);
1047 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
1048 !edge->fLeft->isLeftOf(bottom)) {
1049 rewind(activeEdges, current, leftTop, c);
1050 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
1051 rewind(activeEdges, current, top, c);
1052 }
1053 }
1054 if (edge->fRight) {
1055 Vertex* rightTop = edge->fRight->fTop;
1056 Vertex* rightBottom = edge->fRight->fBottom;
1057 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
1058 rewind(activeEdges, current, rightTop, c);
1059 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
1060 rewind(activeEdges, current, top, c);
1061 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1062 !edge->fRight->isRightOf(bottom)) {
1063 rewind(activeEdges, current, rightTop, c);
1064 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1065 !edge->isLeftOf(rightBottom)) {
1066 rewind(activeEdges, current, top, c);
1067 }
1068 }
1069}
1070
Stephen White3b5a3fa2017-06-06 14:51:19 -04001071void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001072 remove_edge_below(edge);
1073 edge->fTop = v;
1074 edge->recompute();
1075 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001076 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001077 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001078}
1079
Stephen White3b5a3fa2017-06-06 14:51:19 -04001080void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001081 remove_edge_above(edge);
1082 edge->fBottom = v;
1083 edge->recompute();
1084 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001085 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001086 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001087}
1088
Stephen White3b5a3fa2017-06-06 14:51:19 -04001089void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1090 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001091 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001092 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1093 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1094 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001095 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001096 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001097 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001098 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001099 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
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 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001103 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001104 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001105 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001106 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001107 }
1108}
1109
Stephen White3b5a3fa2017-06-06 14:51:19 -04001110void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1111 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001112 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001113 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1114 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1115 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001116 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001117 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001118 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001119 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001120 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001121 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001122 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001123 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001124 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001125 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001126 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001127 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001128 }
1129}
1130
Stephen Whited26b4d82018-07-26 10:02:27 -04001131bool top_collinear(Edge* left, Edge* right) {
1132 if (!left || !right) {
1133 return false;
1134 }
1135 return left->fTop->fPoint == right->fTop->fPoint ||
1136 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1137}
1138
1139bool bottom_collinear(Edge* left, Edge* right) {
1140 if (!left || !right) {
1141 return false;
1142 }
1143 return left->fBottom->fPoint == right->fBottom->fPoint ||
1144 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1145}
1146
Stephen White3b5a3fa2017-06-06 14:51:19 -04001147void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001148 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001149 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001150 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001151 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001152 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001153 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001154 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001155 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001156 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001157 } else {
1158 break;
1159 }
ethannicholase9709e82016-01-07 13:34:16 -08001160 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001161 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1162 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1163 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1164 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001165}
1166
Stephen White89042d52018-06-08 12:18:22 -04001167bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001168 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001169 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001170 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001171 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001172 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1173 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001174 Vertex* top;
1175 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001176 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001177 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001178 top = v;
1179 bottom = edge->fTop;
1180 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001181 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001182 top = edge->fBottom;
1183 bottom = v;
1184 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001185 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001186 top = v;
1187 bottom = edge->fBottom;
1188 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001189 }
Stephen White531a48e2018-06-01 09:49:39 -04001190 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001191 insert_edge_below(newEdge, top, c);
1192 insert_edge_above(newEdge, bottom, c);
1193 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001194 return true;
1195}
1196
1197bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1198 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1199 return false;
1200 }
Stephen White1c5fd182018-07-12 15:54:05 -04001201 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1202 return false;
1203 }
Stephen White89042d52018-06-08 12:18:22 -04001204 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1205 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001206 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001207 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1208 }
1209 } else {
1210 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001211 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001212 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1213 }
1214 }
1215 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1216 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001217 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001218 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1219 }
1220 } else {
1221 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001222 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001223 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1224 }
1225 }
1226 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001227}
1228
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001229Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001230 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001231 if (!prev || !next || prev->fPoint == next->fPoint) {
1232 return nullptr;
1233 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001234 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001235 insert_edge_below(edge, edge->fTop, c);
1236 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001237 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001238 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001239 return edge;
1240}
1241
Stephen Whitebf6137e2017-01-04 15:43:26 -05001242void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001243 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001244 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1245 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -05001246 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001247 if (src->fPartner) {
1248 src->fPartner->fPartner = dst;
1249 }
Stephen White7b376942018-05-22 11:51:32 -04001250 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001251 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001252 }
Stephen White7b376942018-05-22 11:51:32 -04001253 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001254 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001255 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001256 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001257 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001258}
1259
Stephen White95152e12017-12-18 10:52:44 -05001260Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1261 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1262 Vertex* prevV = reference;
1263 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1264 prevV = prevV->fPrev;
1265 }
1266 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1267 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1268 prevV = nextV;
1269 nextV = nextV->fNext;
1270 }
1271 Vertex* v;
1272 if (prevV && coincident(prevV->fPoint, p)) {
1273 v = prevV;
1274 } else if (nextV && coincident(nextV->fPoint, p)) {
1275 v = nextV;
1276 } else {
1277 v = alloc.make<Vertex>(p, alpha);
1278#if LOGGING_ENABLED
1279 if (!prevV) {
1280 v->fID = mesh->fHead->fID - 1.0f;
1281 } else if (!nextV) {
1282 v->fID = mesh->fTail->fID + 1.0f;
1283 } else {
1284 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1285 }
1286#endif
1287 mesh->insert(v, prevV, nextV);
1288 }
1289 return v;
1290}
1291
Stephen White53a02982018-05-30 22:47:46 -04001292// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1293// sort criterion, it may not be possible to split correctly, since there is no point which is
1294// below the top and above the bottom. This function detects that case.
1295bool nearly_flat(Comparator& c, Edge* edge) {
1296 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1297 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001298 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001299}
1300
Stephen Whitee62999f2018-06-05 18:45:07 -04001301SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1302 if (c.sweep_lt(p, min)) {
1303 return min;
1304 } else if (c.sweep_lt(max, p)) {
1305 return max;
1306 } else {
1307 return p;
1308 }
1309}
1310
Stephen Whitec4dbc372019-05-22 10:50:14 -04001311void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1312 Line line1 = edge1->fLine;
1313 Line line2 = edge2->fLine;
1314 line1.normalize();
1315 line2.normalize();
1316 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1317 if (cosAngle > 0.999) {
1318 return;
1319 }
1320 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1321 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1322 SkPoint p;
1323 if (line1.intersect(line2, &p)) {
1324 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1325 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001326 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 -04001327 }
1328}
1329
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001330bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001331 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001332 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001333 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001334 }
Stephen White56158ae2017-01-30 14:31:31 -05001335 SkPoint p;
1336 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001337 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001338 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001339 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001340 Vertex* top = *current;
1341 // If the intersection point is above the current vertex, rewind to the vertex above the
1342 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001343 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001344 top = top->fPrev;
1345 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001346 if (!nearly_flat(c, left)) {
1347 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001348 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001349 if (!nearly_flat(c, right)) {
1350 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001351 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001352 if (p == left->fTop->fPoint) {
1353 v = left->fTop;
1354 } else if (p == left->fBottom->fPoint) {
1355 v = left->fBottom;
1356 } else if (p == right->fTop->fPoint) {
1357 v = right->fTop;
1358 } else if (p == right->fBottom->fPoint) {
1359 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001360 } else {
Stephen White95152e12017-12-18 10:52:44 -05001361 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001362 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001363 v->fSynthetic = true;
1364 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001365 }
ethannicholase9709e82016-01-07 13:34:16 -08001366 }
Stephen White0cb31672017-06-08 14:41:01 -04001367 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001368 split_edge(left, v, activeEdges, current, c, alloc);
1369 split_edge(right, v, activeEdges, current, c, alloc);
Brian Osman788b9162020-02-07 10:36:46 -05001370 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001371 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001372 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001373 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001374}
1375
Chris Daltondcc8c542020-01-28 17:55:56 -07001376void sanitize_contours(VertexList* contours, int contourCnt, Mode mode) {
1377 bool approximate = (Mode::kEdgeAntialias == mode);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001378 bool removeCollinearVertices = (Mode::kSimpleInnerPolygons != mode);
Stephen White3a9aab92017-03-07 14:07:18 -05001379 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1380 SkASSERT(contour->fHead);
1381 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001382 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001383 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001384 }
Stephen White3a9aab92017-03-07 14:07:18 -05001385 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001386 if (approximate) {
1387 round(&v->fPoint);
1388 }
Stephen White3a9aab92017-03-07 14:07:18 -05001389 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001390 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001391 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001392 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001393 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001394 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001395 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001396 contour->remove(v);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001397 } else if (removeCollinearVertices &&
1398 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001399 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001400 contour->remove(v);
1401 } else {
1402 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001403 }
Stephen White3a9aab92017-03-07 14:07:18 -05001404 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001405 }
1406 }
1407}
1408
Stephen Whitee260c462017-12-19 18:09:54 -05001409bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001410 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001411 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001412 }
Stephen Whitee260c462017-12-19 18:09:54 -05001413 bool merged = false;
1414 for (Vertex* v = mesh->fHead->fNext; v;) {
1415 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001416 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1417 v->fPoint = v->fPrev->fPoint;
1418 }
1419 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001420 merge_vertices(v, v->fPrev, mesh, c, alloc);
1421 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001422 }
Stephen Whitee260c462017-12-19 18:09:54 -05001423 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001424 }
Stephen Whitee260c462017-12-19 18:09:54 -05001425 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001426}
1427
1428// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1429
Stephen White3a9aab92017-03-07 14:07:18 -05001430void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001431 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001432 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1433 Vertex* prev = contour->fTail;
1434 for (Vertex* v = contour->fHead; v;) {
1435 Vertex* next = v->fNext;
1436 connect(prev, v, Edge::Type::kInner, c, alloc);
1437 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001438 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001439 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001440 }
1441 }
ethannicholase9709e82016-01-07 13:34:16 -08001442}
1443
Stephen Whitee260c462017-12-19 18:09:54 -05001444void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1445 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001446 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001447 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1448 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1449 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1450 // number.
1451 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1452 inner->fPartner = outer->fPartner = nullptr;
1453 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001454 }
1455 }
1456}
1457
1458template <CompareFunc sweep_lt>
1459void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1460 Vertex* a = front->fHead;
1461 Vertex* b = back->fHead;
1462 while (a && b) {
1463 if (sweep_lt(a->fPoint, b->fPoint)) {
1464 front->remove(a);
1465 result->append(a);
1466 a = front->fHead;
1467 } else {
1468 back->remove(b);
1469 result->append(b);
1470 b = back->fHead;
1471 }
1472 }
1473 result->append(*front);
1474 result->append(*back);
1475}
1476
1477void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1478 if (c.fDirection == Comparator::Direction::kHorizontal) {
1479 sorted_merge<sweep_lt_horiz>(front, back, result);
1480 } else {
1481 sorted_merge<sweep_lt_vert>(front, back, result);
1482 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001483#if LOGGING_ENABLED
1484 float id = 0.0f;
1485 for (Vertex* v = result->fHead; v; v = v->fNext) {
1486 v->fID = id++;
1487 }
1488#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001489}
1490
ethannicholase9709e82016-01-07 13:34:16 -08001491// Stage 3: sort the vertices by increasing sweep direction.
1492
Stephen White16a40cb2017-02-23 11:10:01 -05001493template <CompareFunc sweep_lt>
1494void merge_sort(VertexList* vertices) {
1495 Vertex* slow = vertices->fHead;
1496 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001497 return;
1498 }
Stephen White16a40cb2017-02-23 11:10:01 -05001499 Vertex* fast = slow->fNext;
1500 if (!fast) {
1501 return;
1502 }
1503 do {
1504 fast = fast->fNext;
1505 if (fast) {
1506 fast = fast->fNext;
1507 slow = slow->fNext;
1508 }
1509 } while (fast);
1510 VertexList front(vertices->fHead, slow);
1511 VertexList back(slow->fNext, vertices->fTail);
1512 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001513
Stephen White16a40cb2017-02-23 11:10:01 -05001514 merge_sort<sweep_lt>(&front);
1515 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001516
Stephen White16a40cb2017-02-23 11:10:01 -05001517 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001518 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001519}
1520
Stephen White95152e12017-12-18 10:52:44 -05001521void dump_mesh(const VertexList& mesh) {
1522#if LOGGING_ENABLED
1523 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001524 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 -05001525 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001526 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1527 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001528 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001529 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001530 }
1531 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001532 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001533 }
1534 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001535 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001536 }
1537 }
1538#endif
1539}
1540
Stephen Whitec4dbc372019-05-22 10:50:14 -04001541void dump_skel(const SSEdgeList& ssEdges) {
1542#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001543 for (SSEdge* edge : ssEdges) {
1544 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001545 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001546 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001547 edge->fNext->fVertex->fID);
1548 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001549 TESS_LOG(" (original %g -> %g)\n",
1550 edge->fEdge->fTop->fID,
1551 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001552 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001553 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001554 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001555 }
1556 }
1557#endif
1558}
1559
Stephen White89042d52018-06-08 12:18:22 -04001560#ifdef SK_DEBUG
1561void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1562 if (!left || !right) {
1563 return;
1564 }
1565 if (left->fTop == right->fTop) {
1566 SkASSERT(left->isLeftOf(right->fBottom));
1567 SkASSERT(right->isRightOf(left->fBottom));
1568 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1569 SkASSERT(left->isLeftOf(right->fTop));
1570 } else {
1571 SkASSERT(right->isRightOf(left->fTop));
1572 }
1573 if (left->fBottom == right->fBottom) {
1574 SkASSERT(left->isLeftOf(right->fTop));
1575 SkASSERT(right->isRightOf(left->fTop));
1576 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1577 SkASSERT(left->isLeftOf(right->fBottom));
1578 } else {
1579 SkASSERT(right->isRightOf(left->fBottom));
1580 }
1581}
1582
1583void validate_edge_list(EdgeList* edges, Comparator& c) {
1584 Edge* left = edges->fHead;
1585 if (!left) {
1586 return;
1587 }
1588 for (Edge* right = left->fRight; right; right = right->fRight) {
1589 validate_edge_pair(left, right, c);
1590 left = right;
1591 }
1592}
1593#endif
1594
ethannicholase9709e82016-01-07 13:34:16 -08001595// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1596
Stephen Whitec4dbc372019-05-22 10:50:14 -04001597bool connected(Vertex* v) {
1598 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1599}
1600
Chris Dalton6ccc0322020-01-29 11:38:16 -07001601enum class SimplifyResult {
1602 kAlreadySimple,
1603 kFoundSelfIntersection,
1604 kAbort
1605};
1606
1607SimplifyResult simplify(Mode mode, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001608 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001609 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001610 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001611 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001612 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001613 continue;
1614 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001615 Edge* leftEnclosingEdge;
1616 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001617 bool restartChecks;
1618 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001619 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1620 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001621 restartChecks = false;
1622 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001623 v->fLeftEnclosingEdge = leftEnclosingEdge;
1624 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001625 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001626 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001627 if (check_for_intersection(
1628 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c, alloc) ||
1629 check_for_intersection(
1630 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c, alloc)) {
1631 if (Mode::kSimpleInnerPolygons == mode) {
1632 return SimplifyResult::kAbort;
1633 }
1634 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001635 restartChecks = true;
1636 break;
1637 }
1638 }
1639 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001640 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001641 &activeEdges, &v, mesh, c, alloc)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001642 if (Mode::kSimpleInnerPolygons == mode) {
1643 return SimplifyResult::kAbort;
1644 }
1645 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001646 restartChecks = true;
1647 }
1648
1649 }
1650 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001651#ifdef SK_DEBUG
1652 validate_edge_list(&activeEdges, c);
1653#endif
ethannicholase9709e82016-01-07 13:34:16 -08001654 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1655 remove_edge(e, &activeEdges);
1656 }
1657 Edge* leftEdge = leftEnclosingEdge;
1658 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1659 insert_edge(e, leftEdge, &activeEdges);
1660 leftEdge = e;
1661 }
ethannicholase9709e82016-01-07 13:34:16 -08001662 }
Stephen Whitee260c462017-12-19 18:09:54 -05001663 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001664 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001665}
1666
1667// Stage 5: Tessellate the simplified mesh into monotone polygons.
1668
Chris Dalton6ccc0322020-01-29 11:38:16 -07001669Poly* tessellate(SkPathFillType fillType, Mode mode, const VertexList& vertices,
1670 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001671 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001672 int maxWindMagnitude = std::numeric_limits<int>::max();
1673 if (Mode::kSimpleInnerPolygons == mode && !SkPathFillType_IsEvenOdd(fillType)) {
1674 maxWindMagnitude = 1;
1675 }
ethannicholase9709e82016-01-07 13:34:16 -08001676 EdgeList activeEdges;
1677 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001678 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001679 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001680 continue;
1681 }
1682#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001683 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 -08001684#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001685 Edge* leftEnclosingEdge;
1686 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001687 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001688 Poly* leftPoly;
1689 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001690 if (v->fFirstEdgeAbove) {
1691 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1692 rightPoly = v->fLastEdgeAbove->fRightPoly;
1693 } else {
1694 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1695 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1696 }
1697#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001698 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001699 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001700 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1701 e->fTop->fID, e->fBottom->fID,
1702 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1703 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001704 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001705 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001706 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001707 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1708 e->fTop->fID, e->fBottom->fID,
1709 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1710 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001711 }
1712#endif
1713 if (v->fFirstEdgeAbove) {
1714 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001715 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001716 }
1717 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001718 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001719 }
1720 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001721 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001722 remove_edge(e, &activeEdges);
1723 if (e->fRightPoly) {
1724 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001725 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001726 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001727 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001728 }
1729 }
1730 remove_edge(v->fLastEdgeAbove, &activeEdges);
1731 if (!v->fFirstEdgeBelow) {
1732 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1733 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1734 rightPoly->fPartner = leftPoly;
1735 leftPoly->fPartner = rightPoly;
1736 }
1737 }
1738 }
1739 if (v->fFirstEdgeBelow) {
1740 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001741 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001742 if (leftPoly == rightPoly) {
1743 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1744 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1745 leftPoly->fWinding, alloc);
1746 leftEnclosingEdge->fRightPoly = leftPoly;
1747 } else {
1748 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1749 rightPoly->fWinding, alloc);
1750 rightEnclosingEdge->fLeftPoly = rightPoly;
1751 }
ethannicholase9709e82016-01-07 13:34:16 -08001752 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001753 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001754 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1755 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001756 }
1757 }
1758 Edge* leftEdge = v->fFirstEdgeBelow;
1759 leftEdge->fLeftPoly = leftPoly;
1760 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1761 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1762 rightEdge = rightEdge->fNextEdgeBelow) {
1763 insert_edge(rightEdge, leftEdge, &activeEdges);
1764 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1765 winding += leftEdge->fWinding;
1766 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001767 if (abs(winding) > maxWindMagnitude) {
1768 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1769 }
ethannicholase9709e82016-01-07 13:34:16 -08001770 Poly* poly = new_poly(&polys, v, winding, alloc);
1771 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1772 }
1773 leftEdge = rightEdge;
1774 }
1775 v->fLastEdgeBelow->fRightPoly = rightPoly;
1776 }
1777#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001778 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001779 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001780 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1781 e->fTop->fID, e->fBottom->fID,
1782 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1783 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001784 }
1785#endif
1786 }
1787 return polys;
1788}
1789
Mike Reed7d34dc72019-11-26 12:17:17 -05001790void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001791 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001792 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001793 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001794 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001795 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001796 continue;
1797 }
1798 Edge* leftEnclosingEdge;
1799 Edge* rightEnclosingEdge;
1800 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1801 bool prevFilled = leftEnclosingEdge &&
1802 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1803 for (Edge* e = v->fFirstEdgeAbove; e;) {
1804 Edge* next = e->fNextEdgeAbove;
1805 remove_edge(e, &activeEdges);
1806 bool filled = apply_fill_type(fillType, e->fWinding);
1807 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001808 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001809 }
Stephen White49789062017-02-21 10:35:49 -05001810 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001811 e = next;
1812 }
Stephen White49789062017-02-21 10:35:49 -05001813 Edge* prev = leftEnclosingEdge;
1814 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1815 if (prev) {
1816 e->fWinding += prev->fWinding;
1817 }
1818 insert_edge(e, prev, &activeEdges);
1819 prev = e;
1820 }
senorblancof57372d2016-08-31 10:36:19 -07001821 }
senorblancof57372d2016-08-31 10:36:19 -07001822}
1823
Stephen White66412122017-03-01 11:48:27 -05001824// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001825void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001826 normal->set(SkDoubleToScalar(e->fLine.fA),
1827 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001828}
1829
1830// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1831// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1832// invert on stroking.
1833
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001834void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001835 Edge* prevEdge = boundary->fTail;
1836 SkVector prevNormal;
1837 get_edge_normal(prevEdge, &prevNormal);
1838 for (Edge* e = boundary->fHead; e != nullptr;) {
1839 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1840 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001841 double distPrev = e->dist(prev->fPoint);
1842 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001843 SkVector normal;
1844 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001845 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001846 if (prev == next) {
1847 remove_edge(prevEdge, boundary);
1848 remove_edge(e, boundary);
1849 prevEdge = boundary->fTail;
1850 e = boundary->fHead;
1851 if (prevEdge) {
1852 get_edge_normal(prevEdge, &prevNormal);
1853 }
1854 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001855 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001856 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001857 if (prev->fPoint != next->fPoint) {
1858 join->fLine.normalize();
1859 join->fLine = join->fLine * join->fWinding;
1860 }
senorblancof57372d2016-08-31 10:36:19 -07001861 insert_edge(join, e, boundary);
1862 remove_edge(prevEdge, boundary);
1863 remove_edge(e, boundary);
1864 if (join->fLeft && join->fRight) {
1865 prevEdge = join->fLeft;
1866 e = join;
1867 } else {
1868 prevEdge = boundary->fTail;
1869 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1870 }
1871 get_edge_normal(prevEdge, &prevNormal);
1872 } else {
1873 prevEdge = e;
1874 prevNormal = normal;
1875 e = e->fRight;
1876 }
1877 }
1878}
1879
Stephen Whitec4dbc372019-05-22 10:50:14 -04001880void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1881 if (v == dest) {
1882 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001883 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001884 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001885 if (v->fSynthetic) {
1886 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1887 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001888 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1889 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001890 v->fPartner->fPartner = dest;
1891 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001892 }
1893}
1894
Stephen Whitec4dbc372019-05-22 10:50:14 -04001895void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1896 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001897 return;
1898 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001899 Vertex* prev = fEdge->fPrev->fVertex;
1900 Vertex* next = fEdge->fNext->fVertex;
1901 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1902 SSEdge* nextEdge = fEdge->fNext->fNext;
1903 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1904 return;
Stephen White77169c82018-06-05 09:15:59 -04001905 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001906 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1907 dest->fSynthetic = true;
1908 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001909 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1910 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1911 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001912 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001913
Stephen Whitec4dbc372019-05-22 10:50:14 -04001914 ss_connect(prev, dest, c, alloc);
1915 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001916
Stephen Whitec4dbc372019-05-22 10:50:14 -04001917 prevEdge->fNext = nextEdge->fPrev = ssv;
1918 ssv->fPrev = prevEdge;
1919 ssv->fNext = nextEdge;
1920 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1921 return;
1922 }
1923 if (prevEdge->fEvent) {
1924 prevEdge->fEvent->fEdge = nullptr;
1925 }
1926 if (nextEdge->fEvent) {
1927 nextEdge->fEvent->fEdge = nullptr;
1928 }
1929 if (prevEdge->fPrev == nextEdge->fNext) {
1930 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1931 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1932 } else {
1933 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1934 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1935 if (dest->fPartner) {
1936 create_event(prevEdge, events, alloc);
1937 create_event(nextEdge, events, alloc);
1938 } else {
1939 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1940 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1941 }
1942 }
Stephen Whitee260c462017-12-19 18:09:54 -05001943}
1944
1945bool is_overlap_edge(Edge* e) {
1946 if (e->fType == Edge::Type::kOuter) {
1947 return e->fWinding != 0 && e->fWinding != 1;
1948 } else if (e->fType == Edge::Type::kInner) {
1949 return e->fWinding != 0 && e->fWinding != -2;
1950 } else {
1951 return false;
1952 }
1953}
1954
1955// This is a stripped-down version of tessellate() which computes edges which
1956// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001957bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1958 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001959 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001960 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001961 EventList events(comp);
1962 SSVertexMap ssVertices;
1963 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001964 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001965 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001966 continue;
1967 }
1968 Edge* leftEnclosingEdge;
1969 Edge* rightEnclosingEdge;
1970 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001971 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001972 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1973 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001974 bool leftOverlap = prev && is_overlap_edge(prev);
1975 bool rightOverlap = is_overlap_edge(e);
1976 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1977 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001978 if (prev) {
1979 e->fWinding -= prev->fWinding;
1980 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001981 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001982 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1983 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001984 disconnect(e);
1985 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001986 TESS_LOG("found overlap edge %g -> %g%s\n",
1987 e->fTop->fID, e->fBottom->fID,
1988 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001989 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1990 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1991 SSVertex* ssPrev = ssVertices[prevVertex];
1992 if (!ssPrev) {
1993 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1994 }
1995 SSVertex* ssNext = ssVertices[nextVertex];
1996 if (!ssNext) {
1997 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1998 }
1999 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
2000 ssEdges.push_back(ssEdge);
2001// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
2002 ssPrev->fNext = ssNext->fPrev = ssEdge;
2003 create_event(ssEdge, &events, alloc);
2004 if (!isOuterBoundary) {
2005 disconnect(e);
2006 }
2007 }
2008 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05002009 }
2010 Edge* prev = leftEnclosingEdge;
2011 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2012 if (prev) {
2013 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05002014 }
2015 insert_edge(e, prev, &activeEdges);
2016 prev = e;
2017 }
2018 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04002019 bool complex = events.size() > 0;
2020
Brian Salomon120e7d62019-09-11 10:29:22 -04002021 TESS_LOG("\ncollapsing overlap regions\n");
2022 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04002023 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002024 while (events.size() > 0) {
2025 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05002026 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04002027 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002028 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002029 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002030 dump_skel(ssEdges);
2031 for (SSEdge* edge : ssEdges) {
2032 if (Edge* e = edge->fEdge) {
2033 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
2034 }
2035 }
2036 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002037}
2038
2039bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
2040 if (!prev || !next) {
2041 return true;
2042 }
2043 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
2044 return winding != origEdge->fWinding;
2045}
Stephen White92eba8a2017-02-06 09:50:27 -05002046
senorblancof57372d2016-08-31 10:36:19 -07002047// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
2048// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
2049// new antialiased mesh from those vertices.
2050
Stephen Whitee260c462017-12-19 18:09:54 -05002051void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
2052 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002053 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002054 // A boundary with fewer than 3 edges is degenerate.
2055 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
2056 return;
2057 }
2058 Edge* prevEdge = boundary->fTail;
2059 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
2060 SkVector prevNormal;
2061 get_edge_normal(prevEdge, &prevNormal);
2062 double radius = 0.5;
2063 Line prevInner(prevEdge->fLine);
2064 prevInner.fC -= radius;
2065 Line prevOuter(prevEdge->fLine);
2066 prevOuter.fC += radius;
2067 VertexList innerVertices;
2068 VertexList outerVertices;
2069 bool innerInversion = true;
2070 bool outerInversion = true;
2071 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
2072 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2073 SkVector normal;
2074 get_edge_normal(e, &normal);
2075 Line inner(e->fLine);
2076 inner.fC -= radius;
2077 Line outer(e->fLine);
2078 outer.fC += radius;
2079 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04002080 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002081 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2082 prevOuter.intersect(outer, &outerPoint)) {
2083 float cosAngle = normal.dot(prevNormal);
2084 if (cosAngle < -kCosMiterAngle) {
2085 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2086
2087 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2088 Line bisector(innerPoint, outerPoint);
2089 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2090 if (tangent.fA == 0 && tangent.fB == 0) {
2091 continue;
2092 }
2093 tangent.normalize();
2094 Line innerTangent(tangent);
2095 Line outerTangent(tangent);
2096 innerTangent.fC -= 0.5;
2097 outerTangent.fC += 0.5;
2098 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2099 if (prevNormal.cross(normal) > 0) {
2100 // Miter inner points
2101 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2102 !innerTangent.intersect(inner, &innerPoint2) ||
2103 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002104 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002105 }
2106 Line prevTangent(prevV->fPoint,
2107 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2108 Line nextTangent(nextV->fPoint,
2109 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002110 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002111 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002112 }
2113 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002114 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002115 }
2116 outerPoint1 = outerPoint2 = outerPoint;
2117 } else {
2118 // Miter outer points
2119 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2120 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002121 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002122 }
2123 Line prevTangent(prevV->fPoint,
2124 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2125 Line nextTangent(nextV->fPoint,
2126 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002127 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002128 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002129 }
2130 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002131 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002132 }
2133 innerPoint1 = innerPoint2 = innerPoint;
2134 }
Stephen Whiteea495232018-04-03 11:28:15 -04002135 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2136 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2137 continue;
2138 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002139 TESS_LOG("inner (%g, %g), (%g, %g), ",
2140 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2141 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2142 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002143 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2144 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2145 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2146 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2147 innerVertex1->fPartner = outerVertex1;
2148 innerVertex2->fPartner = outerVertex2;
2149 outerVertex1->fPartner = innerVertex1;
2150 outerVertex2->fPartner = innerVertex2;
2151 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2152 innerInversion = false;
2153 }
2154 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2155 outerInversion = false;
2156 }
2157 innerVertices.append(innerVertex1);
2158 innerVertices.append(innerVertex2);
2159 outerVertices.append(outerVertex1);
2160 outerVertices.append(outerVertex2);
2161 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002162 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2163 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002164 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2165 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2166 innerVertex->fPartner = outerVertex;
2167 outerVertex->fPartner = innerVertex;
2168 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2169 innerInversion = false;
2170 }
2171 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2172 outerInversion = false;
2173 }
2174 innerVertices.append(innerVertex);
2175 outerVertices.append(outerVertex);
2176 }
2177 }
2178 prevInner = inner;
2179 prevOuter = outer;
2180 prevV = v;
2181 prevEdge = e;
2182 prevNormal = normal;
2183 }
2184 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2185 innerInversion = false;
2186 }
2187 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2188 outerInversion = false;
2189 }
2190 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2191 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2192 // interior inverts).
2193 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2194 // so it will be detected during collapse_overlap_regions().
2195 int innerWinding = innerInversion ? 2 : -2;
2196 int outerWinding = outerInversion ? -1 : 1;
2197 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2198 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2199 }
2200 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2201 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2202 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2203 }
2204 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2205 innerMesh->append(innerVertices);
2206 outerMesh->append(outerVertices);
2207}
senorblancof57372d2016-08-31 10:36:19 -07002208
Mike Reed7d34dc72019-11-26 12:17:17 -05002209void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002210 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002211 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002212 Vertex* start = down ? e->fTop : e->fBottom;
2213 do {
senorblancof57372d2016-08-31 10:36:19 -07002214 e->fWinding = down ? 1 : -1;
2215 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002216 e->fLine.normalize();
2217 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002218 boundary->append(e);
2219 if (down) {
2220 // Find outgoing edge, in clockwise order.
2221 if ((next = e->fNextEdgeAbove)) {
2222 down = false;
2223 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2224 down = true;
2225 } else if ((next = e->fPrevEdgeAbove)) {
2226 down = false;
2227 }
2228 } else {
2229 // Find outgoing edge, in counter-clockwise order.
2230 if ((next = e->fPrevEdgeBelow)) {
2231 down = true;
2232 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2233 down = false;
2234 } else if ((next = e->fNextEdgeBelow)) {
2235 down = true;
2236 }
2237 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002238 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002239 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002240 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002241}
2242
Stephen White5ad721e2017-02-23 16:50:47 -05002243// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002244
Stephen Whitebda29c02017-03-13 15:10:13 -04002245void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Mike Reed7d34dc72019-11-26 12:17:17 -05002246 VertexList* outerVertices, SkPathFillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002247 Comparator& c, SkArenaAlloc& alloc) {
2248 remove_non_boundary_edges(inMesh, fillType, alloc);
2249 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002250 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002251 EdgeList boundary;
2252 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2253 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002254 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002255 }
2256 }
senorblancof57372d2016-08-31 10:36:19 -07002257}
2258
Stephen Whitebda29c02017-03-13 15:10:13 -04002259// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002260
Chris Daltondcc8c542020-01-28 17:55:56 -07002261void contours_to_mesh(VertexList* contours, int contourCnt, Mode mode,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002262 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002263#if LOGGING_ENABLED
2264 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002265 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002266 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002267 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002268 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002269 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002270 }
2271 }
2272#endif
Chris Daltondcc8c542020-01-28 17:55:56 -07002273 sanitize_contours(contours, contourCnt, mode);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002274 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002275}
2276
Stephen Whitebda29c02017-03-13 15:10:13 -04002277void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002278 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002279 return;
ethannicholase9709e82016-01-07 13:34:16 -08002280 }
2281
2282 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002283 if (c.fDirection == Comparator::Direction::kHorizontal) {
2284 merge_sort<sweep_lt_horiz>(vertices);
2285 } else {
2286 merge_sort<sweep_lt_vert>(vertices);
2287 }
ethannicholase9709e82016-01-07 13:34:16 -08002288#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002289 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002290 static float gID = 0.0f;
2291 v->fID = gID++;
2292 }
2293#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002294}
2295
Mike Reed7d34dc72019-11-26 12:17:17 -05002296Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPathFillType fillType,
Chris Daltondcc8c542020-01-28 17:55:56 -07002297 const SkRect& pathBounds, Mode mode, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002298 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002299 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2300 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002301 VertexList mesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002302 contours_to_mesh(contours, contourCnt, mode, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002303 sort_mesh(&mesh, c, alloc);
2304 merge_coincident_vertices(&mesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002305 if (SimplifyResult::kAbort == simplify(mode, &mesh, c, alloc)) {
2306 return nullptr;
2307 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002308 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002309 dump_mesh(mesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002310 if (Mode::kEdgeAntialias == mode) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002311 VertexList innerMesh;
2312 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2313 sort_mesh(&innerMesh, c, alloc);
2314 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002315 merge_coincident_vertices(&innerMesh, c, alloc);
2316 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002317 auto result = simplify(mode, &innerMesh, c, alloc);
2318 SkASSERT(SimplifyResult::kAbort != result);
2319 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
2320 result = simplify(mode, outerMesh, c, alloc);
2321 SkASSERT(SimplifyResult::kAbort != result);
2322 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002323 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002324 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002325 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002326 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002327 EventComparator eventLT(EventComparator::Op::kLessThan);
2328 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2329 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2330 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002331 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002332 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002333 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002334 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002335 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002336 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002337 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002338 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002339 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002340 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2341 merge_coincident_vertices(&aaMesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002342 result = simplify(mode, &aaMesh, c, alloc);
2343 SkASSERT(SimplifyResult::kAbort != result);
Brian Salomon120e7d62019-09-11 10:29:22 -04002344 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002345 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002346 outerMesh->fHead = outerMesh->fTail = nullptr;
Chris Dalton6ccc0322020-01-29 11:38:16 -07002347 return tessellate(fillType, mode, aaMesh, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002348 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002349 TESS_LOG("no complex polygons; taking fast path\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07002350 return tessellate(fillType, mode, innerMesh, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002351 }
Stephen White49789062017-02-21 10:35:49 -05002352 } else {
Chris Dalton6ccc0322020-01-29 11:38:16 -07002353 return tessellate(fillType, mode, mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002354 }
senorblancof57372d2016-08-31 10:36:19 -07002355}
2356
2357// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Daltondcc8c542020-01-28 17:55:56 -07002358void* polys_to_triangles(Poly* polys, SkPathFillType fillType, Mode mode, void* data) {
2359 bool emitCoverage = (Mode::kEdgeAntialias == mode);
senorblancof57372d2016-08-31 10:36:19 -07002360 for (Poly* poly = polys; poly; poly = poly->fNext) {
2361 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002362 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002363 }
2364 }
2365 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002366}
2367
halcanary9d524f22016-03-29 09:03:52 -07002368Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002369 int contourCnt, SkArenaAlloc& alloc, Mode mode, int* numCountedCurves,
Stephen Whitebda29c02017-03-13 15:10:13 -04002370 VertexList* outerMesh) {
Mike Reedcf0e3c62019-12-03 16:26:15 -05002371 SkPathFillType fillType = path.getFillType();
Mike Reed7d34dc72019-11-26 12:17:17 -05002372 if (SkPathFillType_IsInverse(fillType)) {
ethannicholase9709e82016-01-07 13:34:16 -08002373 contourCnt++;
2374 }
Stephen White3a9aab92017-03-07 14:07:18 -05002375 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002376
Chris Dalton8e2b6942020-04-22 15:55:00 -06002377 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, mode, numCountedCurves);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002378 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Chris Daltondcc8c542020-01-28 17:55:56 -07002379 mode, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002380}
2381
Stephen White11f65e02017-02-16 19:00:39 -05002382int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002383 // We could theoretically be more aggressive about not counting empty contours, but we need to
2384 // actually match the exact number of contour linked lists the tessellator will create later on.
2385 int contourCnt = 1;
2386 bool hasPoints = false;
2387
Chris Daltonc71b3d42020-01-08 21:29:59 -07002388 bool first = true;
Chris Dalton64964bb2020-05-01 16:01:46 -06002389 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002390 switch (verb) {
Chris Dalton64964bb2020-05-01 16:01:46 -06002391 case SkPathVerb::kMove:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002392 if (!first) {
2393 ++contourCnt;
2394 }
2395 // fallthru.
Chris Dalton64964bb2020-05-01 16:01:46 -06002396 case SkPathVerb::kLine:
2397 case SkPathVerb::kConic:
2398 case SkPathVerb::kQuad:
2399 case SkPathVerb::kCubic:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002400 hasPoints = true;
2401 // fallthru to break.
2402 default:
2403 break;
2404 }
2405 first = false;
2406 }
2407 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002408 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002409 }
Stephen White11f65e02017-02-16 19:00:39 -05002410 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002411}
2412
Mike Reed7d34dc72019-11-26 12:17:17 -05002413int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002414 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002415 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002416 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002417 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
ethannicholase9709e82016-01-07 13:34:16 -08002418 }
2419 }
2420 return count;
2421}
2422
Greg Danield5b45932018-06-07 13:15:10 -04002423int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2424 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002425 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2426 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002427 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002428 }
2429 }
2430 return count;
2431}
2432
Brian Osman0995fd52019-01-09 09:52:25 -05002433void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002434 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2435 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2436 Vertex* v0 = e->fTop;
2437 Vertex* v1 = e->fBottom;
2438 Vertex* v2 = e->fBottom->fPartner;
2439 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002440 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2441 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002442 }
2443 }
2444 return data;
2445}
2446
ethannicholase9709e82016-01-07 13:34:16 -08002447} // namespace
2448
Chris Dalton17dc4182020-03-25 16:18:16 -06002449namespace GrTriangulator {
ethannicholase9709e82016-01-07 13:34:16 -08002450
2451// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2452
halcanary9d524f22016-03-29 09:03:52 -07002453int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002454 GrEagerVertexAllocator* vertexAllocator, Mode mode, int* numCountedCurves) {
Stephen White11f65e02017-02-16 19:00:39 -05002455 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002456 if (contourCnt <= 0) {
Chris Dalton8e2b6942020-04-22 15:55:00 -06002457 *numCountedCurves = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002458 return 0;
2459 }
Stephen White11f65e02017-02-16 19:00:39 -05002460 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002461 VertexList outerMesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002462 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, mode,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002463 numCountedCurves, &outerMesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002464 SkPathFillType fillType = (Mode::kEdgeAntialias == mode) ?
2465 SkPathFillType::kWinding : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002466 int64_t count64 = count_points(polys, fillType);
Chris Daltondcc8c542020-01-28 17:55:56 -07002467 if (Mode::kEdgeAntialias == mode) {
Greg Danield5b45932018-06-07 13:15:10 -04002468 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002469 }
Greg Danield5b45932018-06-07 13:15:10 -04002470 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002471 return 0;
2472 }
Greg Danield5b45932018-06-07 13:15:10 -04002473 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002474
Chris Daltondcc8c542020-01-28 17:55:56 -07002475 size_t vertexStride = GetVertexStride(mode);
Chris Daltond081dce2020-01-23 12:09:04 -07002476 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002477 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002478 SkDebugf("Could not allocate vertices\n");
2479 return 0;
2480 }
senorblancof57372d2016-08-31 10:36:19 -07002481
Brian Salomon120e7d62019-09-11 10:29:22 -04002482 TESS_LOG("emitting %d verts\n", count);
Chris Daltondcc8c542020-01-28 17:55:56 -07002483 void* end = polys_to_triangles(polys, fillType, mode, verts);
Brian Osman80879d42019-01-07 16:15:27 -05002484 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002485
senorblancof57372d2016-08-31 10:36:19 -07002486 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002487 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002488 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002489 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002490 return actualCount;
2491}
2492
halcanary9d524f22016-03-29 09:03:52 -07002493int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton17dc4182020-03-25 16:18:16 -06002494 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002495 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002496 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002497 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002498 return 0;
2499 }
Stephen White11f65e02017-02-16 19:00:39 -05002500 SkArenaAlloc alloc(kArenaChunkSize);
Chris Dalton8e2b6942020-04-22 15:55:00 -06002501 int numCountedCurves;
Chris Daltondcc8c542020-01-28 17:55:56 -07002502 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, Mode::kNormal,
Chris Dalton8e2b6942020-04-22 15:55:00 -06002503 &numCountedCurves, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002504 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002505 int64_t count64 = count_points(polys, fillType);
2506 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002507 *verts = nullptr;
2508 return 0;
2509 }
Greg Danield5b45932018-06-07 13:15:10 -04002510 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002511
Chris Dalton17dc4182020-03-25 16:18:16 -06002512 *verts = new WindingVertex[count];
2513 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002514 SkPoint* points = new SkPoint[count];
2515 SkPoint* pointsEnd = points;
2516 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002517 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002518 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002519 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002520 while (start != pointsEnd) {
2521 vertsEnd->fPos = *start;
2522 vertsEnd->fWinding = poly->fWinding;
2523 ++start;
2524 ++vertsEnd;
2525 }
2526 }
2527 }
2528 int actualCount = static_cast<int>(vertsEnd - *verts);
2529 SkASSERT(actualCount <= count);
2530 SkASSERT(pointsEnd - points == actualCount);
2531 delete[] points;
2532 return actualCount;
2533}
2534
2535} // namespace