blob: 8c374c533f18faa24422db2f0f709f9a9a4f6552 [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 Daltond60c9192021-01-07 18:30:08 +0000433 void apply(VertexList* mesh, 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 Daltond60c9192021-01-07 18:30:08 +0000474 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 Daltond60c9192021-01-07 18:30:08 +0000839static Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, 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 Daltond60c9192021-01-07 18:30:08 +0000878static void insert_edge_above(Edge* edge, Vertex* v, 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 Daltond60c9192021-01-07 18:30:08 +0000897static void insert_edge_below(Edge* edge, Vertex* v, 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 Daltond60c9192021-01-07 18:30:08 +0000939 Comparator& c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400940
Chris Daltond60c9192021-01-07 18:30:08 +0000941static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, 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 Daltond60c9192021-01-07 18:30:08 +0000968 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 Daltond60c9192021-01-07 18:30:08 +00001005static void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001006 remove_edge_below(edge);
1007 edge->fTop = v;
1008 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +00001009 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001010 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001011 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001012}
1013
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001014static void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Daltond60c9192021-01-07 18:30:08 +00001015 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001016 remove_edge_above(edge);
1017 edge->fBottom = v;
1018 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +00001019 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001020 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001021 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001022}
1023
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001024static void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Daltond60c9192021-01-07 18:30:08 +00001025 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001026 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001027 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1028 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1029 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001030 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001031 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +00001032 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001033 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001034 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001035 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001036 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001037 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001038 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001039 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001040 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001041 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001042 }
1043}
1044
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001045static void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Daltond60c9192021-01-07 18:30:08 +00001046 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001047 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001048 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1049 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1050 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001051 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001052 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +00001053 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001054 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001055 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001056 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001057 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001058 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001059 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001060 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001061 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001062 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001063 }
1064}
1065
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001066static bool top_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001067 if (!left || !right) {
1068 return false;
1069 }
1070 return left->fTop->fPoint == right->fTop->fPoint ||
1071 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1072}
1073
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001074static bool bottom_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001075 if (!left || !right) {
1076 return false;
1077 }
1078 return left->fBottom->fPoint == right->fBottom->fPoint ||
1079 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1080}
1081
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001082static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Daltond60c9192021-01-07 18:30:08 +00001083 Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001084 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001085 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001086 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001087 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001088 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001089 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001090 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001091 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001092 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001093 } else {
1094 break;
1095 }
ethannicholase9709e82016-01-07 13:34:16 -08001096 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001097 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1098 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1099 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1100 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001101}
1102
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001103bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Daltond60c9192021-01-07 18:30:08 +00001104 Comparator& c) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001105 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001106 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001107 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001108 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1109 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001110 Vertex* top;
1111 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001112 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001113 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001114 top = v;
1115 bottom = edge->fTop;
1116 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001117 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001118 top = edge->fBottom;
1119 bottom = v;
1120 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001121 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001122 top = v;
1123 bottom = edge->fBottom;
1124 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001125 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001126 Edge* newEdge = fAlloc.make<Edge>(top, bottom, winding, edge->fType);
Chris Daltond60c9192021-01-07 18:30:08 +00001127 insert_edge_below(newEdge, top, c);
1128 insert_edge_above(newEdge, bottom, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001129 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001130 return true;
1131}
1132
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001133bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Daltond60c9192021-01-07 18:30:08 +00001134 Vertex** current, Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001135 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1136 return false;
1137 }
Stephen White1c5fd182018-07-12 15:54:05 -04001138 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1139 return false;
1140 }
Stephen White89042d52018-06-08 12:18:22 -04001141 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1142 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001143 rewind(activeEdges, current, right->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001144 return this->splitEdge(left, right->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001145 }
1146 } else {
1147 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001148 rewind(activeEdges, current, left->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001149 return this->splitEdge(right, left->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001150 }
1151 }
1152 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1153 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001154 rewind(activeEdges, current, right->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001155 return this->splitEdge(left, right->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001156 }
1157 } else {
1158 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001159 rewind(activeEdges, current, left->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001160 return this->splitEdge(right, left->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001161 }
1162 }
1163 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001164}
1165
Chris Daltond60c9192021-01-07 18:30:08 +00001166static Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c,
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001167 SkArenaAlloc& alloc, int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001168 if (!prev || !next || prev->fPoint == next->fPoint) {
1169 return nullptr;
1170 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001171 Edge* edge = new_edge(prev, next, type, c, alloc);
Chris Daltond60c9192021-01-07 18:30:08 +00001172 insert_edge_below(edge, edge->fTop, c);
1173 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001174 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001175 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001176 return edge;
1177}
1178
Chris Daltond60c9192021-01-07 18:30:08 +00001179static void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001180 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1181 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -05001182 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001183 if (src->fPartner) {
1184 src->fPartner->fPartner = dst;
1185 }
Stephen White7b376942018-05-22 11:51:32 -04001186 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001187 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001188 }
Stephen White7b376942018-05-22 11:51:32 -04001189 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001190 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001191 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001192 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001193 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001194}
1195
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001196static Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
Chris Daltond60c9192021-01-07 18:30:08 +00001197 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001198 Vertex* prevV = reference;
1199 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1200 prevV = prevV->fPrev;
1201 }
1202 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1203 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1204 prevV = nextV;
1205 nextV = nextV->fNext;
1206 }
1207 Vertex* v;
1208 if (prevV && coincident(prevV->fPoint, p)) {
1209 v = prevV;
1210 } else if (nextV && coincident(nextV->fPoint, p)) {
1211 v = nextV;
1212 } else {
1213 v = alloc.make<Vertex>(p, alpha);
Chris Daltond60c9192021-01-07 18:30:08 +00001214#if LOGGING_ENABLED
Stephen White95152e12017-12-18 10:52:44 -05001215 if (!prevV) {
1216 v->fID = mesh->fHead->fID - 1.0f;
1217 } else if (!nextV) {
1218 v->fID = mesh->fTail->fID + 1.0f;
1219 } else {
1220 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1221 }
1222#endif
1223 mesh->insert(v, prevV, nextV);
1224 }
1225 return v;
1226}
1227
Stephen White53a02982018-05-30 22:47:46 -04001228// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1229// sort criterion, it may not be possible to split correctly, since there is no point which is
1230// below the top and above the bottom. This function detects that case.
Chris Daltond60c9192021-01-07 18:30:08 +00001231static bool nearly_flat(Comparator& c, Edge* edge) {
Stephen White53a02982018-05-30 22:47:46 -04001232 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1233 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001234 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001235}
1236
Chris Daltond60c9192021-01-07 18:30:08 +00001237static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
Stephen Whitee62999f2018-06-05 18:45:07 -04001238 if (c.sweep_lt(p, min)) {
1239 return min;
1240 } else if (c.sweep_lt(max, p)) {
1241 return max;
1242 } else {
1243 return p;
1244 }
1245}
1246
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001247static void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001248 Line line1 = edge1->fLine;
1249 Line line2 = edge2->fLine;
1250 line1.normalize();
1251 line2.normalize();
1252 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1253 if (cosAngle > 0.999) {
1254 return;
1255 }
1256 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1257 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1258 SkPoint p;
1259 if (line1.intersect(line2, &p)) {
Chris Daltond60c9192021-01-07 18:30:08 +00001260 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001261 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001262 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 -04001263 }
1264}
1265
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001266bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Daltond60c9192021-01-07 18:30:08 +00001267 Vertex** current, VertexList* mesh, Comparator& c) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001268 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001269 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001270 }
Stephen White56158ae2017-01-30 14:31:31 -05001271 SkPoint p;
1272 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001273 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001274 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001275 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001276 Vertex* top = *current;
1277 // If the intersection point is above the current vertex, rewind to the vertex above the
1278 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001279 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001280 top = top->fPrev;
1281 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001282 if (!nearly_flat(c, left)) {
1283 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001284 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001285 if (!nearly_flat(c, right)) {
1286 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001287 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001288 if (p == left->fTop->fPoint) {
1289 v = left->fTop;
1290 } else if (p == left->fBottom->fPoint) {
1291 v = left->fBottom;
1292 } else if (p == right->fTop->fPoint) {
1293 v = right->fTop;
1294 } else if (p == right->fBottom->fPoint) {
1295 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001296 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001297 v = create_sorted_vertex(p, alpha, mesh, top, c, fAlloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001298 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001299 v->fSynthetic = true;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001300 compute_bisector(left, right, v, fAlloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001301 }
ethannicholase9709e82016-01-07 13:34:16 -08001302 }
Stephen White0cb31672017-06-08 14:41:01 -04001303 rewind(activeEdges, current, top ? top : v, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001304 this->splitEdge(left, v, activeEdges, current, c);
1305 this->splitEdge(right, v, activeEdges, current, c);
Brian Osman788b9162020-02-07 10:36:46 -05001306 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001307 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001308 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001309 return this->intersectEdgePair(left, right, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001310}
1311
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001312void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) {
Stephen White3a9aab92017-03-07 14:07:18 -05001313 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1314 SkASSERT(contour->fHead);
1315 Vertex* prev = contour->fTail;
Chris Dalton854ee852021-01-05 15:12:59 -07001316 if (fRoundVerticesToQuarterPixel) {
Stephen White3a9aab92017-03-07 14:07:18 -05001317 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001318 }
Stephen White3a9aab92017-03-07 14:07:18 -05001319 for (Vertex* v = contour->fHead; v;) {
Chris Dalton854ee852021-01-05 15:12:59 -07001320 if (fRoundVerticesToQuarterPixel) {
senorblancof57372d2016-08-31 10:36:19 -07001321 round(&v->fPoint);
1322 }
Stephen White3a9aab92017-03-07 14:07:18 -05001323 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001324 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001325 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001326 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001327 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001328 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001329 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001330 contour->remove(v);
Chris Dalton854ee852021-01-05 15:12:59 -07001331 } else if (fCullCollinearVertices &&
Chris Dalton6ccc0322020-01-29 11:38:16 -07001332 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001333 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001334 contour->remove(v);
1335 } else {
1336 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001337 }
Stephen White3a9aab92017-03-07 14:07:18 -05001338 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001339 }
1340 }
1341}
1342
Chris Daltond60c9192021-01-07 18:30:08 +00001343bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001344 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001345 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001346 }
Stephen Whitee260c462017-12-19 18:09:54 -05001347 bool merged = false;
1348 for (Vertex* v = mesh->fHead->fNext; v;) {
1349 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001350 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1351 v->fPoint = v->fPrev->fPoint;
1352 }
1353 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001354 merge_vertices(v, v->fPrev, mesh, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001355 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001356 }
Stephen Whitee260c462017-12-19 18:09:54 -05001357 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001358 }
Stephen Whitee260c462017-12-19 18:09:54 -05001359 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001360}
1361
1362// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1363
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001364void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Daltond60c9192021-01-07 18:30:08 +00001365 Comparator& c) {
Stephen White3a9aab92017-03-07 14:07:18 -05001366 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1367 Vertex* prev = contour->fTail;
1368 for (Vertex* v = contour->fHead; v;) {
1369 Vertex* next = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +00001370 connect(prev, v, Edge::Type::kInner, c, fAlloc);
Stephen White3a9aab92017-03-07 14:07:18 -05001371 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001372 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001373 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001374 }
1375 }
ethannicholase9709e82016-01-07 13:34:16 -08001376}
1377
Chris Daltond60c9192021-01-07 18:30:08 +00001378static void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitee260c462017-12-19 18:09:54 -05001379 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001380 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001381 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1382 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1383 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1384 // number.
Chris Daltond60c9192021-01-07 18:30:08 +00001385 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001386 inner->fPartner = outer->fPartner = nullptr;
1387 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001388 }
1389 }
1390}
1391
1392template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001393static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001394 Vertex* a = front->fHead;
1395 Vertex* b = back->fHead;
1396 while (a && b) {
1397 if (sweep_lt(a->fPoint, b->fPoint)) {
1398 front->remove(a);
1399 result->append(a);
1400 a = front->fHead;
1401 } else {
1402 back->remove(b);
1403 result->append(b);
1404 b = back->fHead;
1405 }
1406 }
1407 result->append(*front);
1408 result->append(*back);
1409}
1410
Chris Daltond60c9192021-01-07 18:30:08 +00001411static void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001412 if (c.fDirection == Comparator::Direction::kHorizontal) {
1413 sorted_merge<sweep_lt_horiz>(front, back, result);
1414 } else {
1415 sorted_merge<sweep_lt_vert>(front, back, result);
1416 }
Chris Daltond60c9192021-01-07 18:30:08 +00001417#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -04001418 float id = 0.0f;
1419 for (Vertex* v = result->fHead; v; v = v->fNext) {
1420 v->fID = id++;
1421 }
1422#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001423}
1424
ethannicholase9709e82016-01-07 13:34:16 -08001425// Stage 3: sort the vertices by increasing sweep direction.
1426
Stephen White16a40cb2017-02-23 11:10:01 -05001427template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001428static void merge_sort(VertexList* vertices) {
Stephen White16a40cb2017-02-23 11:10:01 -05001429 Vertex* slow = vertices->fHead;
1430 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001431 return;
1432 }
Stephen White16a40cb2017-02-23 11:10:01 -05001433 Vertex* fast = slow->fNext;
1434 if (!fast) {
1435 return;
1436 }
1437 do {
1438 fast = fast->fNext;
1439 if (fast) {
1440 fast = fast->fNext;
1441 slow = slow->fNext;
1442 }
1443 } while (fast);
1444 VertexList front(vertices->fHead, slow);
1445 VertexList back(slow->fNext, vertices->fTail);
1446 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001447
Stephen White16a40cb2017-02-23 11:10:01 -05001448 merge_sort<sweep_lt>(&front);
1449 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001450
Stephen White16a40cb2017-02-23 11:10:01 -05001451 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001452 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001453}
1454
Chris Daltond60c9192021-01-07 18:30:08 +00001455static void dump_mesh(const VertexList& mesh) {
1456#if LOGGING_ENABLED
1457 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001458 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 -05001459 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001460 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1461 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001462 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001463 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001464 }
1465 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001466 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001467 }
1468 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001469 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001470 }
1471 }
Chris Dalton75887a22021-01-06 00:23:13 -07001472#endif
Chris Daltond60c9192021-01-07 18:30:08 +00001473}
Stephen White95152e12017-12-18 10:52:44 -05001474
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001475static void dump_skel(const SSEdgeList& ssEdges) {
Chris Daltond60c9192021-01-07 18:30:08 +00001476#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001477 for (SSEdge* edge : ssEdges) {
1478 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001479 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001480 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001481 edge->fNext->fVertex->fID);
1482 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001483 TESS_LOG(" (original %g -> %g)\n",
1484 edge->fEdge->fTop->fID,
1485 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001486 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001487 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001488 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001489 }
1490 }
1491#endif
1492}
1493
Stephen White89042d52018-06-08 12:18:22 -04001494#ifdef SK_DEBUG
Chris Daltond60c9192021-01-07 18:30:08 +00001495static void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001496 if (!left || !right) {
1497 return;
1498 }
1499 if (left->fTop == right->fTop) {
1500 SkASSERT(left->isLeftOf(right->fBottom));
1501 SkASSERT(right->isRightOf(left->fBottom));
1502 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1503 SkASSERT(left->isLeftOf(right->fTop));
1504 } else {
1505 SkASSERT(right->isRightOf(left->fTop));
1506 }
1507 if (left->fBottom == right->fBottom) {
1508 SkASSERT(left->isLeftOf(right->fTop));
1509 SkASSERT(right->isRightOf(left->fTop));
1510 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1511 SkASSERT(left->isLeftOf(right->fBottom));
1512 } else {
1513 SkASSERT(right->isRightOf(left->fBottom));
1514 }
1515}
1516
Chris Daltond60c9192021-01-07 18:30:08 +00001517static void validate_edge_list(EdgeList* edges, Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001518 Edge* left = edges->fHead;
1519 if (!left) {
1520 return;
1521 }
1522 for (Edge* right = left->fRight; right; right = right->fRight) {
1523 validate_edge_pair(left, right, c);
1524 left = right;
1525 }
1526}
1527#endif
1528
ethannicholase9709e82016-01-07 13:34:16 -08001529// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1530
Chris Daltond60c9192021-01-07 18:30:08 +00001531static bool connected(Vertex* v) {
1532 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1533}
1534
1535GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001536 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001537 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001538 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001539 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001540 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001541 continue;
1542 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001543 Edge* leftEnclosingEdge;
1544 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001545 bool restartChecks;
1546 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001547 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1548 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001549 restartChecks = false;
1550 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001551 v->fLeftEnclosingEdge = leftEnclosingEdge;
1552 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001553 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001554 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001555 if (this->checkForIntersection(
1556 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) ||
1557 this->checkForIntersection(
1558 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001559 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001560 return SimplifyResult::kAbort;
1561 }
1562 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001563 restartChecks = true;
1564 break;
1565 }
1566 }
1567 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001568 if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges,
1569 &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001570 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001571 return SimplifyResult::kAbort;
1572 }
1573 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001574 restartChecks = true;
1575 }
1576
1577 }
1578 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001579#ifdef SK_DEBUG
1580 validate_edge_list(&activeEdges, c);
1581#endif
ethannicholase9709e82016-01-07 13:34:16 -08001582 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +00001583 remove_edge(e, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001584 }
1585 Edge* leftEdge = leftEnclosingEdge;
1586 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001587 insert_edge(e, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001588 leftEdge = e;
1589 }
ethannicholase9709e82016-01-07 13:34:16 -08001590 }
Stephen Whitee260c462017-12-19 18:09:54 -05001591 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001592 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001593}
1594
1595// Stage 5: Tessellate the simplified mesh into monotone polygons.
1596
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001597Poly* GrTriangulator::tessellate(const VertexList& vertices) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001598 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001599 int maxWindMagnitude = std::numeric_limits<int>::max();
Chris Dalton854ee852021-01-05 15:12:59 -07001600 if (fSimpleInnerPolygons && !SkPathFillType_IsEvenOdd(fPath.getFillType())) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001601 maxWindMagnitude = 1;
1602 }
ethannicholase9709e82016-01-07 13:34:16 -08001603 EdgeList activeEdges;
1604 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001605 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001606 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001607 continue;
1608 }
Chris Daltond60c9192021-01-07 18:30:08 +00001609#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001610 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 -08001611#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001612 Edge* leftEnclosingEdge;
1613 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001614 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001615 Poly* leftPoly;
1616 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001617 if (v->fFirstEdgeAbove) {
1618 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1619 rightPoly = v->fLastEdgeAbove->fRightPoly;
1620 } else {
1621 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1622 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1623 }
Chris Daltond60c9192021-01-07 18:30:08 +00001624#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001625 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001626 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001627 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1628 e->fTop->fID, e->fBottom->fID,
1629 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1630 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001631 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001632 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001633 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001634 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1635 e->fTop->fID, e->fBottom->fID,
1636 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1637 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001638 }
1639#endif
1640 if (v->fFirstEdgeAbove) {
1641 if (leftPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001642 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001643 }
1644 if (rightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001645 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001646 }
1647 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001648 Edge* rightEdge = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001649 remove_edge(e, &activeEdges);
Stephen White8a0bfc52017-02-21 15:24:13 -05001650 if (e->fRightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001651 e->fRightPoly->addEdge(e, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001652 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001653 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001654 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001655 }
1656 }
Chris Daltond60c9192021-01-07 18:30:08 +00001657 remove_edge(v->fLastEdgeAbove, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001658 if (!v->fFirstEdgeBelow) {
1659 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1660 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1661 rightPoly->fPartner = leftPoly;
1662 leftPoly->fPartner = rightPoly;
1663 }
1664 }
1665 }
1666 if (v->fFirstEdgeBelow) {
1667 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001668 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001669 if (leftPoly == rightPoly) {
Chris Daltond60c9192021-01-07 18:30:08 +00001670 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
senorblanco531237e2016-06-02 11:36:48 -07001671 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001672 leftPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001673 leftEnclosingEdge->fRightPoly = leftPoly;
1674 } else {
1675 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001676 rightPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001677 rightEnclosingEdge->fLeftPoly = rightPoly;
1678 }
ethannicholase9709e82016-01-07 13:34:16 -08001679 }
Chris Daltond60c9192021-01-07 18:30:08 +00001680 Edge* join = fAlloc.make<Edge>(leftPoly->lastVertex(), v, 1,
1681 Edge::Type::kInner);
1682 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, fAlloc);
1683 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001684 }
1685 }
1686 Edge* leftEdge = v->fFirstEdgeBelow;
1687 leftEdge->fLeftPoly = leftPoly;
Chris Daltond60c9192021-01-07 18:30:08 +00001688 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001689 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1690 rightEdge = rightEdge->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001691 insert_edge(rightEdge, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001692 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1693 winding += leftEdge->fWinding;
1694 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001695 if (abs(winding) > maxWindMagnitude) {
1696 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1697 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001698 Poly* poly = new_poly(&polys, v, winding, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001699 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1700 }
1701 leftEdge = rightEdge;
1702 }
1703 v->fLastEdgeBelow->fRightPoly = rightPoly;
1704 }
Chris Daltond60c9192021-01-07 18:30:08 +00001705#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001706 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001707 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001708 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1709 e->fTop->fID, e->fBottom->fID,
1710 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1711 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001712 }
1713#endif
1714 }
1715 return polys;
1716}
1717
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001718static void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
1719 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001720 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001721 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001722 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001723 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001724 continue;
1725 }
1726 Edge* leftEnclosingEdge;
1727 Edge* rightEnclosingEdge;
1728 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1729 bool prevFilled = leftEnclosingEdge &&
1730 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1731 for (Edge* e = v->fFirstEdgeAbove; e;) {
1732 Edge* next = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001733 remove_edge(e, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001734 bool filled = apply_fill_type(fillType, e->fWinding);
1735 if (filled == prevFilled) {
Chris Daltond60c9192021-01-07 18:30:08 +00001736 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001737 }
Stephen White49789062017-02-21 10:35:49 -05001738 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001739 e = next;
1740 }
Stephen White49789062017-02-21 10:35:49 -05001741 Edge* prev = leftEnclosingEdge;
1742 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1743 if (prev) {
1744 e->fWinding += prev->fWinding;
1745 }
Chris Daltond60c9192021-01-07 18:30:08 +00001746 insert_edge(e, prev, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001747 prev = e;
1748 }
senorblancof57372d2016-08-31 10:36:19 -07001749 }
senorblancof57372d2016-08-31 10:36:19 -07001750}
1751
Stephen White66412122017-03-01 11:48:27 -05001752// Note: this is the normal to the edge, but not necessarily unit length.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001753static void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001754 normal->set(SkDoubleToScalar(e->fLine.fA),
1755 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001756}
1757
1758// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1759// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1760// invert on stroking.
1761
Chris Daltond60c9192021-01-07 18:30:08 +00001762static void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001763 Edge* prevEdge = boundary->fTail;
1764 SkVector prevNormal;
1765 get_edge_normal(prevEdge, &prevNormal);
1766 for (Edge* e = boundary->fHead; e != nullptr;) {
1767 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1768 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001769 double distPrev = e->dist(prev->fPoint);
1770 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001771 SkVector normal;
1772 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001773 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001774 if (prev == next) {
Chris Daltond60c9192021-01-07 18:30:08 +00001775 remove_edge(prevEdge, boundary);
1776 remove_edge(e, boundary);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001777 prevEdge = boundary->fTail;
1778 e = boundary->fHead;
1779 if (prevEdge) {
1780 get_edge_normal(prevEdge, &prevNormal);
1781 }
1782 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001783 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Chris Daltond60c9192021-01-07 18:30:08 +00001784 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001785 if (prev->fPoint != next->fPoint) {
1786 join->fLine.normalize();
1787 join->fLine = join->fLine * join->fWinding;
1788 }
Chris Daltond60c9192021-01-07 18:30:08 +00001789 insert_edge(join, e, boundary);
1790 remove_edge(prevEdge, boundary);
1791 remove_edge(e, boundary);
senorblancof57372d2016-08-31 10:36:19 -07001792 if (join->fLeft && join->fRight) {
1793 prevEdge = join->fLeft;
1794 e = join;
1795 } else {
1796 prevEdge = boundary->fTail;
1797 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1798 }
1799 get_edge_normal(prevEdge, &prevNormal);
1800 } else {
1801 prevEdge = e;
1802 prevNormal = normal;
1803 e = e->fRight;
1804 }
1805 }
1806}
1807
Chris Daltond60c9192021-01-07 18:30:08 +00001808static void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001809 if (v == dest) {
1810 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001811 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001812 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001813 if (v->fSynthetic) {
Chris Daltond60c9192021-01-07 18:30:08 +00001814 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001815 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001816 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1817 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001818 v->fPartner->fPartner = dest;
1819 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001820 }
1821}
1822
Chris Daltond60c9192021-01-07 18:30:08 +00001823void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001824 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001825 return;
1826 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001827 Vertex* prev = fEdge->fPrev->fVertex;
1828 Vertex* next = fEdge->fNext->fVertex;
1829 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1830 SSEdge* nextEdge = fEdge->fNext->fNext;
1831 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1832 return;
Stephen White77169c82018-06-05 09:15:59 -04001833 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001834 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1835 dest->fSynthetic = true;
1836 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001837 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1838 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1839 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001840 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001841
Stephen Whitec4dbc372019-05-22 10:50:14 -04001842 ss_connect(prev, dest, c, alloc);
1843 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001844
Stephen Whitec4dbc372019-05-22 10:50:14 -04001845 prevEdge->fNext = nextEdge->fPrev = ssv;
1846 ssv->fPrev = prevEdge;
1847 ssv->fNext = nextEdge;
1848 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1849 return;
1850 }
1851 if (prevEdge->fEvent) {
1852 prevEdge->fEvent->fEdge = nullptr;
1853 }
1854 if (nextEdge->fEvent) {
1855 nextEdge->fEvent->fEdge = nullptr;
1856 }
1857 if (prevEdge->fPrev == nextEdge->fNext) {
1858 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1859 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1860 } else {
1861 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1862 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1863 if (dest->fPartner) {
1864 create_event(prevEdge, events, alloc);
1865 create_event(nextEdge, events, alloc);
1866 } else {
1867 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1868 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1869 }
1870 }
Stephen Whitee260c462017-12-19 18:09:54 -05001871}
1872
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001873static bool is_overlap_edge(Edge* e) {
Chris Daltond60c9192021-01-07 18:30:08 +00001874 if (e->fType == Edge::Type::kOuter) {
Stephen Whitee260c462017-12-19 18:09:54 -05001875 return e->fWinding != 0 && e->fWinding != 1;
Chris Daltond60c9192021-01-07 18:30:08 +00001876 } else if (e->fType == Edge::Type::kInner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001877 return e->fWinding != 0 && e->fWinding != -2;
1878 } else {
1879 return false;
1880 }
1881}
1882
1883// This is a stripped-down version of tessellate() which computes edges which
1884// join two filled regions, which represent overlap regions, and collapses them.
Chris Daltond60c9192021-01-07 18:30:08 +00001885static bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001886 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001887 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001888 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001889 EventList events(comp);
1890 SSVertexMap ssVertices;
1891 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001892 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001893 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001894 continue;
1895 }
1896 Edge* leftEnclosingEdge;
1897 Edge* rightEnclosingEdge;
1898 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001899 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001900 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
Chris Daltond60c9192021-01-07 18:30:08 +00001901 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001902 bool leftOverlap = prev && is_overlap_edge(prev);
1903 bool rightOverlap = is_overlap_edge(e);
Chris Daltond60c9192021-01-07 18:30:08 +00001904 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
Stephen Whitec4dbc372019-05-22 10:50:14 -04001905 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001906 if (prev) {
1907 e->fWinding -= prev->fWinding;
1908 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001909 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001910 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1911 e->fTop->fID, e->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +00001912 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001913 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001914 TESS_LOG("found overlap edge %g -> %g%s\n",
1915 e->fTop->fID, e->fBottom->fID,
1916 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001917 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1918 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1919 SSVertex* ssPrev = ssVertices[prevVertex];
1920 if (!ssPrev) {
1921 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1922 }
1923 SSVertex* ssNext = ssVertices[nextVertex];
1924 if (!ssNext) {
1925 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1926 }
1927 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1928 ssEdges.push_back(ssEdge);
1929// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1930 ssPrev->fNext = ssNext->fPrev = ssEdge;
1931 create_event(ssEdge, &events, alloc);
1932 if (!isOuterBoundary) {
Chris Daltond60c9192021-01-07 18:30:08 +00001933 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001934 }
1935 }
1936 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001937 }
1938 Edge* prev = leftEnclosingEdge;
1939 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1940 if (prev) {
1941 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001942 }
Chris Daltond60c9192021-01-07 18:30:08 +00001943 insert_edge(e, prev, &activeEdges);
Stephen Whitee260c462017-12-19 18:09:54 -05001944 prev = e;
1945 }
1946 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001947 bool complex = events.size() > 0;
1948
Brian Salomon120e7d62019-09-11 10:29:22 -04001949 TESS_LOG("\ncollapsing overlap regions\n");
1950 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001951 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001952 while (events.size() > 0) {
1953 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001954 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001955 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001956 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001957 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001958 dump_skel(ssEdges);
1959 for (SSEdge* edge : ssEdges) {
1960 if (Edge* e = edge->fEdge) {
1961 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1962 }
1963 }
1964 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001965}
1966
Chris Daltond60c9192021-01-07 18:30:08 +00001967static bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001968 if (!prev || !next) {
1969 return true;
1970 }
1971 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1972 return winding != origEdge->fWinding;
1973}
Stephen White92eba8a2017-02-06 09:50:27 -05001974
senorblancof57372d2016-08-31 10:36:19 -07001975// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1976// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1977// new antialiased mesh from those vertices.
1978
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001979static void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
Chris Daltond60c9192021-01-07 18:30:08 +00001980 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001981 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001982 // A boundary with fewer than 3 edges is degenerate.
1983 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1984 return;
1985 }
1986 Edge* prevEdge = boundary->fTail;
1987 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1988 SkVector prevNormal;
1989 get_edge_normal(prevEdge, &prevNormal);
1990 double radius = 0.5;
1991 Line prevInner(prevEdge->fLine);
1992 prevInner.fC -= radius;
1993 Line prevOuter(prevEdge->fLine);
1994 prevOuter.fC += radius;
1995 VertexList innerVertices;
1996 VertexList outerVertices;
1997 bool innerInversion = true;
1998 bool outerInversion = true;
1999 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
2000 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2001 SkVector normal;
2002 get_edge_normal(e, &normal);
2003 Line inner(e->fLine);
2004 inner.fC -= radius;
2005 Line outer(e->fLine);
2006 outer.fC += radius;
2007 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04002008 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002009 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2010 prevOuter.intersect(outer, &outerPoint)) {
2011 float cosAngle = normal.dot(prevNormal);
2012 if (cosAngle < -kCosMiterAngle) {
2013 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2014
2015 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2016 Line bisector(innerPoint, outerPoint);
2017 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2018 if (tangent.fA == 0 && tangent.fB == 0) {
2019 continue;
2020 }
2021 tangent.normalize();
2022 Line innerTangent(tangent);
2023 Line outerTangent(tangent);
2024 innerTangent.fC -= 0.5;
2025 outerTangent.fC += 0.5;
2026 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2027 if (prevNormal.cross(normal) > 0) {
2028 // Miter inner points
2029 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2030 !innerTangent.intersect(inner, &innerPoint2) ||
2031 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002032 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002033 }
2034 Line prevTangent(prevV->fPoint,
2035 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2036 Line nextTangent(nextV->fPoint,
2037 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002038 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002039 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002040 }
2041 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002042 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002043 }
2044 outerPoint1 = outerPoint2 = outerPoint;
2045 } else {
2046 // Miter outer points
2047 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2048 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002049 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002050 }
2051 Line prevTangent(prevV->fPoint,
2052 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2053 Line nextTangent(nextV->fPoint,
2054 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002055 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002056 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002057 }
2058 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002059 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002060 }
2061 innerPoint1 = innerPoint2 = innerPoint;
2062 }
Stephen Whiteea495232018-04-03 11:28:15 -04002063 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2064 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2065 continue;
2066 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002067 TESS_LOG("inner (%g, %g), (%g, %g), ",
2068 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2069 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2070 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002071 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2072 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2073 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2074 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2075 innerVertex1->fPartner = outerVertex1;
2076 innerVertex2->fPartner = outerVertex2;
2077 outerVertex1->fPartner = innerVertex1;
2078 outerVertex2->fPartner = innerVertex2;
2079 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2080 innerInversion = false;
2081 }
2082 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2083 outerInversion = false;
2084 }
2085 innerVertices.append(innerVertex1);
2086 innerVertices.append(innerVertex2);
2087 outerVertices.append(outerVertex1);
2088 outerVertices.append(outerVertex2);
2089 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002090 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2091 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002092 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2093 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2094 innerVertex->fPartner = outerVertex;
2095 outerVertex->fPartner = innerVertex;
2096 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2097 innerInversion = false;
2098 }
2099 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2100 outerInversion = false;
2101 }
2102 innerVertices.append(innerVertex);
2103 outerVertices.append(outerVertex);
2104 }
2105 }
2106 prevInner = inner;
2107 prevOuter = outer;
2108 prevV = v;
2109 prevEdge = e;
2110 prevNormal = normal;
2111 }
2112 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2113 innerInversion = false;
2114 }
2115 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2116 outerInversion = false;
2117 }
2118 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2119 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2120 // interior inverts).
2121 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2122 // so it will be detected during collapse_overlap_regions().
2123 int innerWinding = innerInversion ? 2 : -2;
2124 int outerWinding = outerInversion ? -1 : 1;
2125 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00002126 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002127 }
Chris Daltond60c9192021-01-07 18:30:08 +00002128 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002129 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00002130 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002131 }
Chris Daltond60c9192021-01-07 18:30:08 +00002132 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05002133 innerMesh->append(innerVertices);
2134 outerMesh->append(outerVertices);
2135}
senorblancof57372d2016-08-31 10:36:19 -07002136
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002137static void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType,
2138 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002139 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002140 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002141 Vertex* start = down ? e->fTop : e->fBottom;
2142 do {
senorblancof57372d2016-08-31 10:36:19 -07002143 e->fWinding = down ? 1 : -1;
2144 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002145 e->fLine.normalize();
2146 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002147 boundary->append(e);
2148 if (down) {
2149 // Find outgoing edge, in clockwise order.
2150 if ((next = e->fNextEdgeAbove)) {
2151 down = false;
2152 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2153 down = true;
2154 } else if ((next = e->fPrevEdgeAbove)) {
2155 down = false;
2156 }
2157 } else {
2158 // Find outgoing edge, in counter-clockwise order.
2159 if ((next = e->fPrevEdgeBelow)) {
2160 down = true;
2161 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2162 down = false;
2163 } else if ((next = e->fNextEdgeBelow)) {
2164 down = true;
2165 }
2166 }
Chris Daltond60c9192021-01-07 18:30:08 +00002167 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002168 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002169 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002170}
2171
Stephen White5ad721e2017-02-23 16:50:47 -05002172// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002173
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002174static void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Chris Daltond60c9192021-01-07 18:30:08 +00002175 VertexList* outerVertices, SkPathFillType fillType, Comparator& c,
2176 SkArenaAlloc& alloc) {
Stephen White5ad721e2017-02-23 16:50:47 -05002177 remove_non_boundary_edges(inMesh, fillType, alloc);
2178 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002179 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002180 EdgeList boundary;
2181 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2182 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002183 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002184 }
2185 }
senorblancof57372d2016-08-31 10:36:19 -07002186}
2187
Stephen Whitebda29c02017-03-13 15:10:13 -04002188// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002189
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002190void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Daltond60c9192021-01-07 18:30:08 +00002191 Comparator& c) {
2192#if LOGGING_ENABLED
ethannicholase9709e82016-01-07 13:34:16 -08002193 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002194 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002195 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002196 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002197 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002198 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002199 }
2200 }
2201#endif
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002202 this->sanitizeContours(contours, contourCnt);
2203 this->buildEdges(contours, contourCnt, mesh, c);
senorblancof57372d2016-08-31 10:36:19 -07002204}
2205
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002206void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002207 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002208 return;
ethannicholase9709e82016-01-07 13:34:16 -08002209 }
2210
2211 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002212 if (c.fDirection == Comparator::Direction::kHorizontal) {
2213 merge_sort<sweep_lt_horiz>(vertices);
2214 } else {
2215 merge_sort<sweep_lt_vert>(vertices);
2216 }
Chris Daltond60c9192021-01-07 18:30:08 +00002217#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002218 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002219 static float gID = 0.0f;
2220 v->fID = gID++;
2221 }
2222#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002223}
2224
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002225Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt, VertexList* outerMesh) {
2226 const SkRect& pathBounds = fPath.getBounds();
Stephen White16a40cb2017-02-23 11:10:01 -05002227 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2228 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002229 VertexList mesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002230 this->contoursToMesh(contours, contourCnt, &mesh, c);
2231 SortMesh(&mesh, c);
2232 this->mergeCoincidentVertices(&mesh, c);
2233 if (SimplifyResult::kAbort == this->simplify(&mesh, c)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07002234 return nullptr;
2235 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002236 TESS_LOG("\nsimplified mesh:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002237 dump_mesh(mesh);
Chris Dalton854ee852021-01-05 15:12:59 -07002238 if (fEmitCoverage) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002239 VertexList innerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002240 extract_boundaries(mesh, &innerMesh, outerMesh, fPath.getFillType(), c, fAlloc);
2241 SortMesh(&innerMesh, c);
2242 SortMesh(outerMesh, c);
2243 this->mergeCoincidentVertices(&innerMesh, c);
2244 bool was_complex = this->mergeCoincidentVertices(outerMesh, c);
2245 auto result = this->simplify(&innerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002246 SkASSERT(SimplifyResult::kAbort != result);
2247 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002248 result = this->simplify(outerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002249 SkASSERT(SimplifyResult::kAbort != result);
2250 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002251 TESS_LOG("\ninner mesh before:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002252 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002253 TESS_LOG("\nouter mesh before:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002254 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002255 EventComparator eventLT(EventComparator::Op::kLessThan);
2256 EventComparator eventGT(EventComparator::Op::kGreaterThan);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002257 was_complex = collapse_overlap_regions(&innerMesh, c, fAlloc, eventLT) || was_complex;
2258 was_complex = collapse_overlap_regions(outerMesh, c, fAlloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002259 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002260 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002261 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002262 TESS_LOG("\ninner mesh after:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002263 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002264 TESS_LOG("\nouter mesh after:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002265 dump_mesh(*outerMesh);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002266 connect_partners(outerMesh, c, fAlloc);
2267 connect_partners(&innerMesh, c, fAlloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002268 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002269 this->mergeCoincidentVertices(&aaMesh, c);
2270 result = this->simplify(&aaMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002271 SkASSERT(SimplifyResult::kAbort != result);
Brian Salomon120e7d62019-09-11 10:29:22 -04002272 TESS_LOG("combined and simplified mesh:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002273 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002274 outerMesh->fHead = outerMesh->fTail = nullptr;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002275 return this->tessellate(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002276 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002277 TESS_LOG("no complex polygons; taking fast path\n");
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002278 return this->tessellate(innerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002279 }
Stephen White49789062017-02-21 10:35:49 -05002280 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002281 return this->tessellate(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002282 }
senorblancof57372d2016-08-31 10:36:19 -07002283}
2284
2285// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002286void* GrTriangulator::polysToTriangles(Poly* polys, void* data, SkPathFillType overrideFillType) {
senorblancof57372d2016-08-31 10:36:19 -07002287 for (Poly* poly = polys; poly; poly = poly->fNext) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002288 if (apply_fill_type(overrideFillType, poly)) {
Chris Daltond60c9192021-01-07 18:30:08 +00002289 data = poly->emit(fEmitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002290 }
2291 }
2292 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002293}
2294
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002295Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt,
2296 VertexList* outerMesh) {
2297 if (SkPathFillType_IsInverse(fPath.getFillType())) {
ethannicholase9709e82016-01-07 13:34:16 -08002298 contourCnt++;
2299 }
Stephen White3a9aab92017-03-07 14:07:18 -05002300 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002301
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002302 this->pathToContours(tolerance, clipBounds, contours.get());
2303 return this->contoursToPolys(contours.get(), contourCnt, outerMesh);
ethannicholase9709e82016-01-07 13:34:16 -08002304}
2305
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002306static int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002307 // We could theoretically be more aggressive about not counting empty contours, but we need to
2308 // actually match the exact number of contour linked lists the tessellator will create later on.
2309 int contourCnt = 1;
2310 bool hasPoints = false;
2311
Chris Dalton7156db22020-05-07 22:06:28 +00002312 SkPath::Iter iter(path, false);
2313 SkPath::Verb verb;
2314 SkPoint pts[4];
Chris Daltonc71b3d42020-01-08 21:29:59 -07002315 bool first = true;
Chris Dalton7156db22020-05-07 22:06:28 +00002316 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002317 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +00002318 case SkPath::kMove_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002319 if (!first) {
2320 ++contourCnt;
2321 }
John Stiles30212b72020-06-11 17:55:07 -04002322 [[fallthrough]];
Chris Dalton7156db22020-05-07 22:06:28 +00002323 case SkPath::kLine_Verb:
2324 case SkPath::kConic_Verb:
2325 case SkPath::kQuad_Verb:
2326 case SkPath::kCubic_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002327 hasPoints = true;
John Stiles30212b72020-06-11 17:55:07 -04002328 break;
Chris Daltonc71b3d42020-01-08 21:29:59 -07002329 default:
2330 break;
2331 }
2332 first = false;
2333 }
2334 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002335 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002336 }
Stephen White11f65e02017-02-16 19:00:39 -05002337 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002338}
2339
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002340static int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002341 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002342 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002343 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002344 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
ethannicholase9709e82016-01-07 13:34:16 -08002345 }
2346 }
2347 return count;
2348}
2349
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002350static int64_t count_outer_mesh_points(const VertexList& outerMesh) {
Greg Danield5b45932018-06-07 13:15:10 -04002351 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002352 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2353 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002354 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002355 }
2356 }
2357 return count;
2358}
2359
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002360static void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002361 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2362 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2363 Vertex* v0 = e->fTop;
2364 Vertex* v1 = e->fBottom;
2365 Vertex* v2 = e->fBottom->fPartner;
2366 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002367 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2368 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002369 }
2370 }
2371 return data;
2372}
2373
ethannicholase9709e82016-01-07 13:34:16 -08002374// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2375
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002376int GrTriangulator::pathToTriangles(float tolerance, const SkRect& clipBounds,
2377 GrEagerVertexAllocator* vertexAllocator,
2378 SkPathFillType overrideFillType) {
2379 int contourCnt = get_contour_count(fPath, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002380 if (contourCnt <= 0) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002381 fIsLinear = true;
ethannicholase9709e82016-01-07 13:34:16 -08002382 return 0;
2383 }
Stephen Whitebda29c02017-03-13 15:10:13 -04002384 VertexList outerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002385 Poly* polys = this->pathToPolys(tolerance, clipBounds, contourCnt, &outerMesh);
2386 int64_t count64 = count_points(polys, overrideFillType);
Chris Dalton854ee852021-01-05 15:12:59 -07002387 if (fEmitCoverage) {
Greg Danield5b45932018-06-07 13:15:10 -04002388 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002389 }
Greg Danield5b45932018-06-07 13:15:10 -04002390 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002391 return 0;
2392 }
Greg Danield5b45932018-06-07 13:15:10 -04002393 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002394
Chris Dalton854ee852021-01-05 15:12:59 -07002395 size_t vertexStride = sizeof(SkPoint);
2396 if (fEmitCoverage) {
2397 vertexStride += sizeof(float);
2398 }
Chris Daltond081dce2020-01-23 12:09:04 -07002399 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002400 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002401 SkDebugf("Could not allocate vertices\n");
2402 return 0;
2403 }
senorblancof57372d2016-08-31 10:36:19 -07002404
Brian Salomon120e7d62019-09-11 10:29:22 -04002405 TESS_LOG("emitting %d verts\n", count);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002406 void* end = this->polysToTriangles(polys, verts, overrideFillType);
Brian Osman80879d42019-01-07 16:15:27 -05002407 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002408
senorblancof57372d2016-08-31 10:36:19 -07002409 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002410 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002411 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002412 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002413 return actualCount;
2414}
2415
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002416int GrTriangulator::PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
2417 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002418 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002419 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002420 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002421 return 0;
2422 }
Chris Dalton854ee852021-01-05 15:12:59 -07002423 GrTriangulator triangulator(path);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002424 Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, contourCnt, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002425 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002426 int64_t count64 = count_points(polys, fillType);
2427 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002428 *verts = nullptr;
2429 return 0;
2430 }
Greg Danield5b45932018-06-07 13:15:10 -04002431 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002432
Chris Dalton17dc4182020-03-25 16:18:16 -06002433 *verts = new WindingVertex[count];
2434 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002435 SkPoint* points = new SkPoint[count];
2436 SkPoint* pointsEnd = points;
2437 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002438 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002439 SkPoint* start = pointsEnd;
Chris Daltond60c9192021-01-07 18:30:08 +00002440 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002441 while (start != pointsEnd) {
2442 vertsEnd->fPos = *start;
2443 vertsEnd->fWinding = poly->fWinding;
2444 ++start;
2445 ++vertsEnd;
2446 }
2447 }
2448 }
2449 int actualCount = static_cast<int>(vertsEnd - *verts);
2450 SkASSERT(actualCount <= count);
2451 SkASSERT(pointsEnd - points == actualCount);
2452 delete[] points;
2453 return actualCount;
2454}