blob: 4813a806e015384a27a28b42bb1a51b5279aa945 [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 {
senorblanco531237e2016-06-02 11:36:48 -0700583 MonotonePoly(Edge* edge, Side side)
584 : fSide(side)
585 , fFirstEdge(nullptr)
586 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800587 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700588 , fNext(nullptr) {
589 this->addEdge(edge);
590 }
ethannicholase9709e82016-01-07 13:34:16 -0800591 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700592 Edge* fFirstEdge;
593 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800594 MonotonePoly* fPrev;
595 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700596 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800597 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700598 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700599 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
600 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700601 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800602 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700603 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700604 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
605 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700606 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800607 }
ethannicholase9709e82016-01-07 13:34:16 -0800608 }
609
Brian Osman0995fd52019-01-09 09:52:25 -0500610 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700611 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700612 VertexList vertices;
613 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500614 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700615 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700616 if (kRight_Side == fSide) {
617 vertices.append(e->fBottom);
618 e = e->fRightPolyNext;
619 } else {
620 vertices.prepend(e->fBottom);
621 e = e->fLeftPolyNext;
622 }
Stephen White651cbe92017-03-03 12:24:16 -0500623 count++;
senorblanco531237e2016-06-02 11:36:48 -0700624 }
625 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800626 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700627 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800628 SkASSERT(v && v->fPrev && v->fNext);
629 Vertex* prev = v->fPrev;
630 Vertex* curr = v;
631 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500632 if (count == 3) {
Brian Osman0995fd52019-01-09 09:52:25 -0500633 return emit_triangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500634 }
ethannicholase9709e82016-01-07 13:34:16 -0800635 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
636 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
637 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
638 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
639 if (ax * by - ay * bx >= 0.0) {
Brian Osman0995fd52019-01-09 09:52:25 -0500640 data = emit_triangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800641 v->fPrev->fNext = v->fNext;
642 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500643 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800644 if (v->fPrev == first) {
645 v = v->fNext;
646 } else {
647 v = v->fPrev;
648 }
649 } else {
650 v = v->fNext;
651 }
652 }
653 return data;
654 }
655 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500656 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400657 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
658 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800659 Poly* partner = fPartner;
660 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700661 if (side == kRight_Side) {
662 if (e->fUsedInRightPoly) {
663 return this;
664 }
665 } else {
666 if (e->fUsedInLeftPoly) {
667 return this;
668 }
669 }
ethannicholase9709e82016-01-07 13:34:16 -0800670 if (partner) {
671 fPartner = partner->fPartner = nullptr;
672 }
senorblanco531237e2016-06-02 11:36:48 -0700673 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500674 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700675 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700676 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
677 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700678 } else if (side == fTail->fSide) {
679 fTail->addEdge(e);
680 fCount++;
681 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500682 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700683 fTail->addEdge(e);
684 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800685 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700686 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800687 poly = partner;
688 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500689 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700690 m->fPrev = fTail;
691 fTail->fNext = m;
692 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800693 }
694 }
ethannicholase9709e82016-01-07 13:34:16 -0800695 return poly;
696 }
Brian Osman0995fd52019-01-09 09:52:25 -0500697 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800698 if (fCount < 3) {
699 return data;
700 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400701 TESS_LOG("emit() %d, size %d\n", fID, fCount);
ethannicholase9709e82016-01-07 13:34:16 -0800702 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500703 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800704 }
705 return data;
706 }
senorblanco531237e2016-06-02 11:36:48 -0700707 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
708 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800709 int fWinding;
710 MonotonePoly* fHead;
711 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800712 Poly* fNext;
713 Poly* fPartner;
714 int fCount;
715#if LOGGING_ENABLED
716 int fID;
717#endif
718};
719
720/***************************************************************************************/
721
722bool coincident(const SkPoint& a, const SkPoint& b) {
723 return a == b;
724}
725
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500726Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
727 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800728 poly->fNext = *head;
729 *head = poly;
730 return poly;
731}
732
Stephen White3a9aab92017-03-07 14:07:18 -0500733void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500734 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800735#if LOGGING_ENABLED
736 static float gID = 0.0f;
737 v->fID = gID++;
738#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500739 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800740}
741
Stephen White36e4f062017-03-27 16:11:31 -0400742SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
743 SkQuadCoeff quad(pts);
744 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
745 SkPoint mid = to_point(quad.eval(t));
746 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400747 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
748 return 0;
749 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500750 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400751}
752
753void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
754 SkArenaAlloc& alloc) {
755 SkQuadCoeff quad(pts);
756 Sk2s aa = quad.fA * quad.fA;
757 SkScalar denom = 2.0f * (aa[0] + aa[1]);
758 Sk2s ab = quad.fA * quad.fB;
759 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
760 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500761 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400762 // Test possible subdivision values only at the point of maximum curvature.
763 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500764 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400765 u = 1.0f / nPoints;
766 if (quad_error_at(pts, t, u) < toleranceSqd) {
767 break;
768 }
769 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800770 }
Stephen White36e4f062017-03-27 16:11:31 -0400771 for (int j = 1; j <= nPoints; j++) {
772 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
773 }
ethannicholase9709e82016-01-07 13:34:16 -0800774}
775
Stephen White3a9aab92017-03-07 14:07:18 -0500776void generate_cubic_points(const SkPoint& p0,
777 const SkPoint& p1,
778 const SkPoint& p2,
779 const SkPoint& p3,
780 SkScalar tolSqd,
781 VertexList* contour,
782 int pointsLeft,
783 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500784 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
785 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800786 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
787 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500788 append_point_to_contour(p3, contour, alloc);
789 return;
ethannicholase9709e82016-01-07 13:34:16 -0800790 }
791 const SkPoint q[] = {
792 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
793 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
794 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
795 };
796 const SkPoint r[] = {
797 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
798 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
799 };
800 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
801 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500802 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
803 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800804}
805
806// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
807
808void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500809 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800810 SkScalar toleranceSqd = tolerance * tolerance;
811
812 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800813 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500814 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800815 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800816 if (path.isInverseFillType()) {
817 SkPoint quad[4];
818 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700819 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500820 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800821 }
Stephen White3a9aab92017-03-07 14:07:18 -0500822 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800823 }
824 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500825 SkPath::Verb verb;
Mike Reedba7e9a62019-08-16 13:30:34 -0400826 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800827 switch (verb) {
828 case SkPath::kConic_Verb: {
829 SkScalar weight = iter.conicWeight();
830 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
831 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400832 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800833 quadPts += 2;
834 }
835 *isLinear = false;
836 break;
837 }
838 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500839 if (contour->fHead) {
840 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800841 }
Stephen White3a9aab92017-03-07 14:07:18 -0500842 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800843 break;
844 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500845 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800846 break;
847 }
848 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400849 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800850 *isLinear = false;
851 break;
852 }
853 case SkPath::kCubic_Verb: {
854 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500855 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
856 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800857 *isLinear = false;
858 break;
859 }
860 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800861 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800862 break;
863 }
864 }
865}
866
Mike Reed7d34dc72019-11-26 12:17:17 -0500867inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800868 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500869 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800870 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500871 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800872 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500873 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700874 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500875 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800876 return (winding & 1) == 1;
877 default:
878 SkASSERT(false);
879 return false;
880 }
881}
882
Mike Reed7d34dc72019-11-26 12:17:17 -0500883inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500884 return poly && apply_fill_type(fillType, poly->fWinding);
885}
886
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500887Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500888 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800889 Vertex* top = winding < 0 ? next : prev;
890 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500891 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800892}
893
894void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400895 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700896 SkASSERT(edges->contains(edge));
897 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800898}
899
900void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400901 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700902 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800903 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700904 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800905}
906
907void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500908 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800909 *left = v->fFirstEdgeAbove->fLeft;
910 *right = v->fLastEdgeAbove->fRight;
911 return;
912 }
913 Edge* next = nullptr;
914 Edge* prev;
915 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
916 if (prev->isLeftOf(v)) {
917 break;
918 }
919 next = prev;
920 }
921 *left = prev;
922 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800923}
924
ethannicholase9709e82016-01-07 13:34:16 -0800925void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
926 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500927 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800928 return;
929 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400930 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
931 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800932 Edge* prev = nullptr;
933 Edge* next;
934 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
935 if (next->isRightOf(edge->fTop)) {
936 break;
937 }
938 prev = next;
939 }
senorblancoe6eaa322016-03-08 09:06:44 -0800940 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800941 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
942}
943
944void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
945 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500946 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800947 return;
948 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400949 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
950 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800951 Edge* prev = nullptr;
952 Edge* next;
953 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
954 if (next->isRightOf(edge->fBottom)) {
955 break;
956 }
957 prev = next;
958 }
senorblancoe6eaa322016-03-08 09:06:44 -0800959 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800960 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
961}
962
963void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400964 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400965 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
966 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800967 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800968 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
969}
970
971void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400972 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400973 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
974 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800975 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800976 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
977}
978
Stephen Whitee7a364d2017-01-11 16:19:26 -0500979void disconnect(Edge* edge)
980{
ethannicholase9709e82016-01-07 13:34:16 -0800981 remove_edge_above(edge);
982 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500983}
984
Stephen White3b5a3fa2017-06-06 14:51:19 -0400985void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
986
987void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
988 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
989 return;
990 }
991 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400992 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400993 while (v != dst) {
994 v = v->fPrev;
995 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
996 remove_edge(e, activeEdges);
997 }
998 Edge* leftEdge = v->fLeftEnclosingEdge;
999 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1000 insert_edge(e, leftEdge, activeEdges);
1001 leftEdge = e;
1002 }
1003 }
1004 *current = v;
1005}
1006
Stephen White3b5a3fa2017-06-06 14:51:19 -04001007void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001008 remove_edge_below(edge);
1009 edge->fTop = v;
1010 edge->recompute();
1011 insert_edge_below(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001012 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001013 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001014}
1015
Stephen White3b5a3fa2017-06-06 14:51:19 -04001016void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001017 remove_edge_above(edge);
1018 edge->fBottom = v;
1019 edge->recompute();
1020 insert_edge_above(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001021 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001022 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001023}
1024
Stephen White3b5a3fa2017-06-06 14:51:19 -04001025void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1026 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001027 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001028 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1029 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1030 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001031 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001032 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001033 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001034 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001035 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001036 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001037 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001038 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001039 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001040 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001041 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001042 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001043 }
1044}
1045
Stephen White3b5a3fa2017-06-06 14:51:19 -04001046void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1047 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001048 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001049 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1050 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1051 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001052 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001053 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001054 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001055 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001056 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001057 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001058 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001059 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001060 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001061 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001062 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001063 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001064 }
1065}
1066
Stephen Whited26b4d82018-07-26 10:02:27 -04001067bool top_collinear(Edge* left, Edge* right) {
1068 if (!left || !right) {
1069 return false;
1070 }
1071 return left->fTop->fPoint == right->fTop->fPoint ||
1072 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1073}
1074
1075bool bottom_collinear(Edge* left, Edge* right) {
1076 if (!left || !right) {
1077 return false;
1078 }
1079 return left->fBottom->fPoint == right->fBottom->fPoint ||
1080 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1081}
1082
Stephen White3b5a3fa2017-06-06 14:51:19 -04001083void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001084 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001085 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001086 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001087 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001088 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001089 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001090 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001091 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001092 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001093 } else {
1094 break;
1095 }
ethannicholase9709e82016-01-07 13:34:16 -08001096 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001097 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1098 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1099 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1100 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001101}
1102
Stephen White89042d52018-06-08 12:18:22 -04001103bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001104 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001105 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001106 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001107 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001108 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1109 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001110 Vertex* top;
1111 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001112 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001113 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001114 top = v;
1115 bottom = edge->fTop;
1116 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001117 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001118 top = edge->fBottom;
1119 bottom = v;
1120 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001121 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001122 top = v;
1123 bottom = edge->fBottom;
1124 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001125 }
Stephen White531a48e2018-06-01 09:49:39 -04001126 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001127 insert_edge_below(newEdge, top, c);
1128 insert_edge_above(newEdge, bottom, c);
1129 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001130 return true;
1131}
1132
1133bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1134 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1135 return false;
1136 }
Stephen White1c5fd182018-07-12 15:54:05 -04001137 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1138 return false;
1139 }
Stephen White89042d52018-06-08 12:18:22 -04001140 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1141 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001142 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001143 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1144 }
1145 } else {
1146 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001147 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001148 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1149 }
1150 }
1151 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1152 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001153 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001154 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1155 }
1156 } else {
1157 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001158 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001159 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1160 }
1161 }
1162 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001163}
1164
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001165Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001166 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001167 if (!prev || !next || prev->fPoint == next->fPoint) {
1168 return nullptr;
1169 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001170 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001171 insert_edge_below(edge, edge->fTop, c);
1172 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001173 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001174 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001175 return edge;
1176}
1177
Stephen Whitebf6137e2017-01-04 15:43:26 -05001178void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001179 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001180 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1181 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001182 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001183 if (src->fPartner) {
1184 src->fPartner->fPartner = dst;
1185 }
Stephen White7b376942018-05-22 11:51:32 -04001186 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001187 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001188 }
Stephen White7b376942018-05-22 11:51:32 -04001189 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001190 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001191 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001192 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001193 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001194}
1195
Stephen White95152e12017-12-18 10:52:44 -05001196Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1197 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1198 Vertex* prevV = reference;
1199 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1200 prevV = prevV->fPrev;
1201 }
1202 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1203 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1204 prevV = nextV;
1205 nextV = nextV->fNext;
1206 }
1207 Vertex* v;
1208 if (prevV && coincident(prevV->fPoint, p)) {
1209 v = prevV;
1210 } else if (nextV && coincident(nextV->fPoint, p)) {
1211 v = nextV;
1212 } else {
1213 v = alloc.make<Vertex>(p, alpha);
1214#if LOGGING_ENABLED
1215 if (!prevV) {
1216 v->fID = mesh->fHead->fID - 1.0f;
1217 } else if (!nextV) {
1218 v->fID = mesh->fTail->fID + 1.0f;
1219 } else {
1220 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1221 }
1222#endif
1223 mesh->insert(v, prevV, nextV);
1224 }
1225 return v;
1226}
1227
Stephen White53a02982018-05-30 22:47:46 -04001228// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1229// sort criterion, it may not be possible to split correctly, since there is no point which is
1230// below the top and above the bottom. This function detects that case.
1231bool nearly_flat(Comparator& c, Edge* edge) {
1232 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1233 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001234 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001235}
1236
Stephen Whitee62999f2018-06-05 18:45:07 -04001237SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1238 if (c.sweep_lt(p, min)) {
1239 return min;
1240 } else if (c.sweep_lt(max, p)) {
1241 return max;
1242 } else {
1243 return p;
1244 }
1245}
1246
Stephen Whitec4dbc372019-05-22 10:50:14 -04001247void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1248 Line line1 = edge1->fLine;
1249 Line line2 = edge2->fLine;
1250 line1.normalize();
1251 line2.normalize();
1252 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1253 if (cosAngle > 0.999) {
1254 return;
1255 }
1256 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1257 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1258 SkPoint p;
1259 if (line1.intersect(line2, &p)) {
1260 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1261 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001262 TESS_LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001263 }
1264}
1265
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001266bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001267 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001268 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001269 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001270 }
Stephen White56158ae2017-01-30 14:31:31 -05001271 SkPoint p;
1272 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001273 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001274 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001275 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001276 Vertex* top = *current;
1277 // If the intersection point is above the current vertex, rewind to the vertex above the
1278 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001279 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001280 top = top->fPrev;
1281 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001282 if (!nearly_flat(c, left)) {
1283 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001284 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001285 if (!nearly_flat(c, right)) {
1286 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001287 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001288 if (p == left->fTop->fPoint) {
1289 v = left->fTop;
1290 } else if (p == left->fBottom->fPoint) {
1291 v = left->fBottom;
1292 } else if (p == right->fTop->fPoint) {
1293 v = right->fTop;
1294 } else if (p == right->fBottom->fPoint) {
1295 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001296 } else {
Stephen White95152e12017-12-18 10:52:44 -05001297 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001298 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001299 v->fSynthetic = true;
1300 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001301 }
ethannicholase9709e82016-01-07 13:34:16 -08001302 }
Stephen White0cb31672017-06-08 14:41:01 -04001303 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001304 split_edge(left, v, activeEdges, current, c, alloc);
1305 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001306 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001307 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001308 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001309 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001310}
1311
Chris Daltondcc8c542020-01-28 17:55:56 -07001312void sanitize_contours(VertexList* contours, int contourCnt, Mode mode) {
1313 bool approximate = (Mode::kEdgeAntialias == mode);
Stephen White3a9aab92017-03-07 14:07:18 -05001314 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1315 SkASSERT(contour->fHead);
1316 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001317 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001318 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001319 }
Stephen White3a9aab92017-03-07 14:07:18 -05001320 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001321 if (approximate) {
1322 round(&v->fPoint);
1323 }
Stephen White3a9aab92017-03-07 14:07:18 -05001324 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001325 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001326 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001327 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001328 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001329 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001330 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001331 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001332 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001333 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001334 contour->remove(v);
1335 } else {
1336 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001337 }
Stephen White3a9aab92017-03-07 14:07:18 -05001338 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001339 }
1340 }
1341}
1342
Stephen Whitee260c462017-12-19 18:09:54 -05001343bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001344 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001345 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001346 }
Stephen Whitee260c462017-12-19 18:09:54 -05001347 bool merged = false;
1348 for (Vertex* v = mesh->fHead->fNext; v;) {
1349 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001350 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1351 v->fPoint = v->fPrev->fPoint;
1352 }
1353 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001354 merge_vertices(v, v->fPrev, mesh, c, alloc);
1355 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001356 }
Stephen Whitee260c462017-12-19 18:09:54 -05001357 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001358 }
Stephen Whitee260c462017-12-19 18:09:54 -05001359 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001360}
1361
1362// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1363
Stephen White3a9aab92017-03-07 14:07:18 -05001364void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001365 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001366 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1367 Vertex* prev = contour->fTail;
1368 for (Vertex* v = contour->fHead; v;) {
1369 Vertex* next = v->fNext;
1370 connect(prev, v, Edge::Type::kInner, c, alloc);
1371 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001372 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001373 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001374 }
1375 }
ethannicholase9709e82016-01-07 13:34:16 -08001376}
1377
Stephen Whitee260c462017-12-19 18:09:54 -05001378void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1379 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001380 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001381 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1382 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1383 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1384 // number.
1385 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1386 inner->fPartner = outer->fPartner = nullptr;
1387 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001388 }
1389 }
1390}
1391
1392template <CompareFunc sweep_lt>
1393void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1394 Vertex* a = front->fHead;
1395 Vertex* b = back->fHead;
1396 while (a && b) {
1397 if (sweep_lt(a->fPoint, b->fPoint)) {
1398 front->remove(a);
1399 result->append(a);
1400 a = front->fHead;
1401 } else {
1402 back->remove(b);
1403 result->append(b);
1404 b = back->fHead;
1405 }
1406 }
1407 result->append(*front);
1408 result->append(*back);
1409}
1410
1411void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1412 if (c.fDirection == Comparator::Direction::kHorizontal) {
1413 sorted_merge<sweep_lt_horiz>(front, back, result);
1414 } else {
1415 sorted_merge<sweep_lt_vert>(front, back, result);
1416 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001417#if LOGGING_ENABLED
1418 float id = 0.0f;
1419 for (Vertex* v = result->fHead; v; v = v->fNext) {
1420 v->fID = id++;
1421 }
1422#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001423}
1424
ethannicholase9709e82016-01-07 13:34:16 -08001425// Stage 3: sort the vertices by increasing sweep direction.
1426
Stephen White16a40cb2017-02-23 11:10:01 -05001427template <CompareFunc sweep_lt>
1428void merge_sort(VertexList* vertices) {
1429 Vertex* slow = vertices->fHead;
1430 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001431 return;
1432 }
Stephen White16a40cb2017-02-23 11:10:01 -05001433 Vertex* fast = slow->fNext;
1434 if (!fast) {
1435 return;
1436 }
1437 do {
1438 fast = fast->fNext;
1439 if (fast) {
1440 fast = fast->fNext;
1441 slow = slow->fNext;
1442 }
1443 } while (fast);
1444 VertexList front(vertices->fHead, slow);
1445 VertexList back(slow->fNext, vertices->fTail);
1446 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001447
Stephen White16a40cb2017-02-23 11:10:01 -05001448 merge_sort<sweep_lt>(&front);
1449 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001450
Stephen White16a40cb2017-02-23 11:10:01 -05001451 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001452 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001453}
1454
Stephen White95152e12017-12-18 10:52:44 -05001455void dump_mesh(const VertexList& mesh) {
1456#if LOGGING_ENABLED
1457 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001458 TESS_LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001459 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001460 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1461 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001462 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001463 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001464 }
1465 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001466 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001467 }
1468 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001469 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001470 }
1471 }
1472#endif
1473}
1474
Stephen Whitec4dbc372019-05-22 10:50:14 -04001475void dump_skel(const SSEdgeList& ssEdges) {
1476#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001477 for (SSEdge* edge : ssEdges) {
1478 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001479 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001480 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001481 edge->fNext->fVertex->fID);
1482 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001483 TESS_LOG(" (original %g -> %g)\n",
1484 edge->fEdge->fTop->fID,
1485 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001486 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001487 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001488 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001489 }
1490 }
1491#endif
1492}
1493
Stephen White89042d52018-06-08 12:18:22 -04001494#ifdef SK_DEBUG
1495void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1496 if (!left || !right) {
1497 return;
1498 }
1499 if (left->fTop == right->fTop) {
1500 SkASSERT(left->isLeftOf(right->fBottom));
1501 SkASSERT(right->isRightOf(left->fBottom));
1502 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1503 SkASSERT(left->isLeftOf(right->fTop));
1504 } else {
1505 SkASSERT(right->isRightOf(left->fTop));
1506 }
1507 if (left->fBottom == right->fBottom) {
1508 SkASSERT(left->isLeftOf(right->fTop));
1509 SkASSERT(right->isRightOf(left->fTop));
1510 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1511 SkASSERT(left->isLeftOf(right->fBottom));
1512 } else {
1513 SkASSERT(right->isRightOf(left->fBottom));
1514 }
1515}
1516
1517void validate_edge_list(EdgeList* edges, Comparator& c) {
1518 Edge* left = edges->fHead;
1519 if (!left) {
1520 return;
1521 }
1522 for (Edge* right = left->fRight; right; right = right->fRight) {
1523 validate_edge_pair(left, right, c);
1524 left = right;
1525 }
1526}
1527#endif
1528
ethannicholase9709e82016-01-07 13:34:16 -08001529// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1530
Stephen Whitec4dbc372019-05-22 10:50:14 -04001531bool connected(Vertex* v) {
1532 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1533}
1534
Stephen Whitee260c462017-12-19 18:09:54 -05001535bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001536 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001537 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001538 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001539 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001540 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001541 continue;
1542 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001543 Edge* leftEnclosingEdge;
1544 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001545 bool restartChecks;
1546 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001547 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1548 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001549 restartChecks = false;
1550 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001551 v->fLeftEnclosingEdge = leftEnclosingEdge;
1552 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001553 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001554 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001555 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001556 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001557 restartChecks = true;
1558 break;
1559 }
Stephen White0cb31672017-06-08 14:41:01 -04001560 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001561 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001562 restartChecks = true;
1563 break;
1564 }
1565 }
1566 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001567 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001568 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001569 restartChecks = true;
1570 }
1571
1572 }
Stephen Whitee260c462017-12-19 18:09:54 -05001573 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001574 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001575#ifdef SK_DEBUG
1576 validate_edge_list(&activeEdges, c);
1577#endif
ethannicholase9709e82016-01-07 13:34:16 -08001578 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1579 remove_edge(e, &activeEdges);
1580 }
1581 Edge* leftEdge = leftEnclosingEdge;
1582 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1583 insert_edge(e, leftEdge, &activeEdges);
1584 leftEdge = e;
1585 }
ethannicholase9709e82016-01-07 13:34:16 -08001586 }
Stephen Whitee260c462017-12-19 18:09:54 -05001587 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1588 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001589}
1590
1591// Stage 5: Tessellate the simplified mesh into monotone polygons.
1592
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001593Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001594 TESS_LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001595 EdgeList activeEdges;
1596 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001597 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001598 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001599 continue;
1600 }
1601#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001602 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 -08001603#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001604 Edge* leftEnclosingEdge;
1605 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001606 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001607 Poly* leftPoly;
1608 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001609 if (v->fFirstEdgeAbove) {
1610 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1611 rightPoly = v->fLastEdgeAbove->fRightPoly;
1612 } else {
1613 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1614 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1615 }
1616#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001617 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001618 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001619 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1620 e->fTop->fID, e->fBottom->fID,
1621 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1622 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001623 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001624 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001625 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001626 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1627 e->fTop->fID, e->fBottom->fID,
1628 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1629 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001630 }
1631#endif
1632 if (v->fFirstEdgeAbove) {
1633 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001634 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001635 }
1636 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001637 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001638 }
1639 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001640 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001641 remove_edge(e, &activeEdges);
1642 if (e->fRightPoly) {
1643 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001644 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001645 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001646 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001647 }
1648 }
1649 remove_edge(v->fLastEdgeAbove, &activeEdges);
1650 if (!v->fFirstEdgeBelow) {
1651 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1652 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1653 rightPoly->fPartner = leftPoly;
1654 leftPoly->fPartner = rightPoly;
1655 }
1656 }
1657 }
1658 if (v->fFirstEdgeBelow) {
1659 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001660 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001661 if (leftPoly == rightPoly) {
1662 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1663 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1664 leftPoly->fWinding, alloc);
1665 leftEnclosingEdge->fRightPoly = leftPoly;
1666 } else {
1667 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1668 rightPoly->fWinding, alloc);
1669 rightEnclosingEdge->fLeftPoly = rightPoly;
1670 }
ethannicholase9709e82016-01-07 13:34:16 -08001671 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001672 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001673 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1674 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001675 }
1676 }
1677 Edge* leftEdge = v->fFirstEdgeBelow;
1678 leftEdge->fLeftPoly = leftPoly;
1679 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1680 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1681 rightEdge = rightEdge->fNextEdgeBelow) {
1682 insert_edge(rightEdge, leftEdge, &activeEdges);
1683 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1684 winding += leftEdge->fWinding;
1685 if (winding != 0) {
1686 Poly* poly = new_poly(&polys, v, winding, alloc);
1687 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1688 }
1689 leftEdge = rightEdge;
1690 }
1691 v->fLastEdgeBelow->fRightPoly = rightPoly;
1692 }
1693#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001694 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001695 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001696 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1697 e->fTop->fID, e->fBottom->fID,
1698 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1699 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001700 }
1701#endif
1702 }
1703 return polys;
1704}
1705
Mike Reed7d34dc72019-11-26 12:17:17 -05001706void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001707 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001708 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001709 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001710 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001711 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001712 continue;
1713 }
1714 Edge* leftEnclosingEdge;
1715 Edge* rightEnclosingEdge;
1716 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1717 bool prevFilled = leftEnclosingEdge &&
1718 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1719 for (Edge* e = v->fFirstEdgeAbove; e;) {
1720 Edge* next = e->fNextEdgeAbove;
1721 remove_edge(e, &activeEdges);
1722 bool filled = apply_fill_type(fillType, e->fWinding);
1723 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001724 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001725 }
Stephen White49789062017-02-21 10:35:49 -05001726 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001727 e = next;
1728 }
Stephen White49789062017-02-21 10:35:49 -05001729 Edge* prev = leftEnclosingEdge;
1730 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1731 if (prev) {
1732 e->fWinding += prev->fWinding;
1733 }
1734 insert_edge(e, prev, &activeEdges);
1735 prev = e;
1736 }
senorblancof57372d2016-08-31 10:36:19 -07001737 }
senorblancof57372d2016-08-31 10:36:19 -07001738}
1739
Stephen White66412122017-03-01 11:48:27 -05001740// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001741void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001742 normal->set(SkDoubleToScalar(e->fLine.fA),
1743 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001744}
1745
1746// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1747// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1748// invert on stroking.
1749
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001750void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001751 Edge* prevEdge = boundary->fTail;
1752 SkVector prevNormal;
1753 get_edge_normal(prevEdge, &prevNormal);
1754 for (Edge* e = boundary->fHead; e != nullptr;) {
1755 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1756 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001757 double distPrev = e->dist(prev->fPoint);
1758 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001759 SkVector normal;
1760 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001761 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001762 if (prev == next) {
1763 remove_edge(prevEdge, boundary);
1764 remove_edge(e, boundary);
1765 prevEdge = boundary->fTail;
1766 e = boundary->fHead;
1767 if (prevEdge) {
1768 get_edge_normal(prevEdge, &prevNormal);
1769 }
1770 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001771 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001772 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001773 if (prev->fPoint != next->fPoint) {
1774 join->fLine.normalize();
1775 join->fLine = join->fLine * join->fWinding;
1776 }
senorblancof57372d2016-08-31 10:36:19 -07001777 insert_edge(join, e, boundary);
1778 remove_edge(prevEdge, boundary);
1779 remove_edge(e, boundary);
1780 if (join->fLeft && join->fRight) {
1781 prevEdge = join->fLeft;
1782 e = join;
1783 } else {
1784 prevEdge = boundary->fTail;
1785 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1786 }
1787 get_edge_normal(prevEdge, &prevNormal);
1788 } else {
1789 prevEdge = e;
1790 prevNormal = normal;
1791 e = e->fRight;
1792 }
1793 }
1794}
1795
Stephen Whitec4dbc372019-05-22 10:50:14 -04001796void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1797 if (v == dest) {
1798 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001799 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001800 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001801 if (v->fSynthetic) {
1802 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1803 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001804 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1805 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001806 v->fPartner->fPartner = dest;
1807 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001808 }
1809}
1810
Stephen Whitec4dbc372019-05-22 10:50:14 -04001811void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1812 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001813 return;
1814 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001815 Vertex* prev = fEdge->fPrev->fVertex;
1816 Vertex* next = fEdge->fNext->fVertex;
1817 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1818 SSEdge* nextEdge = fEdge->fNext->fNext;
1819 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1820 return;
Stephen White77169c82018-06-05 09:15:59 -04001821 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001822 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1823 dest->fSynthetic = true;
1824 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001825 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1826 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1827 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001828 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001829
Stephen Whitec4dbc372019-05-22 10:50:14 -04001830 ss_connect(prev, dest, c, alloc);
1831 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001832
Stephen Whitec4dbc372019-05-22 10:50:14 -04001833 prevEdge->fNext = nextEdge->fPrev = ssv;
1834 ssv->fPrev = prevEdge;
1835 ssv->fNext = nextEdge;
1836 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1837 return;
1838 }
1839 if (prevEdge->fEvent) {
1840 prevEdge->fEvent->fEdge = nullptr;
1841 }
1842 if (nextEdge->fEvent) {
1843 nextEdge->fEvent->fEdge = nullptr;
1844 }
1845 if (prevEdge->fPrev == nextEdge->fNext) {
1846 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1847 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1848 } else {
1849 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1850 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1851 if (dest->fPartner) {
1852 create_event(prevEdge, events, alloc);
1853 create_event(nextEdge, events, alloc);
1854 } else {
1855 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1856 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1857 }
1858 }
Stephen Whitee260c462017-12-19 18:09:54 -05001859}
1860
1861bool is_overlap_edge(Edge* e) {
1862 if (e->fType == Edge::Type::kOuter) {
1863 return e->fWinding != 0 && e->fWinding != 1;
1864 } else if (e->fType == Edge::Type::kInner) {
1865 return e->fWinding != 0 && e->fWinding != -2;
1866 } else {
1867 return false;
1868 }
1869}
1870
1871// This is a stripped-down version of tessellate() which computes edges which
1872// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001873bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1874 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001875 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001876 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001877 EventList events(comp);
1878 SSVertexMap ssVertices;
1879 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001880 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001881 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001882 continue;
1883 }
1884 Edge* leftEnclosingEdge;
1885 Edge* rightEnclosingEdge;
1886 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001887 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001888 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1889 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001890 bool leftOverlap = prev && is_overlap_edge(prev);
1891 bool rightOverlap = is_overlap_edge(e);
1892 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1893 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001894 if (prev) {
1895 e->fWinding -= prev->fWinding;
1896 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001897 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001898 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1899 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001900 disconnect(e);
1901 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001902 TESS_LOG("found overlap edge %g -> %g%s\n",
1903 e->fTop->fID, e->fBottom->fID,
1904 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001905 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1906 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1907 SSVertex* ssPrev = ssVertices[prevVertex];
1908 if (!ssPrev) {
1909 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1910 }
1911 SSVertex* ssNext = ssVertices[nextVertex];
1912 if (!ssNext) {
1913 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1914 }
1915 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1916 ssEdges.push_back(ssEdge);
1917// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1918 ssPrev->fNext = ssNext->fPrev = ssEdge;
1919 create_event(ssEdge, &events, alloc);
1920 if (!isOuterBoundary) {
1921 disconnect(e);
1922 }
1923 }
1924 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001925 }
1926 Edge* prev = leftEnclosingEdge;
1927 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1928 if (prev) {
1929 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001930 }
1931 insert_edge(e, prev, &activeEdges);
1932 prev = e;
1933 }
1934 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001935 bool complex = events.size() > 0;
1936
Brian Salomon120e7d62019-09-11 10:29:22 -04001937 TESS_LOG("\ncollapsing overlap regions\n");
1938 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001939 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001940 while (events.size() > 0) {
1941 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001942 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001943 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001944 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001945 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001946 dump_skel(ssEdges);
1947 for (SSEdge* edge : ssEdges) {
1948 if (Edge* e = edge->fEdge) {
1949 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1950 }
1951 }
1952 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001953}
1954
1955bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1956 if (!prev || !next) {
1957 return true;
1958 }
1959 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1960 return winding != origEdge->fWinding;
1961}
Stephen White92eba8a2017-02-06 09:50:27 -05001962
senorblancof57372d2016-08-31 10:36:19 -07001963// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1964// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1965// new antialiased mesh from those vertices.
1966
Stephen Whitee260c462017-12-19 18:09:54 -05001967void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1968 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001969 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001970 // A boundary with fewer than 3 edges is degenerate.
1971 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1972 return;
1973 }
1974 Edge* prevEdge = boundary->fTail;
1975 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1976 SkVector prevNormal;
1977 get_edge_normal(prevEdge, &prevNormal);
1978 double radius = 0.5;
1979 Line prevInner(prevEdge->fLine);
1980 prevInner.fC -= radius;
1981 Line prevOuter(prevEdge->fLine);
1982 prevOuter.fC += radius;
1983 VertexList innerVertices;
1984 VertexList outerVertices;
1985 bool innerInversion = true;
1986 bool outerInversion = true;
1987 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1988 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1989 SkVector normal;
1990 get_edge_normal(e, &normal);
1991 Line inner(e->fLine);
1992 inner.fC -= radius;
1993 Line outer(e->fLine);
1994 outer.fC += radius;
1995 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04001996 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001997 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1998 prevOuter.intersect(outer, &outerPoint)) {
1999 float cosAngle = normal.dot(prevNormal);
2000 if (cosAngle < -kCosMiterAngle) {
2001 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2002
2003 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2004 Line bisector(innerPoint, outerPoint);
2005 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2006 if (tangent.fA == 0 && tangent.fB == 0) {
2007 continue;
2008 }
2009 tangent.normalize();
2010 Line innerTangent(tangent);
2011 Line outerTangent(tangent);
2012 innerTangent.fC -= 0.5;
2013 outerTangent.fC += 0.5;
2014 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2015 if (prevNormal.cross(normal) > 0) {
2016 // Miter inner points
2017 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2018 !innerTangent.intersect(inner, &innerPoint2) ||
2019 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002020 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002021 }
2022 Line prevTangent(prevV->fPoint,
2023 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2024 Line nextTangent(nextV->fPoint,
2025 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002026 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002027 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002028 }
2029 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002030 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002031 }
2032 outerPoint1 = outerPoint2 = outerPoint;
2033 } else {
2034 // Miter outer points
2035 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2036 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002037 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002038 }
2039 Line prevTangent(prevV->fPoint,
2040 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2041 Line nextTangent(nextV->fPoint,
2042 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002043 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002044 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002045 }
2046 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002047 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002048 }
2049 innerPoint1 = innerPoint2 = innerPoint;
2050 }
Stephen Whiteea495232018-04-03 11:28:15 -04002051 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2052 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2053 continue;
2054 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002055 TESS_LOG("inner (%g, %g), (%g, %g), ",
2056 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2057 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2058 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002059 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2060 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2061 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2062 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2063 innerVertex1->fPartner = outerVertex1;
2064 innerVertex2->fPartner = outerVertex2;
2065 outerVertex1->fPartner = innerVertex1;
2066 outerVertex2->fPartner = innerVertex2;
2067 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2068 innerInversion = false;
2069 }
2070 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2071 outerInversion = false;
2072 }
2073 innerVertices.append(innerVertex1);
2074 innerVertices.append(innerVertex2);
2075 outerVertices.append(outerVertex1);
2076 outerVertices.append(outerVertex2);
2077 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002078 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2079 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002080 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2081 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2082 innerVertex->fPartner = outerVertex;
2083 outerVertex->fPartner = innerVertex;
2084 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2085 innerInversion = false;
2086 }
2087 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2088 outerInversion = false;
2089 }
2090 innerVertices.append(innerVertex);
2091 outerVertices.append(outerVertex);
2092 }
2093 }
2094 prevInner = inner;
2095 prevOuter = outer;
2096 prevV = v;
2097 prevEdge = e;
2098 prevNormal = normal;
2099 }
2100 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2101 innerInversion = false;
2102 }
2103 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2104 outerInversion = false;
2105 }
2106 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2107 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2108 // interior inverts).
2109 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2110 // so it will be detected during collapse_overlap_regions().
2111 int innerWinding = innerInversion ? 2 : -2;
2112 int outerWinding = outerInversion ? -1 : 1;
2113 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2114 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2115 }
2116 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2117 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2118 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2119 }
2120 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2121 innerMesh->append(innerVertices);
2122 outerMesh->append(outerVertices);
2123}
senorblancof57372d2016-08-31 10:36:19 -07002124
Mike Reed7d34dc72019-11-26 12:17:17 -05002125void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002126 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002127 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002128 Vertex* start = down ? e->fTop : e->fBottom;
2129 do {
senorblancof57372d2016-08-31 10:36:19 -07002130 e->fWinding = down ? 1 : -1;
2131 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002132 e->fLine.normalize();
2133 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002134 boundary->append(e);
2135 if (down) {
2136 // Find outgoing edge, in clockwise order.
2137 if ((next = e->fNextEdgeAbove)) {
2138 down = false;
2139 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2140 down = true;
2141 } else if ((next = e->fPrevEdgeAbove)) {
2142 down = false;
2143 }
2144 } else {
2145 // Find outgoing edge, in counter-clockwise order.
2146 if ((next = e->fPrevEdgeBelow)) {
2147 down = true;
2148 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2149 down = false;
2150 } else if ((next = e->fNextEdgeBelow)) {
2151 down = true;
2152 }
2153 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002154 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002155 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002156 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002157}
2158
Stephen White5ad721e2017-02-23 16:50:47 -05002159// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002160
Stephen Whitebda29c02017-03-13 15:10:13 -04002161void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Mike Reed7d34dc72019-11-26 12:17:17 -05002162 VertexList* outerVertices, SkPathFillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002163 Comparator& c, SkArenaAlloc& alloc) {
2164 remove_non_boundary_edges(inMesh, fillType, alloc);
2165 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002166 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002167 EdgeList boundary;
2168 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2169 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002170 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002171 }
2172 }
senorblancof57372d2016-08-31 10:36:19 -07002173}
2174
Stephen Whitebda29c02017-03-13 15:10:13 -04002175// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002176
Chris Daltondcc8c542020-01-28 17:55:56 -07002177void contours_to_mesh(VertexList* contours, int contourCnt, Mode mode,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002178 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002179#if LOGGING_ENABLED
2180 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002181 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002182 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002183 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002184 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002185 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002186 }
2187 }
2188#endif
Chris Daltondcc8c542020-01-28 17:55:56 -07002189 sanitize_contours(contours, contourCnt, mode);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002190 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002191}
2192
Stephen Whitebda29c02017-03-13 15:10:13 -04002193void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002194 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002195 return;
ethannicholase9709e82016-01-07 13:34:16 -08002196 }
2197
2198 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002199 if (c.fDirection == Comparator::Direction::kHorizontal) {
2200 merge_sort<sweep_lt_horiz>(vertices);
2201 } else {
2202 merge_sort<sweep_lt_vert>(vertices);
2203 }
ethannicholase9709e82016-01-07 13:34:16 -08002204#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002205 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002206 static float gID = 0.0f;
2207 v->fID = gID++;
2208 }
2209#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002210}
2211
Mike Reed7d34dc72019-11-26 12:17:17 -05002212Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPathFillType fillType,
Chris Daltondcc8c542020-01-28 17:55:56 -07002213 const SkRect& pathBounds, Mode mode, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002214 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002215 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2216 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002217 VertexList mesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002218 contours_to_mesh(contours, contourCnt, mode, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002219 sort_mesh(&mesh, c, alloc);
2220 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002221 simplify(&mesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002222 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002223 dump_mesh(mesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002224 if (Mode::kEdgeAntialias == mode) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002225 VertexList innerMesh;
2226 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2227 sort_mesh(&innerMesh, c, alloc);
2228 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002229 merge_coincident_vertices(&innerMesh, c, alloc);
2230 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2231 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2232 was_complex = simplify(outerMesh, c, alloc) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002233 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002234 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002235 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002236 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002237 EventComparator eventLT(EventComparator::Op::kLessThan);
2238 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2239 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2240 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002241 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002242 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002243 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002244 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002245 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002246 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002247 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002248 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002249 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002250 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2251 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002252 simplify(&aaMesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002253 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002254 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002255 outerMesh->fHead = outerMesh->fTail = nullptr;
2256 return tessellate(aaMesh, alloc);
2257 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002258 TESS_LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002259 return tessellate(innerMesh, alloc);
2260 }
Stephen White49789062017-02-21 10:35:49 -05002261 } else {
2262 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002263 }
senorblancof57372d2016-08-31 10:36:19 -07002264}
2265
2266// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Daltondcc8c542020-01-28 17:55:56 -07002267void* polys_to_triangles(Poly* polys, SkPathFillType fillType, Mode mode, void* data) {
2268 bool emitCoverage = (Mode::kEdgeAntialias == mode);
senorblancof57372d2016-08-31 10:36:19 -07002269 for (Poly* poly = polys; poly; poly = poly->fNext) {
2270 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002271 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002272 }
2273 }
2274 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002275}
2276
halcanary9d524f22016-03-29 09:03:52 -07002277Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Daltondcc8c542020-01-28 17:55:56 -07002278 int contourCnt, SkArenaAlloc& alloc, Mode mode, bool* isLinear,
Stephen Whitebda29c02017-03-13 15:10:13 -04002279 VertexList* outerMesh) {
Mike Reedcf0e3c62019-12-03 16:26:15 -05002280 SkPathFillType fillType = path.getFillType();
Mike Reed7d34dc72019-11-26 12:17:17 -05002281 if (SkPathFillType_IsInverse(fillType)) {
ethannicholase9709e82016-01-07 13:34:16 -08002282 contourCnt++;
2283 }
Stephen White3a9aab92017-03-07 14:07:18 -05002284 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002285
2286 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002287 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Chris Daltondcc8c542020-01-28 17:55:56 -07002288 mode, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002289}
2290
Stephen White11f65e02017-02-16 19:00:39 -05002291int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002292 // We could theoretically be more aggressive about not counting empty contours, but we need to
2293 // actually match the exact number of contour linked lists the tessellator will create later on.
2294 int contourCnt = 1;
2295 bool hasPoints = false;
2296
2297 SkPath::Iter iter(path, false);
2298 SkPath::Verb verb;
2299 SkPoint pts[4];
2300 bool first = true;
2301 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
2302 switch (verb) {
2303 case SkPath::kMove_Verb:
2304 if (!first) {
2305 ++contourCnt;
2306 }
2307 // fallthru.
2308 case SkPath::kLine_Verb:
2309 case SkPath::kConic_Verb:
2310 case SkPath::kQuad_Verb:
2311 case SkPath::kCubic_Verb:
2312 hasPoints = true;
2313 // fallthru to break.
2314 default:
2315 break;
2316 }
2317 first = false;
2318 }
2319 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002320 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002321 }
Stephen White11f65e02017-02-16 19:00:39 -05002322 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002323}
2324
Mike Reed7d34dc72019-11-26 12:17:17 -05002325int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002326 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002327 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002328 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002329 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2330 }
2331 }
2332 return count;
2333}
2334
Greg Danield5b45932018-06-07 13:15:10 -04002335int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2336 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002337 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2338 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2339 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2340 }
2341 }
2342 return count;
2343}
2344
Brian Osman0995fd52019-01-09 09:52:25 -05002345void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002346 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2347 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2348 Vertex* v0 = e->fTop;
2349 Vertex* v1 = e->fBottom;
2350 Vertex* v2 = e->fBottom->fPartner;
2351 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002352 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2353 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002354 }
2355 }
2356 return data;
2357}
2358
ethannicholase9709e82016-01-07 13:34:16 -08002359} // namespace
2360
2361namespace GrTessellator {
2362
2363// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2364
halcanary9d524f22016-03-29 09:03:52 -07002365int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Daltondcc8c542020-01-28 17:55:56 -07002366 GrEagerVertexAllocator* vertexAllocator, Mode mode, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002367 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002368 if (contourCnt <= 0) {
2369 *isLinear = true;
2370 return 0;
2371 }
Stephen White11f65e02017-02-16 19:00:39 -05002372 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002373 VertexList outerMesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002374 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, mode,
Stephen Whitebda29c02017-03-13 15:10:13 -04002375 isLinear, &outerMesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002376 SkPathFillType fillType = (Mode::kEdgeAntialias == mode) ?
2377 SkPathFillType::kWinding : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002378 int64_t count64 = count_points(polys, fillType);
Chris Daltondcc8c542020-01-28 17:55:56 -07002379 if (Mode::kEdgeAntialias == mode) {
Greg Danield5b45932018-06-07 13:15:10 -04002380 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002381 }
Greg Danield5b45932018-06-07 13:15:10 -04002382 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002383 return 0;
2384 }
Greg Danield5b45932018-06-07 13:15:10 -04002385 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002386
Chris Daltondcc8c542020-01-28 17:55:56 -07002387 size_t vertexStride = GetVertexStride(mode);
Chris Daltond081dce2020-01-23 12:09:04 -07002388 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002389 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002390 SkDebugf("Could not allocate vertices\n");
2391 return 0;
2392 }
senorblancof57372d2016-08-31 10:36:19 -07002393
Brian Salomon120e7d62019-09-11 10:29:22 -04002394 TESS_LOG("emitting %d verts\n", count);
Chris Daltondcc8c542020-01-28 17:55:56 -07002395 void* end = polys_to_triangles(polys, fillType, mode, verts);
Brian Osman80879d42019-01-07 16:15:27 -05002396 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002397
senorblancof57372d2016-08-31 10:36:19 -07002398 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002399 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002400 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002401 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002402 return actualCount;
2403}
2404
halcanary9d524f22016-03-29 09:03:52 -07002405int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002406 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002407 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002408 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002409 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002410 return 0;
2411 }
Stephen White11f65e02017-02-16 19:00:39 -05002412 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002413 bool isLinear;
Chris Daltondcc8c542020-01-28 17:55:56 -07002414 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, Mode::kNormal,
2415 &isLinear, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002416 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002417 int64_t count64 = count_points(polys, fillType);
2418 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002419 *verts = nullptr;
2420 return 0;
2421 }
Greg Danield5b45932018-06-07 13:15:10 -04002422 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002423
2424 *verts = new GrTessellator::WindingVertex[count];
2425 GrTessellator::WindingVertex* vertsEnd = *verts;
2426 SkPoint* points = new SkPoint[count];
2427 SkPoint* pointsEnd = points;
2428 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002429 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002430 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002431 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002432 while (start != pointsEnd) {
2433 vertsEnd->fPos = *start;
2434 vertsEnd->fWinding = poly->fWinding;
2435 ++start;
2436 ++vertsEnd;
2437 }
2438 }
2439 }
2440 int actualCount = static_cast<int>(vertsEnd - *verts);
2441 SkASSERT(actualCount <= count);
2442 SkASSERT(pointsEnd - points == actualCount);
2443 delete[] points;
2444 return actualCount;
2445}
2446
2447} // namespace