blob: e387a063a1bf5dfef2d37ebf03c1472f6109378c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrTessellator.h"
ethannicholase9709e82016-01-07 13:34:16 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrDefaultGeoProcFactory.h"
Chris Daltond081dce2020-01-23 12:09:04 -070011#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040013#include "src/gpu/geometry/GrPathUtils.h"
ethannicholase9709e82016-01-07 13:34:16 -080014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPath.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040016#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkGeometry.h"
18#include "src/core/SkPointPriv.h"
ethannicholase9709e82016-01-07 13:34:16 -080019
Stephen White94b7e542018-01-04 14:01:10 -050020#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040021#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040022#include <queue>
23#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040024#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080025
26/*
senorblancof57372d2016-08-31 10:36:19 -070027 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080028 *
29 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
30 * 2) Build a mesh of edges connecting the vertices (build_edges()).
31 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
32 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
33 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
34 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
35 *
senorblancof57372d2016-08-31 10:36:19 -070036 * For screenspace antialiasing, the algorithm is modified as follows:
37 *
38 * Run steps 1-5 above to produce polygons.
39 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040040 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070041 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
42 * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
Stephen Whitebda29c02017-03-13 15:10:13 -040043 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070044 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
45 *
ethannicholase9709e82016-01-07 13:34:16 -080046 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
47 * of vertices (and the necessity of inserting new vertices on intersection).
48 *
Stephen Whitebda29c02017-03-13 15:10:13 -040049 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080050 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
51 * left-to-right based on the point where both edges are active (when both top vertices
52 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
53 * (shared), it's sorted based on the last point where both edges are active, so the
54 * "upper" bottom vertex.
55 *
56 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
57 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
58 * not exact and may violate the mesh topology or active edge list ordering. We
59 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040060 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080061 *
62 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
63 * neighbouring edges at the top or bottom vertex. This is handled by merging the
64 * edges (merge_collinear_edges()).
65 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen Whiteb67b2352019-06-01 13:07:27 -040066 * active edge list. This is handled during merging or splitting by rewind()ing the
67 * active edge list to the vertex before potential violations occur.
ethannicholase9709e82016-01-07 13:34:16 -080068 *
69 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
70 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
71 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
72 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
73 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
74 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
75 * linked list implementation. With the latter, all removals are O(1), and most insertions
76 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
77 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
78 * frequent. There may be other data structures worth investigating, however.
79 *
80 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
81 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
82 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
83 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
84 * that the "left" and "right" orientation in the code remains correct (edges to the left are
85 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
86 * degrees counterclockwise, rather that transposing.
87 */
88
89#define LOGGING_ENABLED 0
90
91#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -040092#define TESS_LOG printf
ethannicholase9709e82016-01-07 13:34:16 -080093#else
Brian Salomon120e7d62019-09-11 10:29:22 -040094#define TESS_LOG(...)
ethannicholase9709e82016-01-07 13:34:16 -080095#endif
96
ethannicholase9709e82016-01-07 13:34:16 -080097namespace {
98
Chris Daltondcc8c542020-01-28 17:55:56 -070099using GrTessellator::Mode;
100
Stephen White11f65e02017-02-16 19:00:39 -0500101const int kArenaChunkSize = 16 * 1024;
Stephen Whitee260c462017-12-19 18:09:54 -0500102const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
Stephen White11f65e02017-02-16 19:00:39 -0500103
ethannicholase9709e82016-01-07 13:34:16 -0800104struct Vertex;
105struct Edge;
Stephen Whitee260c462017-12-19 18:09:54 -0500106struct Event;
ethannicholase9709e82016-01-07 13:34:16 -0800107struct Poly;
108
109template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800110void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800111 t->*Prev = prev;
112 t->*Next = next;
113 if (prev) {
114 prev->*Next = t;
115 } else if (head) {
116 *head = t;
117 }
118 if (next) {
119 next->*Prev = t;
120 } else if (tail) {
121 *tail = t;
122 }
123}
124
125template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800126void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800127 if (t->*Prev) {
128 t->*Prev->*Next = t->*Next;
129 } else if (head) {
130 *head = t->*Next;
131 }
132 if (t->*Next) {
133 t->*Next->*Prev = t->*Prev;
134 } else if (tail) {
135 *tail = t->*Prev;
136 }
137 t->*Prev = t->*Next = nullptr;
138}
139
140/**
141 * Vertices are used in three ways: first, the path contours are converted into a
142 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
143 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
144 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
145 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
146 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
147 * an individual Vertex from the path mesh may belong to multiple
148 * MonotonePolys, so the original Vertices cannot be re-used.
149 */
150
151struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700152 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800153 : fPoint(point), fPrev(nullptr), fNext(nullptr)
154 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
155 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -0400156 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -0400157 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -0700158 , fAlpha(alpha)
Stephen Whitec4dbc372019-05-22 10:50:14 -0400159 , fSynthetic(false)
ethannicholase9709e82016-01-07 13:34:16 -0800160#if LOGGING_ENABLED
161 , fID (-1.0f)
162#endif
163 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400164 SkPoint fPoint; // Vertex position
165 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
166 Vertex* fNext; // "
167 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
168 Edge* fLastEdgeAbove; // "
169 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
170 Edge* fLastEdgeBelow; // "
171 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
172 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
173 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700174 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400175 bool fSynthetic; // Is this a synthetic vertex?
ethannicholase9709e82016-01-07 13:34:16 -0800176#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400177 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800178#endif
179};
180
181/***************************************************************************************/
182
183typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
184
ethannicholase9709e82016-01-07 13:34:16 -0800185bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500186 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800187}
188
189bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500190 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800191}
192
Stephen White16a40cb2017-02-23 11:10:01 -0500193struct Comparator {
194 enum class Direction { kVertical, kHorizontal };
195 Comparator(Direction direction) : fDirection(direction) {}
196 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
197 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
198 }
Stephen White16a40cb2017-02-23 11:10:01 -0500199 Direction fDirection;
200};
201
Brian Osman0995fd52019-01-09 09:52:25 -0500202inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500203 GrVertexWriter verts{data};
204 verts.write(v->fPoint);
205
Brian Osman80879d42019-01-07 16:15:27 -0500206 if (emitCoverage) {
207 verts.write(GrNormalizeByteToFloat(v->fAlpha));
208 }
Brian Osman0995fd52019-01-09 09:52:25 -0500209
Brian Osmanf9aabff2018-11-13 16:11:38 -0500210 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800211}
212
Brian Osman0995fd52019-01-09 09:52:25 -0500213void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400214 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
215 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
216 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700217#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500218 data = emit_vertex(v0, emitCoverage, data);
219 data = emit_vertex(v1, emitCoverage, data);
220 data = emit_vertex(v1, emitCoverage, data);
221 data = emit_vertex(v2, emitCoverage, data);
222 data = emit_vertex(v2, emitCoverage, data);
223 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800224#else
Brian Osman0995fd52019-01-09 09:52:25 -0500225 data = emit_vertex(v0, emitCoverage, data);
226 data = emit_vertex(v1, emitCoverage, data);
227 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800228#endif
229 return data;
230}
231
senorblancoe6eaa322016-03-08 09:06:44 -0800232struct VertexList {
233 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500234 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800235 Vertex* fHead;
236 Vertex* fTail;
237 void insert(Vertex* v, Vertex* prev, Vertex* next) {
238 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
239 }
240 void append(Vertex* v) {
241 insert(v, fTail, nullptr);
242 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400243 void append(const VertexList& list) {
244 if (!list.fHead) {
245 return;
246 }
247 if (fTail) {
248 fTail->fNext = list.fHead;
249 list.fHead->fPrev = fTail;
250 } else {
251 fHead = list.fHead;
252 }
253 fTail = list.fTail;
254 }
senorblancoe6eaa322016-03-08 09:06:44 -0800255 void prepend(Vertex* v) {
256 insert(v, nullptr, fHead);
257 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500258 void remove(Vertex* v) {
259 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
260 }
senorblancof57372d2016-08-31 10:36:19 -0700261 void close() {
262 if (fHead && fTail) {
263 fTail->fNext = fHead;
264 fHead->fPrev = fTail;
265 }
266 }
senorblancoe6eaa322016-03-08 09:06:44 -0800267};
268
senorblancof57372d2016-08-31 10:36:19 -0700269// Round to nearest quarter-pixel. This is used for screenspace tessellation.
270
271inline void round(SkPoint* p) {
272 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
273 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
274}
275
Stephen White94b7e542018-01-04 14:01:10 -0500276inline SkScalar double_to_clamped_scalar(double d) {
277 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
278}
279
senorblanco49df8d12016-10-07 08:36:56 -0700280// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
281struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500282 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700283 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
284 Line(const SkPoint& p, const SkPoint& q)
285 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
286 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
287 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
288 static_cast<double>(p.fX) * q.fY) {}
289 double dist(const SkPoint& p) const {
290 return fA * p.fX + fB * p.fY + fC;
291 }
Stephen Whitee260c462017-12-19 18:09:54 -0500292 Line operator*(double v) const {
293 return Line(fA * v, fB * v, fC * v);
294 }
senorblanco49df8d12016-10-07 08:36:56 -0700295 double magSq() const {
296 return fA * fA + fB * fB;
297 }
Stephen Whitee260c462017-12-19 18:09:54 -0500298 void normalize() {
299 double len = sqrt(this->magSq());
300 if (len == 0.0) {
301 return;
302 }
303 double scale = 1.0f / len;
304 fA *= scale;
305 fB *= scale;
306 fC *= scale;
307 }
308 bool nearParallel(const Line& o) const {
309 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
310 }
senorblanco49df8d12016-10-07 08:36:56 -0700311
312 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500313 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700314 double denom = fA * other.fB - fB * other.fA;
315 if (denom == 0.0) {
316 return false;
317 }
Stephen White94b7e542018-01-04 14:01:10 -0500318 double scale = 1.0 / denom;
319 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
320 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500321 round(point);
Stephen White9b7f1432019-06-08 08:56:58 -0400322 return point->isFinite();
senorblanco49df8d12016-10-07 08:36:56 -0700323 }
324 double fA, fB, fC;
325};
326
ethannicholase9709e82016-01-07 13:34:16 -0800327/**
328 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
329 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
330 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whiteb67b2352019-06-01 13:07:27 -0400331 * point). For speed, that case is only tested by the callers that require it. Edges also handle
332 * checking for intersection with other edges. Currently, this converts the edges to the
333 * parametric form, in order to avoid doing a division until an intersection has been confirmed.
334 * This is slightly slower in the "found" case, but a lot faster in the "not found" case.
ethannicholase9709e82016-01-07 13:34:16 -0800335 *
336 * The coefficients of the line equation stored in double precision to avoid catastrphic
337 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
338 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
339 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
340 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
341 * this file).
342 */
343
344struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500345 enum class Type { kInner, kOuter, kConnector };
346 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800347 : fWinding(winding)
348 , fTop(top)
349 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500350 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800351 , fLeft(nullptr)
352 , fRight(nullptr)
353 , fPrevEdgeAbove(nullptr)
354 , fNextEdgeAbove(nullptr)
355 , fPrevEdgeBelow(nullptr)
356 , fNextEdgeBelow(nullptr)
357 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700358 , fRightPoly(nullptr)
359 , fLeftPolyPrev(nullptr)
360 , fLeftPolyNext(nullptr)
361 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700362 , fRightPolyNext(nullptr)
363 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700364 , fUsedInRightPoly(false)
365 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800366 }
367 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
368 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
369 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500370 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800371 Edge* fLeft; // The linked list of edges in the active edge list.
372 Edge* fRight; // "
373 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
374 Edge* fNextEdgeAbove; // "
375 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
376 Edge* fNextEdgeBelow; // "
377 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
378 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700379 Edge* fLeftPolyPrev;
380 Edge* fLeftPolyNext;
381 Edge* fRightPolyPrev;
382 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700383 bool fUsedInLeftPoly;
384 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700385 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800386 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700387 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800388 }
389 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700390 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800391 }
392 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700393 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800394 }
395 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700396 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800397 }
Stephen White95152e12017-12-18 10:52:44 -0500398 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
Brian Salomon120e7d62019-09-11 10:29:22 -0400399 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
400 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800401 if (fTop == other.fTop || fBottom == other.fBottom) {
402 return false;
403 }
senorblanco49df8d12016-10-07 08:36:56 -0700404 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800405 if (denom == 0.0) {
406 return false;
407 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500408 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
409 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
410 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
411 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800412 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
413 // This saves us doing the divide below unless absolutely necessary.
414 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
415 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
416 return false;
417 }
418 double s = sNumer / denom;
419 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700420 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
421 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500422 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500423 if (fType == Type::kConnector) {
424 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
425 } else if (other.fType == Type::kConnector) {
426 double t = tNumer / denom;
427 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500428 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
429 *alpha = 0;
430 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500431 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500432 }
433 }
ethannicholase9709e82016-01-07 13:34:16 -0800434 return true;
435 }
senorblancof57372d2016-08-31 10:36:19 -0700436};
437
Stephen Whitec4dbc372019-05-22 10:50:14 -0400438struct SSEdge;
439
440struct SSVertex {
441 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
442 Vertex* fVertex;
443 SSEdge* fPrev;
444 SSEdge* fNext;
445};
446
447struct SSEdge {
448 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
449 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
450 }
451 Edge* fEdge;
452 Event* fEvent;
453 SSVertex* fPrev;
454 SSVertex* fNext;
455};
456
457typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
458typedef std::vector<SSEdge*> SSEdgeList;
459
senorblancof57372d2016-08-31 10:36:19 -0700460struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500461 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700462 Edge* fHead;
463 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700464 void insert(Edge* edge, Edge* prev, Edge* next) {
465 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700466 }
467 void append(Edge* e) {
468 insert(e, fTail, nullptr);
469 }
470 void remove(Edge* edge) {
471 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700472 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400473 void removeAll() {
474 while (fHead) {
475 this->remove(fHead);
476 }
477 }
senorblancof57372d2016-08-31 10:36:19 -0700478 void close() {
479 if (fHead && fTail) {
480 fTail->fRight = fHead;
481 fHead->fLeft = fTail;
482 }
483 }
484 bool contains(Edge* edge) const {
485 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800486 }
487};
488
Stephen Whitec4dbc372019-05-22 10:50:14 -0400489struct EventList;
490
Stephen Whitee260c462017-12-19 18:09:54 -0500491struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400492 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
493 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500494 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400495 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500496 SkPoint fPoint;
497 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400498 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500499};
500
Stephen Whitec4dbc372019-05-22 10:50:14 -0400501struct EventComparator {
502 enum class Op { kLessThan, kGreaterThan };
503 EventComparator(Op op) : fOp(op) {}
504 bool operator() (Event* const &e1, Event* const &e2) {
505 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
506 : e1->fAlpha > e2->fAlpha;
507 }
508 Op fOp;
509};
Stephen Whitee260c462017-12-19 18:09:54 -0500510
Stephen Whitec4dbc372019-05-22 10:50:14 -0400511typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500512
Stephen Whitec4dbc372019-05-22 10:50:14 -0400513struct EventList : EventPQ {
514 EventList(EventComparator comparison) : EventPQ(comparison) {
515 }
516};
517
518void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
519 Vertex* prev = e->fPrev->fVertex;
520 Vertex* next = e->fNext->fVertex;
521 if (prev == next || !prev->fPartner || !next->fPartner) {
522 return;
523 }
524 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
525 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500526 SkPoint p;
527 uint8_t alpha;
528 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400529 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
530 "will collapse to %g,%g alpha %d\n",
531 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
532 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400533 e->fEvent = alloc.make<Event>(e, p, alpha);
534 events->push(e->fEvent);
535 }
536}
537
538void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
539 Comparator& c, SkArenaAlloc& alloc) {
540 if (!v->fPartner) {
541 return;
542 }
Stephen White8a3c0592019-05-29 11:26:16 -0400543 Vertex* top = edge->fEdge->fTop;
544 Vertex* bottom = edge->fEdge->fBottom;
545 if (!top || !bottom ) {
546 return;
547 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400548 Line line = edge->fEdge->fLine;
549 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
550 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
551 SkPoint p;
552 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400553 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
554 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400555 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
556 "will collapse to %g,%g alpha %d\n",
557 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400558 edge->fEvent = alloc.make<Event>(edge, p, alpha);
559 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500560 }
561}
Stephen Whitee260c462017-12-19 18:09:54 -0500562
ethannicholase9709e82016-01-07 13:34:16 -0800563/***************************************************************************************/
564
565struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700566 Poly(Vertex* v, int winding)
567 : fFirstVertex(v)
568 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800569 , fHead(nullptr)
570 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800571 , fNext(nullptr)
572 , fPartner(nullptr)
573 , fCount(0)
574 {
575#if LOGGING_ENABLED
576 static int gID = 0;
577 fID = gID++;
Brian Salomon120e7d62019-09-11 10:29:22 -0400578 TESS_LOG("*** created Poly %d\n", fID);
ethannicholase9709e82016-01-07 13:34:16 -0800579#endif
580 }
senorblanco531237e2016-06-02 11:36:48 -0700581 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800582 struct MonotonePoly {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700583 MonotonePoly(Edge* edge, Side side, int winding)
senorblanco531237e2016-06-02 11:36:48 -0700584 : fSide(side)
585 , fFirstEdge(nullptr)
586 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800587 , fPrev(nullptr)
Chris Dalton022bd3b2020-01-24 13:48:53 -0700588 , fNext(nullptr)
589 , fWinding(winding) {
senorblanco531237e2016-06-02 11:36:48 -0700590 this->addEdge(edge);
591 }
ethannicholase9709e82016-01-07 13:34:16 -0800592 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700593 Edge* fFirstEdge;
594 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800595 MonotonePoly* fPrev;
596 MonotonePoly* fNext;
Chris Dalton022bd3b2020-01-24 13:48:53 -0700597 int fWinding;
senorblanco531237e2016-06-02 11:36:48 -0700598 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800599 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700600 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700601 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
602 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700603 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800604 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700605 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700606 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
607 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700608 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800609 }
ethannicholase9709e82016-01-07 13:34:16 -0800610 }
611
Brian Osman0995fd52019-01-09 09:52:25 -0500612 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700613 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700614 VertexList vertices;
615 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500616 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700617 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700618 if (kRight_Side == fSide) {
619 vertices.append(e->fBottom);
620 e = e->fRightPolyNext;
621 } else {
622 vertices.prepend(e->fBottom);
623 e = e->fLeftPolyNext;
624 }
Stephen White651cbe92017-03-03 12:24:16 -0500625 count++;
senorblanco531237e2016-06-02 11:36:48 -0700626 }
627 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800628 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700629 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800630 SkASSERT(v && v->fPrev && v->fNext);
631 Vertex* prev = v->fPrev;
632 Vertex* curr = v;
633 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500634 if (count == 3) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700635 return this->emitTriangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500636 }
ethannicholase9709e82016-01-07 13:34:16 -0800637 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
638 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
639 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
640 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
641 if (ax * by - ay * bx >= 0.0) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700642 data = this->emitTriangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800643 v->fPrev->fNext = v->fNext;
644 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500645 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800646 if (v->fPrev == first) {
647 v = v->fNext;
648 } else {
649 v = v->fPrev;
650 }
651 } else {
652 v = v->fNext;
653 }
654 }
655 return data;
656 }
Chris Dalton022bd3b2020-01-24 13:48:53 -0700657 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, bool emitCoverage,
658 void* data) const {
659 if (fWinding < 0) {
660 // Ensure our triangles always wind in the same direction as if the path had been
661 // triangulated as a simple fan (a la red book).
662 std::swap(prev, next);
663 }
664 return emit_triangle(next, curr, prev, emitCoverage, data);
665 }
ethannicholase9709e82016-01-07 13:34:16 -0800666 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500667 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400668 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
669 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800670 Poly* partner = fPartner;
671 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700672 if (side == kRight_Side) {
673 if (e->fUsedInRightPoly) {
674 return this;
675 }
676 } else {
677 if (e->fUsedInLeftPoly) {
678 return this;
679 }
680 }
ethannicholase9709e82016-01-07 13:34:16 -0800681 if (partner) {
682 fPartner = partner->fPartner = nullptr;
683 }
senorblanco531237e2016-06-02 11:36:48 -0700684 if (!fTail) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700685 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700686 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700687 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
688 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700689 } else if (side == fTail->fSide) {
690 fTail->addEdge(e);
691 fCount++;
692 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500693 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700694 fTail->addEdge(e);
695 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800696 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700697 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800698 poly = partner;
699 } else {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700700 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700701 m->fPrev = fTail;
702 fTail->fNext = m;
703 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800704 }
705 }
ethannicholase9709e82016-01-07 13:34:16 -0800706 return poly;
707 }
Brian Osman0995fd52019-01-09 09:52:25 -0500708 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800709 if (fCount < 3) {
710 return data;
711 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400712 TESS_LOG("emit() %d, size %d\n", fID, fCount);
ethannicholase9709e82016-01-07 13:34:16 -0800713 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500714 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800715 }
716 return data;
717 }
senorblanco531237e2016-06-02 11:36:48 -0700718 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
719 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800720 int fWinding;
721 MonotonePoly* fHead;
722 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800723 Poly* fNext;
724 Poly* fPartner;
725 int fCount;
726#if LOGGING_ENABLED
727 int fID;
728#endif
729};
730
731/***************************************************************************************/
732
733bool coincident(const SkPoint& a, const SkPoint& b) {
734 return a == b;
735}
736
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500737Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
738 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800739 poly->fNext = *head;
740 *head = poly;
741 return poly;
742}
743
Stephen White3a9aab92017-03-07 14:07:18 -0500744void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500745 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800746#if LOGGING_ENABLED
747 static float gID = 0.0f;
748 v->fID = gID++;
749#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500750 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800751}
752
Stephen White36e4f062017-03-27 16:11:31 -0400753SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
754 SkQuadCoeff quad(pts);
755 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
756 SkPoint mid = to_point(quad.eval(t));
757 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400758 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
759 return 0;
760 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500761 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400762}
763
764void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
765 SkArenaAlloc& alloc) {
766 SkQuadCoeff quad(pts);
767 Sk2s aa = quad.fA * quad.fA;
768 SkScalar denom = 2.0f * (aa[0] + aa[1]);
769 Sk2s ab = quad.fA * quad.fB;
770 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
771 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500772 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400773 // Test possible subdivision values only at the point of maximum curvature.
774 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500775 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400776 u = 1.0f / nPoints;
777 if (quad_error_at(pts, t, u) < toleranceSqd) {
778 break;
779 }
780 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800781 }
Stephen White36e4f062017-03-27 16:11:31 -0400782 for (int j = 1; j <= nPoints; j++) {
783 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
784 }
ethannicholase9709e82016-01-07 13:34:16 -0800785}
786
Stephen White3a9aab92017-03-07 14:07:18 -0500787void generate_cubic_points(const SkPoint& p0,
788 const SkPoint& p1,
789 const SkPoint& p2,
790 const SkPoint& p3,
791 SkScalar tolSqd,
792 VertexList* contour,
793 int pointsLeft,
794 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500795 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
796 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800797 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
798 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500799 append_point_to_contour(p3, contour, alloc);
800 return;
ethannicholase9709e82016-01-07 13:34:16 -0800801 }
802 const SkPoint q[] = {
803 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
804 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
805 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
806 };
807 const SkPoint r[] = {
808 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
809 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
810 };
811 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
812 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500813 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
814 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800815}
816
817// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
818
819void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Dalton6ccc0322020-01-29 11:38:16 -0700820 VertexList* contours, SkArenaAlloc& alloc, Mode mode, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800821 SkScalar toleranceSqd = tolerance * tolerance;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700822 bool innerPolygons = (Mode::kSimpleInnerPolygons == mode);
ethannicholase9709e82016-01-07 13:34:16 -0800823
824 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800825 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500826 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800827 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800828 if (path.isInverseFillType()) {
829 SkPoint quad[4];
830 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700831 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500832 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800833 }
Stephen White3a9aab92017-03-07 14:07:18 -0500834 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800835 }
836 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500837 SkPath::Verb verb;
Mike Reedba7e9a62019-08-16 13:30:34 -0400838 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800839 switch (verb) {
840 case SkPath::kConic_Verb: {
Chris Dalton6ccc0322020-01-29 11:38:16 -0700841 *isLinear = false;
842 if (innerPolygons) {
843 append_point_to_contour(pts[2], contour, alloc);
844 break;
845 }
ethannicholase9709e82016-01-07 13:34:16 -0800846 SkScalar weight = iter.conicWeight();
847 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
848 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400849 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800850 quadPts += 2;
851 }
ethannicholase9709e82016-01-07 13:34:16 -0800852 break;
853 }
854 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500855 if (contour->fHead) {
856 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800857 }
Stephen White3a9aab92017-03-07 14:07:18 -0500858 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800859 break;
860 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500861 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800862 break;
863 }
864 case SkPath::kQuad_Verb: {
ethannicholase9709e82016-01-07 13:34:16 -0800865 *isLinear = false;
Chris Dalton6ccc0322020-01-29 11:38:16 -0700866 if (innerPolygons) {
867 append_point_to_contour(pts[2], contour, alloc);
868 break;
869 }
870 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800871 break;
872 }
873 case SkPath::kCubic_Verb: {
Chris Dalton6ccc0322020-01-29 11:38:16 -0700874 *isLinear = false;
875 if (innerPolygons) {
876 append_point_to_contour(pts[3], contour, alloc);
877 break;
878 }
ethannicholase9709e82016-01-07 13:34:16 -0800879 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500880 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
881 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800882 break;
883 }
884 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800885 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800886 break;
887 }
888 }
889}
890
Mike Reed7d34dc72019-11-26 12:17:17 -0500891inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800892 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500893 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800894 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500895 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800896 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500897 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700898 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500899 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800900 return (winding & 1) == 1;
901 default:
902 SkASSERT(false);
903 return false;
904 }
905}
906
Mike Reed7d34dc72019-11-26 12:17:17 -0500907inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500908 return poly && apply_fill_type(fillType, poly->fWinding);
909}
910
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500911Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500912 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800913 Vertex* top = winding < 0 ? next : prev;
914 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500915 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800916}
917
918void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400919 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700920 SkASSERT(edges->contains(edge));
921 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800922}
923
924void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400925 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700926 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800927 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700928 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800929}
930
931void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500932 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800933 *left = v->fFirstEdgeAbove->fLeft;
934 *right = v->fLastEdgeAbove->fRight;
935 return;
936 }
937 Edge* next = nullptr;
938 Edge* prev;
939 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
940 if (prev->isLeftOf(v)) {
941 break;
942 }
943 next = prev;
944 }
945 *left = prev;
946 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800947}
948
ethannicholase9709e82016-01-07 13:34:16 -0800949void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
950 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500951 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800952 return;
953 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400954 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
955 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800956 Edge* prev = nullptr;
957 Edge* next;
958 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
959 if (next->isRightOf(edge->fTop)) {
960 break;
961 }
962 prev = next;
963 }
senorblancoe6eaa322016-03-08 09:06:44 -0800964 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800965 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
966}
967
968void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
969 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500970 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800971 return;
972 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400973 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
974 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800975 Edge* prev = nullptr;
976 Edge* next;
977 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
978 if (next->isRightOf(edge->fBottom)) {
979 break;
980 }
981 prev = next;
982 }
senorblancoe6eaa322016-03-08 09:06:44 -0800983 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800984 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
985}
986
987void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400988 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400989 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
990 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800991 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800992 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
993}
994
995void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400996 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400997 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
998 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800999 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -08001000 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
1001}
1002
Stephen Whitee7a364d2017-01-11 16:19:26 -05001003void disconnect(Edge* edge)
1004{
ethannicholase9709e82016-01-07 13:34:16 -08001005 remove_edge_above(edge);
1006 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -05001007}
1008
Stephen White3b5a3fa2017-06-06 14:51:19 -04001009void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
1010
1011void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
1012 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
1013 return;
1014 }
1015 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -04001016 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001017 while (v != dst) {
1018 v = v->fPrev;
1019 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1020 remove_edge(e, activeEdges);
1021 }
1022 Edge* leftEdge = v->fLeftEnclosingEdge;
1023 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1024 insert_edge(e, leftEdge, activeEdges);
1025 leftEdge = e;
1026 }
1027 }
1028 *current = v;
1029}
1030
Stephen White3b5a3fa2017-06-06 14:51:19 -04001031void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001032 remove_edge_below(edge);
1033 edge->fTop = v;
1034 edge->recompute();
1035 insert_edge_below(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001036 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001037 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001038}
1039
Stephen White3b5a3fa2017-06-06 14:51:19 -04001040void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001041 remove_edge_above(edge);
1042 edge->fBottom = v;
1043 edge->recompute();
1044 insert_edge_above(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001045 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001046 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001047}
1048
Stephen White3b5a3fa2017-06-06 14:51:19 -04001049void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1050 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001051 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001052 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1053 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1054 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001055 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001056 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001057 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001058 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001059 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001060 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001061 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001062 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001063 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001064 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001065 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001066 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001067 }
1068}
1069
Stephen White3b5a3fa2017-06-06 14:51:19 -04001070void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1071 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001072 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001073 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1074 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1075 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001076 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001077 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001078 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001079 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001080 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001081 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001082 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001083 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001084 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001085 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001086 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001087 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001088 }
1089}
1090
Stephen Whited26b4d82018-07-26 10:02:27 -04001091bool top_collinear(Edge* left, Edge* right) {
1092 if (!left || !right) {
1093 return false;
1094 }
1095 return left->fTop->fPoint == right->fTop->fPoint ||
1096 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1097}
1098
1099bool bottom_collinear(Edge* left, Edge* right) {
1100 if (!left || !right) {
1101 return false;
1102 }
1103 return left->fBottom->fPoint == right->fBottom->fPoint ||
1104 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1105}
1106
Stephen White3b5a3fa2017-06-06 14:51:19 -04001107void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001108 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001109 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001110 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001111 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001112 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001113 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001114 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001115 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001116 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001117 } else {
1118 break;
1119 }
ethannicholase9709e82016-01-07 13:34:16 -08001120 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001121 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1122 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1123 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1124 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001125}
1126
Stephen White89042d52018-06-08 12:18:22 -04001127bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001128 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001129 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001130 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001131 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001132 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1133 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001134 Vertex* top;
1135 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001136 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001137 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001138 top = v;
1139 bottom = edge->fTop;
1140 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001141 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001142 top = edge->fBottom;
1143 bottom = v;
1144 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001145 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001146 top = v;
1147 bottom = edge->fBottom;
1148 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001149 }
Stephen White531a48e2018-06-01 09:49:39 -04001150 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001151 insert_edge_below(newEdge, top, c);
1152 insert_edge_above(newEdge, bottom, c);
1153 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001154 return true;
1155}
1156
1157bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1158 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1159 return false;
1160 }
Stephen White1c5fd182018-07-12 15:54:05 -04001161 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1162 return false;
1163 }
Stephen White89042d52018-06-08 12:18:22 -04001164 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1165 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001166 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001167 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1168 }
1169 } else {
1170 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001171 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001172 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1173 }
1174 }
1175 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1176 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001177 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001178 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1179 }
1180 } else {
1181 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001182 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001183 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1184 }
1185 }
1186 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001187}
1188
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001189Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001190 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001191 if (!prev || !next || prev->fPoint == next->fPoint) {
1192 return nullptr;
1193 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001194 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001195 insert_edge_below(edge, edge->fTop, c);
1196 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001197 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001198 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001199 return edge;
1200}
1201
Stephen Whitebf6137e2017-01-04 15:43:26 -05001202void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001203 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001204 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1205 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -05001206 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001207 if (src->fPartner) {
1208 src->fPartner->fPartner = dst;
1209 }
Stephen White7b376942018-05-22 11:51:32 -04001210 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001211 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001212 }
Stephen White7b376942018-05-22 11:51:32 -04001213 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001214 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001215 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001216 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001217 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001218}
1219
Stephen White95152e12017-12-18 10:52:44 -05001220Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1221 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1222 Vertex* prevV = reference;
1223 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1224 prevV = prevV->fPrev;
1225 }
1226 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1227 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1228 prevV = nextV;
1229 nextV = nextV->fNext;
1230 }
1231 Vertex* v;
1232 if (prevV && coincident(prevV->fPoint, p)) {
1233 v = prevV;
1234 } else if (nextV && coincident(nextV->fPoint, p)) {
1235 v = nextV;
1236 } else {
1237 v = alloc.make<Vertex>(p, alpha);
1238#if LOGGING_ENABLED
1239 if (!prevV) {
1240 v->fID = mesh->fHead->fID - 1.0f;
1241 } else if (!nextV) {
1242 v->fID = mesh->fTail->fID + 1.0f;
1243 } else {
1244 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1245 }
1246#endif
1247 mesh->insert(v, prevV, nextV);
1248 }
1249 return v;
1250}
1251
Stephen White53a02982018-05-30 22:47:46 -04001252// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1253// sort criterion, it may not be possible to split correctly, since there is no point which is
1254// below the top and above the bottom. This function detects that case.
1255bool nearly_flat(Comparator& c, Edge* edge) {
1256 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1257 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001258 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001259}
1260
Stephen Whitee62999f2018-06-05 18:45:07 -04001261SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1262 if (c.sweep_lt(p, min)) {
1263 return min;
1264 } else if (c.sweep_lt(max, p)) {
1265 return max;
1266 } else {
1267 return p;
1268 }
1269}
1270
Stephen Whitec4dbc372019-05-22 10:50:14 -04001271void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1272 Line line1 = edge1->fLine;
1273 Line line2 = edge2->fLine;
1274 line1.normalize();
1275 line2.normalize();
1276 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1277 if (cosAngle > 0.999) {
1278 return;
1279 }
1280 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1281 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1282 SkPoint p;
1283 if (line1.intersect(line2, &p)) {
1284 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1285 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001286 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 -04001287 }
1288}
1289
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001290bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001291 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001292 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001293 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001294 }
Stephen White56158ae2017-01-30 14:31:31 -05001295 SkPoint p;
1296 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001297 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001298 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001299 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001300 Vertex* top = *current;
1301 // If the intersection point is above the current vertex, rewind to the vertex above the
1302 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001303 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001304 top = top->fPrev;
1305 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001306 if (!nearly_flat(c, left)) {
1307 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001308 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001309 if (!nearly_flat(c, right)) {
1310 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001311 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001312 if (p == left->fTop->fPoint) {
1313 v = left->fTop;
1314 } else if (p == left->fBottom->fPoint) {
1315 v = left->fBottom;
1316 } else if (p == right->fTop->fPoint) {
1317 v = right->fTop;
1318 } else if (p == right->fBottom->fPoint) {
1319 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001320 } else {
Stephen White95152e12017-12-18 10:52:44 -05001321 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001322 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001323 v->fSynthetic = true;
1324 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001325 }
ethannicholase9709e82016-01-07 13:34:16 -08001326 }
Stephen White0cb31672017-06-08 14:41:01 -04001327 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001328 split_edge(left, v, activeEdges, current, c, alloc);
1329 split_edge(right, v, activeEdges, current, c, alloc);
Brian Osman788b9162020-02-07 10:36:46 -05001330 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001331 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001332 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001333 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001334}
1335
Chris Daltondcc8c542020-01-28 17:55:56 -07001336void sanitize_contours(VertexList* contours, int contourCnt, Mode mode) {
1337 bool approximate = (Mode::kEdgeAntialias == mode);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001338 bool removeCollinearVertices = (Mode::kSimpleInnerPolygons != mode);
Stephen White3a9aab92017-03-07 14:07:18 -05001339 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1340 SkASSERT(contour->fHead);
1341 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001342 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001343 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001344 }
Stephen White3a9aab92017-03-07 14:07:18 -05001345 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001346 if (approximate) {
1347 round(&v->fPoint);
1348 }
Stephen White3a9aab92017-03-07 14:07:18 -05001349 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001350 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001351 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001352 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001353 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001354 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001355 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001356 contour->remove(v);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001357 } else if (removeCollinearVertices &&
1358 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001359 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001360 contour->remove(v);
1361 } else {
1362 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001363 }
Stephen White3a9aab92017-03-07 14:07:18 -05001364 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001365 }
1366 }
1367}
1368
Stephen Whitee260c462017-12-19 18:09:54 -05001369bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001370 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001371 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001372 }
Stephen Whitee260c462017-12-19 18:09:54 -05001373 bool merged = false;
1374 for (Vertex* v = mesh->fHead->fNext; v;) {
1375 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001376 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1377 v->fPoint = v->fPrev->fPoint;
1378 }
1379 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001380 merge_vertices(v, v->fPrev, mesh, c, alloc);
1381 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001382 }
Stephen Whitee260c462017-12-19 18:09:54 -05001383 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001384 }
Stephen Whitee260c462017-12-19 18:09:54 -05001385 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001386}
1387
1388// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1389
Stephen White3a9aab92017-03-07 14:07:18 -05001390void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001391 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001392 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1393 Vertex* prev = contour->fTail;
1394 for (Vertex* v = contour->fHead; v;) {
1395 Vertex* next = v->fNext;
1396 connect(prev, v, Edge::Type::kInner, c, alloc);
1397 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001398 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001399 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001400 }
1401 }
ethannicholase9709e82016-01-07 13:34:16 -08001402}
1403
Stephen Whitee260c462017-12-19 18:09:54 -05001404void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1405 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001406 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001407 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1408 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1409 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1410 // number.
1411 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1412 inner->fPartner = outer->fPartner = nullptr;
1413 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001414 }
1415 }
1416}
1417
1418template <CompareFunc sweep_lt>
1419void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1420 Vertex* a = front->fHead;
1421 Vertex* b = back->fHead;
1422 while (a && b) {
1423 if (sweep_lt(a->fPoint, b->fPoint)) {
1424 front->remove(a);
1425 result->append(a);
1426 a = front->fHead;
1427 } else {
1428 back->remove(b);
1429 result->append(b);
1430 b = back->fHead;
1431 }
1432 }
1433 result->append(*front);
1434 result->append(*back);
1435}
1436
1437void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1438 if (c.fDirection == Comparator::Direction::kHorizontal) {
1439 sorted_merge<sweep_lt_horiz>(front, back, result);
1440 } else {
1441 sorted_merge<sweep_lt_vert>(front, back, result);
1442 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001443#if LOGGING_ENABLED
1444 float id = 0.0f;
1445 for (Vertex* v = result->fHead; v; v = v->fNext) {
1446 v->fID = id++;
1447 }
1448#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001449}
1450
ethannicholase9709e82016-01-07 13:34:16 -08001451// Stage 3: sort the vertices by increasing sweep direction.
1452
Stephen White16a40cb2017-02-23 11:10:01 -05001453template <CompareFunc sweep_lt>
1454void merge_sort(VertexList* vertices) {
1455 Vertex* slow = vertices->fHead;
1456 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001457 return;
1458 }
Stephen White16a40cb2017-02-23 11:10:01 -05001459 Vertex* fast = slow->fNext;
1460 if (!fast) {
1461 return;
1462 }
1463 do {
1464 fast = fast->fNext;
1465 if (fast) {
1466 fast = fast->fNext;
1467 slow = slow->fNext;
1468 }
1469 } while (fast);
1470 VertexList front(vertices->fHead, slow);
1471 VertexList back(slow->fNext, vertices->fTail);
1472 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001473
Stephen White16a40cb2017-02-23 11:10:01 -05001474 merge_sort<sweep_lt>(&front);
1475 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001476
Stephen White16a40cb2017-02-23 11:10:01 -05001477 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001478 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001479}
1480
Stephen White95152e12017-12-18 10:52:44 -05001481void dump_mesh(const VertexList& mesh) {
1482#if LOGGING_ENABLED
1483 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001484 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 -05001485 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001486 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1487 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001488 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001489 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001490 }
1491 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001492 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001493 }
1494 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001495 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001496 }
1497 }
1498#endif
1499}
1500
Stephen Whitec4dbc372019-05-22 10:50:14 -04001501void dump_skel(const SSEdgeList& ssEdges) {
1502#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001503 for (SSEdge* edge : ssEdges) {
1504 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001505 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001506 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001507 edge->fNext->fVertex->fID);
1508 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001509 TESS_LOG(" (original %g -> %g)\n",
1510 edge->fEdge->fTop->fID,
1511 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001512 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001513 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001514 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001515 }
1516 }
1517#endif
1518}
1519
Stephen White89042d52018-06-08 12:18:22 -04001520#ifdef SK_DEBUG
1521void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1522 if (!left || !right) {
1523 return;
1524 }
1525 if (left->fTop == right->fTop) {
1526 SkASSERT(left->isLeftOf(right->fBottom));
1527 SkASSERT(right->isRightOf(left->fBottom));
1528 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1529 SkASSERT(left->isLeftOf(right->fTop));
1530 } else {
1531 SkASSERT(right->isRightOf(left->fTop));
1532 }
1533 if (left->fBottom == right->fBottom) {
1534 SkASSERT(left->isLeftOf(right->fTop));
1535 SkASSERT(right->isRightOf(left->fTop));
1536 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1537 SkASSERT(left->isLeftOf(right->fBottom));
1538 } else {
1539 SkASSERT(right->isRightOf(left->fBottom));
1540 }
1541}
1542
1543void validate_edge_list(EdgeList* edges, Comparator& c) {
1544 Edge* left = edges->fHead;
1545 if (!left) {
1546 return;
1547 }
1548 for (Edge* right = left->fRight; right; right = right->fRight) {
1549 validate_edge_pair(left, right, c);
1550 left = right;
1551 }
1552}
1553#endif
1554
ethannicholase9709e82016-01-07 13:34:16 -08001555// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1556
Stephen Whitec4dbc372019-05-22 10:50:14 -04001557bool connected(Vertex* v) {
1558 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1559}
1560
Chris Dalton6ccc0322020-01-29 11:38:16 -07001561enum class SimplifyResult {
1562 kAlreadySimple,
1563 kFoundSelfIntersection,
1564 kAbort
1565};
1566
1567SimplifyResult simplify(Mode mode, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001568 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001569 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001570 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001571 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001572 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001573 continue;
1574 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001575 Edge* leftEnclosingEdge;
1576 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001577 bool restartChecks;
1578 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001579 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1580 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001581 restartChecks = false;
1582 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001583 v->fLeftEnclosingEdge = leftEnclosingEdge;
1584 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001585 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001586 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001587 if (check_for_intersection(
1588 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c, alloc) ||
1589 check_for_intersection(
1590 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c, alloc)) {
1591 if (Mode::kSimpleInnerPolygons == mode) {
1592 return SimplifyResult::kAbort;
1593 }
1594 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001595 restartChecks = true;
1596 break;
1597 }
1598 }
1599 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001600 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001601 &activeEdges, &v, mesh, c, alloc)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001602 if (Mode::kSimpleInnerPolygons == mode) {
1603 return SimplifyResult::kAbort;
1604 }
1605 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001606 restartChecks = true;
1607 }
1608
1609 }
1610 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001611#ifdef SK_DEBUG
1612 validate_edge_list(&activeEdges, c);
1613#endif
ethannicholase9709e82016-01-07 13:34:16 -08001614 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1615 remove_edge(e, &activeEdges);
1616 }
1617 Edge* leftEdge = leftEnclosingEdge;
1618 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1619 insert_edge(e, leftEdge, &activeEdges);
1620 leftEdge = e;
1621 }
ethannicholase9709e82016-01-07 13:34:16 -08001622 }
Stephen Whitee260c462017-12-19 18:09:54 -05001623 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001624 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001625}
1626
1627// Stage 5: Tessellate the simplified mesh into monotone polygons.
1628
Chris Dalton6ccc0322020-01-29 11:38:16 -07001629Poly* tessellate(SkPathFillType fillType, Mode mode, const VertexList& vertices,
1630 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001631 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001632 int maxWindMagnitude = std::numeric_limits<int>::max();
1633 if (Mode::kSimpleInnerPolygons == mode && !SkPathFillType_IsEvenOdd(fillType)) {
1634 maxWindMagnitude = 1;
1635 }
ethannicholase9709e82016-01-07 13:34:16 -08001636 EdgeList activeEdges;
1637 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001638 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001639 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001640 continue;
1641 }
1642#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001643 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 -08001644#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001645 Edge* leftEnclosingEdge;
1646 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001647 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001648 Poly* leftPoly;
1649 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001650 if (v->fFirstEdgeAbove) {
1651 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1652 rightPoly = v->fLastEdgeAbove->fRightPoly;
1653 } else {
1654 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1655 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1656 }
1657#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001658 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001659 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001660 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1661 e->fTop->fID, e->fBottom->fID,
1662 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1663 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001664 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001665 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001666 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001667 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1668 e->fTop->fID, e->fBottom->fID,
1669 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1670 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001671 }
1672#endif
1673 if (v->fFirstEdgeAbove) {
1674 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001675 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001676 }
1677 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001678 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001679 }
1680 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001681 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001682 remove_edge(e, &activeEdges);
1683 if (e->fRightPoly) {
1684 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001685 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001686 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001687 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001688 }
1689 }
1690 remove_edge(v->fLastEdgeAbove, &activeEdges);
1691 if (!v->fFirstEdgeBelow) {
1692 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1693 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1694 rightPoly->fPartner = leftPoly;
1695 leftPoly->fPartner = rightPoly;
1696 }
1697 }
1698 }
1699 if (v->fFirstEdgeBelow) {
1700 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001701 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001702 if (leftPoly == rightPoly) {
1703 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1704 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1705 leftPoly->fWinding, alloc);
1706 leftEnclosingEdge->fRightPoly = leftPoly;
1707 } else {
1708 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1709 rightPoly->fWinding, alloc);
1710 rightEnclosingEdge->fLeftPoly = rightPoly;
1711 }
ethannicholase9709e82016-01-07 13:34:16 -08001712 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001713 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001714 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1715 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001716 }
1717 }
1718 Edge* leftEdge = v->fFirstEdgeBelow;
1719 leftEdge->fLeftPoly = leftPoly;
1720 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1721 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1722 rightEdge = rightEdge->fNextEdgeBelow) {
1723 insert_edge(rightEdge, leftEdge, &activeEdges);
1724 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1725 winding += leftEdge->fWinding;
1726 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001727 if (abs(winding) > maxWindMagnitude) {
1728 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1729 }
ethannicholase9709e82016-01-07 13:34:16 -08001730 Poly* poly = new_poly(&polys, v, winding, alloc);
1731 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1732 }
1733 leftEdge = rightEdge;
1734 }
1735 v->fLastEdgeBelow->fRightPoly = rightPoly;
1736 }
1737#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001738 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001739 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001740 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1741 e->fTop->fID, e->fBottom->fID,
1742 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1743 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001744 }
1745#endif
1746 }
1747 return polys;
1748}
1749
Mike Reed7d34dc72019-11-26 12:17:17 -05001750void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001751 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001752 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001753 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001754 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001755 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001756 continue;
1757 }
1758 Edge* leftEnclosingEdge;
1759 Edge* rightEnclosingEdge;
1760 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1761 bool prevFilled = leftEnclosingEdge &&
1762 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1763 for (Edge* e = v->fFirstEdgeAbove; e;) {
1764 Edge* next = e->fNextEdgeAbove;
1765 remove_edge(e, &activeEdges);
1766 bool filled = apply_fill_type(fillType, e->fWinding);
1767 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001768 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001769 }
Stephen White49789062017-02-21 10:35:49 -05001770 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001771 e = next;
1772 }
Stephen White49789062017-02-21 10:35:49 -05001773 Edge* prev = leftEnclosingEdge;
1774 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1775 if (prev) {
1776 e->fWinding += prev->fWinding;
1777 }
1778 insert_edge(e, prev, &activeEdges);
1779 prev = e;
1780 }
senorblancof57372d2016-08-31 10:36:19 -07001781 }
senorblancof57372d2016-08-31 10:36:19 -07001782}
1783
Stephen White66412122017-03-01 11:48:27 -05001784// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001785void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001786 normal->set(SkDoubleToScalar(e->fLine.fA),
1787 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001788}
1789
1790// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1791// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1792// invert on stroking.
1793
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001794void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001795 Edge* prevEdge = boundary->fTail;
1796 SkVector prevNormal;
1797 get_edge_normal(prevEdge, &prevNormal);
1798 for (Edge* e = boundary->fHead; e != nullptr;) {
1799 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1800 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001801 double distPrev = e->dist(prev->fPoint);
1802 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001803 SkVector normal;
1804 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001805 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001806 if (prev == next) {
1807 remove_edge(prevEdge, boundary);
1808 remove_edge(e, boundary);
1809 prevEdge = boundary->fTail;
1810 e = boundary->fHead;
1811 if (prevEdge) {
1812 get_edge_normal(prevEdge, &prevNormal);
1813 }
1814 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001815 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001816 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001817 if (prev->fPoint != next->fPoint) {
1818 join->fLine.normalize();
1819 join->fLine = join->fLine * join->fWinding;
1820 }
senorblancof57372d2016-08-31 10:36:19 -07001821 insert_edge(join, e, boundary);
1822 remove_edge(prevEdge, boundary);
1823 remove_edge(e, boundary);
1824 if (join->fLeft && join->fRight) {
1825 prevEdge = join->fLeft;
1826 e = join;
1827 } else {
1828 prevEdge = boundary->fTail;
1829 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1830 }
1831 get_edge_normal(prevEdge, &prevNormal);
1832 } else {
1833 prevEdge = e;
1834 prevNormal = normal;
1835 e = e->fRight;
1836 }
1837 }
1838}
1839
Stephen Whitec4dbc372019-05-22 10:50:14 -04001840void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1841 if (v == dest) {
1842 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001843 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001844 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001845 if (v->fSynthetic) {
1846 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1847 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001848 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1849 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001850 v->fPartner->fPartner = dest;
1851 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001852 }
1853}
1854
Stephen Whitec4dbc372019-05-22 10:50:14 -04001855void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1856 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001857 return;
1858 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001859 Vertex* prev = fEdge->fPrev->fVertex;
1860 Vertex* next = fEdge->fNext->fVertex;
1861 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1862 SSEdge* nextEdge = fEdge->fNext->fNext;
1863 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1864 return;
Stephen White77169c82018-06-05 09:15:59 -04001865 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001866 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1867 dest->fSynthetic = true;
1868 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001869 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1870 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1871 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001872 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001873
Stephen Whitec4dbc372019-05-22 10:50:14 -04001874 ss_connect(prev, dest, c, alloc);
1875 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001876
Stephen Whitec4dbc372019-05-22 10:50:14 -04001877 prevEdge->fNext = nextEdge->fPrev = ssv;
1878 ssv->fPrev = prevEdge;
1879 ssv->fNext = nextEdge;
1880 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1881 return;
1882 }
1883 if (prevEdge->fEvent) {
1884 prevEdge->fEvent->fEdge = nullptr;
1885 }
1886 if (nextEdge->fEvent) {
1887 nextEdge->fEvent->fEdge = nullptr;
1888 }
1889 if (prevEdge->fPrev == nextEdge->fNext) {
1890 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1891 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1892 } else {
1893 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1894 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1895 if (dest->fPartner) {
1896 create_event(prevEdge, events, alloc);
1897 create_event(nextEdge, events, alloc);
1898 } else {
1899 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1900 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1901 }
1902 }
Stephen Whitee260c462017-12-19 18:09:54 -05001903}
1904
1905bool is_overlap_edge(Edge* e) {
1906 if (e->fType == Edge::Type::kOuter) {
1907 return e->fWinding != 0 && e->fWinding != 1;
1908 } else if (e->fType == Edge::Type::kInner) {
1909 return e->fWinding != 0 && e->fWinding != -2;
1910 } else {
1911 return false;
1912 }
1913}
1914
1915// This is a stripped-down version of tessellate() which computes edges which
1916// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001917bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1918 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001919 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001920 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001921 EventList events(comp);
1922 SSVertexMap ssVertices;
1923 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001924 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001925 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001926 continue;
1927 }
1928 Edge* leftEnclosingEdge;
1929 Edge* rightEnclosingEdge;
1930 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001931 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001932 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1933 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001934 bool leftOverlap = prev && is_overlap_edge(prev);
1935 bool rightOverlap = is_overlap_edge(e);
1936 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1937 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001938 if (prev) {
1939 e->fWinding -= prev->fWinding;
1940 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001941 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001942 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1943 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001944 disconnect(e);
1945 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001946 TESS_LOG("found overlap edge %g -> %g%s\n",
1947 e->fTop->fID, e->fBottom->fID,
1948 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001949 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1950 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1951 SSVertex* ssPrev = ssVertices[prevVertex];
1952 if (!ssPrev) {
1953 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1954 }
1955 SSVertex* ssNext = ssVertices[nextVertex];
1956 if (!ssNext) {
1957 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1958 }
1959 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1960 ssEdges.push_back(ssEdge);
1961// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1962 ssPrev->fNext = ssNext->fPrev = ssEdge;
1963 create_event(ssEdge, &events, alloc);
1964 if (!isOuterBoundary) {
1965 disconnect(e);
1966 }
1967 }
1968 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001969 }
1970 Edge* prev = leftEnclosingEdge;
1971 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1972 if (prev) {
1973 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001974 }
1975 insert_edge(e, prev, &activeEdges);
1976 prev = e;
1977 }
1978 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001979 bool complex = events.size() > 0;
1980
Brian Salomon120e7d62019-09-11 10:29:22 -04001981 TESS_LOG("\ncollapsing overlap regions\n");
1982 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001983 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001984 while (events.size() > 0) {
1985 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001986 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001987 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001988 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001989 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001990 dump_skel(ssEdges);
1991 for (SSEdge* edge : ssEdges) {
1992 if (Edge* e = edge->fEdge) {
1993 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1994 }
1995 }
1996 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001997}
1998
1999bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
2000 if (!prev || !next) {
2001 return true;
2002 }
2003 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
2004 return winding != origEdge->fWinding;
2005}
Stephen White92eba8a2017-02-06 09:50:27 -05002006
senorblancof57372d2016-08-31 10:36:19 -07002007// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
2008// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
2009// new antialiased mesh from those vertices.
2010
Stephen Whitee260c462017-12-19 18:09:54 -05002011void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
2012 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002013 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002014 // A boundary with fewer than 3 edges is degenerate.
2015 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
2016 return;
2017 }
2018 Edge* prevEdge = boundary->fTail;
2019 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
2020 SkVector prevNormal;
2021 get_edge_normal(prevEdge, &prevNormal);
2022 double radius = 0.5;
2023 Line prevInner(prevEdge->fLine);
2024 prevInner.fC -= radius;
2025 Line prevOuter(prevEdge->fLine);
2026 prevOuter.fC += radius;
2027 VertexList innerVertices;
2028 VertexList outerVertices;
2029 bool innerInversion = true;
2030 bool outerInversion = true;
2031 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
2032 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2033 SkVector normal;
2034 get_edge_normal(e, &normal);
2035 Line inner(e->fLine);
2036 inner.fC -= radius;
2037 Line outer(e->fLine);
2038 outer.fC += radius;
2039 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04002040 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002041 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2042 prevOuter.intersect(outer, &outerPoint)) {
2043 float cosAngle = normal.dot(prevNormal);
2044 if (cosAngle < -kCosMiterAngle) {
2045 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2046
2047 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2048 Line bisector(innerPoint, outerPoint);
2049 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2050 if (tangent.fA == 0 && tangent.fB == 0) {
2051 continue;
2052 }
2053 tangent.normalize();
2054 Line innerTangent(tangent);
2055 Line outerTangent(tangent);
2056 innerTangent.fC -= 0.5;
2057 outerTangent.fC += 0.5;
2058 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2059 if (prevNormal.cross(normal) > 0) {
2060 // Miter inner points
2061 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2062 !innerTangent.intersect(inner, &innerPoint2) ||
2063 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002064 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002065 }
2066 Line prevTangent(prevV->fPoint,
2067 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2068 Line nextTangent(nextV->fPoint,
2069 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002070 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002071 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002072 }
2073 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002074 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002075 }
2076 outerPoint1 = outerPoint2 = outerPoint;
2077 } else {
2078 // Miter outer points
2079 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2080 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002081 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002082 }
2083 Line prevTangent(prevV->fPoint,
2084 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2085 Line nextTangent(nextV->fPoint,
2086 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002087 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002088 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002089 }
2090 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002091 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002092 }
2093 innerPoint1 = innerPoint2 = innerPoint;
2094 }
Stephen Whiteea495232018-04-03 11:28:15 -04002095 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2096 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2097 continue;
2098 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002099 TESS_LOG("inner (%g, %g), (%g, %g), ",
2100 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2101 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2102 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002103 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2104 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2105 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2106 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2107 innerVertex1->fPartner = outerVertex1;
2108 innerVertex2->fPartner = outerVertex2;
2109 outerVertex1->fPartner = innerVertex1;
2110 outerVertex2->fPartner = innerVertex2;
2111 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2112 innerInversion = false;
2113 }
2114 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2115 outerInversion = false;
2116 }
2117 innerVertices.append(innerVertex1);
2118 innerVertices.append(innerVertex2);
2119 outerVertices.append(outerVertex1);
2120 outerVertices.append(outerVertex2);
2121 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002122 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2123 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002124 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2125 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2126 innerVertex->fPartner = outerVertex;
2127 outerVertex->fPartner = innerVertex;
2128 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2129 innerInversion = false;
2130 }
2131 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2132 outerInversion = false;
2133 }
2134 innerVertices.append(innerVertex);
2135 outerVertices.append(outerVertex);
2136 }
2137 }
2138 prevInner = inner;
2139 prevOuter = outer;
2140 prevV = v;
2141 prevEdge = e;
2142 prevNormal = normal;
2143 }
2144 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2145 innerInversion = false;
2146 }
2147 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2148 outerInversion = false;
2149 }
2150 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2151 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2152 // interior inverts).
2153 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2154 // so it will be detected during collapse_overlap_regions().
2155 int innerWinding = innerInversion ? 2 : -2;
2156 int outerWinding = outerInversion ? -1 : 1;
2157 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2158 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2159 }
2160 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2161 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2162 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2163 }
2164 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2165 innerMesh->append(innerVertices);
2166 outerMesh->append(outerVertices);
2167}
senorblancof57372d2016-08-31 10:36:19 -07002168
Mike Reed7d34dc72019-11-26 12:17:17 -05002169void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002170 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002171 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002172 Vertex* start = down ? e->fTop : e->fBottom;
2173 do {
senorblancof57372d2016-08-31 10:36:19 -07002174 e->fWinding = down ? 1 : -1;
2175 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002176 e->fLine.normalize();
2177 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002178 boundary->append(e);
2179 if (down) {
2180 // Find outgoing edge, in clockwise order.
2181 if ((next = e->fNextEdgeAbove)) {
2182 down = false;
2183 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2184 down = true;
2185 } else if ((next = e->fPrevEdgeAbove)) {
2186 down = false;
2187 }
2188 } else {
2189 // Find outgoing edge, in counter-clockwise order.
2190 if ((next = e->fPrevEdgeBelow)) {
2191 down = true;
2192 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2193 down = false;
2194 } else if ((next = e->fNextEdgeBelow)) {
2195 down = true;
2196 }
2197 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002198 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002199 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002200 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002201}
2202
Stephen White5ad721e2017-02-23 16:50:47 -05002203// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002204
Stephen Whitebda29c02017-03-13 15:10:13 -04002205void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Mike Reed7d34dc72019-11-26 12:17:17 -05002206 VertexList* outerVertices, SkPathFillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002207 Comparator& c, SkArenaAlloc& alloc) {
2208 remove_non_boundary_edges(inMesh, fillType, alloc);
2209 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002210 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002211 EdgeList boundary;
2212 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2213 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002214 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002215 }
2216 }
senorblancof57372d2016-08-31 10:36:19 -07002217}
2218
Stephen Whitebda29c02017-03-13 15:10:13 -04002219// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002220
Chris Daltondcc8c542020-01-28 17:55:56 -07002221void contours_to_mesh(VertexList* contours, int contourCnt, Mode mode,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002222 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002223#if LOGGING_ENABLED
2224 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002225 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002226 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002227 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002228 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002229 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002230 }
2231 }
2232#endif
Chris Daltondcc8c542020-01-28 17:55:56 -07002233 sanitize_contours(contours, contourCnt, mode);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002234 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002235}
2236
Stephen Whitebda29c02017-03-13 15:10:13 -04002237void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002238 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002239 return;
ethannicholase9709e82016-01-07 13:34:16 -08002240 }
2241
2242 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002243 if (c.fDirection == Comparator::Direction::kHorizontal) {
2244 merge_sort<sweep_lt_horiz>(vertices);
2245 } else {
2246 merge_sort<sweep_lt_vert>(vertices);
2247 }
ethannicholase9709e82016-01-07 13:34:16 -08002248#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002249 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002250 static float gID = 0.0f;
2251 v->fID = gID++;
2252 }
2253#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002254}
2255
Mike Reed7d34dc72019-11-26 12:17:17 -05002256Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPathFillType fillType,
Chris Daltondcc8c542020-01-28 17:55:56 -07002257 const SkRect& pathBounds, Mode mode, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002258 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002259 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2260 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002261 VertexList mesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002262 contours_to_mesh(contours, contourCnt, mode, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002263 sort_mesh(&mesh, c, alloc);
2264 merge_coincident_vertices(&mesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002265 if (SimplifyResult::kAbort == simplify(mode, &mesh, c, alloc)) {
2266 return nullptr;
2267 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002268 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002269 dump_mesh(mesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002270 if (Mode::kEdgeAntialias == mode) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002271 VertexList innerMesh;
2272 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2273 sort_mesh(&innerMesh, c, alloc);
2274 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002275 merge_coincident_vertices(&innerMesh, c, alloc);
2276 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002277 auto result = simplify(mode, &innerMesh, c, alloc);
2278 SkASSERT(SimplifyResult::kAbort != result);
2279 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
2280 result = simplify(mode, outerMesh, c, alloc);
2281 SkASSERT(SimplifyResult::kAbort != result);
2282 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002283 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002284 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002285 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002286 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002287 EventComparator eventLT(EventComparator::Op::kLessThan);
2288 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2289 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2290 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002291 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002292 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002293 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002294 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002295 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002296 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002297 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002298 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002299 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002300 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2301 merge_coincident_vertices(&aaMesh, c, alloc);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002302 result = simplify(mode, &aaMesh, c, alloc);
2303 SkASSERT(SimplifyResult::kAbort != result);
Brian Salomon120e7d62019-09-11 10:29:22 -04002304 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002305 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002306 outerMesh->fHead = outerMesh->fTail = nullptr;
Chris Dalton6ccc0322020-01-29 11:38:16 -07002307 return tessellate(fillType, mode, aaMesh, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002308 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002309 TESS_LOG("no complex polygons; taking fast path\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07002310 return tessellate(fillType, mode, innerMesh, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002311 }
Stephen White49789062017-02-21 10:35:49 -05002312 } else {
Chris Dalton6ccc0322020-01-29 11:38:16 -07002313 return tessellate(fillType, mode, mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002314 }
senorblancof57372d2016-08-31 10:36:19 -07002315}
2316
2317// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Daltondcc8c542020-01-28 17:55:56 -07002318void* polys_to_triangles(Poly* polys, SkPathFillType fillType, Mode mode, void* data) {
2319 bool emitCoverage = (Mode::kEdgeAntialias == mode);
senorblancof57372d2016-08-31 10:36:19 -07002320 for (Poly* poly = polys; poly; poly = poly->fNext) {
2321 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002322 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002323 }
2324 }
2325 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002326}
2327
halcanary9d524f22016-03-29 09:03:52 -07002328Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Daltondcc8c542020-01-28 17:55:56 -07002329 int contourCnt, SkArenaAlloc& alloc, Mode mode, bool* isLinear,
Stephen Whitebda29c02017-03-13 15:10:13 -04002330 VertexList* outerMesh) {
Mike Reedcf0e3c62019-12-03 16:26:15 -05002331 SkPathFillType fillType = path.getFillType();
Mike Reed7d34dc72019-11-26 12:17:17 -05002332 if (SkPathFillType_IsInverse(fillType)) {
ethannicholase9709e82016-01-07 13:34:16 -08002333 contourCnt++;
2334 }
Stephen White3a9aab92017-03-07 14:07:18 -05002335 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002336
Chris Dalton6ccc0322020-01-29 11:38:16 -07002337 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, mode, isLinear);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002338 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Chris Daltondcc8c542020-01-28 17:55:56 -07002339 mode, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002340}
2341
Stephen White11f65e02017-02-16 19:00:39 -05002342int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002343 // We could theoretically be more aggressive about not counting empty contours, but we need to
2344 // actually match the exact number of contour linked lists the tessellator will create later on.
2345 int contourCnt = 1;
2346 bool hasPoints = false;
2347
2348 SkPath::Iter iter(path, false);
2349 SkPath::Verb verb;
2350 SkPoint pts[4];
2351 bool first = true;
2352 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
2353 switch (verb) {
2354 case SkPath::kMove_Verb:
2355 if (!first) {
2356 ++contourCnt;
2357 }
2358 // fallthru.
2359 case SkPath::kLine_Verb:
2360 case SkPath::kConic_Verb:
2361 case SkPath::kQuad_Verb:
2362 case SkPath::kCubic_Verb:
2363 hasPoints = true;
2364 // fallthru to break.
2365 default:
2366 break;
2367 }
2368 first = false;
2369 }
2370 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002371 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002372 }
Stephen White11f65e02017-02-16 19:00:39 -05002373 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002374}
2375
Mike Reed7d34dc72019-11-26 12:17:17 -05002376int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002377 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002378 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002379 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002380 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2381 }
2382 }
2383 return count;
2384}
2385
Greg Danield5b45932018-06-07 13:15:10 -04002386int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2387 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002388 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2389 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2390 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2391 }
2392 }
2393 return count;
2394}
2395
Brian Osman0995fd52019-01-09 09:52:25 -05002396void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002397 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2398 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2399 Vertex* v0 = e->fTop;
2400 Vertex* v1 = e->fBottom;
2401 Vertex* v2 = e->fBottom->fPartner;
2402 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002403 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2404 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002405 }
2406 }
2407 return data;
2408}
2409
ethannicholase9709e82016-01-07 13:34:16 -08002410} // namespace
2411
2412namespace GrTessellator {
2413
2414// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2415
halcanary9d524f22016-03-29 09:03:52 -07002416int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Daltondcc8c542020-01-28 17:55:56 -07002417 GrEagerVertexAllocator* vertexAllocator, Mode mode, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002418 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002419 if (contourCnt <= 0) {
2420 *isLinear = true;
2421 return 0;
2422 }
Stephen White11f65e02017-02-16 19:00:39 -05002423 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002424 VertexList outerMesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002425 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, mode,
Stephen Whitebda29c02017-03-13 15:10:13 -04002426 isLinear, &outerMesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002427 SkPathFillType fillType = (Mode::kEdgeAntialias == mode) ?
2428 SkPathFillType::kWinding : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002429 int64_t count64 = count_points(polys, fillType);
Chris Daltondcc8c542020-01-28 17:55:56 -07002430 if (Mode::kEdgeAntialias == mode) {
Greg Danield5b45932018-06-07 13:15:10 -04002431 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002432 }
Greg Danield5b45932018-06-07 13:15:10 -04002433 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002434 return 0;
2435 }
Greg Danield5b45932018-06-07 13:15:10 -04002436 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002437
Chris Daltondcc8c542020-01-28 17:55:56 -07002438 size_t vertexStride = GetVertexStride(mode);
Chris Daltond081dce2020-01-23 12:09:04 -07002439 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002440 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002441 SkDebugf("Could not allocate vertices\n");
2442 return 0;
2443 }
senorblancof57372d2016-08-31 10:36:19 -07002444
Brian Salomon120e7d62019-09-11 10:29:22 -04002445 TESS_LOG("emitting %d verts\n", count);
Chris Daltondcc8c542020-01-28 17:55:56 -07002446 void* end = polys_to_triangles(polys, fillType, mode, verts);
Brian Osman80879d42019-01-07 16:15:27 -05002447 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002448
senorblancof57372d2016-08-31 10:36:19 -07002449 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002450 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002451 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002452 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002453 return actualCount;
2454}
2455
halcanary9d524f22016-03-29 09:03:52 -07002456int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002457 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002458 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002459 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002460 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002461 return 0;
2462 }
Stephen White11f65e02017-02-16 19:00:39 -05002463 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002464 bool isLinear;
Chris Daltondcc8c542020-01-28 17:55:56 -07002465 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, Mode::kNormal,
2466 &isLinear, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002467 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002468 int64_t count64 = count_points(polys, fillType);
2469 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002470 *verts = nullptr;
2471 return 0;
2472 }
Greg Danield5b45932018-06-07 13:15:10 -04002473 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002474
2475 *verts = new GrTessellator::WindingVertex[count];
2476 GrTessellator::WindingVertex* vertsEnd = *verts;
2477 SkPoint* points = new SkPoint[count];
2478 SkPoint* pointsEnd = points;
2479 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002480 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002481 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002482 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002483 while (start != pointsEnd) {
2484 vertsEnd->fPos = *start;
2485 vertsEnd->fWinding = poly->fWinding;
2486 ++start;
2487 ++vertsEnd;
2488 }
2489 }
2490 }
2491 int actualCount = static_cast<int>(vertsEnd - *verts);
2492 SkASSERT(actualCount <= count);
2493 SkASSERT(pointsEnd - points == actualCount);
2494 delete[] points;
2495 return actualCount;
2496}
2497
2498} // namespace