blob: 7d3b2a74fd6d96ed1d4d0ef27cb728306e9eb805 [file] [log] [blame]
ethannicholase9709e82016-01-07 13:34:16 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Chris Dalton17dc4182020-03-25 16:18:16 -06008#include "src/gpu/GrTriangulator.h"
ethannicholase9709e82016-01-07 13:34:16 -08009
Chris Daltond081dce2020-01-23 12:09:04 -070010#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040012#include "src/gpu/geometry/GrPathUtils.h"
ethannicholase9709e82016-01-07 13:34:16 -080013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkGeometry.h"
15#include "src/core/SkPointPriv.h"
ethannicholase9709e82016-01-07 13:34:16 -080016
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
ethannicholase9709e82016-01-07 13:34:16 -080023
24#define LOGGING_ENABLED 0
25
26#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -040027#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
36using Mode = GrTriangulator::Mode;
37using Vertex = GrTriangulator::Vertex;
38using VertexList = GrTriangulator::VertexList;
39using Edge = GrTriangulator::Edge;
40using EdgeList = GrTriangulator::EdgeList;
41using Poly = GrTriangulator::Poly;
42using Comparator = GrTriangulator::Comparator;
ethannicholase9709e82016-01-07 13:34:16 -080043
44template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070045static void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080046 t->*Prev = prev;
47 t->*Next = next;
48 if (prev) {
49 prev->*Next = t;
50 } else if (head) {
51 *head = t;
52 }
53 if (next) {
54 next->*Prev = t;
55 } else if (tail) {
56 *tail = t;
57 }
58}
59
60template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070061static void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080062 if (t->*Prev) {
63 t->*Prev->*Next = t->*Next;
64 } else if (head) {
65 *head = t->*Next;
66 }
67 if (t->*Next) {
68 t->*Next->*Prev = t->*Prev;
69 } else if (tail) {
70 *tail = t->*Prev;
71 }
72 t->*Prev = t->*Next = nullptr;
73}
74
75/**
76 * Vertices are used in three ways: first, the path contours are converted into a
77 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
78 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
79 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
80 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
81 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
82 * an individual Vertex from the path mesh may belong to multiple
83 * MonotonePolys, so the original Vertices cannot be re-used.
84 */
85
Chris Dalton57ea1fc2021-01-05 13:37:44 -070086struct GrTriangulator::Vertex {
senorblancof57372d2016-08-31 10:36:19 -070087 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -080088 : fPoint(point), fPrev(nullptr), fNext(nullptr)
89 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
90 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -040091 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -040092 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -070093 , fAlpha(alpha)
Stephen Whitec4dbc372019-05-22 10:50:14 -040094 , fSynthetic(false)
ethannicholase9709e82016-01-07 13:34:16 -080095#if LOGGING_ENABLED
96 , fID (-1.0f)
97#endif
98 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -040099 SkPoint fPoint; // Vertex position
100 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
101 Vertex* fNext; // "
102 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
103 Edge* fLastEdgeAbove; // "
104 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
105 Edge* fLastEdgeBelow; // "
106 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
107 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
108 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700109 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400110 bool fSynthetic; // Is this a synthetic vertex?
ethannicholase9709e82016-01-07 13:34:16 -0800111#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400112 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800113#endif
114};
115
116/***************************************************************************************/
117
118typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
119
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700120static bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500121 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800122}
123
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700124static bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500125 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800126}
127
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700128struct GrTriangulator::Comparator {
Stephen White16a40cb2017-02-23 11:10:01 -0500129 enum class Direction { kVertical, kHorizontal };
130 Comparator(Direction direction) : fDirection(direction) {}
131 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
132 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
133 }
Stephen White16a40cb2017-02-23 11:10:01 -0500134 Direction fDirection;
135};
136
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700137static inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500138 GrVertexWriter verts{data};
139 verts.write(v->fPoint);
140
Brian Osman80879d42019-01-07 16:15:27 -0500141 if (emitCoverage) {
142 verts.write(GrNormalizeByteToFloat(v->fAlpha));
143 }
Brian Osman0995fd52019-01-09 09:52:25 -0500144
Brian Osmanf9aabff2018-11-13 16:11:38 -0500145 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800146}
147
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700148static void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400149 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
150 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
151 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700152#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500153 data = emit_vertex(v0, emitCoverage, data);
154 data = emit_vertex(v1, emitCoverage, data);
155 data = emit_vertex(v1, emitCoverage, data);
156 data = emit_vertex(v2, emitCoverage, data);
157 data = emit_vertex(v2, emitCoverage, data);
158 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800159#else
Brian Osman0995fd52019-01-09 09:52:25 -0500160 data = emit_vertex(v0, emitCoverage, data);
161 data = emit_vertex(v1, emitCoverage, data);
162 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800163#endif
164 return data;
165}
166
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700167struct GrTriangulator::VertexList {
senorblancoe6eaa322016-03-08 09:06:44 -0800168 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500169 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800170 Vertex* fHead;
171 Vertex* fTail;
172 void insert(Vertex* v, Vertex* prev, Vertex* next) {
173 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
174 }
175 void append(Vertex* v) {
176 insert(v, fTail, nullptr);
177 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400178 void append(const VertexList& list) {
179 if (!list.fHead) {
180 return;
181 }
182 if (fTail) {
183 fTail->fNext = list.fHead;
184 list.fHead->fPrev = fTail;
185 } else {
186 fHead = list.fHead;
187 }
188 fTail = list.fTail;
189 }
senorblancoe6eaa322016-03-08 09:06:44 -0800190 void prepend(Vertex* v) {
191 insert(v, nullptr, fHead);
192 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500193 void remove(Vertex* v) {
194 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
195 }
senorblancof57372d2016-08-31 10:36:19 -0700196 void close() {
197 if (fHead && fTail) {
198 fTail->fNext = fHead;
199 fHead->fPrev = fTail;
200 }
201 }
senorblancoe6eaa322016-03-08 09:06:44 -0800202};
203
senorblancof57372d2016-08-31 10:36:19 -0700204// Round to nearest quarter-pixel. This is used for screenspace tessellation.
205
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700206static inline void round(SkPoint* p) {
senorblancof57372d2016-08-31 10:36:19 -0700207 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
208 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
209}
210
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700211static inline SkScalar double_to_clamped_scalar(double d) {
Stephen White94b7e542018-01-04 14:01:10 -0500212 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
213}
214
senorblanco49df8d12016-10-07 08:36:56 -0700215// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
216struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500217 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700218 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
219 Line(const SkPoint& p, const SkPoint& q)
220 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
221 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
222 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
223 static_cast<double>(p.fX) * q.fY) {}
224 double dist(const SkPoint& p) const {
225 return fA * p.fX + fB * p.fY + fC;
226 }
Stephen Whitee260c462017-12-19 18:09:54 -0500227 Line operator*(double v) const {
228 return Line(fA * v, fB * v, fC * v);
229 }
senorblanco49df8d12016-10-07 08:36:56 -0700230 double magSq() const {
231 return fA * fA + fB * fB;
232 }
Stephen Whitee260c462017-12-19 18:09:54 -0500233 void normalize() {
234 double len = sqrt(this->magSq());
235 if (len == 0.0) {
236 return;
237 }
238 double scale = 1.0f / len;
239 fA *= scale;
240 fB *= scale;
241 fC *= scale;
242 }
243 bool nearParallel(const Line& o) const {
244 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
245 }
senorblanco49df8d12016-10-07 08:36:56 -0700246
247 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500248 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700249 double denom = fA * other.fB - fB * other.fA;
250 if (denom == 0.0) {
251 return false;
252 }
Stephen White94b7e542018-01-04 14:01:10 -0500253 double scale = 1.0 / denom;
254 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
255 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500256 round(point);
Stephen White9b7f1432019-06-08 08:56:58 -0400257 return point->isFinite();
senorblanco49df8d12016-10-07 08:36:56 -0700258 }
259 double fA, fB, fC;
260};
261
ethannicholase9709e82016-01-07 13:34:16 -0800262/**
263 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
264 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
265 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whitec03e6982020-02-06 16:32:14 -0500266 * point). For speed, that case is only tested by the callers that require it (e.g.,
267 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
268 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
269 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
270 * a lot faster in the "not found" case.
ethannicholase9709e82016-01-07 13:34:16 -0800271 *
272 * The coefficients of the line equation stored in double precision to avoid catastrphic
273 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
274 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
275 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
276 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
277 * this file).
278 */
279
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700280struct GrTriangulator::Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500281 enum class Type { kInner, kOuter, kConnector };
282 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800283 : fWinding(winding)
284 , fTop(top)
285 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500286 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800287 , fLeft(nullptr)
288 , fRight(nullptr)
289 , fPrevEdgeAbove(nullptr)
290 , fNextEdgeAbove(nullptr)
291 , fPrevEdgeBelow(nullptr)
292 , fNextEdgeBelow(nullptr)
293 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700294 , fRightPoly(nullptr)
295 , fLeftPolyPrev(nullptr)
296 , fLeftPolyNext(nullptr)
297 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700298 , fRightPolyNext(nullptr)
299 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700300 , fUsedInRightPoly(false)
301 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800302 }
303 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
304 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
305 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500306 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800307 Edge* fLeft; // The linked list of edges in the active edge list.
308 Edge* fRight; // "
309 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
310 Edge* fNextEdgeAbove; // "
311 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
312 Edge* fNextEdgeBelow; // "
313 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
314 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700315 Edge* fLeftPolyPrev;
316 Edge* fLeftPolyNext;
317 Edge* fRightPolyPrev;
318 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700319 bool fUsedInLeftPoly;
320 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700321 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800322 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700323 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800324 }
325 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700326 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800327 }
328 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700329 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800330 }
331 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700332 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800333 }
Stephen White95152e12017-12-18 10:52:44 -0500334 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
Brian Salomon120e7d62019-09-11 10:29:22 -0400335 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
336 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800337 if (fTop == other.fTop || fBottom == other.fBottom) {
338 return false;
339 }
senorblanco49df8d12016-10-07 08:36:56 -0700340 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800341 if (denom == 0.0) {
342 return false;
343 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500344 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
345 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
346 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
347 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800348 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
349 // This saves us doing the divide below unless absolutely necessary.
350 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
351 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
352 return false;
353 }
354 double s = sNumer / denom;
355 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700356 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
357 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500358 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500359 if (fType == Type::kConnector) {
360 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
361 } else if (other.fType == Type::kConnector) {
362 double t = tNumer / denom;
363 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500364 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
365 *alpha = 0;
366 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500367 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500368 }
369 }
ethannicholase9709e82016-01-07 13:34:16 -0800370 return true;
371 }
senorblancof57372d2016-08-31 10:36:19 -0700372};
373
Stephen Whitec4dbc372019-05-22 10:50:14 -0400374struct SSEdge;
375
376struct SSVertex {
377 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
378 Vertex* fVertex;
379 SSEdge* fPrev;
380 SSEdge* fNext;
381};
382
383struct SSEdge {
384 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
385 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
386 }
387 Edge* fEdge;
388 Event* fEvent;
389 SSVertex* fPrev;
390 SSVertex* fNext;
391};
392
393typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
394typedef std::vector<SSEdge*> SSEdgeList;
395
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700396struct GrTriangulator::EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500397 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700398 Edge* fHead;
399 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700400 void insert(Edge* edge, Edge* prev, Edge* next) {
401 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700402 }
403 void append(Edge* e) {
404 insert(e, fTail, nullptr);
405 }
406 void remove(Edge* edge) {
407 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700408 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400409 void removeAll() {
410 while (fHead) {
411 this->remove(fHead);
412 }
413 }
senorblancof57372d2016-08-31 10:36:19 -0700414 void close() {
415 if (fHead && fTail) {
416 fTail->fRight = fHead;
417 fHead->fLeft = fTail;
418 }
419 }
420 bool contains(Edge* edge) const {
421 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800422 }
423};
424
Stephen Whitec4dbc372019-05-22 10:50:14 -0400425struct EventList;
426
Stephen Whitee260c462017-12-19 18:09:54 -0500427struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400428 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
429 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500430 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400431 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500432 SkPoint fPoint;
433 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400434 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500435};
436
Stephen Whitec4dbc372019-05-22 10:50:14 -0400437struct EventComparator {
438 enum class Op { kLessThan, kGreaterThan };
439 EventComparator(Op op) : fOp(op) {}
440 bool operator() (Event* const &e1, Event* const &e2) {
441 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
442 : e1->fAlpha > e2->fAlpha;
443 }
444 Op fOp;
445};
Stephen Whitee260c462017-12-19 18:09:54 -0500446
Stephen Whitec4dbc372019-05-22 10:50:14 -0400447typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500448
Stephen Whitec4dbc372019-05-22 10:50:14 -0400449struct EventList : EventPQ {
450 EventList(EventComparator comparison) : EventPQ(comparison) {
451 }
452};
453
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700454static void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400455 Vertex* prev = e->fPrev->fVertex;
456 Vertex* next = e->fNext->fVertex;
457 if (prev == next || !prev->fPartner || !next->fPartner) {
458 return;
459 }
460 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
461 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500462 SkPoint p;
463 uint8_t alpha;
464 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400465 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
466 "will collapse to %g,%g alpha %d\n",
467 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
468 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400469 e->fEvent = alloc.make<Event>(e, p, alpha);
470 events->push(e->fEvent);
471 }
472}
473
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700474static void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
475 Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400476 if (!v->fPartner) {
477 return;
478 }
Stephen White8a3c0592019-05-29 11:26:16 -0400479 Vertex* top = edge->fEdge->fTop;
480 Vertex* bottom = edge->fEdge->fBottom;
481 if (!top || !bottom ) {
482 return;
483 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400484 Line line = edge->fEdge->fLine;
485 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
486 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
487 SkPoint p;
488 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400489 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
490 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400491 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
492 "will collapse to %g,%g alpha %d\n",
493 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400494 edge->fEvent = alloc.make<Event>(edge, p, alpha);
495 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500496 }
497}
Stephen Whitee260c462017-12-19 18:09:54 -0500498
ethannicholase9709e82016-01-07 13:34:16 -0800499/***************************************************************************************/
500
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700501struct GrTriangulator::Poly {
senorblanco531237e2016-06-02 11:36:48 -0700502 Poly(Vertex* v, int winding)
503 : fFirstVertex(v)
504 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800505 , fHead(nullptr)
506 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800507 , fNext(nullptr)
508 , fPartner(nullptr)
509 , fCount(0)
510 {
511#if LOGGING_ENABLED
512 static int gID = 0;
513 fID = gID++;
Brian Salomon120e7d62019-09-11 10:29:22 -0400514 TESS_LOG("*** created Poly %d\n", fID);
ethannicholase9709e82016-01-07 13:34:16 -0800515#endif
516 }
senorblanco531237e2016-06-02 11:36:48 -0700517 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800518 struct MonotonePoly {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700519 MonotonePoly(Edge* edge, Side side, int winding)
senorblanco531237e2016-06-02 11:36:48 -0700520 : fSide(side)
521 , fFirstEdge(nullptr)
522 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800523 , fPrev(nullptr)
Chris Dalton022bd3b2020-01-24 13:48:53 -0700524 , fNext(nullptr)
525 , fWinding(winding) {
senorblanco531237e2016-06-02 11:36:48 -0700526 this->addEdge(edge);
527 }
ethannicholase9709e82016-01-07 13:34:16 -0800528 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700529 Edge* fFirstEdge;
530 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800531 MonotonePoly* fPrev;
532 MonotonePoly* fNext;
Chris Dalton022bd3b2020-01-24 13:48:53 -0700533 int fWinding;
senorblanco531237e2016-06-02 11:36:48 -0700534 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800535 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700536 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700537 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
538 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700539 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800540 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700541 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700542 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
543 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700544 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800545 }
ethannicholase9709e82016-01-07 13:34:16 -0800546 }
547
Brian Osman0995fd52019-01-09 09:52:25 -0500548 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700549 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700550 VertexList vertices;
551 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500552 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700553 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700554 if (kRight_Side == fSide) {
555 vertices.append(e->fBottom);
556 e = e->fRightPolyNext;
557 } else {
558 vertices.prepend(e->fBottom);
559 e = e->fLeftPolyNext;
560 }
Stephen White651cbe92017-03-03 12:24:16 -0500561 count++;
senorblanco531237e2016-06-02 11:36:48 -0700562 }
563 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800564 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700565 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800566 SkASSERT(v && v->fPrev && v->fNext);
567 Vertex* prev = v->fPrev;
568 Vertex* curr = v;
569 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500570 if (count == 3) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700571 return this->emitTriangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500572 }
ethannicholase9709e82016-01-07 13:34:16 -0800573 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
574 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
575 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
576 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
577 if (ax * by - ay * bx >= 0.0) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700578 data = this->emitTriangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800579 v->fPrev->fNext = v->fNext;
580 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500581 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800582 if (v->fPrev == first) {
583 v = v->fNext;
584 } else {
585 v = v->fPrev;
586 }
587 } else {
588 v = v->fNext;
589 }
590 }
591 return data;
592 }
Chris Dalton022bd3b2020-01-24 13:48:53 -0700593 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, bool emitCoverage,
594 void* data) const {
595 if (fWinding < 0) {
596 // Ensure our triangles always wind in the same direction as if the path had been
597 // triangulated as a simple fan (a la red book).
598 std::swap(prev, next);
599 }
600 return emit_triangle(next, curr, prev, emitCoverage, data);
601 }
ethannicholase9709e82016-01-07 13:34:16 -0800602 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500603 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400604 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
605 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800606 Poly* partner = fPartner;
607 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700608 if (side == kRight_Side) {
609 if (e->fUsedInRightPoly) {
610 return this;
611 }
612 } else {
613 if (e->fUsedInLeftPoly) {
614 return this;
615 }
616 }
ethannicholase9709e82016-01-07 13:34:16 -0800617 if (partner) {
618 fPartner = partner->fPartner = nullptr;
619 }
senorblanco531237e2016-06-02 11:36:48 -0700620 if (!fTail) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700621 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700622 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700623 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
624 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700625 } else if (side == fTail->fSide) {
626 fTail->addEdge(e);
627 fCount++;
628 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500629 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700630 fTail->addEdge(e);
631 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800632 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700633 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800634 poly = partner;
635 } else {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700636 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700637 m->fPrev = fTail;
638 fTail->fNext = m;
639 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800640 }
641 }
ethannicholase9709e82016-01-07 13:34:16 -0800642 return poly;
643 }
Brian Osman0995fd52019-01-09 09:52:25 -0500644 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800645 if (fCount < 3) {
646 return data;
647 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400648 TESS_LOG("emit() %d, size %d\n", fID, fCount);
ethannicholase9709e82016-01-07 13:34:16 -0800649 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500650 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800651 }
652 return data;
653 }
senorblanco531237e2016-06-02 11:36:48 -0700654 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
655 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800656 int fWinding;
657 MonotonePoly* fHead;
658 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800659 Poly* fNext;
660 Poly* fPartner;
661 int fCount;
662#if LOGGING_ENABLED
663 int fID;
664#endif
665};
666
667/***************************************************************************************/
668
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700669static bool coincident(const SkPoint& a, const SkPoint& b) {
ethannicholase9709e82016-01-07 13:34:16 -0800670 return a == b;
671}
672
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700673static Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500674 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800675 poly->fNext = *head;
676 *head = poly;
677 return poly;
678}
679
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700680void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) {
681 Vertex* v = fAlloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800682#if LOGGING_ENABLED
683 static float gID = 0.0f;
684 v->fID = gID++;
685#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500686 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800687}
688
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700689static SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
Stephen White36e4f062017-03-27 16:11:31 -0400690 SkQuadCoeff quad(pts);
691 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
692 SkPoint mid = to_point(quad.eval(t));
693 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400694 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
695 return 0;
696 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500697 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400698}
699
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700700void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd,
701 VertexList* contour) {
Stephen White36e4f062017-03-27 16:11:31 -0400702 SkQuadCoeff quad(pts);
703 Sk2s aa = quad.fA * quad.fA;
704 SkScalar denom = 2.0f * (aa[0] + aa[1]);
705 Sk2s ab = quad.fA * quad.fB;
706 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
707 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500708 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400709 // Test possible subdivision values only at the point of maximum curvature.
710 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500711 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400712 u = 1.0f / nPoints;
713 if (quad_error_at(pts, t, u) < toleranceSqd) {
714 break;
715 }
716 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800717 }
Stephen White36e4f062017-03-27 16:11:31 -0400718 for (int j = 1; j <= nPoints; j++) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700719 this->appendPointToContour(to_point(quad.eval(j * u)), contour);
Stephen White36e4f062017-03-27 16:11:31 -0400720 }
ethannicholase9709e82016-01-07 13:34:16 -0800721}
722
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700723void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2,
724 const SkPoint& p3, SkScalar tolSqd, VertexList* contour,
725 int pointsLeft) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500726 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
727 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800728 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
729 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700730 this->appendPointToContour(p3, contour);
Stephen White3a9aab92017-03-07 14:07:18 -0500731 return;
ethannicholase9709e82016-01-07 13:34:16 -0800732 }
733 const SkPoint q[] = {
734 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
735 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
736 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
737 };
738 const SkPoint r[] = {
739 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
740 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
741 };
742 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
743 pointsLeft >>= 1;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700744 this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft);
745 this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800746}
747
748// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
749
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700750void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds,
751 VertexList* contours) {
ethannicholase9709e82016-01-07 13:34:16 -0800752 SkScalar toleranceSqd = tolerance * tolerance;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700753 bool innerPolygons = (Mode::kSimpleInnerPolygons == fMode);
ethannicholase9709e82016-01-07 13:34:16 -0800754
Chris Dalton7156db22020-05-07 22:06:28 +0000755 SkPoint pts[4];
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700756 fIsLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500757 VertexList* contour = contours;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700758 SkPath::Iter iter(fPath, false);
759 if (fPath.isInverseFillType()) {
ethannicholase9709e82016-01-07 13:34:16 -0800760 SkPoint quad[4];
761 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700762 for (int i = 3; i >= 0; i--) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700763 this->appendPointToContour(quad[i], contours);
ethannicholase9709e82016-01-07 13:34:16 -0800764 }
Stephen White3a9aab92017-03-07 14:07:18 -0500765 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800766 }
767 SkAutoConicToQuads converter;
Chris Dalton7156db22020-05-07 22:06:28 +0000768 SkPath::Verb verb;
769 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800770 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +0000771 case SkPath::kConic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700772 fIsLinear = false;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700773 if (innerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700774 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700775 break;
776 }
Chris Dalton7156db22020-05-07 22:06:28 +0000777 SkScalar weight = iter.conicWeight();
778 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
ethannicholase9709e82016-01-07 13:34:16 -0800779 for (int i = 0; i < converter.countQuads(); ++i) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700780 this->appendQuadraticToContour(quadPts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800781 quadPts += 2;
782 }
ethannicholase9709e82016-01-07 13:34:16 -0800783 break;
784 }
Chris Dalton7156db22020-05-07 22:06:28 +0000785 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500786 if (contour->fHead) {
787 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800788 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700789 this->appendPointToContour(pts[0], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800790 break;
Chris Dalton7156db22020-05-07 22:06:28 +0000791 case SkPath::kLine_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700792 this->appendPointToContour(pts[1], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800793 break;
794 }
Chris Dalton7156db22020-05-07 22:06:28 +0000795 case SkPath::kQuad_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700796 fIsLinear = false;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700797 if (innerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700798 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700799 break;
800 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700801 this->appendQuadraticToContour(pts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800802 break;
803 }
Chris Dalton7156db22020-05-07 22:06:28 +0000804 case SkPath::kCubic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700805 fIsLinear = false;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700806 if (innerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700807 this->appendPointToContour(pts[3], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700808 break;
809 }
ethannicholase9709e82016-01-07 13:34:16 -0800810 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700811 this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
812 pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800813 break;
814 }
Chris Dalton7156db22020-05-07 22:06:28 +0000815 case SkPath::kClose_Verb:
816 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800817 break;
818 }
819 }
820}
821
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700822static inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800823 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500824 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800825 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500826 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800827 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500828 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700829 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500830 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800831 return (winding & 1) == 1;
832 default:
833 SkASSERT(false);
834 return false;
835 }
836}
837
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700838static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500839 return poly && apply_fill_type(fillType, poly->fWinding);
840}
841
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700842static Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c,
843 SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500844 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800845 Vertex* top = winding < 0 ? next : prev;
846 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500847 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800848}
849
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700850static void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400851 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700852 SkASSERT(edges->contains(edge));
853 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800854}
855
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700856static void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400857 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700858 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800859 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700860 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800861}
862
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700863static void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500864 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800865 *left = v->fFirstEdgeAbove->fLeft;
866 *right = v->fLastEdgeAbove->fRight;
867 return;
868 }
869 Edge* next = nullptr;
870 Edge* prev;
871 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
872 if (prev->isLeftOf(v)) {
873 break;
874 }
875 next = prev;
876 }
877 *left = prev;
878 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800879}
880
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700881static void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800882 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500883 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800884 return;
885 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400886 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
887 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800888 Edge* prev = nullptr;
889 Edge* next;
890 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
891 if (next->isRightOf(edge->fTop)) {
892 break;
893 }
894 prev = next;
895 }
senorblancoe6eaa322016-03-08 09:06:44 -0800896 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800897 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
898}
899
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700900static void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800901 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500902 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800903 return;
904 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400905 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
906 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800907 Edge* prev = nullptr;
908 Edge* next;
909 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
910 if (next->isRightOf(edge->fBottom)) {
911 break;
912 }
913 prev = next;
914 }
senorblancoe6eaa322016-03-08 09:06:44 -0800915 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800916 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
917}
918
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700919static void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400920 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400921 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
922 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800923 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800924 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
925}
926
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700927static void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400928 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400929 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
930 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800931 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800932 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
933}
934
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700935static void disconnect(Edge* edge)
Stephen Whitee7a364d2017-01-11 16:19:26 -0500936{
ethannicholase9709e82016-01-07 13:34:16 -0800937 remove_edge_above(edge);
938 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500939}
940
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700941static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
942 Comparator& c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400943
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700944static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400945 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
946 return;
947 }
948 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400949 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400950 while (v != dst) {
951 v = v->fPrev;
952 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
953 remove_edge(e, activeEdges);
954 }
955 Edge* leftEdge = v->fLeftEnclosingEdge;
956 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
957 insert_edge(e, leftEdge, activeEdges);
958 leftEdge = e;
Stephen Whitec03e6982020-02-06 16:32:14 -0500959 Vertex* top = e->fTop;
960 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
961 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
962 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
963 dst = top;
964 }
Stephen White3b5a3fa2017-06-06 14:51:19 -0400965 }
966 }
967 *current = v;
968}
969
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700970static void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current,
971 Comparator& c) {
Stephen Whitec03e6982020-02-06 16:32:14 -0500972 if (!activeEdges || !current) {
973 return;
974 }
975 Vertex* top = edge->fTop;
976 Vertex* bottom = edge->fBottom;
977 if (edge->fLeft) {
978 Vertex* leftTop = edge->fLeft->fTop;
979 Vertex* leftBottom = edge->fLeft->fBottom;
980 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
981 rewind(activeEdges, current, leftTop, c);
982 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
983 rewind(activeEdges, current, top, c);
984 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
985 !edge->fLeft->isLeftOf(bottom)) {
986 rewind(activeEdges, current, leftTop, c);
987 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
988 rewind(activeEdges, current, top, c);
989 }
990 }
991 if (edge->fRight) {
992 Vertex* rightTop = edge->fRight->fTop;
993 Vertex* rightBottom = edge->fRight->fBottom;
994 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
995 rewind(activeEdges, current, rightTop, c);
996 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
997 rewind(activeEdges, current, top, c);
998 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
999 !edge->fRight->isRightOf(bottom)) {
1000 rewind(activeEdges, current, rightTop, c);
1001 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1002 !edge->isLeftOf(rightBottom)) {
1003 rewind(activeEdges, current, top, c);
1004 }
1005 }
1006}
1007
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001008static void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001009 remove_edge_below(edge);
1010 edge->fTop = v;
1011 edge->recompute();
1012 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001013 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001014 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001015}
1016
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001017static void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
1018 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001019 remove_edge_above(edge);
1020 edge->fBottom = v;
1021 edge->recompute();
1022 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -05001023 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001024 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001025}
1026
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001027static void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1028 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001029 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001030 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1031 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1032 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001033 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001034 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001035 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001036 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001037 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001038 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001039 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001040 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001041 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001042 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001043 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001044 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001045 }
1046}
1047
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001048static void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1049 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001050 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001051 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1052 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1053 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001054 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001055 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001056 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001057 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001058 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001059 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001060 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001061 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001062 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001063 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001064 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001065 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001066 }
1067}
1068
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001069static bool top_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001070 if (!left || !right) {
1071 return false;
1072 }
1073 return left->fTop->fPoint == right->fTop->fPoint ||
1074 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1075}
1076
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001077static bool bottom_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001078 if (!left || !right) {
1079 return false;
1080 }
1081 return left->fBottom->fPoint == right->fBottom->fPoint ||
1082 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1083}
1084
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001085static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
1086 Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001087 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001088 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001089 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001090 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001091 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001092 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001093 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001094 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001095 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001096 } else {
1097 break;
1098 }
ethannicholase9709e82016-01-07 13:34:16 -08001099 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001100 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1101 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1102 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1103 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001104}
1105
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001106bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
1107 Comparator& c) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001108 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001109 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001110 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001111 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1112 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001113 Vertex* top;
1114 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001115 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001116 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001117 top = v;
1118 bottom = edge->fTop;
1119 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001120 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001121 top = edge->fBottom;
1122 bottom = v;
1123 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001124 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001125 top = v;
1126 bottom = edge->fBottom;
1127 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001128 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001129 Edge* newEdge = fAlloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001130 insert_edge_below(newEdge, top, c);
1131 insert_edge_above(newEdge, bottom, c);
1132 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001133 return true;
1134}
1135
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001136bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges,
1137 Vertex** current, Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001138 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1139 return false;
1140 }
Stephen White1c5fd182018-07-12 15:54:05 -04001141 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1142 return false;
1143 }
Stephen White89042d52018-06-08 12:18:22 -04001144 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1145 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001146 rewind(activeEdges, current, right->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001147 return this->splitEdge(left, right->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001148 }
1149 } else {
1150 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001151 rewind(activeEdges, current, left->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001152 return this->splitEdge(right, left->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001153 }
1154 }
1155 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1156 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001157 rewind(activeEdges, current, right->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001158 return this->splitEdge(left, right->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001159 }
1160 } else {
1161 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001162 rewind(activeEdges, current, left->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001163 return this->splitEdge(right, left->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001164 }
1165 }
1166 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001167}
1168
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001169static Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c,
1170 SkArenaAlloc& alloc, int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001171 if (!prev || !next || prev->fPoint == next->fPoint) {
1172 return nullptr;
1173 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001174 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001175 insert_edge_below(edge, edge->fTop, c);
1176 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001177 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001178 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001179 return edge;
1180}
1181
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001182static void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001183 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1184 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -05001185 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001186 if (src->fPartner) {
1187 src->fPartner->fPartner = dst;
1188 }
Stephen White7b376942018-05-22 11:51:32 -04001189 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001190 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001191 }
Stephen White7b376942018-05-22 11:51:32 -04001192 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001193 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001194 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001195 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001196 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001197}
1198
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001199static Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1200 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001201 Vertex* prevV = reference;
1202 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1203 prevV = prevV->fPrev;
1204 }
1205 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1206 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1207 prevV = nextV;
1208 nextV = nextV->fNext;
1209 }
1210 Vertex* v;
1211 if (prevV && coincident(prevV->fPoint, p)) {
1212 v = prevV;
1213 } else if (nextV && coincident(nextV->fPoint, p)) {
1214 v = nextV;
1215 } else {
1216 v = alloc.make<Vertex>(p, alpha);
1217#if LOGGING_ENABLED
1218 if (!prevV) {
1219 v->fID = mesh->fHead->fID - 1.0f;
1220 } else if (!nextV) {
1221 v->fID = mesh->fTail->fID + 1.0f;
1222 } else {
1223 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1224 }
1225#endif
1226 mesh->insert(v, prevV, nextV);
1227 }
1228 return v;
1229}
1230
Stephen White53a02982018-05-30 22:47:46 -04001231// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1232// sort criterion, it may not be possible to split correctly, since there is no point which is
1233// below the top and above the bottom. This function detects that case.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001234static bool nearly_flat(Comparator& c, Edge* edge) {
Stephen White53a02982018-05-30 22:47:46 -04001235 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1236 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001237 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001238}
1239
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001240static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
Stephen Whitee62999f2018-06-05 18:45:07 -04001241 if (c.sweep_lt(p, min)) {
1242 return min;
1243 } else if (c.sweep_lt(max, p)) {
1244 return max;
1245 } else {
1246 return p;
1247 }
1248}
1249
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001250static void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001251 Line line1 = edge1->fLine;
1252 Line line2 = edge2->fLine;
1253 line1.normalize();
1254 line2.normalize();
1255 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1256 if (cosAngle > 0.999) {
1257 return;
1258 }
1259 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1260 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1261 SkPoint p;
1262 if (line1.intersect(line2, &p)) {
1263 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1264 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001265 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 -04001266 }
1267}
1268
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001269bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges,
1270 Vertex** current, VertexList* mesh, Comparator& c) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001271 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001272 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001273 }
Stephen White56158ae2017-01-30 14:31:31 -05001274 SkPoint p;
1275 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001276 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001277 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001278 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001279 Vertex* top = *current;
1280 // If the intersection point is above the current vertex, rewind to the vertex above the
1281 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001282 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001283 top = top->fPrev;
1284 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001285 if (!nearly_flat(c, left)) {
1286 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001287 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001288 if (!nearly_flat(c, right)) {
1289 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001290 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001291 if (p == left->fTop->fPoint) {
1292 v = left->fTop;
1293 } else if (p == left->fBottom->fPoint) {
1294 v = left->fBottom;
1295 } else if (p == right->fTop->fPoint) {
1296 v = right->fTop;
1297 } else if (p == right->fBottom->fPoint) {
1298 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001299 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001300 v = create_sorted_vertex(p, alpha, mesh, top, c, fAlloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001301 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001302 v->fSynthetic = true;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001303 compute_bisector(left, right, v, fAlloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001304 }
ethannicholase9709e82016-01-07 13:34:16 -08001305 }
Stephen White0cb31672017-06-08 14:41:01 -04001306 rewind(activeEdges, current, top ? top : v, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001307 this->splitEdge(left, v, activeEdges, current, c);
1308 this->splitEdge(right, v, activeEdges, current, c);
Brian Osman788b9162020-02-07 10:36:46 -05001309 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001310 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001311 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001312 return this->intersectEdgePair(left, right, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001313}
1314
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001315void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) {
1316 bool approximate = (Mode::kEdgeAntialias == fMode);
1317 bool removeCollinearVertices = (Mode::kSimpleInnerPolygons != fMode);
Stephen White3a9aab92017-03-07 14:07:18 -05001318 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1319 SkASSERT(contour->fHead);
1320 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001321 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001322 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001323 }
Stephen White3a9aab92017-03-07 14:07:18 -05001324 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001325 if (approximate) {
1326 round(&v->fPoint);
1327 }
Stephen White3a9aab92017-03-07 14:07:18 -05001328 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001329 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001330 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001331 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001332 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001333 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001334 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001335 contour->remove(v);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001336 } else if (removeCollinearVertices &&
1337 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001338 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001339 contour->remove(v);
1340 } else {
1341 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001342 }
Stephen White3a9aab92017-03-07 14:07:18 -05001343 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001344 }
1345 }
1346}
1347
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001348bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001349 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001350 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001351 }
Stephen Whitee260c462017-12-19 18:09:54 -05001352 bool merged = false;
1353 for (Vertex* v = mesh->fHead->fNext; v;) {
1354 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001355 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1356 v->fPoint = v->fPrev->fPoint;
1357 }
1358 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001359 merge_vertices(v, v->fPrev, mesh, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001360 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001361 }
Stephen Whitee260c462017-12-19 18:09:54 -05001362 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001363 }
Stephen Whitee260c462017-12-19 18:09:54 -05001364 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001365}
1366
1367// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1368
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001369void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh,
1370 Comparator& c) {
Stephen White3a9aab92017-03-07 14:07:18 -05001371 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1372 Vertex* prev = contour->fTail;
1373 for (Vertex* v = contour->fHead; v;) {
1374 Vertex* next = v->fNext;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001375 connect(prev, v, Edge::Type::kInner, c, fAlloc);
Stephen White3a9aab92017-03-07 14:07:18 -05001376 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001377 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001378 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001379 }
1380 }
ethannicholase9709e82016-01-07 13:34:16 -08001381}
1382
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001383static void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitee260c462017-12-19 18:09:54 -05001384 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001385 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001386 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1387 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1388 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1389 // number.
1390 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1391 inner->fPartner = outer->fPartner = nullptr;
1392 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001393 }
1394 }
1395}
1396
1397template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001398static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001399 Vertex* a = front->fHead;
1400 Vertex* b = back->fHead;
1401 while (a && b) {
1402 if (sweep_lt(a->fPoint, b->fPoint)) {
1403 front->remove(a);
1404 result->append(a);
1405 a = front->fHead;
1406 } else {
1407 back->remove(b);
1408 result->append(b);
1409 b = back->fHead;
1410 }
1411 }
1412 result->append(*front);
1413 result->append(*back);
1414}
1415
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001416static void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001417 if (c.fDirection == Comparator::Direction::kHorizontal) {
1418 sorted_merge<sweep_lt_horiz>(front, back, result);
1419 } else {
1420 sorted_merge<sweep_lt_vert>(front, back, result);
1421 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001422#if LOGGING_ENABLED
1423 float id = 0.0f;
1424 for (Vertex* v = result->fHead; v; v = v->fNext) {
1425 v->fID = id++;
1426 }
1427#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001428}
1429
ethannicholase9709e82016-01-07 13:34:16 -08001430// Stage 3: sort the vertices by increasing sweep direction.
1431
Stephen White16a40cb2017-02-23 11:10:01 -05001432template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001433static void merge_sort(VertexList* vertices) {
Stephen White16a40cb2017-02-23 11:10:01 -05001434 Vertex* slow = vertices->fHead;
1435 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001436 return;
1437 }
Stephen White16a40cb2017-02-23 11:10:01 -05001438 Vertex* fast = slow->fNext;
1439 if (!fast) {
1440 return;
1441 }
1442 do {
1443 fast = fast->fNext;
1444 if (fast) {
1445 fast = fast->fNext;
1446 slow = slow->fNext;
1447 }
1448 } while (fast);
1449 VertexList front(vertices->fHead, slow);
1450 VertexList back(slow->fNext, vertices->fTail);
1451 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001452
Stephen White16a40cb2017-02-23 11:10:01 -05001453 merge_sort<sweep_lt>(&front);
1454 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001455
Stephen White16a40cb2017-02-23 11:10:01 -05001456 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001457 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001458}
1459
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001460static void dump_mesh(const VertexList& mesh) {
Stephen White95152e12017-12-18 10:52:44 -05001461#if LOGGING_ENABLED
1462 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001463 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 -05001464 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001465 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1466 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001467 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001468 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001469 }
1470 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001471 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001472 }
1473 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001474 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001475 }
1476 }
1477#endif
1478}
1479
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001480static void dump_skel(const SSEdgeList& ssEdges) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001481#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001482 for (SSEdge* edge : ssEdges) {
1483 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001484 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001485 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001486 edge->fNext->fVertex->fID);
1487 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001488 TESS_LOG(" (original %g -> %g)\n",
1489 edge->fEdge->fTop->fID,
1490 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001491 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001492 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001493 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001494 }
1495 }
1496#endif
1497}
1498
Stephen White89042d52018-06-08 12:18:22 -04001499#ifdef SK_DEBUG
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001500static void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001501 if (!left || !right) {
1502 return;
1503 }
1504 if (left->fTop == right->fTop) {
1505 SkASSERT(left->isLeftOf(right->fBottom));
1506 SkASSERT(right->isRightOf(left->fBottom));
1507 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1508 SkASSERT(left->isLeftOf(right->fTop));
1509 } else {
1510 SkASSERT(right->isRightOf(left->fTop));
1511 }
1512 if (left->fBottom == right->fBottom) {
1513 SkASSERT(left->isLeftOf(right->fTop));
1514 SkASSERT(right->isRightOf(left->fTop));
1515 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1516 SkASSERT(left->isLeftOf(right->fBottom));
1517 } else {
1518 SkASSERT(right->isRightOf(left->fBottom));
1519 }
1520}
1521
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001522static void validate_edge_list(EdgeList* edges, Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001523 Edge* left = edges->fHead;
1524 if (!left) {
1525 return;
1526 }
1527 for (Edge* right = left->fRight; right; right = right->fRight) {
1528 validate_edge_pair(left, right, c);
1529 left = right;
1530 }
1531}
1532#endif
1533
ethannicholase9709e82016-01-07 13:34:16 -08001534// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1535
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001536static bool connected(Vertex* v) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001537 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1538}
1539
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001540GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001541 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001542 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001543 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001544 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001545 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001546 continue;
1547 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001548 Edge* leftEnclosingEdge;
1549 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001550 bool restartChecks;
1551 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001552 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1553 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001554 restartChecks = false;
1555 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001556 v->fLeftEnclosingEdge = leftEnclosingEdge;
1557 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001558 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001559 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001560 if (this->checkForIntersection(
1561 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) ||
1562 this->checkForIntersection(
1563 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) {
1564 if (Mode::kSimpleInnerPolygons == fMode) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001565 return SimplifyResult::kAbort;
1566 }
1567 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001568 restartChecks = true;
1569 break;
1570 }
1571 }
1572 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001573 if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges,
1574 &v, mesh, c)) {
1575 if (Mode::kSimpleInnerPolygons == fMode) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001576 return SimplifyResult::kAbort;
1577 }
1578 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001579 restartChecks = true;
1580 }
1581
1582 }
1583 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001584#ifdef SK_DEBUG
1585 validate_edge_list(&activeEdges, c);
1586#endif
ethannicholase9709e82016-01-07 13:34:16 -08001587 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1588 remove_edge(e, &activeEdges);
1589 }
1590 Edge* leftEdge = leftEnclosingEdge;
1591 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1592 insert_edge(e, leftEdge, &activeEdges);
1593 leftEdge = e;
1594 }
ethannicholase9709e82016-01-07 13:34:16 -08001595 }
Stephen Whitee260c462017-12-19 18:09:54 -05001596 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001597 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001598}
1599
1600// Stage 5: Tessellate the simplified mesh into monotone polygons.
1601
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001602Poly* GrTriangulator::tessellate(const VertexList& vertices) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001603 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001604 int maxWindMagnitude = std::numeric_limits<int>::max();
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001605 if (Mode::kSimpleInnerPolygons == fMode && !SkPathFillType_IsEvenOdd(fPath.getFillType())) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001606 maxWindMagnitude = 1;
1607 }
ethannicholase9709e82016-01-07 13:34:16 -08001608 EdgeList activeEdges;
1609 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001610 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001611 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001612 continue;
1613 }
1614#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001615 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 -08001616#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001617 Edge* leftEnclosingEdge;
1618 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001619 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001620 Poly* leftPoly;
1621 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001622 if (v->fFirstEdgeAbove) {
1623 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1624 rightPoly = v->fLastEdgeAbove->fRightPoly;
1625 } else {
1626 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1627 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1628 }
1629#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001630 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001631 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001632 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1633 e->fTop->fID, e->fBottom->fID,
1634 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1635 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001636 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001637 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001638 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001639 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1640 e->fTop->fID, e->fBottom->fID,
1641 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1642 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001643 }
1644#endif
1645 if (v->fFirstEdgeAbove) {
1646 if (leftPoly) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001647 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001648 }
1649 if (rightPoly) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001650 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001651 }
1652 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001653 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001654 remove_edge(e, &activeEdges);
1655 if (e->fRightPoly) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001656 e->fRightPoly->addEdge(e, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001657 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001658 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001659 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001660 }
1661 }
1662 remove_edge(v->fLastEdgeAbove, &activeEdges);
1663 if (!v->fFirstEdgeBelow) {
1664 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1665 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1666 rightPoly->fPartner = leftPoly;
1667 leftPoly->fPartner = rightPoly;
1668 }
1669 }
1670 }
1671 if (v->fFirstEdgeBelow) {
1672 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001673 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001674 if (leftPoly == rightPoly) {
1675 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1676 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001677 leftPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001678 leftEnclosingEdge->fRightPoly = leftPoly;
1679 } else {
1680 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001681 rightPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001682 rightEnclosingEdge->fLeftPoly = rightPoly;
1683 }
ethannicholase9709e82016-01-07 13:34:16 -08001684 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001685 Edge* join = fAlloc.make<Edge>(leftPoly->lastVertex(), v, 1,
1686 Edge::Type::kInner);
1687 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, fAlloc);
1688 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001689 }
1690 }
1691 Edge* leftEdge = v->fFirstEdgeBelow;
1692 leftEdge->fLeftPoly = leftPoly;
1693 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1694 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1695 rightEdge = rightEdge->fNextEdgeBelow) {
1696 insert_edge(rightEdge, leftEdge, &activeEdges);
1697 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1698 winding += leftEdge->fWinding;
1699 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001700 if (abs(winding) > maxWindMagnitude) {
1701 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1702 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001703 Poly* poly = new_poly(&polys, v, winding, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001704 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1705 }
1706 leftEdge = rightEdge;
1707 }
1708 v->fLastEdgeBelow->fRightPoly = rightPoly;
1709 }
1710#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001711 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001712 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001713 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1714 e->fTop->fID, e->fBottom->fID,
1715 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1716 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001717 }
1718#endif
1719 }
1720 return polys;
1721}
1722
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001723static void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
1724 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001725 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001726 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001727 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001728 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001729 continue;
1730 }
1731 Edge* leftEnclosingEdge;
1732 Edge* rightEnclosingEdge;
1733 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1734 bool prevFilled = leftEnclosingEdge &&
1735 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1736 for (Edge* e = v->fFirstEdgeAbove; e;) {
1737 Edge* next = e->fNextEdgeAbove;
1738 remove_edge(e, &activeEdges);
1739 bool filled = apply_fill_type(fillType, e->fWinding);
1740 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001741 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001742 }
Stephen White49789062017-02-21 10:35:49 -05001743 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001744 e = next;
1745 }
Stephen White49789062017-02-21 10:35:49 -05001746 Edge* prev = leftEnclosingEdge;
1747 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1748 if (prev) {
1749 e->fWinding += prev->fWinding;
1750 }
1751 insert_edge(e, prev, &activeEdges);
1752 prev = e;
1753 }
senorblancof57372d2016-08-31 10:36:19 -07001754 }
senorblancof57372d2016-08-31 10:36:19 -07001755}
1756
Stephen White66412122017-03-01 11:48:27 -05001757// Note: this is the normal to the edge, but not necessarily unit length.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001758static void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001759 normal->set(SkDoubleToScalar(e->fLine.fA),
1760 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001761}
1762
1763// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1764// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1765// invert on stroking.
1766
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001767static void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001768 Edge* prevEdge = boundary->fTail;
1769 SkVector prevNormal;
1770 get_edge_normal(prevEdge, &prevNormal);
1771 for (Edge* e = boundary->fHead; e != nullptr;) {
1772 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1773 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001774 double distPrev = e->dist(prev->fPoint);
1775 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001776 SkVector normal;
1777 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001778 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001779 if (prev == next) {
1780 remove_edge(prevEdge, boundary);
1781 remove_edge(e, boundary);
1782 prevEdge = boundary->fTail;
1783 e = boundary->fHead;
1784 if (prevEdge) {
1785 get_edge_normal(prevEdge, &prevNormal);
1786 }
1787 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001788 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001789 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001790 if (prev->fPoint != next->fPoint) {
1791 join->fLine.normalize();
1792 join->fLine = join->fLine * join->fWinding;
1793 }
senorblancof57372d2016-08-31 10:36:19 -07001794 insert_edge(join, e, boundary);
1795 remove_edge(prevEdge, boundary);
1796 remove_edge(e, boundary);
1797 if (join->fLeft && join->fRight) {
1798 prevEdge = join->fLeft;
1799 e = join;
1800 } else {
1801 prevEdge = boundary->fTail;
1802 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1803 }
1804 get_edge_normal(prevEdge, &prevNormal);
1805 } else {
1806 prevEdge = e;
1807 prevNormal = normal;
1808 e = e->fRight;
1809 }
1810 }
1811}
1812
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001813static void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001814 if (v == dest) {
1815 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001816 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001817 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001818 if (v->fSynthetic) {
1819 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1820 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001821 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1822 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001823 v->fPartner->fPartner = dest;
1824 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001825 }
1826}
1827
Stephen Whitec4dbc372019-05-22 10:50:14 -04001828void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1829 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001830 return;
1831 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001832 Vertex* prev = fEdge->fPrev->fVertex;
1833 Vertex* next = fEdge->fNext->fVertex;
1834 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1835 SSEdge* nextEdge = fEdge->fNext->fNext;
1836 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1837 return;
Stephen White77169c82018-06-05 09:15:59 -04001838 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001839 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1840 dest->fSynthetic = true;
1841 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001842 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1843 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1844 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001845 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001846
Stephen Whitec4dbc372019-05-22 10:50:14 -04001847 ss_connect(prev, dest, c, alloc);
1848 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001849
Stephen Whitec4dbc372019-05-22 10:50:14 -04001850 prevEdge->fNext = nextEdge->fPrev = ssv;
1851 ssv->fPrev = prevEdge;
1852 ssv->fNext = nextEdge;
1853 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1854 return;
1855 }
1856 if (prevEdge->fEvent) {
1857 prevEdge->fEvent->fEdge = nullptr;
1858 }
1859 if (nextEdge->fEvent) {
1860 nextEdge->fEvent->fEdge = nullptr;
1861 }
1862 if (prevEdge->fPrev == nextEdge->fNext) {
1863 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1864 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1865 } else {
1866 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1867 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1868 if (dest->fPartner) {
1869 create_event(prevEdge, events, alloc);
1870 create_event(nextEdge, events, alloc);
1871 } else {
1872 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1873 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1874 }
1875 }
Stephen Whitee260c462017-12-19 18:09:54 -05001876}
1877
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001878static bool is_overlap_edge(Edge* e) {
Stephen Whitee260c462017-12-19 18:09:54 -05001879 if (e->fType == Edge::Type::kOuter) {
1880 return e->fWinding != 0 && e->fWinding != 1;
1881 } else if (e->fType == Edge::Type::kInner) {
1882 return e->fWinding != 0 && e->fWinding != -2;
1883 } else {
1884 return false;
1885 }
1886}
1887
1888// This is a stripped-down version of tessellate() which computes edges which
1889// join two filled regions, which represent overlap regions, and collapses them.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001890static bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1891 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001892 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001893 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001894 EventList events(comp);
1895 SSVertexMap ssVertices;
1896 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001897 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001898 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001899 continue;
1900 }
1901 Edge* leftEnclosingEdge;
1902 Edge* rightEnclosingEdge;
1903 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001904 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001905 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1906 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001907 bool leftOverlap = prev && is_overlap_edge(prev);
1908 bool rightOverlap = is_overlap_edge(e);
1909 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1910 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001911 if (prev) {
1912 e->fWinding -= prev->fWinding;
1913 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001914 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001915 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1916 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001917 disconnect(e);
1918 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001919 TESS_LOG("found overlap edge %g -> %g%s\n",
1920 e->fTop->fID, e->fBottom->fID,
1921 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001922 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1923 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1924 SSVertex* ssPrev = ssVertices[prevVertex];
1925 if (!ssPrev) {
1926 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1927 }
1928 SSVertex* ssNext = ssVertices[nextVertex];
1929 if (!ssNext) {
1930 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1931 }
1932 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1933 ssEdges.push_back(ssEdge);
1934// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1935 ssPrev->fNext = ssNext->fPrev = ssEdge;
1936 create_event(ssEdge, &events, alloc);
1937 if (!isOuterBoundary) {
1938 disconnect(e);
1939 }
1940 }
1941 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001942 }
1943 Edge* prev = leftEnclosingEdge;
1944 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1945 if (prev) {
1946 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001947 }
1948 insert_edge(e, prev, &activeEdges);
1949 prev = e;
1950 }
1951 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001952 bool complex = events.size() > 0;
1953
Brian Salomon120e7d62019-09-11 10:29:22 -04001954 TESS_LOG("\ncollapsing overlap regions\n");
1955 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001956 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001957 while (events.size() > 0) {
1958 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001959 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001960 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001961 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001962 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001963 dump_skel(ssEdges);
1964 for (SSEdge* edge : ssEdges) {
1965 if (Edge* e = edge->fEdge) {
1966 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1967 }
1968 }
1969 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001970}
1971
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001972static bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001973 if (!prev || !next) {
1974 return true;
1975 }
1976 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1977 return winding != origEdge->fWinding;
1978}
Stephen White92eba8a2017-02-06 09:50:27 -05001979
senorblancof57372d2016-08-31 10:36:19 -07001980// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1981// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1982// new antialiased mesh from those vertices.
1983
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001984static void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1985 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001986 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001987 // A boundary with fewer than 3 edges is degenerate.
1988 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1989 return;
1990 }
1991 Edge* prevEdge = boundary->fTail;
1992 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1993 SkVector prevNormal;
1994 get_edge_normal(prevEdge, &prevNormal);
1995 double radius = 0.5;
1996 Line prevInner(prevEdge->fLine);
1997 prevInner.fC -= radius;
1998 Line prevOuter(prevEdge->fLine);
1999 prevOuter.fC += radius;
2000 VertexList innerVertices;
2001 VertexList outerVertices;
2002 bool innerInversion = true;
2003 bool outerInversion = true;
2004 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
2005 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2006 SkVector normal;
2007 get_edge_normal(e, &normal);
2008 Line inner(e->fLine);
2009 inner.fC -= radius;
2010 Line outer(e->fLine);
2011 outer.fC += radius;
2012 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04002013 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002014 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2015 prevOuter.intersect(outer, &outerPoint)) {
2016 float cosAngle = normal.dot(prevNormal);
2017 if (cosAngle < -kCosMiterAngle) {
2018 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2019
2020 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2021 Line bisector(innerPoint, outerPoint);
2022 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2023 if (tangent.fA == 0 && tangent.fB == 0) {
2024 continue;
2025 }
2026 tangent.normalize();
2027 Line innerTangent(tangent);
2028 Line outerTangent(tangent);
2029 innerTangent.fC -= 0.5;
2030 outerTangent.fC += 0.5;
2031 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2032 if (prevNormal.cross(normal) > 0) {
2033 // Miter inner points
2034 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2035 !innerTangent.intersect(inner, &innerPoint2) ||
2036 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002037 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002038 }
2039 Line prevTangent(prevV->fPoint,
2040 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2041 Line nextTangent(nextV->fPoint,
2042 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002043 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002044 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002045 }
2046 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002047 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002048 }
2049 outerPoint1 = outerPoint2 = outerPoint;
2050 } else {
2051 // Miter outer points
2052 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2053 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002054 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002055 }
2056 Line prevTangent(prevV->fPoint,
2057 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2058 Line nextTangent(nextV->fPoint,
2059 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002060 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002061 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002062 }
2063 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002064 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002065 }
2066 innerPoint1 = innerPoint2 = innerPoint;
2067 }
Stephen Whiteea495232018-04-03 11:28:15 -04002068 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2069 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2070 continue;
2071 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002072 TESS_LOG("inner (%g, %g), (%g, %g), ",
2073 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2074 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2075 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002076 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2077 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2078 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2079 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2080 innerVertex1->fPartner = outerVertex1;
2081 innerVertex2->fPartner = outerVertex2;
2082 outerVertex1->fPartner = innerVertex1;
2083 outerVertex2->fPartner = innerVertex2;
2084 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2085 innerInversion = false;
2086 }
2087 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2088 outerInversion = false;
2089 }
2090 innerVertices.append(innerVertex1);
2091 innerVertices.append(innerVertex2);
2092 outerVertices.append(outerVertex1);
2093 outerVertices.append(outerVertex2);
2094 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002095 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2096 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002097 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2098 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2099 innerVertex->fPartner = outerVertex;
2100 outerVertex->fPartner = innerVertex;
2101 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2102 innerInversion = false;
2103 }
2104 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2105 outerInversion = false;
2106 }
2107 innerVertices.append(innerVertex);
2108 outerVertices.append(outerVertex);
2109 }
2110 }
2111 prevInner = inner;
2112 prevOuter = outer;
2113 prevV = v;
2114 prevEdge = e;
2115 prevNormal = normal;
2116 }
2117 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2118 innerInversion = false;
2119 }
2120 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2121 outerInversion = false;
2122 }
2123 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2124 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2125 // interior inverts).
2126 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2127 // so it will be detected during collapse_overlap_regions().
2128 int innerWinding = innerInversion ? 2 : -2;
2129 int outerWinding = outerInversion ? -1 : 1;
2130 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2131 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2132 }
2133 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2134 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2135 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2136 }
2137 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2138 innerMesh->append(innerVertices);
2139 outerMesh->append(outerVertices);
2140}
senorblancof57372d2016-08-31 10:36:19 -07002141
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002142static void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType,
2143 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002144 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002145 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002146 Vertex* start = down ? e->fTop : e->fBottom;
2147 do {
senorblancof57372d2016-08-31 10:36:19 -07002148 e->fWinding = down ? 1 : -1;
2149 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002150 e->fLine.normalize();
2151 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002152 boundary->append(e);
2153 if (down) {
2154 // Find outgoing edge, in clockwise order.
2155 if ((next = e->fNextEdgeAbove)) {
2156 down = false;
2157 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2158 down = true;
2159 } else if ((next = e->fPrevEdgeAbove)) {
2160 down = false;
2161 }
2162 } else {
2163 // Find outgoing edge, in counter-clockwise order.
2164 if ((next = e->fPrevEdgeBelow)) {
2165 down = true;
2166 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2167 down = false;
2168 } else if ((next = e->fNextEdgeBelow)) {
2169 down = true;
2170 }
2171 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002172 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002173 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002174 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002175}
2176
Stephen White5ad721e2017-02-23 16:50:47 -05002177// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002178
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002179static void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
2180 VertexList* outerVertices, SkPathFillType fillType, Comparator& c,
2181 SkArenaAlloc& alloc) {
Stephen White5ad721e2017-02-23 16:50:47 -05002182 remove_non_boundary_edges(inMesh, fillType, alloc);
2183 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002184 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002185 EdgeList boundary;
2186 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2187 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002188 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002189 }
2190 }
senorblancof57372d2016-08-31 10:36:19 -07002191}
2192
Stephen Whitebda29c02017-03-13 15:10:13 -04002193// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002194
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002195void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh,
2196 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08002197#if LOGGING_ENABLED
2198 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002199 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002200 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002201 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002202 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002203 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002204 }
2205 }
2206#endif
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002207 this->sanitizeContours(contours, contourCnt);
2208 this->buildEdges(contours, contourCnt, mesh, c);
senorblancof57372d2016-08-31 10:36:19 -07002209}
2210
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002211void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002212 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002213 return;
ethannicholase9709e82016-01-07 13:34:16 -08002214 }
2215
2216 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002217 if (c.fDirection == Comparator::Direction::kHorizontal) {
2218 merge_sort<sweep_lt_horiz>(vertices);
2219 } else {
2220 merge_sort<sweep_lt_vert>(vertices);
2221 }
ethannicholase9709e82016-01-07 13:34:16 -08002222#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002223 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002224 static float gID = 0.0f;
2225 v->fID = gID++;
2226 }
2227#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002228}
2229
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002230Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt, VertexList* outerMesh) {
2231 const SkRect& pathBounds = fPath.getBounds();
Stephen White16a40cb2017-02-23 11:10:01 -05002232 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2233 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002234 VertexList mesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002235 this->contoursToMesh(contours, contourCnt, &mesh, c);
2236 SortMesh(&mesh, c);
2237 this->mergeCoincidentVertices(&mesh, c);
2238 if (SimplifyResult::kAbort == this->simplify(&mesh, c)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07002239 return nullptr;
2240 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002241 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002242 dump_mesh(mesh);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002243 if (Mode::kEdgeAntialias == fMode) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002244 VertexList innerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002245 extract_boundaries(mesh, &innerMesh, outerMesh, fPath.getFillType(), c, fAlloc);
2246 SortMesh(&innerMesh, c);
2247 SortMesh(outerMesh, c);
2248 this->mergeCoincidentVertices(&innerMesh, c);
2249 bool was_complex = this->mergeCoincidentVertices(outerMesh, c);
2250 auto result = this->simplify(&innerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002251 SkASSERT(SimplifyResult::kAbort != result);
2252 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002253 result = this->simplify(outerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002254 SkASSERT(SimplifyResult::kAbort != result);
2255 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002256 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002257 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002258 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002259 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002260 EventComparator eventLT(EventComparator::Op::kLessThan);
2261 EventComparator eventGT(EventComparator::Op::kGreaterThan);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002262 was_complex = collapse_overlap_regions(&innerMesh, c, fAlloc, eventLT) || was_complex;
2263 was_complex = collapse_overlap_regions(outerMesh, c, fAlloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002264 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002265 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002266 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002267 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002268 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002269 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002270 dump_mesh(*outerMesh);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002271 connect_partners(outerMesh, c, fAlloc);
2272 connect_partners(&innerMesh, c, fAlloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002273 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002274 this->mergeCoincidentVertices(&aaMesh, c);
2275 result = this->simplify(&aaMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002276 SkASSERT(SimplifyResult::kAbort != result);
Brian Salomon120e7d62019-09-11 10:29:22 -04002277 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002278 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002279 outerMesh->fHead = outerMesh->fTail = nullptr;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002280 return this->tessellate(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002281 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002282 TESS_LOG("no complex polygons; taking fast path\n");
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002283 return this->tessellate(innerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002284 }
Stephen White49789062017-02-21 10:35:49 -05002285 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002286 return this->tessellate(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002287 }
senorblancof57372d2016-08-31 10:36:19 -07002288}
2289
2290// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002291void* GrTriangulator::polysToTriangles(Poly* polys, void* data, SkPathFillType overrideFillType) {
2292 bool emitCoverage = (Mode::kEdgeAntialias == fMode);
senorblancof57372d2016-08-31 10:36:19 -07002293 for (Poly* poly = polys; poly; poly = poly->fNext) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002294 if (apply_fill_type(overrideFillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002295 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002296 }
2297 }
2298 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002299}
2300
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002301Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt,
2302 VertexList* outerMesh) {
2303 if (SkPathFillType_IsInverse(fPath.getFillType())) {
ethannicholase9709e82016-01-07 13:34:16 -08002304 contourCnt++;
2305 }
Stephen White3a9aab92017-03-07 14:07:18 -05002306 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002307
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002308 this->pathToContours(tolerance, clipBounds, contours.get());
2309 return this->contoursToPolys(contours.get(), contourCnt, outerMesh);
ethannicholase9709e82016-01-07 13:34:16 -08002310}
2311
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002312static int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002313 // We could theoretically be more aggressive about not counting empty contours, but we need to
2314 // actually match the exact number of contour linked lists the tessellator will create later on.
2315 int contourCnt = 1;
2316 bool hasPoints = false;
2317
Chris Dalton7156db22020-05-07 22:06:28 +00002318 SkPath::Iter iter(path, false);
2319 SkPath::Verb verb;
2320 SkPoint pts[4];
Chris Daltonc71b3d42020-01-08 21:29:59 -07002321 bool first = true;
Chris Dalton7156db22020-05-07 22:06:28 +00002322 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002323 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +00002324 case SkPath::kMove_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002325 if (!first) {
2326 ++contourCnt;
2327 }
John Stiles30212b72020-06-11 17:55:07 -04002328 [[fallthrough]];
Chris Dalton7156db22020-05-07 22:06:28 +00002329 case SkPath::kLine_Verb:
2330 case SkPath::kConic_Verb:
2331 case SkPath::kQuad_Verb:
2332 case SkPath::kCubic_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002333 hasPoints = true;
John Stiles30212b72020-06-11 17:55:07 -04002334 break;
Chris Daltonc71b3d42020-01-08 21:29:59 -07002335 default:
2336 break;
2337 }
2338 first = false;
2339 }
2340 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002341 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002342 }
Stephen White11f65e02017-02-16 19:00:39 -05002343 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002344}
2345
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002346static int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002347 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002348 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002349 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002350 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
ethannicholase9709e82016-01-07 13:34:16 -08002351 }
2352 }
2353 return count;
2354}
2355
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002356static int64_t count_outer_mesh_points(const VertexList& outerMesh) {
Greg Danield5b45932018-06-07 13:15:10 -04002357 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002358 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2359 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002360 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002361 }
2362 }
2363 return count;
2364}
2365
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002366static void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002367 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2368 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2369 Vertex* v0 = e->fTop;
2370 Vertex* v1 = e->fBottom;
2371 Vertex* v2 = e->fBottom->fPartner;
2372 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002373 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2374 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002375 }
2376 }
2377 return data;
2378}
2379
ethannicholase9709e82016-01-07 13:34:16 -08002380// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2381
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002382int GrTriangulator::PathToTriangles(const SkPath& path, SkScalar tolerance,
2383 const SkRect& clipBounds,
2384 GrEagerVertexAllocator* vertexAllocator, Mode mode,
2385 bool* isLinear) {
2386 GrTriangulator triangulator(path, mode);
2387 SkPathFillType overrideFillType = (GrTriangulator::Mode::kEdgeAntialias == mode)
2388 ? SkPathFillType::kWinding
2389 : path.getFillType();
2390 int count = triangulator.pathToTriangles(tolerance, clipBounds, vertexAllocator,
2391 overrideFillType);
2392 *isLinear = triangulator.fIsLinear;
2393 return count;
2394}
2395
2396int GrTriangulator::pathToTriangles(float tolerance, const SkRect& clipBounds,
2397 GrEagerVertexAllocator* vertexAllocator,
2398 SkPathFillType overrideFillType) {
2399 int contourCnt = get_contour_count(fPath, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002400 if (contourCnt <= 0) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002401 fIsLinear = true;
ethannicholase9709e82016-01-07 13:34:16 -08002402 return 0;
2403 }
Stephen Whitebda29c02017-03-13 15:10:13 -04002404 VertexList outerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002405 Poly* polys = this->pathToPolys(tolerance, clipBounds, contourCnt, &outerMesh);
2406 int64_t count64 = count_points(polys, overrideFillType);
2407 if (GrTriangulator::Mode::kEdgeAntialias == fMode) {
Greg Danield5b45932018-06-07 13:15:10 -04002408 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002409 }
Greg Danield5b45932018-06-07 13:15:10 -04002410 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002411 return 0;
2412 }
Greg Danield5b45932018-06-07 13:15:10 -04002413 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002414
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002415 size_t vertexStride = GetVertexStride(fMode);
Chris Daltond081dce2020-01-23 12:09:04 -07002416 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002417 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002418 SkDebugf("Could not allocate vertices\n");
2419 return 0;
2420 }
senorblancof57372d2016-08-31 10:36:19 -07002421
Brian Salomon120e7d62019-09-11 10:29:22 -04002422 TESS_LOG("emitting %d verts\n", count);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002423 void* end = this->polysToTriangles(polys, verts, overrideFillType);
Brian Osman80879d42019-01-07 16:15:27 -05002424 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002425
senorblancof57372d2016-08-31 10:36:19 -07002426 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002427 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002428 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002429 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002430 return actualCount;
2431}
2432
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002433int GrTriangulator::PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
2434 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002435 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002436 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002437 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002438 return 0;
2439 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002440 GrTriangulator triangulator(path, Mode::kNormal);
2441 Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, contourCnt, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002442 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002443 int64_t count64 = count_points(polys, fillType);
2444 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002445 *verts = nullptr;
2446 return 0;
2447 }
Greg Danield5b45932018-06-07 13:15:10 -04002448 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002449
Chris Dalton17dc4182020-03-25 16:18:16 -06002450 *verts = new WindingVertex[count];
2451 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002452 SkPoint* points = new SkPoint[count];
2453 SkPoint* pointsEnd = points;
2454 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002455 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002456 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002457 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002458 while (start != pointsEnd) {
2459 vertsEnd->fPos = *start;
2460 vertsEnd->fWinding = poly->fWinding;
2461 ++start;
2462 ++vertsEnd;
2463 }
2464 }
2465 }
2466 int actualCount = static_cast<int>(vertsEnd - *verts);
2467 SkASSERT(actualCount <= count);
2468 SkASSERT(pointsEnd - points == actualCount);
2469 delete[] points;
2470 return actualCount;
2471}