blob: 6f4fd44ffe946d2363461f9777ead7bdfc01f8d4 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040012#include "src/gpu/geometry/GrPathUtils.h"
ethannicholase9709e82016-01-07 13:34:16 -080013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPath.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040015#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkGeometry.h"
17#include "src/core/SkPointPriv.h"
ethannicholase9709e82016-01-07 13:34:16 -080018
Stephen White94b7e542018-01-04 14:01:10 -050019#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040020#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040021#include <queue>
22#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040023#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080024
25/*
senorblancof57372d2016-08-31 10:36:19 -070026 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080027 *
28 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
29 * 2) Build a mesh of edges connecting the vertices (build_edges()).
30 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
31 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
32 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
33 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
34 *
senorblancof57372d2016-08-31 10:36:19 -070035 * For screenspace antialiasing, the algorithm is modified as follows:
36 *
37 * Run steps 1-5 above to produce polygons.
38 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040039 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070040 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
41 * 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 -040042 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070043 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
44 *
ethannicholase9709e82016-01-07 13:34:16 -080045 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
46 * of vertices (and the necessity of inserting new vertices on intersection).
47 *
Stephen Whitebda29c02017-03-13 15:10:13 -040048 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080049 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
50 * left-to-right based on the point where both edges are active (when both top vertices
51 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
52 * (shared), it's sorted based on the last point where both edges are active, so the
53 * "upper" bottom vertex.
54 *
55 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
56 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
57 * not exact and may violate the mesh topology or active edge list ordering. We
58 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040059 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080060 *
61 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
62 * neighbouring edges at the top or bottom vertex. This is handled by merging the
63 * edges (merge_collinear_edges()).
64 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen Whiteb67b2352019-06-01 13:07:27 -040065 * active edge list. This is handled during merging or splitting by rewind()ing the
66 * active edge list to the vertex before potential violations occur.
ethannicholase9709e82016-01-07 13:34:16 -080067 *
68 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
69 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
70 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
71 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
72 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
73 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
74 * linked list implementation. With the latter, all removals are O(1), and most insertions
75 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
76 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
77 * frequent. There may be other data structures worth investigating, however.
78 *
79 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
80 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
81 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
82 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
83 * that the "left" and "right" orientation in the code remains correct (edges to the left are
84 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
85 * degrees counterclockwise, rather that transposing.
86 */
87
88#define LOGGING_ENABLED 0
89
90#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -040091#define TESS_LOG printf
ethannicholase9709e82016-01-07 13:34:16 -080092#else
Brian Salomon120e7d62019-09-11 10:29:22 -040093#define TESS_LOG(...)
ethannicholase9709e82016-01-07 13:34:16 -080094#endif
95
ethannicholase9709e82016-01-07 13:34:16 -080096namespace {
97
Stephen White11f65e02017-02-16 19:00:39 -050098const int kArenaChunkSize = 16 * 1024;
Stephen Whitee260c462017-12-19 18:09:54 -050099const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
Stephen White11f65e02017-02-16 19:00:39 -0500100
ethannicholase9709e82016-01-07 13:34:16 -0800101struct Vertex;
102struct Edge;
Stephen Whitee260c462017-12-19 18:09:54 -0500103struct Event;
ethannicholase9709e82016-01-07 13:34:16 -0800104struct Poly;
105
106template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800107void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800108 t->*Prev = prev;
109 t->*Next = next;
110 if (prev) {
111 prev->*Next = t;
112 } else if (head) {
113 *head = t;
114 }
115 if (next) {
116 next->*Prev = t;
117 } else if (tail) {
118 *tail = t;
119 }
120}
121
122template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800123void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800124 if (t->*Prev) {
125 t->*Prev->*Next = t->*Next;
126 } else if (head) {
127 *head = t->*Next;
128 }
129 if (t->*Next) {
130 t->*Next->*Prev = t->*Prev;
131 } else if (tail) {
132 *tail = t->*Prev;
133 }
134 t->*Prev = t->*Next = nullptr;
135}
136
137/**
138 * Vertices are used in three ways: first, the path contours are converted into a
139 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
140 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
141 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
142 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
143 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
144 * an individual Vertex from the path mesh may belong to multiple
145 * MonotonePolys, so the original Vertices cannot be re-used.
146 */
147
148struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700149 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800150 : fPoint(point), fPrev(nullptr), fNext(nullptr)
151 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
152 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -0400153 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -0400154 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -0700155 , fAlpha(alpha)
Stephen Whitec4dbc372019-05-22 10:50:14 -0400156 , fSynthetic(false)
ethannicholase9709e82016-01-07 13:34:16 -0800157#if LOGGING_ENABLED
158 , fID (-1.0f)
159#endif
160 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400161 SkPoint fPoint; // Vertex position
162 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
163 Vertex* fNext; // "
164 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
165 Edge* fLastEdgeAbove; // "
166 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
167 Edge* fLastEdgeBelow; // "
168 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
169 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
170 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700171 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400172 bool fSynthetic; // Is this a synthetic vertex?
ethannicholase9709e82016-01-07 13:34:16 -0800173#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400174 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800175#endif
176};
177
178/***************************************************************************************/
179
180typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
181
ethannicholase9709e82016-01-07 13:34:16 -0800182bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500183 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800184}
185
186bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500187 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800188}
189
Stephen White16a40cb2017-02-23 11:10:01 -0500190struct Comparator {
191 enum class Direction { kVertical, kHorizontal };
192 Comparator(Direction direction) : fDirection(direction) {}
193 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
194 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
195 }
Stephen White16a40cb2017-02-23 11:10:01 -0500196 Direction fDirection;
197};
198
Brian Osman0995fd52019-01-09 09:52:25 -0500199inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500200 GrVertexWriter verts{data};
201 verts.write(v->fPoint);
202
Brian Osman80879d42019-01-07 16:15:27 -0500203 if (emitCoverage) {
204 verts.write(GrNormalizeByteToFloat(v->fAlpha));
205 }
Brian Osman0995fd52019-01-09 09:52:25 -0500206
Brian Osmanf9aabff2018-11-13 16:11:38 -0500207 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800208}
209
Brian Osman0995fd52019-01-09 09:52:25 -0500210void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400211 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
212 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
213 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700214#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500215 data = emit_vertex(v0, emitCoverage, data);
216 data = emit_vertex(v1, emitCoverage, data);
217 data = emit_vertex(v1, emitCoverage, data);
218 data = emit_vertex(v2, emitCoverage, data);
219 data = emit_vertex(v2, emitCoverage, data);
220 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800221#else
Brian Osman0995fd52019-01-09 09:52:25 -0500222 data = emit_vertex(v0, emitCoverage, data);
223 data = emit_vertex(v1, emitCoverage, data);
224 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800225#endif
226 return data;
227}
228
senorblancoe6eaa322016-03-08 09:06:44 -0800229struct VertexList {
230 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500231 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800232 Vertex* fHead;
233 Vertex* fTail;
234 void insert(Vertex* v, Vertex* prev, Vertex* next) {
235 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
236 }
237 void append(Vertex* v) {
238 insert(v, fTail, nullptr);
239 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400240 void append(const VertexList& list) {
241 if (!list.fHead) {
242 return;
243 }
244 if (fTail) {
245 fTail->fNext = list.fHead;
246 list.fHead->fPrev = fTail;
247 } else {
248 fHead = list.fHead;
249 }
250 fTail = list.fTail;
251 }
senorblancoe6eaa322016-03-08 09:06:44 -0800252 void prepend(Vertex* v) {
253 insert(v, nullptr, fHead);
254 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500255 void remove(Vertex* v) {
256 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
257 }
senorblancof57372d2016-08-31 10:36:19 -0700258 void close() {
259 if (fHead && fTail) {
260 fTail->fNext = fHead;
261 fHead->fPrev = fTail;
262 }
263 }
senorblancoe6eaa322016-03-08 09:06:44 -0800264};
265
senorblancof57372d2016-08-31 10:36:19 -0700266// Round to nearest quarter-pixel. This is used for screenspace tessellation.
267
268inline void round(SkPoint* p) {
269 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
270 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
271}
272
Stephen White94b7e542018-01-04 14:01:10 -0500273inline SkScalar double_to_clamped_scalar(double d) {
274 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
275}
276
senorblanco49df8d12016-10-07 08:36:56 -0700277// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
278struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500279 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700280 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
281 Line(const SkPoint& p, const SkPoint& q)
282 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
283 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
284 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
285 static_cast<double>(p.fX) * q.fY) {}
286 double dist(const SkPoint& p) const {
287 return fA * p.fX + fB * p.fY + fC;
288 }
Stephen Whitee260c462017-12-19 18:09:54 -0500289 Line operator*(double v) const {
290 return Line(fA * v, fB * v, fC * v);
291 }
senorblanco49df8d12016-10-07 08:36:56 -0700292 double magSq() const {
293 return fA * fA + fB * fB;
294 }
Stephen Whitee260c462017-12-19 18:09:54 -0500295 void normalize() {
296 double len = sqrt(this->magSq());
297 if (len == 0.0) {
298 return;
299 }
300 double scale = 1.0f / len;
301 fA *= scale;
302 fB *= scale;
303 fC *= scale;
304 }
305 bool nearParallel(const Line& o) const {
306 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
307 }
senorblanco49df8d12016-10-07 08:36:56 -0700308
309 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500310 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700311 double denom = fA * other.fB - fB * other.fA;
312 if (denom == 0.0) {
313 return false;
314 }
Stephen White94b7e542018-01-04 14:01:10 -0500315 double scale = 1.0 / denom;
316 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
317 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500318 round(point);
Stephen White9b7f1432019-06-08 08:56:58 -0400319 return point->isFinite();
senorblanco49df8d12016-10-07 08:36:56 -0700320 }
321 double fA, fB, fC;
322};
323
ethannicholase9709e82016-01-07 13:34:16 -0800324/**
325 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
326 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
327 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whiteb67b2352019-06-01 13:07:27 -0400328 * point). For speed, that case is only tested by the callers that require it. Edges also handle
329 * checking for intersection with other edges. Currently, this converts the edges to the
330 * parametric form, in order to avoid doing a division until an intersection has been confirmed.
331 * This is slightly slower in the "found" case, but a lot faster in the "not found" case.
ethannicholase9709e82016-01-07 13:34:16 -0800332 *
333 * The coefficients of the line equation stored in double precision to avoid catastrphic
334 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
335 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
336 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
337 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
338 * this file).
339 */
340
341struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500342 enum class Type { kInner, kOuter, kConnector };
343 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800344 : fWinding(winding)
345 , fTop(top)
346 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500347 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800348 , fLeft(nullptr)
349 , fRight(nullptr)
350 , fPrevEdgeAbove(nullptr)
351 , fNextEdgeAbove(nullptr)
352 , fPrevEdgeBelow(nullptr)
353 , fNextEdgeBelow(nullptr)
354 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700355 , fRightPoly(nullptr)
356 , fLeftPolyPrev(nullptr)
357 , fLeftPolyNext(nullptr)
358 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700359 , fRightPolyNext(nullptr)
360 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700361 , fUsedInRightPoly(false)
362 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800363 }
364 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
365 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
366 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500367 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800368 Edge* fLeft; // The linked list of edges in the active edge list.
369 Edge* fRight; // "
370 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
371 Edge* fNextEdgeAbove; // "
372 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
373 Edge* fNextEdgeBelow; // "
374 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
375 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700376 Edge* fLeftPolyPrev;
377 Edge* fLeftPolyNext;
378 Edge* fRightPolyPrev;
379 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700380 bool fUsedInLeftPoly;
381 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700382 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800383 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700384 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800385 }
386 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700387 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800388 }
389 bool isLeftOf(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 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700393 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800394 }
Stephen White95152e12017-12-18 10:52:44 -0500395 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
Brian Salomon120e7d62019-09-11 10:29:22 -0400396 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
397 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800398 if (fTop == other.fTop || fBottom == other.fBottom) {
399 return false;
400 }
senorblanco49df8d12016-10-07 08:36:56 -0700401 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800402 if (denom == 0.0) {
403 return false;
404 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500405 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
406 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
407 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
408 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800409 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
410 // This saves us doing the divide below unless absolutely necessary.
411 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
412 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
413 return false;
414 }
415 double s = sNumer / denom;
416 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700417 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
418 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500419 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500420 if (fType == Type::kConnector) {
421 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
422 } else if (other.fType == Type::kConnector) {
423 double t = tNumer / denom;
424 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500425 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
426 *alpha = 0;
427 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500428 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500429 }
430 }
ethannicholase9709e82016-01-07 13:34:16 -0800431 return true;
432 }
senorblancof57372d2016-08-31 10:36:19 -0700433};
434
Stephen Whitec4dbc372019-05-22 10:50:14 -0400435struct SSEdge;
436
437struct SSVertex {
438 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
439 Vertex* fVertex;
440 SSEdge* fPrev;
441 SSEdge* fNext;
442};
443
444struct SSEdge {
445 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
446 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
447 }
448 Edge* fEdge;
449 Event* fEvent;
450 SSVertex* fPrev;
451 SSVertex* fNext;
452};
453
454typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
455typedef std::vector<SSEdge*> SSEdgeList;
456
senorblancof57372d2016-08-31 10:36:19 -0700457struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500458 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700459 Edge* fHead;
460 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700461 void insert(Edge* edge, Edge* prev, Edge* next) {
462 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700463 }
464 void append(Edge* e) {
465 insert(e, fTail, nullptr);
466 }
467 void remove(Edge* edge) {
468 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700469 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400470 void removeAll() {
471 while (fHead) {
472 this->remove(fHead);
473 }
474 }
senorblancof57372d2016-08-31 10:36:19 -0700475 void close() {
476 if (fHead && fTail) {
477 fTail->fRight = fHead;
478 fHead->fLeft = fTail;
479 }
480 }
481 bool contains(Edge* edge) const {
482 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800483 }
484};
485
Stephen Whitec4dbc372019-05-22 10:50:14 -0400486struct EventList;
487
Stephen Whitee260c462017-12-19 18:09:54 -0500488struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400489 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
490 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500491 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400492 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500493 SkPoint fPoint;
494 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400495 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500496};
497
Stephen Whitec4dbc372019-05-22 10:50:14 -0400498struct EventComparator {
499 enum class Op { kLessThan, kGreaterThan };
500 EventComparator(Op op) : fOp(op) {}
501 bool operator() (Event* const &e1, Event* const &e2) {
502 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
503 : e1->fAlpha > e2->fAlpha;
504 }
505 Op fOp;
506};
Stephen Whitee260c462017-12-19 18:09:54 -0500507
Stephen Whitec4dbc372019-05-22 10:50:14 -0400508typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500509
Stephen Whitec4dbc372019-05-22 10:50:14 -0400510struct EventList : EventPQ {
511 EventList(EventComparator comparison) : EventPQ(comparison) {
512 }
513};
514
515void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
516 Vertex* prev = e->fPrev->fVertex;
517 Vertex* next = e->fNext->fVertex;
518 if (prev == next || !prev->fPartner || !next->fPartner) {
519 return;
520 }
521 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
522 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500523 SkPoint p;
524 uint8_t alpha;
525 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400526 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
527 "will collapse to %g,%g alpha %d\n",
528 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
529 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400530 e->fEvent = alloc.make<Event>(e, p, alpha);
531 events->push(e->fEvent);
532 }
533}
534
535void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
536 Comparator& c, SkArenaAlloc& alloc) {
537 if (!v->fPartner) {
538 return;
539 }
Stephen White8a3c0592019-05-29 11:26:16 -0400540 Vertex* top = edge->fEdge->fTop;
541 Vertex* bottom = edge->fEdge->fBottom;
542 if (!top || !bottom ) {
543 return;
544 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400545 Line line = edge->fEdge->fLine;
546 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
547 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
548 SkPoint p;
549 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400550 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
551 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400552 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
553 "will collapse to %g,%g alpha %d\n",
554 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400555 edge->fEvent = alloc.make<Event>(edge, p, alpha);
556 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500557 }
558}
Stephen Whitee260c462017-12-19 18:09:54 -0500559
ethannicholase9709e82016-01-07 13:34:16 -0800560/***************************************************************************************/
561
562struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700563 Poly(Vertex* v, int winding)
564 : fFirstVertex(v)
565 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800566 , fHead(nullptr)
567 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800568 , fNext(nullptr)
569 , fPartner(nullptr)
570 , fCount(0)
571 {
572#if LOGGING_ENABLED
573 static int gID = 0;
574 fID = gID++;
Brian Salomon120e7d62019-09-11 10:29:22 -0400575 TESS_LOG("*** created Poly %d\n", fID);
ethannicholase9709e82016-01-07 13:34:16 -0800576#endif
577 }
senorblanco531237e2016-06-02 11:36:48 -0700578 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800579 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700580 MonotonePoly(Edge* edge, Side side)
581 : fSide(side)
582 , fFirstEdge(nullptr)
583 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800584 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700585 , fNext(nullptr) {
586 this->addEdge(edge);
587 }
ethannicholase9709e82016-01-07 13:34:16 -0800588 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700589 Edge* fFirstEdge;
590 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800591 MonotonePoly* fPrev;
592 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700593 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800594 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700595 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700596 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
597 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700598 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800599 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700600 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700601 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
602 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700603 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800604 }
ethannicholase9709e82016-01-07 13:34:16 -0800605 }
606
Brian Osman0995fd52019-01-09 09:52:25 -0500607 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700608 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700609 VertexList vertices;
610 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500611 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700612 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700613 if (kRight_Side == fSide) {
614 vertices.append(e->fBottom);
615 e = e->fRightPolyNext;
616 } else {
617 vertices.prepend(e->fBottom);
618 e = e->fLeftPolyNext;
619 }
Stephen White651cbe92017-03-03 12:24:16 -0500620 count++;
senorblanco531237e2016-06-02 11:36:48 -0700621 }
622 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800623 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700624 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800625 SkASSERT(v && v->fPrev && v->fNext);
626 Vertex* prev = v->fPrev;
627 Vertex* curr = v;
628 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500629 if (count == 3) {
Brian Osman0995fd52019-01-09 09:52:25 -0500630 return emit_triangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500631 }
ethannicholase9709e82016-01-07 13:34:16 -0800632 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
633 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
634 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
635 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
636 if (ax * by - ay * bx >= 0.0) {
Brian Osman0995fd52019-01-09 09:52:25 -0500637 data = emit_triangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800638 v->fPrev->fNext = v->fNext;
639 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500640 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800641 if (v->fPrev == first) {
642 v = v->fNext;
643 } else {
644 v = v->fPrev;
645 }
646 } else {
647 v = v->fNext;
648 }
649 }
650 return data;
651 }
652 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500653 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400654 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
655 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800656 Poly* partner = fPartner;
657 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700658 if (side == kRight_Side) {
659 if (e->fUsedInRightPoly) {
660 return this;
661 }
662 } else {
663 if (e->fUsedInLeftPoly) {
664 return this;
665 }
666 }
ethannicholase9709e82016-01-07 13:34:16 -0800667 if (partner) {
668 fPartner = partner->fPartner = nullptr;
669 }
senorblanco531237e2016-06-02 11:36:48 -0700670 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500671 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700672 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700673 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
674 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700675 } else if (side == fTail->fSide) {
676 fTail->addEdge(e);
677 fCount++;
678 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500679 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700680 fTail->addEdge(e);
681 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800682 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700683 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800684 poly = partner;
685 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500686 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700687 m->fPrev = fTail;
688 fTail->fNext = m;
689 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800690 }
691 }
ethannicholase9709e82016-01-07 13:34:16 -0800692 return poly;
693 }
Brian Osman0995fd52019-01-09 09:52:25 -0500694 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800695 if (fCount < 3) {
696 return data;
697 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400698 TESS_LOG("emit() %d, size %d\n", fID, fCount);
ethannicholase9709e82016-01-07 13:34:16 -0800699 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500700 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800701 }
702 return data;
703 }
senorblanco531237e2016-06-02 11:36:48 -0700704 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
705 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800706 int fWinding;
707 MonotonePoly* fHead;
708 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800709 Poly* fNext;
710 Poly* fPartner;
711 int fCount;
712#if LOGGING_ENABLED
713 int fID;
714#endif
715};
716
717/***************************************************************************************/
718
719bool coincident(const SkPoint& a, const SkPoint& b) {
720 return a == b;
721}
722
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500723Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
724 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800725 poly->fNext = *head;
726 *head = poly;
727 return poly;
728}
729
Stephen White3a9aab92017-03-07 14:07:18 -0500730void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500731 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800732#if LOGGING_ENABLED
733 static float gID = 0.0f;
734 v->fID = gID++;
735#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500736 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800737}
738
Stephen White36e4f062017-03-27 16:11:31 -0400739SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
740 SkQuadCoeff quad(pts);
741 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
742 SkPoint mid = to_point(quad.eval(t));
743 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400744 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
745 return 0;
746 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500747 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400748}
749
750void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
751 SkArenaAlloc& alloc) {
752 SkQuadCoeff quad(pts);
753 Sk2s aa = quad.fA * quad.fA;
754 SkScalar denom = 2.0f * (aa[0] + aa[1]);
755 Sk2s ab = quad.fA * quad.fB;
756 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
757 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500758 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400759 // Test possible subdivision values only at the point of maximum curvature.
760 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500761 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400762 u = 1.0f / nPoints;
763 if (quad_error_at(pts, t, u) < toleranceSqd) {
764 break;
765 }
766 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800767 }
Stephen White36e4f062017-03-27 16:11:31 -0400768 for (int j = 1; j <= nPoints; j++) {
769 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
770 }
ethannicholase9709e82016-01-07 13:34:16 -0800771}
772
Stephen White3a9aab92017-03-07 14:07:18 -0500773void generate_cubic_points(const SkPoint& p0,
774 const SkPoint& p1,
775 const SkPoint& p2,
776 const SkPoint& p3,
777 SkScalar tolSqd,
778 VertexList* contour,
779 int pointsLeft,
780 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500781 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
782 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800783 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
784 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500785 append_point_to_contour(p3, contour, alloc);
786 return;
ethannicholase9709e82016-01-07 13:34:16 -0800787 }
788 const SkPoint q[] = {
789 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
790 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
791 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
792 };
793 const SkPoint r[] = {
794 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
795 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
796 };
797 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
798 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500799 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
800 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800801}
802
803// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
804
805void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500806 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800807 SkScalar toleranceSqd = tolerance * tolerance;
808
809 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800810 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500811 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800812 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800813 if (path.isInverseFillType()) {
814 SkPoint quad[4];
815 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700816 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500817 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800818 }
Stephen White3a9aab92017-03-07 14:07:18 -0500819 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800820 }
821 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500822 SkPath::Verb verb;
Mike Reedba7e9a62019-08-16 13:30:34 -0400823 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800824 switch (verb) {
825 case SkPath::kConic_Verb: {
826 SkScalar weight = iter.conicWeight();
827 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
828 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400829 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800830 quadPts += 2;
831 }
832 *isLinear = false;
833 break;
834 }
835 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500836 if (contour->fHead) {
837 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800838 }
Stephen White3a9aab92017-03-07 14:07:18 -0500839 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800840 break;
841 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500842 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800843 break;
844 }
845 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400846 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800847 *isLinear = false;
848 break;
849 }
850 case SkPath::kCubic_Verb: {
851 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500852 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
853 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800854 *isLinear = false;
855 break;
856 }
857 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800858 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800859 break;
860 }
861 }
862}
863
Stephen White49789062017-02-21 10:35:49 -0500864inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800865 switch (fillType) {
866 case SkPath::kWinding_FillType:
867 return winding != 0;
868 case SkPath::kEvenOdd_FillType:
869 return (winding & 1) != 0;
870 case SkPath::kInverseWinding_FillType:
senorblanco7ab96e92016-10-12 06:47:44 -0700871 return winding == 1;
ethannicholase9709e82016-01-07 13:34:16 -0800872 case SkPath::kInverseEvenOdd_FillType:
873 return (winding & 1) == 1;
874 default:
875 SkASSERT(false);
876 return false;
877 }
878}
879
Stephen White49789062017-02-21 10:35:49 -0500880inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
881 return poly && apply_fill_type(fillType, poly->fWinding);
882}
883
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500884Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500885 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800886 Vertex* top = winding < 0 ? next : prev;
887 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500888 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800889}
890
891void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400892 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700893 SkASSERT(edges->contains(edge));
894 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800895}
896
897void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400898 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700899 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800900 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700901 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800902}
903
904void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500905 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800906 *left = v->fFirstEdgeAbove->fLeft;
907 *right = v->fLastEdgeAbove->fRight;
908 return;
909 }
910 Edge* next = nullptr;
911 Edge* prev;
912 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
913 if (prev->isLeftOf(v)) {
914 break;
915 }
916 next = prev;
917 }
918 *left = prev;
919 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800920}
921
ethannicholase9709e82016-01-07 13:34:16 -0800922void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
923 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500924 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800925 return;
926 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400927 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
928 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800929 Edge* prev = nullptr;
930 Edge* next;
931 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
932 if (next->isRightOf(edge->fTop)) {
933 break;
934 }
935 prev = next;
936 }
senorblancoe6eaa322016-03-08 09:06:44 -0800937 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800938 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
939}
940
941void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
942 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500943 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800944 return;
945 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400946 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
947 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800948 Edge* prev = nullptr;
949 Edge* next;
950 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
951 if (next->isRightOf(edge->fBottom)) {
952 break;
953 }
954 prev = next;
955 }
senorblancoe6eaa322016-03-08 09:06:44 -0800956 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800957 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
958}
959
960void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400961 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400962 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
963 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800964 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800965 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
966}
967
968void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400969 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400970 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
971 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800972 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800973 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
974}
975
Stephen Whitee7a364d2017-01-11 16:19:26 -0500976void disconnect(Edge* edge)
977{
ethannicholase9709e82016-01-07 13:34:16 -0800978 remove_edge_above(edge);
979 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500980}
981
Stephen White3b5a3fa2017-06-06 14:51:19 -0400982void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
983
984void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
985 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
986 return;
987 }
988 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400989 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400990 while (v != dst) {
991 v = v->fPrev;
992 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
993 remove_edge(e, activeEdges);
994 }
995 Edge* leftEdge = v->fLeftEnclosingEdge;
996 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
997 insert_edge(e, leftEdge, activeEdges);
998 leftEdge = e;
999 }
1000 }
1001 *current = v;
1002}
1003
Stephen White3b5a3fa2017-06-06 14:51:19 -04001004void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001005 remove_edge_below(edge);
1006 edge->fTop = v;
1007 edge->recompute();
1008 insert_edge_below(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001009 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001010 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001011}
1012
Stephen White3b5a3fa2017-06-06 14:51:19 -04001013void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001014 remove_edge_above(edge);
1015 edge->fBottom = v;
1016 edge->recompute();
1017 insert_edge_above(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001018 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001019 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001020}
1021
Stephen White3b5a3fa2017-06-06 14:51:19 -04001022void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1023 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001024 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001025 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1026 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1027 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001028 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001029 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001030 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001031 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001032 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001033 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001034 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001035 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001036 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001037 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001038 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001039 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001040 }
1041}
1042
Stephen White3b5a3fa2017-06-06 14:51:19 -04001043void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1044 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001045 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001046 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1047 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1048 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001049 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001050 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001051 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001052 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001053 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001054 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001055 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001056 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001057 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001058 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001059 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001060 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001061 }
1062}
1063
Stephen Whited26b4d82018-07-26 10:02:27 -04001064bool top_collinear(Edge* left, Edge* right) {
1065 if (!left || !right) {
1066 return false;
1067 }
1068 return left->fTop->fPoint == right->fTop->fPoint ||
1069 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1070}
1071
1072bool bottom_collinear(Edge* left, Edge* right) {
1073 if (!left || !right) {
1074 return false;
1075 }
1076 return left->fBottom->fPoint == right->fBottom->fPoint ||
1077 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1078}
1079
Stephen White3b5a3fa2017-06-06 14:51:19 -04001080void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001081 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001082 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001083 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001084 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001085 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001086 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001087 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001088 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001089 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001090 } else {
1091 break;
1092 }
ethannicholase9709e82016-01-07 13:34:16 -08001093 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001094 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1095 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1096 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1097 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001098}
1099
Stephen White89042d52018-06-08 12:18:22 -04001100bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001101 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001102 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001103 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001104 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001105 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1106 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001107 Vertex* top;
1108 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001109 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001110 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001111 top = v;
1112 bottom = edge->fTop;
1113 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001114 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001115 top = edge->fBottom;
1116 bottom = v;
1117 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001118 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001119 top = v;
1120 bottom = edge->fBottom;
1121 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001122 }
Stephen White531a48e2018-06-01 09:49:39 -04001123 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001124 insert_edge_below(newEdge, top, c);
1125 insert_edge_above(newEdge, bottom, c);
1126 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001127 return true;
1128}
1129
1130bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1131 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1132 return false;
1133 }
Stephen White1c5fd182018-07-12 15:54:05 -04001134 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1135 return false;
1136 }
Stephen White89042d52018-06-08 12:18:22 -04001137 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1138 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001139 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001140 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1141 }
1142 } else {
1143 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001144 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001145 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1146 }
1147 }
1148 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1149 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001150 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001151 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1152 }
1153 } else {
1154 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001155 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001156 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1157 }
1158 }
1159 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001160}
1161
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001162Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001163 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001164 if (!prev || !next || prev->fPoint == next->fPoint) {
1165 return nullptr;
1166 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001167 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001168 insert_edge_below(edge, edge->fTop, c);
1169 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001170 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001171 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001172 return edge;
1173}
1174
Stephen Whitebf6137e2017-01-04 15:43:26 -05001175void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001176 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001177 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1178 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001179 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001180 if (src->fPartner) {
1181 src->fPartner->fPartner = dst;
1182 }
Stephen White7b376942018-05-22 11:51:32 -04001183 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001184 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001185 }
Stephen White7b376942018-05-22 11:51:32 -04001186 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001187 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001188 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001189 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001190 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001191}
1192
Stephen White95152e12017-12-18 10:52:44 -05001193Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1194 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1195 Vertex* prevV = reference;
1196 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1197 prevV = prevV->fPrev;
1198 }
1199 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1200 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1201 prevV = nextV;
1202 nextV = nextV->fNext;
1203 }
1204 Vertex* v;
1205 if (prevV && coincident(prevV->fPoint, p)) {
1206 v = prevV;
1207 } else if (nextV && coincident(nextV->fPoint, p)) {
1208 v = nextV;
1209 } else {
1210 v = alloc.make<Vertex>(p, alpha);
1211#if LOGGING_ENABLED
1212 if (!prevV) {
1213 v->fID = mesh->fHead->fID - 1.0f;
1214 } else if (!nextV) {
1215 v->fID = mesh->fTail->fID + 1.0f;
1216 } else {
1217 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1218 }
1219#endif
1220 mesh->insert(v, prevV, nextV);
1221 }
1222 return v;
1223}
1224
Stephen White53a02982018-05-30 22:47:46 -04001225// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1226// sort criterion, it may not be possible to split correctly, since there is no point which is
1227// below the top and above the bottom. This function detects that case.
1228bool nearly_flat(Comparator& c, Edge* edge) {
1229 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1230 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001231 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001232}
1233
Stephen Whitee62999f2018-06-05 18:45:07 -04001234SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1235 if (c.sweep_lt(p, min)) {
1236 return min;
1237 } else if (c.sweep_lt(max, p)) {
1238 return max;
1239 } else {
1240 return p;
1241 }
1242}
1243
Stephen Whitec4dbc372019-05-22 10:50:14 -04001244void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1245 Line line1 = edge1->fLine;
1246 Line line2 = edge2->fLine;
1247 line1.normalize();
1248 line2.normalize();
1249 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1250 if (cosAngle > 0.999) {
1251 return;
1252 }
1253 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1254 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1255 SkPoint p;
1256 if (line1.intersect(line2, &p)) {
1257 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1258 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001259 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 -04001260 }
1261}
1262
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001263bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001264 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001265 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001266 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001267 }
Stephen White56158ae2017-01-30 14:31:31 -05001268 SkPoint p;
1269 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001270 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001271 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001272 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001273 Vertex* top = *current;
1274 // If the intersection point is above the current vertex, rewind to the vertex above the
1275 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001276 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001277 top = top->fPrev;
1278 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001279 if (!nearly_flat(c, left)) {
1280 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001281 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001282 if (!nearly_flat(c, right)) {
1283 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001284 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001285 if (p == left->fTop->fPoint) {
1286 v = left->fTop;
1287 } else if (p == left->fBottom->fPoint) {
1288 v = left->fBottom;
1289 } else if (p == right->fTop->fPoint) {
1290 v = right->fTop;
1291 } else if (p == right->fBottom->fPoint) {
1292 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001293 } else {
Stephen White95152e12017-12-18 10:52:44 -05001294 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001295 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001296 v->fSynthetic = true;
1297 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001298 }
ethannicholase9709e82016-01-07 13:34:16 -08001299 }
Stephen White0cb31672017-06-08 14:41:01 -04001300 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001301 split_edge(left, v, activeEdges, current, c, alloc);
1302 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001303 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001304 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001305 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001306 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001307}
1308
Stephen White3a9aab92017-03-07 14:07:18 -05001309void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
1310 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1311 SkASSERT(contour->fHead);
1312 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001313 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001314 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001315 }
Stephen White3a9aab92017-03-07 14:07:18 -05001316 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001317 if (approximate) {
1318 round(&v->fPoint);
1319 }
Stephen White3a9aab92017-03-07 14:07:18 -05001320 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001321 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001322 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001323 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001324 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001325 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001326 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001327 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001328 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001329 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001330 contour->remove(v);
1331 } else {
1332 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001333 }
Stephen White3a9aab92017-03-07 14:07:18 -05001334 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001335 }
1336 }
1337}
1338
Stephen Whitee260c462017-12-19 18:09:54 -05001339bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001340 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001341 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001342 }
Stephen Whitee260c462017-12-19 18:09:54 -05001343 bool merged = false;
1344 for (Vertex* v = mesh->fHead->fNext; v;) {
1345 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001346 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1347 v->fPoint = v->fPrev->fPoint;
1348 }
1349 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001350 merge_vertices(v, v->fPrev, mesh, c, alloc);
1351 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001352 }
Stephen Whitee260c462017-12-19 18:09:54 -05001353 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001354 }
Stephen Whitee260c462017-12-19 18:09:54 -05001355 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001356}
1357
1358// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1359
Stephen White3a9aab92017-03-07 14:07:18 -05001360void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001361 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001362 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1363 Vertex* prev = contour->fTail;
1364 for (Vertex* v = contour->fHead; v;) {
1365 Vertex* next = v->fNext;
1366 connect(prev, v, Edge::Type::kInner, c, alloc);
1367 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001368 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001369 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001370 }
1371 }
ethannicholase9709e82016-01-07 13:34:16 -08001372}
1373
Stephen Whitee260c462017-12-19 18:09:54 -05001374void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1375 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001376 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001377 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1378 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1379 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1380 // number.
1381 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1382 inner->fPartner = outer->fPartner = nullptr;
1383 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001384 }
1385 }
1386}
1387
1388template <CompareFunc sweep_lt>
1389void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1390 Vertex* a = front->fHead;
1391 Vertex* b = back->fHead;
1392 while (a && b) {
1393 if (sweep_lt(a->fPoint, b->fPoint)) {
1394 front->remove(a);
1395 result->append(a);
1396 a = front->fHead;
1397 } else {
1398 back->remove(b);
1399 result->append(b);
1400 b = back->fHead;
1401 }
1402 }
1403 result->append(*front);
1404 result->append(*back);
1405}
1406
1407void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1408 if (c.fDirection == Comparator::Direction::kHorizontal) {
1409 sorted_merge<sweep_lt_horiz>(front, back, result);
1410 } else {
1411 sorted_merge<sweep_lt_vert>(front, back, result);
1412 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001413#if LOGGING_ENABLED
1414 float id = 0.0f;
1415 for (Vertex* v = result->fHead; v; v = v->fNext) {
1416 v->fID = id++;
1417 }
1418#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001419}
1420
ethannicholase9709e82016-01-07 13:34:16 -08001421// Stage 3: sort the vertices by increasing sweep direction.
1422
Stephen White16a40cb2017-02-23 11:10:01 -05001423template <CompareFunc sweep_lt>
1424void merge_sort(VertexList* vertices) {
1425 Vertex* slow = vertices->fHead;
1426 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001427 return;
1428 }
Stephen White16a40cb2017-02-23 11:10:01 -05001429 Vertex* fast = slow->fNext;
1430 if (!fast) {
1431 return;
1432 }
1433 do {
1434 fast = fast->fNext;
1435 if (fast) {
1436 fast = fast->fNext;
1437 slow = slow->fNext;
1438 }
1439 } while (fast);
1440 VertexList front(vertices->fHead, slow);
1441 VertexList back(slow->fNext, vertices->fTail);
1442 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001443
Stephen White16a40cb2017-02-23 11:10:01 -05001444 merge_sort<sweep_lt>(&front);
1445 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001446
Stephen White16a40cb2017-02-23 11:10:01 -05001447 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001448 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001449}
1450
Stephen White95152e12017-12-18 10:52:44 -05001451void dump_mesh(const VertexList& mesh) {
1452#if LOGGING_ENABLED
1453 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001454 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 -05001455 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001456 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1457 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001458 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001459 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001460 }
1461 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001462 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001463 }
1464 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001465 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001466 }
1467 }
1468#endif
1469}
1470
Stephen Whitec4dbc372019-05-22 10:50:14 -04001471void dump_skel(const SSEdgeList& ssEdges) {
1472#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001473 for (SSEdge* edge : ssEdges) {
1474 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001475 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001476 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001477 edge->fNext->fVertex->fID);
1478 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001479 TESS_LOG(" (original %g -> %g)\n",
1480 edge->fEdge->fTop->fID,
1481 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001482 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001483 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001484 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001485 }
1486 }
1487#endif
1488}
1489
Stephen White89042d52018-06-08 12:18:22 -04001490#ifdef SK_DEBUG
1491void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1492 if (!left || !right) {
1493 return;
1494 }
1495 if (left->fTop == right->fTop) {
1496 SkASSERT(left->isLeftOf(right->fBottom));
1497 SkASSERT(right->isRightOf(left->fBottom));
1498 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1499 SkASSERT(left->isLeftOf(right->fTop));
1500 } else {
1501 SkASSERT(right->isRightOf(left->fTop));
1502 }
1503 if (left->fBottom == right->fBottom) {
1504 SkASSERT(left->isLeftOf(right->fTop));
1505 SkASSERT(right->isRightOf(left->fTop));
1506 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1507 SkASSERT(left->isLeftOf(right->fBottom));
1508 } else {
1509 SkASSERT(right->isRightOf(left->fBottom));
1510 }
1511}
1512
1513void validate_edge_list(EdgeList* edges, Comparator& c) {
1514 Edge* left = edges->fHead;
1515 if (!left) {
1516 return;
1517 }
1518 for (Edge* right = left->fRight; right; right = right->fRight) {
1519 validate_edge_pair(left, right, c);
1520 left = right;
1521 }
1522}
1523#endif
1524
ethannicholase9709e82016-01-07 13:34:16 -08001525// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1526
Stephen Whitec4dbc372019-05-22 10:50:14 -04001527bool connected(Vertex* v) {
1528 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1529}
1530
Stephen Whitee260c462017-12-19 18:09:54 -05001531bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001532 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001533 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001534 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001535 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001536 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001537 continue;
1538 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001539 Edge* leftEnclosingEdge;
1540 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001541 bool restartChecks;
1542 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001543 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1544 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001545 restartChecks = false;
1546 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001547 v->fLeftEnclosingEdge = leftEnclosingEdge;
1548 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001549 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001550 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001551 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001552 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001553 restartChecks = true;
1554 break;
1555 }
Stephen White0cb31672017-06-08 14:41:01 -04001556 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001557 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001558 restartChecks = true;
1559 break;
1560 }
1561 }
1562 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001563 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001564 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001565 restartChecks = true;
1566 }
1567
1568 }
Stephen Whitee260c462017-12-19 18:09:54 -05001569 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001570 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001571#ifdef SK_DEBUG
1572 validate_edge_list(&activeEdges, c);
1573#endif
ethannicholase9709e82016-01-07 13:34:16 -08001574 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1575 remove_edge(e, &activeEdges);
1576 }
1577 Edge* leftEdge = leftEnclosingEdge;
1578 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1579 insert_edge(e, leftEdge, &activeEdges);
1580 leftEdge = e;
1581 }
ethannicholase9709e82016-01-07 13:34:16 -08001582 }
Stephen Whitee260c462017-12-19 18:09:54 -05001583 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1584 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001585}
1586
1587// Stage 5: Tessellate the simplified mesh into monotone polygons.
1588
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001589Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001590 TESS_LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001591 EdgeList activeEdges;
1592 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001593 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001594 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001595 continue;
1596 }
1597#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001598 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 -08001599#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001600 Edge* leftEnclosingEdge;
1601 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001602 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001603 Poly* leftPoly;
1604 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001605 if (v->fFirstEdgeAbove) {
1606 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1607 rightPoly = v->fLastEdgeAbove->fRightPoly;
1608 } else {
1609 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1610 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1611 }
1612#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001613 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001614 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001615 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1616 e->fTop->fID, e->fBottom->fID,
1617 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1618 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001619 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001620 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001621 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001622 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1623 e->fTop->fID, e->fBottom->fID,
1624 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1625 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001626 }
1627#endif
1628 if (v->fFirstEdgeAbove) {
1629 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001630 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001631 }
1632 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001633 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001634 }
1635 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001636 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001637 remove_edge(e, &activeEdges);
1638 if (e->fRightPoly) {
1639 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001640 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001641 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001642 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001643 }
1644 }
1645 remove_edge(v->fLastEdgeAbove, &activeEdges);
1646 if (!v->fFirstEdgeBelow) {
1647 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1648 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1649 rightPoly->fPartner = leftPoly;
1650 leftPoly->fPartner = rightPoly;
1651 }
1652 }
1653 }
1654 if (v->fFirstEdgeBelow) {
1655 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001656 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001657 if (leftPoly == rightPoly) {
1658 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1659 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1660 leftPoly->fWinding, alloc);
1661 leftEnclosingEdge->fRightPoly = leftPoly;
1662 } else {
1663 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1664 rightPoly->fWinding, alloc);
1665 rightEnclosingEdge->fLeftPoly = rightPoly;
1666 }
ethannicholase9709e82016-01-07 13:34:16 -08001667 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001668 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001669 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1670 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001671 }
1672 }
1673 Edge* leftEdge = v->fFirstEdgeBelow;
1674 leftEdge->fLeftPoly = leftPoly;
1675 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1676 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1677 rightEdge = rightEdge->fNextEdgeBelow) {
1678 insert_edge(rightEdge, leftEdge, &activeEdges);
1679 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1680 winding += leftEdge->fWinding;
1681 if (winding != 0) {
1682 Poly* poly = new_poly(&polys, v, winding, alloc);
1683 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1684 }
1685 leftEdge = rightEdge;
1686 }
1687 v->fLastEdgeBelow->fRightPoly = rightPoly;
1688 }
1689#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001690 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001691 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001692 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1693 e->fTop->fID, e->fBottom->fID,
1694 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1695 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001696 }
1697#endif
1698 }
1699 return polys;
1700}
1701
Stephen Whitebf6137e2017-01-04 15:43:26 -05001702void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001703 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001704 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001705 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001706 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001707 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001708 continue;
1709 }
1710 Edge* leftEnclosingEdge;
1711 Edge* rightEnclosingEdge;
1712 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1713 bool prevFilled = leftEnclosingEdge &&
1714 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1715 for (Edge* e = v->fFirstEdgeAbove; e;) {
1716 Edge* next = e->fNextEdgeAbove;
1717 remove_edge(e, &activeEdges);
1718 bool filled = apply_fill_type(fillType, e->fWinding);
1719 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001720 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001721 }
Stephen White49789062017-02-21 10:35:49 -05001722 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001723 e = next;
1724 }
Stephen White49789062017-02-21 10:35:49 -05001725 Edge* prev = leftEnclosingEdge;
1726 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1727 if (prev) {
1728 e->fWinding += prev->fWinding;
1729 }
1730 insert_edge(e, prev, &activeEdges);
1731 prev = e;
1732 }
senorblancof57372d2016-08-31 10:36:19 -07001733 }
senorblancof57372d2016-08-31 10:36:19 -07001734}
1735
Stephen White66412122017-03-01 11:48:27 -05001736// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001737void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001738 normal->set(SkDoubleToScalar(e->fLine.fA),
1739 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001740}
1741
1742// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1743// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1744// invert on stroking.
1745
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001746void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001747 Edge* prevEdge = boundary->fTail;
1748 SkVector prevNormal;
1749 get_edge_normal(prevEdge, &prevNormal);
1750 for (Edge* e = boundary->fHead; e != nullptr;) {
1751 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1752 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001753 double distPrev = e->dist(prev->fPoint);
1754 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001755 SkVector normal;
1756 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001757 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001758 if (prev == next) {
1759 remove_edge(prevEdge, boundary);
1760 remove_edge(e, boundary);
1761 prevEdge = boundary->fTail;
1762 e = boundary->fHead;
1763 if (prevEdge) {
1764 get_edge_normal(prevEdge, &prevNormal);
1765 }
1766 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001767 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001768 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001769 if (prev->fPoint != next->fPoint) {
1770 join->fLine.normalize();
1771 join->fLine = join->fLine * join->fWinding;
1772 }
senorblancof57372d2016-08-31 10:36:19 -07001773 insert_edge(join, e, boundary);
1774 remove_edge(prevEdge, boundary);
1775 remove_edge(e, boundary);
1776 if (join->fLeft && join->fRight) {
1777 prevEdge = join->fLeft;
1778 e = join;
1779 } else {
1780 prevEdge = boundary->fTail;
1781 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1782 }
1783 get_edge_normal(prevEdge, &prevNormal);
1784 } else {
1785 prevEdge = e;
1786 prevNormal = normal;
1787 e = e->fRight;
1788 }
1789 }
1790}
1791
Stephen Whitec4dbc372019-05-22 10:50:14 -04001792void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1793 if (v == dest) {
1794 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001795 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001796 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001797 if (v->fSynthetic) {
1798 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1799 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001800 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1801 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001802 v->fPartner->fPartner = dest;
1803 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001804 }
1805}
1806
Stephen Whitec4dbc372019-05-22 10:50:14 -04001807void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1808 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001809 return;
1810 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001811 Vertex* prev = fEdge->fPrev->fVertex;
1812 Vertex* next = fEdge->fNext->fVertex;
1813 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1814 SSEdge* nextEdge = fEdge->fNext->fNext;
1815 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1816 return;
Stephen White77169c82018-06-05 09:15:59 -04001817 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001818 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1819 dest->fSynthetic = true;
1820 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001821 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1822 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1823 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001824 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001825
Stephen Whitec4dbc372019-05-22 10:50:14 -04001826 ss_connect(prev, dest, c, alloc);
1827 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001828
Stephen Whitec4dbc372019-05-22 10:50:14 -04001829 prevEdge->fNext = nextEdge->fPrev = ssv;
1830 ssv->fPrev = prevEdge;
1831 ssv->fNext = nextEdge;
1832 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1833 return;
1834 }
1835 if (prevEdge->fEvent) {
1836 prevEdge->fEvent->fEdge = nullptr;
1837 }
1838 if (nextEdge->fEvent) {
1839 nextEdge->fEvent->fEdge = nullptr;
1840 }
1841 if (prevEdge->fPrev == nextEdge->fNext) {
1842 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1843 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1844 } else {
1845 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1846 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1847 if (dest->fPartner) {
1848 create_event(prevEdge, events, alloc);
1849 create_event(nextEdge, events, alloc);
1850 } else {
1851 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1852 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1853 }
1854 }
Stephen Whitee260c462017-12-19 18:09:54 -05001855}
1856
1857bool is_overlap_edge(Edge* e) {
1858 if (e->fType == Edge::Type::kOuter) {
1859 return e->fWinding != 0 && e->fWinding != 1;
1860 } else if (e->fType == Edge::Type::kInner) {
1861 return e->fWinding != 0 && e->fWinding != -2;
1862 } else {
1863 return false;
1864 }
1865}
1866
1867// This is a stripped-down version of tessellate() which computes edges which
1868// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001869bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1870 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001871 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001872 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001873 EventList events(comp);
1874 SSVertexMap ssVertices;
1875 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001876 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001877 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001878 continue;
1879 }
1880 Edge* leftEnclosingEdge;
1881 Edge* rightEnclosingEdge;
1882 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001883 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001884 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1885 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001886 bool leftOverlap = prev && is_overlap_edge(prev);
1887 bool rightOverlap = is_overlap_edge(e);
1888 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1889 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001890 if (prev) {
1891 e->fWinding -= prev->fWinding;
1892 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001893 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001894 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1895 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001896 disconnect(e);
1897 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001898 TESS_LOG("found overlap edge %g -> %g%s\n",
1899 e->fTop->fID, e->fBottom->fID,
1900 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001901 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1902 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1903 SSVertex* ssPrev = ssVertices[prevVertex];
1904 if (!ssPrev) {
1905 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1906 }
1907 SSVertex* ssNext = ssVertices[nextVertex];
1908 if (!ssNext) {
1909 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1910 }
1911 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1912 ssEdges.push_back(ssEdge);
1913// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1914 ssPrev->fNext = ssNext->fPrev = ssEdge;
1915 create_event(ssEdge, &events, alloc);
1916 if (!isOuterBoundary) {
1917 disconnect(e);
1918 }
1919 }
1920 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001921 }
1922 Edge* prev = leftEnclosingEdge;
1923 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1924 if (prev) {
1925 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001926 }
1927 insert_edge(e, prev, &activeEdges);
1928 prev = e;
1929 }
1930 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001931 bool complex = events.size() > 0;
1932
Brian Salomon120e7d62019-09-11 10:29:22 -04001933 TESS_LOG("\ncollapsing overlap regions\n");
1934 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001935 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001936 while (events.size() > 0) {
1937 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001938 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001939 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001940 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001941 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001942 dump_skel(ssEdges);
1943 for (SSEdge* edge : ssEdges) {
1944 if (Edge* e = edge->fEdge) {
1945 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1946 }
1947 }
1948 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001949}
1950
1951bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1952 if (!prev || !next) {
1953 return true;
1954 }
1955 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1956 return winding != origEdge->fWinding;
1957}
Stephen White92eba8a2017-02-06 09:50:27 -05001958
senorblancof57372d2016-08-31 10:36:19 -07001959// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1960// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1961// new antialiased mesh from those vertices.
1962
Stephen Whitee260c462017-12-19 18:09:54 -05001963void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1964 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001965 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001966 // A boundary with fewer than 3 edges is degenerate.
1967 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1968 return;
1969 }
1970 Edge* prevEdge = boundary->fTail;
1971 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1972 SkVector prevNormal;
1973 get_edge_normal(prevEdge, &prevNormal);
1974 double radius = 0.5;
1975 Line prevInner(prevEdge->fLine);
1976 prevInner.fC -= radius;
1977 Line prevOuter(prevEdge->fLine);
1978 prevOuter.fC += radius;
1979 VertexList innerVertices;
1980 VertexList outerVertices;
1981 bool innerInversion = true;
1982 bool outerInversion = true;
1983 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1984 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1985 SkVector normal;
1986 get_edge_normal(e, &normal);
1987 Line inner(e->fLine);
1988 inner.fC -= radius;
1989 Line outer(e->fLine);
1990 outer.fC += radius;
1991 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04001992 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001993 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1994 prevOuter.intersect(outer, &outerPoint)) {
1995 float cosAngle = normal.dot(prevNormal);
1996 if (cosAngle < -kCosMiterAngle) {
1997 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1998
1999 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2000 Line bisector(innerPoint, outerPoint);
2001 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2002 if (tangent.fA == 0 && tangent.fB == 0) {
2003 continue;
2004 }
2005 tangent.normalize();
2006 Line innerTangent(tangent);
2007 Line outerTangent(tangent);
2008 innerTangent.fC -= 0.5;
2009 outerTangent.fC += 0.5;
2010 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2011 if (prevNormal.cross(normal) > 0) {
2012 // Miter inner points
2013 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2014 !innerTangent.intersect(inner, &innerPoint2) ||
2015 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002016 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002017 }
2018 Line prevTangent(prevV->fPoint,
2019 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2020 Line nextTangent(nextV->fPoint,
2021 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002022 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002023 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002024 }
2025 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002026 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002027 }
2028 outerPoint1 = outerPoint2 = outerPoint;
2029 } else {
2030 // Miter outer points
2031 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2032 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002033 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002034 }
2035 Line prevTangent(prevV->fPoint,
2036 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2037 Line nextTangent(nextV->fPoint,
2038 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002039 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002040 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002041 }
2042 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002043 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002044 }
2045 innerPoint1 = innerPoint2 = innerPoint;
2046 }
Stephen Whiteea495232018-04-03 11:28:15 -04002047 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2048 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2049 continue;
2050 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002051 TESS_LOG("inner (%g, %g), (%g, %g), ",
2052 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2053 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2054 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002055 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2056 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2057 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2058 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2059 innerVertex1->fPartner = outerVertex1;
2060 innerVertex2->fPartner = outerVertex2;
2061 outerVertex1->fPartner = innerVertex1;
2062 outerVertex2->fPartner = innerVertex2;
2063 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2064 innerInversion = false;
2065 }
2066 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2067 outerInversion = false;
2068 }
2069 innerVertices.append(innerVertex1);
2070 innerVertices.append(innerVertex2);
2071 outerVertices.append(outerVertex1);
2072 outerVertices.append(outerVertex2);
2073 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002074 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2075 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002076 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2077 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2078 innerVertex->fPartner = outerVertex;
2079 outerVertex->fPartner = innerVertex;
2080 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2081 innerInversion = false;
2082 }
2083 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2084 outerInversion = false;
2085 }
2086 innerVertices.append(innerVertex);
2087 outerVertices.append(outerVertex);
2088 }
2089 }
2090 prevInner = inner;
2091 prevOuter = outer;
2092 prevV = v;
2093 prevEdge = e;
2094 prevNormal = normal;
2095 }
2096 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2097 innerInversion = false;
2098 }
2099 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2100 outerInversion = false;
2101 }
2102 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2103 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2104 // interior inverts).
2105 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2106 // so it will be detected during collapse_overlap_regions().
2107 int innerWinding = innerInversion ? 2 : -2;
2108 int outerWinding = outerInversion ? -1 : 1;
2109 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2110 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2111 }
2112 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2113 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2114 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2115 }
2116 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2117 innerMesh->append(innerVertices);
2118 outerMesh->append(outerVertices);
2119}
senorblancof57372d2016-08-31 10:36:19 -07002120
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002121void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002122 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002123 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002124 Vertex* start = down ? e->fTop : e->fBottom;
2125 do {
senorblancof57372d2016-08-31 10:36:19 -07002126 e->fWinding = down ? 1 : -1;
2127 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002128 e->fLine.normalize();
2129 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002130 boundary->append(e);
2131 if (down) {
2132 // Find outgoing edge, in clockwise order.
2133 if ((next = e->fNextEdgeAbove)) {
2134 down = false;
2135 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2136 down = true;
2137 } else if ((next = e->fPrevEdgeAbove)) {
2138 down = false;
2139 }
2140 } else {
2141 // Find outgoing edge, in counter-clockwise order.
2142 if ((next = e->fPrevEdgeBelow)) {
2143 down = true;
2144 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2145 down = false;
2146 } else if ((next = e->fNextEdgeBelow)) {
2147 down = true;
2148 }
2149 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002150 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002151 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002152 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002153}
2154
Stephen White5ad721e2017-02-23 16:50:47 -05002155// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002156
Stephen Whitebda29c02017-03-13 15:10:13 -04002157void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
2158 VertexList* outerVertices, SkPath::FillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002159 Comparator& c, SkArenaAlloc& alloc) {
2160 remove_non_boundary_edges(inMesh, fillType, alloc);
2161 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002162 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002163 EdgeList boundary;
2164 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2165 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002166 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002167 }
2168 }
senorblancof57372d2016-08-31 10:36:19 -07002169}
2170
Stephen Whitebda29c02017-03-13 15:10:13 -04002171// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002172
Stephen White3a9aab92017-03-07 14:07:18 -05002173void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002174 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002175#if LOGGING_ENABLED
2176 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002177 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002178 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002179 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002180 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002181 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002182 }
2183 }
2184#endif
senorblancof57372d2016-08-31 10:36:19 -07002185 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002186 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002187}
2188
Stephen Whitebda29c02017-03-13 15:10:13 -04002189void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002190 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002191 return;
ethannicholase9709e82016-01-07 13:34:16 -08002192 }
2193
2194 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002195 if (c.fDirection == Comparator::Direction::kHorizontal) {
2196 merge_sort<sweep_lt_horiz>(vertices);
2197 } else {
2198 merge_sort<sweep_lt_vert>(vertices);
2199 }
ethannicholase9709e82016-01-07 13:34:16 -08002200#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002201 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002202 static float gID = 0.0f;
2203 v->fID = gID++;
2204 }
2205#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002206}
2207
Stephen White3a9aab92017-03-07 14:07:18 -05002208Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType,
Stephen Whitebda29c02017-03-13 15:10:13 -04002209 const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002210 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002211 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2212 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002213 VertexList mesh;
2214 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002215 sort_mesh(&mesh, c, alloc);
2216 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002217 simplify(&mesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002218 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002219 dump_mesh(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002220 if (antialias) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002221 VertexList innerMesh;
2222 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2223 sort_mesh(&innerMesh, c, alloc);
2224 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002225 merge_coincident_vertices(&innerMesh, c, alloc);
2226 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2227 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2228 was_complex = simplify(outerMesh, c, alloc) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002229 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002230 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002231 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002232 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002233 EventComparator eventLT(EventComparator::Op::kLessThan);
2234 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2235 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2236 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002237 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002238 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002239 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002240 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002241 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002242 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002243 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002244 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002245 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002246 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2247 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002248 simplify(&aaMesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002249 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002250 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002251 outerMesh->fHead = outerMesh->fTail = nullptr;
2252 return tessellate(aaMesh, alloc);
2253 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002254 TESS_LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002255 return tessellate(innerMesh, alloc);
2256 }
Stephen White49789062017-02-21 10:35:49 -05002257 } else {
2258 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002259 }
senorblancof57372d2016-08-31 10:36:19 -07002260}
2261
2262// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Brian Osman0995fd52019-01-09 09:52:25 -05002263void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, bool emitCoverage, void* data) {
senorblancof57372d2016-08-31 10:36:19 -07002264 for (Poly* poly = polys; poly; poly = poly->fNext) {
2265 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002266 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002267 }
2268 }
2269 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002270}
2271
halcanary9d524f22016-03-29 09:03:52 -07002272Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen Whitebda29c02017-03-13 15:10:13 -04002273 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
2274 VertexList* outerMesh) {
ethannicholase9709e82016-01-07 13:34:16 -08002275 SkPath::FillType fillType = path.getFillType();
2276 if (SkPath::IsInverseFillType(fillType)) {
2277 contourCnt++;
2278 }
Stephen White3a9aab92017-03-07 14:07:18 -05002279 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002280
2281 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07002282 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Stephen Whitebda29c02017-03-13 15:10:13 -04002283 antialias, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002284}
2285
Stephen White11f65e02017-02-16 19:00:39 -05002286int get_contour_count(const SkPath& path, SkScalar tolerance) {
2287 int contourCnt;
2288 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002289 if (maxPts <= 0) {
Stephen White11f65e02017-02-16 19:00:39 -05002290 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002291 }
Stephen White11f65e02017-02-16 19:00:39 -05002292 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002293}
2294
Greg Danield5b45932018-06-07 13:15:10 -04002295int64_t count_points(Poly* polys, SkPath::FillType fillType) {
2296 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002297 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002298 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002299 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2300 }
2301 }
2302 return count;
2303}
2304
Greg Danield5b45932018-06-07 13:15:10 -04002305int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2306 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002307 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2308 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2309 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2310 }
2311 }
2312 return count;
2313}
2314
Brian Osman0995fd52019-01-09 09:52:25 -05002315void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002316 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2317 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2318 Vertex* v0 = e->fTop;
2319 Vertex* v1 = e->fBottom;
2320 Vertex* v2 = e->fBottom->fPartner;
2321 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002322 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2323 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002324 }
2325 }
2326 return data;
2327}
2328
ethannicholase9709e82016-01-07 13:34:16 -08002329} // namespace
2330
2331namespace GrTessellator {
2332
2333// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2334
halcanary9d524f22016-03-29 09:03:52 -07002335int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Brian Osman0995fd52019-01-09 09:52:25 -05002336 VertexAllocator* vertexAllocator, bool antialias, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002337 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002338 if (contourCnt <= 0) {
2339 *isLinear = true;
2340 return 0;
2341 }
Stephen White11f65e02017-02-16 19:00:39 -05002342 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002343 VertexList outerMesh;
senorblancof57372d2016-08-31 10:36:19 -07002344 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
Stephen Whitebda29c02017-03-13 15:10:13 -04002345 isLinear, &outerMesh);
senorblanco7ab96e92016-10-12 06:47:44 -07002346 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002347 int64_t count64 = count_points(polys, fillType);
Stephen Whitebda29c02017-03-13 15:10:13 -04002348 if (antialias) {
Greg Danield5b45932018-06-07 13:15:10 -04002349 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002350 }
Greg Danield5b45932018-06-07 13:15:10 -04002351 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002352 return 0;
2353 }
Greg Danield5b45932018-06-07 13:15:10 -04002354 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002355
senorblancof57372d2016-08-31 10:36:19 -07002356 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08002357 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002358 SkDebugf("Could not allocate vertices\n");
2359 return 0;
2360 }
senorblancof57372d2016-08-31 10:36:19 -07002361
Brian Salomon120e7d62019-09-11 10:29:22 -04002362 TESS_LOG("emitting %d verts\n", count);
Brian Osman80879d42019-01-07 16:15:27 -05002363 void* end = polys_to_triangles(polys, fillType, antialias, verts);
2364 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002365
senorblancof57372d2016-08-31 10:36:19 -07002366 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
2367 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08002368 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002369 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002370 return actualCount;
2371}
2372
halcanary9d524f22016-03-29 09:03:52 -07002373int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002374 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002375 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002376 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002377 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002378 return 0;
2379 }
Stephen White11f65e02017-02-16 19:00:39 -05002380 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002381 bool isLinear;
Stephen Whitebda29c02017-03-13 15:10:13 -04002382 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
2383 nullptr);
ethannicholase9709e82016-01-07 13:34:16 -08002384 SkPath::FillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002385 int64_t count64 = count_points(polys, fillType);
2386 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002387 *verts = nullptr;
2388 return 0;
2389 }
Greg Danield5b45932018-06-07 13:15:10 -04002390 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002391
2392 *verts = new GrTessellator::WindingVertex[count];
2393 GrTessellator::WindingVertex* vertsEnd = *verts;
2394 SkPoint* points = new SkPoint[count];
2395 SkPoint* pointsEnd = points;
2396 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002397 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002398 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002399 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002400 while (start != pointsEnd) {
2401 vertsEnd->fPos = *start;
2402 vertsEnd->fWinding = poly->fWinding;
2403 ++start;
2404 ++vertsEnd;
2405 }
2406 }
2407 }
2408 int actualCount = static_cast<int>(vertsEnd - *verts);
2409 SkASSERT(actualCount <= count);
2410 SkASSERT(pointsEnd - points == actualCount);
2411 delete[] points;
2412 return actualCount;
2413}
2414
2415} // namespace