blob: 88049c8979e871d11c7a8296c865080a1fd89d32 [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"
11#include "src/gpu/GrPathUtils.h"
12#include "src/gpu/GrVertexWriter.h"
ethannicholase9709e82016-01-07 13:34:16 -080013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPath.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040015#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkGeometry.h"
17#include "src/core/SkPointPriv.h"
ethannicholase9709e82016-01-07 13:34:16 -080018
Stephen White94b7e542018-01-04 14:01:10 -050019#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040020#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040021#include <queue>
22#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040023#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080024
25/*
senorblancof57372d2016-08-31 10:36:19 -070026 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080027 *
28 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
29 * 2) Build a mesh of edges connecting the vertices (build_edges()).
30 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
31 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
32 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
33 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
34 *
senorblancof57372d2016-08-31 10:36:19 -070035 * For screenspace antialiasing, the algorithm is modified as follows:
36 *
37 * Run steps 1-5 above to produce polygons.
38 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040039 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070040 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
41 * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
Stephen Whitebda29c02017-03-13 15:10:13 -040042 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070043 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
44 *
ethannicholase9709e82016-01-07 13:34:16 -080045 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
46 * of vertices (and the necessity of inserting new vertices on intersection).
47 *
Stephen Whitebda29c02017-03-13 15:10:13 -040048 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080049 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
50 * left-to-right based on the point where both edges are active (when both top vertices
51 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
52 * (shared), it's sorted based on the last point where both edges are active, so the
53 * "upper" bottom vertex.
54 *
55 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
56 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
57 * not exact and may violate the mesh topology or active edge list ordering. We
58 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040059 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080060 *
61 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
62 * neighbouring edges at the top or bottom vertex. This is handled by merging the
63 * edges (merge_collinear_edges()).
64 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen White019f6c02017-06-09 10:06:26 -040065 * active edge list. This is handled by detecting potential violations and rewinding
Stephen White3b5a3fa2017-06-06 14:51:19 -040066 * the active edge list to the vertex before they occur (rewind() during merging,
67 * rewind_if_necessary() during splitting).
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
92#define LOG printf
93#else
94#define LOG(...)
95#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) {
Stephen White95152e12017-12-18 10:52:44 -0500212 LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
213 LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
214 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);
senorblanco49df8d12016-10-07 08:36:56 -0700320 return true;
321 }
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 Whitebda29c02017-03-13 15:10:13 -0400329 * point). For speed, that case is only tested by the callers that require it (e.g.,
Stephen White3b5a3fa2017-06-06 14:51:19 -0400330 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
ethannicholase9709e82016-01-07 13:34:16 -0800331 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
332 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
333 * a lot faster in the "not found" case.
334 *
335 * The coefficients of the line equation stored in double precision to avoid catastrphic
336 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
337 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
338 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
339 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
340 * this file).
341 */
342
343struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500344 enum class Type { kInner, kOuter, kConnector };
345 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800346 : fWinding(winding)
347 , fTop(top)
348 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500349 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800350 , fLeft(nullptr)
351 , fRight(nullptr)
352 , fPrevEdgeAbove(nullptr)
353 , fNextEdgeAbove(nullptr)
354 , fPrevEdgeBelow(nullptr)
355 , fNextEdgeBelow(nullptr)
356 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700357 , fRightPoly(nullptr)
358 , fLeftPolyPrev(nullptr)
359 , fLeftPolyNext(nullptr)
360 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700361 , fRightPolyNext(nullptr)
362 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700363 , fUsedInRightPoly(false)
364 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800365 }
366 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
367 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
368 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500369 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800370 Edge* fLeft; // The linked list of edges in the active edge list.
371 Edge* fRight; // "
372 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
373 Edge* fNextEdgeAbove; // "
374 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
375 Edge* fNextEdgeBelow; // "
376 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
377 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700378 Edge* fLeftPolyPrev;
379 Edge* fLeftPolyNext;
380 Edge* fRightPolyPrev;
381 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700382 bool fUsedInLeftPoly;
383 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700384 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800385 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700386 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800387 }
388 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700389 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800390 }
391 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700392 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800393 }
394 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700395 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800396 }
Stephen White95152e12017-12-18 10:52:44 -0500397 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
ethannicholase9709e82016-01-07 13:34:16 -0800398 LOG("intersecting %g -> %g with %g -> %g\n",
399 fTop->fID, fBottom->fID,
400 other.fTop->fID, other.fBottom->fID);
401 if (fTop == other.fTop || fBottom == other.fBottom) {
402 return false;
403 }
senorblanco49df8d12016-10-07 08:36:56 -0700404 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800405 if (denom == 0.0) {
406 return false;
407 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500408 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
409 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
410 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
411 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800412 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
413 // This saves us doing the divide below unless absolutely necessary.
414 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
415 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
416 return false;
417 }
418 double s = sNumer / denom;
419 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700420 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
421 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500422 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500423 if (fType == Type::kConnector) {
424 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
425 } else if (other.fType == Type::kConnector) {
426 double t = tNumer / denom;
427 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500428 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
429 *alpha = 0;
430 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500431 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500432 }
433 }
ethannicholase9709e82016-01-07 13:34:16 -0800434 return true;
435 }
senorblancof57372d2016-08-31 10:36:19 -0700436};
437
Stephen Whitec4dbc372019-05-22 10:50:14 -0400438struct SSEdge;
439
440struct SSVertex {
441 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
442 Vertex* fVertex;
443 SSEdge* fPrev;
444 SSEdge* fNext;
445};
446
447struct SSEdge {
448 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
449 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
450 }
451 Edge* fEdge;
452 Event* fEvent;
453 SSVertex* fPrev;
454 SSVertex* fNext;
455};
456
457typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
458typedef std::vector<SSEdge*> SSEdgeList;
459
senorblancof57372d2016-08-31 10:36:19 -0700460struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500461 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700462 Edge* fHead;
463 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700464 void insert(Edge* edge, Edge* prev, Edge* next) {
465 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700466 }
467 void append(Edge* e) {
468 insert(e, fTail, nullptr);
469 }
470 void remove(Edge* edge) {
471 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700472 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400473 void removeAll() {
474 while (fHead) {
475 this->remove(fHead);
476 }
477 }
senorblancof57372d2016-08-31 10:36:19 -0700478 void close() {
479 if (fHead && fTail) {
480 fTail->fRight = fHead;
481 fHead->fLeft = fTail;
482 }
483 }
484 bool contains(Edge* edge) const {
485 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800486 }
487};
488
Stephen Whitec4dbc372019-05-22 10:50:14 -0400489struct EventList;
490
Stephen Whitee260c462017-12-19 18:09:54 -0500491struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400492 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
493 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500494 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400495 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500496 SkPoint fPoint;
497 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400498 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500499};
500
Stephen Whitec4dbc372019-05-22 10:50:14 -0400501struct EventComparator {
502 enum class Op { kLessThan, kGreaterThan };
503 EventComparator(Op op) : fOp(op) {}
504 bool operator() (Event* const &e1, Event* const &e2) {
505 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
506 : e1->fAlpha > e2->fAlpha;
507 }
508 Op fOp;
509};
Stephen Whitee260c462017-12-19 18:09:54 -0500510
Stephen Whitec4dbc372019-05-22 10:50:14 -0400511typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500512
Stephen Whitec4dbc372019-05-22 10:50:14 -0400513struct EventList : EventPQ {
514 EventList(EventComparator comparison) : EventPQ(comparison) {
515 }
516};
517
518void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
519 Vertex* prev = e->fPrev->fVertex;
520 Vertex* next = e->fNext->fVertex;
521 if (prev == next || !prev->fPartner || !next->fPartner) {
522 return;
523 }
524 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
525 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500526 SkPoint p;
527 uint8_t alpha;
528 if (bisector1.intersect(bisector2, &p, &alpha)) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400529 LOG("found edge event for %g, %g (original %g -> %g), will collapse to %g,%g alpha %d\n",
530 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY, alpha);
531 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)) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400553 LOG("found p edge event for %g, %g (original %g -> %g), will collapse to %g,%g alpha %d\n",
Stephen White8a3c0592019-05-29 11:26:16 -0400554 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400555 edge->fEvent = alloc.make<Event>(edge, p, alpha);
556 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500557 }
558}
Stephen Whitee260c462017-12-19 18:09:54 -0500559
ethannicholase9709e82016-01-07 13:34:16 -0800560/***************************************************************************************/
561
562struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700563 Poly(Vertex* v, int winding)
564 : fFirstVertex(v)
565 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800566 , fHead(nullptr)
567 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800568 , fNext(nullptr)
569 , fPartner(nullptr)
570 , fCount(0)
571 {
572#if LOGGING_ENABLED
573 static int gID = 0;
574 fID = gID++;
575 LOG("*** created Poly %d\n", fID);
576#endif
577 }
senorblanco531237e2016-06-02 11:36:48 -0700578 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800579 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700580 MonotonePoly(Edge* edge, Side side)
581 : fSide(side)
582 , fFirstEdge(nullptr)
583 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800584 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700585 , fNext(nullptr) {
586 this->addEdge(edge);
587 }
ethannicholase9709e82016-01-07 13:34:16 -0800588 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700589 Edge* fFirstEdge;
590 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800591 MonotonePoly* fPrev;
592 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700593 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800594 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700595 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700596 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
597 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700598 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800599 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700600 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700601 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
602 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700603 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800604 }
ethannicholase9709e82016-01-07 13:34:16 -0800605 }
606
Brian Osman0995fd52019-01-09 09:52:25 -0500607 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700608 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700609 VertexList vertices;
610 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500611 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700612 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700613 if (kRight_Side == fSide) {
614 vertices.append(e->fBottom);
615 e = e->fRightPolyNext;
616 } else {
617 vertices.prepend(e->fBottom);
618 e = e->fLeftPolyNext;
619 }
Stephen White651cbe92017-03-03 12:24:16 -0500620 count++;
senorblanco531237e2016-06-02 11:36:48 -0700621 }
622 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800623 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700624 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800625 SkASSERT(v && v->fPrev && v->fNext);
626 Vertex* prev = v->fPrev;
627 Vertex* curr = v;
628 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500629 if (count == 3) {
Brian Osman0995fd52019-01-09 09:52:25 -0500630 return emit_triangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500631 }
ethannicholase9709e82016-01-07 13:34:16 -0800632 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
633 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
634 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
635 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
636 if (ax * by - ay * bx >= 0.0) {
Brian Osman0995fd52019-01-09 09:52:25 -0500637 data = emit_triangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800638 v->fPrev->fNext = v->fNext;
639 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500640 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800641 if (v->fPrev == first) {
642 v = v->fNext;
643 } else {
644 v = v->fPrev;
645 }
646 } else {
647 v = v->fNext;
648 }
649 }
650 return data;
651 }
652 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500653 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
senorblanco70f52512016-08-17 14:56:22 -0700654 LOG("addEdge (%g -> %g) to poly %d, %s side\n",
655 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800656 Poly* partner = fPartner;
657 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700658 if (side == kRight_Side) {
659 if (e->fUsedInRightPoly) {
660 return this;
661 }
662 } else {
663 if (e->fUsedInLeftPoly) {
664 return this;
665 }
666 }
ethannicholase9709e82016-01-07 13:34:16 -0800667 if (partner) {
668 fPartner = partner->fPartner = nullptr;
669 }
senorblanco531237e2016-06-02 11:36:48 -0700670 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500671 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700672 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700673 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
674 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700675 } else if (side == fTail->fSide) {
676 fTail->addEdge(e);
677 fCount++;
678 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500679 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700680 fTail->addEdge(e);
681 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800682 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700683 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800684 poly = partner;
685 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500686 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700687 m->fPrev = fTail;
688 fTail->fNext = m;
689 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800690 }
691 }
ethannicholase9709e82016-01-07 13:34:16 -0800692 return poly;
693 }
Brian Osman0995fd52019-01-09 09:52:25 -0500694 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800695 if (fCount < 3) {
696 return data;
697 }
698 LOG("emit() %d, size %d\n", fID, fCount);
699 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500700 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800701 }
702 return data;
703 }
senorblanco531237e2016-06-02 11:36:48 -0700704 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
705 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800706 int fWinding;
707 MonotonePoly* fHead;
708 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800709 Poly* fNext;
710 Poly* fPartner;
711 int fCount;
712#if LOGGING_ENABLED
713 int fID;
714#endif
715};
716
717/***************************************************************************************/
718
719bool coincident(const SkPoint& a, const SkPoint& b) {
720 return a == b;
721}
722
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500723Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
724 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800725 poly->fNext = *head;
726 *head = poly;
727 return poly;
728}
729
Stephen White3a9aab92017-03-07 14:07:18 -0500730void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500731 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800732#if LOGGING_ENABLED
733 static float gID = 0.0f;
734 v->fID = gID++;
735#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500736 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800737}
738
Stephen White36e4f062017-03-27 16:11:31 -0400739SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
740 SkQuadCoeff quad(pts);
741 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
742 SkPoint mid = to_point(quad.eval(t));
743 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400744 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
745 return 0;
746 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500747 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400748}
749
750void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
751 SkArenaAlloc& alloc) {
752 SkQuadCoeff quad(pts);
753 Sk2s aa = quad.fA * quad.fA;
754 SkScalar denom = 2.0f * (aa[0] + aa[1]);
755 Sk2s ab = quad.fA * quad.fB;
756 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
757 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500758 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400759 // Test possible subdivision values only at the point of maximum curvature.
760 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500761 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400762 u = 1.0f / nPoints;
763 if (quad_error_at(pts, t, u) < toleranceSqd) {
764 break;
765 }
766 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800767 }
Stephen White36e4f062017-03-27 16:11:31 -0400768 for (int j = 1; j <= nPoints; j++) {
769 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
770 }
ethannicholase9709e82016-01-07 13:34:16 -0800771}
772
Stephen White3a9aab92017-03-07 14:07:18 -0500773void generate_cubic_points(const SkPoint& p0,
774 const SkPoint& p1,
775 const SkPoint& p2,
776 const SkPoint& p3,
777 SkScalar tolSqd,
778 VertexList* contour,
779 int pointsLeft,
780 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500781 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
782 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800783 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
784 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500785 append_point_to_contour(p3, contour, alloc);
786 return;
ethannicholase9709e82016-01-07 13:34:16 -0800787 }
788 const SkPoint q[] = {
789 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
790 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
791 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
792 };
793 const SkPoint r[] = {
794 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
795 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
796 };
797 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
798 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500799 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
800 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800801}
802
803// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
804
805void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500806 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800807 SkScalar toleranceSqd = tolerance * tolerance;
808
809 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800810 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500811 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800812 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800813 if (path.isInverseFillType()) {
814 SkPoint quad[4];
815 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700816 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500817 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800818 }
Stephen White3a9aab92017-03-07 14:07:18 -0500819 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800820 }
821 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500822 SkPath::Verb verb;
Stephen White2cee2832017-08-23 09:37:16 -0400823 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800824 switch (verb) {
825 case SkPath::kConic_Verb: {
826 SkScalar weight = iter.conicWeight();
827 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
828 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400829 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800830 quadPts += 2;
831 }
832 *isLinear = false;
833 break;
834 }
835 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500836 if (contour->fHead) {
837 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800838 }
Stephen White3a9aab92017-03-07 14:07:18 -0500839 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800840 break;
841 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500842 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800843 break;
844 }
845 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400846 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800847 *isLinear = false;
848 break;
849 }
850 case SkPath::kCubic_Verb: {
851 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500852 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
853 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800854 *isLinear = false;
855 break;
856 }
857 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800858 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800859 break;
860 }
861 }
862}
863
Stephen White49789062017-02-21 10:35:49 -0500864inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800865 switch (fillType) {
866 case SkPath::kWinding_FillType:
867 return winding != 0;
868 case SkPath::kEvenOdd_FillType:
869 return (winding & 1) != 0;
870 case SkPath::kInverseWinding_FillType:
senorblanco7ab96e92016-10-12 06:47:44 -0700871 return winding == 1;
ethannicholase9709e82016-01-07 13:34:16 -0800872 case SkPath::kInverseEvenOdd_FillType:
873 return (winding & 1) == 1;
874 default:
875 SkASSERT(false);
876 return false;
877 }
878}
879
Stephen White49789062017-02-21 10:35:49 -0500880inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
881 return poly && apply_fill_type(fillType, poly->fWinding);
882}
883
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500884Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500885 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800886 Vertex* top = winding < 0 ? next : prev;
887 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500888 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800889}
890
891void remove_edge(Edge* edge, EdgeList* edges) {
892 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700893 SkASSERT(edges->contains(edge));
894 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800895}
896
897void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
898 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700899 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800900 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700901 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800902}
903
904void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500905 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800906 *left = v->fFirstEdgeAbove->fLeft;
907 *right = v->fLastEdgeAbove->fRight;
908 return;
909 }
910 Edge* next = nullptr;
911 Edge* prev;
912 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
913 if (prev->isLeftOf(v)) {
914 break;
915 }
916 next = prev;
917 }
918 *left = prev;
919 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800920}
921
ethannicholase9709e82016-01-07 13:34:16 -0800922void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
923 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500924 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800925 return;
926 }
927 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
928 Edge* prev = nullptr;
929 Edge* next;
930 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
931 if (next->isRightOf(edge->fTop)) {
932 break;
933 }
934 prev = next;
935 }
senorblancoe6eaa322016-03-08 09:06:44 -0800936 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800937 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
938}
939
940void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
941 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500942 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800943 return;
944 }
945 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
946 Edge* prev = nullptr;
947 Edge* next;
948 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
949 if (next->isRightOf(edge->fBottom)) {
950 break;
951 }
952 prev = next;
953 }
senorblancoe6eaa322016-03-08 09:06:44 -0800954 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800955 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
956}
957
958void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400959 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800960 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
961 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800962 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800963 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
964}
965
966void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400967 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800968 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
969 edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800970 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800971 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
972}
973
Stephen Whitee7a364d2017-01-11 16:19:26 -0500974void disconnect(Edge* edge)
975{
ethannicholase9709e82016-01-07 13:34:16 -0800976 remove_edge_above(edge);
977 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500978}
979
Stephen White3b5a3fa2017-06-06 14:51:19 -0400980void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
981
982void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
983 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
984 return;
985 }
986 Vertex* v = *current;
987 LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
988 while (v != dst) {
989 v = v->fPrev;
990 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
991 remove_edge(e, activeEdges);
992 }
993 Edge* leftEdge = v->fLeftEnclosingEdge;
994 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
995 insert_edge(e, leftEdge, activeEdges);
996 leftEdge = e;
997 }
998 }
999 *current = v;
1000}
1001
1002void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
1003 if (!activeEdges || !current) {
1004 return;
1005 }
1006 Vertex* top = edge->fTop;
1007 Vertex* bottom = edge->fBottom;
1008 if (edge->fLeft) {
1009 Vertex* leftTop = edge->fLeft->fTop;
1010 Vertex* leftBottom = edge->fLeft->fBottom;
1011 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
1012 rewind(activeEdges, current, leftTop, c);
1013 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
1014 rewind(activeEdges, current, top, c);
1015 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
1016 !edge->fLeft->isLeftOf(bottom)) {
1017 rewind(activeEdges, current, leftTop, c);
1018 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
1019 rewind(activeEdges, current, top, c);
1020 }
1021 }
1022 if (edge->fRight) {
1023 Vertex* rightTop = edge->fRight->fTop;
1024 Vertex* rightBottom = edge->fRight->fBottom;
1025 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
1026 rewind(activeEdges, current, rightTop, c);
1027 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
1028 rewind(activeEdges, current, top, c);
1029 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1030 !edge->fRight->isRightOf(bottom)) {
1031 rewind(activeEdges, current, rightTop, c);
1032 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1033 !edge->isLeftOf(rightBottom)) {
1034 rewind(activeEdges, current, top, c);
1035 }
ethannicholase9709e82016-01-07 13:34:16 -08001036 }
1037}
1038
Stephen White3b5a3fa2017-06-06 14:51:19 -04001039void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001040 remove_edge_below(edge);
1041 edge->fTop = v;
1042 edge->recompute();
1043 insert_edge_below(edge, v, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001044 rewind_if_necessary(edge, activeEdges, current, c);
1045 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001046}
1047
Stephen White3b5a3fa2017-06-06 14:51:19 -04001048void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001049 remove_edge_above(edge);
1050 edge->fBottom = v;
1051 edge->recompute();
1052 insert_edge_above(edge, v, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001053 rewind_if_necessary(edge, activeEdges, current, c);
1054 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001055}
1056
Stephen White3b5a3fa2017-06-06 14:51:19 -04001057void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1058 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001059 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
1060 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1061 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1062 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001063 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001064 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001065 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001066 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001067 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001068 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001069 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001070 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001071 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001072 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001073 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001074 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001075 }
1076}
1077
Stephen White3b5a3fa2017-06-06 14:51:19 -04001078void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1079 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001080 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
1081 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1082 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1083 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001084 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001085 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001086 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001087 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001088 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001089 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001090 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001091 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001092 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001093 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001094 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001095 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001096 }
1097}
1098
Stephen Whited26b4d82018-07-26 10:02:27 -04001099bool top_collinear(Edge* left, Edge* right) {
1100 if (!left || !right) {
1101 return false;
1102 }
1103 return left->fTop->fPoint == right->fTop->fPoint ||
1104 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1105}
1106
1107bool bottom_collinear(Edge* left, Edge* right) {
1108 if (!left || !right) {
1109 return false;
1110 }
1111 return left->fBottom->fPoint == right->fBottom->fPoint ||
1112 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1113}
1114
Stephen White3b5a3fa2017-06-06 14:51:19 -04001115void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001116 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001117 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001118 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001119 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001120 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001121 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001122 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001123 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001124 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001125 } else {
1126 break;
1127 }
ethannicholase9709e82016-01-07 13:34:16 -08001128 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001129 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1130 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1131 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1132 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001133}
1134
Stephen White89042d52018-06-08 12:18:22 -04001135bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001136 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001137 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001138 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001139 }
ethannicholase9709e82016-01-07 13:34:16 -08001140 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1141 edge->fTop->fID, edge->fBottom->fID,
1142 v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001143 Vertex* top;
1144 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001145 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001146 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001147 top = v;
1148 bottom = edge->fTop;
1149 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001150 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001151 top = edge->fBottom;
1152 bottom = v;
1153 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001154 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001155 top = v;
1156 bottom = edge->fBottom;
1157 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001158 }
Stephen White531a48e2018-06-01 09:49:39 -04001159 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001160 insert_edge_below(newEdge, top, c);
1161 insert_edge_above(newEdge, bottom, c);
1162 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001163 return true;
1164}
1165
1166bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1167 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1168 return false;
1169 }
Stephen White1c5fd182018-07-12 15:54:05 -04001170 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1171 return false;
1172 }
Stephen White89042d52018-06-08 12:18:22 -04001173 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1174 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001175 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001176 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1177 }
1178 } else {
1179 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001180 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001181 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1182 }
1183 }
1184 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1185 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001186 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001187 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1188 }
1189 } else {
1190 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001191 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001192 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1193 }
1194 }
1195 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001196}
1197
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001198Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001199 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001200 if (!prev || !next || prev->fPoint == next->fPoint) {
1201 return nullptr;
1202 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001203 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001204 insert_edge_below(edge, edge->fTop, c);
1205 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001206 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001207 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001208 return edge;
1209}
1210
Stephen Whitebf6137e2017-01-04 15:43:26 -05001211void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001212 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001213 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1214 src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001215 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001216 if (src->fPartner) {
1217 src->fPartner->fPartner = dst;
1218 }
Stephen White7b376942018-05-22 11:51:32 -04001219 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001220 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001221 }
Stephen White7b376942018-05-22 11:51:32 -04001222 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001223 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001224 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001225 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001226 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001227}
1228
Stephen White95152e12017-12-18 10:52:44 -05001229Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1230 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1231 Vertex* prevV = reference;
1232 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1233 prevV = prevV->fPrev;
1234 }
1235 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1236 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1237 prevV = nextV;
1238 nextV = nextV->fNext;
1239 }
1240 Vertex* v;
1241 if (prevV && coincident(prevV->fPoint, p)) {
1242 v = prevV;
1243 } else if (nextV && coincident(nextV->fPoint, p)) {
1244 v = nextV;
1245 } else {
1246 v = alloc.make<Vertex>(p, alpha);
1247#if LOGGING_ENABLED
1248 if (!prevV) {
1249 v->fID = mesh->fHead->fID - 1.0f;
1250 } else if (!nextV) {
1251 v->fID = mesh->fTail->fID + 1.0f;
1252 } else {
1253 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1254 }
1255#endif
1256 mesh->insert(v, prevV, nextV);
1257 }
1258 return v;
1259}
1260
Stephen White53a02982018-05-30 22:47:46 -04001261// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1262// sort criterion, it may not be possible to split correctly, since there is no point which is
1263// below the top and above the bottom. This function detects that case.
1264bool nearly_flat(Comparator& c, Edge* edge) {
1265 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1266 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001267 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001268}
1269
Stephen Whitee62999f2018-06-05 18:45:07 -04001270SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1271 if (c.sweep_lt(p, min)) {
1272 return min;
1273 } else if (c.sweep_lt(max, p)) {
1274 return max;
1275 } else {
1276 return p;
1277 }
1278}
1279
Stephen Whitec4dbc372019-05-22 10:50:14 -04001280void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1281 Line line1 = edge1->fLine;
1282 Line line2 = edge2->fLine;
1283 line1.normalize();
1284 line2.normalize();
1285 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1286 if (cosAngle > 0.999) {
1287 return;
1288 }
1289 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1290 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1291 SkPoint p;
1292 if (line1.intersect(line2, &p)) {
1293 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1294 v->fPartner = alloc.make<Vertex>(p, alpha);
1295 LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID);
1296 }
1297}
1298
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001299bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001300 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001301 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001302 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001303 }
Stephen White56158ae2017-01-30 14:31:31 -05001304 SkPoint p;
1305 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001306 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001307 Vertex* v;
ethannicholase9709e82016-01-07 13:34:16 -08001308 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001309 Vertex* top = *current;
1310 // If the intersection point is above the current vertex, rewind to the vertex above the
1311 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001312 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001313 top = top->fPrev;
1314 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001315 if (!nearly_flat(c, left)) {
1316 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001317 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001318 if (!nearly_flat(c, right)) {
1319 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001320 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001321 if (p == left->fTop->fPoint) {
1322 v = left->fTop;
1323 } else if (p == left->fBottom->fPoint) {
1324 v = left->fBottom;
1325 } else if (p == right->fTop->fPoint) {
1326 v = right->fTop;
1327 } else if (p == right->fBottom->fPoint) {
1328 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001329 } else {
Stephen White95152e12017-12-18 10:52:44 -05001330 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001331 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001332 v->fSynthetic = true;
1333 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001334 }
ethannicholase9709e82016-01-07 13:34:16 -08001335 }
Stephen White0cb31672017-06-08 14:41:01 -04001336 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001337 split_edge(left, v, activeEdges, current, c, alloc);
1338 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001339 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001340 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001341 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001342 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001343}
1344
Stephen White3a9aab92017-03-07 14:07:18 -05001345void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
1346 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1347 SkASSERT(contour->fHead);
1348 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001349 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001350 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001351 }
Stephen White3a9aab92017-03-07 14:07:18 -05001352 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001353 if (approximate) {
1354 round(&v->fPoint);
1355 }
Stephen White3a9aab92017-03-07 14:07:18 -05001356 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001357 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001358 if (coincident(prev->fPoint, v->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -08001359 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001360 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001361 } else if (!v->fPoint.isFinite()) {
1362 LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
1363 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001364 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Stephen White06768ca2018-05-25 14:50:56 -04001365 LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
1366 contour->remove(v);
1367 } else {
1368 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001369 }
Stephen White3a9aab92017-03-07 14:07:18 -05001370 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001371 }
1372 }
1373}
1374
Stephen Whitee260c462017-12-19 18:09:54 -05001375bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001376 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001377 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001378 }
Stephen Whitee260c462017-12-19 18:09:54 -05001379 bool merged = false;
1380 for (Vertex* v = mesh->fHead->fNext; v;) {
1381 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001382 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1383 v->fPoint = v->fPrev->fPoint;
1384 }
1385 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001386 merge_vertices(v, v->fPrev, mesh, c, alloc);
1387 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001388 }
Stephen Whitee260c462017-12-19 18:09:54 -05001389 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001390 }
Stephen Whitee260c462017-12-19 18:09:54 -05001391 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001392}
1393
1394// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1395
Stephen White3a9aab92017-03-07 14:07:18 -05001396void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001397 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001398 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1399 Vertex* prev = contour->fTail;
1400 for (Vertex* v = contour->fHead; v;) {
1401 Vertex* next = v->fNext;
1402 connect(prev, v, Edge::Type::kInner, c, alloc);
1403 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001404 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001405 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001406 }
1407 }
ethannicholase9709e82016-01-07 13:34:16 -08001408}
1409
Stephen Whitee260c462017-12-19 18:09:54 -05001410void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1411 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001412 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001413 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1414 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1415 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1416 // number.
1417 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1418 inner->fPartner = outer->fPartner = nullptr;
1419 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001420 }
1421 }
1422}
1423
1424template <CompareFunc sweep_lt>
1425void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1426 Vertex* a = front->fHead;
1427 Vertex* b = back->fHead;
1428 while (a && b) {
1429 if (sweep_lt(a->fPoint, b->fPoint)) {
1430 front->remove(a);
1431 result->append(a);
1432 a = front->fHead;
1433 } else {
1434 back->remove(b);
1435 result->append(b);
1436 b = back->fHead;
1437 }
1438 }
1439 result->append(*front);
1440 result->append(*back);
1441}
1442
1443void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1444 if (c.fDirection == Comparator::Direction::kHorizontal) {
1445 sorted_merge<sweep_lt_horiz>(front, back, result);
1446 } else {
1447 sorted_merge<sweep_lt_vert>(front, back, result);
1448 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001449#if LOGGING_ENABLED
1450 float id = 0.0f;
1451 for (Vertex* v = result->fHead; v; v = v->fNext) {
1452 v->fID = id++;
1453 }
1454#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001455}
1456
ethannicholase9709e82016-01-07 13:34:16 -08001457// Stage 3: sort the vertices by increasing sweep direction.
1458
Stephen White16a40cb2017-02-23 11:10:01 -05001459template <CompareFunc sweep_lt>
1460void merge_sort(VertexList* vertices) {
1461 Vertex* slow = vertices->fHead;
1462 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001463 return;
1464 }
Stephen White16a40cb2017-02-23 11:10:01 -05001465 Vertex* fast = slow->fNext;
1466 if (!fast) {
1467 return;
1468 }
1469 do {
1470 fast = fast->fNext;
1471 if (fast) {
1472 fast = fast->fNext;
1473 slow = slow->fNext;
1474 }
1475 } while (fast);
1476 VertexList front(vertices->fHead, slow);
1477 VertexList back(slow->fNext, vertices->fTail);
1478 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001479
Stephen White16a40cb2017-02-23 11:10:01 -05001480 merge_sort<sweep_lt>(&front);
1481 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001482
Stephen White16a40cb2017-02-23 11:10:01 -05001483 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001484 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001485}
1486
Stephen White95152e12017-12-18 10:52:44 -05001487void dump_mesh(const VertexList& mesh) {
1488#if LOGGING_ENABLED
1489 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
1490 LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1491 if (Vertex* p = v->fPartner) {
1492 LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
1493 } else {
1494 LOG(", null partner\n");
1495 }
1496 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1497 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1498 }
1499 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1500 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1501 }
1502 }
1503#endif
1504}
1505
Stephen Whitec4dbc372019-05-22 10:50:14 -04001506void dump_skel(const SSEdgeList& ssEdges) {
1507#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001508 for (SSEdge* edge : ssEdges) {
1509 if (edge->fEdge) {
Stephen White8a3c0592019-05-29 11:26:16 -04001510 LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001511 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001512 edge->fNext->fVertex->fID);
1513 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
1514 LOG(" (original %g -> %g)\n",
1515 edge->fEdge->fTop->fID,
1516 edge->fEdge->fBottom->fID);
1517 } else {
1518 LOG("\n");
1519 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001520 }
1521 }
1522#endif
1523}
1524
Stephen White89042d52018-06-08 12:18:22 -04001525#ifdef SK_DEBUG
1526void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1527 if (!left || !right) {
1528 return;
1529 }
1530 if (left->fTop == right->fTop) {
1531 SkASSERT(left->isLeftOf(right->fBottom));
1532 SkASSERT(right->isRightOf(left->fBottom));
1533 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1534 SkASSERT(left->isLeftOf(right->fTop));
1535 } else {
1536 SkASSERT(right->isRightOf(left->fTop));
1537 }
1538 if (left->fBottom == right->fBottom) {
1539 SkASSERT(left->isLeftOf(right->fTop));
1540 SkASSERT(right->isRightOf(left->fTop));
1541 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1542 SkASSERT(left->isLeftOf(right->fBottom));
1543 } else {
1544 SkASSERT(right->isRightOf(left->fBottom));
1545 }
1546}
1547
1548void validate_edge_list(EdgeList* edges, Comparator& c) {
1549 Edge* left = edges->fHead;
1550 if (!left) {
1551 return;
1552 }
1553 for (Edge* right = left->fRight; right; right = right->fRight) {
1554 validate_edge_pair(left, right, c);
1555 left = right;
1556 }
1557}
1558#endif
1559
ethannicholase9709e82016-01-07 13:34:16 -08001560// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1561
Stephen Whitec4dbc372019-05-22 10:50:14 -04001562bool connected(Vertex* v) {
1563 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1564}
1565
Stephen Whitee260c462017-12-19 18:09:54 -05001566bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001567 LOG("simplifying complex polygons\n");
1568 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001569 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001570 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001571 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001572 continue;
1573 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001574 Edge* leftEnclosingEdge;
1575 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001576 bool restartChecks;
1577 do {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001578 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001579 restartChecks = false;
1580 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001581 v->fLeftEnclosingEdge = leftEnclosingEdge;
1582 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001583 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001584 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001585 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001586 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001587 restartChecks = true;
1588 break;
1589 }
Stephen White0cb31672017-06-08 14:41:01 -04001590 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001591 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001592 restartChecks = true;
1593 break;
1594 }
1595 }
1596 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001597 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001598 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001599 restartChecks = true;
1600 }
1601
1602 }
Stephen Whitee260c462017-12-19 18:09:54 -05001603 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001604 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001605#ifdef SK_DEBUG
1606 validate_edge_list(&activeEdges, c);
1607#endif
ethannicholase9709e82016-01-07 13:34:16 -08001608 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1609 remove_edge(e, &activeEdges);
1610 }
1611 Edge* leftEdge = leftEnclosingEdge;
1612 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1613 insert_edge(e, leftEdge, &activeEdges);
1614 leftEdge = e;
1615 }
ethannicholase9709e82016-01-07 13:34:16 -08001616 }
Stephen Whitee260c462017-12-19 18:09:54 -05001617 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1618 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001619}
1620
1621// Stage 5: Tessellate the simplified mesh into monotone polygons.
1622
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001623Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001624 LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001625 EdgeList activeEdges;
1626 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001627 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001628 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001629 continue;
1630 }
1631#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001632 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001633#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001634 Edge* leftEnclosingEdge;
1635 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001636 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001637 Poly* leftPoly;
1638 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001639 if (v->fFirstEdgeAbove) {
1640 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1641 rightPoly = v->fLastEdgeAbove->fRightPoly;
1642 } else {
1643 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1644 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1645 }
1646#if LOGGING_ENABLED
1647 LOG("edges above:\n");
1648 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1649 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1650 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1651 }
1652 LOG("edges below:\n");
1653 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1654 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1655 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1656 }
1657#endif
1658 if (v->fFirstEdgeAbove) {
1659 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001660 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001661 }
1662 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001663 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001664 }
1665 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001666 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001667 remove_edge(e, &activeEdges);
1668 if (e->fRightPoly) {
1669 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001670 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001671 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001672 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001673 }
1674 }
1675 remove_edge(v->fLastEdgeAbove, &activeEdges);
1676 if (!v->fFirstEdgeBelow) {
1677 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1678 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1679 rightPoly->fPartner = leftPoly;
1680 leftPoly->fPartner = rightPoly;
1681 }
1682 }
1683 }
1684 if (v->fFirstEdgeBelow) {
1685 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001686 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001687 if (leftPoly == rightPoly) {
1688 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1689 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1690 leftPoly->fWinding, alloc);
1691 leftEnclosingEdge->fRightPoly = leftPoly;
1692 } else {
1693 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1694 rightPoly->fWinding, alloc);
1695 rightEnclosingEdge->fLeftPoly = rightPoly;
1696 }
ethannicholase9709e82016-01-07 13:34:16 -08001697 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001698 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001699 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1700 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001701 }
1702 }
1703 Edge* leftEdge = v->fFirstEdgeBelow;
1704 leftEdge->fLeftPoly = leftPoly;
1705 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1706 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1707 rightEdge = rightEdge->fNextEdgeBelow) {
1708 insert_edge(rightEdge, leftEdge, &activeEdges);
1709 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1710 winding += leftEdge->fWinding;
1711 if (winding != 0) {
1712 Poly* poly = new_poly(&polys, v, winding, alloc);
1713 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1714 }
1715 leftEdge = rightEdge;
1716 }
1717 v->fLastEdgeBelow->fRightPoly = rightPoly;
1718 }
1719#if LOGGING_ENABLED
1720 LOG("\nactive edges:\n");
1721 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1722 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1723 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1724 }
1725#endif
1726 }
1727 return polys;
1728}
1729
Stephen Whitebf6137e2017-01-04 15:43:26 -05001730void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001731 SkArenaAlloc& alloc) {
Stephen White49789062017-02-21 10:35:49 -05001732 LOG("removing non-boundary edges\n");
1733 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001734 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001735 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001736 continue;
1737 }
1738 Edge* leftEnclosingEdge;
1739 Edge* rightEnclosingEdge;
1740 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1741 bool prevFilled = leftEnclosingEdge &&
1742 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1743 for (Edge* e = v->fFirstEdgeAbove; e;) {
1744 Edge* next = e->fNextEdgeAbove;
1745 remove_edge(e, &activeEdges);
1746 bool filled = apply_fill_type(fillType, e->fWinding);
1747 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001748 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001749 }
Stephen White49789062017-02-21 10:35:49 -05001750 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001751 e = next;
1752 }
Stephen White49789062017-02-21 10:35:49 -05001753 Edge* prev = leftEnclosingEdge;
1754 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1755 if (prev) {
1756 e->fWinding += prev->fWinding;
1757 }
1758 insert_edge(e, prev, &activeEdges);
1759 prev = e;
1760 }
senorblancof57372d2016-08-31 10:36:19 -07001761 }
senorblancof57372d2016-08-31 10:36:19 -07001762}
1763
Stephen White66412122017-03-01 11:48:27 -05001764// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001765void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001766 normal->set(SkDoubleToScalar(e->fLine.fA),
1767 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001768}
1769
1770// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1771// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1772// invert on stroking.
1773
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001774void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001775 Edge* prevEdge = boundary->fTail;
1776 SkVector prevNormal;
1777 get_edge_normal(prevEdge, &prevNormal);
1778 for (Edge* e = boundary->fHead; e != nullptr;) {
1779 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1780 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001781 double distPrev = e->dist(prev->fPoint);
1782 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001783 SkVector normal;
1784 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001785 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001786 if (prev == next) {
1787 remove_edge(prevEdge, boundary);
1788 remove_edge(e, boundary);
1789 prevEdge = boundary->fTail;
1790 e = boundary->fHead;
1791 if (prevEdge) {
1792 get_edge_normal(prevEdge, &prevNormal);
1793 }
1794 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001795 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001796 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001797 if (prev->fPoint != next->fPoint) {
1798 join->fLine.normalize();
1799 join->fLine = join->fLine * join->fWinding;
1800 }
senorblancof57372d2016-08-31 10:36:19 -07001801 insert_edge(join, e, boundary);
1802 remove_edge(prevEdge, boundary);
1803 remove_edge(e, boundary);
1804 if (join->fLeft && join->fRight) {
1805 prevEdge = join->fLeft;
1806 e = join;
1807 } else {
1808 prevEdge = boundary->fTail;
1809 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1810 }
1811 get_edge_normal(prevEdge, &prevNormal);
1812 } else {
1813 prevEdge = e;
1814 prevNormal = normal;
1815 e = e->fRight;
1816 }
1817 }
1818}
1819
Stephen Whitec4dbc372019-05-22 10:50:14 -04001820void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1821 if (v == dest) {
1822 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001823 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001824 LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
1825 if (v->fSynthetic) {
1826 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1827 } else if (v->fPartner) {
1828 LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1829 LOG("and %g's partner to null\n", v->fID);
1830 v->fPartner->fPartner = dest;
1831 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001832 }
1833}
1834
Stephen Whitec4dbc372019-05-22 10:50:14 -04001835void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1836 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001837 return;
1838 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001839 Vertex* prev = fEdge->fPrev->fVertex;
1840 Vertex* next = fEdge->fNext->fVertex;
1841 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1842 SSEdge* nextEdge = fEdge->fNext->fNext;
1843 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1844 return;
Stephen White77169c82018-06-05 09:15:59 -04001845 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001846 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1847 dest->fSynthetic = true;
1848 SSVertex* ssv = alloc.make<SSVertex>(dest);
1849 LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1850 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID,
1851 dest->fID, fPoint.fX, fPoint.fY, fAlpha);
1852 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001853
Stephen Whitec4dbc372019-05-22 10:50:14 -04001854 ss_connect(prev, dest, c, alloc);
1855 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001856
Stephen Whitec4dbc372019-05-22 10:50:14 -04001857 prevEdge->fNext = nextEdge->fPrev = ssv;
1858 ssv->fPrev = prevEdge;
1859 ssv->fNext = nextEdge;
1860 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1861 return;
1862 }
1863 if (prevEdge->fEvent) {
1864 prevEdge->fEvent->fEdge = nullptr;
1865 }
1866 if (nextEdge->fEvent) {
1867 nextEdge->fEvent->fEdge = nullptr;
1868 }
1869 if (prevEdge->fPrev == nextEdge->fNext) {
1870 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1871 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1872 } else {
1873 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1874 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1875 if (dest->fPartner) {
1876 create_event(prevEdge, events, alloc);
1877 create_event(nextEdge, events, alloc);
1878 } else {
1879 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1880 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1881 }
1882 }
Stephen Whitee260c462017-12-19 18:09:54 -05001883}
1884
1885bool is_overlap_edge(Edge* e) {
1886 if (e->fType == Edge::Type::kOuter) {
1887 return e->fWinding != 0 && e->fWinding != 1;
1888 } else if (e->fType == Edge::Type::kInner) {
1889 return e->fWinding != 0 && e->fWinding != -2;
1890 } else {
1891 return false;
1892 }
1893}
1894
1895// This is a stripped-down version of tessellate() which computes edges which
1896// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001897bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1898 EventComparator comp) {
Stephen Whitee260c462017-12-19 18:09:54 -05001899 LOG("\nfinding overlap regions\n");
1900 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001901 EventList events(comp);
1902 SSVertexMap ssVertices;
1903 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001904 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001905 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001906 continue;
1907 }
1908 Edge* leftEnclosingEdge;
1909 Edge* rightEnclosingEdge;
1910 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001911 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001912 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1913 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001914 bool leftOverlap = prev && is_overlap_edge(prev);
1915 bool rightOverlap = is_overlap_edge(e);
1916 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1917 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001918 if (prev) {
1919 e->fWinding -= prev->fWinding;
1920 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001921 if (leftOverlap && rightOverlap) {
1922 LOG("found interior overlap edge %g -> %g, disconnecting\n",
1923 e->fTop->fID, e->fBottom->fID);
1924 disconnect(e);
1925 } else if (leftOverlap || rightOverlap) {
1926 LOG("found overlap edge %g -> %g%s\n", e->fTop->fID, e->fBottom->fID,
1927 isOuterBoundary ? ", is outer boundary" : "");
1928 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1929 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1930 SSVertex* ssPrev = ssVertices[prevVertex];
1931 if (!ssPrev) {
1932 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1933 }
1934 SSVertex* ssNext = ssVertices[nextVertex];
1935 if (!ssNext) {
1936 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1937 }
1938 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1939 ssEdges.push_back(ssEdge);
1940// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1941 ssPrev->fNext = ssNext->fPrev = ssEdge;
1942 create_event(ssEdge, &events, alloc);
1943 if (!isOuterBoundary) {
1944 disconnect(e);
1945 }
1946 }
1947 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001948 }
1949 Edge* prev = leftEnclosingEdge;
1950 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1951 if (prev) {
1952 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001953 }
1954 insert_edge(e, prev, &activeEdges);
1955 prev = e;
1956 }
1957 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001958 bool complex = events.size() > 0;
1959
Stephen Whitee260c462017-12-19 18:09:54 -05001960 LOG("\ncollapsing overlap regions\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001961 LOG("skeleton before:\n");
1962 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001963 while (events.size() > 0) {
1964 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001965 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001966 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001967 }
Stephen White8a3c0592019-05-29 11:26:16 -04001968 LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001969 dump_skel(ssEdges);
1970 for (SSEdge* edge : ssEdges) {
1971 if (Edge* e = edge->fEdge) {
1972 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1973 }
1974 }
1975 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001976}
1977
1978bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1979 if (!prev || !next) {
1980 return true;
1981 }
1982 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1983 return winding != origEdge->fWinding;
1984}
Stephen White92eba8a2017-02-06 09:50:27 -05001985
senorblancof57372d2016-08-31 10:36:19 -07001986// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1987// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1988// new antialiased mesh from those vertices.
1989
Stephen Whitee260c462017-12-19 18:09:54 -05001990void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1991 Comparator& c, SkArenaAlloc& alloc) {
1992 LOG("\nstroking boundary\n");
1993 // A boundary with fewer than 3 edges is degenerate.
1994 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1995 return;
1996 }
1997 Edge* prevEdge = boundary->fTail;
1998 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1999 SkVector prevNormal;
2000 get_edge_normal(prevEdge, &prevNormal);
2001 double radius = 0.5;
2002 Line prevInner(prevEdge->fLine);
2003 prevInner.fC -= radius;
2004 Line prevOuter(prevEdge->fLine);
2005 prevOuter.fC += radius;
2006 VertexList innerVertices;
2007 VertexList outerVertices;
2008 bool innerInversion = true;
2009 bool outerInversion = true;
2010 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
2011 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
2012 SkVector normal;
2013 get_edge_normal(e, &normal);
2014 Line inner(e->fLine);
2015 inner.fC -= radius;
2016 Line outer(e->fLine);
2017 outer.fC += radius;
2018 SkPoint innerPoint, outerPoint;
2019 LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
2020 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
2021 prevOuter.intersect(outer, &outerPoint)) {
2022 float cosAngle = normal.dot(prevNormal);
2023 if (cosAngle < -kCosMiterAngle) {
2024 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
2025
2026 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
2027 Line bisector(innerPoint, outerPoint);
2028 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
2029 if (tangent.fA == 0 && tangent.fB == 0) {
2030 continue;
2031 }
2032 tangent.normalize();
2033 Line innerTangent(tangent);
2034 Line outerTangent(tangent);
2035 innerTangent.fC -= 0.5;
2036 outerTangent.fC += 0.5;
2037 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
2038 if (prevNormal.cross(normal) > 0) {
2039 // Miter inner points
2040 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2041 !innerTangent.intersect(inner, &innerPoint2) ||
2042 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002043 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002044 }
2045 Line prevTangent(prevV->fPoint,
2046 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2047 Line nextTangent(nextV->fPoint,
2048 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002049 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002050 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002051 }
2052 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002053 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002054 }
2055 outerPoint1 = outerPoint2 = outerPoint;
2056 } else {
2057 // Miter outer points
2058 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2059 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002060 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002061 }
2062 Line prevTangent(prevV->fPoint,
2063 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2064 Line nextTangent(nextV->fPoint,
2065 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002066 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002067 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002068 }
2069 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002070 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002071 }
2072 innerPoint1 = innerPoint2 = innerPoint;
2073 }
Stephen Whiteea495232018-04-03 11:28:15 -04002074 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2075 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2076 continue;
2077 }
Stephen Whitee260c462017-12-19 18:09:54 -05002078 LOG("inner (%g, %g), (%g, %g), ",
2079 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2080 LOG("outer (%g, %g), (%g, %g)\n",
2081 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
2082 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2083 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2084 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2085 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2086 innerVertex1->fPartner = outerVertex1;
2087 innerVertex2->fPartner = outerVertex2;
2088 outerVertex1->fPartner = innerVertex1;
2089 outerVertex2->fPartner = innerVertex2;
2090 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2091 innerInversion = false;
2092 }
2093 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2094 outerInversion = false;
2095 }
2096 innerVertices.append(innerVertex1);
2097 innerVertices.append(innerVertex2);
2098 outerVertices.append(outerVertex1);
2099 outerVertices.append(outerVertex2);
2100 } else {
2101 LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2102 LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
2103 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2104 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2105 innerVertex->fPartner = outerVertex;
2106 outerVertex->fPartner = innerVertex;
2107 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2108 innerInversion = false;
2109 }
2110 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2111 outerInversion = false;
2112 }
2113 innerVertices.append(innerVertex);
2114 outerVertices.append(outerVertex);
2115 }
2116 }
2117 prevInner = inner;
2118 prevOuter = outer;
2119 prevV = v;
2120 prevEdge = e;
2121 prevNormal = normal;
2122 }
2123 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2124 innerInversion = false;
2125 }
2126 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2127 outerInversion = false;
2128 }
2129 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2130 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2131 // interior inverts).
2132 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2133 // so it will be detected during collapse_overlap_regions().
2134 int innerWinding = innerInversion ? 2 : -2;
2135 int outerWinding = outerInversion ? -1 : 1;
2136 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2137 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2138 }
2139 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2140 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2141 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2142 }
2143 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2144 innerMesh->append(innerVertices);
2145 outerMesh->append(outerVertices);
2146}
senorblancof57372d2016-08-31 10:36:19 -07002147
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002148void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05002149 LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002150 bool down = apply_fill_type(fillType, e->fWinding);
senorblancof57372d2016-08-31 10:36:19 -07002151 while (e) {
2152 e->fWinding = down ? 1 : -1;
2153 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002154 e->fLine.normalize();
2155 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002156 boundary->append(e);
2157 if (down) {
2158 // Find outgoing edge, in clockwise order.
2159 if ((next = e->fNextEdgeAbove)) {
2160 down = false;
2161 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2162 down = true;
2163 } else if ((next = e->fPrevEdgeAbove)) {
2164 down = false;
2165 }
2166 } else {
2167 // Find outgoing edge, in counter-clockwise order.
2168 if ((next = e->fPrevEdgeBelow)) {
2169 down = true;
2170 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2171 down = false;
2172 } else if ((next = e->fNextEdgeBelow)) {
2173 down = true;
2174 }
2175 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002176 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002177 e = next;
2178 }
2179}
2180
Stephen White5ad721e2017-02-23 16:50:47 -05002181// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002182
Stephen Whitebda29c02017-03-13 15:10:13 -04002183void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
2184 VertexList* outerVertices, SkPath::FillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002185 Comparator& c, SkArenaAlloc& alloc) {
2186 remove_non_boundary_edges(inMesh, fillType, alloc);
2187 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002188 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002189 EdgeList boundary;
2190 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2191 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002192 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002193 }
2194 }
senorblancof57372d2016-08-31 10:36:19 -07002195}
2196
Stephen Whitebda29c02017-03-13 15:10:13 -04002197// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002198
Stephen White3a9aab92017-03-07 14:07:18 -05002199void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002200 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002201#if LOGGING_ENABLED
2202 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002203 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002204 SkASSERT(v);
2205 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002206 for (v = v->fNext; v; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002207 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
2208 }
2209 }
2210#endif
senorblancof57372d2016-08-31 10:36:19 -07002211 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002212 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002213}
2214
Stephen Whitebda29c02017-03-13 15:10:13 -04002215void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002216 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002217 return;
ethannicholase9709e82016-01-07 13:34:16 -08002218 }
2219
2220 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002221 if (c.fDirection == Comparator::Direction::kHorizontal) {
2222 merge_sort<sweep_lt_horiz>(vertices);
2223 } else {
2224 merge_sort<sweep_lt_vert>(vertices);
2225 }
ethannicholase9709e82016-01-07 13:34:16 -08002226#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002227 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002228 static float gID = 0.0f;
2229 v->fID = gID++;
2230 }
2231#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002232}
2233
Stephen White3a9aab92017-03-07 14:07:18 -05002234Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType,
Stephen Whitebda29c02017-03-13 15:10:13 -04002235 const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002236 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002237 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2238 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002239 VertexList mesh;
2240 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002241 sort_mesh(&mesh, c, alloc);
2242 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002243 simplify(&mesh, c, alloc);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002244 LOG("\nsimplified mesh:\n");
2245 dump_mesh(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002246 if (antialias) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002247 VertexList innerMesh;
2248 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2249 sort_mesh(&innerMesh, c, alloc);
2250 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002251 merge_coincident_vertices(&innerMesh, c, alloc);
2252 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2253 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2254 was_complex = simplify(outerMesh, c, alloc) || was_complex;
2255 LOG("\ninner mesh before:\n");
2256 dump_mesh(innerMesh);
2257 LOG("\nouter mesh before:\n");
2258 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002259 EventComparator eventLT(EventComparator::Op::kLessThan);
2260 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2261 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2262 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002263 if (was_complex) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002264 LOG("found complex mesh; taking slow path\n");
2265 VertexList aaMesh;
Stephen White95152e12017-12-18 10:52:44 -05002266 LOG("\ninner mesh after:\n");
2267 dump_mesh(innerMesh);
2268 LOG("\nouter mesh after:\n");
2269 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002270 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002271 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002272 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2273 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002274 simplify(&aaMesh, c, alloc);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002275 LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002276 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002277 outerMesh->fHead = outerMesh->fTail = nullptr;
2278 return tessellate(aaMesh, alloc);
2279 } else {
2280 LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002281 return tessellate(innerMesh, alloc);
2282 }
Stephen White49789062017-02-21 10:35:49 -05002283 } else {
2284 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002285 }
senorblancof57372d2016-08-31 10:36:19 -07002286}
2287
2288// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Brian Osman0995fd52019-01-09 09:52:25 -05002289void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, bool emitCoverage, void* data) {
senorblancof57372d2016-08-31 10:36:19 -07002290 for (Poly* poly = polys; poly; poly = poly->fNext) {
2291 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002292 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002293 }
2294 }
2295 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002296}
2297
halcanary9d524f22016-03-29 09:03:52 -07002298Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen Whitebda29c02017-03-13 15:10:13 -04002299 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
2300 VertexList* outerMesh) {
ethannicholase9709e82016-01-07 13:34:16 -08002301 SkPath::FillType fillType = path.getFillType();
2302 if (SkPath::IsInverseFillType(fillType)) {
2303 contourCnt++;
2304 }
Stephen White3a9aab92017-03-07 14:07:18 -05002305 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002306
2307 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07002308 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Stephen Whitebda29c02017-03-13 15:10:13 -04002309 antialias, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002310}
2311
Stephen White11f65e02017-02-16 19:00:39 -05002312int get_contour_count(const SkPath& path, SkScalar tolerance) {
2313 int contourCnt;
2314 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002315 if (maxPts <= 0) {
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
Greg Danield5b45932018-06-07 13:15:10 -04002321int64_t count_points(Poly* polys, SkPath::FillType fillType) {
2322 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,
Brian Osman0995fd52019-01-09 09:52:25 -05002362 VertexAllocator* 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);
senorblanco7ab96e92016-10-12 06:47:44 -07002372 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : 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
senorblancof57372d2016-08-31 10:36:19 -07002382 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08002383 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002384 SkDebugf("Could not allocate vertices\n");
2385 return 0;
2386 }
senorblancof57372d2016-08-31 10:36:19 -07002387
2388 LOG("emitting %d verts\n", count);
Brian Osman80879d42019-01-07 16:15:27 -05002389 void* end = polys_to_triangles(polys, fillType, antialias, verts);
2390 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002391
senorblancof57372d2016-08-31 10:36:19 -07002392 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
2393 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08002394 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002395 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002396 return actualCount;
2397}
2398
halcanary9d524f22016-03-29 09:03:52 -07002399int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002400 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002401 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002402 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002403 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002404 return 0;
2405 }
Stephen White11f65e02017-02-16 19:00:39 -05002406 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002407 bool isLinear;
Stephen Whitebda29c02017-03-13 15:10:13 -04002408 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
2409 nullptr);
ethannicholase9709e82016-01-07 13:34:16 -08002410 SkPath::FillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002411 int64_t count64 = count_points(polys, fillType);
2412 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002413 *verts = nullptr;
2414 return 0;
2415 }
Greg Danield5b45932018-06-07 13:15:10 -04002416 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002417
2418 *verts = new GrTessellator::WindingVertex[count];
2419 GrTessellator::WindingVertex* vertsEnd = *verts;
2420 SkPoint* points = new SkPoint[count];
2421 SkPoint* pointsEnd = points;
2422 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002423 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002424 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002425 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002426 while (start != pointsEnd) {
2427 vertsEnd->fPos = *start;
2428 vertsEnd->fWinding = poly->fWinding;
2429 ++start;
2430 ++vertsEnd;
2431 }
2432 }
2433 }
2434 int actualCount = static_cast<int>(vertsEnd - *verts);
2435 SkASSERT(actualCount <= count);
2436 SkASSERT(pointsEnd - points == actualCount);
2437 delete[] points;
2438 return actualCount;
2439}
2440
2441} // namespace