blob: 49eecaea7658a0715ad730651d48b8a243ec00f9 [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"
Chris Daltond60c9192021-01-07 18:30:08 +000013
14#include "src/core/SkGeometry.h"
15#include "src/core/SkPointPriv.h"
16
Stephen White94b7e542018-01-04 14:01:10 -050017#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040018#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040019#include <queue>
20#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040021#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080022
Chris Daltond60c9192021-01-07 18:30:08 +000023
24#define LOGGING_ENABLED 0
25
26#if LOGGING_ENABLED
27#define TESS_LOG printf
ethannicholase9709e82016-01-07 13:34:16 -080028#else
Brian Salomon120e7d62019-09-11 10:29:22 -040029#define TESS_LOG(...)
ethannicholase9709e82016-01-07 13:34:16 -080030#endif
31
Chris Dalton57ea1fc2021-01-05 13:37:44 -070032constexpr static float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
ethannicholase9709e82016-01-07 13:34:16 -080033
Stephen Whitee260c462017-12-19 18:09:54 -050034struct Event;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070035
Chris Dalton57ea1fc2021-01-05 13:37:44 -070036using Vertex = GrTriangulator::Vertex;
37using VertexList = GrTriangulator::VertexList;
38using Edge = GrTriangulator::Edge;
39using EdgeList = GrTriangulator::EdgeList;
40using Poly = GrTriangulator::Poly;
41using Comparator = GrTriangulator::Comparator;
ethannicholase9709e82016-01-07 13:34:16 -080042
43template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070044static void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080045 t->*Prev = prev;
46 t->*Next = next;
47 if (prev) {
48 prev->*Next = t;
49 } else if (head) {
50 *head = t;
51 }
52 if (next) {
53 next->*Prev = t;
54 } else if (tail) {
55 *tail = t;
56 }
57}
58
59template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070060static void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080061 if (t->*Prev) {
62 t->*Prev->*Next = t->*Next;
63 } else if (head) {
64 *head = t->*Next;
65 }
66 if (t->*Next) {
67 t->*Next->*Prev = t->*Prev;
68 } else if (tail) {
69 *tail = t->*Prev;
70 }
71 t->*Prev = t->*Next = nullptr;
72}
73
Chris Daltond60c9192021-01-07 18:30:08 +000074/**
75 * Vertices are used in three ways: first, the path contours are converted into a
76 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
77 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
78 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
79 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
80 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
81 * an individual Vertex from the path mesh may belong to multiple
82 * MonotonePolys, so the original Vertices cannot be re-used.
83 */
84
85struct GrTriangulator::Vertex {
86 Vertex(const SkPoint& point, uint8_t alpha)
87 : fPoint(point), fPrev(nullptr), fNext(nullptr)
88 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
89 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
90 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
91 , fPartner(nullptr)
92 , fAlpha(alpha)
93 , fSynthetic(false)
94#if LOGGING_ENABLED
95 , fID (-1.0f)
96#endif
97 {}
98 SkPoint fPoint; // Vertex position
99 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
100 Vertex* fNext; // "
101 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
102 Edge* fLastEdgeAbove; // "
103 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
104 Edge* fLastEdgeBelow; // "
105 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
106 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
107 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
108 uint8_t fAlpha;
109 bool fSynthetic; // Is this a synthetic vertex?
110#if LOGGING_ENABLED
111 float fID; // Identifier used for logging.
112#endif
113};
114
115/***************************************************************************************/
116
ethannicholase9709e82016-01-07 13:34:16 -0800117typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
118
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700119static bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500120 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800121}
122
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700123static bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500124 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800125}
126
Chris Daltond60c9192021-01-07 18:30:08 +0000127struct GrTriangulator::Comparator {
128 enum class Direction { kVertical, kHorizontal };
129 Comparator(Direction direction) : fDirection(direction) {}
130 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
131 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
132 }
133 Direction fDirection;
134};
Stephen White16a40cb2017-02-23 11:10:01 -0500135
Chris Daltond60c9192021-01-07 18:30:08 +0000136static inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500137 GrVertexWriter verts{data};
138 verts.write(v->fPoint);
139
Brian Osman80879d42019-01-07 16:15:27 -0500140 if (emitCoverage) {
141 verts.write(GrNormalizeByteToFloat(v->fAlpha));
142 }
Brian Osman0995fd52019-01-09 09:52:25 -0500143
Brian Osmanf9aabff2018-11-13 16:11:38 -0500144 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800145}
146
Chris Daltond60c9192021-01-07 18:30:08 +0000147static void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400148 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
149 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
150 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700151#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500152 data = emit_vertex(v0, emitCoverage, data);
153 data = emit_vertex(v1, emitCoverage, data);
154 data = emit_vertex(v1, emitCoverage, data);
155 data = emit_vertex(v2, emitCoverage, data);
156 data = emit_vertex(v2, emitCoverage, data);
157 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800158#else
Brian Osman0995fd52019-01-09 09:52:25 -0500159 data = emit_vertex(v0, emitCoverage, data);
160 data = emit_vertex(v1, emitCoverage, data);
161 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800162#endif
163 return data;
164}
165
Chris Daltond60c9192021-01-07 18:30:08 +0000166struct GrTriangulator::VertexList {
167 VertexList() : fHead(nullptr), fTail(nullptr) {}
168 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
169 Vertex* fHead;
170 Vertex* fTail;
171 void insert(Vertex* v, Vertex* prev, Vertex* next) {
172 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
173 }
174 void append(Vertex* v) {
175 insert(v, fTail, nullptr);
176 }
177 void append(const VertexList& list) {
178 if (!list.fHead) {
179 return;
180 }
181 if (fTail) {
182 fTail->fNext = list.fHead;
183 list.fHead->fPrev = fTail;
184 } else {
185 fHead = list.fHead;
186 }
187 fTail = list.fTail;
188 }
189 void prepend(Vertex* v) {
190 insert(v, nullptr, fHead);
191 }
192 void remove(Vertex* v) {
193 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
194 }
195 void close() {
196 if (fHead && fTail) {
197 fTail->fNext = fHead;
198 fHead->fPrev = fTail;
199 }
200 }
201};
senorblancoe6eaa322016-03-08 09:06:44 -0800202
senorblancof57372d2016-08-31 10:36:19 -0700203// Round to nearest quarter-pixel. This is used for screenspace tessellation.
204
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700205static inline void round(SkPoint* p) {
senorblancof57372d2016-08-31 10:36:19 -0700206 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
207 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
208}
209
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700210static inline SkScalar double_to_clamped_scalar(double d) {
Stephen White94b7e542018-01-04 14:01:10 -0500211 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
212}
213
Chris Daltond60c9192021-01-07 18:30:08 +0000214// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
215struct Line {
216 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
217 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
218 Line(const SkPoint& p, const SkPoint& q)
219 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
220 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
221 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
222 static_cast<double>(p.fX) * q.fY) {}
223 double dist(const SkPoint& p) const {
224 return fA * p.fX + fB * p.fY + fC;
senorblanco49df8d12016-10-07 08:36:56 -0700225 }
Chris Daltond60c9192021-01-07 18:30:08 +0000226 Line operator*(double v) const {
227 return Line(fA * v, fB * v, fC * v);
senorblanco49df8d12016-10-07 08:36:56 -0700228 }
Chris Daltond60c9192021-01-07 18:30:08 +0000229 double magSq() const {
230 return fA * fA + fB * fB;
ethannicholase9709e82016-01-07 13:34:16 -0800231 }
Chris Daltond60c9192021-01-07 18:30:08 +0000232 void normalize() {
233 double len = sqrt(this->magSq());
234 if (len == 0.0) {
235 return;
Chris Dalton75887a22021-01-06 00:23:13 -0700236 }
Chris Daltond60c9192021-01-07 18:30:08 +0000237 double scale = 1.0f / len;
238 fA *= scale;
239 fB *= scale;
240 fC *= scale;
ethannicholase9709e82016-01-07 13:34:16 -0800241 }
Chris Daltond60c9192021-01-07 18:30:08 +0000242 bool nearParallel(const Line& o) const {
243 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
244 }
245
246 // Compute the intersection of two (infinite) Lines.
247 bool intersect(const Line& other, SkPoint* point) const {
248 double denom = fA * other.fB - fB * other.fA;
249 if (denom == 0.0) {
250 return false;
251 }
252 double scale = 1.0 / denom;
253 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
254 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
255 round(point);
256 return point->isFinite();
257 }
258 double fA, fB, fC;
259};
260
261/**
262 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
263 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
264 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
265 * point). For speed, that case is only tested by the callers that require it (e.g.,
266 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
267 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
268 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
269 * a lot faster in the "not found" case.
270 *
271 * The coefficients of the line equation stored in double precision to avoid catastrphic
272 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
273 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
274 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
275 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
276 * this file).
277 */
278
279struct GrTriangulator::Edge {
280 enum class Type { kInner, kOuter, kConnector };
281 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
282 : fWinding(winding)
283 , fTop(top)
284 , fBottom(bottom)
285 , fType(type)
286 , fLeft(nullptr)
287 , fRight(nullptr)
288 , fPrevEdgeAbove(nullptr)
289 , fNextEdgeAbove(nullptr)
290 , fPrevEdgeBelow(nullptr)
291 , fNextEdgeBelow(nullptr)
292 , fLeftPoly(nullptr)
293 , fRightPoly(nullptr)
294 , fLeftPolyPrev(nullptr)
295 , fLeftPolyNext(nullptr)
296 , fRightPolyPrev(nullptr)
297 , fRightPolyNext(nullptr)
298 , fUsedInLeftPoly(false)
299 , fUsedInRightPoly(false)
300 , fLine(top, bottom) {
301 }
302 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
303 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
304 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
305 Type fType;
306 Edge* fLeft; // The linked list of edges in the active edge list.
307 Edge* fRight; // "
308 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
309 Edge* fNextEdgeAbove; // "
310 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
311 Edge* fNextEdgeBelow; // "
312 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
313 Poly* fRightPoly; // The Poly to the right of this edge, if any.
314 Edge* fLeftPolyPrev;
315 Edge* fLeftPolyNext;
316 Edge* fRightPolyPrev;
317 Edge* fRightPolyNext;
318 bool fUsedInLeftPoly;
319 bool fUsedInRightPoly;
320 Line fLine;
321 double dist(const SkPoint& p) const {
322 return fLine.dist(p);
323 }
324 bool isRightOf(Vertex* v) const {
325 return fLine.dist(v->fPoint) < 0.0;
326 }
327 bool isLeftOf(Vertex* v) const {
328 return fLine.dist(v->fPoint) > 0.0;
329 }
330 void recompute() {
331 fLine = Line(fTop, fBottom);
332 }
333 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
334 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
335 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
336 if (fTop == other.fTop || fBottom == other.fBottom) {
337 return false;
338 }
339 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
340 if (denom == 0.0) {
341 return false;
342 }
343 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
344 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
345 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
346 double tNumer = dy * fLine.fB + dx * fLine.fA;
347 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
348 // This saves us doing the divide below unless absolutely necessary.
349 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
350 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
351 return false;
352 }
353 double s = sNumer / denom;
354 SkASSERT(s >= 0.0 && s <= 1.0);
355 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
356 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
357 if (alpha) {
358 if (fType == Type::kConnector) {
359 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
360 } else if (other.fType == Type::kConnector) {
361 double t = tNumer / denom;
362 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
363 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
364 *alpha = 0;
365 } else {
366 *alpha = 255;
367 }
368 }
369 return true;
370 }
371};
senorblancof57372d2016-08-31 10:36:19 -0700372
Stephen Whitec4dbc372019-05-22 10:50:14 -0400373struct SSEdge;
374
375struct SSVertex {
376 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
377 Vertex* fVertex;
378 SSEdge* fPrev;
379 SSEdge* fNext;
380};
381
382struct SSEdge {
383 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
384 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
385 }
386 Edge* fEdge;
387 Event* fEvent;
388 SSVertex* fPrev;
389 SSVertex* fNext;
390};
391
392typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
393typedef std::vector<SSEdge*> SSEdgeList;
394
Chris Daltond60c9192021-01-07 18:30:08 +0000395struct GrTriangulator::EdgeList {
396 EdgeList() : fHead(nullptr), fTail(nullptr) {}
397 Edge* fHead;
398 Edge* fTail;
399 void insert(Edge* edge, Edge* prev, Edge* next) {
400 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
401 }
402 void append(Edge* e) {
403 insert(e, fTail, nullptr);
404 }
405 void remove(Edge* edge) {
406 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
407 }
408 void removeAll() {
409 while (fHead) {
410 this->remove(fHead);
411 }
412 }
413 void close() {
414 if (fHead && fTail) {
415 fTail->fRight = fHead;
416 fHead->fLeft = fTail;
417 }
418 }
419 bool contains(Edge* edge) const {
420 return edge->fLeft || edge->fRight || fHead == edge;
421 }
422};
ethannicholase9709e82016-01-07 13:34:16 -0800423
Stephen Whitec4dbc372019-05-22 10:50:14 -0400424struct EventList;
425
Stephen Whitee260c462017-12-19 18:09:54 -0500426struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400427 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
428 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500429 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400430 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500431 SkPoint fPoint;
432 uint8_t fAlpha;
Chris Dalton811dc6a2021-01-07 16:40:32 -0700433 void apply(VertexList* mesh, const Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500434};
435
Stephen Whitec4dbc372019-05-22 10:50:14 -0400436struct EventComparator {
437 enum class Op { kLessThan, kGreaterThan };
438 EventComparator(Op op) : fOp(op) {}
439 bool operator() (Event* const &e1, Event* const &e2) {
440 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
441 : e1->fAlpha > e2->fAlpha;
442 }
443 Op fOp;
444};
Stephen Whitee260c462017-12-19 18:09:54 -0500445
Stephen Whitec4dbc372019-05-22 10:50:14 -0400446typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500447
Stephen Whitec4dbc372019-05-22 10:50:14 -0400448struct EventList : EventPQ {
449 EventList(EventComparator comparison) : EventPQ(comparison) {
450 }
451};
452
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700453static void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400454 Vertex* prev = e->fPrev->fVertex;
455 Vertex* next = e->fNext->fVertex;
456 if (prev == next || !prev->fPartner || !next->fPartner) {
457 return;
458 }
Chris Daltond60c9192021-01-07 18:30:08 +0000459 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
460 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500461 SkPoint p;
462 uint8_t alpha;
463 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400464 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
465 "will collapse to %g,%g alpha %d\n",
466 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
467 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400468 e->fEvent = alloc.make<Event>(e, p, alpha);
469 events->push(e->fEvent);
470 }
471}
472
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700473static void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700474 const Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400475 if (!v->fPartner) {
476 return;
477 }
Stephen White8a3c0592019-05-29 11:26:16 -0400478 Vertex* top = edge->fEdge->fTop;
479 Vertex* bottom = edge->fEdge->fBottom;
480 if (!top || !bottom ) {
481 return;
482 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400483 Line line = edge->fEdge->fLine;
484 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
Chris Daltond60c9192021-01-07 18:30:08 +0000485 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400486 SkPoint p;
487 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400488 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
489 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400490 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
491 "will collapse to %g,%g alpha %d\n",
492 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400493 edge->fEvent = alloc.make<Event>(edge, p, alpha);
494 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500495 }
496}
Stephen Whitee260c462017-12-19 18:09:54 -0500497
ethannicholase9709e82016-01-07 13:34:16 -0800498/***************************************************************************************/
499
Chris Daltond60c9192021-01-07 18:30:08 +0000500struct GrTriangulator::Poly {
501 Poly(Vertex* v, int winding)
502 : fFirstVertex(v)
503 , fWinding(winding)
504 , fHead(nullptr)
505 , fTail(nullptr)
506 , fNext(nullptr)
507 , fPartner(nullptr)
508 , fCount(0)
509 {
510#if LOGGING_ENABLED
511 static int gID = 0;
512 fID = gID++;
513 TESS_LOG("*** created Poly %d\n", fID);
514#endif
ethannicholase9709e82016-01-07 13:34:16 -0800515 }
Chris Daltond60c9192021-01-07 18:30:08 +0000516 typedef enum { kLeft_Side, kRight_Side } Side;
517 struct MonotonePoly {
518 MonotonePoly(Edge* edge, Side side, int winding)
519 : fSide(side)
520 , fFirstEdge(nullptr)
521 , fLastEdge(nullptr)
522 , fPrev(nullptr)
523 , fNext(nullptr)
524 , fWinding(winding) {
525 this->addEdge(edge);
senorblanco212c7c32016-08-18 10:20:47 -0700526 }
Chris Daltond60c9192021-01-07 18:30:08 +0000527 Side fSide;
528 Edge* fFirstEdge;
529 Edge* fLastEdge;
530 MonotonePoly* fPrev;
531 MonotonePoly* fNext;
532 int fWinding;
533 void addEdge(Edge* edge) {
534 if (fSide == kRight_Side) {
535 SkASSERT(!edge->fUsedInRightPoly);
536 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
537 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
538 edge->fUsedInRightPoly = true;
Chris Dalton75887a22021-01-06 00:23:13 -0700539 } else {
Chris Daltond60c9192021-01-07 18:30:08 +0000540 SkASSERT(!edge->fUsedInLeftPoly);
541 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
542 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
543 edge->fUsedInLeftPoly = true;
544 }
545 }
546
547 void* emit(bool emitCoverage, void* data) {
548 Edge* e = fFirstEdge;
549 VertexList vertices;
550 vertices.append(e->fTop);
551 int count = 1;
552 while (e != nullptr) {
553 if (kRight_Side == fSide) {
554 vertices.append(e->fBottom);
555 e = e->fRightPolyNext;
556 } else {
557 vertices.prepend(e->fBottom);
558 e = e->fLeftPolyNext;
559 }
560 count++;
561 }
562 Vertex* first = vertices.fHead;
563 Vertex* v = first->fNext;
564 while (v != vertices.fTail) {
565 SkASSERT(v && v->fPrev && v->fNext);
566 Vertex* prev = v->fPrev;
567 Vertex* curr = v;
568 Vertex* next = v->fNext;
569 if (count == 3) {
570 return this->emitTriangle(prev, curr, next, emitCoverage, data);
571 }
572 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
573 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
574 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
575 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
576 if (ax * by - ay * bx >= 0.0) {
577 data = this->emitTriangle(prev, curr, next, emitCoverage, data);
578 v->fPrev->fNext = v->fNext;
579 v->fNext->fPrev = v->fPrev;
580 count--;
581 if (v->fPrev == first) {
582 v = v->fNext;
583 } else {
584 v = v->fPrev;
585 }
586 } else {
587 v = v->fNext;
588 }
589 }
590 return data;
591 }
592 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, bool emitCoverage,
593 void* data) const {
594 if (fWinding < 0) {
595 // Ensure our triangles always wind in the same direction as if the path had been
596 // triangulated as a simple fan (a la red book).
597 std::swap(prev, next);
598 }
599 return emit_triangle(next, curr, prev, emitCoverage, data);
600 }
601 };
602 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
603 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
604 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
605 Poly* partner = fPartner;
606 Poly* poly = this;
607 if (side == kRight_Side) {
608 if (e->fUsedInRightPoly) {
609 return this;
Chris Dalton75887a22021-01-06 00:23:13 -0700610 }
611 } else {
Chris Daltond60c9192021-01-07 18:30:08 +0000612 if (e->fUsedInLeftPoly) {
613 return this;
614 }
ethannicholase9709e82016-01-07 13:34:16 -0800615 }
Chris Dalton75887a22021-01-06 00:23:13 -0700616 if (partner) {
Chris Daltond60c9192021-01-07 18:30:08 +0000617 fPartner = partner->fPartner = nullptr;
Chris Dalton75887a22021-01-06 00:23:13 -0700618 }
Chris Daltond60c9192021-01-07 18:30:08 +0000619 if (!fTail) {
620 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
621 fCount += 2;
622 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
623 return poly;
624 } else if (side == fTail->fSide) {
625 fTail->addEdge(e);
626 fCount++;
627 } else {
628 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
629 fTail->addEdge(e);
630 fCount++;
631 if (partner) {
632 partner->addEdge(e, side, alloc);
633 poly = partner;
634 } else {
635 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
636 m->fPrev = fTail;
637 fTail->fNext = m;
638 fTail = m;
639 }
640 }
641 return poly;
Chris Dalton75887a22021-01-06 00:23:13 -0700642 }
Chris Daltond60c9192021-01-07 18:30:08 +0000643 void* emit(bool emitCoverage, void *data) {
644 if (fCount < 3) {
645 return data;
646 }
647 TESS_LOG("emit() %d, size %d\n", fID, fCount);
648 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
649 data = m->emit(emitCoverage, data);
650 }
ethannicholase9709e82016-01-07 13:34:16 -0800651 return data;
652 }
Chris Daltond60c9192021-01-07 18:30:08 +0000653 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
654 Vertex* fFirstVertex;
655 int fWinding;
656 MonotonePoly* fHead;
657 MonotonePoly* fTail;
658 Poly* fNext;
659 Poly* fPartner;
660 int fCount;
661#if LOGGING_ENABLED
662 int fID;
663#endif
664};
ethannicholase9709e82016-01-07 13:34:16 -0800665
Chris Daltond60c9192021-01-07 18:30:08 +0000666/***************************************************************************************/
ethannicholase9709e82016-01-07 13:34:16 -0800667
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700668static bool coincident(const SkPoint& a, const SkPoint& b) {
ethannicholase9709e82016-01-07 13:34:16 -0800669 return a == b;
670}
671
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700672static Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500673 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800674 poly->fNext = *head;
675 *head = poly;
676 return poly;
677}
678
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700679void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) {
680 Vertex* v = fAlloc.make<Vertex>(p, 255);
Chris Daltond60c9192021-01-07 18:30:08 +0000681#if LOGGING_ENABLED
ethannicholase9709e82016-01-07 13:34:16 -0800682 static float gID = 0.0f;
683 v->fID = gID++;
684#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500685 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800686}
687
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700688static SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
Stephen White36e4f062017-03-27 16:11:31 -0400689 SkQuadCoeff quad(pts);
690 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
691 SkPoint mid = to_point(quad.eval(t));
692 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400693 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
694 return 0;
695 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500696 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400697}
698
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700699void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd,
700 VertexList* contour) {
Stephen White36e4f062017-03-27 16:11:31 -0400701 SkQuadCoeff quad(pts);
702 Sk2s aa = quad.fA * quad.fA;
703 SkScalar denom = 2.0f * (aa[0] + aa[1]);
704 Sk2s ab = quad.fA * quad.fB;
705 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
706 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500707 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400708 // Test possible subdivision values only at the point of maximum curvature.
709 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500710 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400711 u = 1.0f / nPoints;
712 if (quad_error_at(pts, t, u) < toleranceSqd) {
713 break;
714 }
715 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800716 }
Stephen White36e4f062017-03-27 16:11:31 -0400717 for (int j = 1; j <= nPoints; j++) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700718 this->appendPointToContour(to_point(quad.eval(j * u)), contour);
Stephen White36e4f062017-03-27 16:11:31 -0400719 }
ethannicholase9709e82016-01-07 13:34:16 -0800720}
721
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700722void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2,
723 const SkPoint& p3, SkScalar tolSqd, VertexList* contour,
724 int pointsLeft) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500725 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
726 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800727 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
728 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700729 this->appendPointToContour(p3, contour);
Stephen White3a9aab92017-03-07 14:07:18 -0500730 return;
ethannicholase9709e82016-01-07 13:34:16 -0800731 }
732 const SkPoint q[] = {
733 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
734 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
735 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
736 };
737 const SkPoint r[] = {
738 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
739 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
740 };
741 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
742 pointsLeft >>= 1;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700743 this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft);
744 this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800745}
746
747// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
748
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700749void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds,
750 VertexList* contours) {
ethannicholase9709e82016-01-07 13:34:16 -0800751 SkScalar toleranceSqd = tolerance * tolerance;
Chris Dalton7156db22020-05-07 22:06:28 +0000752 SkPoint pts[4];
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700753 fIsLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500754 VertexList* contour = contours;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700755 SkPath::Iter iter(fPath, false);
756 if (fPath.isInverseFillType()) {
ethannicholase9709e82016-01-07 13:34:16 -0800757 SkPoint quad[4];
758 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700759 for (int i = 3; i >= 0; i--) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700760 this->appendPointToContour(quad[i], contours);
ethannicholase9709e82016-01-07 13:34:16 -0800761 }
Stephen White3a9aab92017-03-07 14:07:18 -0500762 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800763 }
764 SkAutoConicToQuads converter;
Chris Dalton7156db22020-05-07 22:06:28 +0000765 SkPath::Verb verb;
766 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800767 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +0000768 case SkPath::kConic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700769 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700770 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700771 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700772 break;
773 }
Chris Dalton7156db22020-05-07 22:06:28 +0000774 SkScalar weight = iter.conicWeight();
775 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
ethannicholase9709e82016-01-07 13:34:16 -0800776 for (int i = 0; i < converter.countQuads(); ++i) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700777 this->appendQuadraticToContour(quadPts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800778 quadPts += 2;
779 }
ethannicholase9709e82016-01-07 13:34:16 -0800780 break;
781 }
Chris Dalton7156db22020-05-07 22:06:28 +0000782 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500783 if (contour->fHead) {
784 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800785 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700786 this->appendPointToContour(pts[0], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800787 break;
Chris Dalton7156db22020-05-07 22:06:28 +0000788 case SkPath::kLine_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700789 this->appendPointToContour(pts[1], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800790 break;
791 }
Chris Dalton7156db22020-05-07 22:06:28 +0000792 case SkPath::kQuad_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700793 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700794 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700795 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700796 break;
797 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700798 this->appendQuadraticToContour(pts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800799 break;
800 }
Chris Dalton7156db22020-05-07 22:06:28 +0000801 case SkPath::kCubic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700802 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700803 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700804 this->appendPointToContour(pts[3], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700805 break;
806 }
ethannicholase9709e82016-01-07 13:34:16 -0800807 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700808 this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
809 pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800810 break;
811 }
Chris Dalton7156db22020-05-07 22:06:28 +0000812 case SkPath::kClose_Verb:
813 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800814 break;
815 }
816 }
817}
818
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700819static inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800820 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500821 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800822 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500823 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800824 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500825 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700826 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500827 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800828 return (winding & 1) == 1;
829 default:
830 SkASSERT(false);
831 return false;
832 }
833}
834
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700835static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500836 return poly && apply_fill_type(fillType, poly->fWinding);
837}
838
Chris Dalton811dc6a2021-01-07 16:40:32 -0700839static Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, const Comparator& c,
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700840 SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500841 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800842 Vertex* top = winding < 0 ? next : prev;
843 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500844 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800845}
846
Chris Daltond60c9192021-01-07 18:30:08 +0000847static void remove_edge(Edge* edge, EdgeList* edges) {
848 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
849 SkASSERT(edges->contains(edge));
850 edges->remove(edge);
851}
852
853static void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400854 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +0000855 SkASSERT(!edges->contains(edge));
856 Edge* next = prev ? prev->fRight : edges->fHead;
857 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800858}
859
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700860static void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500861 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800862 *left = v->fFirstEdgeAbove->fLeft;
863 *right = v->fLastEdgeAbove->fRight;
864 return;
865 }
866 Edge* next = nullptr;
867 Edge* prev;
868 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
869 if (prev->isLeftOf(v)) {
870 break;
871 }
872 next = prev;
873 }
874 *left = prev;
875 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800876}
877
Chris Dalton811dc6a2021-01-07 16:40:32 -0700878static void insert_edge_above(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800879 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500880 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800881 return;
882 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400883 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
884 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800885 Edge* prev = nullptr;
886 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000887 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800888 if (next->isRightOf(edge->fTop)) {
889 break;
890 }
891 prev = next;
892 }
senorblancoe6eaa322016-03-08 09:06:44 -0800893 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
Chris Daltond60c9192021-01-07 18:30:08 +0000894 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
ethannicholase9709e82016-01-07 13:34:16 -0800895}
896
Chris Dalton811dc6a2021-01-07 16:40:32 -0700897static void insert_edge_below(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800898 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500899 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800900 return;
901 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400902 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
903 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800904 Edge* prev = nullptr;
905 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000906 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
ethannicholase9709e82016-01-07 13:34:16 -0800907 if (next->isRightOf(edge->fBottom)) {
908 break;
909 }
910 prev = next;
911 }
senorblancoe6eaa322016-03-08 09:06:44 -0800912 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
Chris Daltond60c9192021-01-07 18:30:08 +0000913 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
ethannicholase9709e82016-01-07 13:34:16 -0800914}
915
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700916static void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400917 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400918 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
919 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800920 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800921 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
922}
923
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700924static void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400925 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400926 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
927 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800928 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800929 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
930}
931
Chris Daltond60c9192021-01-07 18:30:08 +0000932static void disconnect(Edge* edge)
933{
934 remove_edge_above(edge);
935 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500936}
937
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700938static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700939 const Comparator& c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400940
Chris Dalton811dc6a2021-01-07 16:40:32 -0700941static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, const Comparator& c) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400942 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
943 return;
944 }
945 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400946 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400947 while (v != dst) {
948 v = v->fPrev;
949 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +0000950 remove_edge(e, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400951 }
952 Edge* leftEdge = v->fLeftEnclosingEdge;
953 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +0000954 insert_edge(e, leftEdge, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400955 leftEdge = e;
Stephen Whitec03e6982020-02-06 16:32:14 -0500956 Vertex* top = e->fTop;
957 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
958 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
959 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
960 dst = top;
961 }
Stephen White3b5a3fa2017-06-06 14:51:19 -0400962 }
963 }
964 *current = v;
965}
966
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700967static void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700968 const Comparator& c) {
Stephen Whitec03e6982020-02-06 16:32:14 -0500969 if (!activeEdges || !current) {
970 return;
971 }
972 Vertex* top = edge->fTop;
973 Vertex* bottom = edge->fBottom;
974 if (edge->fLeft) {
975 Vertex* leftTop = edge->fLeft->fTop;
976 Vertex* leftBottom = edge->fLeft->fBottom;
977 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
978 rewind(activeEdges, current, leftTop, c);
979 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
980 rewind(activeEdges, current, top, c);
981 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
982 !edge->fLeft->isLeftOf(bottom)) {
983 rewind(activeEdges, current, leftTop, c);
984 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
985 rewind(activeEdges, current, top, c);
986 }
987 }
988 if (edge->fRight) {
989 Vertex* rightTop = edge->fRight->fTop;
990 Vertex* rightBottom = edge->fRight->fBottom;
991 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
992 rewind(activeEdges, current, rightTop, c);
993 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
994 rewind(activeEdges, current, top, c);
995 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
996 !edge->fRight->isRightOf(bottom)) {
997 rewind(activeEdges, current, rightTop, c);
998 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
999 !edge->isLeftOf(rightBottom)) {
1000 rewind(activeEdges, current, top, c);
1001 }
1002 }
1003}
1004
Chris Dalton811dc6a2021-01-07 16:40:32 -07001005static void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
1006 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001007 remove_edge_below(edge);
1008 edge->fTop = v;
1009 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +00001010 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001011 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001012 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001013}
1014
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001015static void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001016 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001017 remove_edge_above(edge);
1018 edge->fBottom = v;
1019 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +00001020 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001021 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001022 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001023}
1024
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001025static void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001026 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001027 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001028 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1029 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1030 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001031 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001032 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +00001033 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001034 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001035 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001036 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001037 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001038 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001039 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001040 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001041 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001042 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001043 }
1044}
1045
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001046static void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001047 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001048 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001049 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1050 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1051 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001052 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001053 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +00001054 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001055 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001056 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001057 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001058 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001059 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001060 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001061 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001062 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001063 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001064 }
1065}
1066
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001067static bool top_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001068 if (!left || !right) {
1069 return false;
1070 }
1071 return left->fTop->fPoint == right->fTop->fPoint ||
1072 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1073}
1074
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001075static bool bottom_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001076 if (!left || !right) {
1077 return false;
1078 }
1079 return left->fBottom->fPoint == right->fBottom->fPoint ||
1080 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1081}
1082
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001083static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001084 const Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001085 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001086 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001087 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001088 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001089 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001090 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001091 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001092 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001093 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001094 } else {
1095 break;
1096 }
ethannicholase9709e82016-01-07 13:34:16 -08001097 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001098 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1099 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1100 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1101 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001102}
1103
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001104bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001105 const Comparator& c) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001106 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001107 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001108 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001109 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1110 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001111 Vertex* top;
1112 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001113 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001114 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001115 top = v;
1116 bottom = edge->fTop;
1117 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001118 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001119 top = edge->fBottom;
1120 bottom = v;
1121 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001122 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001123 top = v;
1124 bottom = edge->fBottom;
1125 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001126 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001127 Edge* newEdge = fAlloc.make<Edge>(top, bottom, winding, edge->fType);
Chris Daltond60c9192021-01-07 18:30:08 +00001128 insert_edge_below(newEdge, top, c);
1129 insert_edge_above(newEdge, bottom, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001130 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001131 return true;
1132}
1133
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001134bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001135 Vertex** current, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001136 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1137 return false;
1138 }
Stephen White1c5fd182018-07-12 15:54:05 -04001139 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1140 return false;
1141 }
Stephen White89042d52018-06-08 12:18:22 -04001142 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1143 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001144 rewind(activeEdges, current, right->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001145 return this->splitEdge(left, right->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001146 }
1147 } else {
1148 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001149 rewind(activeEdges, current, left->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001150 return this->splitEdge(right, left->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001151 }
1152 }
1153 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1154 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001155 rewind(activeEdges, current, right->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001156 return this->splitEdge(left, right->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001157 }
1158 } else {
1159 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001160 rewind(activeEdges, current, left->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001161 return this->splitEdge(right, left->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001162 }
1163 }
1164 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001165}
1166
Chris Dalton811dc6a2021-01-07 16:40:32 -07001167static Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, const Comparator& c,
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001168 SkArenaAlloc& alloc, int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001169 if (!prev || !next || prev->fPoint == next->fPoint) {
1170 return nullptr;
1171 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001172 Edge* edge = new_edge(prev, next, type, c, alloc);
Chris Daltond60c9192021-01-07 18:30:08 +00001173 insert_edge_below(edge, edge->fTop, c);
1174 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001175 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001176 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001177 return edge;
1178}
1179
Chris Dalton811dc6a2021-01-07 16:40:32 -07001180static void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001181 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1182 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -05001183 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001184 if (src->fPartner) {
1185 src->fPartner->fPartner = dst;
1186 }
Stephen White7b376942018-05-22 11:51:32 -04001187 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001188 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001189 }
Stephen White7b376942018-05-22 11:51:32 -04001190 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001191 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001192 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001193 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001194 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001195}
1196
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001197static Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001198 Vertex* reference, const Comparator& c, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001199 Vertex* prevV = reference;
1200 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1201 prevV = prevV->fPrev;
1202 }
1203 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1204 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1205 prevV = nextV;
1206 nextV = nextV->fNext;
1207 }
1208 Vertex* v;
1209 if (prevV && coincident(prevV->fPoint, p)) {
1210 v = prevV;
1211 } else if (nextV && coincident(nextV->fPoint, p)) {
1212 v = nextV;
1213 } else {
1214 v = alloc.make<Vertex>(p, alpha);
Chris Daltond60c9192021-01-07 18:30:08 +00001215#if LOGGING_ENABLED
Stephen White95152e12017-12-18 10:52:44 -05001216 if (!prevV) {
1217 v->fID = mesh->fHead->fID - 1.0f;
1218 } else if (!nextV) {
1219 v->fID = mesh->fTail->fID + 1.0f;
1220 } else {
1221 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1222 }
1223#endif
1224 mesh->insert(v, prevV, nextV);
1225 }
1226 return v;
1227}
1228
Stephen White53a02982018-05-30 22:47:46 -04001229// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1230// sort criterion, it may not be possible to split correctly, since there is no point which is
1231// below the top and above the bottom. This function detects that case.
Chris Dalton811dc6a2021-01-07 16:40:32 -07001232static bool nearly_flat(const Comparator& c, Edge* edge) {
Stephen White53a02982018-05-30 22:47:46 -04001233 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1234 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001235 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001236}
1237
Chris Dalton811dc6a2021-01-07 16:40:32 -07001238static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, const Comparator& c) {
Stephen Whitee62999f2018-06-05 18:45:07 -04001239 if (c.sweep_lt(p, min)) {
1240 return min;
1241 } else if (c.sweep_lt(max, p)) {
1242 return max;
1243 } else {
1244 return p;
1245 }
1246}
1247
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001248static void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001249 Line line1 = edge1->fLine;
1250 Line line2 = edge2->fLine;
1251 line1.normalize();
1252 line2.normalize();
1253 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1254 if (cosAngle > 0.999) {
1255 return;
1256 }
1257 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1258 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1259 SkPoint p;
1260 if (line1.intersect(line2, &p)) {
Chris Daltond60c9192021-01-07 18:30:08 +00001261 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001262 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001263 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 -04001264 }
1265}
1266
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001267bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001268 Vertex** current, VertexList* mesh, const Comparator& c) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001269 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001270 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001271 }
Stephen White56158ae2017-01-30 14:31:31 -05001272 SkPoint p;
1273 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001274 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001275 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001276 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001277 Vertex* top = *current;
1278 // If the intersection point is above the current vertex, rewind to the vertex above the
1279 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001280 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001281 top = top->fPrev;
1282 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001283 if (!nearly_flat(c, left)) {
1284 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001285 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001286 if (!nearly_flat(c, right)) {
1287 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001288 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001289 if (p == left->fTop->fPoint) {
1290 v = left->fTop;
1291 } else if (p == left->fBottom->fPoint) {
1292 v = left->fBottom;
1293 } else if (p == right->fTop->fPoint) {
1294 v = right->fTop;
1295 } else if (p == right->fBottom->fPoint) {
1296 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001297 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001298 v = create_sorted_vertex(p, alpha, mesh, top, c, fAlloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001299 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001300 v->fSynthetic = true;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001301 compute_bisector(left, right, v, fAlloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001302 }
ethannicholase9709e82016-01-07 13:34:16 -08001303 }
Stephen White0cb31672017-06-08 14:41:01 -04001304 rewind(activeEdges, current, top ? top : v, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001305 this->splitEdge(left, v, activeEdges, current, c);
1306 this->splitEdge(right, v, activeEdges, current, c);
Brian Osman788b9162020-02-07 10:36:46 -05001307 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001308 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001309 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001310 return this->intersectEdgePair(left, right, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001311}
1312
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001313void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) {
Stephen White3a9aab92017-03-07 14:07:18 -05001314 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1315 SkASSERT(contour->fHead);
1316 Vertex* prev = contour->fTail;
Chris Dalton854ee852021-01-05 15:12:59 -07001317 if (fRoundVerticesToQuarterPixel) {
Stephen White3a9aab92017-03-07 14:07:18 -05001318 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001319 }
Stephen White3a9aab92017-03-07 14:07:18 -05001320 for (Vertex* v = contour->fHead; v;) {
Chris Dalton854ee852021-01-05 15:12:59 -07001321 if (fRoundVerticesToQuarterPixel) {
senorblancof57372d2016-08-31 10:36:19 -07001322 round(&v->fPoint);
1323 }
Stephen White3a9aab92017-03-07 14:07:18 -05001324 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001325 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001326 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001327 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001328 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001329 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001330 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001331 contour->remove(v);
Chris Dalton854ee852021-01-05 15:12:59 -07001332 } else if (fCullCollinearVertices &&
Chris Dalton6ccc0322020-01-29 11:38:16 -07001333 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001334 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001335 contour->remove(v);
1336 } else {
1337 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001338 }
Stephen White3a9aab92017-03-07 14:07:18 -05001339 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001340 }
1341 }
1342}
1343
Chris Dalton811dc6a2021-01-07 16:40:32 -07001344bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001345 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001346 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001347 }
Stephen Whitee260c462017-12-19 18:09:54 -05001348 bool merged = false;
1349 for (Vertex* v = mesh->fHead->fNext; v;) {
1350 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001351 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1352 v->fPoint = v->fPrev->fPoint;
1353 }
1354 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001355 merge_vertices(v, v->fPrev, mesh, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001356 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001357 }
Stephen Whitee260c462017-12-19 18:09:54 -05001358 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001359 }
Stephen Whitee260c462017-12-19 18:09:54 -05001360 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001361}
1362
1363// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1364
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001365void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001366 const Comparator& c) {
Stephen White3a9aab92017-03-07 14:07:18 -05001367 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1368 Vertex* prev = contour->fTail;
1369 for (Vertex* v = contour->fHead; v;) {
1370 Vertex* next = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +00001371 connect(prev, v, Edge::Type::kInner, c, fAlloc);
Stephen White3a9aab92017-03-07 14:07:18 -05001372 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001373 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001374 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001375 }
1376 }
ethannicholase9709e82016-01-07 13:34:16 -08001377}
1378
Chris Dalton811dc6a2021-01-07 16:40:32 -07001379static void connect_partners(VertexList* mesh, const Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitee260c462017-12-19 18:09:54 -05001380 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001381 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001382 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1383 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1384 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1385 // number.
Chris Daltond60c9192021-01-07 18:30:08 +00001386 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001387 inner->fPartner = outer->fPartner = nullptr;
1388 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001389 }
1390 }
1391}
1392
1393template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001394static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001395 Vertex* a = front->fHead;
1396 Vertex* b = back->fHead;
1397 while (a && b) {
1398 if (sweep_lt(a->fPoint, b->fPoint)) {
1399 front->remove(a);
1400 result->append(a);
1401 a = front->fHead;
1402 } else {
1403 back->remove(b);
1404 result->append(b);
1405 b = back->fHead;
1406 }
1407 }
1408 result->append(*front);
1409 result->append(*back);
1410}
1411
Chris Dalton811dc6a2021-01-07 16:40:32 -07001412static void sorted_merge(VertexList* front, VertexList* back, VertexList* result,
1413 const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001414 if (c.fDirection == Comparator::Direction::kHorizontal) {
1415 sorted_merge<sweep_lt_horiz>(front, back, result);
1416 } else {
1417 sorted_merge<sweep_lt_vert>(front, back, result);
1418 }
Chris Daltond60c9192021-01-07 18:30:08 +00001419#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -04001420 float id = 0.0f;
1421 for (Vertex* v = result->fHead; v; v = v->fNext) {
1422 v->fID = id++;
1423 }
1424#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001425}
1426
ethannicholase9709e82016-01-07 13:34:16 -08001427// Stage 3: sort the vertices by increasing sweep direction.
1428
Stephen White16a40cb2017-02-23 11:10:01 -05001429template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001430static void merge_sort(VertexList* vertices) {
Stephen White16a40cb2017-02-23 11:10:01 -05001431 Vertex* slow = vertices->fHead;
1432 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001433 return;
1434 }
Stephen White16a40cb2017-02-23 11:10:01 -05001435 Vertex* fast = slow->fNext;
1436 if (!fast) {
1437 return;
1438 }
1439 do {
1440 fast = fast->fNext;
1441 if (fast) {
1442 fast = fast->fNext;
1443 slow = slow->fNext;
1444 }
1445 } while (fast);
1446 VertexList front(vertices->fHead, slow);
1447 VertexList back(slow->fNext, vertices->fTail);
1448 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001449
Stephen White16a40cb2017-02-23 11:10:01 -05001450 merge_sort<sweep_lt>(&front);
1451 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001452
Stephen White16a40cb2017-02-23 11:10:01 -05001453 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001454 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001455}
1456
Chris Daltond60c9192021-01-07 18:30:08 +00001457static void dump_mesh(const VertexList& mesh) {
1458#if LOGGING_ENABLED
1459 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001460 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 -05001461 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001462 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1463 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001464 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001465 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001466 }
1467 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001468 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001469 }
1470 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001471 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001472 }
1473 }
Chris Dalton75887a22021-01-06 00:23:13 -07001474#endif
Chris Daltond60c9192021-01-07 18:30:08 +00001475}
Stephen White95152e12017-12-18 10:52:44 -05001476
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001477static void dump_skel(const SSEdgeList& ssEdges) {
Chris Daltond60c9192021-01-07 18:30:08 +00001478#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001479 for (SSEdge* edge : ssEdges) {
1480 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001481 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001482 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001483 edge->fNext->fVertex->fID);
1484 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001485 TESS_LOG(" (original %g -> %g)\n",
1486 edge->fEdge->fTop->fID,
1487 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001488 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001489 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001490 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001491 }
1492 }
1493#endif
1494}
1495
Stephen White89042d52018-06-08 12:18:22 -04001496#ifdef SK_DEBUG
Chris Dalton811dc6a2021-01-07 16:40:32 -07001497static void validate_edge_pair(Edge* left, Edge* right, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001498 if (!left || !right) {
1499 return;
1500 }
1501 if (left->fTop == right->fTop) {
1502 SkASSERT(left->isLeftOf(right->fBottom));
1503 SkASSERT(right->isRightOf(left->fBottom));
1504 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1505 SkASSERT(left->isLeftOf(right->fTop));
1506 } else {
1507 SkASSERT(right->isRightOf(left->fTop));
1508 }
1509 if (left->fBottom == right->fBottom) {
1510 SkASSERT(left->isLeftOf(right->fTop));
1511 SkASSERT(right->isRightOf(left->fTop));
1512 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1513 SkASSERT(left->isLeftOf(right->fBottom));
1514 } else {
1515 SkASSERT(right->isRightOf(left->fBottom));
1516 }
1517}
1518
Chris Dalton811dc6a2021-01-07 16:40:32 -07001519static void validate_edge_list(EdgeList* edges, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001520 Edge* left = edges->fHead;
1521 if (!left) {
1522 return;
1523 }
1524 for (Edge* right = left->fRight; right; right = right->fRight) {
1525 validate_edge_pair(left, right, c);
1526 left = right;
1527 }
1528}
1529#endif
1530
ethannicholase9709e82016-01-07 13:34:16 -08001531// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1532
Chris Daltond60c9192021-01-07 18:30:08 +00001533static bool connected(Vertex* v) {
1534 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1535}
1536
Chris Dalton811dc6a2021-01-07 16:40:32 -07001537GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001538 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001539 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001540 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001541 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001542 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001543 continue;
1544 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001545 Edge* leftEnclosingEdge;
1546 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001547 bool restartChecks;
1548 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001549 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1550 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001551 restartChecks = false;
1552 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001553 v->fLeftEnclosingEdge = leftEnclosingEdge;
1554 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001555 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001556 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001557 if (this->checkForIntersection(
1558 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) ||
1559 this->checkForIntersection(
1560 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001561 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001562 return SimplifyResult::kAbort;
1563 }
1564 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001565 restartChecks = true;
1566 break;
1567 }
1568 }
1569 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001570 if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges,
1571 &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001572 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001573 return SimplifyResult::kAbort;
1574 }
1575 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001576 restartChecks = true;
1577 }
1578
1579 }
1580 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001581#ifdef SK_DEBUG
1582 validate_edge_list(&activeEdges, c);
1583#endif
ethannicholase9709e82016-01-07 13:34:16 -08001584 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +00001585 remove_edge(e, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001586 }
1587 Edge* leftEdge = leftEnclosingEdge;
1588 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001589 insert_edge(e, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001590 leftEdge = e;
1591 }
ethannicholase9709e82016-01-07 13:34:16 -08001592 }
Stephen Whitee260c462017-12-19 18:09:54 -05001593 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001594 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001595}
1596
1597// Stage 5: Tessellate the simplified mesh into monotone polygons.
1598
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001599Poly* GrTriangulator::tessellate(const VertexList& vertices) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001600 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001601 int maxWindMagnitude = std::numeric_limits<int>::max();
Chris Dalton854ee852021-01-05 15:12:59 -07001602 if (fSimpleInnerPolygons && !SkPathFillType_IsEvenOdd(fPath.getFillType())) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001603 maxWindMagnitude = 1;
1604 }
ethannicholase9709e82016-01-07 13:34:16 -08001605 EdgeList activeEdges;
1606 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001607 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001608 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001609 continue;
1610 }
Chris Daltond60c9192021-01-07 18:30:08 +00001611#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001612 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 -08001613#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001614 Edge* leftEnclosingEdge;
1615 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001616 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001617 Poly* leftPoly;
1618 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001619 if (v->fFirstEdgeAbove) {
1620 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1621 rightPoly = v->fLastEdgeAbove->fRightPoly;
1622 } else {
1623 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1624 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1625 }
Chris Daltond60c9192021-01-07 18:30:08 +00001626#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001627 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001628 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001629 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1630 e->fTop->fID, e->fBottom->fID,
1631 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1632 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001633 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001634 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001635 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001636 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1637 e->fTop->fID, e->fBottom->fID,
1638 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1639 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001640 }
1641#endif
1642 if (v->fFirstEdgeAbove) {
1643 if (leftPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001644 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001645 }
1646 if (rightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001647 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001648 }
1649 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001650 Edge* rightEdge = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001651 remove_edge(e, &activeEdges);
Stephen White8a0bfc52017-02-21 15:24:13 -05001652 if (e->fRightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001653 e->fRightPoly->addEdge(e, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001654 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001655 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001656 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001657 }
1658 }
Chris Daltond60c9192021-01-07 18:30:08 +00001659 remove_edge(v->fLastEdgeAbove, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001660 if (!v->fFirstEdgeBelow) {
1661 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1662 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1663 rightPoly->fPartner = leftPoly;
1664 leftPoly->fPartner = rightPoly;
1665 }
1666 }
1667 }
1668 if (v->fFirstEdgeBelow) {
1669 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001670 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001671 if (leftPoly == rightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001672 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
senorblanco531237e2016-06-02 11:36:48 -07001673 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001674 leftPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001675 leftEnclosingEdge->fRightPoly = leftPoly;
1676 } else {
1677 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001678 rightPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001679 rightEnclosingEdge->fLeftPoly = rightPoly;
1680 }
ethannicholase9709e82016-01-07 13:34:16 -08001681 }
Chris Daltond60c9192021-01-07 18:30:08 +00001682 Edge* join = fAlloc.make<Edge>(leftPoly->lastVertex(), v, 1,
1683 Edge::Type::kInner);
1684 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, fAlloc);
1685 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001686 }
1687 }
1688 Edge* leftEdge = v->fFirstEdgeBelow;
1689 leftEdge->fLeftPoly = leftPoly;
Chris Daltond60c9192021-01-07 18:30:08 +00001690 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001691 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1692 rightEdge = rightEdge->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001693 insert_edge(rightEdge, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001694 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1695 winding += leftEdge->fWinding;
1696 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001697 if (abs(winding) > maxWindMagnitude) {
1698 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1699 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001700 Poly* poly = new_poly(&polys, v, winding, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001701 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1702 }
1703 leftEdge = rightEdge;
1704 }
1705 v->fLastEdgeBelow->fRightPoly = rightPoly;
1706 }
Chris Daltond60c9192021-01-07 18:30:08 +00001707#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001708 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001709 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001710 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1711 e->fTop->fID, e->fBottom->fID,
1712 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1713 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001714 }
1715#endif
1716 }
1717 return polys;
1718}
1719
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001720static void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
1721 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001722 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001723 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001724 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001725 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001726 continue;
1727 }
1728 Edge* leftEnclosingEdge;
1729 Edge* rightEnclosingEdge;
1730 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1731 bool prevFilled = leftEnclosingEdge &&
1732 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1733 for (Edge* e = v->fFirstEdgeAbove; e;) {
1734 Edge* next = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001735 remove_edge(e, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001736 bool filled = apply_fill_type(fillType, e->fWinding);
1737 if (filled == prevFilled) {
Chris Daltond60c9192021-01-07 18:30:08 +00001738 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001739 }
Stephen White49789062017-02-21 10:35:49 -05001740 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001741 e = next;
1742 }
Stephen White49789062017-02-21 10:35:49 -05001743 Edge* prev = leftEnclosingEdge;
1744 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1745 if (prev) {
1746 e->fWinding += prev->fWinding;
1747 }
Chris Daltond60c9192021-01-07 18:30:08 +00001748 insert_edge(e, prev, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001749 prev = e;
1750 }
senorblancof57372d2016-08-31 10:36:19 -07001751 }
senorblancof57372d2016-08-31 10:36:19 -07001752}
1753
Stephen White66412122017-03-01 11:48:27 -05001754// Note: this is the normal to the edge, but not necessarily unit length.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001755static void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001756 normal->set(SkDoubleToScalar(e->fLine.fA),
1757 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001758}
1759
1760// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1761// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1762// invert on stroking.
1763
Chris Dalton811dc6a2021-01-07 16:40:32 -07001764static void simplify_boundary(EdgeList* boundary, const Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001765 Edge* prevEdge = boundary->fTail;
1766 SkVector prevNormal;
1767 get_edge_normal(prevEdge, &prevNormal);
1768 for (Edge* e = boundary->fHead; e != nullptr;) {
1769 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1770 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001771 double distPrev = e->dist(prev->fPoint);
1772 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001773 SkVector normal;
1774 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001775 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001776 if (prev == next) {
Chris Daltond60c9192021-01-07 18:30:08 +00001777 remove_edge(prevEdge, boundary);
1778 remove_edge(e, boundary);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001779 prevEdge = boundary->fTail;
1780 e = boundary->fHead;
1781 if (prevEdge) {
1782 get_edge_normal(prevEdge, &prevNormal);
1783 }
1784 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001785 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Chris Daltond60c9192021-01-07 18:30:08 +00001786 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001787 if (prev->fPoint != next->fPoint) {
1788 join->fLine.normalize();
1789 join->fLine = join->fLine * join->fWinding;
1790 }
Chris Daltond60c9192021-01-07 18:30:08 +00001791 insert_edge(join, e, boundary);
1792 remove_edge(prevEdge, boundary);
1793 remove_edge(e, boundary);
senorblancof57372d2016-08-31 10:36:19 -07001794 if (join->fLeft && join->fRight) {
1795 prevEdge = join->fLeft;
1796 e = join;
1797 } else {
1798 prevEdge = boundary->fTail;
1799 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1800 }
1801 get_edge_normal(prevEdge, &prevNormal);
1802 } else {
1803 prevEdge = e;
1804 prevNormal = normal;
1805 e = e->fRight;
1806 }
1807 }
1808}
1809
Chris Dalton811dc6a2021-01-07 16:40:32 -07001810static void ss_connect(Vertex* v, Vertex* dest, const Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001811 if (v == dest) {
1812 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001813 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001814 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001815 if (v->fSynthetic) {
Chris Daltond60c9192021-01-07 18:30:08 +00001816 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001817 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001818 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1819 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001820 v->fPartner->fPartner = dest;
1821 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001822 }
1823}
1824
Chris Dalton811dc6a2021-01-07 16:40:32 -07001825void Event::apply(VertexList* mesh, const Comparator& c, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001826 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001827 return;
1828 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001829 Vertex* prev = fEdge->fPrev->fVertex;
1830 Vertex* next = fEdge->fNext->fVertex;
1831 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1832 SSEdge* nextEdge = fEdge->fNext->fNext;
1833 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1834 return;
Stephen White77169c82018-06-05 09:15:59 -04001835 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001836 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1837 dest->fSynthetic = true;
1838 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001839 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1840 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1841 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001842 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001843
Stephen Whitec4dbc372019-05-22 10:50:14 -04001844 ss_connect(prev, dest, c, alloc);
1845 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001846
Stephen Whitec4dbc372019-05-22 10:50:14 -04001847 prevEdge->fNext = nextEdge->fPrev = ssv;
1848 ssv->fPrev = prevEdge;
1849 ssv->fNext = nextEdge;
1850 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1851 return;
1852 }
1853 if (prevEdge->fEvent) {
1854 prevEdge->fEvent->fEdge = nullptr;
1855 }
1856 if (nextEdge->fEvent) {
1857 nextEdge->fEvent->fEdge = nullptr;
1858 }
1859 if (prevEdge->fPrev == nextEdge->fNext) {
1860 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1861 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1862 } else {
1863 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1864 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1865 if (dest->fPartner) {
1866 create_event(prevEdge, events, alloc);
1867 create_event(nextEdge, events, alloc);
1868 } else {
1869 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1870 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1871 }
1872 }
Stephen Whitee260c462017-12-19 18:09:54 -05001873}
1874
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001875static bool is_overlap_edge(Edge* e) {
Chris Daltond60c9192021-01-07 18:30:08 +00001876 if (e->fType == Edge::Type::kOuter) {
Stephen Whitee260c462017-12-19 18:09:54 -05001877 return e->fWinding != 0 && e->fWinding != 1;
Chris Daltond60c9192021-01-07 18:30:08 +00001878 } else if (e->fType == Edge::Type::kInner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001879 return e->fWinding != 0 && e->fWinding != -2;
1880 } else {
1881 return false;
1882 }
1883}
1884
1885// This is a stripped-down version of tessellate() which computes edges which
1886// join two filled regions, which represent overlap regions, and collapses them.
Chris Dalton811dc6a2021-01-07 16:40:32 -07001887static bool collapse_overlap_regions(VertexList* mesh, const Comparator& c, SkArenaAlloc& alloc,
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001888 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001889 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001890 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001891 EventList events(comp);
1892 SSVertexMap ssVertices;
1893 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001894 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001895 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001896 continue;
1897 }
1898 Edge* leftEnclosingEdge;
1899 Edge* rightEnclosingEdge;
1900 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001901 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001902 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
Chris Daltond60c9192021-01-07 18:30:08 +00001903 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001904 bool leftOverlap = prev && is_overlap_edge(prev);
1905 bool rightOverlap = is_overlap_edge(e);
Chris Daltond60c9192021-01-07 18:30:08 +00001906 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
Stephen Whitec4dbc372019-05-22 10:50:14 -04001907 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001908 if (prev) {
1909 e->fWinding -= prev->fWinding;
1910 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001911 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001912 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1913 e->fTop->fID, e->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +00001914 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001915 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001916 TESS_LOG("found overlap edge %g -> %g%s\n",
1917 e->fTop->fID, e->fBottom->fID,
1918 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001919 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1920 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1921 SSVertex* ssPrev = ssVertices[prevVertex];
1922 if (!ssPrev) {
1923 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1924 }
1925 SSVertex* ssNext = ssVertices[nextVertex];
1926 if (!ssNext) {
1927 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1928 }
1929 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1930 ssEdges.push_back(ssEdge);
1931// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1932 ssPrev->fNext = ssNext->fPrev = ssEdge;
1933 create_event(ssEdge, &events, alloc);
1934 if (!isOuterBoundary) {
Chris Daltond60c9192021-01-07 18:30:08 +00001935 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001936 }
1937 }
1938 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001939 }
1940 Edge* prev = leftEnclosingEdge;
1941 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1942 if (prev) {
1943 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001944 }
Chris Daltond60c9192021-01-07 18:30:08 +00001945 insert_edge(e, prev, &activeEdges);
Stephen Whitee260c462017-12-19 18:09:54 -05001946 prev = e;
1947 }
1948 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001949 bool complex = events.size() > 0;
1950
Brian Salomon120e7d62019-09-11 10:29:22 -04001951 TESS_LOG("\ncollapsing overlap regions\n");
1952 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001953 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001954 while (events.size() > 0) {
1955 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001956 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001957 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001958 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001959 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001960 dump_skel(ssEdges);
1961 for (SSEdge* edge : ssEdges) {
1962 if (Edge* e = edge->fEdge) {
1963 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1964 }
1965 }
1966 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001967}
1968
Chris Dalton811dc6a2021-01-07 16:40:32 -07001969static bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, const Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001970 if (!prev || !next) {
1971 return true;
1972 }
1973 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1974 return winding != origEdge->fWinding;
1975}
Stephen White92eba8a2017-02-06 09:50:27 -05001976
senorblancof57372d2016-08-31 10:36:19 -07001977// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1978// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1979// new antialiased mesh from those vertices.
1980
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001981static void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001982 const Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001983 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001984 // A boundary with fewer than 3 edges is degenerate.
1985 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1986 return;
1987 }
1988 Edge* prevEdge = boundary->fTail;
1989 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1990 SkVector prevNormal;
1991 get_edge_normal(prevEdge, &prevNormal);
1992 double radius = 0.5;
1993 Line prevInner(prevEdge->fLine);
1994 prevInner.fC -= radius;
1995 Line prevOuter(prevEdge->fLine);
1996 prevOuter.fC += radius;
1997 VertexList innerVertices;
1998 VertexList outerVertices;
1999 bool innerInversion = true;
2000 bool outerInversion = true;
2001 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
2002 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2003 SkVector normal;
2004 get_edge_normal(e, &normal);
2005 Line inner(e->fLine);
2006 inner.fC -= radius;
2007 Line outer(e->fLine);
2008 outer.fC += radius;
2009 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04002010 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002011 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2012 prevOuter.intersect(outer, &outerPoint)) {
2013 float cosAngle = normal.dot(prevNormal);
2014 if (cosAngle < -kCosMiterAngle) {
2015 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2016
2017 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2018 Line bisector(innerPoint, outerPoint);
2019 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2020 if (tangent.fA == 0 && tangent.fB == 0) {
2021 continue;
2022 }
2023 tangent.normalize();
2024 Line innerTangent(tangent);
2025 Line outerTangent(tangent);
2026 innerTangent.fC -= 0.5;
2027 outerTangent.fC += 0.5;
2028 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2029 if (prevNormal.cross(normal) > 0) {
2030 // Miter inner points
2031 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2032 !innerTangent.intersect(inner, &innerPoint2) ||
2033 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002034 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002035 }
2036 Line prevTangent(prevV->fPoint,
2037 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2038 Line nextTangent(nextV->fPoint,
2039 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002040 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002041 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002042 }
2043 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002044 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002045 }
2046 outerPoint1 = outerPoint2 = outerPoint;
2047 } else {
2048 // Miter outer points
2049 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2050 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002051 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002052 }
2053 Line prevTangent(prevV->fPoint,
2054 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2055 Line nextTangent(nextV->fPoint,
2056 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002057 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002058 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002059 }
2060 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002061 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002062 }
2063 innerPoint1 = innerPoint2 = innerPoint;
2064 }
Stephen Whiteea495232018-04-03 11:28:15 -04002065 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2066 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2067 continue;
2068 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002069 TESS_LOG("inner (%g, %g), (%g, %g), ",
2070 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2071 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2072 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002073 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2074 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2075 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2076 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2077 innerVertex1->fPartner = outerVertex1;
2078 innerVertex2->fPartner = outerVertex2;
2079 outerVertex1->fPartner = innerVertex1;
2080 outerVertex2->fPartner = innerVertex2;
2081 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2082 innerInversion = false;
2083 }
2084 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2085 outerInversion = false;
2086 }
2087 innerVertices.append(innerVertex1);
2088 innerVertices.append(innerVertex2);
2089 outerVertices.append(outerVertex1);
2090 outerVertices.append(outerVertex2);
2091 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002092 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2093 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002094 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2095 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2096 innerVertex->fPartner = outerVertex;
2097 outerVertex->fPartner = innerVertex;
2098 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2099 innerInversion = false;
2100 }
2101 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2102 outerInversion = false;
2103 }
2104 innerVertices.append(innerVertex);
2105 outerVertices.append(outerVertex);
2106 }
2107 }
2108 prevInner = inner;
2109 prevOuter = outer;
2110 prevV = v;
2111 prevEdge = e;
2112 prevNormal = normal;
2113 }
2114 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2115 innerInversion = false;
2116 }
2117 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2118 outerInversion = false;
2119 }
2120 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2121 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2122 // interior inverts).
2123 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2124 // so it will be detected during collapse_overlap_regions().
2125 int innerWinding = innerInversion ? 2 : -2;
2126 int outerWinding = outerInversion ? -1 : 1;
2127 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00002128 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002129 }
Chris Daltond60c9192021-01-07 18:30:08 +00002130 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002131 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00002132 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002133 }
Chris Daltond60c9192021-01-07 18:30:08 +00002134 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002135 innerMesh->append(innerVertices);
2136 outerMesh->append(outerVertices);
2137}
senorblancof57372d2016-08-31 10:36:19 -07002138
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002139static void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType,
2140 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002141 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002142 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002143 Vertex* start = down ? e->fTop : e->fBottom;
2144 do {
senorblancof57372d2016-08-31 10:36:19 -07002145 e->fWinding = down ? 1 : -1;
2146 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002147 e->fLine.normalize();
2148 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002149 boundary->append(e);
2150 if (down) {
2151 // Find outgoing edge, in clockwise order.
2152 if ((next = e->fNextEdgeAbove)) {
2153 down = false;
2154 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2155 down = true;
2156 } else if ((next = e->fPrevEdgeAbove)) {
2157 down = false;
2158 }
2159 } else {
2160 // Find outgoing edge, in counter-clockwise order.
2161 if ((next = e->fPrevEdgeBelow)) {
2162 down = true;
2163 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2164 down = false;
2165 } else if ((next = e->fNextEdgeBelow)) {
2166 down = true;
2167 }
2168 }
Chris Daltond60c9192021-01-07 18:30:08 +00002169 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002170 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002171 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002172}
2173
Stephen White5ad721e2017-02-23 16:50:47 -05002174// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002175
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002176static void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Chris Dalton811dc6a2021-01-07 16:40:32 -07002177 VertexList* outerVertices, SkPathFillType fillType,
2178 const Comparator& c, SkArenaAlloc& alloc) {
Stephen White5ad721e2017-02-23 16:50:47 -05002179 remove_non_boundary_edges(inMesh, fillType, alloc);
2180 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002181 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002182 EdgeList boundary;
2183 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2184 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002185 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002186 }
2187 }
senorblancof57372d2016-08-31 10:36:19 -07002188}
2189
Stephen Whitebda29c02017-03-13 15:10:13 -04002190// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002191
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002192void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07002193 const Comparator& c) {
Chris Daltond60c9192021-01-07 18:30:08 +00002194#if LOGGING_ENABLED
ethannicholase9709e82016-01-07 13:34:16 -08002195 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002196 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002197 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002198 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002199 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002200 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002201 }
2202 }
2203#endif
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002204 this->sanitizeContours(contours, contourCnt);
2205 this->buildEdges(contours, contourCnt, mesh, c);
senorblancof57372d2016-08-31 10:36:19 -07002206}
2207
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002208void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002209 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002210 return;
ethannicholase9709e82016-01-07 13:34:16 -08002211 }
2212
2213 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002214 if (c.fDirection == Comparator::Direction::kHorizontal) {
2215 merge_sort<sweep_lt_horiz>(vertices);
2216 } else {
2217 merge_sort<sweep_lt_vert>(vertices);
2218 }
Chris Daltond60c9192021-01-07 18:30:08 +00002219#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002220 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002221 static float gID = 0.0f;
2222 v->fID = gID++;
2223 }
2224#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002225}
2226
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002227Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt, VertexList* outerMesh) {
2228 const SkRect& pathBounds = fPath.getBounds();
Stephen White16a40cb2017-02-23 11:10:01 -05002229 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2230 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002231 VertexList mesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002232 this->contoursToMesh(contours, contourCnt, &mesh, c);
2233 SortMesh(&mesh, c);
2234 this->mergeCoincidentVertices(&mesh, c);
2235 if (SimplifyResult::kAbort == this->simplify(&mesh, c)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07002236 return nullptr;
2237 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002238 TESS_LOG("\nsimplified mesh:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002239 dump_mesh(mesh);
Chris Dalton854ee852021-01-05 15:12:59 -07002240 if (fEmitCoverage) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002241 VertexList innerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002242 extract_boundaries(mesh, &innerMesh, outerMesh, fPath.getFillType(), c, fAlloc);
2243 SortMesh(&innerMesh, c);
2244 SortMesh(outerMesh, c);
2245 this->mergeCoincidentVertices(&innerMesh, c);
2246 bool was_complex = this->mergeCoincidentVertices(outerMesh, c);
2247 auto result = this->simplify(&innerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002248 SkASSERT(SimplifyResult::kAbort != result);
2249 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002250 result = this->simplify(outerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002251 SkASSERT(SimplifyResult::kAbort != result);
2252 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002253 TESS_LOG("\ninner mesh before:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002254 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002255 TESS_LOG("\nouter mesh before:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002256 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002257 EventComparator eventLT(EventComparator::Op::kLessThan);
2258 EventComparator eventGT(EventComparator::Op::kGreaterThan);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002259 was_complex = collapse_overlap_regions(&innerMesh, c, fAlloc, eventLT) || was_complex;
2260 was_complex = collapse_overlap_regions(outerMesh, c, fAlloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002261 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002262 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002263 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002264 TESS_LOG("\ninner mesh after:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002265 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002266 TESS_LOG("\nouter mesh after:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002267 dump_mesh(*outerMesh);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002268 connect_partners(outerMesh, c, fAlloc);
2269 connect_partners(&innerMesh, c, fAlloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002270 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002271 this->mergeCoincidentVertices(&aaMesh, c);
2272 result = this->simplify(&aaMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002273 SkASSERT(SimplifyResult::kAbort != result);
Brian Salomon120e7d62019-09-11 10:29:22 -04002274 TESS_LOG("combined and simplified mesh:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002275 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002276 outerMesh->fHead = outerMesh->fTail = nullptr;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002277 return this->tessellate(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002278 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002279 TESS_LOG("no complex polygons; taking fast path\n");
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002280 return this->tessellate(innerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002281 }
Stephen White49789062017-02-21 10:35:49 -05002282 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002283 return this->tessellate(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002284 }
senorblancof57372d2016-08-31 10:36:19 -07002285}
2286
2287// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002288void* GrTriangulator::polysToTriangles(Poly* polys, void* data, SkPathFillType overrideFillType) {
senorblancof57372d2016-08-31 10:36:19 -07002289 for (Poly* poly = polys; poly; poly = poly->fNext) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002290 if (apply_fill_type(overrideFillType, poly)) {
Chris Daltond60c9192021-01-07 18:30:08 +00002291 data = poly->emit(fEmitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002292 }
2293 }
2294 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002295}
2296
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002297Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt,
2298 VertexList* outerMesh) {
2299 if (SkPathFillType_IsInverse(fPath.getFillType())) {
ethannicholase9709e82016-01-07 13:34:16 -08002300 contourCnt++;
2301 }
Stephen White3a9aab92017-03-07 14:07:18 -05002302 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002303
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002304 this->pathToContours(tolerance, clipBounds, contours.get());
2305 return this->contoursToPolys(contours.get(), contourCnt, outerMesh);
ethannicholase9709e82016-01-07 13:34:16 -08002306}
2307
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002308static int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002309 // We could theoretically be more aggressive about not counting empty contours, but we need to
2310 // actually match the exact number of contour linked lists the tessellator will create later on.
2311 int contourCnt = 1;
2312 bool hasPoints = false;
2313
Chris Dalton7156db22020-05-07 22:06:28 +00002314 SkPath::Iter iter(path, false);
2315 SkPath::Verb verb;
2316 SkPoint pts[4];
Chris Daltonc71b3d42020-01-08 21:29:59 -07002317 bool first = true;
Chris Dalton7156db22020-05-07 22:06:28 +00002318 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002319 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +00002320 case SkPath::kMove_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002321 if (!first) {
2322 ++contourCnt;
2323 }
John Stiles30212b72020-06-11 17:55:07 -04002324 [[fallthrough]];
Chris Dalton7156db22020-05-07 22:06:28 +00002325 case SkPath::kLine_Verb:
2326 case SkPath::kConic_Verb:
2327 case SkPath::kQuad_Verb:
2328 case SkPath::kCubic_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002329 hasPoints = true;
John Stiles30212b72020-06-11 17:55:07 -04002330 break;
Chris Daltonc71b3d42020-01-08 21:29:59 -07002331 default:
2332 break;
2333 }
2334 first = false;
2335 }
2336 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002337 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002338 }
Stephen White11f65e02017-02-16 19:00:39 -05002339 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002340}
2341
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002342static int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002343 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002344 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002345 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002346 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
ethannicholase9709e82016-01-07 13:34:16 -08002347 }
2348 }
2349 return count;
2350}
2351
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002352static int64_t count_outer_mesh_points(const VertexList& outerMesh) {
Greg Danield5b45932018-06-07 13:15:10 -04002353 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002354 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2355 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002356 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002357 }
2358 }
2359 return count;
2360}
2361
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002362static void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002363 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2364 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2365 Vertex* v0 = e->fTop;
2366 Vertex* v1 = e->fBottom;
2367 Vertex* v2 = e->fBottom->fPartner;
2368 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002369 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2370 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002371 }
2372 }
2373 return data;
2374}
2375
ethannicholase9709e82016-01-07 13:34:16 -08002376// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2377
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002378int GrTriangulator::pathToTriangles(float tolerance, const SkRect& clipBounds,
2379 GrEagerVertexAllocator* vertexAllocator,
2380 SkPathFillType overrideFillType) {
2381 int contourCnt = get_contour_count(fPath, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002382 if (contourCnt <= 0) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002383 fIsLinear = true;
ethannicholase9709e82016-01-07 13:34:16 -08002384 return 0;
2385 }
Stephen Whitebda29c02017-03-13 15:10:13 -04002386 VertexList outerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002387 Poly* polys = this->pathToPolys(tolerance, clipBounds, contourCnt, &outerMesh);
2388 int64_t count64 = count_points(polys, overrideFillType);
Chris Dalton854ee852021-01-05 15:12:59 -07002389 if (fEmitCoverage) {
Greg Danield5b45932018-06-07 13:15:10 -04002390 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002391 }
Greg Danield5b45932018-06-07 13:15:10 -04002392 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002393 return 0;
2394 }
Greg Danield5b45932018-06-07 13:15:10 -04002395 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002396
Chris Dalton854ee852021-01-05 15:12:59 -07002397 size_t vertexStride = sizeof(SkPoint);
2398 if (fEmitCoverage) {
2399 vertexStride += sizeof(float);
2400 }
Chris Daltond081dce2020-01-23 12:09:04 -07002401 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002402 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002403 SkDebugf("Could not allocate vertices\n");
2404 return 0;
2405 }
senorblancof57372d2016-08-31 10:36:19 -07002406
Brian Salomon120e7d62019-09-11 10:29:22 -04002407 TESS_LOG("emitting %d verts\n", count);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002408 void* end = this->polysToTriangles(polys, verts, overrideFillType);
Brian Osman80879d42019-01-07 16:15:27 -05002409 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002410
senorblancof57372d2016-08-31 10:36:19 -07002411 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002412 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002413 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002414 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002415 return actualCount;
2416}
2417
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002418int GrTriangulator::PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
2419 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002420 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002421 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002422 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002423 return 0;
2424 }
Chris Dalton854ee852021-01-05 15:12:59 -07002425 GrTriangulator triangulator(path);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002426 Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, contourCnt, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002427 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002428 int64_t count64 = count_points(polys, fillType);
2429 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002430 *verts = nullptr;
2431 return 0;
2432 }
Greg Danield5b45932018-06-07 13:15:10 -04002433 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002434
Chris Dalton17dc4182020-03-25 16:18:16 -06002435 *verts = new WindingVertex[count];
2436 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002437 SkPoint* points = new SkPoint[count];
2438 SkPoint* pointsEnd = points;
2439 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002440 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002441 SkPoint* start = pointsEnd;
Chris Daltond60c9192021-01-07 18:30:08 +00002442 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002443 while (start != pointsEnd) {
2444 vertsEnd->fPos = *start;
2445 vertsEnd->fWinding = poly->fWinding;
2446 ++start;
2447 ++vertsEnd;
2448 }
2449 }
2450 }
2451 int actualCount = static_cast<int>(vertsEnd - *verts);
2452 SkASSERT(actualCount <= count);
2453 SkASSERT(pointsEnd - points == actualCount);
2454 delete[] points;
2455 return actualCount;
2456}