blob: 0138dbf61648488be71f92e84517afe4385aca82 [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
8#include "GrTessellator.h"
9
senorblancof57372d2016-08-31 10:36:19 -070010#include "GrDefaultGeoProcFactory.h"
ethannicholase9709e82016-01-07 13:34:16 -080011#include "GrPathUtils.h"
Brian Osmanf9aabff2018-11-13 16:11:38 -050012#include "GrVertexWriter.h"
ethannicholase9709e82016-01-07 13:34:16 -080013
Herb Derby5cdc9dd2017-02-13 12:10:46 -050014#include "SkArenaAlloc.h"
senorblanco6599eff2016-03-10 08:38:45 -080015#include "SkGeometry.h"
16#include "SkPath.h"
Cary Clarkdf429f32017-11-08 11:44:31 -050017#include "SkPointPriv.h"
Stephen Whitee260c462017-12-19 18:09:54 -050018#include "SkTDPQueue.h"
ethannicholase9709e82016-01-07 13:34:16 -080019
Stephen White94b7e542018-01-04 14:01:10 -050020#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040021#include <cstdio>
22#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080023
24/*
senorblancof57372d2016-08-31 10:36:19 -070025 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080026 *
27 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
28 * 2) Build a mesh of edges connecting the vertices (build_edges()).
29 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
30 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
31 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
32 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
33 *
senorblancof57372d2016-08-31 10:36:19 -070034 * For screenspace antialiasing, the algorithm is modified as follows:
35 *
36 * Run steps 1-5 above to produce polygons.
37 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040038 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070039 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
40 * 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 -040041 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070042 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
43 *
ethannicholase9709e82016-01-07 13:34:16 -080044 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
45 * of vertices (and the necessity of inserting new vertices on intersection).
46 *
Stephen Whitebda29c02017-03-13 15:10:13 -040047 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080048 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
49 * left-to-right based on the point where both edges are active (when both top vertices
50 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
51 * (shared), it's sorted based on the last point where both edges are active, so the
52 * "upper" bottom vertex.
53 *
54 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
55 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
56 * not exact and may violate the mesh topology or active edge list ordering. We
57 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040058 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080059 *
60 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
61 * neighbouring edges at the top or bottom vertex. This is handled by merging the
62 * edges (merge_collinear_edges()).
63 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen White019f6c02017-06-09 10:06:26 -040064 * active edge list. This is handled by detecting potential violations and rewinding
Stephen White3b5a3fa2017-06-06 14:51:19 -040065 * the active edge list to the vertex before they occur (rewind() during merging,
66 * rewind_if_necessary() during splitting).
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)
ethannicholase9709e82016-01-07 13:34:16 -0800156#if LOGGING_ENABLED
157 , fID (-1.0f)
158#endif
159 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400160 SkPoint fPoint; // Vertex position
161 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
162 Vertex* fNext; // "
163 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
164 Edge* fLastEdgeAbove; // "
165 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
166 Edge* fLastEdgeBelow; // "
167 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
168 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
169 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700170 uint8_t fAlpha;
ethannicholase9709e82016-01-07 13:34:16 -0800171#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400172 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800173#endif
174};
175
176/***************************************************************************************/
177
178typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
179
ethannicholase9709e82016-01-07 13:34:16 -0800180bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500181 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800182}
183
184bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500185 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800186}
187
Stephen White16a40cb2017-02-23 11:10:01 -0500188struct Comparator {
189 enum class Direction { kVertical, kHorizontal };
190 Comparator(Direction direction) : fDirection(direction) {}
191 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
192 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
193 }
Stephen White16a40cb2017-02-23 11:10:01 -0500194 Direction fDirection;
195};
196
Brian Osman0995fd52019-01-09 09:52:25 -0500197inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -0500198 GrVertexWriter verts{data};
199 verts.write(v->fPoint);
200
Brian Osman80879d42019-01-07 16:15:27 -0500201 if (emitCoverage) {
202 verts.write(GrNormalizeByteToFloat(v->fAlpha));
203 }
Brian Osman0995fd52019-01-09 09:52:25 -0500204
Brian Osmanf9aabff2018-11-13 16:11:38 -0500205 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800206}
207
Brian Osman0995fd52019-01-09 09:52:25 -0500208void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Stephen White95152e12017-12-18 10:52:44 -0500209 LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
210 LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
211 LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700212#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500213 data = emit_vertex(v0, emitCoverage, data);
214 data = emit_vertex(v1, emitCoverage, data);
215 data = emit_vertex(v1, emitCoverage, data);
216 data = emit_vertex(v2, emitCoverage, data);
217 data = emit_vertex(v2, emitCoverage, data);
218 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800219#else
Brian Osman0995fd52019-01-09 09:52:25 -0500220 data = emit_vertex(v0, emitCoverage, data);
221 data = emit_vertex(v1, emitCoverage, data);
222 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800223#endif
224 return data;
225}
226
senorblancoe6eaa322016-03-08 09:06:44 -0800227struct VertexList {
228 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500229 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800230 Vertex* fHead;
231 Vertex* fTail;
232 void insert(Vertex* v, Vertex* prev, Vertex* next) {
233 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
234 }
235 void append(Vertex* v) {
236 insert(v, fTail, nullptr);
237 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400238 void append(const VertexList& list) {
239 if (!list.fHead) {
240 return;
241 }
242 if (fTail) {
243 fTail->fNext = list.fHead;
244 list.fHead->fPrev = fTail;
245 } else {
246 fHead = list.fHead;
247 }
248 fTail = list.fTail;
249 }
senorblancoe6eaa322016-03-08 09:06:44 -0800250 void prepend(Vertex* v) {
251 insert(v, nullptr, fHead);
252 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500253 void remove(Vertex* v) {
254 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
255 }
senorblancof57372d2016-08-31 10:36:19 -0700256 void close() {
257 if (fHead && fTail) {
258 fTail->fNext = fHead;
259 fHead->fPrev = fTail;
260 }
261 }
senorblancoe6eaa322016-03-08 09:06:44 -0800262};
263
senorblancof57372d2016-08-31 10:36:19 -0700264// Round to nearest quarter-pixel. This is used for screenspace tessellation.
265
266inline void round(SkPoint* p) {
267 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
268 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
269}
270
Stephen White94b7e542018-01-04 14:01:10 -0500271inline SkScalar double_to_clamped_scalar(double d) {
272 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
273}
274
senorblanco49df8d12016-10-07 08:36:56 -0700275// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
276struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500277 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700278 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
279 Line(const SkPoint& p, const SkPoint& q)
280 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
281 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
282 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
283 static_cast<double>(p.fX) * q.fY) {}
284 double dist(const SkPoint& p) const {
285 return fA * p.fX + fB * p.fY + fC;
286 }
Stephen Whitee260c462017-12-19 18:09:54 -0500287 Line operator*(double v) const {
288 return Line(fA * v, fB * v, fC * v);
289 }
senorblanco49df8d12016-10-07 08:36:56 -0700290 double magSq() const {
291 return fA * fA + fB * fB;
292 }
Stephen Whitee260c462017-12-19 18:09:54 -0500293 void normalize() {
294 double len = sqrt(this->magSq());
295 if (len == 0.0) {
296 return;
297 }
298 double scale = 1.0f / len;
299 fA *= scale;
300 fB *= scale;
301 fC *= scale;
302 }
303 bool nearParallel(const Line& o) const {
304 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
305 }
senorblanco49df8d12016-10-07 08:36:56 -0700306
307 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500308 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700309 double denom = fA * other.fB - fB * other.fA;
310 if (denom == 0.0) {
311 return false;
312 }
Stephen White94b7e542018-01-04 14:01:10 -0500313 double scale = 1.0 / denom;
314 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
315 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500316 round(point);
senorblanco49df8d12016-10-07 08:36:56 -0700317 return true;
318 }
319 double fA, fB, fC;
320};
321
ethannicholase9709e82016-01-07 13:34:16 -0800322/**
323 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
324 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
325 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whitebda29c02017-03-13 15:10:13 -0400326 * point). For speed, that case is only tested by the callers that require it (e.g.,
Stephen White3b5a3fa2017-06-06 14:51:19 -0400327 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
ethannicholase9709e82016-01-07 13:34:16 -0800328 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
329 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
330 * a lot faster in the "not found" case.
331 *
332 * The coefficients of the line equation stored in double precision to avoid catastrphic
333 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
334 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
335 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
336 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
337 * this file).
338 */
339
340struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500341 enum class Type { kInner, kOuter, kConnector };
342 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800343 : fWinding(winding)
344 , fTop(top)
345 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500346 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800347 , fLeft(nullptr)
348 , fRight(nullptr)
349 , fPrevEdgeAbove(nullptr)
350 , fNextEdgeAbove(nullptr)
351 , fPrevEdgeBelow(nullptr)
352 , fNextEdgeBelow(nullptr)
353 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700354 , fRightPoly(nullptr)
Stephen Whitee260c462017-12-19 18:09:54 -0500355 , fEvent(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700356 , fLeftPolyPrev(nullptr)
357 , fLeftPolyNext(nullptr)
358 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700359 , fRightPolyNext(nullptr)
Stephen Whitee260c462017-12-19 18:09:54 -0500360 , fOverlap(false)
senorblanco70f52512016-08-17 14:56:22 -0700361 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700362 , fUsedInRightPoly(false)
363 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800364 }
365 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
366 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
367 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500368 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800369 Edge* fLeft; // The linked list of edges in the active edge list.
370 Edge* fRight; // "
371 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
372 Edge* fNextEdgeAbove; // "
373 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
374 Edge* fNextEdgeBelow; // "
375 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
376 Poly* fRightPoly; // The Poly to the right of this edge, if any.
Stephen Whitee260c462017-12-19 18:09:54 -0500377 Event* fEvent;
senorblanco531237e2016-06-02 11:36:48 -0700378 Edge* fLeftPolyPrev;
379 Edge* fLeftPolyNext;
380 Edge* fRightPolyPrev;
381 Edge* fRightPolyNext;
Stephen Whitee260c462017-12-19 18:09:54 -0500382 bool fOverlap; // True if there's an overlap region adjacent to this edge.
senorblanco70f52512016-08-17 14:56:22 -0700383 bool fUsedInLeftPoly;
384 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700385 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800386 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700387 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800388 }
389 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700390 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800391 }
392 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700393 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800394 }
395 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700396 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800397 }
Stephen White95152e12017-12-18 10:52:44 -0500398 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
ethannicholase9709e82016-01-07 13:34:16 -0800399 LOG("intersecting %g -> %g with %g -> %g\n",
400 fTop->fID, fBottom->fID,
401 other.fTop->fID, other.fBottom->fID);
402 if (fTop == other.fTop || fBottom == other.fBottom) {
403 return false;
404 }
senorblanco49df8d12016-10-07 08:36:56 -0700405 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800406 if (denom == 0.0) {
407 return false;
408 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500409 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
410 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
411 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
412 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800413 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
414 // This saves us doing the divide below unless absolutely necessary.
415 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
416 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
417 return false;
418 }
419 double s = sNumer / denom;
420 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700421 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
422 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500423 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500424 if (fType == Type::kConnector) {
425 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
426 } else if (other.fType == Type::kConnector) {
427 double t = tNumer / denom;
428 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500429 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
430 *alpha = 0;
431 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500432 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500433 }
434 }
ethannicholase9709e82016-01-07 13:34:16 -0800435 return true;
436 }
senorblancof57372d2016-08-31 10:36:19 -0700437};
438
439struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500440 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700441 Edge* fHead;
442 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700443 void insert(Edge* edge, Edge* prev, Edge* next) {
444 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700445 }
446 void append(Edge* e) {
447 insert(e, fTail, nullptr);
448 }
449 void remove(Edge* edge) {
450 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700451 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400452 void removeAll() {
453 while (fHead) {
454 this->remove(fHead);
455 }
456 }
senorblancof57372d2016-08-31 10:36:19 -0700457 void close() {
458 if (fHead && fTail) {
459 fTail->fRight = fHead;
460 fHead->fLeft = fTail;
461 }
462 }
463 bool contains(Edge* edge) const {
464 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800465 }
466};
467
Stephen Whitee260c462017-12-19 18:09:54 -0500468struct Event {
Stephen White77169c82018-06-05 09:15:59 -0400469 Event(Edge* edge, bool isOuterBoundary, const SkPoint& point, uint8_t alpha)
470 : fEdge(edge), fIsOuterBoundary(isOuterBoundary), fPoint(point), fAlpha(alpha)
471 , fPrev(nullptr), fNext(nullptr) {
Stephen Whitee260c462017-12-19 18:09:54 -0500472 }
473 Edge* fEdge;
Stephen White77169c82018-06-05 09:15:59 -0400474 bool fIsOuterBoundary;
Stephen Whitee260c462017-12-19 18:09:54 -0500475 SkPoint fPoint;
476 uint8_t fAlpha;
477 Event* fPrev;
478 Event* fNext;
479 void apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc);
480};
481
482bool compare(Event* const& e1, Event* const& e2) {
483 return e1->fAlpha > e2->fAlpha;
484}
485
486struct EventList : public SkTDPQueue<Event*, &compare> {};
487
Stephen White77169c82018-06-05 09:15:59 -0400488void create_event(Edge* e, bool isOuterBoundary, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitee260c462017-12-19 18:09:54 -0500489 Edge bisector1(e->fTop, e->fTop->fPartner, 1, Edge::Type::kConnector);
490 Edge bisector2(e->fBottom, e->fBottom->fPartner, 1, Edge::Type::kConnector);
491 SkPoint p;
492 uint8_t alpha;
493 if (bisector1.intersect(bisector2, &p, &alpha)) {
494 LOG("found overlap edge %g -> %g, will collapse to %g,%g alpha %d\n",
495 e->fTop->fID, e->fBottom->fID, p.fX, p.fY, alpha);
Stephen White77169c82018-06-05 09:15:59 -0400496 e->fEvent = alloc.make<Event>(e, isOuterBoundary, p, alpha);
Stephen Whitee260c462017-12-19 18:09:54 -0500497 events->insert(e->fEvent);
498 }
499}
Stephen Whitee260c462017-12-19 18:09:54 -0500500
ethannicholase9709e82016-01-07 13:34:16 -0800501/***************************************************************************************/
502
503struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700504 Poly(Vertex* v, int winding)
505 : fFirstVertex(v)
506 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800507 , fHead(nullptr)
508 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800509 , fNext(nullptr)
510 , fPartner(nullptr)
511 , fCount(0)
512 {
513#if LOGGING_ENABLED
514 static int gID = 0;
515 fID = gID++;
516 LOG("*** created Poly %d\n", fID);
517#endif
518 }
senorblanco531237e2016-06-02 11:36:48 -0700519 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800520 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700521 MonotonePoly(Edge* edge, Side side)
522 : fSide(side)
523 , fFirstEdge(nullptr)
524 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800525 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700526 , fNext(nullptr) {
527 this->addEdge(edge);
528 }
ethannicholase9709e82016-01-07 13:34:16 -0800529 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700530 Edge* fFirstEdge;
531 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800532 MonotonePoly* fPrev;
533 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700534 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800535 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700536 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700537 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
538 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700539 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800540 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700541 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700542 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
543 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700544 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800545 }
ethannicholase9709e82016-01-07 13:34:16 -0800546 }
547
Brian Osman0995fd52019-01-09 09:52:25 -0500548 void* emit(bool emitCoverage, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700549 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700550 VertexList vertices;
551 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500552 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700553 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700554 if (kRight_Side == fSide) {
555 vertices.append(e->fBottom);
556 e = e->fRightPolyNext;
557 } else {
558 vertices.prepend(e->fBottom);
559 e = e->fLeftPolyNext;
560 }
Stephen White651cbe92017-03-03 12:24:16 -0500561 count++;
senorblanco531237e2016-06-02 11:36:48 -0700562 }
563 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800564 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700565 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800566 SkASSERT(v && v->fPrev && v->fNext);
567 Vertex* prev = v->fPrev;
568 Vertex* curr = v;
569 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500570 if (count == 3) {
Brian Osman0995fd52019-01-09 09:52:25 -0500571 return emit_triangle(prev, curr, next, emitCoverage, data);
Stephen White651cbe92017-03-03 12:24:16 -0500572 }
ethannicholase9709e82016-01-07 13:34:16 -0800573 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
574 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
575 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
576 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
577 if (ax * by - ay * bx >= 0.0) {
Brian Osman0995fd52019-01-09 09:52:25 -0500578 data = emit_triangle(prev, curr, next, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800579 v->fPrev->fNext = v->fNext;
580 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500581 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800582 if (v->fPrev == first) {
583 v = v->fNext;
584 } else {
585 v = v->fPrev;
586 }
587 } else {
588 v = v->fNext;
589 }
590 }
591 return data;
592 }
593 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500594 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
senorblanco70f52512016-08-17 14:56:22 -0700595 LOG("addEdge (%g -> %g) to poly %d, %s side\n",
596 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800597 Poly* partner = fPartner;
598 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700599 if (side == kRight_Side) {
600 if (e->fUsedInRightPoly) {
601 return this;
602 }
603 } else {
604 if (e->fUsedInLeftPoly) {
605 return this;
606 }
607 }
ethannicholase9709e82016-01-07 13:34:16 -0800608 if (partner) {
609 fPartner = partner->fPartner = nullptr;
610 }
senorblanco531237e2016-06-02 11:36:48 -0700611 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500612 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700613 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700614 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
615 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700616 } else if (side == fTail->fSide) {
617 fTail->addEdge(e);
618 fCount++;
619 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500620 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700621 fTail->addEdge(e);
622 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800623 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700624 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800625 poly = partner;
626 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500627 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700628 m->fPrev = fTail;
629 fTail->fNext = m;
630 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800631 }
632 }
ethannicholase9709e82016-01-07 13:34:16 -0800633 return poly;
634 }
Brian Osman0995fd52019-01-09 09:52:25 -0500635 void* emit(bool emitCoverage, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800636 if (fCount < 3) {
637 return data;
638 }
639 LOG("emit() %d, size %d\n", fID, fCount);
640 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
Brian Osman0995fd52019-01-09 09:52:25 -0500641 data = m->emit(emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800642 }
643 return data;
644 }
senorblanco531237e2016-06-02 11:36:48 -0700645 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
646 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800647 int fWinding;
648 MonotonePoly* fHead;
649 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800650 Poly* fNext;
651 Poly* fPartner;
652 int fCount;
653#if LOGGING_ENABLED
654 int fID;
655#endif
656};
657
658/***************************************************************************************/
659
660bool coincident(const SkPoint& a, const SkPoint& b) {
661 return a == b;
662}
663
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500664Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
665 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800666 poly->fNext = *head;
667 *head = poly;
668 return poly;
669}
670
Stephen White3a9aab92017-03-07 14:07:18 -0500671void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500672 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800673#if LOGGING_ENABLED
674 static float gID = 0.0f;
675 v->fID = gID++;
676#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500677 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800678}
679
Stephen White36e4f062017-03-27 16:11:31 -0400680SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
681 SkQuadCoeff quad(pts);
682 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
683 SkPoint mid = to_point(quad.eval(t));
684 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400685 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
686 return 0;
687 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500688 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400689}
690
691void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
692 SkArenaAlloc& alloc) {
693 SkQuadCoeff quad(pts);
694 Sk2s aa = quad.fA * quad.fA;
695 SkScalar denom = 2.0f * (aa[0] + aa[1]);
696 Sk2s ab = quad.fA * quad.fB;
697 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
698 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500699 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400700 // Test possible subdivision values only at the point of maximum curvature.
701 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500702 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400703 u = 1.0f / nPoints;
704 if (quad_error_at(pts, t, u) < toleranceSqd) {
705 break;
706 }
707 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800708 }
Stephen White36e4f062017-03-27 16:11:31 -0400709 for (int j = 1; j <= nPoints; j++) {
710 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
711 }
ethannicholase9709e82016-01-07 13:34:16 -0800712}
713
Stephen White3a9aab92017-03-07 14:07:18 -0500714void generate_cubic_points(const SkPoint& p0,
715 const SkPoint& p1,
716 const SkPoint& p2,
717 const SkPoint& p3,
718 SkScalar tolSqd,
719 VertexList* contour,
720 int pointsLeft,
721 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500722 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
723 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800724 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
725 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500726 append_point_to_contour(p3, contour, alloc);
727 return;
ethannicholase9709e82016-01-07 13:34:16 -0800728 }
729 const SkPoint q[] = {
730 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
731 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
732 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
733 };
734 const SkPoint r[] = {
735 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
736 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
737 };
738 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
739 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500740 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
741 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800742}
743
744// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
745
746void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500747 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800748 SkScalar toleranceSqd = tolerance * tolerance;
749
750 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800751 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500752 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800753 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800754 if (path.isInverseFillType()) {
755 SkPoint quad[4];
756 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700757 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500758 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800759 }
Stephen White3a9aab92017-03-07 14:07:18 -0500760 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800761 }
762 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500763 SkPath::Verb verb;
Stephen White2cee2832017-08-23 09:37:16 -0400764 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800765 switch (verb) {
766 case SkPath::kConic_Verb: {
767 SkScalar weight = iter.conicWeight();
768 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
769 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400770 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800771 quadPts += 2;
772 }
773 *isLinear = false;
774 break;
775 }
776 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500777 if (contour->fHead) {
778 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800779 }
Stephen White3a9aab92017-03-07 14:07:18 -0500780 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800781 break;
782 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500783 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800784 break;
785 }
786 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400787 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800788 *isLinear = false;
789 break;
790 }
791 case SkPath::kCubic_Verb: {
792 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500793 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
794 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800795 *isLinear = false;
796 break;
797 }
798 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800799 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800800 break;
801 }
802 }
803}
804
Stephen White49789062017-02-21 10:35:49 -0500805inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800806 switch (fillType) {
807 case SkPath::kWinding_FillType:
808 return winding != 0;
809 case SkPath::kEvenOdd_FillType:
810 return (winding & 1) != 0;
811 case SkPath::kInverseWinding_FillType:
senorblanco7ab96e92016-10-12 06:47:44 -0700812 return winding == 1;
ethannicholase9709e82016-01-07 13:34:16 -0800813 case SkPath::kInverseEvenOdd_FillType:
814 return (winding & 1) == 1;
815 default:
816 SkASSERT(false);
817 return false;
818 }
819}
820
Stephen White49789062017-02-21 10:35:49 -0500821inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
822 return poly && apply_fill_type(fillType, poly->fWinding);
823}
824
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500825Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500826 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800827 Vertex* top = winding < 0 ? next : prev;
828 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500829 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800830}
831
832void remove_edge(Edge* edge, EdgeList* edges) {
833 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700834 SkASSERT(edges->contains(edge));
835 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800836}
837
838void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
839 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700840 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800841 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700842 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800843}
844
845void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500846 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800847 *left = v->fFirstEdgeAbove->fLeft;
848 *right = v->fLastEdgeAbove->fRight;
849 return;
850 }
851 Edge* next = nullptr;
852 Edge* prev;
853 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
854 if (prev->isLeftOf(v)) {
855 break;
856 }
857 next = prev;
858 }
859 *left = prev;
860 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800861}
862
ethannicholase9709e82016-01-07 13:34:16 -0800863void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
864 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500865 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800866 return;
867 }
868 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
869 Edge* prev = nullptr;
870 Edge* next;
871 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
872 if (next->isRightOf(edge->fTop)) {
873 break;
874 }
875 prev = next;
876 }
senorblancoe6eaa322016-03-08 09:06:44 -0800877 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800878 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
879}
880
881void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
882 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500883 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800884 return;
885 }
886 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
887 Edge* prev = nullptr;
888 Edge* next;
889 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
890 if (next->isRightOf(edge->fBottom)) {
891 break;
892 }
893 prev = next;
894 }
senorblancoe6eaa322016-03-08 09:06:44 -0800895 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800896 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
897}
898
899void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400900 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800901 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
902 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800903 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800904 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
905}
906
907void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400908 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800909 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
910 edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800911 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800912 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
913}
914
Stephen Whitee7a364d2017-01-11 16:19:26 -0500915void disconnect(Edge* edge)
916{
ethannicholase9709e82016-01-07 13:34:16 -0800917 remove_edge_above(edge);
918 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500919}
920
Stephen White3b5a3fa2017-06-06 14:51:19 -0400921void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
922
923void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
924 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
925 return;
926 }
927 Vertex* v = *current;
928 LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
929 while (v != dst) {
930 v = v->fPrev;
931 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
932 remove_edge(e, activeEdges);
933 }
934 Edge* leftEdge = v->fLeftEnclosingEdge;
935 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
936 insert_edge(e, leftEdge, activeEdges);
937 leftEdge = e;
938 }
939 }
940 *current = v;
941}
942
943void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
944 if (!activeEdges || !current) {
945 return;
946 }
947 Vertex* top = edge->fTop;
948 Vertex* bottom = edge->fBottom;
949 if (edge->fLeft) {
950 Vertex* leftTop = edge->fLeft->fTop;
951 Vertex* leftBottom = edge->fLeft->fBottom;
952 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
953 rewind(activeEdges, current, leftTop, c);
954 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
955 rewind(activeEdges, current, top, c);
956 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
957 !edge->fLeft->isLeftOf(bottom)) {
958 rewind(activeEdges, current, leftTop, c);
959 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
960 rewind(activeEdges, current, top, c);
961 }
962 }
963 if (edge->fRight) {
964 Vertex* rightTop = edge->fRight->fTop;
965 Vertex* rightBottom = edge->fRight->fBottom;
966 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
967 rewind(activeEdges, current, rightTop, c);
968 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
969 rewind(activeEdges, current, top, c);
970 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
971 !edge->fRight->isRightOf(bottom)) {
972 rewind(activeEdges, current, rightTop, c);
973 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
974 !edge->isLeftOf(rightBottom)) {
975 rewind(activeEdges, current, top, c);
976 }
ethannicholase9709e82016-01-07 13:34:16 -0800977 }
978}
979
Stephen White3b5a3fa2017-06-06 14:51:19 -0400980void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800981 remove_edge_below(edge);
982 edge->fTop = v;
983 edge->recompute();
984 insert_edge_below(edge, v, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400985 rewind_if_necessary(edge, activeEdges, current, c);
986 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800987}
988
Stephen White3b5a3fa2017-06-06 14:51:19 -0400989void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800990 remove_edge_above(edge);
991 edge->fBottom = v;
992 edge->recompute();
993 insert_edge_above(edge, v, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400994 rewind_if_necessary(edge, activeEdges, current, c);
995 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800996}
997
Stephen White3b5a3fa2017-06-06 14:51:19 -0400998void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
999 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001000 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
1001 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1002 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1003 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001004 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001005 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001006 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001007 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001008 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001009 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001010 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001011 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001012 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001013 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001014 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001015 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001016 }
1017}
1018
Stephen White3b5a3fa2017-06-06 14:51:19 -04001019void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1020 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001021 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
1022 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1023 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1024 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001025 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001026 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001027 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001028 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001029 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001030 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001031 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001032 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001033 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001034 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001035 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001036 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001037 }
1038}
1039
Stephen Whited26b4d82018-07-26 10:02:27 -04001040bool top_collinear(Edge* left, Edge* right) {
1041 if (!left || !right) {
1042 return false;
1043 }
1044 return left->fTop->fPoint == right->fTop->fPoint ||
1045 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1046}
1047
1048bool bottom_collinear(Edge* left, Edge* right) {
1049 if (!left || !right) {
1050 return false;
1051 }
1052 return left->fBottom->fPoint == right->fBottom->fPoint ||
1053 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1054}
1055
Stephen White3b5a3fa2017-06-06 14:51:19 -04001056void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001057 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001058 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001059 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001060 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001061 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001062 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001063 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001064 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001065 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001066 } else {
1067 break;
1068 }
ethannicholase9709e82016-01-07 13:34:16 -08001069 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001070 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1071 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1072 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1073 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001074}
1075
Stephen White89042d52018-06-08 12:18:22 -04001076bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001077 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001078 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001079 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001080 }
ethannicholase9709e82016-01-07 13:34:16 -08001081 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1082 edge->fTop->fID, edge->fBottom->fID,
1083 v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001084 Vertex* top;
1085 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001086 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001087 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001088 top = v;
1089 bottom = edge->fTop;
1090 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001091 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001092 top = edge->fBottom;
1093 bottom = v;
1094 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001095 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001096 top = v;
1097 bottom = edge->fBottom;
1098 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001099 }
Stephen White531a48e2018-06-01 09:49:39 -04001100 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001101 insert_edge_below(newEdge, top, c);
1102 insert_edge_above(newEdge, bottom, c);
1103 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001104 return true;
1105}
1106
1107bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1108 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1109 return false;
1110 }
Stephen White1c5fd182018-07-12 15:54:05 -04001111 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1112 return false;
1113 }
Stephen White89042d52018-06-08 12:18:22 -04001114 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1115 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001116 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001117 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1118 }
1119 } else {
1120 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001121 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001122 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1123 }
1124 }
1125 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1126 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001127 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001128 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1129 }
1130 } else {
1131 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001132 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001133 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1134 }
1135 }
1136 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001137}
1138
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001139Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001140 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001141 if (!prev || !next || prev->fPoint == next->fPoint) {
1142 return nullptr;
1143 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001144 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001145 insert_edge_below(edge, edge->fTop, c);
1146 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001147 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001148 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001149 return edge;
1150}
1151
Stephen Whitebf6137e2017-01-04 15:43:26 -05001152void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001153 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001154 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1155 src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001156 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001157 if (src->fPartner) {
1158 src->fPartner->fPartner = dst;
1159 }
Stephen White7b376942018-05-22 11:51:32 -04001160 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001161 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001162 }
Stephen White7b376942018-05-22 11:51:32 -04001163 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001164 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001165 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001166 mesh->remove(src);
ethannicholase9709e82016-01-07 13:34:16 -08001167}
1168
Stephen White95152e12017-12-18 10:52:44 -05001169Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1170 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1171 Vertex* prevV = reference;
1172 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1173 prevV = prevV->fPrev;
1174 }
1175 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1176 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1177 prevV = nextV;
1178 nextV = nextV->fNext;
1179 }
1180 Vertex* v;
1181 if (prevV && coincident(prevV->fPoint, p)) {
1182 v = prevV;
1183 } else if (nextV && coincident(nextV->fPoint, p)) {
1184 v = nextV;
1185 } else {
1186 v = alloc.make<Vertex>(p, alpha);
1187#if LOGGING_ENABLED
1188 if (!prevV) {
1189 v->fID = mesh->fHead->fID - 1.0f;
1190 } else if (!nextV) {
1191 v->fID = mesh->fTail->fID + 1.0f;
1192 } else {
1193 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1194 }
1195#endif
1196 mesh->insert(v, prevV, nextV);
1197 }
1198 return v;
1199}
1200
Stephen White53a02982018-05-30 22:47:46 -04001201// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1202// sort criterion, it may not be possible to split correctly, since there is no point which is
1203// below the top and above the bottom. This function detects that case.
1204bool nearly_flat(Comparator& c, Edge* edge) {
1205 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1206 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001207 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001208}
1209
Stephen Whitee62999f2018-06-05 18:45:07 -04001210SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1211 if (c.sweep_lt(p, min)) {
1212 return min;
1213 } else if (c.sweep_lt(max, p)) {
1214 return max;
1215 } else {
1216 return p;
1217 }
1218}
1219
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001220bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001221 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001222 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001223 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001224 }
Stephen White56158ae2017-01-30 14:31:31 -05001225 SkPoint p;
1226 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001227 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001228 Vertex* v;
ethannicholase9709e82016-01-07 13:34:16 -08001229 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001230 Vertex* top = *current;
1231 // If the intersection point is above the current vertex, rewind to the vertex above the
1232 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001233 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001234 top = top->fPrev;
1235 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001236 if (!nearly_flat(c, left)) {
1237 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001238 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001239 if (!nearly_flat(c, right)) {
1240 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001241 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001242 if (p == left->fTop->fPoint) {
1243 v = left->fTop;
1244 } else if (p == left->fBottom->fPoint) {
1245 v = left->fBottom;
1246 } else if (p == right->fTop->fPoint) {
1247 v = right->fTop;
1248 } else if (p == right->fBottom->fPoint) {
1249 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001250 } else {
Stephen White95152e12017-12-18 10:52:44 -05001251 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001252 if (left->fTop->fPartner) {
1253 Line line1 = left->fLine;
1254 Line line2 = right->fLine;
1255 int dir = left->fType == Edge::Type::kOuter ? -1 : 1;
1256 line1.fC += sqrt(left->fLine.magSq()) * (left->fWinding > 0 ? 1 : -1) * dir;
1257 line2.fC += sqrt(right->fLine.magSq()) * (right->fWinding > 0 ? 1 : -1) * dir;
Stephen Whitee260c462017-12-19 18:09:54 -05001258 SkPoint p;
1259 if (line1.intersect(line2, &p)) {
1260 LOG("synthesizing partner (%g,%g) for intersection vertex %g\n",
1261 p.fX, p.fY, v->fID);
1262 v->fPartner = alloc.make<Vertex>(p, 255 - v->fAlpha);
1263 }
1264 }
ethannicholase9709e82016-01-07 13:34:16 -08001265 }
Stephen White0cb31672017-06-08 14:41:01 -04001266 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001267 split_edge(left, v, activeEdges, current, c, alloc);
1268 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001269 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001270 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001271 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001272 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001273}
1274
Stephen White3a9aab92017-03-07 14:07:18 -05001275void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
1276 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1277 SkASSERT(contour->fHead);
1278 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001279 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001280 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001281 }
Stephen White3a9aab92017-03-07 14:07:18 -05001282 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001283 if (approximate) {
1284 round(&v->fPoint);
1285 }
Stephen White3a9aab92017-03-07 14:07:18 -05001286 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001287 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001288 if (coincident(prev->fPoint, v->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -08001289 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001290 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001291 } else if (!v->fPoint.isFinite()) {
1292 LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
1293 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001294 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Stephen White06768ca2018-05-25 14:50:56 -04001295 LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
1296 contour->remove(v);
1297 } else {
1298 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001299 }
Stephen White3a9aab92017-03-07 14:07:18 -05001300 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001301 }
1302 }
1303}
1304
Stephen Whitee260c462017-12-19 18:09:54 -05001305bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001306 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001307 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001308 }
Stephen Whitee260c462017-12-19 18:09:54 -05001309 bool merged = false;
1310 for (Vertex* v = mesh->fHead->fNext; v;) {
1311 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001312 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1313 v->fPoint = v->fPrev->fPoint;
1314 }
1315 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001316 merge_vertices(v, v->fPrev, mesh, c, alloc);
1317 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001318 }
Stephen Whitee260c462017-12-19 18:09:54 -05001319 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001320 }
Stephen Whitee260c462017-12-19 18:09:54 -05001321 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001322}
1323
1324// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1325
Stephen White3a9aab92017-03-07 14:07:18 -05001326void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001327 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001328 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1329 Vertex* prev = contour->fTail;
1330 for (Vertex* v = contour->fHead; v;) {
1331 Vertex* next = v->fNext;
1332 connect(prev, v, Edge::Type::kInner, c, alloc);
1333 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001334 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001335 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001336 }
1337 }
ethannicholase9709e82016-01-07 13:34:16 -08001338}
1339
Stephen Whitee260c462017-12-19 18:09:54 -05001340void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1341 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001342 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001343 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1344 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1345 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1346 // number.
1347 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1348 inner->fPartner = outer->fPartner = nullptr;
1349 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001350 }
1351 }
1352}
1353
1354template <CompareFunc sweep_lt>
1355void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1356 Vertex* a = front->fHead;
1357 Vertex* b = back->fHead;
1358 while (a && b) {
1359 if (sweep_lt(a->fPoint, b->fPoint)) {
1360 front->remove(a);
1361 result->append(a);
1362 a = front->fHead;
1363 } else {
1364 back->remove(b);
1365 result->append(b);
1366 b = back->fHead;
1367 }
1368 }
1369 result->append(*front);
1370 result->append(*back);
1371}
1372
1373void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1374 if (c.fDirection == Comparator::Direction::kHorizontal) {
1375 sorted_merge<sweep_lt_horiz>(front, back, result);
1376 } else {
1377 sorted_merge<sweep_lt_vert>(front, back, result);
1378 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001379#if LOGGING_ENABLED
1380 float id = 0.0f;
1381 for (Vertex* v = result->fHead; v; v = v->fNext) {
1382 v->fID = id++;
1383 }
1384#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001385}
1386
ethannicholase9709e82016-01-07 13:34:16 -08001387// Stage 3: sort the vertices by increasing sweep direction.
1388
Stephen White16a40cb2017-02-23 11:10:01 -05001389template <CompareFunc sweep_lt>
1390void merge_sort(VertexList* vertices) {
1391 Vertex* slow = vertices->fHead;
1392 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001393 return;
1394 }
Stephen White16a40cb2017-02-23 11:10:01 -05001395 Vertex* fast = slow->fNext;
1396 if (!fast) {
1397 return;
1398 }
1399 do {
1400 fast = fast->fNext;
1401 if (fast) {
1402 fast = fast->fNext;
1403 slow = slow->fNext;
1404 }
1405 } while (fast);
1406 VertexList front(vertices->fHead, slow);
1407 VertexList back(slow->fNext, vertices->fTail);
1408 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001409
Stephen White16a40cb2017-02-23 11:10:01 -05001410 merge_sort<sweep_lt>(&front);
1411 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001412
Stephen White16a40cb2017-02-23 11:10:01 -05001413 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001414 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001415}
1416
Stephen White95152e12017-12-18 10:52:44 -05001417void dump_mesh(const VertexList& mesh) {
1418#if LOGGING_ENABLED
1419 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
1420 LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1421 if (Vertex* p = v->fPartner) {
1422 LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
1423 } else {
1424 LOG(", null partner\n");
1425 }
1426 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1427 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1428 }
1429 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1430 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1431 }
1432 }
1433#endif
1434}
1435
Stephen White89042d52018-06-08 12:18:22 -04001436#ifdef SK_DEBUG
1437void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1438 if (!left || !right) {
1439 return;
1440 }
1441 if (left->fTop == right->fTop) {
1442 SkASSERT(left->isLeftOf(right->fBottom));
1443 SkASSERT(right->isRightOf(left->fBottom));
1444 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1445 SkASSERT(left->isLeftOf(right->fTop));
1446 } else {
1447 SkASSERT(right->isRightOf(left->fTop));
1448 }
1449 if (left->fBottom == right->fBottom) {
1450 SkASSERT(left->isLeftOf(right->fTop));
1451 SkASSERT(right->isRightOf(left->fTop));
1452 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1453 SkASSERT(left->isLeftOf(right->fBottom));
1454 } else {
1455 SkASSERT(right->isRightOf(left->fBottom));
1456 }
1457}
1458
1459void validate_edge_list(EdgeList* edges, Comparator& c) {
1460 Edge* left = edges->fHead;
1461 if (!left) {
1462 return;
1463 }
1464 for (Edge* right = left->fRight; right; right = right->fRight) {
1465 validate_edge_pair(left, right, c);
1466 left = right;
1467 }
1468}
1469#endif
1470
ethannicholase9709e82016-01-07 13:34:16 -08001471// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1472
Stephen Whitee260c462017-12-19 18:09:54 -05001473bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001474 LOG("simplifying complex polygons\n");
1475 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001476 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001477 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001478 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1479 continue;
1480 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001481 Edge* leftEnclosingEdge;
1482 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001483 bool restartChecks;
1484 do {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001485 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001486 restartChecks = false;
1487 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001488 v->fLeftEnclosingEdge = leftEnclosingEdge;
1489 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001490 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001491 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001492 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001493 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001494 restartChecks = true;
1495 break;
1496 }
Stephen White0cb31672017-06-08 14:41:01 -04001497 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001498 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001499 restartChecks = true;
1500 break;
1501 }
1502 }
1503 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001504 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001505 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001506 restartChecks = true;
1507 }
1508
1509 }
Stephen Whitee260c462017-12-19 18:09:54 -05001510 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001511 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001512#ifdef SK_DEBUG
1513 validate_edge_list(&activeEdges, c);
1514#endif
ethannicholase9709e82016-01-07 13:34:16 -08001515 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1516 remove_edge(e, &activeEdges);
1517 }
1518 Edge* leftEdge = leftEnclosingEdge;
1519 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1520 insert_edge(e, leftEdge, &activeEdges);
1521 leftEdge = e;
1522 }
ethannicholase9709e82016-01-07 13:34:16 -08001523 }
Stephen Whitee260c462017-12-19 18:09:54 -05001524 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1525 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001526}
1527
1528// Stage 5: Tessellate the simplified mesh into monotone polygons.
1529
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001530Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001531 LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001532 EdgeList activeEdges;
1533 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001534 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001535 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1536 continue;
1537 }
1538#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001539 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#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001541 Edge* leftEnclosingEdge;
1542 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001543 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001544 Poly* leftPoly;
1545 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001546 if (v->fFirstEdgeAbove) {
1547 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1548 rightPoly = v->fLastEdgeAbove->fRightPoly;
1549 } else {
1550 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1551 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1552 }
1553#if LOGGING_ENABLED
1554 LOG("edges above:\n");
1555 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1556 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1557 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1558 }
1559 LOG("edges below:\n");
1560 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1561 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1562 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1563 }
1564#endif
1565 if (v->fFirstEdgeAbove) {
1566 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001567 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001568 }
1569 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001570 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001571 }
1572 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001573 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001574 remove_edge(e, &activeEdges);
1575 if (e->fRightPoly) {
1576 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001577 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001578 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001579 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001580 }
1581 }
1582 remove_edge(v->fLastEdgeAbove, &activeEdges);
1583 if (!v->fFirstEdgeBelow) {
1584 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1585 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1586 rightPoly->fPartner = leftPoly;
1587 leftPoly->fPartner = rightPoly;
1588 }
1589 }
1590 }
1591 if (v->fFirstEdgeBelow) {
1592 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001593 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001594 if (leftPoly == rightPoly) {
1595 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1596 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1597 leftPoly->fWinding, alloc);
1598 leftEnclosingEdge->fRightPoly = leftPoly;
1599 } else {
1600 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1601 rightPoly->fWinding, alloc);
1602 rightEnclosingEdge->fLeftPoly = rightPoly;
1603 }
ethannicholase9709e82016-01-07 13:34:16 -08001604 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001605 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001606 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1607 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001608 }
1609 }
1610 Edge* leftEdge = v->fFirstEdgeBelow;
1611 leftEdge->fLeftPoly = leftPoly;
1612 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1613 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1614 rightEdge = rightEdge->fNextEdgeBelow) {
1615 insert_edge(rightEdge, leftEdge, &activeEdges);
1616 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1617 winding += leftEdge->fWinding;
1618 if (winding != 0) {
1619 Poly* poly = new_poly(&polys, v, winding, alloc);
1620 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1621 }
1622 leftEdge = rightEdge;
1623 }
1624 v->fLastEdgeBelow->fRightPoly = rightPoly;
1625 }
1626#if LOGGING_ENABLED
1627 LOG("\nactive edges:\n");
1628 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1629 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1630 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1631 }
1632#endif
1633 }
1634 return polys;
1635}
1636
Stephen Whitebf6137e2017-01-04 15:43:26 -05001637void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001638 SkArenaAlloc& alloc) {
Stephen White49789062017-02-21 10:35:49 -05001639 LOG("removing non-boundary edges\n");
1640 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001641 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen White49789062017-02-21 10:35:49 -05001642 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1643 continue;
1644 }
1645 Edge* leftEnclosingEdge;
1646 Edge* rightEnclosingEdge;
1647 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1648 bool prevFilled = leftEnclosingEdge &&
1649 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1650 for (Edge* e = v->fFirstEdgeAbove; e;) {
1651 Edge* next = e->fNextEdgeAbove;
1652 remove_edge(e, &activeEdges);
1653 bool filled = apply_fill_type(fillType, e->fWinding);
1654 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001655 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001656 }
Stephen White49789062017-02-21 10:35:49 -05001657 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001658 e = next;
1659 }
Stephen White49789062017-02-21 10:35:49 -05001660 Edge* prev = leftEnclosingEdge;
1661 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1662 if (prev) {
1663 e->fWinding += prev->fWinding;
1664 }
1665 insert_edge(e, prev, &activeEdges);
1666 prev = e;
1667 }
senorblancof57372d2016-08-31 10:36:19 -07001668 }
senorblancof57372d2016-08-31 10:36:19 -07001669}
1670
Stephen White66412122017-03-01 11:48:27 -05001671// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001672void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001673 normal->set(SkDoubleToScalar(e->fLine.fA),
1674 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001675}
1676
Stephen Whitee260c462017-12-19 18:09:54 -05001677void reconnect(Edge* edge, Vertex* src, Vertex* dst, Comparator& c) {
1678 disconnect(edge);
1679 if (src == edge->fTop) {
1680 edge->fTop = dst;
1681 } else {
1682 SkASSERT(src == edge->fBottom);
1683 edge->fBottom = dst;
1684 }
1685 if (edge->fEvent) {
1686 edge->fEvent->fEdge = nullptr;
1687 }
1688 if (edge->fTop == edge->fBottom) {
1689 return;
1690 }
1691 if (c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001692 using std::swap;
1693 swap(edge->fTop, edge->fBottom);
Stephen Whitee260c462017-12-19 18:09:54 -05001694 edge->fWinding *= -1;
1695 }
1696 edge->recompute();
1697 insert_edge_below(edge, edge->fTop, c);
1698 insert_edge_above(edge, edge->fBottom, c);
1699 merge_collinear_edges(edge, nullptr, nullptr, c);
1700}
Stephen Whitee260c462017-12-19 18:09:54 -05001701
senorblancof57372d2016-08-31 10:36:19 -07001702// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1703// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1704// invert on stroking.
1705
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001706void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001707 Edge* prevEdge = boundary->fTail;
1708 SkVector prevNormal;
1709 get_edge_normal(prevEdge, &prevNormal);
1710 for (Edge* e = boundary->fHead; e != nullptr;) {
1711 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1712 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001713 double distPrev = e->dist(prev->fPoint);
1714 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001715 SkVector normal;
1716 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001717 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
1718 if (prev != next && prevNormal.dot(normal) < 0.0 &&
1719 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001720 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001721 if (prev->fPoint != next->fPoint) {
1722 join->fLine.normalize();
1723 join->fLine = join->fLine * join->fWinding;
1724 }
senorblancof57372d2016-08-31 10:36:19 -07001725 insert_edge(join, e, boundary);
1726 remove_edge(prevEdge, boundary);
1727 remove_edge(e, boundary);
1728 if (join->fLeft && join->fRight) {
1729 prevEdge = join->fLeft;
1730 e = join;
1731 } else {
1732 prevEdge = boundary->fTail;
1733 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1734 }
1735 get_edge_normal(prevEdge, &prevNormal);
1736 } else {
1737 prevEdge = e;
1738 prevNormal = normal;
1739 e = e->fRight;
1740 }
1741 }
1742}
1743
Stephen Whitee260c462017-12-19 18:09:54 -05001744void reconnect_all_overlap_edges(Vertex* src, Vertex* dst, Edge* current, Comparator& c) {
1745 if (src->fPartner) {
1746 src->fPartner->fPartner = dst;
1747 }
1748 for (Edge* e = src->fFirstEdgeAbove; e; ) {
1749 Edge* next = e->fNextEdgeAbove;
1750 if (e->fOverlap && e != current) {
1751 reconnect(e, src, dst, c);
1752 }
1753 e = next;
1754 }
1755 for (Edge* e = src->fFirstEdgeBelow; e; ) {
1756 Edge* next = e->fNextEdgeBelow;
1757 if (e->fOverlap && e != current) {
1758 reconnect(e, src, dst, c);
1759 }
1760 e = next;
1761 }
1762}
1763
1764void Event::apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen White24289e02018-06-29 17:02:21 -04001765 if (!fEdge || !fEdge->fTop || !fEdge->fBottom) {
Stephen Whitee260c462017-12-19 18:09:54 -05001766 return;
1767 }
1768 Vertex* top = fEdge->fTop;
1769 Vertex* bottom = fEdge->fBottom;
1770 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, fEdge->fTop, c, alloc);
1771 LOG("collapsing edge %g -> %g to %g (%g, %g) alpha %d\n",
1772 top->fID, bottom->fID, dest->fID, fPoint.fX, fPoint.fY, fAlpha);
1773 reconnect_all_overlap_edges(top, dest, fEdge, c);
1774 reconnect_all_overlap_edges(bottom, dest, fEdge, c);
1775
1776 // Since the destination has multiple partners, give it none.
1777 dest->fPartner = nullptr;
Stephen White77169c82018-06-05 09:15:59 -04001778
1779 // Disconnect all collapsed edges except outer boundaries.
1780 // Those are required to preserve shape coverage and winding correctness.
1781 if (!fIsOuterBoundary) {
1782 disconnect(fEdge);
1783 } else {
1784 LOG("edge %g -> %g is outer boundary; not disconnecting.\n",
1785 fEdge->fTop->fID, fEdge->fBottom->fID);
Stephen White85dcf6b2018-07-17 16:14:31 -04001786 fEdge->fWinding = fEdge->fWinding >= 0 ? 1 : -1;
Stephen White77169c82018-06-05 09:15:59 -04001787 }
Stephen Whitee260c462017-12-19 18:09:54 -05001788
1789 // If top still has some connected edges, set its partner to dest.
1790 top->fPartner = top->fFirstEdgeAbove || top->fFirstEdgeBelow ? dest : nullptr;
1791
1792 // If bottom still has some connected edges, set its partner to dest.
1793 bottom->fPartner = bottom->fFirstEdgeAbove || bottom->fFirstEdgeBelow ? dest : nullptr;
1794}
1795
1796bool is_overlap_edge(Edge* e) {
1797 if (e->fType == Edge::Type::kOuter) {
1798 return e->fWinding != 0 && e->fWinding != 1;
1799 } else if (e->fType == Edge::Type::kInner) {
1800 return e->fWinding != 0 && e->fWinding != -2;
1801 } else {
1802 return false;
1803 }
1804}
1805
1806// This is a stripped-down version of tessellate() which computes edges which
1807// join two filled regions, which represent overlap regions, and collapses them.
1808bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1809 LOG("\nfinding overlap regions\n");
1810 EdgeList activeEdges;
1811 EventList events;
1812 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
1813 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1814 continue;
1815 }
1816 Edge* leftEnclosingEdge;
1817 Edge* rightEnclosingEdge;
1818 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1819 for (Edge* e = v->fLastEdgeAbove; e; e = e->fPrevEdgeAbove) {
1820 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1821 remove_edge(e, &activeEdges);
1822 if (prev) {
1823 e->fWinding -= prev->fWinding;
1824 }
1825 }
1826 Edge* prev = leftEnclosingEdge;
1827 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1828 if (prev) {
1829 e->fWinding += prev->fWinding;
1830 e->fOverlap = e->fOverlap || is_overlap_edge(prev);
1831 }
1832 e->fOverlap = e->fOverlap || is_overlap_edge(e);
1833 if (e->fOverlap) {
Stephen White77169c82018-06-05 09:15:59 -04001834 // If this edge borders a zero-winding area, it's a boundary; don't disconnect it.
1835 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1836 (!prev || prev->fWinding == 0 || e->fWinding == 0);
1837 create_event(e, isOuterBoundary, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001838 }
1839 insert_edge(e, prev, &activeEdges);
1840 prev = e;
1841 }
1842 }
1843 LOG("\ncollapsing overlap regions\n");
1844 if (events.count() == 0) {
1845 return false;
1846 }
1847 while (events.count() > 0) {
1848 Event* event = events.peek();
1849 events.pop();
1850 event->apply(mesh, c, alloc);
1851 }
1852 return true;
1853}
1854
1855bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1856 if (!prev || !next) {
1857 return true;
1858 }
1859 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1860 return winding != origEdge->fWinding;
1861}
Stephen White92eba8a2017-02-06 09:50:27 -05001862
senorblancof57372d2016-08-31 10:36:19 -07001863// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1864// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1865// new antialiased mesh from those vertices.
1866
Stephen Whitee260c462017-12-19 18:09:54 -05001867void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1868 Comparator& c, SkArenaAlloc& alloc) {
1869 LOG("\nstroking boundary\n");
1870 // A boundary with fewer than 3 edges is degenerate.
1871 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1872 return;
1873 }
1874 Edge* prevEdge = boundary->fTail;
1875 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1876 SkVector prevNormal;
1877 get_edge_normal(prevEdge, &prevNormal);
1878 double radius = 0.5;
1879 Line prevInner(prevEdge->fLine);
1880 prevInner.fC -= radius;
1881 Line prevOuter(prevEdge->fLine);
1882 prevOuter.fC += radius;
1883 VertexList innerVertices;
1884 VertexList outerVertices;
1885 bool innerInversion = true;
1886 bool outerInversion = true;
1887 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1888 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1889 SkVector normal;
1890 get_edge_normal(e, &normal);
1891 Line inner(e->fLine);
1892 inner.fC -= radius;
1893 Line outer(e->fLine);
1894 outer.fC += radius;
1895 SkPoint innerPoint, outerPoint;
1896 LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1897 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1898 prevOuter.intersect(outer, &outerPoint)) {
1899 float cosAngle = normal.dot(prevNormal);
1900 if (cosAngle < -kCosMiterAngle) {
1901 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1902
1903 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
1904 Line bisector(innerPoint, outerPoint);
1905 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
1906 if (tangent.fA == 0 && tangent.fB == 0) {
1907 continue;
1908 }
1909 tangent.normalize();
1910 Line innerTangent(tangent);
1911 Line outerTangent(tangent);
1912 innerTangent.fC -= 0.5;
1913 outerTangent.fC += 0.5;
1914 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
1915 if (prevNormal.cross(normal) > 0) {
1916 // Miter inner points
1917 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
1918 !innerTangent.intersect(inner, &innerPoint2) ||
1919 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001920 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001921 }
1922 Line prevTangent(prevV->fPoint,
1923 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
1924 Line nextTangent(nextV->fPoint,
1925 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001926 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001927 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001928 }
1929 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001930 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001931 }
1932 outerPoint1 = outerPoint2 = outerPoint;
1933 } else {
1934 // Miter outer points
1935 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
1936 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001937 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001938 }
1939 Line prevTangent(prevV->fPoint,
1940 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
1941 Line nextTangent(nextV->fPoint,
1942 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001943 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001944 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001945 }
1946 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001947 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001948 }
1949 innerPoint1 = innerPoint2 = innerPoint;
1950 }
Stephen Whiteea495232018-04-03 11:28:15 -04001951 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
1952 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
1953 continue;
1954 }
Stephen Whitee260c462017-12-19 18:09:54 -05001955 LOG("inner (%g, %g), (%g, %g), ",
1956 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
1957 LOG("outer (%g, %g), (%g, %g)\n",
1958 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
1959 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
1960 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
1961 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
1962 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
1963 innerVertex1->fPartner = outerVertex1;
1964 innerVertex2->fPartner = outerVertex2;
1965 outerVertex1->fPartner = innerVertex1;
1966 outerVertex2->fPartner = innerVertex2;
1967 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
1968 innerInversion = false;
1969 }
1970 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
1971 outerInversion = false;
1972 }
1973 innerVertices.append(innerVertex1);
1974 innerVertices.append(innerVertex2);
1975 outerVertices.append(outerVertex1);
1976 outerVertices.append(outerVertex2);
1977 } else {
1978 LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
1979 LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
1980 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
1981 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
1982 innerVertex->fPartner = outerVertex;
1983 outerVertex->fPartner = innerVertex;
1984 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
1985 innerInversion = false;
1986 }
1987 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
1988 outerInversion = false;
1989 }
1990 innerVertices.append(innerVertex);
1991 outerVertices.append(outerVertex);
1992 }
1993 }
1994 prevInner = inner;
1995 prevOuter = outer;
1996 prevV = v;
1997 prevEdge = e;
1998 prevNormal = normal;
1999 }
2000 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2001 innerInversion = false;
2002 }
2003 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2004 outerInversion = false;
2005 }
2006 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2007 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2008 // interior inverts).
2009 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2010 // so it will be detected during collapse_overlap_regions().
2011 int innerWinding = innerInversion ? 2 : -2;
2012 int outerWinding = outerInversion ? -1 : 1;
2013 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2014 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2015 }
2016 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2017 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2018 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2019 }
2020 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2021 innerMesh->append(innerVertices);
2022 outerMesh->append(outerVertices);
2023}
senorblancof57372d2016-08-31 10:36:19 -07002024
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002025void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05002026 LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002027 bool down = apply_fill_type(fillType, e->fWinding);
senorblancof57372d2016-08-31 10:36:19 -07002028 while (e) {
2029 e->fWinding = down ? 1 : -1;
2030 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002031 e->fLine.normalize();
2032 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002033 boundary->append(e);
2034 if (down) {
2035 // Find outgoing edge, in clockwise order.
2036 if ((next = e->fNextEdgeAbove)) {
2037 down = false;
2038 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2039 down = true;
2040 } else if ((next = e->fPrevEdgeAbove)) {
2041 down = false;
2042 }
2043 } else {
2044 // Find outgoing edge, in counter-clockwise order.
2045 if ((next = e->fPrevEdgeBelow)) {
2046 down = true;
2047 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2048 down = false;
2049 } else if ((next = e->fNextEdgeBelow)) {
2050 down = true;
2051 }
2052 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002053 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002054 e = next;
2055 }
2056}
2057
Stephen White5ad721e2017-02-23 16:50:47 -05002058// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002059
Stephen Whitebda29c02017-03-13 15:10:13 -04002060void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
2061 VertexList* outerVertices, SkPath::FillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002062 Comparator& c, SkArenaAlloc& alloc) {
2063 remove_non_boundary_edges(inMesh, fillType, alloc);
2064 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002065 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002066 EdgeList boundary;
2067 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2068 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002069 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002070 }
2071 }
senorblancof57372d2016-08-31 10:36:19 -07002072}
2073
Stephen Whitebda29c02017-03-13 15:10:13 -04002074// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002075
Stephen White3a9aab92017-03-07 14:07:18 -05002076void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002077 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002078#if LOGGING_ENABLED
2079 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002080 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002081 SkASSERT(v);
2082 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002083 for (v = v->fNext; v; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002084 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
2085 }
2086 }
2087#endif
senorblancof57372d2016-08-31 10:36:19 -07002088 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002089 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002090}
2091
Stephen Whitebda29c02017-03-13 15:10:13 -04002092void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002093 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002094 return;
ethannicholase9709e82016-01-07 13:34:16 -08002095 }
2096
2097 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002098 if (c.fDirection == Comparator::Direction::kHorizontal) {
2099 merge_sort<sweep_lt_horiz>(vertices);
2100 } else {
2101 merge_sort<sweep_lt_vert>(vertices);
2102 }
ethannicholase9709e82016-01-07 13:34:16 -08002103#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002104 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002105 static float gID = 0.0f;
2106 v->fID = gID++;
2107 }
2108#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002109}
2110
Stephen White3a9aab92017-03-07 14:07:18 -05002111Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType,
Stephen Whitebda29c02017-03-13 15:10:13 -04002112 const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002113 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002114 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2115 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002116 VertexList mesh;
2117 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002118 sort_mesh(&mesh, c, alloc);
2119 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002120 simplify(&mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002121 if (antialias) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002122 VertexList innerMesh;
2123 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2124 sort_mesh(&innerMesh, c, alloc);
2125 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002126 merge_coincident_vertices(&innerMesh, c, alloc);
2127 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2128 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2129 was_complex = simplify(outerMesh, c, alloc) || was_complex;
2130 LOG("\ninner mesh before:\n");
2131 dump_mesh(innerMesh);
2132 LOG("\nouter mesh before:\n");
2133 dump_mesh(*outerMesh);
2134 was_complex = collapse_overlap_regions(&innerMesh, c, alloc) || was_complex;
2135 was_complex = collapse_overlap_regions(outerMesh, c, alloc) || was_complex;
2136 if (was_complex) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002137 LOG("found complex mesh; taking slow path\n");
2138 VertexList aaMesh;
Stephen White95152e12017-12-18 10:52:44 -05002139 LOG("\ninner mesh after:\n");
2140 dump_mesh(innerMesh);
2141 LOG("\nouter mesh after:\n");
2142 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002143 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002144 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002145 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2146 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002147 simplify(&aaMesh, c, alloc);
Stephen White95152e12017-12-18 10:52:44 -05002148 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002149 outerMesh->fHead = outerMesh->fTail = nullptr;
2150 return tessellate(aaMesh, alloc);
2151 } else {
2152 LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002153 return tessellate(innerMesh, alloc);
2154 }
Stephen White49789062017-02-21 10:35:49 -05002155 } else {
2156 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002157 }
senorblancof57372d2016-08-31 10:36:19 -07002158}
2159
2160// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Brian Osman0995fd52019-01-09 09:52:25 -05002161void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, bool emitCoverage, void* data) {
senorblancof57372d2016-08-31 10:36:19 -07002162 for (Poly* poly = polys; poly; poly = poly->fNext) {
2163 if (apply_fill_type(fillType, poly)) {
Brian Osman0995fd52019-01-09 09:52:25 -05002164 data = poly->emit(emitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002165 }
2166 }
2167 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002168}
2169
halcanary9d524f22016-03-29 09:03:52 -07002170Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen Whitebda29c02017-03-13 15:10:13 -04002171 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
2172 VertexList* outerMesh) {
ethannicholase9709e82016-01-07 13:34:16 -08002173 SkPath::FillType fillType = path.getFillType();
2174 if (SkPath::IsInverseFillType(fillType)) {
2175 contourCnt++;
2176 }
Stephen White3a9aab92017-03-07 14:07:18 -05002177 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002178
2179 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07002180 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Stephen Whitebda29c02017-03-13 15:10:13 -04002181 antialias, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002182}
2183
Stephen White11f65e02017-02-16 19:00:39 -05002184int get_contour_count(const SkPath& path, SkScalar tolerance) {
2185 int contourCnt;
2186 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002187 if (maxPts <= 0) {
Stephen White11f65e02017-02-16 19:00:39 -05002188 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002189 }
Stephen White11f65e02017-02-16 19:00:39 -05002190 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002191}
2192
Greg Danield5b45932018-06-07 13:15:10 -04002193int64_t count_points(Poly* polys, SkPath::FillType fillType) {
2194 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002195 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002196 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002197 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2198 }
2199 }
2200 return count;
2201}
2202
Greg Danield5b45932018-06-07 13:15:10 -04002203int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2204 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002205 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2206 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2207 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2208 }
2209 }
2210 return count;
2211}
2212
Brian Osman0995fd52019-01-09 09:52:25 -05002213void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002214 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2215 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2216 Vertex* v0 = e->fTop;
2217 Vertex* v1 = e->fBottom;
2218 Vertex* v2 = e->fBottom->fPartner;
2219 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002220 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2221 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002222 }
2223 }
2224 return data;
2225}
2226
ethannicholase9709e82016-01-07 13:34:16 -08002227} // namespace
2228
2229namespace GrTessellator {
2230
2231// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2232
halcanary9d524f22016-03-29 09:03:52 -07002233int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Brian Osman0995fd52019-01-09 09:52:25 -05002234 VertexAllocator* vertexAllocator, bool antialias, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002235 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002236 if (contourCnt <= 0) {
2237 *isLinear = true;
2238 return 0;
2239 }
Stephen White11f65e02017-02-16 19:00:39 -05002240 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002241 VertexList outerMesh;
senorblancof57372d2016-08-31 10:36:19 -07002242 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
Stephen Whitebda29c02017-03-13 15:10:13 -04002243 isLinear, &outerMesh);
senorblanco7ab96e92016-10-12 06:47:44 -07002244 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002245 int64_t count64 = count_points(polys, fillType);
Stephen Whitebda29c02017-03-13 15:10:13 -04002246 if (antialias) {
Greg Danield5b45932018-06-07 13:15:10 -04002247 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002248 }
Greg Danield5b45932018-06-07 13:15:10 -04002249 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002250 return 0;
2251 }
Greg Danield5b45932018-06-07 13:15:10 -04002252 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002253
senorblancof57372d2016-08-31 10:36:19 -07002254 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08002255 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002256 SkDebugf("Could not allocate vertices\n");
2257 return 0;
2258 }
senorblancof57372d2016-08-31 10:36:19 -07002259
2260 LOG("emitting %d verts\n", count);
Brian Osman80879d42019-01-07 16:15:27 -05002261 void* end = polys_to_triangles(polys, fillType, antialias, verts);
2262 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002263
senorblancof57372d2016-08-31 10:36:19 -07002264 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
2265 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08002266 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002267 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002268 return actualCount;
2269}
2270
halcanary9d524f22016-03-29 09:03:52 -07002271int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002272 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002273 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002274 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002275 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002276 return 0;
2277 }
Stephen White11f65e02017-02-16 19:00:39 -05002278 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002279 bool isLinear;
Stephen Whitebda29c02017-03-13 15:10:13 -04002280 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
2281 nullptr);
ethannicholase9709e82016-01-07 13:34:16 -08002282 SkPath::FillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002283 int64_t count64 = count_points(polys, fillType);
2284 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002285 *verts = nullptr;
2286 return 0;
2287 }
Greg Danield5b45932018-06-07 13:15:10 -04002288 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002289
2290 *verts = new GrTessellator::WindingVertex[count];
2291 GrTessellator::WindingVertex* vertsEnd = *verts;
2292 SkPoint* points = new SkPoint[count];
2293 SkPoint* pointsEnd = points;
2294 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002295 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002296 SkPoint* start = pointsEnd;
Brian Osman80879d42019-01-07 16:15:27 -05002297 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002298 while (start != pointsEnd) {
2299 vertsEnd->fPos = *start;
2300 vertsEnd->fWinding = poly->fWinding;
2301 ++start;
2302 ++vertsEnd;
2303 }
2304 }
2305 }
2306 int actualCount = static_cast<int>(vertsEnd - *verts);
2307 SkASSERT(actualCount <= count);
2308 SkASSERT(pointsEnd - points == actualCount);
2309 delete[] points;
2310 return actualCount;
2311}
2312
2313} // namespace