blob: 19c2447ab39fb89f3ca8e4cbed9a573f9232b93e [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 Whiteb67b2352019-06-01 13:07:27 -040065 * active edge list. This is handled during merging or splitting by rewind()ing the
66 * active edge list to the vertex before potential violations occur.
ethannicholase9709e82016-01-07 13:34:16 -080067 *
68 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
69 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
70 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
71 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
72 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
73 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
74 * linked list implementation. With the latter, all removals are O(1), and most insertions
75 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
76 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
77 * frequent. There may be other data structures worth investigating, however.
78 *
79 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
80 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
81 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
82 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
83 * that the "left" and "right" orientation in the code remains correct (edges to the left are
84 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
85 * degrees counterclockwise, rather that transposing.
86 */
87
88#define LOGGING_ENABLED 0
89
90#if LOGGING_ENABLED
91#define LOG printf
92#else
93#define LOG(...)
94#endif
95
ethannicholase9709e82016-01-07 13:34:16 -080096namespace {
97
Stephen White11f65e02017-02-16 19:00:39 -050098const int kArenaChunkSize = 16 * 1024;
Stephen Whitee260c462017-12-19 18:09:54 -050099const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
Stephen White11f65e02017-02-16 19:00:39 -0500100
ethannicholase9709e82016-01-07 13:34:16 -0800101struct Vertex;
102struct Edge;
Stephen Whitee260c462017-12-19 18:09:54 -0500103struct Event;
ethannicholase9709e82016-01-07 13:34:16 -0800104struct Poly;
105
106template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800107void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800108 t->*Prev = prev;
109 t->*Next = next;
110 if (prev) {
111 prev->*Next = t;
112 } else if (head) {
113 *head = t;
114 }
115 if (next) {
116 next->*Prev = t;
117 } else if (tail) {
118 *tail = t;
119 }
120}
121
122template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800123void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800124 if (t->*Prev) {
125 t->*Prev->*Next = t->*Next;
126 } else if (head) {
127 *head = t->*Next;
128 }
129 if (t->*Next) {
130 t->*Next->*Prev = t->*Prev;
131 } else if (tail) {
132 *tail = t->*Prev;
133 }
134 t->*Prev = t->*Next = nullptr;
135}
136
137/**
138 * Vertices are used in three ways: first, the path contours are converted into a
139 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
140 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
141 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
142 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
143 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
144 * an individual Vertex from the path mesh may belong to multiple
145 * MonotonePolys, so the original Vertices cannot be re-used.
146 */
147
148struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700149 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800150 : fPoint(point), fPrev(nullptr), fNext(nullptr)
151 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
152 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -0400153 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -0400154 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -0700155 , fAlpha(alpha)
Stephen Whitec4dbc372019-05-22 10:50:14 -0400156 , fSynthetic(false)
ethannicholase9709e82016-01-07 13:34:16 -0800157#if LOGGING_ENABLED
158 , fID (-1.0f)
159#endif
160 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400161 SkPoint fPoint; // Vertex position
162 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
163 Vertex* fNext; // "
164 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
165 Edge* fLastEdgeAbove; // "
166 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
167 Edge* fLastEdgeBelow; // "
168 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
169 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
170 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700171 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400172 bool fSynthetic; // Is this a synthetic vertex?
ethannicholase9709e82016-01-07 13:34:16 -0800173#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400174 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800175#endif
176};
177
178/***************************************************************************************/
179
180typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
181
ethannicholase9709e82016-01-07 13:34:16 -0800182bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500183 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800184}
185
186bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500187 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800188}
189
Stephen White16a40cb2017-02-23 11:10:01 -0500190struct Comparator {
191 enum class Direction { kVertical, kHorizontal };
192 Comparator(Direction direction) : fDirection(direction) {}
193 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
194 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
195 }
Stephen White16a40cb2017-02-23 11:10:01 -0500196 Direction fDirection;
197};
198
Brian Osman0995fd52019-01-09 09:52:25 -0500199inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500200 GrVertexWriter verts{data};
201 verts.write(v->fPoint);
202
Brian Osman80879d42019-01-07 16:15:27 -0500203 if (emitCoverage) {
204 verts.write(GrNormalizeByteToFloat(v->fAlpha));
205 }
Brian Osman0995fd52019-01-09 09:52:25 -0500206
Brian Osmanf9aabff2018-11-13 16:11:38 -0500207 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800208}
209
Brian Osman0995fd52019-01-09 09:52:25 -0500210void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Stephen White95152e12017-12-18 10:52:44 -0500211 LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
212 LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
213 LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700214#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500215 data = emit_vertex(v0, emitCoverage, data);
216 data = emit_vertex(v1, emitCoverage, data);
217 data = emit_vertex(v1, emitCoverage, data);
218 data = emit_vertex(v2, emitCoverage, data);
219 data = emit_vertex(v2, emitCoverage, data);
220 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800221#else
Brian Osman0995fd52019-01-09 09:52:25 -0500222 data = emit_vertex(v0, emitCoverage, data);
223 data = emit_vertex(v1, emitCoverage, data);
224 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800225#endif
226 return data;
227}
228
senorblancoe6eaa322016-03-08 09:06:44 -0800229struct VertexList {
230 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500231 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800232 Vertex* fHead;
233 Vertex* fTail;
234 void insert(Vertex* v, Vertex* prev, Vertex* next) {
235 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
236 }
237 void append(Vertex* v) {
238 insert(v, fTail, nullptr);
239 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400240 void append(const VertexList& list) {
241 if (!list.fHead) {
242 return;
243 }
244 if (fTail) {
245 fTail->fNext = list.fHead;
246 list.fHead->fPrev = fTail;
247 } else {
248 fHead = list.fHead;
249 }
250 fTail = list.fTail;
251 }
senorblancoe6eaa322016-03-08 09:06:44 -0800252 void prepend(Vertex* v) {
253 insert(v, nullptr, fHead);
254 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500255 void remove(Vertex* v) {
256 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
257 }
senorblancof57372d2016-08-31 10:36:19 -0700258 void close() {
259 if (fHead && fTail) {
260 fTail->fNext = fHead;
261 fHead->fPrev = fTail;
262 }
263 }
senorblancoe6eaa322016-03-08 09:06:44 -0800264};
265
senorblancof57372d2016-08-31 10:36:19 -0700266// Round to nearest quarter-pixel. This is used for screenspace tessellation.
267
268inline void round(SkPoint* p) {
269 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
270 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
271}
272
Stephen White94b7e542018-01-04 14:01:10 -0500273inline SkScalar double_to_clamped_scalar(double d) {
274 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
275}
276
senorblanco49df8d12016-10-07 08:36:56 -0700277// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
278struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500279 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700280 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
281 Line(const SkPoint& p, const SkPoint& q)
282 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
283 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
284 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
285 static_cast<double>(p.fX) * q.fY) {}
286 double dist(const SkPoint& p) const {
287 return fA * p.fX + fB * p.fY + fC;
288 }
Stephen Whitee260c462017-12-19 18:09:54 -0500289 Line operator*(double v) const {
290 return Line(fA * v, fB * v, fC * v);
291 }
senorblanco49df8d12016-10-07 08:36:56 -0700292 double magSq() const {
293 return fA * fA + fB * fB;
294 }
Stephen Whitee260c462017-12-19 18:09:54 -0500295 void normalize() {
296 double len = sqrt(this->magSq());
297 if (len == 0.0) {
298 return;
299 }
300 double scale = 1.0f / len;
301 fA *= scale;
302 fB *= scale;
303 fC *= scale;
304 }
305 bool nearParallel(const Line& o) const {
306 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
307 }
senorblanco49df8d12016-10-07 08:36:56 -0700308
309 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500310 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700311 double denom = fA * other.fB - fB * other.fA;
312 if (denom == 0.0) {
313 return false;
314 }
Stephen White94b7e542018-01-04 14:01:10 -0500315 double scale = 1.0 / denom;
316 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
317 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500318 round(point);
senorblanco49df8d12016-10-07 08:36:56 -0700319 return true;
320 }
321 double fA, fB, fC;
322};
323
ethannicholase9709e82016-01-07 13:34:16 -0800324/**
325 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
326 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
327 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whiteb67b2352019-06-01 13:07:27 -0400328 * point). For speed, that case is only tested by the callers that require it. Edges also handle
329 * checking for intersection with other edges. Currently, this converts the edges to the
330 * parametric form, in order to avoid doing a division until an intersection has been confirmed.
331 * This is slightly slower in the "found" case, but a lot faster in the "not found" case.
ethannicholase9709e82016-01-07 13:34:16 -0800332 *
333 * The coefficients of the line equation stored in double precision to avoid catastrphic
334 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
335 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
336 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
337 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
338 * this file).
339 */
340
341struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500342 enum class Type { kInner, kOuter, kConnector };
343 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800344 : fWinding(winding)
345 , fTop(top)
346 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500347 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800348 , fLeft(nullptr)
349 , fRight(nullptr)
350 , fPrevEdgeAbove(nullptr)
351 , fNextEdgeAbove(nullptr)
352 , fPrevEdgeBelow(nullptr)
353 , fNextEdgeBelow(nullptr)
354 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700355 , fRightPoly(nullptr)
356 , fLeftPolyPrev(nullptr)
357 , fLeftPolyNext(nullptr)
358 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700359 , fRightPolyNext(nullptr)
360 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700361 , fUsedInRightPoly(false)
362 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800363 }
364 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
365 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
366 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500367 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800368 Edge* fLeft; // The linked list of edges in the active edge list.
369 Edge* fRight; // "
370 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
371 Edge* fNextEdgeAbove; // "
372 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
373 Edge* fNextEdgeBelow; // "
374 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
375 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700376 Edge* fLeftPolyPrev;
377 Edge* fLeftPolyNext;
378 Edge* fRightPolyPrev;
379 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700380 bool fUsedInLeftPoly;
381 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700382 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800383 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700384 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800385 }
386 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700387 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800388 }
389 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700390 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800391 }
392 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700393 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800394 }
Stephen White95152e12017-12-18 10:52:44 -0500395 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
ethannicholase9709e82016-01-07 13:34:16 -0800396 LOG("intersecting %g -> %g with %g -> %g\n",
397 fTop->fID, fBottom->fID,
398 other.fTop->fID, other.fBottom->fID);
399 if (fTop == other.fTop || fBottom == other.fBottom) {
400 return false;
401 }
senorblanco49df8d12016-10-07 08:36:56 -0700402 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800403 if (denom == 0.0) {
404 return false;
405 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500406 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
407 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
408 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
409 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800410 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
411 // This saves us doing the divide below unless absolutely necessary.
412 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
413 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
414 return false;
415 }
416 double s = sNumer / denom;
417 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700418 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
419 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500420 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500421 if (fType == Type::kConnector) {
422 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
423 } else if (other.fType == Type::kConnector) {
424 double t = tNumer / denom;
425 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500426 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
427 *alpha = 0;
428 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500429 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500430 }
431 }
ethannicholase9709e82016-01-07 13:34:16 -0800432 return true;
433 }
senorblancof57372d2016-08-31 10:36:19 -0700434};
435
Stephen Whitec4dbc372019-05-22 10:50:14 -0400436struct SSEdge;
437
438struct SSVertex {
439 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
440 Vertex* fVertex;
441 SSEdge* fPrev;
442 SSEdge* fNext;
443};
444
445struct SSEdge {
446 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
447 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
448 }
449 Edge* fEdge;
450 Event* fEvent;
451 SSVertex* fPrev;
452 SSVertex* fNext;
453};
454
455typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
456typedef std::vector<SSEdge*> SSEdgeList;
457
senorblancof57372d2016-08-31 10:36:19 -0700458struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500459 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700460 Edge* fHead;
461 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700462 void insert(Edge* edge, Edge* prev, Edge* next) {
463 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700464 }
465 void append(Edge* e) {
466 insert(e, fTail, nullptr);
467 }
468 void remove(Edge* edge) {
469 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700470 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400471 void removeAll() {
472 while (fHead) {
473 this->remove(fHead);
474 }
475 }
senorblancof57372d2016-08-31 10:36:19 -0700476 void close() {
477 if (fHead && fTail) {
478 fTail->fRight = fHead;
479 fHead->fLeft = fTail;
480 }
481 }
482 bool contains(Edge* edge) const {
483 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800484 }
485};
486
Stephen Whitec4dbc372019-05-22 10:50:14 -0400487struct EventList;
488
Stephen Whitee260c462017-12-19 18:09:54 -0500489struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400490 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
491 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500492 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400493 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500494 SkPoint fPoint;
495 uint8_t fAlpha;
Stephen Whitec4dbc372019-05-22 10:50:14 -0400496 void apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500497};
498
Stephen Whitec4dbc372019-05-22 10:50:14 -0400499struct EventComparator {
500 enum class Op { kLessThan, kGreaterThan };
501 EventComparator(Op op) : fOp(op) {}
502 bool operator() (Event* const &e1, Event* const &e2) {
503 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
504 : e1->fAlpha > e2->fAlpha;
505 }
506 Op fOp;
507};
Stephen Whitee260c462017-12-19 18:09:54 -0500508
Stephen Whitec4dbc372019-05-22 10:50:14 -0400509typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500510
Stephen Whitec4dbc372019-05-22 10:50:14 -0400511struct EventList : EventPQ {
512 EventList(EventComparator comparison) : EventPQ(comparison) {
513 }
514};
515
516void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
517 Vertex* prev = e->fPrev->fVertex;
518 Vertex* next = e->fNext->fVertex;
519 if (prev == next || !prev->fPartner || !next->fPartner) {
520 return;
521 }
522 Edge bisector1(prev, prev->fPartner, 1, Edge::Type::kConnector);
523 Edge bisector2(next, next->fPartner, 1, Edge::Type::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500524 SkPoint p;
525 uint8_t alpha;
526 if (bisector1.intersect(bisector2, &p, &alpha)) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400527 LOG("found edge event for %g, %g (original %g -> %g), will collapse to %g,%g alpha %d\n",
528 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY, alpha);
529 e->fEvent = alloc.make<Event>(e, p, alpha);
530 events->push(e->fEvent);
531 }
532}
533
534void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
535 Comparator& c, SkArenaAlloc& alloc) {
536 if (!v->fPartner) {
537 return;
538 }
Stephen White8a3c0592019-05-29 11:26:16 -0400539 Vertex* top = edge->fEdge->fTop;
540 Vertex* bottom = edge->fEdge->fBottom;
541 if (!top || !bottom ) {
542 return;
543 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400544 Line line = edge->fEdge->fLine;
545 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
546 Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector);
547 SkPoint p;
548 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400549 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
550 c.sweep_lt(p, bottom->fPoint)) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400551 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 -0400552 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400553 edge->fEvent = alloc.make<Event>(edge, p, alpha);
554 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500555 }
556}
Stephen Whitee260c462017-12-19 18:09:54 -0500557
ethannicholase9709e82016-01-07 13:34:16 -0800558/***************************************************************************************/
559
560struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700561 Poly(Vertex* v, int winding)
562 : fFirstVertex(v)
563 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800564 , fHead(nullptr)
565 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800566 , fNext(nullptr)
567 , fPartner(nullptr)
568 , fCount(0)
569 {
570#if LOGGING_ENABLED
571 static int gID = 0;
572 fID = gID++;
573 LOG("*** created Poly %d\n", fID);
574#endif
575 }
senorblanco531237e2016-06-02 11:36:48 -0700576 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800577 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700578 MonotonePoly(Edge* edge, Side side)
579 : fSide(side)
580 , fFirstEdge(nullptr)
581 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800582 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700583 , fNext(nullptr) {
584 this->addEdge(edge);
585 }
ethannicholase9709e82016-01-07 13:34:16 -0800586 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700587 Edge* fFirstEdge;
588 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800589 MonotonePoly* fPrev;
590 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700591 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800592 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700593 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700594 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
595 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700596 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800597 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700598 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700599 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
600 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700601 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800602 }
ethannicholase9709e82016-01-07 13:34:16 -0800603 }
604
Brian Osman0995fd52019-01-09 09:52:25 -0500605 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700606 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700607 VertexList vertices;
608 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500609 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700610 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700611 if (kRight_Side == fSide) {
612 vertices.append(e->fBottom);
613 e = e->fRightPolyNext;
614 } else {
615 vertices.prepend(e->fBottom);
616 e = e->fLeftPolyNext;
617 }
Stephen White651cbe92017-03-03 12:24:16 -0500618 count++;
senorblanco531237e2016-06-02 11:36:48 -0700619 }
620 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800621 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700622 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800623 SkASSERT(v && v->fPrev && v->fNext);
624 Vertex* prev = v->fPrev;
625 Vertex* curr = v;
626 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500627 if (count == 3) {
Brian Osman0995fd52019-01-09 09:52:25 -0500628 return emit_triangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500629 }
ethannicholase9709e82016-01-07 13:34:16 -0800630 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
631 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
632 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
633 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
634 if (ax * by - ay * bx >= 0.0) {
Brian Osman0995fd52019-01-09 09:52:25 -0500635 data = emit_triangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800636 v->fPrev->fNext = v->fNext;
637 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500638 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800639 if (v->fPrev == first) {
640 v = v->fNext;
641 } else {
642 v = v->fPrev;
643 }
644 } else {
645 v = v->fNext;
646 }
647 }
648 return data;
649 }
650 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500651 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
senorblanco70f52512016-08-17 14:56:22 -0700652 LOG("addEdge (%g -> %g) to poly %d, %s side\n",
653 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800654 Poly* partner = fPartner;
655 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700656 if (side == kRight_Side) {
657 if (e->fUsedInRightPoly) {
658 return this;
659 }
660 } else {
661 if (e->fUsedInLeftPoly) {
662 return this;
663 }
664 }
ethannicholase9709e82016-01-07 13:34:16 -0800665 if (partner) {
666 fPartner = partner->fPartner = nullptr;
667 }
senorblanco531237e2016-06-02 11:36:48 -0700668 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500669 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700670 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700671 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
672 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700673 } else if (side == fTail->fSide) {
674 fTail->addEdge(e);
675 fCount++;
676 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500677 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700678 fTail->addEdge(e);
679 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800680 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700681 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800682 poly = partner;
683 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500684 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700685 m->fPrev = fTail;
686 fTail->fNext = m;
687 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800688 }
689 }
ethannicholase9709e82016-01-07 13:34:16 -0800690 return poly;
691 }
Brian Osman0995fd52019-01-09 09:52:25 -0500692 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800693 if (fCount < 3) {
694 return data;
695 }
696 LOG("emit() %d, size %d\n", fID, fCount);
697 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500698 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800699 }
700 return data;
701 }
senorblanco531237e2016-06-02 11:36:48 -0700702 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
703 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800704 int fWinding;
705 MonotonePoly* fHead;
706 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800707 Poly* fNext;
708 Poly* fPartner;
709 int fCount;
710#if LOGGING_ENABLED
711 int fID;
712#endif
713};
714
715/***************************************************************************************/
716
717bool coincident(const SkPoint& a, const SkPoint& b) {
718 return a == b;
719}
720
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500721Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
722 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800723 poly->fNext = *head;
724 *head = poly;
725 return poly;
726}
727
Stephen White3a9aab92017-03-07 14:07:18 -0500728void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500729 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800730#if LOGGING_ENABLED
731 static float gID = 0.0f;
732 v->fID = gID++;
733#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500734 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800735}
736
Stephen White36e4f062017-03-27 16:11:31 -0400737SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
738 SkQuadCoeff quad(pts);
739 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
740 SkPoint mid = to_point(quad.eval(t));
741 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400742 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
743 return 0;
744 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500745 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400746}
747
748void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
749 SkArenaAlloc& alloc) {
750 SkQuadCoeff quad(pts);
751 Sk2s aa = quad.fA * quad.fA;
752 SkScalar denom = 2.0f * (aa[0] + aa[1]);
753 Sk2s ab = quad.fA * quad.fB;
754 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
755 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500756 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400757 // Test possible subdivision values only at the point of maximum curvature.
758 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500759 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400760 u = 1.0f / nPoints;
761 if (quad_error_at(pts, t, u) < toleranceSqd) {
762 break;
763 }
764 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800765 }
Stephen White36e4f062017-03-27 16:11:31 -0400766 for (int j = 1; j <= nPoints; j++) {
767 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
768 }
ethannicholase9709e82016-01-07 13:34:16 -0800769}
770
Stephen White3a9aab92017-03-07 14:07:18 -0500771void generate_cubic_points(const SkPoint& p0,
772 const SkPoint& p1,
773 const SkPoint& p2,
774 const SkPoint& p3,
775 SkScalar tolSqd,
776 VertexList* contour,
777 int pointsLeft,
778 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500779 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
780 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800781 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
782 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500783 append_point_to_contour(p3, contour, alloc);
784 return;
ethannicholase9709e82016-01-07 13:34:16 -0800785 }
786 const SkPoint q[] = {
787 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
788 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
789 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
790 };
791 const SkPoint r[] = {
792 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
793 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
794 };
795 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
796 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500797 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
798 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800799}
800
801// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
802
803void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500804 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800805 SkScalar toleranceSqd = tolerance * tolerance;
806
807 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800808 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500809 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800810 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800811 if (path.isInverseFillType()) {
812 SkPoint quad[4];
813 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700814 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500815 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800816 }
Stephen White3a9aab92017-03-07 14:07:18 -0500817 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800818 }
819 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500820 SkPath::Verb verb;
Stephen White2cee2832017-08-23 09:37:16 -0400821 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800822 switch (verb) {
823 case SkPath::kConic_Verb: {
824 SkScalar weight = iter.conicWeight();
825 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
826 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400827 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800828 quadPts += 2;
829 }
830 *isLinear = false;
831 break;
832 }
833 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500834 if (contour->fHead) {
835 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800836 }
Stephen White3a9aab92017-03-07 14:07:18 -0500837 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800838 break;
839 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500840 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800841 break;
842 }
843 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400844 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800845 *isLinear = false;
846 break;
847 }
848 case SkPath::kCubic_Verb: {
849 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500850 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
851 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800852 *isLinear = false;
853 break;
854 }
855 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800856 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800857 break;
858 }
859 }
860}
861
Stephen White49789062017-02-21 10:35:49 -0500862inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800863 switch (fillType) {
864 case SkPath::kWinding_FillType:
865 return winding != 0;
866 case SkPath::kEvenOdd_FillType:
867 return (winding & 1) != 0;
868 case SkPath::kInverseWinding_FillType:
senorblanco7ab96e92016-10-12 06:47:44 -0700869 return winding == 1;
ethannicholase9709e82016-01-07 13:34:16 -0800870 case SkPath::kInverseEvenOdd_FillType:
871 return (winding & 1) == 1;
872 default:
873 SkASSERT(false);
874 return false;
875 }
876}
877
Stephen White49789062017-02-21 10:35:49 -0500878inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
879 return poly && apply_fill_type(fillType, poly->fWinding);
880}
881
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500882Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500883 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800884 Vertex* top = winding < 0 ? next : prev;
885 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500886 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800887}
888
889void remove_edge(Edge* edge, EdgeList* edges) {
890 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700891 SkASSERT(edges->contains(edge));
892 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800893}
894
895void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
896 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700897 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800898 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700899 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800900}
901
902void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500903 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800904 *left = v->fFirstEdgeAbove->fLeft;
905 *right = v->fLastEdgeAbove->fRight;
906 return;
907 }
908 Edge* next = nullptr;
909 Edge* prev;
910 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
911 if (prev->isLeftOf(v)) {
912 break;
913 }
914 next = prev;
915 }
916 *left = prev;
917 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800918}
919
ethannicholase9709e82016-01-07 13:34:16 -0800920void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
921 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500922 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800923 return;
924 }
925 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
926 Edge* prev = nullptr;
927 Edge* next;
928 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
929 if (next->isRightOf(edge->fTop)) {
930 break;
931 }
932 prev = next;
933 }
senorblancoe6eaa322016-03-08 09:06:44 -0800934 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800935 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
936}
937
938void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
939 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500940 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800941 return;
942 }
943 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
944 Edge* prev = nullptr;
945 Edge* next;
946 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
947 if (next->isRightOf(edge->fBottom)) {
948 break;
949 }
950 prev = next;
951 }
senorblancoe6eaa322016-03-08 09:06:44 -0800952 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800953 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
954}
955
956void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400957 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800958 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
959 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800960 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800961 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
962}
963
964void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400965 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800966 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
967 edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800968 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800969 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
970}
971
Stephen Whitee7a364d2017-01-11 16:19:26 -0500972void disconnect(Edge* edge)
973{
ethannicholase9709e82016-01-07 13:34:16 -0800974 remove_edge_above(edge);
975 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500976}
977
Stephen White3b5a3fa2017-06-06 14:51:19 -0400978void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
979
980void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
981 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
982 return;
983 }
984 Vertex* v = *current;
985 LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
986 while (v != dst) {
987 v = v->fPrev;
988 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
989 remove_edge(e, activeEdges);
990 }
991 Edge* leftEdge = v->fLeftEnclosingEdge;
992 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
993 insert_edge(e, leftEdge, activeEdges);
994 leftEdge = e;
995 }
996 }
997 *current = v;
998}
999
Stephen White3b5a3fa2017-06-06 14:51:19 -04001000void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001001 remove_edge_below(edge);
1002 edge->fTop = v;
1003 edge->recompute();
1004 insert_edge_below(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001005 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001006 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001007}
1008
Stephen White3b5a3fa2017-06-06 14:51:19 -04001009void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001010 remove_edge_above(edge);
1011 edge->fBottom = v;
1012 edge->recompute();
1013 insert_edge_above(edge, v, c);
Stephen Whiteb67b2352019-06-01 13:07:27 -04001014 rewind(activeEdges, current, edge->fTop, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001015 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001016}
1017
Stephen White3b5a3fa2017-06-06 14:51:19 -04001018void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1019 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001020 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
1021 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1022 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1023 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001024 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001025 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001026 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001027 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001028 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001029 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001030 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001031 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001032 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001033 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001034 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001035 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001036 }
1037}
1038
Stephen White3b5a3fa2017-06-06 14:51:19 -04001039void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1040 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001041 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
1042 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1043 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1044 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001045 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001046 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001047 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001048 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001049 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001050 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001051 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001052 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001053 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001054 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001055 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001056 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001057 }
1058}
1059
Stephen Whited26b4d82018-07-26 10:02:27 -04001060bool top_collinear(Edge* left, Edge* right) {
1061 if (!left || !right) {
1062 return false;
1063 }
1064 return left->fTop->fPoint == right->fTop->fPoint ||
1065 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1066}
1067
1068bool bottom_collinear(Edge* left, Edge* right) {
1069 if (!left || !right) {
1070 return false;
1071 }
1072 return left->fBottom->fPoint == right->fBottom->fPoint ||
1073 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1074}
1075
Stephen White3b5a3fa2017-06-06 14:51:19 -04001076void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001077 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001078 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001079 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001080 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001081 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001082 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001083 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001084 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001085 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001086 } else {
1087 break;
1088 }
ethannicholase9709e82016-01-07 13:34:16 -08001089 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001090 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1091 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1092 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1093 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001094}
1095
Stephen White89042d52018-06-08 12:18:22 -04001096bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001097 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001098 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001099 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001100 }
ethannicholase9709e82016-01-07 13:34:16 -08001101 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1102 edge->fTop->fID, edge->fBottom->fID,
1103 v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001104 Vertex* top;
1105 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001106 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001107 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001108 top = v;
1109 bottom = edge->fTop;
1110 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001111 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001112 top = edge->fBottom;
1113 bottom = v;
1114 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001115 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001116 top = v;
1117 bottom = edge->fBottom;
1118 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001119 }
Stephen White531a48e2018-06-01 09:49:39 -04001120 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001121 insert_edge_below(newEdge, top, c);
1122 insert_edge_above(newEdge, bottom, c);
1123 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001124 return true;
1125}
1126
1127bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1128 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1129 return false;
1130 }
Stephen White1c5fd182018-07-12 15:54:05 -04001131 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1132 return false;
1133 }
Stephen White89042d52018-06-08 12:18:22 -04001134 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1135 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001136 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001137 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1138 }
1139 } else {
1140 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001141 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001142 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1143 }
1144 }
1145 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1146 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001147 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001148 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1149 }
1150 } else {
1151 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001152 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001153 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1154 }
1155 }
1156 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001157}
1158
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001159Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001160 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001161 if (!prev || !next || prev->fPoint == next->fPoint) {
1162 return nullptr;
1163 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001164 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001165 insert_edge_below(edge, edge->fTop, c);
1166 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001167 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001168 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001169 return edge;
1170}
1171
Stephen Whitebf6137e2017-01-04 15:43:26 -05001172void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001173 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001174 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1175 src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001176 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001177 if (src->fPartner) {
1178 src->fPartner->fPartner = dst;
1179 }
Stephen White7b376942018-05-22 11:51:32 -04001180 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001181 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001182 }
Stephen White7b376942018-05-22 11:51:32 -04001183 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001184 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001185 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001186 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001187 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -08001188}
1189
Stephen White95152e12017-12-18 10:52:44 -05001190Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1191 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1192 Vertex* prevV = reference;
1193 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1194 prevV = prevV->fPrev;
1195 }
1196 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1197 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1198 prevV = nextV;
1199 nextV = nextV->fNext;
1200 }
1201 Vertex* v;
1202 if (prevV && coincident(prevV->fPoint, p)) {
1203 v = prevV;
1204 } else if (nextV && coincident(nextV->fPoint, p)) {
1205 v = nextV;
1206 } else {
1207 v = alloc.make<Vertex>(p, alpha);
1208#if LOGGING_ENABLED
1209 if (!prevV) {
1210 v->fID = mesh->fHead->fID - 1.0f;
1211 } else if (!nextV) {
1212 v->fID = mesh->fTail->fID + 1.0f;
1213 } else {
1214 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1215 }
1216#endif
1217 mesh->insert(v, prevV, nextV);
1218 }
1219 return v;
1220}
1221
Stephen White53a02982018-05-30 22:47:46 -04001222// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1223// sort criterion, it may not be possible to split correctly, since there is no point which is
1224// below the top and above the bottom. This function detects that case.
1225bool nearly_flat(Comparator& c, Edge* edge) {
1226 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1227 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001228 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001229}
1230
Stephen Whitee62999f2018-06-05 18:45:07 -04001231SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1232 if (c.sweep_lt(p, min)) {
1233 return min;
1234 } else if (c.sweep_lt(max, p)) {
1235 return max;
1236 } else {
1237 return p;
1238 }
1239}
1240
Stephen Whitec4dbc372019-05-22 10:50:14 -04001241void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
1242 Line line1 = edge1->fLine;
1243 Line line2 = edge2->fLine;
1244 line1.normalize();
1245 line2.normalize();
1246 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1247 if (cosAngle > 0.999) {
1248 return;
1249 }
1250 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1251 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1252 SkPoint p;
1253 if (line1.intersect(line2, &p)) {
1254 uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0;
1255 v->fPartner = alloc.make<Vertex>(p, alpha);
1256 LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID);
1257 }
1258}
1259
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001260bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001261 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001262 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001263 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001264 }
Stephen White56158ae2017-01-30 14:31:31 -05001265 SkPoint p;
1266 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001267 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001268 Vertex* v;
ethannicholase9709e82016-01-07 13:34:16 -08001269 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001270 Vertex* top = *current;
1271 // If the intersection point is above the current vertex, rewind to the vertex above the
1272 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001273 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001274 top = top->fPrev;
1275 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001276 if (!nearly_flat(c, left)) {
1277 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001278 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001279 if (!nearly_flat(c, right)) {
1280 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001281 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001282 if (p == left->fTop->fPoint) {
1283 v = left->fTop;
1284 } else if (p == left->fBottom->fPoint) {
1285 v = left->fBottom;
1286 } else if (p == right->fTop->fPoint) {
1287 v = right->fTop;
1288 } else if (p == right->fBottom->fPoint) {
1289 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001290 } else {
Stephen White95152e12017-12-18 10:52:44 -05001291 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001292 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001293 v->fSynthetic = true;
1294 compute_bisector(left, right, v, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001295 }
ethannicholase9709e82016-01-07 13:34:16 -08001296 }
Stephen White0cb31672017-06-08 14:41:01 -04001297 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001298 split_edge(left, v, activeEdges, current, c, alloc);
1299 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001300 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001301 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001302 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001303 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001304}
1305
Stephen White3a9aab92017-03-07 14:07:18 -05001306void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
1307 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1308 SkASSERT(contour->fHead);
1309 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001310 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001311 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001312 }
Stephen White3a9aab92017-03-07 14:07:18 -05001313 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001314 if (approximate) {
1315 round(&v->fPoint);
1316 }
Stephen White3a9aab92017-03-07 14:07:18 -05001317 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001318 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001319 if (coincident(prev->fPoint, v->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -08001320 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001321 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001322 } else if (!v->fPoint.isFinite()) {
1323 LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
1324 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001325 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Stephen White06768ca2018-05-25 14:50:56 -04001326 LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
1327 contour->remove(v);
1328 } else {
1329 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001330 }
Stephen White3a9aab92017-03-07 14:07:18 -05001331 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001332 }
1333 }
1334}
1335
Stephen Whitee260c462017-12-19 18:09:54 -05001336bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001337 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001338 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001339 }
Stephen Whitee260c462017-12-19 18:09:54 -05001340 bool merged = false;
1341 for (Vertex* v = mesh->fHead->fNext; v;) {
1342 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001343 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1344 v->fPoint = v->fPrev->fPoint;
1345 }
1346 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001347 merge_vertices(v, v->fPrev, mesh, c, alloc);
1348 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001349 }
Stephen Whitee260c462017-12-19 18:09:54 -05001350 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001351 }
Stephen Whitee260c462017-12-19 18:09:54 -05001352 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001353}
1354
1355// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1356
Stephen White3a9aab92017-03-07 14:07:18 -05001357void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001358 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001359 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1360 Vertex* prev = contour->fTail;
1361 for (Vertex* v = contour->fHead; v;) {
1362 Vertex* next = v->fNext;
1363 connect(prev, v, Edge::Type::kInner, c, alloc);
1364 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001365 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001366 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001367 }
1368 }
ethannicholase9709e82016-01-07 13:34:16 -08001369}
1370
Stephen Whitee260c462017-12-19 18:09:54 -05001371void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1372 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001373 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001374 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1375 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1376 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1377 // number.
1378 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1379 inner->fPartner = outer->fPartner = nullptr;
1380 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001381 }
1382 }
1383}
1384
1385template <CompareFunc sweep_lt>
1386void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1387 Vertex* a = front->fHead;
1388 Vertex* b = back->fHead;
1389 while (a && b) {
1390 if (sweep_lt(a->fPoint, b->fPoint)) {
1391 front->remove(a);
1392 result->append(a);
1393 a = front->fHead;
1394 } else {
1395 back->remove(b);
1396 result->append(b);
1397 b = back->fHead;
1398 }
1399 }
1400 result->append(*front);
1401 result->append(*back);
1402}
1403
1404void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1405 if (c.fDirection == Comparator::Direction::kHorizontal) {
1406 sorted_merge<sweep_lt_horiz>(front, back, result);
1407 } else {
1408 sorted_merge<sweep_lt_vert>(front, back, result);
1409 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001410#if LOGGING_ENABLED
1411 float id = 0.0f;
1412 for (Vertex* v = result->fHead; v; v = v->fNext) {
1413 v->fID = id++;
1414 }
1415#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001416}
1417
ethannicholase9709e82016-01-07 13:34:16 -08001418// Stage 3: sort the vertices by increasing sweep direction.
1419
Stephen White16a40cb2017-02-23 11:10:01 -05001420template <CompareFunc sweep_lt>
1421void merge_sort(VertexList* vertices) {
1422 Vertex* slow = vertices->fHead;
1423 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001424 return;
1425 }
Stephen White16a40cb2017-02-23 11:10:01 -05001426 Vertex* fast = slow->fNext;
1427 if (!fast) {
1428 return;
1429 }
1430 do {
1431 fast = fast->fNext;
1432 if (fast) {
1433 fast = fast->fNext;
1434 slow = slow->fNext;
1435 }
1436 } while (fast);
1437 VertexList front(vertices->fHead, slow);
1438 VertexList back(slow->fNext, vertices->fTail);
1439 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001440
Stephen White16a40cb2017-02-23 11:10:01 -05001441 merge_sort<sweep_lt>(&front);
1442 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001443
Stephen White16a40cb2017-02-23 11:10:01 -05001444 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001445 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001446}
1447
Stephen White95152e12017-12-18 10:52:44 -05001448void dump_mesh(const VertexList& mesh) {
1449#if LOGGING_ENABLED
1450 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
1451 LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1452 if (Vertex* p = v->fPartner) {
1453 LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
1454 } else {
1455 LOG(", null partner\n");
1456 }
1457 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1458 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1459 }
1460 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1461 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1462 }
1463 }
1464#endif
1465}
1466
Stephen Whitec4dbc372019-05-22 10:50:14 -04001467void dump_skel(const SSEdgeList& ssEdges) {
1468#if LOGGING_ENABLED
Stephen Whitec4dbc372019-05-22 10:50:14 -04001469 for (SSEdge* edge : ssEdges) {
1470 if (edge->fEdge) {
Stephen White8a3c0592019-05-29 11:26:16 -04001471 LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001472 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001473 edge->fNext->fVertex->fID);
1474 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
1475 LOG(" (original %g -> %g)\n",
1476 edge->fEdge->fTop->fID,
1477 edge->fEdge->fBottom->fID);
1478 } else {
1479 LOG("\n");
1480 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001481 }
1482 }
1483#endif
1484}
1485
Stephen White89042d52018-06-08 12:18:22 -04001486#ifdef SK_DEBUG
1487void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1488 if (!left || !right) {
1489 return;
1490 }
1491 if (left->fTop == right->fTop) {
1492 SkASSERT(left->isLeftOf(right->fBottom));
1493 SkASSERT(right->isRightOf(left->fBottom));
1494 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1495 SkASSERT(left->isLeftOf(right->fTop));
1496 } else {
1497 SkASSERT(right->isRightOf(left->fTop));
1498 }
1499 if (left->fBottom == right->fBottom) {
1500 SkASSERT(left->isLeftOf(right->fTop));
1501 SkASSERT(right->isRightOf(left->fTop));
1502 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1503 SkASSERT(left->isLeftOf(right->fBottom));
1504 } else {
1505 SkASSERT(right->isRightOf(left->fBottom));
1506 }
1507}
1508
1509void validate_edge_list(EdgeList* edges, Comparator& c) {
1510 Edge* left = edges->fHead;
1511 if (!left) {
1512 return;
1513 }
1514 for (Edge* right = left->fRight; right; right = right->fRight) {
1515 validate_edge_pair(left, right, c);
1516 left = right;
1517 }
1518}
1519#endif
1520
ethannicholase9709e82016-01-07 13:34:16 -08001521// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1522
Stephen Whitec4dbc372019-05-22 10:50:14 -04001523bool connected(Vertex* v) {
1524 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1525}
1526
Stephen Whitee260c462017-12-19 18:09:54 -05001527bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001528 LOG("simplifying complex polygons\n");
1529 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001530 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001531 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001532 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001533 continue;
1534 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001535 Edge* leftEnclosingEdge;
1536 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001537 bool restartChecks;
1538 do {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001539 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001540 restartChecks = false;
1541 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001542 v->fLeftEnclosingEdge = leftEnclosingEdge;
1543 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001544 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001545 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001546 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001547 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001548 restartChecks = true;
1549 break;
1550 }
Stephen White0cb31672017-06-08 14:41:01 -04001551 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001552 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001553 restartChecks = true;
1554 break;
1555 }
1556 }
1557 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001558 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001559 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001560 restartChecks = true;
1561 }
1562
1563 }
Stephen Whitee260c462017-12-19 18:09:54 -05001564 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001565 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001566#ifdef SK_DEBUG
1567 validate_edge_list(&activeEdges, c);
1568#endif
ethannicholase9709e82016-01-07 13:34:16 -08001569 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1570 remove_edge(e, &activeEdges);
1571 }
1572 Edge* leftEdge = leftEnclosingEdge;
1573 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1574 insert_edge(e, leftEdge, &activeEdges);
1575 leftEdge = e;
1576 }
ethannicholase9709e82016-01-07 13:34:16 -08001577 }
Stephen Whitee260c462017-12-19 18:09:54 -05001578 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1579 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001580}
1581
1582// Stage 5: Tessellate the simplified mesh into monotone polygons.
1583
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001584Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001585 LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001586 EdgeList activeEdges;
1587 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001588 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001589 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001590 continue;
1591 }
1592#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001593 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001594#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001595 Edge* leftEnclosingEdge;
1596 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001597 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001598 Poly* leftPoly;
1599 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001600 if (v->fFirstEdgeAbove) {
1601 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1602 rightPoly = v->fLastEdgeAbove->fRightPoly;
1603 } else {
1604 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1605 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1606 }
1607#if LOGGING_ENABLED
1608 LOG("edges above:\n");
1609 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1610 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1611 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1612 }
1613 LOG("edges below:\n");
1614 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1615 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1616 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1617 }
1618#endif
1619 if (v->fFirstEdgeAbove) {
1620 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001621 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001622 }
1623 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001624 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001625 }
1626 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001627 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001628 remove_edge(e, &activeEdges);
1629 if (e->fRightPoly) {
1630 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001631 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001632 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001633 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001634 }
1635 }
1636 remove_edge(v->fLastEdgeAbove, &activeEdges);
1637 if (!v->fFirstEdgeBelow) {
1638 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1639 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1640 rightPoly->fPartner = leftPoly;
1641 leftPoly->fPartner = rightPoly;
1642 }
1643 }
1644 }
1645 if (v->fFirstEdgeBelow) {
1646 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001647 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001648 if (leftPoly == rightPoly) {
1649 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1650 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1651 leftPoly->fWinding, alloc);
1652 leftEnclosingEdge->fRightPoly = leftPoly;
1653 } else {
1654 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1655 rightPoly->fWinding, alloc);
1656 rightEnclosingEdge->fLeftPoly = rightPoly;
1657 }
ethannicholase9709e82016-01-07 13:34:16 -08001658 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001659 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001660 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1661 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001662 }
1663 }
1664 Edge* leftEdge = v->fFirstEdgeBelow;
1665 leftEdge->fLeftPoly = leftPoly;
1666 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1667 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1668 rightEdge = rightEdge->fNextEdgeBelow) {
1669 insert_edge(rightEdge, leftEdge, &activeEdges);
1670 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1671 winding += leftEdge->fWinding;
1672 if (winding != 0) {
1673 Poly* poly = new_poly(&polys, v, winding, alloc);
1674 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1675 }
1676 leftEdge = rightEdge;
1677 }
1678 v->fLastEdgeBelow->fRightPoly = rightPoly;
1679 }
1680#if LOGGING_ENABLED
1681 LOG("\nactive edges:\n");
1682 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1683 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1684 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1685 }
1686#endif
1687 }
1688 return polys;
1689}
1690
Stephen Whitebf6137e2017-01-04 15:43:26 -05001691void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001692 SkArenaAlloc& alloc) {
Stephen White49789062017-02-21 10:35:49 -05001693 LOG("removing non-boundary edges\n");
1694 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001695 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001696 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001697 continue;
1698 }
1699 Edge* leftEnclosingEdge;
1700 Edge* rightEnclosingEdge;
1701 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1702 bool prevFilled = leftEnclosingEdge &&
1703 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1704 for (Edge* e = v->fFirstEdgeAbove; e;) {
1705 Edge* next = e->fNextEdgeAbove;
1706 remove_edge(e, &activeEdges);
1707 bool filled = apply_fill_type(fillType, e->fWinding);
1708 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001709 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001710 }
Stephen White49789062017-02-21 10:35:49 -05001711 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001712 e = next;
1713 }
Stephen White49789062017-02-21 10:35:49 -05001714 Edge* prev = leftEnclosingEdge;
1715 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1716 if (prev) {
1717 e->fWinding += prev->fWinding;
1718 }
1719 insert_edge(e, prev, &activeEdges);
1720 prev = e;
1721 }
senorblancof57372d2016-08-31 10:36:19 -07001722 }
senorblancof57372d2016-08-31 10:36:19 -07001723}
1724
Stephen White66412122017-03-01 11:48:27 -05001725// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001726void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001727 normal->set(SkDoubleToScalar(e->fLine.fA),
1728 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001729}
1730
1731// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1732// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1733// invert on stroking.
1734
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001735void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001736 Edge* prevEdge = boundary->fTail;
1737 SkVector prevNormal;
1738 get_edge_normal(prevEdge, &prevNormal);
1739 for (Edge* e = boundary->fHead; e != nullptr;) {
1740 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1741 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001742 double distPrev = e->dist(prev->fPoint);
1743 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001744 SkVector normal;
1745 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001746 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001747 if (prev == next) {
1748 remove_edge(prevEdge, boundary);
1749 remove_edge(e, boundary);
1750 prevEdge = boundary->fTail;
1751 e = boundary->fHead;
1752 if (prevEdge) {
1753 get_edge_normal(prevEdge, &prevNormal);
1754 }
1755 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001756 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001757 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001758 if (prev->fPoint != next->fPoint) {
1759 join->fLine.normalize();
1760 join->fLine = join->fLine * join->fWinding;
1761 }
senorblancof57372d2016-08-31 10:36:19 -07001762 insert_edge(join, e, boundary);
1763 remove_edge(prevEdge, boundary);
1764 remove_edge(e, boundary);
1765 if (join->fLeft && join->fRight) {
1766 prevEdge = join->fLeft;
1767 e = join;
1768 } else {
1769 prevEdge = boundary->fTail;
1770 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1771 }
1772 get_edge_normal(prevEdge, &prevNormal);
1773 } else {
1774 prevEdge = e;
1775 prevNormal = normal;
1776 e = e->fRight;
1777 }
1778 }
1779}
1780
Stephen Whitec4dbc372019-05-22 10:50:14 -04001781void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) {
1782 if (v == dest) {
1783 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001784 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001785 LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
1786 if (v->fSynthetic) {
1787 connect(v, dest, Edge::Type::kConnector, c, alloc, 0);
1788 } else if (v->fPartner) {
1789 LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1790 LOG("and %g's partner to null\n", v->fID);
1791 v->fPartner->fPartner = dest;
1792 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001793 }
1794}
1795
Stephen Whitec4dbc372019-05-22 10:50:14 -04001796void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) {
1797 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001798 return;
1799 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001800 Vertex* prev = fEdge->fPrev->fVertex;
1801 Vertex* next = fEdge->fNext->fVertex;
1802 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1803 SSEdge* nextEdge = fEdge->fNext->fNext;
1804 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1805 return;
Stephen White77169c82018-06-05 09:15:59 -04001806 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001807 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1808 dest->fSynthetic = true;
1809 SSVertex* ssv = alloc.make<SSVertex>(dest);
1810 LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1811 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID,
1812 dest->fID, fPoint.fX, fPoint.fY, fAlpha);
1813 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001814
Stephen Whitec4dbc372019-05-22 10:50:14 -04001815 ss_connect(prev, dest, c, alloc);
1816 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001817
Stephen Whitec4dbc372019-05-22 10:50:14 -04001818 prevEdge->fNext = nextEdge->fPrev = ssv;
1819 ssv->fPrev = prevEdge;
1820 ssv->fNext = nextEdge;
1821 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1822 return;
1823 }
1824 if (prevEdge->fEvent) {
1825 prevEdge->fEvent->fEdge = nullptr;
1826 }
1827 if (nextEdge->fEvent) {
1828 nextEdge->fEvent->fEdge = nullptr;
1829 }
1830 if (prevEdge->fPrev == nextEdge->fNext) {
1831 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1832 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1833 } else {
1834 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1835 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1836 if (dest->fPartner) {
1837 create_event(prevEdge, events, alloc);
1838 create_event(nextEdge, events, alloc);
1839 } else {
1840 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1841 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1842 }
1843 }
Stephen Whitee260c462017-12-19 18:09:54 -05001844}
1845
1846bool is_overlap_edge(Edge* e) {
1847 if (e->fType == Edge::Type::kOuter) {
1848 return e->fWinding != 0 && e->fWinding != 1;
1849 } else if (e->fType == Edge::Type::kInner) {
1850 return e->fWinding != 0 && e->fWinding != -2;
1851 } else {
1852 return false;
1853 }
1854}
1855
1856// This is a stripped-down version of tessellate() which computes edges which
1857// join two filled regions, which represent overlap regions, and collapses them.
Stephen Whitec4dbc372019-05-22 10:50:14 -04001858bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc,
1859 EventComparator comp) {
Stephen Whitee260c462017-12-19 18:09:54 -05001860 LOG("\nfinding overlap regions\n");
1861 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001862 EventList events(comp);
1863 SSVertexMap ssVertices;
1864 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001865 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001866 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001867 continue;
1868 }
1869 Edge* leftEnclosingEdge;
1870 Edge* rightEnclosingEdge;
1871 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001872 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001873 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1874 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001875 bool leftOverlap = prev && is_overlap_edge(prev);
1876 bool rightOverlap = is_overlap_edge(e);
1877 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1878 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001879 if (prev) {
1880 e->fWinding -= prev->fWinding;
1881 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001882 if (leftOverlap && rightOverlap) {
1883 LOG("found interior overlap edge %g -> %g, disconnecting\n",
1884 e->fTop->fID, e->fBottom->fID);
1885 disconnect(e);
1886 } else if (leftOverlap || rightOverlap) {
1887 LOG("found overlap edge %g -> %g%s\n", e->fTop->fID, e->fBottom->fID,
1888 isOuterBoundary ? ", is outer boundary" : "");
1889 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1890 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1891 SSVertex* ssPrev = ssVertices[prevVertex];
1892 if (!ssPrev) {
1893 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1894 }
1895 SSVertex* ssNext = ssVertices[nextVertex];
1896 if (!ssNext) {
1897 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1898 }
1899 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1900 ssEdges.push_back(ssEdge);
1901// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1902 ssPrev->fNext = ssNext->fPrev = ssEdge;
1903 create_event(ssEdge, &events, alloc);
1904 if (!isOuterBoundary) {
1905 disconnect(e);
1906 }
1907 }
1908 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001909 }
1910 Edge* prev = leftEnclosingEdge;
1911 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1912 if (prev) {
1913 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001914 }
1915 insert_edge(e, prev, &activeEdges);
1916 prev = e;
1917 }
1918 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001919 bool complex = events.size() > 0;
1920
Stephen Whitee260c462017-12-19 18:09:54 -05001921 LOG("\ncollapsing overlap regions\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001922 LOG("skeleton before:\n");
1923 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001924 while (events.size() > 0) {
1925 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001926 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001927 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001928 }
Stephen White8a3c0592019-05-29 11:26:16 -04001929 LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001930 dump_skel(ssEdges);
1931 for (SSEdge* edge : ssEdges) {
1932 if (Edge* e = edge->fEdge) {
1933 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1934 }
1935 }
1936 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001937}
1938
1939bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1940 if (!prev || !next) {
1941 return true;
1942 }
1943 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1944 return winding != origEdge->fWinding;
1945}
Stephen White92eba8a2017-02-06 09:50:27 -05001946
senorblancof57372d2016-08-31 10:36:19 -07001947// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1948// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1949// new antialiased mesh from those vertices.
1950
Stephen Whitee260c462017-12-19 18:09:54 -05001951void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1952 Comparator& c, SkArenaAlloc& alloc) {
1953 LOG("\nstroking boundary\n");
1954 // A boundary with fewer than 3 edges is degenerate.
1955 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1956 return;
1957 }
1958 Edge* prevEdge = boundary->fTail;
1959 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1960 SkVector prevNormal;
1961 get_edge_normal(prevEdge, &prevNormal);
1962 double radius = 0.5;
1963 Line prevInner(prevEdge->fLine);
1964 prevInner.fC -= radius;
1965 Line prevOuter(prevEdge->fLine);
1966 prevOuter.fC += radius;
1967 VertexList innerVertices;
1968 VertexList outerVertices;
1969 bool innerInversion = true;
1970 bool outerInversion = true;
1971 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1972 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1973 SkVector normal;
1974 get_edge_normal(e, &normal);
1975 Line inner(e->fLine);
1976 inner.fC -= radius;
1977 Line outer(e->fLine);
1978 outer.fC += radius;
1979 SkPoint innerPoint, outerPoint;
1980 LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1981 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1982 prevOuter.intersect(outer, &outerPoint)) {
1983 float cosAngle = normal.dot(prevNormal);
1984 if (cosAngle < -kCosMiterAngle) {
1985 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1986
1987 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
1988 Line bisector(innerPoint, outerPoint);
1989 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
1990 if (tangent.fA == 0 && tangent.fB == 0) {
1991 continue;
1992 }
1993 tangent.normalize();
1994 Line innerTangent(tangent);
1995 Line outerTangent(tangent);
1996 innerTangent.fC -= 0.5;
1997 outerTangent.fC += 0.5;
1998 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
1999 if (prevNormal.cross(normal) > 0) {
2000 // Miter inner points
2001 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
2002 !innerTangent.intersect(inner, &innerPoint2) ||
2003 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002004 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002005 }
2006 Line prevTangent(prevV->fPoint,
2007 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
2008 Line nextTangent(nextV->fPoint,
2009 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002010 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002011 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002012 }
2013 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002014 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002015 }
2016 outerPoint1 = outerPoint2 = outerPoint;
2017 } else {
2018 // Miter outer points
2019 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
2020 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05002021 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05002022 }
2023 Line prevTangent(prevV->fPoint,
2024 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
2025 Line nextTangent(nextV->fPoint,
2026 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05002027 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002028 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002029 }
2030 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05002031 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05002032 }
2033 innerPoint1 = innerPoint2 = innerPoint;
2034 }
Stephen Whiteea495232018-04-03 11:28:15 -04002035 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
2036 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
2037 continue;
2038 }
Stephen Whitee260c462017-12-19 18:09:54 -05002039 LOG("inner (%g, %g), (%g, %g), ",
2040 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
2041 LOG("outer (%g, %g), (%g, %g)\n",
2042 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
2043 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
2044 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
2045 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
2046 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
2047 innerVertex1->fPartner = outerVertex1;
2048 innerVertex2->fPartner = outerVertex2;
2049 outerVertex1->fPartner = innerVertex1;
2050 outerVertex2->fPartner = innerVertex2;
2051 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
2052 innerInversion = false;
2053 }
2054 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
2055 outerInversion = false;
2056 }
2057 innerVertices.append(innerVertex1);
2058 innerVertices.append(innerVertex2);
2059 outerVertices.append(outerVertex1);
2060 outerVertices.append(outerVertex2);
2061 } else {
2062 LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
2063 LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
2064 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
2065 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
2066 innerVertex->fPartner = outerVertex;
2067 outerVertex->fPartner = innerVertex;
2068 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
2069 innerInversion = false;
2070 }
2071 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2072 outerInversion = false;
2073 }
2074 innerVertices.append(innerVertex);
2075 outerVertices.append(outerVertex);
2076 }
2077 }
2078 prevInner = inner;
2079 prevOuter = outer;
2080 prevV = v;
2081 prevEdge = e;
2082 prevNormal = normal;
2083 }
2084 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2085 innerInversion = false;
2086 }
2087 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2088 outerInversion = false;
2089 }
2090 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2091 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2092 // interior inverts).
2093 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2094 // so it will be detected during collapse_overlap_regions().
2095 int innerWinding = innerInversion ? 2 : -2;
2096 int outerWinding = outerInversion ? -1 : 1;
2097 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2098 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2099 }
2100 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2101 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2102 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2103 }
2104 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2105 innerMesh->append(innerVertices);
2106 outerMesh->append(outerVertices);
2107}
senorblancof57372d2016-08-31 10:36:19 -07002108
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002109void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05002110 LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002111 bool down = apply_fill_type(fillType, e->fWinding);
senorblancof57372d2016-08-31 10:36:19 -07002112 while (e) {
2113 e->fWinding = down ? 1 : -1;
2114 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002115 e->fLine.normalize();
2116 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002117 boundary->append(e);
2118 if (down) {
2119 // Find outgoing edge, in clockwise order.
2120 if ((next = e->fNextEdgeAbove)) {
2121 down = false;
2122 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2123 down = true;
2124 } else if ((next = e->fPrevEdgeAbove)) {
2125 down = false;
2126 }
2127 } else {
2128 // Find outgoing edge, in counter-clockwise order.
2129 if ((next = e->fPrevEdgeBelow)) {
2130 down = true;
2131 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2132 down = false;
2133 } else if ((next = e->fNextEdgeBelow)) {
2134 down = true;
2135 }
2136 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002137 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002138 e = next;
2139 }
2140}
2141
Stephen White5ad721e2017-02-23 16:50:47 -05002142// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002143
Stephen Whitebda29c02017-03-13 15:10:13 -04002144void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
2145 VertexList* outerVertices, SkPath::FillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002146 Comparator& c, SkArenaAlloc& alloc) {
2147 remove_non_boundary_edges(inMesh, fillType, alloc);
2148 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002149 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002150 EdgeList boundary;
2151 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2152 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002153 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002154 }
2155 }
senorblancof57372d2016-08-31 10:36:19 -07002156}
2157
Stephen Whitebda29c02017-03-13 15:10:13 -04002158// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002159
Stephen White3a9aab92017-03-07 14:07:18 -05002160void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002161 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002162#if LOGGING_ENABLED
2163 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002164 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002165 SkASSERT(v);
2166 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002167 for (v = v->fNext; v; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002168 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
2169 }
2170 }
2171#endif
senorblancof57372d2016-08-31 10:36:19 -07002172 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002173 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002174}
2175
Stephen Whitebda29c02017-03-13 15:10:13 -04002176void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002177 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002178 return;
ethannicholase9709e82016-01-07 13:34:16 -08002179 }
2180
2181 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002182 if (c.fDirection == Comparator::Direction::kHorizontal) {
2183 merge_sort<sweep_lt_horiz>(vertices);
2184 } else {
2185 merge_sort<sweep_lt_vert>(vertices);
2186 }
ethannicholase9709e82016-01-07 13:34:16 -08002187#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002188 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002189 static float gID = 0.0f;
2190 v->fID = gID++;
2191 }
2192#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002193}
2194
Stephen White3a9aab92017-03-07 14:07:18 -05002195Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType,
Stephen Whitebda29c02017-03-13 15:10:13 -04002196 const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002197 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002198 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2199 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002200 VertexList mesh;
2201 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002202 sort_mesh(&mesh, c, alloc);
2203 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002204 simplify(&mesh, c, alloc);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002205 LOG("\nsimplified mesh:\n");
2206 dump_mesh(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002207 if (antialias) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002208 VertexList innerMesh;
2209 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2210 sort_mesh(&innerMesh, c, alloc);
2211 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002212 merge_coincident_vertices(&innerMesh, c, alloc);
2213 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2214 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2215 was_complex = simplify(outerMesh, c, alloc) || was_complex;
2216 LOG("\ninner mesh before:\n");
2217 dump_mesh(innerMesh);
2218 LOG("\nouter mesh before:\n");
2219 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002220 EventComparator eventLT(EventComparator::Op::kLessThan);
2221 EventComparator eventGT(EventComparator::Op::kGreaterThan);
2222 was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex;
2223 was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002224 if (was_complex) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002225 LOG("found complex mesh; taking slow path\n");
2226 VertexList aaMesh;
Stephen White95152e12017-12-18 10:52:44 -05002227 LOG("\ninner mesh after:\n");
2228 dump_mesh(innerMesh);
2229 LOG("\nouter mesh after:\n");
2230 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002231 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002232 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002233 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2234 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002235 simplify(&aaMesh, c, alloc);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002236 LOG("combined and simplified mesh:\n");
Stephen White95152e12017-12-18 10:52:44 -05002237 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002238 outerMesh->fHead = outerMesh->fTail = nullptr;
2239 return tessellate(aaMesh, alloc);
2240 } else {
2241 LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002242 return tessellate(innerMesh, alloc);
2243 }
Stephen White49789062017-02-21 10:35:49 -05002244 } else {
2245 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002246 }
senorblancof57372d2016-08-31 10:36:19 -07002247}
2248
2249// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Brian Osman0995fd52019-01-09 09:52:25 -05002250void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, bool emitCoverage, void* data) {
senorblancof57372d2016-08-31 10:36:19 -07002251 for (Poly* poly = polys; poly; poly = poly->fNext) {
2252 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002253 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002254 }
2255 }
2256 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002257}
2258
halcanary9d524f22016-03-29 09:03:52 -07002259Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen Whitebda29c02017-03-13 15:10:13 -04002260 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
2261 VertexList* outerMesh) {
ethannicholase9709e82016-01-07 13:34:16 -08002262 SkPath::FillType fillType = path.getFillType();
2263 if (SkPath::IsInverseFillType(fillType)) {
2264 contourCnt++;
2265 }
Stephen White3a9aab92017-03-07 14:07:18 -05002266 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002267
2268 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07002269 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Stephen Whitebda29c02017-03-13 15:10:13 -04002270 antialias, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002271}
2272
Stephen White11f65e02017-02-16 19:00:39 -05002273int get_contour_count(const SkPath& path, SkScalar tolerance) {
2274 int contourCnt;
2275 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002276 if (maxPts <= 0) {
Stephen White11f65e02017-02-16 19:00:39 -05002277 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002278 }
Stephen White11f65e02017-02-16 19:00:39 -05002279 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002280}
2281
Greg Danield5b45932018-06-07 13:15:10 -04002282int64_t count_points(Poly* polys, SkPath::FillType fillType) {
2283 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002284 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002285 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002286 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2287 }
2288 }
2289 return count;
2290}
2291
Greg Danield5b45932018-06-07 13:15:10 -04002292int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2293 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002294 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2295 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2296 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2297 }
2298 }
2299 return count;
2300}
2301
Brian Osman0995fd52019-01-09 09:52:25 -05002302void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002303 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2304 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2305 Vertex* v0 = e->fTop;
2306 Vertex* v1 = e->fBottom;
2307 Vertex* v2 = e->fBottom->fPartner;
2308 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002309 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2310 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002311 }
2312 }
2313 return data;
2314}
2315
ethannicholase9709e82016-01-07 13:34:16 -08002316} // namespace
2317
2318namespace GrTessellator {
2319
2320// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2321
halcanary9d524f22016-03-29 09:03:52 -07002322int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Brian Osman0995fd52019-01-09 09:52:25 -05002323 VertexAllocator* vertexAllocator, bool antialias, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002324 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002325 if (contourCnt <= 0) {
2326 *isLinear = true;
2327 return 0;
2328 }
Stephen White11f65e02017-02-16 19:00:39 -05002329 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002330 VertexList outerMesh;
senorblancof57372d2016-08-31 10:36:19 -07002331 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
Stephen Whitebda29c02017-03-13 15:10:13 -04002332 isLinear, &outerMesh);
senorblanco7ab96e92016-10-12 06:47:44 -07002333 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002334 int64_t count64 = count_points(polys, fillType);
Stephen Whitebda29c02017-03-13 15:10:13 -04002335 if (antialias) {
Greg Danield5b45932018-06-07 13:15:10 -04002336 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002337 }
Greg Danield5b45932018-06-07 13:15:10 -04002338 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002339 return 0;
2340 }
Greg Danield5b45932018-06-07 13:15:10 -04002341 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002342
senorblancof57372d2016-08-31 10:36:19 -07002343 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08002344 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002345 SkDebugf("Could not allocate vertices\n");
2346 return 0;
2347 }
senorblancof57372d2016-08-31 10:36:19 -07002348
2349 LOG("emitting %d verts\n", count);
Brian Osman80879d42019-01-07 16:15:27 -05002350 void* end = polys_to_triangles(polys, fillType, antialias, verts);
2351 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002352
senorblancof57372d2016-08-31 10:36:19 -07002353 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
2354 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08002355 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002356 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002357 return actualCount;
2358}
2359
halcanary9d524f22016-03-29 09:03:52 -07002360int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002361 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002362 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002363 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002364 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002365 return 0;
2366 }
Stephen White11f65e02017-02-16 19:00:39 -05002367 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002368 bool isLinear;
Stephen Whitebda29c02017-03-13 15:10:13 -04002369 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
2370 nullptr);
ethannicholase9709e82016-01-07 13:34:16 -08002371 SkPath::FillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002372 int64_t count64 = count_points(polys, fillType);
2373 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002374 *verts = nullptr;
2375 return 0;
2376 }
Greg Danield5b45932018-06-07 13:15:10 -04002377 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002378
2379 *verts = new GrTessellator::WindingVertex[count];
2380 GrTessellator::WindingVertex* vertsEnd = *verts;
2381 SkPoint* points = new SkPoint[count];
2382 SkPoint* pointsEnd = points;
2383 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002384 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002385 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002386 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002387 while (start != pointsEnd) {
2388 vertsEnd->fPos = *start;
2389 vertsEnd->fWinding = poly->fWinding;
2390 ++start;
2391 ++vertsEnd;
2392 }
2393 }
2394 }
2395 int actualCount = static_cast<int>(vertsEnd - *verts);
2396 SkASSERT(actualCount <= count);
2397 SkASSERT(pointsEnd - points == actualCount);
2398 delete[] points;
2399 return actualCount;
2400}
2401
2402} // namespace