blob: eb5e84bffbca9ca406c64423798281a285ceef86 [file] [log] [blame]
ethannicholase9709e82016-01-07 13:34:16 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrTessellator.h"
ethannicholase9709e82016-01-07 13:34:16 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrDefaultGeoProcFactory.h"
Chris Daltond081dce2020-01-23 12:09:04 -070011#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040013#include "src/gpu/geometry/GrPathUtils.h"
ethannicholase9709e82016-01-07 13:34:16 -080014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPath.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040016#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkGeometry.h"
18#include "src/core/SkPointPriv.h"
ethannicholase9709e82016-01-07 13:34:16 -080019
Stephen White94b7e542018-01-04 14:01:10 -050020#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040021#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040022#include <queue>
23#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040024#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080025
26/*
senorblancof57372d2016-08-31 10:36:19 -070027 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080028 *
29 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
30 * 2) Build a mesh of edges connecting the vertices (build_edges()).
31 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
32 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
33 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
34 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
35 *
senorblancof57372d2016-08-31 10:36:19 -070036 * For screenspace antialiasing, the algorithm is modified as follows:
37 *
38 * Run steps 1-5 above to produce polygons.
39 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040040 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070041 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
42 * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
Stephen Whitebda29c02017-03-13 15:10:13 -040043 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070044 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
45 *
ethannicholase9709e82016-01-07 13:34:16 -080046 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
47 * of vertices (and the necessity of inserting new vertices on intersection).
48 *
Stephen Whitebda29c02017-03-13 15:10:13 -040049 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080050 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
51 * left-to-right based on the point where both edges are active (when both top vertices
52 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
53 * (shared), it's sorted based on the last point where both edges are active, so the
54 * "upper" bottom vertex.
55 *
56 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
57 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
58 * not exact and may violate the mesh topology or active edge list ordering. We
59 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040060 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080061 *
62 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
63 * neighbouring edges at the top or bottom vertex. This is handled by merging the
64 * edges (merge_collinear_edges()).
65 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen Whiteb67b2352019-06-01 13:07:27 -040066 * active edge list. This is handled during merging or splitting by rewind()ing the
67 * active edge list to the vertex before potential violations occur.
ethannicholase9709e82016-01-07 13:34:16 -080068 *
69 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
70 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
71 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
72 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
73 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
74 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
75 * linked list implementation. With the latter, all removals are O(1), and most insertions
76 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
77 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
78 * frequent. There may be other data structures worth investigating, however.
79 *
80 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
81 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
82 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
83 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
84 * that the "left" and "right" orientation in the code remains correct (edges to the left are
85 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
86 * degrees counterclockwise, rather that transposing.
87 */
88
89#define LOGGING_ENABLED 0
90
91#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -040092#define TESS_LOG printf
ethannicholase9709e82016-01-07 13:34:16 -080093#else
Brian Salomon120e7d62019-09-11 10:29:22 -040094#define TESS_LOG(...)
ethannicholase9709e82016-01-07 13:34:16 -080095#endif
96
ethannicholase9709e82016-01-07 13:34:16 -080097namespace {
98
Chris Daltondcc8c542020-01-28 17:55:56 -070099using GrTessellator::Mode;
100
Stephen White11f65e02017-02-16 19:00:39 -0500101const int kArenaChunkSize = 16 * 1024;
Stephen Whitee260c462017-12-19 18:09:54 -0500102const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
Stephen White11f65e02017-02-16 19:00:39 -0500103
ethannicholase9709e82016-01-07 13:34:16 -0800104struct Vertex;
105struct Edge;
Stephen Whitee260c462017-12-19 18:09:54 -0500106struct Event;
ethannicholase9709e82016-01-07 13:34:16 -0800107struct Poly;
108
109template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800110void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800111 t->*Prev = prev;
112 t->*Next = next;
113 if (prev) {
114 prev->*Next = t;
115 } else if (head) {
116 *head = t;
117 }
118 if (next) {
119 next->*Prev = t;
120 } else if (tail) {
121 *tail = t;
122 }
123}
124
125template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800126void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800127 if (t->*Prev) {
128 t->*Prev->*Next = t->*Next;
129 } else if (head) {
130 *head = t->*Next;
131 }
132 if (t->*Next) {
133 t->*Next->*Prev = t->*Prev;
134 } else if (tail) {
135 *tail = t->*Prev;
136 }
137 t->*Prev = t->*Next = nullptr;
138}
139
140/**
141 * Vertices are used in three ways: first, the path contours are converted into a
142 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
143 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
144 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
145 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
146 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
147 * an individual Vertex from the path mesh may belong to multiple
148 * MonotonePolys, so the original Vertices cannot be re-used.
149 */
150
151struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700152 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800153 : fPoint(point), fPrev(nullptr), fNext(nullptr)
154 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
155 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -0400156 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -0400157 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -0700158 , fAlpha(alpha)
Stephen Whitec4dbc372019-05-22 10:50:14 -0400159 , fSynthetic(false)
ethannicholase9709e82016-01-07 13:34:16 -0800160#if LOGGING_ENABLED
161 , fID (-1.0f)
162#endif
163 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400164 SkPoint fPoint; // Vertex position
165 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
166 Vertex* fNext; // "
167 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
168 Edge* fLastEdgeAbove; // "
169 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
170 Edge* fLastEdgeBelow; // "
171 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
172 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
173 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700174 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400175 bool fSynthetic; // Is this a synthetic vertex?
ethannicholase9709e82016-01-07 13:34:16 -0800176#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400177 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800178#endif
179};
180
181/***************************************************************************************/
182
183typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
184
ethannicholase9709e82016-01-07 13:34:16 -0800185bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500186 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800187}
188
189bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500190 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800191}
192
Stephen White16a40cb2017-02-23 11:10:01 -0500193struct Comparator {
194 enum class Direction { kVertical, kHorizontal };
195 Comparator(Direction direction) : fDirection(direction) {}
196 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
197 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
198 }
Stephen White16a40cb2017-02-23 11:10:01 -0500199 Direction fDirection;
200};
201
Brian Osman0995fd52019-01-09 09:52:25 -0500202inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500203 GrVertexWriter verts{data};
204 verts.write(v->fPoint);
205
Brian Osman80879d42019-01-07 16:15:27 -0500206 if (emitCoverage) {
207 verts.write(GrNormalizeByteToFloat(v->fAlpha));
208 }
Brian Osman0995fd52019-01-09 09:52:25 -0500209
Brian Osmanf9aabff2018-11-13 16:11:38 -0500210 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800211}
212
Brian Osman0995fd52019-01-09 09:52:25 -0500213void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400214 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
215 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
216 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700217#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500218 data = emit_vertex(v0, emitCoverage, data);
219 data = emit_vertex(v1, emitCoverage, data);
220 data = emit_vertex(v1, emitCoverage, data);
221 data = emit_vertex(v2, emitCoverage, data);
222 data = emit_vertex(v2, emitCoverage, data);
223 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800224#else
Brian Osman0995fd52019-01-09 09:52:25 -0500225 data = emit_vertex(v0, emitCoverage, data);
226 data = emit_vertex(v1, emitCoverage, data);
227 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800228#endif
229 return data;
230}
231
senorblancoe6eaa322016-03-08 09:06:44 -0800232struct VertexList {
233 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500234 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800235 Vertex* fHead;
236 Vertex* fTail;
237 void insert(Vertex* v, Vertex* prev, Vertex* next) {
238 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
239 }
240 void append(Vertex* v) {
241 insert(v, fTail, nullptr);
242 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400243 void append(const VertexList& list) {
244 if (!list.fHead) {
245 return;
246 }
247 if (fTail) {
248 fTail->fNext = list.fHead;
249 list.fHead->fPrev = fTail;
250 } else {
251 fHead = list.fHead;
252 }
253 fTail = list.fTail;
254 }
senorblancoe6eaa322016-03-08 09:06:44 -0800255 void prepend(Vertex* v) {
256 insert(v, nullptr, fHead);
257 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500258 void remove(Vertex* v) {
259 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
260 }
senorblancof57372d2016-08-31 10:36:19 -0700261 void close() {
262 if (fHead && fTail) {
263 fTail->fNext = fHead;
264 fHead->fPrev = fTail;
265 }
266 }
senorblancoe6eaa322016-03-08 09:06:44 -0800267};
268
senorblancof57372d2016-08-31 10:36:19 -0700269// Round to nearest quarter-pixel. This is used for screenspace tessellation.
270
271inline void round(SkPoint* p) {
272 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
273 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
274}
275
Stephen White94b7e542018-01-04 14:01:10 -0500276inline SkScalar double_to_clamped_scalar(double d) {
277 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
278}
279
senorblanco49df8d12016-10-07 08:36:56 -0700280// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
281struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500282 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700283 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
284 Line(const SkPoint& p, const SkPoint& q)
285 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
286 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
287 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
288 static_cast<double>(p.fX) * q.fY) {}
289 double dist(const SkPoint& p) const {
290 return fA * p.fX + fB * p.fY + fC;
291 }
Stephen Whitee260c462017-12-19 18:09:54 -0500292 Line operator*(double v) const {
293 return Line(fA * v, fB * v, fC * v);
294 }
senorblanco49df8d12016-10-07 08:36:56 -0700295 double magSq() const {
296 return fA * fA + fB * fB;
297 }
Stephen Whitee260c462017-12-19 18:09:54 -0500298 void normalize() {
299 double len = sqrt(this->magSq());
300 if (len == 0.0) {
301 return;
302 }
303 double scale = 1.0f / len;
304 fA *= scale;
305 fB *= scale;
306 fC *= scale;
307 }
308 bool nearParallel(const Line& o) const {
309 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
310 }
senorblanco49df8d12016-10-07 08:36:56 -0700311
312 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500313 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700314 double denom = fA * other.fB - fB * other.fA;
315 if (denom == 0.0) {
316 return false;
317 }
Stephen White94b7e542018-01-04 14:01:10 -0500318 double scale = 1.0 / denom;
319 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
320 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500321 round(point);
Stephen White9b7f1432019-06-08 08:56:58 -0400322 return point->isFinite();
senorblanco49df8d12016-10-07 08:36:56 -0700323 }
324 double fA, fB, fC;
325};
326
ethannicholase9709e82016-01-07 13:34:16 -0800327/**
328 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
329 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
330 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whiteb67b2352019-06-01 13:07:27 -0400331 * point). For speed, that case is only tested by the callers that require it. Edges also handle
332 * checking for intersection with other edges. Currently, this converts the edges to the
333 * parametric form, in order to avoid doing a division until an intersection has been confirmed.
334 * This is slightly slower in the "found" case, but a lot faster in the "not found" case.
ethannicholase9709e82016-01-07 13:34:16 -0800335 *
336 * The coefficients of the line equation stored in double precision to avoid catastrphic
337 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
338 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
339 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
340 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
341 * this file).
342 */
343
344struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500345 enum class Type { kInner, kOuter, kConnector };
346 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800347 : fWinding(winding)
348 , fTop(top)
349 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500350 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800351 , fLeft(nullptr)
352 , fRight(nullptr)
353 , fPrevEdgeAbove(nullptr)
354 , fNextEdgeAbove(nullptr)
355 , fPrevEdgeBelow(nullptr)
356 , fNextEdgeBelow(nullptr)
357 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700358 , fRightPoly(nullptr)
359 , fLeftPolyPrev(nullptr)
360 , fLeftPolyNext(nullptr)
361 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700362 , fRightPolyNext(nullptr)
363 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700364 , fUsedInRightPoly(false)
365 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800366 }
367 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
368 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
369 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500370 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800371 Edge* fLeft; // The linked list of edges in the active edge list.
372 Edge* fRight; // "
373 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
374 Edge* fNextEdgeAbove; // "
375 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
376 Edge* fNextEdgeBelow; // "
377 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
378 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700379 Edge* fLeftPolyPrev;
380 Edge* fLeftPolyNext;
381 Edge* fRightPolyPrev;
382 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700383 bool fUsedInLeftPoly;
384 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700385 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800386 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700387 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800388 }
389 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700390 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800391 }
392 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700393 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800394 }
395 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700396 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800397 }
Stephen White95152e12017-12-18 10:52:44 -0500398 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
Brian Salomon120e7d62019-09-11 10:29:22 -0400399 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
400 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800401 if (fTop == other.fTop || fBottom == other.fBottom) {
402 return false;
403 }
senorblanco49df8d12016-10-07 08:36:56 -0700404 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800405 if (denom == 0.0) {
406 return false;
407 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500408 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
409 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
410 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
411 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800412 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
413 // This saves us doing the divide below unless absolutely necessary.
414 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
415 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
416 return false;
417 }
418 double s = sNumer / denom;
419 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700420 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
421 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500422 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500423 if (fType == Type::kConnector) {
424 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
425 } else if (other.fType == Type::kConnector) {
426 double t = tNumer / denom;
427 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500428 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
429 *alpha = 0;
430 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500431 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500432 }
433 }
ethannicholase9709e82016-01-07 13:34:16 -0800434 return true;
435 }
senorblancof57372d2016-08-31 10:36:19 -0700436};
437
Stephen Whitec4dbc372019-05-22 10:50:14 -0400438struct SSEdge;
439
440struct SSVertex {
441 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
442 Vertex* fVertex;
443 SSEdge* fPrev;
444 SSEdge* fNext;
445};
446
447struct SSEdge {
448 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
449 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
450 }
451 Edge* fEdge;
452 Event* fEvent;
453 SSVertex* fPrev;
454 SSVertex* fNext;
455};
456
457typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
458typedef std::vector<SSEdge*> SSEdgeList;
459
senorblancof57372d2016-08-31 10:36:19 -0700460struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500461 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700462 Edge* fHead;
463 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700464 void insert(Edge* edge, Edge* prev, Edge* next) {
465 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700466 }
467 void append(Edge* e) {
468 insert(e, fTail, nullptr);
469 }
470 void remove(Edge* edge) {
471 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700472 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400473 void removeAll() {
474 while (fHead) {
475 this->remove(fHead);
476 }
477 }
senorblancof57372d2016-08-31 10:36:19 -0700478 void close() {
479 if (fHead && fTail) {
480 fTail->fRight = fHead;
481 fHead->fLeft = fTail;
482 }
483 }
484 bool contains(Edge* edge) const {
485 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800486 }
487};
488
Stephen Whitec4dbc372019-05-22 10:50:14 -0400489struct EventList;
490
Stephen Whitee260c462017-12-19 18:09:54 -0500491struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400492 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
493 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500494 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400495 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500496 SkPoint fPoint;
497 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400498 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500499};
500
Stephen Whitec4dbc372019-05-22 10:50:14 -0400501struct EventComparator {
502 enum class Op { kLessThan, kGreaterThan };
503 EventComparator(Op op) : fOp(op) {}
504 bool operator() (Event* const &e1, Event* const &e2) {
505 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
506 : e1->fAlpha > e2->fAlpha;
507 }
508 Op fOp;
509};
Stephen Whitee260c462017-12-19 18:09:54 -0500510
Stephen Whitec4dbc372019-05-22 10:50:14 -0400511typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500512
Stephen Whitec4dbc372019-05-22 10:50:14 -0400513struct EventList : EventPQ {
514 EventList(EventComparator comparison) : EventPQ(comparison) {
515 }
516};
517
518void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
519 Vertex* prev = e->fPrev->fVertex;
520 Vertex* next = e->fNext->fVertex;
521 if (prev == next || !prev->fPartner || !next->fPartner) {
522 return;
523 }
524 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
525 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500526 SkPoint p;
527 uint8_t alpha;
528 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400529 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
530 "will collapse to %g,%g alpha %d\n",
531 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
532 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400533 e->fEvent = alloc.make<Event>(e, p, alpha);
534 events->push(e->fEvent);
535 }
536}
537
538void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
539 Comparator& c, SkArenaAlloc& alloc) {
540 if (!v->fPartner) {
541 return;
542 }
Stephen White8a3c0592019-05-29 11:26:16 -0400543 Vertex* top = edge->fEdge->fTop;
544 Vertex* bottom = edge->fEdge->fBottom;
545 if (!top || !bottom ) {
546 return;
547 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400548 Line line = edge->fEdge->fLine;
549 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
550 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
551 SkPoint p;
552 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400553 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
554 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400555 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
556 "will collapse to %g,%g alpha %d\n",
557 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400558 edge->fEvent = alloc.make<Event>(edge, p, alpha);
559 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500560 }
561}
Stephen Whitee260c462017-12-19 18:09:54 -0500562
ethannicholase9709e82016-01-07 13:34:16 -0800563/***************************************************************************************/
564
565struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700566 Poly(Vertex* v, int winding)
567 : fFirstVertex(v)
568 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800569 , fHead(nullptr)
570 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800571 , fNext(nullptr)
572 , fPartner(nullptr)
573 , fCount(0)
574 {
575#if LOGGING_ENABLED
576 static int gID = 0;
577 fID = gID++;
Brian Salomon120e7d62019-09-11 10:29:22 -0400578 TESS_LOG("*** created Poly %d\n", fID);
ethannicholase9709e82016-01-07 13:34:16 -0800579#endif
580 }
senorblanco531237e2016-06-02 11:36:48 -0700581 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800582 struct MonotonePoly {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700583 MonotonePoly(Edge* edge, Side side, int winding)
senorblanco531237e2016-06-02 11:36:48 -0700584 : fSide(side)
585 , fFirstEdge(nullptr)
586 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800587 , fPrev(nullptr)
Chris Dalton022bd3b2020-01-24 13:48:53 -0700588 , fNext(nullptr)
589 , fWinding(winding) {
senorblanco531237e2016-06-02 11:36:48 -0700590 this->addEdge(edge);
591 }
ethannicholase9709e82016-01-07 13:34:16 -0800592 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700593 Edge* fFirstEdge;
594 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800595 MonotonePoly* fPrev;
596 MonotonePoly* fNext;
Chris Dalton022bd3b2020-01-24 13:48:53 -0700597 int fWinding;
senorblanco531237e2016-06-02 11:36:48 -0700598 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800599 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700600 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700601 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
602 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700603 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800604 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700605 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700606 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
607 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700608 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800609 }
ethannicholase9709e82016-01-07 13:34:16 -0800610 }
611
Brian Osman0995fd52019-01-09 09:52:25 -0500612 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700613 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700614 VertexList vertices;
615 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500616 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700617 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700618 if (kRight_Side == fSide) {
619 vertices.append(e->fBottom);
620 e = e->fRightPolyNext;
621 } else {
622 vertices.prepend(e->fBottom);
623 e = e->fLeftPolyNext;
624 }
Stephen White651cbe92017-03-03 12:24:16 -0500625 count++;
senorblanco531237e2016-06-02 11:36:48 -0700626 }
627 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800628 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700629 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800630 SkASSERT(v && v->fPrev && v->fNext);
631 Vertex* prev = v->fPrev;
632 Vertex* curr = v;
633 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500634 if (count == 3) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700635 return this->emitTriangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500636 }
ethannicholase9709e82016-01-07 13:34:16 -0800637 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
638 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
639 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
640 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
641 if (ax * by - ay * bx >= 0.0) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700642 data = this->emitTriangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800643 v->fPrev->fNext = v->fNext;
644 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500645 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800646 if (v->fPrev == first) {
647 v = v->fNext;
648 } else {
649 v = v->fPrev;
650 }
651 } else {
652 v = v->fNext;
653 }
654 }
655 return data;
656 }
Chris Dalton022bd3b2020-01-24 13:48:53 -0700657 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, bool emitCoverage,
658 void* data) const {
659 if (fWinding < 0) {
660 // Ensure our triangles always wind in the same direction as if the path had been
661 // triangulated as a simple fan (a la red book).
662 std::swap(prev, next);
663 }
664 return emit_triangle(next, curr, prev, emitCoverage, data);
665 }
ethannicholase9709e82016-01-07 13:34:16 -0800666 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500667 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400668 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
669 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800670 Poly* partner = fPartner;
671 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700672 if (side == kRight_Side) {
673 if (e->fUsedInRightPoly) {
674 return this;
675 }
676 } else {
677 if (e->fUsedInLeftPoly) {
678 return this;
679 }
680 }
ethannicholase9709e82016-01-07 13:34:16 -0800681 if (partner) {
682 fPartner = partner->fPartner = nullptr;
683 }
senorblanco531237e2016-06-02 11:36:48 -0700684 if (!fTail) {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700685 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700686 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700687 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
688 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700689 } else if (side == fTail->fSide) {
690 fTail->addEdge(e);
691 fCount++;
692 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500693 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700694 fTail->addEdge(e);
695 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800696 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700697 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800698 poly = partner;
699 } else {
Chris Dalton022bd3b2020-01-24 13:48:53 -0700700 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
senorblanco531237e2016-06-02 11:36:48 -0700701 m->fPrev = fTail;
702 fTail->fNext = m;
703 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800704 }
705 }
ethannicholase9709e82016-01-07 13:34:16 -0800706 return poly;
707 }
Brian Osman0995fd52019-01-09 09:52:25 -0500708 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800709 if (fCount < 3) {
710 return data;
711 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400712 TESS_LOG("emit() %d, size %d\n", fID, fCount);
ethannicholase9709e82016-01-07 13:34:16 -0800713 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500714 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800715 }
716 return data;
717 }
senorblanco531237e2016-06-02 11:36:48 -0700718 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
719 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800720 int fWinding;
721 MonotonePoly* fHead;
722 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800723 Poly* fNext;
724 Poly* fPartner;
725 int fCount;
726#if LOGGING_ENABLED
727 int fID;
728#endif
729};
730
731/***************************************************************************************/
732
733bool coincident(const SkPoint& a, const SkPoint& b) {
734 return a == b;
735}
736
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500737Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
738 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800739 poly->fNext = *head;
740 *head = poly;
741 return poly;
742}
743
Stephen White3a9aab92017-03-07 14:07:18 -0500744void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500745 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800746#if LOGGING_ENABLED
747 static float gID = 0.0f;
748 v->fID = gID++;
749#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500750 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800751}
752
Stephen White36e4f062017-03-27 16:11:31 -0400753SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
754 SkQuadCoeff quad(pts);
755 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
756 SkPoint mid = to_point(quad.eval(t));
757 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400758 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
759 return 0;
760 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500761 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400762}
763
764void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
765 SkArenaAlloc& alloc) {
766 SkQuadCoeff quad(pts);
767 Sk2s aa = quad.fA * quad.fA;
768 SkScalar denom = 2.0f * (aa[0] + aa[1]);
769 Sk2s ab = quad.fA * quad.fB;
770 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
771 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500772 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400773 // Test possible subdivision values only at the point of maximum curvature.
774 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500775 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400776 u = 1.0f / nPoints;
777 if (quad_error_at(pts, t, u) < toleranceSqd) {
778 break;
779 }
780 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800781 }
Stephen White36e4f062017-03-27 16:11:31 -0400782 for (int j = 1; j <= nPoints; j++) {
783 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
784 }
ethannicholase9709e82016-01-07 13:34:16 -0800785}
786
Stephen White3a9aab92017-03-07 14:07:18 -0500787void generate_cubic_points(const SkPoint& p0,
788 const SkPoint& p1,
789 const SkPoint& p2,
790 const SkPoint& p3,
791 SkScalar tolSqd,
792 VertexList* contour,
793 int pointsLeft,
794 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500795 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
796 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800797 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
798 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500799 append_point_to_contour(p3, contour, alloc);
800 return;
ethannicholase9709e82016-01-07 13:34:16 -0800801 }
802 const SkPoint q[] = {
803 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
804 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
805 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
806 };
807 const SkPoint r[] = {
808 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
809 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
810 };
811 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
812 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500813 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
814 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800815}
816
817// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
818
819void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500820 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800821 SkScalar toleranceSqd = tolerance * tolerance;
822
823 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800824 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500825 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800826 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800827 if (path.isInverseFillType()) {
828 SkPoint quad[4];
829 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700830 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500831 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800832 }
Stephen White3a9aab92017-03-07 14:07:18 -0500833 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800834 }
835 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500836 SkPath::Verb verb;
Mike Reedba7e9a62019-08-16 13:30:34 -0400837 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800838 switch (verb) {
839 case SkPath::kConic_Verb: {
840 SkScalar weight = iter.conicWeight();
841 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
842 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400843 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800844 quadPts += 2;
845 }
846 *isLinear = false;
847 break;
848 }
849 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500850 if (contour->fHead) {
851 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800852 }
Stephen White3a9aab92017-03-07 14:07:18 -0500853 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800854 break;
855 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500856 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800857 break;
858 }
859 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400860 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800861 *isLinear = false;
862 break;
863 }
864 case SkPath::kCubic_Verb: {
865 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500866 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
867 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800868 *isLinear = false;
869 break;
870 }
871 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800872 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800873 break;
874 }
875 }
876}
877
Mike Reed7d34dc72019-11-26 12:17:17 -0500878inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800879 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500880 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800881 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500882 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800883 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500884 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700885 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500886 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800887 return (winding & 1) == 1;
888 default:
889 SkASSERT(false);
890 return false;
891 }
892}
893
Mike Reed7d34dc72019-11-26 12:17:17 -0500894inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500895 return poly && apply_fill_type(fillType, poly->fWinding);
896}
897
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500898Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500899 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800900 Vertex* top = winding < 0 ? next : prev;
901 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500902 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800903}
904
905void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400906 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700907 SkASSERT(edges->contains(edge));
908 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800909}
910
911void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400912 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700913 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800914 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700915 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800916}
917
918void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500919 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800920 *left = v->fFirstEdgeAbove->fLeft;
921 *right = v->fLastEdgeAbove->fRight;
922 return;
923 }
924 Edge* next = nullptr;
925 Edge* prev;
926 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
927 if (prev->isLeftOf(v)) {
928 break;
929 }
930 next = prev;
931 }
932 *left = prev;
933 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800934}
935
ethannicholase9709e82016-01-07 13:34:16 -0800936void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
937 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500938 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800939 return;
940 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400941 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
942 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800943 Edge* prev = nullptr;
944 Edge* next;
945 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
946 if (next->isRightOf(edge->fTop)) {
947 break;
948 }
949 prev = next;
950 }
senorblancoe6eaa322016-03-08 09:06:44 -0800951 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800952 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
953}
954
955void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
956 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500957 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800958 return;
959 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400960 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
961 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800962 Edge* prev = nullptr;
963 Edge* next;
964 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
965 if (next->isRightOf(edge->fBottom)) {
966 break;
967 }
968 prev = next;
969 }
senorblancoe6eaa322016-03-08 09:06:44 -0800970 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800971 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
972}
973
974void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400975 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400976 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
977 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800978 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800979 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
980}
981
982void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400983 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400984 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
985 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800986 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800987 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
988}
989
Stephen Whitee7a364d2017-01-11 16:19:26 -0500990void disconnect(Edge* edge)
991{
ethannicholase9709e82016-01-07 13:34:16 -0800992 remove_edge_above(edge);
993 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500994}
995
Stephen White3b5a3fa2017-06-06 14:51:19 -0400996void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
997
998void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
999 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
1000 return;
1001 }
1002 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -04001003 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001004 while (v != dst) {
1005 v = v->fPrev;
1006 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1007 remove_edge(e, activeEdges);
1008 }
1009 Edge* leftEdge = v->fLeftEnclosingEdge;
1010 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1011 insert_edge(e, leftEdge, activeEdges);
1012 leftEdge = e;
1013 }
1014 }
1015 *current = v;
1016}
1017
Stephen White3b5a3fa2017-06-06 14:51:19 -04001018void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001019 remove_edge_below(edge);
1020 edge->fTop = v;
1021 edge->recompute();
1022 insert_edge_below(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001023 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001024 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001025}
1026
Stephen White3b5a3fa2017-06-06 14:51:19 -04001027void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001028 remove_edge_above(edge);
1029 edge->fBottom = v;
1030 edge->recompute();
1031 insert_edge_above(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001032 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001033 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001034}
1035
Stephen White3b5a3fa2017-06-06 14:51:19 -04001036void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1037 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001038 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001039 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1040 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1041 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001042 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001043 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001044 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001045 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001046 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001047 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001048 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001049 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001050 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001051 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001052 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001053 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001054 }
1055}
1056
Stephen White3b5a3fa2017-06-06 14:51:19 -04001057void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1058 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001059 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001060 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1061 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1062 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001063 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001064 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001065 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001066 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001067 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001068 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001069 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001070 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001071 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001072 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001073 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001074 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001075 }
1076}
1077
Stephen Whited26b4d82018-07-26 10:02:27 -04001078bool top_collinear(Edge* left, Edge* right) {
1079 if (!left || !right) {
1080 return false;
1081 }
1082 return left->fTop->fPoint == right->fTop->fPoint ||
1083 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1084}
1085
1086bool bottom_collinear(Edge* left, Edge* right) {
1087 if (!left || !right) {
1088 return false;
1089 }
1090 return left->fBottom->fPoint == right->fBottom->fPoint ||
1091 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1092}
1093
Stephen White3b5a3fa2017-06-06 14:51:19 -04001094void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001095 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001096 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001097 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001098 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001099 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001100 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001101 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001102 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001103 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001104 } else {
1105 break;
1106 }
ethannicholase9709e82016-01-07 13:34:16 -08001107 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001108 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1109 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1110 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1111 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001112}
1113
Stephen White89042d52018-06-08 12:18:22 -04001114bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001115 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001116 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001117 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001118 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001119 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1120 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001121 Vertex* top;
1122 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001123 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001124 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001125 top = v;
1126 bottom = edge->fTop;
1127 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001128 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001129 top = edge->fBottom;
1130 bottom = v;
1131 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001132 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001133 top = v;
1134 bottom = edge->fBottom;
1135 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001136 }
Stephen White531a48e2018-06-01 09:49:39 -04001137 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001138 insert_edge_below(newEdge, top, c);
1139 insert_edge_above(newEdge, bottom, c);
1140 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001141 return true;
1142}
1143
1144bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1145 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1146 return false;
1147 }
Stephen White1c5fd182018-07-12 15:54:05 -04001148 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1149 return false;
1150 }
Stephen White89042d52018-06-08 12:18:22 -04001151 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1152 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001153 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001154 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1155 }
1156 } else {
1157 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001158 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001159 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1160 }
1161 }
1162 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1163 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001164 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001165 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1166 }
1167 } else {
1168 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001169 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001170 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1171 }
1172 }
1173 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001174}
1175
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001176Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001177 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001178 if (!prev || !next || prev->fPoint == next->fPoint) {
1179 return nullptr;
1180 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001181 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001182 insert_edge_below(edge, edge->fTop, c);
1183 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001184 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001185 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001186 return edge;
1187}
1188
Stephen Whitebf6137e2017-01-04 15:43:26 -05001189void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001190 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001191 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1192 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001193 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001194 if (src->fPartner) {
1195 src->fPartner->fPartner = dst;
1196 }
Stephen White7b376942018-05-22 11:51:32 -04001197 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001198 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001199 }
Stephen White7b376942018-05-22 11:51:32 -04001200 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001201 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001202 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001203 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001204 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001205}
1206
Stephen White95152e12017-12-18 10:52:44 -05001207Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1208 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1209 Vertex* prevV = reference;
1210 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1211 prevV = prevV->fPrev;
1212 }
1213 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1214 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1215 prevV = nextV;
1216 nextV = nextV->fNext;
1217 }
1218 Vertex* v;
1219 if (prevV && coincident(prevV->fPoint, p)) {
1220 v = prevV;
1221 } else if (nextV && coincident(nextV->fPoint, p)) {
1222 v = nextV;
1223 } else {
1224 v = alloc.make<Vertex>(p, alpha);
1225#if LOGGING_ENABLED
1226 if (!prevV) {
1227 v->fID = mesh->fHead->fID - 1.0f;
1228 } else if (!nextV) {
1229 v->fID = mesh->fTail->fID + 1.0f;
1230 } else {
1231 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1232 }
1233#endif
1234 mesh->insert(v, prevV, nextV);
1235 }
1236 return v;
1237}
1238
Stephen White53a02982018-05-30 22:47:46 -04001239// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1240// sort criterion, it may not be possible to split correctly, since there is no point which is
1241// below the top and above the bottom. This function detects that case.
1242bool nearly_flat(Comparator& c, Edge* edge) {
1243 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1244 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001245 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001246}
1247
Stephen Whitee62999f2018-06-05 18:45:07 -04001248SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1249 if (c.sweep_lt(p, min)) {
1250 return min;
1251 } else if (c.sweep_lt(max, p)) {
1252 return max;
1253 } else {
1254 return p;
1255 }
1256}
1257
Stephen Whitec4dbc372019-05-22 10:50:14 -04001258void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1259 Line line1 = edge1->fLine;
1260 Line line2 = edge2->fLine;
1261 line1.normalize();
1262 line2.normalize();
1263 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1264 if (cosAngle > 0.999) {
1265 return;
1266 }
1267 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1268 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1269 SkPoint p;
1270 if (line1.intersect(line2, &p)) {
1271 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1272 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001273 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 -04001274 }
1275}
1276
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001277bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001278 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001279 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001280 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001281 }
Stephen White56158ae2017-01-30 14:31:31 -05001282 SkPoint p;
1283 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001284 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001285 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001286 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001287 Vertex* top = *current;
1288 // If the intersection point is above the current vertex, rewind to the vertex above the
1289 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001290 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001291 top = top->fPrev;
1292 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001293 if (!nearly_flat(c, left)) {
1294 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001295 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001296 if (!nearly_flat(c, right)) {
1297 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001298 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001299 if (p == left->fTop->fPoint) {
1300 v = left->fTop;
1301 } else if (p == left->fBottom->fPoint) {
1302 v = left->fBottom;
1303 } else if (p == right->fTop->fPoint) {
1304 v = right->fTop;
1305 } else if (p == right->fBottom->fPoint) {
1306 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001307 } else {
Stephen White95152e12017-12-18 10:52:44 -05001308 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001309 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001310 v->fSynthetic = true;
1311 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001312 }
ethannicholase9709e82016-01-07 13:34:16 -08001313 }
Stephen White0cb31672017-06-08 14:41:01 -04001314 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001315 split_edge(left, v, activeEdges, current, c, alloc);
1316 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001317 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001318 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001319 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001320 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001321}
1322
Chris Daltondcc8c542020-01-28 17:55:56 -07001323void sanitize_contours(VertexList* contours, int contourCnt, Mode mode) {
1324 bool approximate = (Mode::kEdgeAntialias == mode);
Stephen White3a9aab92017-03-07 14:07:18 -05001325 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1326 SkASSERT(contour->fHead);
1327 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001328 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001329 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001330 }
Stephen White3a9aab92017-03-07 14:07:18 -05001331 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001332 if (approximate) {
1333 round(&v->fPoint);
1334 }
Stephen White3a9aab92017-03-07 14:07:18 -05001335 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001336 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001337 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001338 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001339 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001340 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001341 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001342 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001343 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001344 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001345 contour->remove(v);
1346 } else {
1347 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001348 }
Stephen White3a9aab92017-03-07 14:07:18 -05001349 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001350 }
1351 }
1352}
1353
Stephen Whitee260c462017-12-19 18:09:54 -05001354bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001355 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001356 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001357 }
Stephen Whitee260c462017-12-19 18:09:54 -05001358 bool merged = false;
1359 for (Vertex* v = mesh->fHead->fNext; v;) {
1360 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001361 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1362 v->fPoint = v->fPrev->fPoint;
1363 }
1364 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001365 merge_vertices(v, v->fPrev, mesh, c, alloc);
1366 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001367 }
Stephen Whitee260c462017-12-19 18:09:54 -05001368 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001369 }
Stephen Whitee260c462017-12-19 18:09:54 -05001370 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001371}
1372
1373// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1374
Stephen White3a9aab92017-03-07 14:07:18 -05001375void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001376 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001377 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1378 Vertex* prev = contour->fTail;
1379 for (Vertex* v = contour->fHead; v;) {
1380 Vertex* next = v->fNext;
1381 connect(prev, v, Edge::Type::kInner, c, alloc);
1382 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001383 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001384 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001385 }
1386 }
ethannicholase9709e82016-01-07 13:34:16 -08001387}
1388
Stephen Whitee260c462017-12-19 18:09:54 -05001389void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1390 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001391 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001392 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1393 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1394 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1395 // number.
1396 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1397 inner->fPartner = outer->fPartner = nullptr;
1398 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001399 }
1400 }
1401}
1402
1403template <CompareFunc sweep_lt>
1404void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1405 Vertex* a = front->fHead;
1406 Vertex* b = back->fHead;
1407 while (a && b) {
1408 if (sweep_lt(a->fPoint, b->fPoint)) {
1409 front->remove(a);
1410 result->append(a);
1411 a = front->fHead;
1412 } else {
1413 back->remove(b);
1414 result->append(b);
1415 b = back->fHead;
1416 }
1417 }
1418 result->append(*front);
1419 result->append(*back);
1420}
1421
1422void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1423 if (c.fDirection == Comparator::Direction::kHorizontal) {
1424 sorted_merge<sweep_lt_horiz>(front, back, result);
1425 } else {
1426 sorted_merge<sweep_lt_vert>(front, back, result);
1427 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001428#if LOGGING_ENABLED
1429 float id = 0.0f;
1430 for (Vertex* v = result->fHead; v; v = v->fNext) {
1431 v->fID = id++;
1432 }
1433#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001434}
1435
ethannicholase9709e82016-01-07 13:34:16 -08001436// Stage 3: sort the vertices by increasing sweep direction.
1437
Stephen White16a40cb2017-02-23 11:10:01 -05001438template <CompareFunc sweep_lt>
1439void merge_sort(VertexList* vertices) {
1440 Vertex* slow = vertices->fHead;
1441 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001442 return;
1443 }
Stephen White16a40cb2017-02-23 11:10:01 -05001444 Vertex* fast = slow->fNext;
1445 if (!fast) {
1446 return;
1447 }
1448 do {
1449 fast = fast->fNext;
1450 if (fast) {
1451 fast = fast->fNext;
1452 slow = slow->fNext;
1453 }
1454 } while (fast);
1455 VertexList front(vertices->fHead, slow);
1456 VertexList back(slow->fNext, vertices->fTail);
1457 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001458
Stephen White16a40cb2017-02-23 11:10:01 -05001459 merge_sort<sweep_lt>(&front);
1460 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001461
Stephen White16a40cb2017-02-23 11:10:01 -05001462 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001463 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001464}
1465
Stephen White95152e12017-12-18 10:52:44 -05001466void dump_mesh(const VertexList& mesh) {
1467#if LOGGING_ENABLED
1468 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001469 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 -05001470 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001471 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1472 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001473 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001474 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001475 }
1476 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001477 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001478 }
1479 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001480 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001481 }
1482 }
1483#endif
1484}
1485
Stephen Whitec4dbc372019-05-22 10:50:14 -04001486void dump_skel(const SSEdgeList& ssEdges) {
1487#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001488 for (SSEdge* edge : ssEdges) {
1489 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001490 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001491 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001492 edge->fNext->fVertex->fID);
1493 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001494 TESS_LOG(" (original %g -> %g)\n",
1495 edge->fEdge->fTop->fID,
1496 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001497 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001498 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001499 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001500 }
1501 }
1502#endif
1503}
1504
Stephen White89042d52018-06-08 12:18:22 -04001505#ifdef SK_DEBUG
1506void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1507 if (!left || !right) {
1508 return;
1509 }
1510 if (left->fTop == right->fTop) {
1511 SkASSERT(left->isLeftOf(right->fBottom));
1512 SkASSERT(right->isRightOf(left->fBottom));
1513 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1514 SkASSERT(left->isLeftOf(right->fTop));
1515 } else {
1516 SkASSERT(right->isRightOf(left->fTop));
1517 }
1518 if (left->fBottom == right->fBottom) {
1519 SkASSERT(left->isLeftOf(right->fTop));
1520 SkASSERT(right->isRightOf(left->fTop));
1521 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1522 SkASSERT(left->isLeftOf(right->fBottom));
1523 } else {
1524 SkASSERT(right->isRightOf(left->fBottom));
1525 }
1526}
1527
1528void validate_edge_list(EdgeList* edges, Comparator& c) {
1529 Edge* left = edges->fHead;
1530 if (!left) {
1531 return;
1532 }
1533 for (Edge* right = left->fRight; right; right = right->fRight) {
1534 validate_edge_pair(left, right, c);
1535 left = right;
1536 }
1537}
1538#endif
1539
ethannicholase9709e82016-01-07 13:34:16 -08001540// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1541
Stephen Whitec4dbc372019-05-22 10:50:14 -04001542bool connected(Vertex* v) {
1543 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1544}
1545
Stephen Whitee260c462017-12-19 18:09:54 -05001546bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001547 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001548 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001549 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001550 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001551 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001552 continue;
1553 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001554 Edge* leftEnclosingEdge;
1555 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001556 bool restartChecks;
1557 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001558 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1559 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001560 restartChecks = false;
1561 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001562 v->fLeftEnclosingEdge = leftEnclosingEdge;
1563 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001564 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001565 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001566 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001567 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001568 restartChecks = true;
1569 break;
1570 }
Stephen White0cb31672017-06-08 14:41:01 -04001571 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001572 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001573 restartChecks = true;
1574 break;
1575 }
1576 }
1577 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001578 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001579 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001580 restartChecks = true;
1581 }
1582
1583 }
Stephen Whitee260c462017-12-19 18:09:54 -05001584 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001585 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001586#ifdef SK_DEBUG
1587 validate_edge_list(&activeEdges, c);
1588#endif
ethannicholase9709e82016-01-07 13:34:16 -08001589 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1590 remove_edge(e, &activeEdges);
1591 }
1592 Edge* leftEdge = leftEnclosingEdge;
1593 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1594 insert_edge(e, leftEdge, &activeEdges);
1595 leftEdge = e;
1596 }
ethannicholase9709e82016-01-07 13:34:16 -08001597 }
Stephen Whitee260c462017-12-19 18:09:54 -05001598 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1599 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001600}
1601
1602// Stage 5: Tessellate the simplified mesh into monotone polygons.
1603
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001604Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001605 TESS_LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001606 EdgeList activeEdges;
1607 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001608 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001609 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001610 continue;
1611 }
1612#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001613 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 -08001614#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001615 Edge* leftEnclosingEdge;
1616 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001617 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001618 Poly* leftPoly;
1619 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001620 if (v->fFirstEdgeAbove) {
1621 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1622 rightPoly = v->fLastEdgeAbove->fRightPoly;
1623 } else {
1624 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1625 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1626 }
1627#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001628 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001629 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001630 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1631 e->fTop->fID, e->fBottom->fID,
1632 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1633 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001634 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001635 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001636 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001637 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1638 e->fTop->fID, e->fBottom->fID,
1639 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1640 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001641 }
1642#endif
1643 if (v->fFirstEdgeAbove) {
1644 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001645 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001646 }
1647 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001648 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001649 }
1650 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001651 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001652 remove_edge(e, &activeEdges);
1653 if (e->fRightPoly) {
1654 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001655 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001656 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001657 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001658 }
1659 }
1660 remove_edge(v->fLastEdgeAbove, &activeEdges);
1661 if (!v->fFirstEdgeBelow) {
1662 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1663 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1664 rightPoly->fPartner = leftPoly;
1665 leftPoly->fPartner = rightPoly;
1666 }
1667 }
1668 }
1669 if (v->fFirstEdgeBelow) {
1670 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001671 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001672 if (leftPoly == rightPoly) {
1673 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1674 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1675 leftPoly->fWinding, alloc);
1676 leftEnclosingEdge->fRightPoly = leftPoly;
1677 } else {
1678 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1679 rightPoly->fWinding, alloc);
1680 rightEnclosingEdge->fLeftPoly = rightPoly;
1681 }
ethannicholase9709e82016-01-07 13:34:16 -08001682 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001683 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001684 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1685 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001686 }
1687 }
1688 Edge* leftEdge = v->fFirstEdgeBelow;
1689 leftEdge->fLeftPoly = leftPoly;
1690 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1691 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1692 rightEdge = rightEdge->fNextEdgeBelow) {
1693 insert_edge(rightEdge, leftEdge, &activeEdges);
1694 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1695 winding += leftEdge->fWinding;
1696 if (winding != 0) {
1697 Poly* poly = new_poly(&polys, v, winding, alloc);
1698 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1699 }
1700 leftEdge = rightEdge;
1701 }
1702 v->fLastEdgeBelow->fRightPoly = rightPoly;
1703 }
1704#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001705 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001706 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001707 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1708 e->fTop->fID, e->fBottom->fID,
1709 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1710 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001711 }
1712#endif
1713 }
1714 return polys;
1715}
1716
Mike Reed7d34dc72019-11-26 12:17:17 -05001717void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001718 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001719 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001720 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001721 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001722 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001723 continue;
1724 }
1725 Edge* leftEnclosingEdge;
1726 Edge* rightEnclosingEdge;
1727 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1728 bool prevFilled = leftEnclosingEdge &&
1729 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1730 for (Edge* e = v->fFirstEdgeAbove; e;) {
1731 Edge* next = e->fNextEdgeAbove;
1732 remove_edge(e, &activeEdges);
1733 bool filled = apply_fill_type(fillType, e->fWinding);
1734 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001735 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001736 }
Stephen White49789062017-02-21 10:35:49 -05001737 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001738 e = next;
1739 }
Stephen White49789062017-02-21 10:35:49 -05001740 Edge* prev = leftEnclosingEdge;
1741 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1742 if (prev) {
1743 e->fWinding += prev->fWinding;
1744 }
1745 insert_edge(e, prev, &activeEdges);
1746 prev = e;
1747 }
senorblancof57372d2016-08-31 10:36:19 -07001748 }
senorblancof57372d2016-08-31 10:36:19 -07001749}
1750
Stephen White66412122017-03-01 11:48:27 -05001751// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001752void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001753 normal->set(SkDoubleToScalar(e->fLine.fA),
1754 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001755}
1756
1757// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1758// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1759// invert on stroking.
1760
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001761void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001762 Edge* prevEdge = boundary->fTail;
1763 SkVector prevNormal;
1764 get_edge_normal(prevEdge, &prevNormal);
1765 for (Edge* e = boundary->fHead; e != nullptr;) {
1766 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1767 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001768 double distPrev = e->dist(prev->fPoint);
1769 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001770 SkVector normal;
1771 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001772 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001773 if (prev == next) {
1774 remove_edge(prevEdge, boundary);
1775 remove_edge(e, boundary);
1776 prevEdge = boundary->fTail;
1777 e = boundary->fHead;
1778 if (prevEdge) {
1779 get_edge_normal(prevEdge, &prevNormal);
1780 }
1781 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001782 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001783 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001784 if (prev->fPoint != next->fPoint) {
1785 join->fLine.normalize();
1786 join->fLine = join->fLine * join->fWinding;
1787 }
senorblancof57372d2016-08-31 10:36:19 -07001788 insert_edge(join, e, boundary);
1789 remove_edge(prevEdge, boundary);
1790 remove_edge(e, boundary);
1791 if (join->fLeft && join->fRight) {
1792 prevEdge = join->fLeft;
1793 e = join;
1794 } else {
1795 prevEdge = boundary->fTail;
1796 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1797 }
1798 get_edge_normal(prevEdge, &prevNormal);
1799 } else {
1800 prevEdge = e;
1801 prevNormal = normal;
1802 e = e->fRight;
1803 }
1804 }
1805}
1806
Stephen Whitec4dbc372019-05-22 10:50:14 -04001807void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1808 if (v == dest) {
1809 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001810 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001811 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001812 if (v->fSynthetic) {
1813 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1814 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001815 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1816 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001817 v->fPartner->fPartner = dest;
1818 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001819 }
1820}
1821
Stephen Whitec4dbc372019-05-22 10:50:14 -04001822void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1823 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001824 return;
1825 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001826 Vertex* prev = fEdge->fPrev->fVertex;
1827 Vertex* next = fEdge->fNext->fVertex;
1828 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1829 SSEdge* nextEdge = fEdge->fNext->fNext;
1830 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1831 return;
Stephen White77169c82018-06-05 09:15:59 -04001832 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001833 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1834 dest->fSynthetic = true;
1835 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001836 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1837 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1838 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001839 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001840
Stephen Whitec4dbc372019-05-22 10:50:14 -04001841 ss_connect(prev, dest, c, alloc);
1842 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001843
Stephen Whitec4dbc372019-05-22 10:50:14 -04001844 prevEdge->fNext = nextEdge->fPrev = ssv;
1845 ssv->fPrev = prevEdge;
1846 ssv->fNext = nextEdge;
1847 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1848 return;
1849 }
1850 if (prevEdge->fEvent) {
1851 prevEdge->fEvent->fEdge = nullptr;
1852 }
1853 if (nextEdge->fEvent) {
1854 nextEdge->fEvent->fEdge = nullptr;
1855 }
1856 if (prevEdge->fPrev == nextEdge->fNext) {
1857 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1858 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1859 } else {
1860 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1861 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1862 if (dest->fPartner) {
1863 create_event(prevEdge, events, alloc);
1864 create_event(nextEdge, events, alloc);
1865 } else {
1866 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1867 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1868 }
1869 }
Stephen Whitee260c462017-12-19 18:09:54 -05001870}
1871
1872bool is_overlap_edge(Edge* e) {
1873 if (e->fType == Edge::Type::kOuter) {
1874 return e->fWinding != 0 && e->fWinding != 1;
1875 } else if (e->fType == Edge::Type::kInner) {
1876 return e->fWinding != 0 && e->fWinding != -2;
1877 } else {
1878 return false;
1879 }
1880}
1881
1882// This is a stripped-down version of tessellate() which computes edges which
1883// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001884bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1885 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001886 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001887 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001888 EventList events(comp);
1889 SSVertexMap ssVertices;
1890 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001891 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001892 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001893 continue;
1894 }
1895 Edge* leftEnclosingEdge;
1896 Edge* rightEnclosingEdge;
1897 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001898 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001899 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1900 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001901 bool leftOverlap = prev && is_overlap_edge(prev);
1902 bool rightOverlap = is_overlap_edge(e);
1903 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1904 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001905 if (prev) {
1906 e->fWinding -= prev->fWinding;
1907 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001908 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001909 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1910 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001911 disconnect(e);
1912 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001913 TESS_LOG("found overlap edge %g -> %g%s\n",
1914 e->fTop->fID, e->fBottom->fID,
1915 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001916 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1917 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1918 SSVertex* ssPrev = ssVertices[prevVertex];
1919 if (!ssPrev) {
1920 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1921 }
1922 SSVertex* ssNext = ssVertices[nextVertex];
1923 if (!ssNext) {
1924 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1925 }
1926 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1927 ssEdges.push_back(ssEdge);
1928// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1929 ssPrev->fNext = ssNext->fPrev = ssEdge;
1930 create_event(ssEdge, &events, alloc);
1931 if (!isOuterBoundary) {
1932 disconnect(e);
1933 }
1934 }
1935 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001936 }
1937 Edge* prev = leftEnclosingEdge;
1938 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1939 if (prev) {
1940 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001941 }
1942 insert_edge(e, prev, &activeEdges);
1943 prev = e;
1944 }
1945 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001946 bool complex = events.size() > 0;
1947
Brian Salomon120e7d62019-09-11 10:29:22 -04001948 TESS_LOG("\ncollapsing overlap regions\n");
1949 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001950 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001951 while (events.size() > 0) {
1952 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001953 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001954 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001955 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001956 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001957 dump_skel(ssEdges);
1958 for (SSEdge* edge : ssEdges) {
1959 if (Edge* e = edge->fEdge) {
1960 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1961 }
1962 }
1963 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001964}
1965
1966bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1967 if (!prev || !next) {
1968 return true;
1969 }
1970 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1971 return winding != origEdge->fWinding;
1972}
Stephen White92eba8a2017-02-06 09:50:27 -05001973
senorblancof57372d2016-08-31 10:36:19 -07001974// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1975// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1976// new antialiased mesh from those vertices.
1977
Stephen Whitee260c462017-12-19 18:09:54 -05001978void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1979 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001980 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001981 // A boundary with fewer than 3 edges is degenerate.
1982 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1983 return;
1984 }
1985 Edge* prevEdge = boundary->fTail;
1986 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1987 SkVector prevNormal;
1988 get_edge_normal(prevEdge, &prevNormal);
1989 double radius = 0.5;
1990 Line prevInner(prevEdge->fLine);
1991 prevInner.fC -= radius;
1992 Line prevOuter(prevEdge->fLine);
1993 prevOuter.fC += radius;
1994 VertexList innerVertices;
1995 VertexList outerVertices;
1996 bool innerInversion = true;
1997 bool outerInversion = true;
1998 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1999 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2000 SkVector normal;
2001 get_edge_normal(e, &normal);
2002 Line inner(e->fLine);
2003 inner.fC -= radius;
2004 Line outer(e->fLine);
2005 outer.fC += radius;
2006 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04002007 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002008 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2009 prevOuter.intersect(outer, &outerPoint)) {
2010 float cosAngle = normal.dot(prevNormal);
2011 if (cosAngle < -kCosMiterAngle) {
2012 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2013
2014 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2015 Line bisector(innerPoint, outerPoint);
2016 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2017 if (tangent.fA == 0 && tangent.fB == 0) {
2018 continue;
2019 }
2020 tangent.normalize();
2021 Line innerTangent(tangent);
2022 Line outerTangent(tangent);
2023 innerTangent.fC -= 0.5;
2024 outerTangent.fC += 0.5;
2025 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2026 if (prevNormal.cross(normal) > 0) {
2027 // Miter inner points
2028 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2029 !innerTangent.intersect(inner, &innerPoint2) ||
2030 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002031 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002032 }
2033 Line prevTangent(prevV->fPoint,
2034 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2035 Line nextTangent(nextV->fPoint,
2036 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002037 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002038 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002039 }
2040 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002041 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002042 }
2043 outerPoint1 = outerPoint2 = outerPoint;
2044 } else {
2045 // Miter outer points
2046 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2047 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002048 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002049 }
2050 Line prevTangent(prevV->fPoint,
2051 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2052 Line nextTangent(nextV->fPoint,
2053 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002054 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002055 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002056 }
2057 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002058 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002059 }
2060 innerPoint1 = innerPoint2 = innerPoint;
2061 }
Stephen Whiteea495232018-04-03 11:28:15 -04002062 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2063 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2064 continue;
2065 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002066 TESS_LOG("inner (%g, %g), (%g, %g), ",
2067 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2068 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2069 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002070 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2071 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2072 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2073 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2074 innerVertex1->fPartner = outerVertex1;
2075 innerVertex2->fPartner = outerVertex2;
2076 outerVertex1->fPartner = innerVertex1;
2077 outerVertex2->fPartner = innerVertex2;
2078 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2079 innerInversion = false;
2080 }
2081 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2082 outerInversion = false;
2083 }
2084 innerVertices.append(innerVertex1);
2085 innerVertices.append(innerVertex2);
2086 outerVertices.append(outerVertex1);
2087 outerVertices.append(outerVertex2);
2088 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002089 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2090 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002091 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2092 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2093 innerVertex->fPartner = outerVertex;
2094 outerVertex->fPartner = innerVertex;
2095 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2096 innerInversion = false;
2097 }
2098 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2099 outerInversion = false;
2100 }
2101 innerVertices.append(innerVertex);
2102 outerVertices.append(outerVertex);
2103 }
2104 }
2105 prevInner = inner;
2106 prevOuter = outer;
2107 prevV = v;
2108 prevEdge = e;
2109 prevNormal = normal;
2110 }
2111 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2112 innerInversion = false;
2113 }
2114 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2115 outerInversion = false;
2116 }
2117 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2118 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2119 // interior inverts).
2120 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2121 // so it will be detected during collapse_overlap_regions().
2122 int innerWinding = innerInversion ? 2 : -2;
2123 int outerWinding = outerInversion ? -1 : 1;
2124 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2125 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2126 }
2127 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2128 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2129 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2130 }
2131 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2132 innerMesh->append(innerVertices);
2133 outerMesh->append(outerVertices);
2134}
senorblancof57372d2016-08-31 10:36:19 -07002135
Mike Reed7d34dc72019-11-26 12:17:17 -05002136void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002137 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002138 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002139 Vertex* start = down ? e->fTop : e->fBottom;
2140 do {
senorblancof57372d2016-08-31 10:36:19 -07002141 e->fWinding = down ? 1 : -1;
2142 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002143 e->fLine.normalize();
2144 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002145 boundary->append(e);
2146 if (down) {
2147 // Find outgoing edge, in clockwise order.
2148 if ((next = e->fNextEdgeAbove)) {
2149 down = false;
2150 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2151 down = true;
2152 } else if ((next = e->fPrevEdgeAbove)) {
2153 down = false;
2154 }
2155 } else {
2156 // Find outgoing edge, in counter-clockwise order.
2157 if ((next = e->fPrevEdgeBelow)) {
2158 down = true;
2159 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2160 down = false;
2161 } else if ((next = e->fNextEdgeBelow)) {
2162 down = true;
2163 }
2164 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002165 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002166 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002167 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002168}
2169
Stephen White5ad721e2017-02-23 16:50:47 -05002170// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002171
Stephen Whitebda29c02017-03-13 15:10:13 -04002172void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Mike Reed7d34dc72019-11-26 12:17:17 -05002173 VertexList* outerVertices, SkPathFillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002174 Comparator& c, SkArenaAlloc& alloc) {
2175 remove_non_boundary_edges(inMesh, fillType, alloc);
2176 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002177 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002178 EdgeList boundary;
2179 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2180 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002181 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002182 }
2183 }
senorblancof57372d2016-08-31 10:36:19 -07002184}
2185
Stephen Whitebda29c02017-03-13 15:10:13 -04002186// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002187
Chris Daltondcc8c542020-01-28 17:55:56 -07002188void contours_to_mesh(VertexList* contours, int contourCnt, Mode mode,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002189 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002190#if LOGGING_ENABLED
2191 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002192 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002193 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002194 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002195 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002196 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002197 }
2198 }
2199#endif
Chris Daltondcc8c542020-01-28 17:55:56 -07002200 sanitize_contours(contours, contourCnt, mode);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002201 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002202}
2203
Stephen Whitebda29c02017-03-13 15:10:13 -04002204void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002205 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002206 return;
ethannicholase9709e82016-01-07 13:34:16 -08002207 }
2208
2209 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002210 if (c.fDirection == Comparator::Direction::kHorizontal) {
2211 merge_sort<sweep_lt_horiz>(vertices);
2212 } else {
2213 merge_sort<sweep_lt_vert>(vertices);
2214 }
ethannicholase9709e82016-01-07 13:34:16 -08002215#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002216 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002217 static float gID = 0.0f;
2218 v->fID = gID++;
2219 }
2220#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002221}
2222
Mike Reed7d34dc72019-11-26 12:17:17 -05002223Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPathFillType fillType,
Chris Daltondcc8c542020-01-28 17:55:56 -07002224 const SkRect& pathBounds, Mode mode, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002225 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002226 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2227 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002228 VertexList mesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002229 contours_to_mesh(contours, contourCnt, mode, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002230 sort_mesh(&mesh, c, alloc);
2231 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002232 simplify(&mesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002233 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002234 dump_mesh(mesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002235 if (Mode::kEdgeAntialias == mode) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002236 VertexList innerMesh;
2237 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2238 sort_mesh(&innerMesh, c, alloc);
2239 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002240 merge_coincident_vertices(&innerMesh, c, alloc);
2241 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2242 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2243 was_complex = simplify(outerMesh, c, alloc) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002244 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002245 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002246 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002247 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002248 EventComparator eventLT(EventComparator::Op::kLessThan);
2249 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2250 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2251 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002252 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002253 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002254 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002255 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002256 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002257 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002258 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002259 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002260 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002261 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2262 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002263 simplify(&aaMesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002264 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002265 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002266 outerMesh->fHead = outerMesh->fTail = nullptr;
2267 return tessellate(aaMesh, alloc);
2268 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002269 TESS_LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002270 return tessellate(innerMesh, alloc);
2271 }
Stephen White49789062017-02-21 10:35:49 -05002272 } else {
2273 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002274 }
senorblancof57372d2016-08-31 10:36:19 -07002275}
2276
2277// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Daltondcc8c542020-01-28 17:55:56 -07002278void* polys_to_triangles(Poly* polys, SkPathFillType fillType, Mode mode, void* data) {
2279 bool emitCoverage = (Mode::kEdgeAntialias == mode);
senorblancof57372d2016-08-31 10:36:19 -07002280 for (Poly* poly = polys; poly; poly = poly->fNext) {
2281 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002282 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002283 }
2284 }
2285 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002286}
2287
halcanary9d524f22016-03-29 09:03:52 -07002288Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Daltondcc8c542020-01-28 17:55:56 -07002289 int contourCnt, SkArenaAlloc& alloc, Mode mode, bool* isLinear,
Stephen Whitebda29c02017-03-13 15:10:13 -04002290 VertexList* outerMesh) {
Mike Reedcf0e3c62019-12-03 16:26:15 -05002291 SkPathFillType fillType = path.getFillType();
Mike Reed7d34dc72019-11-26 12:17:17 -05002292 if (SkPathFillType_IsInverse(fillType)) {
ethannicholase9709e82016-01-07 13:34:16 -08002293 contourCnt++;
2294 }
Stephen White3a9aab92017-03-07 14:07:18 -05002295 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002296
2297 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002298 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Chris Daltondcc8c542020-01-28 17:55:56 -07002299 mode, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002300}
2301
Stephen White11f65e02017-02-16 19:00:39 -05002302int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002303 // We could theoretically be more aggressive about not counting empty contours, but we need to
2304 // actually match the exact number of contour linked lists the tessellator will create later on.
2305 int contourCnt = 1;
2306 bool hasPoints = false;
2307
2308 SkPath::Iter iter(path, false);
2309 SkPath::Verb verb;
2310 SkPoint pts[4];
2311 bool first = true;
2312 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
2313 switch (verb) {
2314 case SkPath::kMove_Verb:
2315 if (!first) {
2316 ++contourCnt;
2317 }
2318 // fallthru.
2319 case SkPath::kLine_Verb:
2320 case SkPath::kConic_Verb:
2321 case SkPath::kQuad_Verb:
2322 case SkPath::kCubic_Verb:
2323 hasPoints = true;
2324 // fallthru to break.
2325 default:
2326 break;
2327 }
2328 first = false;
2329 }
2330 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002331 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002332 }
Stephen White11f65e02017-02-16 19:00:39 -05002333 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002334}
2335
Mike Reed7d34dc72019-11-26 12:17:17 -05002336int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002337 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002338 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002339 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002340 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2341 }
2342 }
2343 return count;
2344}
2345
Greg Danield5b45932018-06-07 13:15:10 -04002346int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2347 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002348 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2349 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2350 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2351 }
2352 }
2353 return count;
2354}
2355
Brian Osman0995fd52019-01-09 09:52:25 -05002356void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002357 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2358 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2359 Vertex* v0 = e->fTop;
2360 Vertex* v1 = e->fBottom;
2361 Vertex* v2 = e->fBottom->fPartner;
2362 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002363 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2364 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002365 }
2366 }
2367 return data;
2368}
2369
ethannicholase9709e82016-01-07 13:34:16 -08002370} // namespace
2371
2372namespace GrTessellator {
2373
2374// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2375
halcanary9d524f22016-03-29 09:03:52 -07002376int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Daltondcc8c542020-01-28 17:55:56 -07002377 GrEagerVertexAllocator* vertexAllocator, Mode mode, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002378 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002379 if (contourCnt <= 0) {
2380 *isLinear = true;
2381 return 0;
2382 }
Stephen White11f65e02017-02-16 19:00:39 -05002383 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002384 VertexList outerMesh;
Chris Daltondcc8c542020-01-28 17:55:56 -07002385 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, mode,
Stephen Whitebda29c02017-03-13 15:10:13 -04002386 isLinear, &outerMesh);
Chris Daltondcc8c542020-01-28 17:55:56 -07002387 SkPathFillType fillType = (Mode::kEdgeAntialias == mode) ?
2388 SkPathFillType::kWinding : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002389 int64_t count64 = count_points(polys, fillType);
Chris Daltondcc8c542020-01-28 17:55:56 -07002390 if (Mode::kEdgeAntialias == mode) {
Greg Danield5b45932018-06-07 13:15:10 -04002391 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002392 }
Greg Danield5b45932018-06-07 13:15:10 -04002393 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002394 return 0;
2395 }
Greg Danield5b45932018-06-07 13:15:10 -04002396 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002397
Chris Daltondcc8c542020-01-28 17:55:56 -07002398 size_t vertexStride = GetVertexStride(mode);
Chris Daltond081dce2020-01-23 12:09:04 -07002399 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002400 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002401 SkDebugf("Could not allocate vertices\n");
2402 return 0;
2403 }
senorblancof57372d2016-08-31 10:36:19 -07002404
Brian Salomon120e7d62019-09-11 10:29:22 -04002405 TESS_LOG("emitting %d verts\n", count);
Chris Daltondcc8c542020-01-28 17:55:56 -07002406 void* end = polys_to_triangles(polys, fillType, mode, verts);
Brian Osman80879d42019-01-07 16:15:27 -05002407 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002408
senorblancof57372d2016-08-31 10:36:19 -07002409 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002410 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002411 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002412 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002413 return actualCount;
2414}
2415
halcanary9d524f22016-03-29 09:03:52 -07002416int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002417 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002418 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002419 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002420 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002421 return 0;
2422 }
Stephen White11f65e02017-02-16 19:00:39 -05002423 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002424 bool isLinear;
Chris Daltondcc8c542020-01-28 17:55:56 -07002425 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, Mode::kNormal,
2426 &isLinear, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002427 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002428 int64_t count64 = count_points(polys, fillType);
2429 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002430 *verts = nullptr;
2431 return 0;
2432 }
Greg Danield5b45932018-06-07 13:15:10 -04002433 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002434
2435 *verts = new GrTessellator::WindingVertex[count];
2436 GrTessellator::WindingVertex* vertsEnd = *verts;
2437 SkPoint* points = new SkPoint[count];
2438 SkPoint* pointsEnd = points;
2439 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002440 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002441 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002442 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002443 while (start != pointsEnd) {
2444 vertsEnd->fPos = *start;
2445 vertsEnd->fWinding = poly->fWinding;
2446 ++start;
2447 ++vertsEnd;
2448 }
2449 }
2450 }
2451 int actualCount = static_cast<int>(vertsEnd - *verts);
2452 SkASSERT(actualCount <= count);
2453 SkASSERT(pointsEnd - points == actualCount);
2454 delete[] points;
2455 return actualCount;
2456}
2457
2458} // namespace