blob: 40b73c94d312be25f204fe4d489a1f46c90ef3a0 [file] [log] [blame]
ethannicholase9709e82016-01-07 13:34:16 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrTessellator.h"
ethannicholase9709e82016-01-07 13:34:16 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrDefaultGeoProcFactory.h"
Chris Daltond081dce2020-01-23 12:09:04 -070011#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040013#include "src/gpu/geometry/GrPathUtils.h"
ethannicholase9709e82016-01-07 13:34:16 -080014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPath.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040016#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkGeometry.h"
18#include "src/core/SkPointPriv.h"
ethannicholase9709e82016-01-07 13:34:16 -080019
Stephen White94b7e542018-01-04 14:01:10 -050020#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040021#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040022#include <queue>
23#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040024#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080025
26/*
senorblancof57372d2016-08-31 10:36:19 -070027 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080028 *
29 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
30 * 2) Build a mesh of edges connecting the vertices (build_edges()).
31 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
32 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
33 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
34 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
35 *
senorblancof57372d2016-08-31 10:36:19 -070036 * For screenspace antialiasing, the algorithm is modified as follows:
37 *
38 * Run steps 1-5 above to produce polygons.
39 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040040 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070041 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
42 * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
Stephen Whitebda29c02017-03-13 15:10:13 -040043 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070044 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
45 *
ethannicholase9709e82016-01-07 13:34:16 -080046 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
47 * of vertices (and the necessity of inserting new vertices on intersection).
48 *
Stephen Whitebda29c02017-03-13 15:10:13 -040049 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080050 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
51 * left-to-right based on the point where both edges are active (when both top vertices
52 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
53 * (shared), it's sorted based on the last point where both edges are active, so the
54 * "upper" bottom vertex.
55 *
56 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
57 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
58 * not exact and may violate the mesh topology or active edge list ordering. We
59 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040060 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080061 *
62 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
63 * neighbouring edges at the top or bottom vertex. This is handled by merging the
64 * edges (merge_collinear_edges()).
65 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen Whiteb67b2352019-06-01 13:07:27 -040066 * active edge list. This is handled during merging or splitting by rewind()ing the
67 * active edge list to the vertex before potential violations occur.
ethannicholase9709e82016-01-07 13:34:16 -080068 *
69 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
70 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
71 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
72 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
73 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
74 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
75 * linked list implementation. With the latter, all removals are O(1), and most insertions
76 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
77 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
78 * frequent. There may be other data structures worth investigating, however.
79 *
80 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
81 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
82 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
83 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
84 * that the "left" and "right" orientation in the code remains correct (edges to the left are
85 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
86 * degrees counterclockwise, rather that transposing.
87 */
88
89#define LOGGING_ENABLED 0
90
91#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -040092#define TESS_LOG printf
ethannicholase9709e82016-01-07 13:34:16 -080093#else
Brian Salomon120e7d62019-09-11 10:29:22 -040094#define TESS_LOG(...)
ethannicholase9709e82016-01-07 13:34:16 -080095#endif
96
ethannicholase9709e82016-01-07 13:34:16 -080097namespace {
98
Stephen White11f65e02017-02-16 19:00:39 -050099const int kArenaChunkSize = 16 * 1024;
Stephen Whitee260c462017-12-19 18:09:54 -0500100const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
Stephen White11f65e02017-02-16 19:00:39 -0500101
ethannicholase9709e82016-01-07 13:34:16 -0800102struct Vertex;
103struct Edge;
Stephen Whitee260c462017-12-19 18:09:54 -0500104struct Event;
ethannicholase9709e82016-01-07 13:34:16 -0800105struct Poly;
106
107template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800108void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800109 t->*Prev = prev;
110 t->*Next = next;
111 if (prev) {
112 prev->*Next = t;
113 } else if (head) {
114 *head = t;
115 }
116 if (next) {
117 next->*Prev = t;
118 } else if (tail) {
119 *tail = t;
120 }
121}
122
123template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800124void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800125 if (t->*Prev) {
126 t->*Prev->*Next = t->*Next;
127 } else if (head) {
128 *head = t->*Next;
129 }
130 if (t->*Next) {
131 t->*Next->*Prev = t->*Prev;
132 } else if (tail) {
133 *tail = t->*Prev;
134 }
135 t->*Prev = t->*Next = nullptr;
136}
137
138/**
139 * Vertices are used in three ways: first, the path contours are converted into a
140 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
141 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
142 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
143 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
144 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
145 * an individual Vertex from the path mesh may belong to multiple
146 * MonotonePolys, so the original Vertices cannot be re-used.
147 */
148
149struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700150 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800151 : fPoint(point), fPrev(nullptr), fNext(nullptr)
152 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
153 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -0400154 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -0400155 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -0700156 , fAlpha(alpha)
Stephen Whitec4dbc372019-05-22 10:50:14 -0400157 , fSynthetic(false)
ethannicholase9709e82016-01-07 13:34:16 -0800158#if LOGGING_ENABLED
159 , fID (-1.0f)
160#endif
161 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400162 SkPoint fPoint; // Vertex position
163 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
164 Vertex* fNext; // "
165 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
166 Edge* fLastEdgeAbove; // "
167 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
168 Edge* fLastEdgeBelow; // "
169 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
170 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
171 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700172 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400173 bool fSynthetic; // Is this a synthetic vertex?
ethannicholase9709e82016-01-07 13:34:16 -0800174#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400175 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800176#endif
177};
178
179/***************************************************************************************/
180
181typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
182
ethannicholase9709e82016-01-07 13:34:16 -0800183bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500184 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800185}
186
187bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500188 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800189}
190
Stephen White16a40cb2017-02-23 11:10:01 -0500191struct Comparator {
192 enum class Direction { kVertical, kHorizontal };
193 Comparator(Direction direction) : fDirection(direction) {}
194 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
195 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
196 }
Stephen White16a40cb2017-02-23 11:10:01 -0500197 Direction fDirection;
198};
199
Brian Osman0995fd52019-01-09 09:52:25 -0500200inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500201 GrVertexWriter verts{data};
202 verts.write(v->fPoint);
203
Brian Osman80879d42019-01-07 16:15:27 -0500204 if (emitCoverage) {
205 verts.write(GrNormalizeByteToFloat(v->fAlpha));
206 }
Brian Osman0995fd52019-01-09 09:52:25 -0500207
Brian Osmanf9aabff2018-11-13 16:11:38 -0500208 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800209}
210
Brian Osman0995fd52019-01-09 09:52:25 -0500211void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400212 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
213 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
214 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700215#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500216 data = emit_vertex(v0, emitCoverage, data);
217 data = emit_vertex(v1, emitCoverage, data);
218 data = emit_vertex(v1, emitCoverage, data);
219 data = emit_vertex(v2, emitCoverage, data);
220 data = emit_vertex(v2, emitCoverage, data);
221 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800222#else
Brian Osman0995fd52019-01-09 09:52:25 -0500223 data = emit_vertex(v0, emitCoverage, data);
224 data = emit_vertex(v1, emitCoverage, data);
225 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800226#endif
227 return data;
228}
229
senorblancoe6eaa322016-03-08 09:06:44 -0800230struct VertexList {
231 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500232 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800233 Vertex* fHead;
234 Vertex* fTail;
235 void insert(Vertex* v, Vertex* prev, Vertex* next) {
236 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
237 }
238 void append(Vertex* v) {
239 insert(v, fTail, nullptr);
240 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400241 void append(const VertexList& list) {
242 if (!list.fHead) {
243 return;
244 }
245 if (fTail) {
246 fTail->fNext = list.fHead;
247 list.fHead->fPrev = fTail;
248 } else {
249 fHead = list.fHead;
250 }
251 fTail = list.fTail;
252 }
senorblancoe6eaa322016-03-08 09:06:44 -0800253 void prepend(Vertex* v) {
254 insert(v, nullptr, fHead);
255 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500256 void remove(Vertex* v) {
257 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
258 }
senorblancof57372d2016-08-31 10:36:19 -0700259 void close() {
260 if (fHead && fTail) {
261 fTail->fNext = fHead;
262 fHead->fPrev = fTail;
263 }
264 }
senorblancoe6eaa322016-03-08 09:06:44 -0800265};
266
senorblancof57372d2016-08-31 10:36:19 -0700267// Round to nearest quarter-pixel. This is used for screenspace tessellation.
268
269inline void round(SkPoint* p) {
270 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
271 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
272}
273
Stephen White94b7e542018-01-04 14:01:10 -0500274inline SkScalar double_to_clamped_scalar(double d) {
275 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
276}
277
senorblanco49df8d12016-10-07 08:36:56 -0700278// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
279struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500280 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700281 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
282 Line(const SkPoint& p, const SkPoint& q)
283 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
284 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
285 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
286 static_cast<double>(p.fX) * q.fY) {}
287 double dist(const SkPoint& p) const {
288 return fA * p.fX + fB * p.fY + fC;
289 }
Stephen Whitee260c462017-12-19 18:09:54 -0500290 Line operator*(double v) const {
291 return Line(fA * v, fB * v, fC * v);
292 }
senorblanco49df8d12016-10-07 08:36:56 -0700293 double magSq() const {
294 return fA * fA + fB * fB;
295 }
Stephen Whitee260c462017-12-19 18:09:54 -0500296 void normalize() {
297 double len = sqrt(this->magSq());
298 if (len == 0.0) {
299 return;
300 }
301 double scale = 1.0f / len;
302 fA *= scale;
303 fB *= scale;
304 fC *= scale;
305 }
306 bool nearParallel(const Line& o) const {
307 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
308 }
senorblanco49df8d12016-10-07 08:36:56 -0700309
310 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500311 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700312 double denom = fA * other.fB - fB * other.fA;
313 if (denom == 0.0) {
314 return false;
315 }
Stephen White94b7e542018-01-04 14:01:10 -0500316 double scale = 1.0 / denom;
317 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
318 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500319 round(point);
Stephen White9b7f1432019-06-08 08:56:58 -0400320 return point->isFinite();
senorblanco49df8d12016-10-07 08:36:56 -0700321 }
322 double fA, fB, fC;
323};
324
ethannicholase9709e82016-01-07 13:34:16 -0800325/**
326 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
327 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
328 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whiteb67b2352019-06-01 13:07:27 -0400329 * point). For speed, that case is only tested by the callers that require it. Edges also handle
330 * checking for intersection with other edges. Currently, this converts the edges to the
331 * parametric form, in order to avoid doing a division until an intersection has been confirmed.
332 * This is slightly slower in the "found" case, but a lot faster in the "not found" case.
ethannicholase9709e82016-01-07 13:34:16 -0800333 *
334 * The coefficients of the line equation stored in double precision to avoid catastrphic
335 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
336 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
337 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
338 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
339 * this file).
340 */
341
342struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500343 enum class Type { kInner, kOuter, kConnector };
344 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800345 : fWinding(winding)
346 , fTop(top)
347 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500348 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800349 , fLeft(nullptr)
350 , fRight(nullptr)
351 , fPrevEdgeAbove(nullptr)
352 , fNextEdgeAbove(nullptr)
353 , fPrevEdgeBelow(nullptr)
354 , fNextEdgeBelow(nullptr)
355 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700356 , fRightPoly(nullptr)
357 , fLeftPolyPrev(nullptr)
358 , fLeftPolyNext(nullptr)
359 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700360 , fRightPolyNext(nullptr)
361 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700362 , fUsedInRightPoly(false)
363 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800364 }
365 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
366 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
367 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500368 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800369 Edge* fLeft; // The linked list of edges in the active edge list.
370 Edge* fRight; // "
371 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
372 Edge* fNextEdgeAbove; // "
373 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
374 Edge* fNextEdgeBelow; // "
375 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
376 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700377 Edge* fLeftPolyPrev;
378 Edge* fLeftPolyNext;
379 Edge* fRightPolyPrev;
380 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700381 bool fUsedInLeftPoly;
382 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700383 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800384 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700385 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800386 }
387 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700388 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800389 }
390 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700391 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800392 }
393 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700394 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800395 }
Stephen White95152e12017-12-18 10:52:44 -0500396 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
Brian Salomon120e7d62019-09-11 10:29:22 -0400397 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
398 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800399 if (fTop == other.fTop || fBottom == other.fBottom) {
400 return false;
401 }
senorblanco49df8d12016-10-07 08:36:56 -0700402 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800403 if (denom == 0.0) {
404 return false;
405 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500406 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
407 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
408 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
409 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800410 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
411 // This saves us doing the divide below unless absolutely necessary.
412 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
413 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
414 return false;
415 }
416 double s = sNumer / denom;
417 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700418 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
419 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500420 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500421 if (fType == Type::kConnector) {
422 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
423 } else if (other.fType == Type::kConnector) {
424 double t = tNumer / denom;
425 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500426 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
427 *alpha = 0;
428 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500429 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500430 }
431 }
ethannicholase9709e82016-01-07 13:34:16 -0800432 return true;
433 }
senorblancof57372d2016-08-31 10:36:19 -0700434};
435
Stephen Whitec4dbc372019-05-22 10:50:14 -0400436struct SSEdge;
437
438struct SSVertex {
439 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
440 Vertex* fVertex;
441 SSEdge* fPrev;
442 SSEdge* fNext;
443};
444
445struct SSEdge {
446 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
447 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
448 }
449 Edge* fEdge;
450 Event* fEvent;
451 SSVertex* fPrev;
452 SSVertex* fNext;
453};
454
455typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
456typedef std::vector<SSEdge*> SSEdgeList;
457
senorblancof57372d2016-08-31 10:36:19 -0700458struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500459 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700460 Edge* fHead;
461 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700462 void insert(Edge* edge, Edge* prev, Edge* next) {
463 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700464 }
465 void append(Edge* e) {
466 insert(e, fTail, nullptr);
467 }
468 void remove(Edge* edge) {
469 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700470 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400471 void removeAll() {
472 while (fHead) {
473 this->remove(fHead);
474 }
475 }
senorblancof57372d2016-08-31 10:36:19 -0700476 void close() {
477 if (fHead && fTail) {
478 fTail->fRight = fHead;
479 fHead->fLeft = fTail;
480 }
481 }
482 bool contains(Edge* edge) const {
483 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800484 }
485};
486
Stephen Whitec4dbc372019-05-22 10:50:14 -0400487struct EventList;
488
Stephen Whitee260c462017-12-19 18:09:54 -0500489struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400490 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
491 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500492 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400493 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500494 SkPoint fPoint;
495 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400496 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500497};
498
Stephen Whitec4dbc372019-05-22 10:50:14 -0400499struct EventComparator {
500 enum class Op { kLessThan, kGreaterThan };
501 EventComparator(Op op) : fOp(op) {}
502 bool operator() (Event* const &e1, Event* const &e2) {
503 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
504 : e1->fAlpha > e2->fAlpha;
505 }
506 Op fOp;
507};
Stephen Whitee260c462017-12-19 18:09:54 -0500508
Stephen Whitec4dbc372019-05-22 10:50:14 -0400509typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500510
Stephen Whitec4dbc372019-05-22 10:50:14 -0400511struct EventList : EventPQ {
512 EventList(EventComparator comparison) : EventPQ(comparison) {
513 }
514};
515
516void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
517 Vertex* prev = e->fPrev->fVertex;
518 Vertex* next = e->fNext->fVertex;
519 if (prev == next || !prev->fPartner || !next->fPartner) {
520 return;
521 }
522 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
523 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500524 SkPoint p;
525 uint8_t alpha;
526 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400527 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
528 "will collapse to %g,%g alpha %d\n",
529 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
530 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400531 e->fEvent = alloc.make<Event>(e, p, alpha);
532 events->push(e->fEvent);
533 }
534}
535
536void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
537 Comparator& c, SkArenaAlloc& alloc) {
538 if (!v->fPartner) {
539 return;
540 }
Stephen White8a3c0592019-05-29 11:26:16 -0400541 Vertex* top = edge->fEdge->fTop;
542 Vertex* bottom = edge->fEdge->fBottom;
543 if (!top || !bottom ) {
544 return;
545 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400546 Line line = edge->fEdge->fLine;
547 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
548 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
549 SkPoint p;
550 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400551 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
552 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400553 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
554 "will collapse to %g,%g alpha %d\n",
555 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400556 edge->fEvent = alloc.make<Event>(edge, p, alpha);
557 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500558 }
559}
Stephen Whitee260c462017-12-19 18:09:54 -0500560
ethannicholase9709e82016-01-07 13:34:16 -0800561/***************************************************************************************/
562
563struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700564 Poly(Vertex* v, int winding)
565 : fFirstVertex(v)
566 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800567 , fHead(nullptr)
568 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800569 , fNext(nullptr)
570 , fPartner(nullptr)
571 , fCount(0)
572 {
573#if LOGGING_ENABLED
574 static int gID = 0;
575 fID = gID++;
Brian Salomon120e7d62019-09-11 10:29:22 -0400576 TESS_LOG("*** created Poly %d\n", fID);
ethannicholase9709e82016-01-07 13:34:16 -0800577#endif
578 }
senorblanco531237e2016-06-02 11:36:48 -0700579 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800580 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700581 MonotonePoly(Edge* edge, Side side)
582 : fSide(side)
583 , fFirstEdge(nullptr)
584 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800585 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700586 , fNext(nullptr) {
587 this->addEdge(edge);
588 }
ethannicholase9709e82016-01-07 13:34:16 -0800589 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700590 Edge* fFirstEdge;
591 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800592 MonotonePoly* fPrev;
593 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700594 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800595 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700596 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700597 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
598 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700599 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800600 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700601 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700602 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
603 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700604 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800605 }
ethannicholase9709e82016-01-07 13:34:16 -0800606 }
607
Brian Osman0995fd52019-01-09 09:52:25 -0500608 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700609 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700610 VertexList vertices;
611 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500612 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700613 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700614 if (kRight_Side == fSide) {
615 vertices.append(e->fBottom);
616 e = e->fRightPolyNext;
617 } else {
618 vertices.prepend(e->fBottom);
619 e = e->fLeftPolyNext;
620 }
Stephen White651cbe92017-03-03 12:24:16 -0500621 count++;
senorblanco531237e2016-06-02 11:36:48 -0700622 }
623 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800624 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700625 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800626 SkASSERT(v && v->fPrev && v->fNext);
627 Vertex* prev = v->fPrev;
628 Vertex* curr = v;
629 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500630 if (count == 3) {
Brian Osman0995fd52019-01-09 09:52:25 -0500631 return emit_triangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500632 }
ethannicholase9709e82016-01-07 13:34:16 -0800633 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
634 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
635 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
636 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
637 if (ax * by - ay * bx >= 0.0) {
Brian Osman0995fd52019-01-09 09:52:25 -0500638 data = emit_triangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800639 v->fPrev->fNext = v->fNext;
640 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500641 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800642 if (v->fPrev == first) {
643 v = v->fNext;
644 } else {
645 v = v->fPrev;
646 }
647 } else {
648 v = v->fNext;
649 }
650 }
651 return data;
652 }
653 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500654 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400655 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
656 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800657 Poly* partner = fPartner;
658 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700659 if (side == kRight_Side) {
660 if (e->fUsedInRightPoly) {
661 return this;
662 }
663 } else {
664 if (e->fUsedInLeftPoly) {
665 return this;
666 }
667 }
ethannicholase9709e82016-01-07 13:34:16 -0800668 if (partner) {
669 fPartner = partner->fPartner = nullptr;
670 }
senorblanco531237e2016-06-02 11:36:48 -0700671 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500672 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700673 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700674 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
675 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700676 } else if (side == fTail->fSide) {
677 fTail->addEdge(e);
678 fCount++;
679 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500680 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700681 fTail->addEdge(e);
682 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800683 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700684 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800685 poly = partner;
686 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500687 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700688 m->fPrev = fTail;
689 fTail->fNext = m;
690 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800691 }
692 }
ethannicholase9709e82016-01-07 13:34:16 -0800693 return poly;
694 }
Brian Osman0995fd52019-01-09 09:52:25 -0500695 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800696 if (fCount < 3) {
697 return data;
698 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400699 TESS_LOG("emit() %d, size %d\n", fID, fCount);
ethannicholase9709e82016-01-07 13:34:16 -0800700 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500701 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800702 }
703 return data;
704 }
senorblanco531237e2016-06-02 11:36:48 -0700705 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
706 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800707 int fWinding;
708 MonotonePoly* fHead;
709 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800710 Poly* fNext;
711 Poly* fPartner;
712 int fCount;
713#if LOGGING_ENABLED
714 int fID;
715#endif
716};
717
718/***************************************************************************************/
719
720bool coincident(const SkPoint& a, const SkPoint& b) {
721 return a == b;
722}
723
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500724Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
725 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800726 poly->fNext = *head;
727 *head = poly;
728 return poly;
729}
730
Stephen White3a9aab92017-03-07 14:07:18 -0500731void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500732 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800733#if LOGGING_ENABLED
734 static float gID = 0.0f;
735 v->fID = gID++;
736#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500737 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800738}
739
Stephen White36e4f062017-03-27 16:11:31 -0400740SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
741 SkQuadCoeff quad(pts);
742 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
743 SkPoint mid = to_point(quad.eval(t));
744 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400745 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
746 return 0;
747 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500748 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400749}
750
751void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
752 SkArenaAlloc& alloc) {
753 SkQuadCoeff quad(pts);
754 Sk2s aa = quad.fA * quad.fA;
755 SkScalar denom = 2.0f * (aa[0] + aa[1]);
756 Sk2s ab = quad.fA * quad.fB;
757 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
758 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500759 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400760 // Test possible subdivision values only at the point of maximum curvature.
761 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500762 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400763 u = 1.0f / nPoints;
764 if (quad_error_at(pts, t, u) < toleranceSqd) {
765 break;
766 }
767 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800768 }
Stephen White36e4f062017-03-27 16:11:31 -0400769 for (int j = 1; j <= nPoints; j++) {
770 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
771 }
ethannicholase9709e82016-01-07 13:34:16 -0800772}
773
Stephen White3a9aab92017-03-07 14:07:18 -0500774void generate_cubic_points(const SkPoint& p0,
775 const SkPoint& p1,
776 const SkPoint& p2,
777 const SkPoint& p3,
778 SkScalar tolSqd,
779 VertexList* contour,
780 int pointsLeft,
781 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500782 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
783 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800784 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
785 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500786 append_point_to_contour(p3, contour, alloc);
787 return;
ethannicholase9709e82016-01-07 13:34:16 -0800788 }
789 const SkPoint q[] = {
790 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
791 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
792 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
793 };
794 const SkPoint r[] = {
795 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
796 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
797 };
798 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
799 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500800 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
801 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800802}
803
804// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
805
806void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500807 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800808 SkScalar toleranceSqd = tolerance * tolerance;
809
810 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800811 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500812 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800813 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800814 if (path.isInverseFillType()) {
815 SkPoint quad[4];
816 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700817 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500818 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800819 }
Stephen White3a9aab92017-03-07 14:07:18 -0500820 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800821 }
822 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500823 SkPath::Verb verb;
Mike Reedba7e9a62019-08-16 13:30:34 -0400824 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800825 switch (verb) {
826 case SkPath::kConic_Verb: {
827 SkScalar weight = iter.conicWeight();
828 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
829 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400830 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800831 quadPts += 2;
832 }
833 *isLinear = false;
834 break;
835 }
836 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500837 if (contour->fHead) {
838 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800839 }
Stephen White3a9aab92017-03-07 14:07:18 -0500840 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800841 break;
842 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500843 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800844 break;
845 }
846 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400847 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800848 *isLinear = false;
849 break;
850 }
851 case SkPath::kCubic_Verb: {
852 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500853 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
854 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800855 *isLinear = false;
856 break;
857 }
858 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800859 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800860 break;
861 }
862 }
863}
864
Mike Reed7d34dc72019-11-26 12:17:17 -0500865inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800866 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500867 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800868 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500869 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800870 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500871 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700872 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500873 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800874 return (winding & 1) == 1;
875 default:
876 SkASSERT(false);
877 return false;
878 }
879}
880
Mike Reed7d34dc72019-11-26 12:17:17 -0500881inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500882 return poly && apply_fill_type(fillType, poly->fWinding);
883}
884
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500885Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500886 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800887 Vertex* top = winding < 0 ? next : prev;
888 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500889 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800890}
891
892void remove_edge(Edge* edge, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400893 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700894 SkASSERT(edges->contains(edge));
895 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800896}
897
898void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400899 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700900 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800901 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700902 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800903}
904
905void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500906 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800907 *left = v->fFirstEdgeAbove->fLeft;
908 *right = v->fLastEdgeAbove->fRight;
909 return;
910 }
911 Edge* next = nullptr;
912 Edge* prev;
913 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
914 if (prev->isLeftOf(v)) {
915 break;
916 }
917 next = prev;
918 }
919 *left = prev;
920 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800921}
922
ethannicholase9709e82016-01-07 13:34:16 -0800923void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
924 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500925 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800926 return;
927 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400928 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
929 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800930 Edge* prev = nullptr;
931 Edge* next;
932 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
933 if (next->isRightOf(edge->fTop)) {
934 break;
935 }
936 prev = next;
937 }
senorblancoe6eaa322016-03-08 09:06:44 -0800938 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800939 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
940}
941
942void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
943 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500944 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800945 return;
946 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400947 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
948 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800949 Edge* prev = nullptr;
950 Edge* next;
951 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
952 if (next->isRightOf(edge->fBottom)) {
953 break;
954 }
955 prev = next;
956 }
senorblancoe6eaa322016-03-08 09:06:44 -0800957 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800958 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
959}
960
961void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400962 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400963 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
964 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800965 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800966 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
967}
968
969void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400970 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400971 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
972 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800973 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800974 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
975}
976
Stephen Whitee7a364d2017-01-11 16:19:26 -0500977void disconnect(Edge* edge)
978{
ethannicholase9709e82016-01-07 13:34:16 -0800979 remove_edge_above(edge);
980 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500981}
982
Stephen White3b5a3fa2017-06-06 14:51:19 -0400983void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
984
985void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
986 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
987 return;
988 }
989 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400990 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400991 while (v != dst) {
992 v = v->fPrev;
993 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
994 remove_edge(e, activeEdges);
995 }
996 Edge* leftEdge = v->fLeftEnclosingEdge;
997 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
998 insert_edge(e, leftEdge, activeEdges);
999 leftEdge = e;
1000 }
1001 }
1002 *current = v;
1003}
1004
Stephen White3b5a3fa2017-06-06 14:51:19 -04001005void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001006 remove_edge_below(edge);
1007 edge->fTop = v;
1008 edge->recompute();
1009 insert_edge_below(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001010 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001011 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001012}
1013
Stephen White3b5a3fa2017-06-06 14:51:19 -04001014void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001015 remove_edge_above(edge);
1016 edge->fBottom = v;
1017 edge->recompute();
1018 insert_edge_above(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001019 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001020 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001021}
1022
Stephen White3b5a3fa2017-06-06 14:51:19 -04001023void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1024 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001025 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001026 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1027 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1028 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001029 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001030 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001031 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001032 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001033 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001034 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001035 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001036 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001037 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001038 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001039 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001040 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001041 }
1042}
1043
Stephen White3b5a3fa2017-06-06 14:51:19 -04001044void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1045 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001046 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001047 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1048 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1049 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001050 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001051 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001052 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001053 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001054 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001055 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001056 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001057 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001058 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001059 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001060 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001061 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001062 }
1063}
1064
Stephen Whited26b4d82018-07-26 10:02:27 -04001065bool top_collinear(Edge* left, Edge* right) {
1066 if (!left || !right) {
1067 return false;
1068 }
1069 return left->fTop->fPoint == right->fTop->fPoint ||
1070 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1071}
1072
1073bool bottom_collinear(Edge* left, Edge* right) {
1074 if (!left || !right) {
1075 return false;
1076 }
1077 return left->fBottom->fPoint == right->fBottom->fPoint ||
1078 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1079}
1080
Stephen White3b5a3fa2017-06-06 14:51:19 -04001081void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001082 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001083 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001084 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001085 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001086 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001087 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001088 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001089 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001090 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001091 } else {
1092 break;
1093 }
ethannicholase9709e82016-01-07 13:34:16 -08001094 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001095 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1096 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1097 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1098 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001099}
1100
Stephen White89042d52018-06-08 12:18:22 -04001101bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001102 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001103 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001104 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001105 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001106 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1107 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001108 Vertex* top;
1109 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001110 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001111 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001112 top = v;
1113 bottom = edge->fTop;
1114 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001115 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001116 top = edge->fBottom;
1117 bottom = v;
1118 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001119 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001120 top = v;
1121 bottom = edge->fBottom;
1122 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001123 }
Stephen White531a48e2018-06-01 09:49:39 -04001124 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001125 insert_edge_below(newEdge, top, c);
1126 insert_edge_above(newEdge, bottom, c);
1127 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001128 return true;
1129}
1130
1131bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1132 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1133 return false;
1134 }
Stephen White1c5fd182018-07-12 15:54:05 -04001135 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1136 return false;
1137 }
Stephen White89042d52018-06-08 12:18:22 -04001138 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1139 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001140 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001141 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1142 }
1143 } else {
1144 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001145 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001146 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1147 }
1148 }
1149 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1150 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001151 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001152 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1153 }
1154 } else {
1155 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001156 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001157 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1158 }
1159 }
1160 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001161}
1162
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001163Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001164 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001165 if (!prev || !next || prev->fPoint == next->fPoint) {
1166 return nullptr;
1167 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001168 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001169 insert_edge_below(edge, edge->fTop, c);
1170 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001171 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001172 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001173 return edge;
1174}
1175
Stephen Whitebf6137e2017-01-04 15:43:26 -05001176void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001177 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001178 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
1179 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001180 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001181 if (src->fPartner) {
1182 src->fPartner->fPartner = dst;
1183 }
Stephen White7b376942018-05-22 11:51:32 -04001184 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001185 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001186 }
Stephen White7b376942018-05-22 11:51:32 -04001187 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001188 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001189 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001190 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001191 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001192}
1193
Stephen White95152e12017-12-18 10:52:44 -05001194Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1195 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1196 Vertex* prevV = reference;
1197 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1198 prevV = prevV->fPrev;
1199 }
1200 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1201 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1202 prevV = nextV;
1203 nextV = nextV->fNext;
1204 }
1205 Vertex* v;
1206 if (prevV && coincident(prevV->fPoint, p)) {
1207 v = prevV;
1208 } else if (nextV && coincident(nextV->fPoint, p)) {
1209 v = nextV;
1210 } else {
1211 v = alloc.make<Vertex>(p, alpha);
1212#if LOGGING_ENABLED
1213 if (!prevV) {
1214 v->fID = mesh->fHead->fID - 1.0f;
1215 } else if (!nextV) {
1216 v->fID = mesh->fTail->fID + 1.0f;
1217 } else {
1218 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1219 }
1220#endif
1221 mesh->insert(v, prevV, nextV);
1222 }
1223 return v;
1224}
1225
Stephen White53a02982018-05-30 22:47:46 -04001226// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1227// sort criterion, it may not be possible to split correctly, since there is no point which is
1228// below the top and above the bottom. This function detects that case.
1229bool nearly_flat(Comparator& c, Edge* edge) {
1230 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1231 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001232 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001233}
1234
Stephen Whitee62999f2018-06-05 18:45:07 -04001235SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1236 if (c.sweep_lt(p, min)) {
1237 return min;
1238 } else if (c.sweep_lt(max, p)) {
1239 return max;
1240 } else {
1241 return p;
1242 }
1243}
1244
Stephen Whitec4dbc372019-05-22 10:50:14 -04001245void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1246 Line line1 = edge1->fLine;
1247 Line line2 = edge2->fLine;
1248 line1.normalize();
1249 line2.normalize();
1250 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1251 if (cosAngle > 0.999) {
1252 return;
1253 }
1254 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1255 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1256 SkPoint p;
1257 if (line1.intersect(line2, &p)) {
1258 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1259 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001260 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 -04001261 }
1262}
1263
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001264bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001265 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001266 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001267 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001268 }
Stephen White56158ae2017-01-30 14:31:31 -05001269 SkPoint p;
1270 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001271 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001272 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001273 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001274 Vertex* top = *current;
1275 // If the intersection point is above the current vertex, rewind to the vertex above the
1276 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001277 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001278 top = top->fPrev;
1279 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001280 if (!nearly_flat(c, left)) {
1281 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001282 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001283 if (!nearly_flat(c, right)) {
1284 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001285 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001286 if (p == left->fTop->fPoint) {
1287 v = left->fTop;
1288 } else if (p == left->fBottom->fPoint) {
1289 v = left->fBottom;
1290 } else if (p == right->fTop->fPoint) {
1291 v = right->fTop;
1292 } else if (p == right->fBottom->fPoint) {
1293 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001294 } else {
Stephen White95152e12017-12-18 10:52:44 -05001295 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001296 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001297 v->fSynthetic = true;
1298 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001299 }
ethannicholase9709e82016-01-07 13:34:16 -08001300 }
Stephen White0cb31672017-06-08 14:41:01 -04001301 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001302 split_edge(left, v, activeEdges, current, c, alloc);
1303 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001304 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001305 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001306 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001307 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001308}
1309
Stephen White3a9aab92017-03-07 14:07:18 -05001310void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
1311 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1312 SkASSERT(contour->fHead);
1313 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001314 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001315 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001316 }
Stephen White3a9aab92017-03-07 14:07:18 -05001317 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001318 if (approximate) {
1319 round(&v->fPoint);
1320 }
Stephen White3a9aab92017-03-07 14:07:18 -05001321 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001322 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001323 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001324 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001325 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001326 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001327 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001328 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001329 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001330 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001331 contour->remove(v);
1332 } else {
1333 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001334 }
Stephen White3a9aab92017-03-07 14:07:18 -05001335 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001336 }
1337 }
1338}
1339
Stephen Whitee260c462017-12-19 18:09:54 -05001340bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001341 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001342 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001343 }
Stephen Whitee260c462017-12-19 18:09:54 -05001344 bool merged = false;
1345 for (Vertex* v = mesh->fHead->fNext; v;) {
1346 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001347 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1348 v->fPoint = v->fPrev->fPoint;
1349 }
1350 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001351 merge_vertices(v, v->fPrev, mesh, c, alloc);
1352 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001353 }
Stephen Whitee260c462017-12-19 18:09:54 -05001354 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001355 }
Stephen Whitee260c462017-12-19 18:09:54 -05001356 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001357}
1358
1359// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1360
Stephen White3a9aab92017-03-07 14:07:18 -05001361void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001362 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001363 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1364 Vertex* prev = contour->fTail;
1365 for (Vertex* v = contour->fHead; v;) {
1366 Vertex* next = v->fNext;
1367 connect(prev, v, Edge::Type::kInner, c, alloc);
1368 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001369 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001370 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001371 }
1372 }
ethannicholase9709e82016-01-07 13:34:16 -08001373}
1374
Stephen Whitee260c462017-12-19 18:09:54 -05001375void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1376 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001377 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001378 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1379 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1380 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1381 // number.
1382 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1383 inner->fPartner = outer->fPartner = nullptr;
1384 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001385 }
1386 }
1387}
1388
1389template <CompareFunc sweep_lt>
1390void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1391 Vertex* a = front->fHead;
1392 Vertex* b = back->fHead;
1393 while (a && b) {
1394 if (sweep_lt(a->fPoint, b->fPoint)) {
1395 front->remove(a);
1396 result->append(a);
1397 a = front->fHead;
1398 } else {
1399 back->remove(b);
1400 result->append(b);
1401 b = back->fHead;
1402 }
1403 }
1404 result->append(*front);
1405 result->append(*back);
1406}
1407
1408void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1409 if (c.fDirection == Comparator::Direction::kHorizontal) {
1410 sorted_merge<sweep_lt_horiz>(front, back, result);
1411 } else {
1412 sorted_merge<sweep_lt_vert>(front, back, result);
1413 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001414#if LOGGING_ENABLED
1415 float id = 0.0f;
1416 for (Vertex* v = result->fHead; v; v = v->fNext) {
1417 v->fID = id++;
1418 }
1419#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001420}
1421
ethannicholase9709e82016-01-07 13:34:16 -08001422// Stage 3: sort the vertices by increasing sweep direction.
1423
Stephen White16a40cb2017-02-23 11:10:01 -05001424template <CompareFunc sweep_lt>
1425void merge_sort(VertexList* vertices) {
1426 Vertex* slow = vertices->fHead;
1427 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001428 return;
1429 }
Stephen White16a40cb2017-02-23 11:10:01 -05001430 Vertex* fast = slow->fNext;
1431 if (!fast) {
1432 return;
1433 }
1434 do {
1435 fast = fast->fNext;
1436 if (fast) {
1437 fast = fast->fNext;
1438 slow = slow->fNext;
1439 }
1440 } while (fast);
1441 VertexList front(vertices->fHead, slow);
1442 VertexList back(slow->fNext, vertices->fTail);
1443 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001444
Stephen White16a40cb2017-02-23 11:10:01 -05001445 merge_sort<sweep_lt>(&front);
1446 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001447
Stephen White16a40cb2017-02-23 11:10:01 -05001448 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001449 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001450}
1451
Stephen White95152e12017-12-18 10:52:44 -05001452void dump_mesh(const VertexList& mesh) {
1453#if LOGGING_ENABLED
1454 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001455 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 -05001456 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001457 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1458 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001459 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001460 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001461 }
1462 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001463 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001464 }
1465 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001466 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001467 }
1468 }
1469#endif
1470}
1471
Stephen Whitec4dbc372019-05-22 10:50:14 -04001472void dump_skel(const SSEdgeList& ssEdges) {
1473#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001474 for (SSEdge* edge : ssEdges) {
1475 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001476 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001477 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001478 edge->fNext->fVertex->fID);
1479 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001480 TESS_LOG(" (original %g -> %g)\n",
1481 edge->fEdge->fTop->fID,
1482 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001483 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001484 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001485 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001486 }
1487 }
1488#endif
1489}
1490
Stephen White89042d52018-06-08 12:18:22 -04001491#ifdef SK_DEBUG
1492void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1493 if (!left || !right) {
1494 return;
1495 }
1496 if (left->fTop == right->fTop) {
1497 SkASSERT(left->isLeftOf(right->fBottom));
1498 SkASSERT(right->isRightOf(left->fBottom));
1499 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1500 SkASSERT(left->isLeftOf(right->fTop));
1501 } else {
1502 SkASSERT(right->isRightOf(left->fTop));
1503 }
1504 if (left->fBottom == right->fBottom) {
1505 SkASSERT(left->isLeftOf(right->fTop));
1506 SkASSERT(right->isRightOf(left->fTop));
1507 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1508 SkASSERT(left->isLeftOf(right->fBottom));
1509 } else {
1510 SkASSERT(right->isRightOf(left->fBottom));
1511 }
1512}
1513
1514void validate_edge_list(EdgeList* edges, Comparator& c) {
1515 Edge* left = edges->fHead;
1516 if (!left) {
1517 return;
1518 }
1519 for (Edge* right = left->fRight; right; right = right->fRight) {
1520 validate_edge_pair(left, right, c);
1521 left = right;
1522 }
1523}
1524#endif
1525
ethannicholase9709e82016-01-07 13:34:16 -08001526// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1527
Stephen Whitec4dbc372019-05-22 10:50:14 -04001528bool connected(Vertex* v) {
1529 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1530}
1531
Stephen Whitee260c462017-12-19 18:09:54 -05001532bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001533 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001534 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001535 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001536 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001537 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001538 continue;
1539 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001540 Edge* leftEnclosingEdge;
1541 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001542 bool restartChecks;
1543 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001544 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1545 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001546 restartChecks = false;
1547 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001548 v->fLeftEnclosingEdge = leftEnclosingEdge;
1549 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001550 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001551 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001552 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001553 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001554 restartChecks = true;
1555 break;
1556 }
Stephen White0cb31672017-06-08 14:41:01 -04001557 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001558 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001559 restartChecks = true;
1560 break;
1561 }
1562 }
1563 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001564 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001565 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001566 restartChecks = true;
1567 }
1568
1569 }
Stephen Whitee260c462017-12-19 18:09:54 -05001570 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001571 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001572#ifdef SK_DEBUG
1573 validate_edge_list(&activeEdges, c);
1574#endif
ethannicholase9709e82016-01-07 13:34:16 -08001575 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1576 remove_edge(e, &activeEdges);
1577 }
1578 Edge* leftEdge = leftEnclosingEdge;
1579 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1580 insert_edge(e, leftEdge, &activeEdges);
1581 leftEdge = e;
1582 }
ethannicholase9709e82016-01-07 13:34:16 -08001583 }
Stephen Whitee260c462017-12-19 18:09:54 -05001584 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1585 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001586}
1587
1588// Stage 5: Tessellate the simplified mesh into monotone polygons.
1589
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001590Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001591 TESS_LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001592 EdgeList activeEdges;
1593 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001594 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001595 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001596 continue;
1597 }
1598#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001599 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 -08001600#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001601 Edge* leftEnclosingEdge;
1602 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001603 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001604 Poly* leftPoly;
1605 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001606 if (v->fFirstEdgeAbove) {
1607 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1608 rightPoly = v->fLastEdgeAbove->fRightPoly;
1609 } else {
1610 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1611 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1612 }
1613#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001614 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001615 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001616 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1617 e->fTop->fID, e->fBottom->fID,
1618 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1619 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001620 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001621 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001622 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001623 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1624 e->fTop->fID, e->fBottom->fID,
1625 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1626 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001627 }
1628#endif
1629 if (v->fFirstEdgeAbove) {
1630 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001631 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001632 }
1633 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001634 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001635 }
1636 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001637 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001638 remove_edge(e, &activeEdges);
1639 if (e->fRightPoly) {
1640 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001641 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001642 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001643 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001644 }
1645 }
1646 remove_edge(v->fLastEdgeAbove, &activeEdges);
1647 if (!v->fFirstEdgeBelow) {
1648 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1649 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1650 rightPoly->fPartner = leftPoly;
1651 leftPoly->fPartner = rightPoly;
1652 }
1653 }
1654 }
1655 if (v->fFirstEdgeBelow) {
1656 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001657 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001658 if (leftPoly == rightPoly) {
1659 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1660 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1661 leftPoly->fWinding, alloc);
1662 leftEnclosingEdge->fRightPoly = leftPoly;
1663 } else {
1664 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1665 rightPoly->fWinding, alloc);
1666 rightEnclosingEdge->fLeftPoly = rightPoly;
1667 }
ethannicholase9709e82016-01-07 13:34:16 -08001668 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001669 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001670 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1671 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001672 }
1673 }
1674 Edge* leftEdge = v->fFirstEdgeBelow;
1675 leftEdge->fLeftPoly = leftPoly;
1676 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1677 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1678 rightEdge = rightEdge->fNextEdgeBelow) {
1679 insert_edge(rightEdge, leftEdge, &activeEdges);
1680 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1681 winding += leftEdge->fWinding;
1682 if (winding != 0) {
1683 Poly* poly = new_poly(&polys, v, winding, alloc);
1684 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1685 }
1686 leftEdge = rightEdge;
1687 }
1688 v->fLastEdgeBelow->fRightPoly = rightPoly;
1689 }
1690#if LOGGING_ENABLED
Brian Salomon120e7d62019-09-11 10:29:22 -04001691 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001692 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001693 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1694 e->fTop->fID, e->fBottom->fID,
1695 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1696 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001697 }
1698#endif
1699 }
1700 return polys;
1701}
1702
Mike Reed7d34dc72019-11-26 12:17:17 -05001703void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001704 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001705 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001706 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001707 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001708 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001709 continue;
1710 }
1711 Edge* leftEnclosingEdge;
1712 Edge* rightEnclosingEdge;
1713 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1714 bool prevFilled = leftEnclosingEdge &&
1715 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1716 for (Edge* e = v->fFirstEdgeAbove; e;) {
1717 Edge* next = e->fNextEdgeAbove;
1718 remove_edge(e, &activeEdges);
1719 bool filled = apply_fill_type(fillType, e->fWinding);
1720 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001721 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001722 }
Stephen White49789062017-02-21 10:35:49 -05001723 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001724 e = next;
1725 }
Stephen White49789062017-02-21 10:35:49 -05001726 Edge* prev = leftEnclosingEdge;
1727 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1728 if (prev) {
1729 e->fWinding += prev->fWinding;
1730 }
1731 insert_edge(e, prev, &activeEdges);
1732 prev = e;
1733 }
senorblancof57372d2016-08-31 10:36:19 -07001734 }
senorblancof57372d2016-08-31 10:36:19 -07001735}
1736
Stephen White66412122017-03-01 11:48:27 -05001737// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001738void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001739 normal->set(SkDoubleToScalar(e->fLine.fA),
1740 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001741}
1742
1743// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1744// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1745// invert on stroking.
1746
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001747void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001748 Edge* prevEdge = boundary->fTail;
1749 SkVector prevNormal;
1750 get_edge_normal(prevEdge, &prevNormal);
1751 for (Edge* e = boundary->fHead; e != nullptr;) {
1752 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1753 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001754 double distPrev = e->dist(prev->fPoint);
1755 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001756 SkVector normal;
1757 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001758 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001759 if (prev == next) {
1760 remove_edge(prevEdge, boundary);
1761 remove_edge(e, boundary);
1762 prevEdge = boundary->fTail;
1763 e = boundary->fHead;
1764 if (prevEdge) {
1765 get_edge_normal(prevEdge, &prevNormal);
1766 }
1767 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001768 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001769 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001770 if (prev->fPoint != next->fPoint) {
1771 join->fLine.normalize();
1772 join->fLine = join->fLine * join->fWinding;
1773 }
senorblancof57372d2016-08-31 10:36:19 -07001774 insert_edge(join, e, boundary);
1775 remove_edge(prevEdge, boundary);
1776 remove_edge(e, boundary);
1777 if (join->fLeft && join->fRight) {
1778 prevEdge = join->fLeft;
1779 e = join;
1780 } else {
1781 prevEdge = boundary->fTail;
1782 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1783 }
1784 get_edge_normal(prevEdge, &prevNormal);
1785 } else {
1786 prevEdge = e;
1787 prevNormal = normal;
1788 e = e->fRight;
1789 }
1790 }
1791}
1792
Stephen Whitec4dbc372019-05-22 10:50:14 -04001793void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1794 if (v == dest) {
1795 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001796 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001797 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001798 if (v->fSynthetic) {
1799 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1800 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001801 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1802 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001803 v->fPartner->fPartner = dest;
1804 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001805 }
1806}
1807
Stephen Whitec4dbc372019-05-22 10:50:14 -04001808void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1809 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001810 return;
1811 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001812 Vertex* prev = fEdge->fPrev->fVertex;
1813 Vertex* next = fEdge->fNext->fVertex;
1814 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1815 SSEdge* nextEdge = fEdge->fNext->fNext;
1816 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1817 return;
Stephen White77169c82018-06-05 09:15:59 -04001818 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001819 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1820 dest->fSynthetic = true;
1821 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001822 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1823 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1824 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001825 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001826
Stephen Whitec4dbc372019-05-22 10:50:14 -04001827 ss_connect(prev, dest, c, alloc);
1828 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001829
Stephen Whitec4dbc372019-05-22 10:50:14 -04001830 prevEdge->fNext = nextEdge->fPrev = ssv;
1831 ssv->fPrev = prevEdge;
1832 ssv->fNext = nextEdge;
1833 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1834 return;
1835 }
1836 if (prevEdge->fEvent) {
1837 prevEdge->fEvent->fEdge = nullptr;
1838 }
1839 if (nextEdge->fEvent) {
1840 nextEdge->fEvent->fEdge = nullptr;
1841 }
1842 if (prevEdge->fPrev == nextEdge->fNext) {
1843 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1844 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1845 } else {
1846 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1847 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1848 if (dest->fPartner) {
1849 create_event(prevEdge, events, alloc);
1850 create_event(nextEdge, events, alloc);
1851 } else {
1852 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1853 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1854 }
1855 }
Stephen Whitee260c462017-12-19 18:09:54 -05001856}
1857
1858bool is_overlap_edge(Edge* e) {
1859 if (e->fType == Edge::Type::kOuter) {
1860 return e->fWinding != 0 && e->fWinding != 1;
1861 } else if (e->fType == Edge::Type::kInner) {
1862 return e->fWinding != 0 && e->fWinding != -2;
1863 } else {
1864 return false;
1865 }
1866}
1867
1868// This is a stripped-down version of tessellate() which computes edges which
1869// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001870bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1871 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001872 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001873 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001874 EventList events(comp);
1875 SSVertexMap ssVertices;
1876 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001877 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001878 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001879 continue;
1880 }
1881 Edge* leftEnclosingEdge;
1882 Edge* rightEnclosingEdge;
1883 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001884 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001885 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1886 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001887 bool leftOverlap = prev && is_overlap_edge(prev);
1888 bool rightOverlap = is_overlap_edge(e);
1889 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1890 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001891 if (prev) {
1892 e->fWinding -= prev->fWinding;
1893 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001894 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001895 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1896 e->fTop->fID, e->fBottom->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001897 disconnect(e);
1898 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001899 TESS_LOG("found overlap edge %g -> %g%s\n",
1900 e->fTop->fID, e->fBottom->fID,
1901 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001902 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1903 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1904 SSVertex* ssPrev = ssVertices[prevVertex];
1905 if (!ssPrev) {
1906 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1907 }
1908 SSVertex* ssNext = ssVertices[nextVertex];
1909 if (!ssNext) {
1910 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1911 }
1912 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1913 ssEdges.push_back(ssEdge);
1914// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1915 ssPrev->fNext = ssNext->fPrev = ssEdge;
1916 create_event(ssEdge, &events, alloc);
1917 if (!isOuterBoundary) {
1918 disconnect(e);
1919 }
1920 }
1921 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001922 }
1923 Edge* prev = leftEnclosingEdge;
1924 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1925 if (prev) {
1926 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001927 }
1928 insert_edge(e, prev, &activeEdges);
1929 prev = e;
1930 }
1931 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001932 bool complex = events.size() > 0;
1933
Brian Salomon120e7d62019-09-11 10:29:22 -04001934 TESS_LOG("\ncollapsing overlap regions\n");
1935 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001936 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001937 while (events.size() > 0) {
1938 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001939 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001940 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001941 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001942 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001943 dump_skel(ssEdges);
1944 for (SSEdge* edge : ssEdges) {
1945 if (Edge* e = edge->fEdge) {
1946 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1947 }
1948 }
1949 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001950}
1951
1952bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1953 if (!prev || !next) {
1954 return true;
1955 }
1956 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1957 return winding != origEdge->fWinding;
1958}
Stephen White92eba8a2017-02-06 09:50:27 -05001959
senorblancof57372d2016-08-31 10:36:19 -07001960// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1961// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1962// new antialiased mesh from those vertices.
1963
Stephen Whitee260c462017-12-19 18:09:54 -05001964void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1965 Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001966 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001967 // A boundary with fewer than 3 edges is degenerate.
1968 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1969 return;
1970 }
1971 Edge* prevEdge = boundary->fTail;
1972 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1973 SkVector prevNormal;
1974 get_edge_normal(prevEdge, &prevNormal);
1975 double radius = 0.5;
1976 Line prevInner(prevEdge->fLine);
1977 prevInner.fC -= radius;
1978 Line prevOuter(prevEdge->fLine);
1979 prevOuter.fC += radius;
1980 VertexList innerVertices;
1981 VertexList outerVertices;
1982 bool innerInversion = true;
1983 bool outerInversion = true;
1984 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1985 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1986 SkVector normal;
1987 get_edge_normal(e, &normal);
1988 Line inner(e->fLine);
1989 inner.fC -= radius;
1990 Line outer(e->fLine);
1991 outer.fC += radius;
1992 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04001993 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001994 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1995 prevOuter.intersect(outer, &outerPoint)) {
1996 float cosAngle = normal.dot(prevNormal);
1997 if (cosAngle < -kCosMiterAngle) {
1998 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1999
2000 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2001 Line bisector(innerPoint, outerPoint);
2002 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2003 if (tangent.fA == 0 && tangent.fB == 0) {
2004 continue;
2005 }
2006 tangent.normalize();
2007 Line innerTangent(tangent);
2008 Line outerTangent(tangent);
2009 innerTangent.fC -= 0.5;
2010 outerTangent.fC += 0.5;
2011 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2012 if (prevNormal.cross(normal) > 0) {
2013 // Miter inner points
2014 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2015 !innerTangent.intersect(inner, &innerPoint2) ||
2016 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002017 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002018 }
2019 Line prevTangent(prevV->fPoint,
2020 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2021 Line nextTangent(nextV->fPoint,
2022 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002023 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002024 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002025 }
2026 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002027 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002028 }
2029 outerPoint1 = outerPoint2 = outerPoint;
2030 } else {
2031 // Miter outer points
2032 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2033 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002034 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002035 }
2036 Line prevTangent(prevV->fPoint,
2037 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2038 Line nextTangent(nextV->fPoint,
2039 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002040 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002041 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002042 }
2043 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002044 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002045 }
2046 innerPoint1 = innerPoint2 = innerPoint;
2047 }
Stephen Whiteea495232018-04-03 11:28:15 -04002048 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2049 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2050 continue;
2051 }
Brian Salomon120e7d62019-09-11 10:29:22 -04002052 TESS_LOG("inner (%g, %g), (%g, %g), ",
2053 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2054 TESS_LOG("outer (%g, %g), (%g, %g)\n",
2055 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002056 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2057 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2058 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2059 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2060 innerVertex1->fPartner = outerVertex1;
2061 innerVertex2->fPartner = outerVertex2;
2062 outerVertex1->fPartner = innerVertex1;
2063 outerVertex2->fPartner = innerVertex2;
2064 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2065 innerInversion = false;
2066 }
2067 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2068 outerInversion = false;
2069 }
2070 innerVertices.append(innerVertex1);
2071 innerVertices.append(innerVertex2);
2072 outerVertices.append(outerVertex1);
2073 outerVertices.append(outerVertex2);
2074 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002075 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2076 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05002077 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2078 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2079 innerVertex->fPartner = outerVertex;
2080 outerVertex->fPartner = innerVertex;
2081 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2082 innerInversion = false;
2083 }
2084 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2085 outerInversion = false;
2086 }
2087 innerVertices.append(innerVertex);
2088 outerVertices.append(outerVertex);
2089 }
2090 }
2091 prevInner = inner;
2092 prevOuter = outer;
2093 prevV = v;
2094 prevEdge = e;
2095 prevNormal = normal;
2096 }
2097 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2098 innerInversion = false;
2099 }
2100 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2101 outerInversion = false;
2102 }
2103 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2104 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2105 // interior inverts).
2106 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2107 // so it will be detected during collapse_overlap_regions().
2108 int innerWinding = innerInversion ? 2 : -2;
2109 int outerWinding = outerInversion ? -1 : 1;
2110 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2111 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2112 }
2113 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2114 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2115 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2116 }
2117 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2118 innerMesh->append(innerVertices);
2119 outerMesh->append(outerVertices);
2120}
senorblancof57372d2016-08-31 10:36:19 -07002121
Mike Reed7d34dc72019-11-26 12:17:17 -05002122void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002123 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002124 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04002125 Vertex* start = down ? e->fTop : e->fBottom;
2126 do {
senorblancof57372d2016-08-31 10:36:19 -07002127 e->fWinding = down ? 1 : -1;
2128 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002129 e->fLine.normalize();
2130 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002131 boundary->append(e);
2132 if (down) {
2133 // Find outgoing edge, in clockwise order.
2134 if ((next = e->fNextEdgeAbove)) {
2135 down = false;
2136 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2137 down = true;
2138 } else if ((next = e->fPrevEdgeAbove)) {
2139 down = false;
2140 }
2141 } else {
2142 // Find outgoing edge, in counter-clockwise order.
2143 if ((next = e->fPrevEdgeBelow)) {
2144 down = true;
2145 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2146 down = false;
2147 } else if ((next = e->fNextEdgeBelow)) {
2148 down = true;
2149 }
2150 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002151 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002152 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04002153 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07002154}
2155
Stephen White5ad721e2017-02-23 16:50:47 -05002156// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002157
Stephen Whitebda29c02017-03-13 15:10:13 -04002158void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Mike Reed7d34dc72019-11-26 12:17:17 -05002159 VertexList* outerVertices, SkPathFillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002160 Comparator& c, SkArenaAlloc& alloc) {
2161 remove_non_boundary_edges(inMesh, fillType, alloc);
2162 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002163 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002164 EdgeList boundary;
2165 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2166 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002167 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002168 }
2169 }
senorblancof57372d2016-08-31 10:36:19 -07002170}
2171
Stephen Whitebda29c02017-03-13 15:10:13 -04002172// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002173
Stephen White3a9aab92017-03-07 14:07:18 -05002174void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002175 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002176#if LOGGING_ENABLED
2177 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002178 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002179 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04002180 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002181 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002182 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08002183 }
2184 }
2185#endif
senorblancof57372d2016-08-31 10:36:19 -07002186 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002187 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002188}
2189
Stephen Whitebda29c02017-03-13 15:10:13 -04002190void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002191 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002192 return;
ethannicholase9709e82016-01-07 13:34:16 -08002193 }
2194
2195 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002196 if (c.fDirection == Comparator::Direction::kHorizontal) {
2197 merge_sort<sweep_lt_horiz>(vertices);
2198 } else {
2199 merge_sort<sweep_lt_vert>(vertices);
2200 }
ethannicholase9709e82016-01-07 13:34:16 -08002201#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002202 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002203 static float gID = 0.0f;
2204 v->fID = gID++;
2205 }
2206#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002207}
2208
Mike Reed7d34dc72019-11-26 12:17:17 -05002209Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPathFillType fillType,
Stephen Whitebda29c02017-03-13 15:10:13 -04002210 const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002211 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002212 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2213 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002214 VertexList mesh;
2215 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002216 sort_mesh(&mesh, c, alloc);
2217 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002218 simplify(&mesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002219 TESS_LOG("\nsimplified mesh:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04002220 dump_mesh(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002221 if (antialias) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002222 VertexList innerMesh;
2223 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2224 sort_mesh(&innerMesh, c, alloc);
2225 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002226 merge_coincident_vertices(&innerMesh, c, alloc);
2227 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2228 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2229 was_complex = simplify(outerMesh, c, alloc) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04002230 TESS_LOG("\ninner mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002231 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002232 TESS_LOG("\nouter mesh before:\n");
Stephen Whitee260c462017-12-19 18:09:54 -05002233 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002234 EventComparator eventLT(EventComparator::Op::kLessThan);
2235 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2236 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2237 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002238 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002239 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002240 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002241 TESS_LOG("\ninner mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002242 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002243 TESS_LOG("\nouter mesh after:\n");
Stephen White95152e12017-12-18 10:52:44 -05002244 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002245 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002246 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002247 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2248 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002249 simplify(&aaMesh, c, alloc);
Brian Salomon120e7d62019-09-11 10:29:22 -04002250 TESS_LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002251 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002252 outerMesh->fHead = outerMesh->fTail = nullptr;
2253 return tessellate(aaMesh, alloc);
2254 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002255 TESS_LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002256 return tessellate(innerMesh, alloc);
2257 }
Stephen White49789062017-02-21 10:35:49 -05002258 } else {
2259 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002260 }
senorblancof57372d2016-08-31 10:36:19 -07002261}
2262
2263// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Mike Reed7d34dc72019-11-26 12:17:17 -05002264void* polys_to_triangles(Poly* polys, SkPathFillType fillType, bool emitCoverage, void* data) {
senorblancof57372d2016-08-31 10:36:19 -07002265 for (Poly* poly = polys; poly; poly = poly->fNext) {
2266 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002267 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002268 }
2269 }
2270 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002271}
2272
halcanary9d524f22016-03-29 09:03:52 -07002273Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen Whitebda29c02017-03-13 15:10:13 -04002274 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
2275 VertexList* outerMesh) {
Mike Reedcf0e3c62019-12-03 16:26:15 -05002276 SkPathFillType fillType = path.getFillType();
Mike Reed7d34dc72019-11-26 12:17:17 -05002277 if (SkPathFillType_IsInverse(fillType)) {
ethannicholase9709e82016-01-07 13:34:16 -08002278 contourCnt++;
2279 }
Stephen White3a9aab92017-03-07 14:07:18 -05002280 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002281
2282 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002283 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Stephen Whitebda29c02017-03-13 15:10:13 -04002284 antialias, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002285}
2286
Stephen White11f65e02017-02-16 19:00:39 -05002287int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002288 // We could theoretically be more aggressive about not counting empty contours, but we need to
2289 // actually match the exact number of contour linked lists the tessellator will create later on.
2290 int contourCnt = 1;
2291 bool hasPoints = false;
2292
2293 SkPath::Iter iter(path, false);
2294 SkPath::Verb verb;
2295 SkPoint pts[4];
2296 bool first = true;
2297 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
2298 switch (verb) {
2299 case SkPath::kMove_Verb:
2300 if (!first) {
2301 ++contourCnt;
2302 }
2303 // fallthru.
2304 case SkPath::kLine_Verb:
2305 case SkPath::kConic_Verb:
2306 case SkPath::kQuad_Verb:
2307 case SkPath::kCubic_Verb:
2308 hasPoints = true;
2309 // fallthru to break.
2310 default:
2311 break;
2312 }
2313 first = false;
2314 }
2315 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002316 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002317 }
Stephen White11f65e02017-02-16 19:00:39 -05002318 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002319}
2320
Mike Reed7d34dc72019-11-26 12:17:17 -05002321int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002322 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002323 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002324 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002325 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2326 }
2327 }
2328 return count;
2329}
2330
Greg Danield5b45932018-06-07 13:15:10 -04002331int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2332 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002333 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2334 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2335 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2336 }
2337 }
2338 return count;
2339}
2340
Brian Osman0995fd52019-01-09 09:52:25 -05002341void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002342 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2343 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2344 Vertex* v0 = e->fTop;
2345 Vertex* v1 = e->fBottom;
2346 Vertex* v2 = e->fBottom->fPartner;
2347 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002348 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2349 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002350 }
2351 }
2352 return data;
2353}
2354
ethannicholase9709e82016-01-07 13:34:16 -08002355} // namespace
2356
2357namespace GrTessellator {
2358
2359// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2360
halcanary9d524f22016-03-29 09:03:52 -07002361int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Chris Daltond081dce2020-01-23 12:09:04 -07002362 GrEagerVertexAllocator* vertexAllocator, bool antialias, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002363 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002364 if (contourCnt <= 0) {
2365 *isLinear = true;
2366 return 0;
2367 }
Stephen White11f65e02017-02-16 19:00:39 -05002368 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002369 VertexList outerMesh;
senorblancof57372d2016-08-31 10:36:19 -07002370 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
Stephen Whitebda29c02017-03-13 15:10:13 -04002371 isLinear, &outerMesh);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002372 SkPathFillType fillType = antialias ? SkPathFillType::kWinding : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002373 int64_t count64 = count_points(polys, fillType);
Stephen Whitebda29c02017-03-13 15:10:13 -04002374 if (antialias) {
Greg Danield5b45932018-06-07 13:15:10 -04002375 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002376 }
Greg Danield5b45932018-06-07 13:15:10 -04002377 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002378 return 0;
2379 }
Greg Danield5b45932018-06-07 13:15:10 -04002380 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002381
Chris Daltond081dce2020-01-23 12:09:04 -07002382 size_t vertexStride = GetVertexStride(antialias);
2383 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002384 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002385 SkDebugf("Could not allocate vertices\n");
2386 return 0;
2387 }
senorblancof57372d2016-08-31 10:36:19 -07002388
Brian Salomon120e7d62019-09-11 10:29:22 -04002389 TESS_LOG("emitting %d verts\n", count);
Brian Osman80879d42019-01-07 16:15:27 -05002390 void* end = polys_to_triangles(polys, fillType, antialias, verts);
2391 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002392
senorblancof57372d2016-08-31 10:36:19 -07002393 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002394 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002395 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002396 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002397 return actualCount;
2398}
2399
halcanary9d524f22016-03-29 09:03:52 -07002400int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002401 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002402 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002403 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002404 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002405 return 0;
2406 }
Stephen White11f65e02017-02-16 19:00:39 -05002407 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002408 bool isLinear;
Stephen Whitebda29c02017-03-13 15:10:13 -04002409 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
2410 nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002411 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002412 int64_t count64 = count_points(polys, fillType);
2413 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002414 *verts = nullptr;
2415 return 0;
2416 }
Greg Danield5b45932018-06-07 13:15:10 -04002417 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002418
2419 *verts = new GrTessellator::WindingVertex[count];
2420 GrTessellator::WindingVertex* vertsEnd = *verts;
2421 SkPoint* points = new SkPoint[count];
2422 SkPoint* pointsEnd = points;
2423 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002424 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002425 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002426 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002427 while (start != pointsEnd) {
2428 vertsEnd->fPos = *start;
2429 vertsEnd->fWinding = poly->fWinding;
2430 ++start;
2431 ++vertsEnd;
2432 }
2433 }
2434 }
2435 int actualCount = static_cast<int>(vertsEnd - *verts);
2436 SkASSERT(actualCount <= count);
2437 SkASSERT(pointsEnd - points == actualCount);
2438 delete[] points;
2439 return actualCount;
2440}
2441
2442} // namespace