blob: 72a3bc8bf1440423b1997c16f4d17eaaf398f1b6 [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"
ethannicholase9709e82016-01-07 13:34:16 -080012
Herb Derby5cdc9dd2017-02-13 12:10:46 -050013#include "SkArenaAlloc.h"
senorblanco6599eff2016-03-10 08:38:45 -080014#include "SkGeometry.h"
15#include "SkPath.h"
Cary Clarkdf429f32017-11-08 11:44:31 -050016#include "SkPointPriv.h"
Stephen Whitee260c462017-12-19 18:09:54 -050017#include "SkTDPQueue.h"
ethannicholase9709e82016-01-07 13:34:16 -080018
Stephen White94b7e542018-01-04 14:01:10 -050019#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040020#include <cstdio>
21#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080022
23/*
senorblancof57372d2016-08-31 10:36:19 -070024 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080025 *
26 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
27 * 2) Build a mesh of edges connecting the vertices (build_edges()).
28 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
29 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
30 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
31 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
32 *
senorblancof57372d2016-08-31 10:36:19 -070033 * For screenspace antialiasing, the algorithm is modified as follows:
34 *
35 * Run steps 1-5 above to produce polygons.
36 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
Stephen Whitebda29c02017-03-13 15:10:13 -040037 * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070038 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
39 * 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 -040040 * antialiased mesh from those vertices (stroke_boundary()).
senorblancof57372d2016-08-31 10:36:19 -070041 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
42 *
ethannicholase9709e82016-01-07 13:34:16 -080043 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
44 * of vertices (and the necessity of inserting new vertices on intersection).
45 *
Stephen Whitebda29c02017-03-13 15:10:13 -040046 * Stages (4) and (5) use an active edge list -- a list of all edges for which the
ethannicholase9709e82016-01-07 13:34:16 -080047 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
48 * left-to-right based on the point where both edges are active (when both top vertices
49 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
50 * (shared), it's sorted based on the last point where both edges are active, so the
51 * "upper" bottom vertex.
52 *
53 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
54 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
55 * not exact and may violate the mesh topology or active edge list ordering. We
56 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
Stephen White3b5a3fa2017-06-06 14:51:19 -040057 * points. This occurs in two ways:
ethannicholase9709e82016-01-07 13:34:16 -080058 *
59 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
60 * neighbouring edges at the top or bottom vertex. This is handled by merging the
61 * edges (merge_collinear_edges()).
62 * B) Intersections may cause an edge to violate the left-to-right ordering of the
Stephen White019f6c02017-06-09 10:06:26 -040063 * active edge list. This is handled by detecting potential violations and rewinding
Stephen White3b5a3fa2017-06-06 14:51:19 -040064 * the active edge list to the vertex before they occur (rewind() during merging,
65 * rewind_if_necessary() during splitting).
ethannicholase9709e82016-01-07 13:34:16 -080066 *
67 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
68 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
69 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
70 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
71 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
72 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
73 * linked list implementation. With the latter, all removals are O(1), and most insertions
74 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
75 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
76 * frequent. There may be other data structures worth investigating, however.
77 *
78 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
79 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
80 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
81 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
82 * that the "left" and "right" orientation in the code remains correct (edges to the left are
83 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
84 * degrees counterclockwise, rather that transposing.
85 */
86
87#define LOGGING_ENABLED 0
88
89#if LOGGING_ENABLED
90#define LOG printf
91#else
92#define LOG(...)
93#endif
94
ethannicholase9709e82016-01-07 13:34:16 -080095namespace {
96
Stephen White11f65e02017-02-16 19:00:39 -050097const int kArenaChunkSize = 16 * 1024;
Stephen Whitee260c462017-12-19 18:09:54 -050098const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
Stephen White11f65e02017-02-16 19:00:39 -050099
ethannicholase9709e82016-01-07 13:34:16 -0800100struct Vertex;
101struct Edge;
Stephen Whitee260c462017-12-19 18:09:54 -0500102struct Event;
ethannicholase9709e82016-01-07 13:34:16 -0800103struct Poly;
104
105template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800106void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800107 t->*Prev = prev;
108 t->*Next = next;
109 if (prev) {
110 prev->*Next = t;
111 } else if (head) {
112 *head = t;
113 }
114 if (next) {
115 next->*Prev = t;
116 } else if (tail) {
117 *tail = t;
118 }
119}
120
121template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800122void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800123 if (t->*Prev) {
124 t->*Prev->*Next = t->*Next;
125 } else if (head) {
126 *head = t->*Next;
127 }
128 if (t->*Next) {
129 t->*Next->*Prev = t->*Prev;
130 } else if (tail) {
131 *tail = t->*Prev;
132 }
133 t->*Prev = t->*Next = nullptr;
134}
135
136/**
137 * Vertices are used in three ways: first, the path contours are converted into a
138 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
139 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
140 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
141 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
142 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
143 * an individual Vertex from the path mesh may belong to multiple
144 * MonotonePolys, so the original Vertices cannot be re-used.
145 */
146
147struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700148 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800149 : fPoint(point), fPrev(nullptr), fNext(nullptr)
150 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
151 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
Stephen White3b5a3fa2017-06-06 14:51:19 -0400152 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
Stephen Whitebda29c02017-03-13 15:10:13 -0400153 , fPartner(nullptr)
senorblancof57372d2016-08-31 10:36:19 -0700154 , fAlpha(alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800155#if LOGGING_ENABLED
156 , fID (-1.0f)
157#endif
158 {}
Stephen White3b5a3fa2017-06-06 14:51:19 -0400159 SkPoint fPoint; // Vertex position
160 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
161 Vertex* fNext; // "
162 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
163 Edge* fLastEdgeAbove; // "
164 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
165 Edge* fLastEdgeBelow; // "
166 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
167 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
168 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
senorblancof57372d2016-08-31 10:36:19 -0700169 uint8_t fAlpha;
ethannicholase9709e82016-01-07 13:34:16 -0800170#if LOGGING_ENABLED
Stephen White3b5a3fa2017-06-06 14:51:19 -0400171 float fID; // Identifier used for logging.
ethannicholase9709e82016-01-07 13:34:16 -0800172#endif
173};
174
175/***************************************************************************************/
176
senorblancof57372d2016-08-31 10:36:19 -0700177struct AAParams {
178 bool fTweakAlpha;
179 GrColor fColor;
180};
181
ethannicholase9709e82016-01-07 13:34:16 -0800182typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
183
ethannicholase9709e82016-01-07 13:34:16 -0800184bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500185 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -0800186}
187
188bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -0500189 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -0800190}
191
Stephen White16a40cb2017-02-23 11:10:01 -0500192struct Comparator {
193 enum class Direction { kVertical, kHorizontal };
194 Comparator(Direction direction) : fDirection(direction) {}
195 bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
196 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
197 }
Stephen White16a40cb2017-02-23 11:10:01 -0500198 Direction fDirection;
199};
200
senorblancof57372d2016-08-31 10:36:19 -0700201inline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) {
202 if (!aaParams) {
203 SkPoint* d = static_cast<SkPoint*>(data);
204 *d++ = v->fPoint;
205 return d;
206 }
207 if (aaParams->fTweakAlpha) {
208 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data);
209 d->fPosition = v->fPoint;
lsalzman8c8fcef2016-10-11 12:20:17 -0700210 d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha));
senorblancof57372d2016-08-31 10:36:19 -0700211 d++;
212 return d;
213 }
214 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data);
215 d->fPosition = v->fPoint;
216 d->fColor = aaParams->fColor;
217 d->fCoverage = GrNormalizeByteToFloat(v->fAlpha);
218 d++;
219 return d;
ethannicholase9709e82016-01-07 13:34:16 -0800220}
221
senorblancof57372d2016-08-31 10:36:19 -0700222void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) {
Stephen White95152e12017-12-18 10:52:44 -0500223 LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
224 LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
225 LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700226#if TESSELLATOR_WIREFRAME
227 data = emit_vertex(v0, aaParams, data);
228 data = emit_vertex(v1, aaParams, data);
229 data = emit_vertex(v1, aaParams, data);
230 data = emit_vertex(v2, aaParams, data);
231 data = emit_vertex(v2, aaParams, data);
232 data = emit_vertex(v0, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800233#else
senorblancof57372d2016-08-31 10:36:19 -0700234 data = emit_vertex(v0, aaParams, data);
235 data = emit_vertex(v1, aaParams, data);
236 data = emit_vertex(v2, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800237#endif
238 return data;
239}
240
senorblancoe6eaa322016-03-08 09:06:44 -0800241struct VertexList {
242 VertexList() : fHead(nullptr), fTail(nullptr) {}
Stephen White16a40cb2017-02-23 11:10:01 -0500243 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
senorblancoe6eaa322016-03-08 09:06:44 -0800244 Vertex* fHead;
245 Vertex* fTail;
246 void insert(Vertex* v, Vertex* prev, Vertex* next) {
247 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
248 }
249 void append(Vertex* v) {
250 insert(v, fTail, nullptr);
251 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400252 void append(const VertexList& list) {
253 if (!list.fHead) {
254 return;
255 }
256 if (fTail) {
257 fTail->fNext = list.fHead;
258 list.fHead->fPrev = fTail;
259 } else {
260 fHead = list.fHead;
261 }
262 fTail = list.fTail;
263 }
senorblancoe6eaa322016-03-08 09:06:44 -0800264 void prepend(Vertex* v) {
265 insert(v, nullptr, fHead);
266 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500267 void remove(Vertex* v) {
268 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
269 }
senorblancof57372d2016-08-31 10:36:19 -0700270 void close() {
271 if (fHead && fTail) {
272 fTail->fNext = fHead;
273 fHead->fPrev = fTail;
274 }
275 }
senorblancoe6eaa322016-03-08 09:06:44 -0800276};
277
senorblancof57372d2016-08-31 10:36:19 -0700278// Round to nearest quarter-pixel. This is used for screenspace tessellation.
279
280inline void round(SkPoint* p) {
281 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
282 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
283}
284
Stephen White94b7e542018-01-04 14:01:10 -0500285inline SkScalar double_to_clamped_scalar(double d) {
286 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
287}
288
senorblanco49df8d12016-10-07 08:36:56 -0700289// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
290struct Line {
Stephen Whitee260c462017-12-19 18:09:54 -0500291 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
senorblanco49df8d12016-10-07 08:36:56 -0700292 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
293 Line(const SkPoint& p, const SkPoint& q)
294 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
295 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
296 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
297 static_cast<double>(p.fX) * q.fY) {}
298 double dist(const SkPoint& p) const {
299 return fA * p.fX + fB * p.fY + fC;
300 }
Stephen Whitee260c462017-12-19 18:09:54 -0500301 Line operator*(double v) const {
302 return Line(fA * v, fB * v, fC * v);
303 }
senorblanco49df8d12016-10-07 08:36:56 -0700304 double magSq() const {
305 return fA * fA + fB * fB;
306 }
Stephen Whitee260c462017-12-19 18:09:54 -0500307 void normalize() {
308 double len = sqrt(this->magSq());
309 if (len == 0.0) {
310 return;
311 }
312 double scale = 1.0f / len;
313 fA *= scale;
314 fB *= scale;
315 fC *= scale;
316 }
317 bool nearParallel(const Line& o) const {
318 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
319 }
senorblanco49df8d12016-10-07 08:36:56 -0700320
321 // Compute the intersection of two (infinite) Lines.
Stephen White95152e12017-12-18 10:52:44 -0500322 bool intersect(const Line& other, SkPoint* point) const {
senorblanco49df8d12016-10-07 08:36:56 -0700323 double denom = fA * other.fB - fB * other.fA;
324 if (denom == 0.0) {
325 return false;
326 }
Stephen White94b7e542018-01-04 14:01:10 -0500327 double scale = 1.0 / denom;
328 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
329 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
Stephen Whiteb56dedf2017-03-02 10:35:56 -0500330 round(point);
senorblanco49df8d12016-10-07 08:36:56 -0700331 return true;
332 }
333 double fA, fB, fC;
334};
335
ethannicholase9709e82016-01-07 13:34:16 -0800336/**
337 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
338 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
339 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
Stephen Whitebda29c02017-03-13 15:10:13 -0400340 * point). For speed, that case is only tested by the callers that require it (e.g.,
Stephen White3b5a3fa2017-06-06 14:51:19 -0400341 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
ethannicholase9709e82016-01-07 13:34:16 -0800342 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
343 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
344 * a lot faster in the "not found" case.
345 *
346 * The coefficients of the line equation stored in double precision to avoid catastrphic
347 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
348 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
349 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
350 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
351 * this file).
352 */
353
354struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500355 enum class Type { kInner, kOuter, kConnector };
356 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800357 : fWinding(winding)
358 , fTop(top)
359 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500360 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800361 , fLeft(nullptr)
362 , fRight(nullptr)
363 , fPrevEdgeAbove(nullptr)
364 , fNextEdgeAbove(nullptr)
365 , fPrevEdgeBelow(nullptr)
366 , fNextEdgeBelow(nullptr)
367 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700368 , fRightPoly(nullptr)
Stephen Whitee260c462017-12-19 18:09:54 -0500369 , fEvent(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700370 , fLeftPolyPrev(nullptr)
371 , fLeftPolyNext(nullptr)
372 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700373 , fRightPolyNext(nullptr)
Stephen Whitee260c462017-12-19 18:09:54 -0500374 , fOverlap(false)
senorblanco70f52512016-08-17 14:56:22 -0700375 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700376 , fUsedInRightPoly(false)
377 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800378 }
379 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
380 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
381 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500382 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800383 Edge* fLeft; // The linked list of edges in the active edge list.
384 Edge* fRight; // "
385 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
386 Edge* fNextEdgeAbove; // "
387 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
388 Edge* fNextEdgeBelow; // "
389 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
390 Poly* fRightPoly; // The Poly to the right of this edge, if any.
Stephen Whitee260c462017-12-19 18:09:54 -0500391 Event* fEvent;
senorblanco531237e2016-06-02 11:36:48 -0700392 Edge* fLeftPolyPrev;
393 Edge* fLeftPolyNext;
394 Edge* fRightPolyPrev;
395 Edge* fRightPolyNext;
Stephen Whitee260c462017-12-19 18:09:54 -0500396 bool fOverlap; // True if there's an overlap region adjacent to this edge.
senorblanco70f52512016-08-17 14:56:22 -0700397 bool fUsedInLeftPoly;
398 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700399 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800400 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700401 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800402 }
403 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700404 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800405 }
406 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700407 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800408 }
409 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700410 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800411 }
Stephen White95152e12017-12-18 10:52:44 -0500412 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
ethannicholase9709e82016-01-07 13:34:16 -0800413 LOG("intersecting %g -> %g with %g -> %g\n",
414 fTop->fID, fBottom->fID,
415 other.fTop->fID, other.fBottom->fID);
416 if (fTop == other.fTop || fBottom == other.fBottom) {
417 return false;
418 }
senorblanco49df8d12016-10-07 08:36:56 -0700419 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800420 if (denom == 0.0) {
421 return false;
422 }
Stephen White8a0bfc52017-02-21 15:24:13 -0500423 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
424 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
425 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
426 double tNumer = dy * fLine.fB + dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800427 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
428 // This saves us doing the divide below unless absolutely necessary.
429 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
430 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
431 return false;
432 }
433 double s = sNumer / denom;
434 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700435 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
436 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500437 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500438 if (fType == Type::kConnector) {
439 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
440 } else if (other.fType == Type::kConnector) {
441 double t = tNumer / denom;
442 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500443 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
444 *alpha = 0;
445 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500446 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500447 }
448 }
ethannicholase9709e82016-01-07 13:34:16 -0800449 return true;
450 }
senorblancof57372d2016-08-31 10:36:19 -0700451};
452
453struct EdgeList {
Stephen White5ad721e2017-02-23 16:50:47 -0500454 EdgeList() : fHead(nullptr), fTail(nullptr) {}
senorblancof57372d2016-08-31 10:36:19 -0700455 Edge* fHead;
456 Edge* fTail;
senorblancof57372d2016-08-31 10:36:19 -0700457 void insert(Edge* edge, Edge* prev, Edge* next) {
458 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700459 }
460 void append(Edge* e) {
461 insert(e, fTail, nullptr);
462 }
463 void remove(Edge* edge) {
464 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
senorblancof57372d2016-08-31 10:36:19 -0700465 }
Stephen Whitebda29c02017-03-13 15:10:13 -0400466 void removeAll() {
467 while (fHead) {
468 this->remove(fHead);
469 }
470 }
senorblancof57372d2016-08-31 10:36:19 -0700471 void close() {
472 if (fHead && fTail) {
473 fTail->fRight = fHead;
474 fHead->fLeft = fTail;
475 }
476 }
477 bool contains(Edge* edge) const {
478 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800479 }
480};
481
Stephen Whitee260c462017-12-19 18:09:54 -0500482struct Event {
Stephen White77169c82018-06-05 09:15:59 -0400483 Event(Edge* edge, bool isOuterBoundary, const SkPoint& point, uint8_t alpha)
484 : fEdge(edge), fIsOuterBoundary(isOuterBoundary), fPoint(point), fAlpha(alpha)
485 , fPrev(nullptr), fNext(nullptr) {
Stephen Whitee260c462017-12-19 18:09:54 -0500486 }
487 Edge* fEdge;
Stephen White77169c82018-06-05 09:15:59 -0400488 bool fIsOuterBoundary;
Stephen Whitee260c462017-12-19 18:09:54 -0500489 SkPoint fPoint;
490 uint8_t fAlpha;
491 Event* fPrev;
492 Event* fNext;
493 void apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc);
494};
495
496bool compare(Event* const& e1, Event* const& e2) {
497 return e1->fAlpha > e2->fAlpha;
498}
499
500struct EventList : public SkTDPQueue<Event*, &compare> {};
501
Stephen White77169c82018-06-05 09:15:59 -0400502void create_event(Edge* e, bool isOuterBoundary, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitee260c462017-12-19 18:09:54 -0500503 Edge bisector1(e->fTop, e->fTop->fPartner, 1, Edge::Type::kConnector);
504 Edge bisector2(e->fBottom, e->fBottom->fPartner, 1, Edge::Type::kConnector);
505 SkPoint p;
506 uint8_t alpha;
507 if (bisector1.intersect(bisector2, &p, &alpha)) {
508 LOG("found overlap edge %g -> %g, will collapse to %g,%g alpha %d\n",
509 e->fTop->fID, e->fBottom->fID, p.fX, p.fY, alpha);
Stephen White77169c82018-06-05 09:15:59 -0400510 e->fEvent = alloc.make<Event>(e, isOuterBoundary, p, alpha);
Stephen Whitee260c462017-12-19 18:09:54 -0500511 events->insert(e->fEvent);
512 }
513}
Stephen Whitee260c462017-12-19 18:09:54 -0500514
ethannicholase9709e82016-01-07 13:34:16 -0800515/***************************************************************************************/
516
517struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700518 Poly(Vertex* v, int winding)
519 : fFirstVertex(v)
520 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800521 , fHead(nullptr)
522 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800523 , fNext(nullptr)
524 , fPartner(nullptr)
525 , fCount(0)
526 {
527#if LOGGING_ENABLED
528 static int gID = 0;
529 fID = gID++;
530 LOG("*** created Poly %d\n", fID);
531#endif
532 }
senorblanco531237e2016-06-02 11:36:48 -0700533 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800534 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700535 MonotonePoly(Edge* edge, Side side)
536 : fSide(side)
537 , fFirstEdge(nullptr)
538 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800539 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700540 , fNext(nullptr) {
541 this->addEdge(edge);
542 }
ethannicholase9709e82016-01-07 13:34:16 -0800543 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700544 Edge* fFirstEdge;
545 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800546 MonotonePoly* fPrev;
547 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700548 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800549 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700550 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700551 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
552 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700553 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800554 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700555 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700556 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
557 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700558 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800559 }
ethannicholase9709e82016-01-07 13:34:16 -0800560 }
561
senorblancof57372d2016-08-31 10:36:19 -0700562 void* emit(const AAParams* aaParams, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700563 Edge* e = fFirstEdge;
senorblanco531237e2016-06-02 11:36:48 -0700564 VertexList vertices;
565 vertices.append(e->fTop);
Stephen White651cbe92017-03-03 12:24:16 -0500566 int count = 1;
senorblanco531237e2016-06-02 11:36:48 -0700567 while (e != nullptr) {
senorblanco531237e2016-06-02 11:36:48 -0700568 if (kRight_Side == fSide) {
569 vertices.append(e->fBottom);
570 e = e->fRightPolyNext;
571 } else {
572 vertices.prepend(e->fBottom);
573 e = e->fLeftPolyNext;
574 }
Stephen White651cbe92017-03-03 12:24:16 -0500575 count++;
senorblanco531237e2016-06-02 11:36:48 -0700576 }
577 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800578 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700579 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800580 SkASSERT(v && v->fPrev && v->fNext);
581 Vertex* prev = v->fPrev;
582 Vertex* curr = v;
583 Vertex* next = v->fNext;
Stephen White651cbe92017-03-03 12:24:16 -0500584 if (count == 3) {
585 return emit_triangle(prev, curr, next, aaParams, data);
586 }
ethannicholase9709e82016-01-07 13:34:16 -0800587 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
588 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
589 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
590 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
591 if (ax * by - ay * bx >= 0.0) {
senorblancof57372d2016-08-31 10:36:19 -0700592 data = emit_triangle(prev, curr, next, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800593 v->fPrev->fNext = v->fNext;
594 v->fNext->fPrev = v->fPrev;
Stephen White651cbe92017-03-03 12:24:16 -0500595 count--;
ethannicholase9709e82016-01-07 13:34:16 -0800596 if (v->fPrev == first) {
597 v = v->fNext;
598 } else {
599 v = v->fPrev;
600 }
601 } else {
602 v = v->fNext;
603 }
604 }
605 return data;
606 }
607 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500608 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
senorblanco70f52512016-08-17 14:56:22 -0700609 LOG("addEdge (%g -> %g) to poly %d, %s side\n",
610 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800611 Poly* partner = fPartner;
612 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700613 if (side == kRight_Side) {
614 if (e->fUsedInRightPoly) {
615 return this;
616 }
617 } else {
618 if (e->fUsedInLeftPoly) {
619 return this;
620 }
621 }
ethannicholase9709e82016-01-07 13:34:16 -0800622 if (partner) {
623 fPartner = partner->fPartner = nullptr;
624 }
senorblanco531237e2016-06-02 11:36:48 -0700625 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500626 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700627 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700628 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
629 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700630 } else if (side == fTail->fSide) {
631 fTail->addEdge(e);
632 fCount++;
633 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500634 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700635 fTail->addEdge(e);
636 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800637 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700638 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800639 poly = partner;
640 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500641 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700642 m->fPrev = fTail;
643 fTail->fNext = m;
644 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800645 }
646 }
ethannicholase9709e82016-01-07 13:34:16 -0800647 return poly;
648 }
senorblancof57372d2016-08-31 10:36:19 -0700649 void* emit(const AAParams* aaParams, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800650 if (fCount < 3) {
651 return data;
652 }
653 LOG("emit() %d, size %d\n", fID, fCount);
654 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
senorblancof57372d2016-08-31 10:36:19 -0700655 data = m->emit(aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800656 }
657 return data;
658 }
senorblanco531237e2016-06-02 11:36:48 -0700659 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
660 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800661 int fWinding;
662 MonotonePoly* fHead;
663 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800664 Poly* fNext;
665 Poly* fPartner;
666 int fCount;
667#if LOGGING_ENABLED
668 int fID;
669#endif
670};
671
672/***************************************************************************************/
673
674bool coincident(const SkPoint& a, const SkPoint& b) {
675 return a == b;
676}
677
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500678Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
679 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800680 poly->fNext = *head;
681 *head = poly;
682 return poly;
683}
684
Stephen White3a9aab92017-03-07 14:07:18 -0500685void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500686 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800687#if LOGGING_ENABLED
688 static float gID = 0.0f;
689 v->fID = gID++;
690#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500691 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800692}
693
Stephen White36e4f062017-03-27 16:11:31 -0400694SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
695 SkQuadCoeff quad(pts);
696 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
697 SkPoint mid = to_point(quad.eval(t));
698 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400699 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
700 return 0;
701 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500702 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400703}
704
705void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour,
706 SkArenaAlloc& alloc) {
707 SkQuadCoeff quad(pts);
708 Sk2s aa = quad.fA * quad.fA;
709 SkScalar denom = 2.0f * (aa[0] + aa[1]);
710 Sk2s ab = quad.fA * quad.fB;
711 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
712 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500713 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400714 // Test possible subdivision values only at the point of maximum curvature.
715 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500716 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400717 u = 1.0f / nPoints;
718 if (quad_error_at(pts, t, u) < toleranceSqd) {
719 break;
720 }
721 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800722 }
Stephen White36e4f062017-03-27 16:11:31 -0400723 for (int j = 1; j <= nPoints; j++) {
724 append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc);
725 }
ethannicholase9709e82016-01-07 13:34:16 -0800726}
727
Stephen White3a9aab92017-03-07 14:07:18 -0500728void generate_cubic_points(const SkPoint& p0,
729 const SkPoint& p1,
730 const SkPoint& p2,
731 const SkPoint& p3,
732 SkScalar tolSqd,
733 VertexList* contour,
734 int pointsLeft,
735 SkArenaAlloc& alloc) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500736 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
737 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800738 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
739 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Stephen White3a9aab92017-03-07 14:07:18 -0500740 append_point_to_contour(p3, contour, alloc);
741 return;
ethannicholase9709e82016-01-07 13:34:16 -0800742 }
743 const SkPoint q[] = {
744 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
745 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
746 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
747 };
748 const SkPoint r[] = {
749 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
750 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
751 };
752 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
753 pointsLeft >>= 1;
Stephen White3a9aab92017-03-07 14:07:18 -0500754 generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc);
755 generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800756}
757
758// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
759
760void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen White3a9aab92017-03-07 14:07:18 -0500761 VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800762 SkScalar toleranceSqd = tolerance * tolerance;
763
764 SkPoint pts[4];
ethannicholase9709e82016-01-07 13:34:16 -0800765 *isLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500766 VertexList* contour = contours;
ethannicholase9709e82016-01-07 13:34:16 -0800767 SkPath::Iter iter(path, false);
ethannicholase9709e82016-01-07 13:34:16 -0800768 if (path.isInverseFillType()) {
769 SkPoint quad[4];
770 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700771 for (int i = 3; i >= 0; i--) {
Stephen White3a9aab92017-03-07 14:07:18 -0500772 append_point_to_contour(quad[i], contours, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800773 }
Stephen White3a9aab92017-03-07 14:07:18 -0500774 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800775 }
776 SkAutoConicToQuads converter;
Stephen White3a9aab92017-03-07 14:07:18 -0500777 SkPath::Verb verb;
Stephen White2cee2832017-08-23 09:37:16 -0400778 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800779 switch (verb) {
780 case SkPath::kConic_Verb: {
781 SkScalar weight = iter.conicWeight();
782 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
783 for (int i = 0; i < converter.countQuads(); ++i) {
Stephen White36e4f062017-03-27 16:11:31 -0400784 append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800785 quadPts += 2;
786 }
787 *isLinear = false;
788 break;
789 }
790 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500791 if (contour->fHead) {
792 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800793 }
Stephen White3a9aab92017-03-07 14:07:18 -0500794 append_point_to_contour(pts[0], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800795 break;
796 case SkPath::kLine_Verb: {
Stephen White3a9aab92017-03-07 14:07:18 -0500797 append_point_to_contour(pts[1], contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800798 break;
799 }
800 case SkPath::kQuad_Verb: {
Stephen White36e4f062017-03-27 16:11:31 -0400801 append_quadratic_to_contour(pts, toleranceSqd, contour, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800802 *isLinear = false;
803 break;
804 }
805 case SkPath::kCubic_Verb: {
806 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Stephen White3a9aab92017-03-07 14:07:18 -0500807 generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
808 pointsLeft, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800809 *isLinear = false;
810 break;
811 }
812 case SkPath::kClose_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800813 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800814 break;
815 }
816 }
817}
818
Stephen White49789062017-02-21 10:35:49 -0500819inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800820 switch (fillType) {
821 case SkPath::kWinding_FillType:
822 return winding != 0;
823 case SkPath::kEvenOdd_FillType:
824 return (winding & 1) != 0;
825 case SkPath::kInverseWinding_FillType:
senorblanco7ab96e92016-10-12 06:47:44 -0700826 return winding == 1;
ethannicholase9709e82016-01-07 13:34:16 -0800827 case SkPath::kInverseEvenOdd_FillType:
828 return (winding & 1) == 1;
829 default:
830 SkASSERT(false);
831 return false;
832 }
833}
834
Stephen White49789062017-02-21 10:35:49 -0500835inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
836 return poly && apply_fill_type(fillType, poly->fWinding);
837}
838
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500839Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500840 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800841 Vertex* top = winding < 0 ? next : prev;
842 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500843 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800844}
845
846void remove_edge(Edge* edge, EdgeList* edges) {
847 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700848 SkASSERT(edges->contains(edge));
849 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800850}
851
852void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
853 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700854 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800855 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700856 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800857}
858
859void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500860 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800861 *left = v->fFirstEdgeAbove->fLeft;
862 *right = v->fLastEdgeAbove->fRight;
863 return;
864 }
865 Edge* next = nullptr;
866 Edge* prev;
867 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
868 if (prev->isLeftOf(v)) {
869 break;
870 }
871 next = prev;
872 }
873 *left = prev;
874 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800875}
876
ethannicholase9709e82016-01-07 13:34:16 -0800877void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
878 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500879 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800880 return;
881 }
882 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
883 Edge* prev = nullptr;
884 Edge* next;
885 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
886 if (next->isRightOf(edge->fTop)) {
887 break;
888 }
889 prev = next;
890 }
senorblancoe6eaa322016-03-08 09:06:44 -0800891 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800892 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
893}
894
895void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
896 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500897 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800898 return;
899 }
900 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
901 Edge* prev = nullptr;
902 Edge* next;
903 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
904 if (next->isRightOf(edge->fBottom)) {
905 break;
906 }
907 prev = next;
908 }
senorblancoe6eaa322016-03-08 09:06:44 -0800909 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800910 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
911}
912
913void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400914 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800915 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
916 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800917 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800918 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
919}
920
921void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400922 SkASSERT(edge->fTop && edge->fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800923 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
924 edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800925 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800926 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
927}
928
Stephen Whitee7a364d2017-01-11 16:19:26 -0500929void disconnect(Edge* edge)
930{
ethannicholase9709e82016-01-07 13:34:16 -0800931 remove_edge_above(edge);
932 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500933}
934
Stephen White3b5a3fa2017-06-06 14:51:19 -0400935void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c);
936
937void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) {
938 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
939 return;
940 }
941 Vertex* v = *current;
942 LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
943 while (v != dst) {
944 v = v->fPrev;
945 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
946 remove_edge(e, activeEdges);
947 }
948 Edge* leftEdge = v->fLeftEnclosingEdge;
949 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
950 insert_edge(e, leftEdge, activeEdges);
951 leftEdge = e;
952 }
953 }
954 *current = v;
955}
956
957void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
958 if (!activeEdges || !current) {
959 return;
960 }
961 Vertex* top = edge->fTop;
962 Vertex* bottom = edge->fBottom;
963 if (edge->fLeft) {
964 Vertex* leftTop = edge->fLeft->fTop;
965 Vertex* leftBottom = edge->fLeft->fBottom;
966 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
967 rewind(activeEdges, current, leftTop, c);
968 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
969 rewind(activeEdges, current, top, c);
970 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
971 !edge->fLeft->isLeftOf(bottom)) {
972 rewind(activeEdges, current, leftTop, c);
973 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
974 rewind(activeEdges, current, top, c);
975 }
976 }
977 if (edge->fRight) {
978 Vertex* rightTop = edge->fRight->fTop;
979 Vertex* rightBottom = edge->fRight->fBottom;
980 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
981 rewind(activeEdges, current, rightTop, c);
982 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
983 rewind(activeEdges, current, top, c);
984 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
985 !edge->fRight->isRightOf(bottom)) {
986 rewind(activeEdges, current, rightTop, c);
987 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
988 !edge->isLeftOf(rightBottom)) {
989 rewind(activeEdges, current, top, c);
990 }
ethannicholase9709e82016-01-07 13:34:16 -0800991 }
992}
993
Stephen White3b5a3fa2017-06-06 14:51:19 -0400994void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800995 remove_edge_below(edge);
996 edge->fTop = v;
997 edge->recompute();
998 insert_edge_below(edge, v, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400999 rewind_if_necessary(edge, activeEdges, current, c);
1000 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001001}
1002
Stephen White3b5a3fa2017-06-06 14:51:19 -04001003void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001004 remove_edge_above(edge);
1005 edge->fBottom = v;
1006 edge->recompute();
1007 insert_edge_above(edge, v, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001008 rewind_if_necessary(edge, activeEdges, current, c);
1009 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001010}
1011
Stephen White3b5a3fa2017-06-06 14:51:19 -04001012void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1013 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001014 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
1015 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
1016 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1017 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001018 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001019 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001020 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001021 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001022 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001023 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001024 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001025 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001026 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001027 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001028 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001029 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001030 }
1031}
1032
Stephen White3b5a3fa2017-06-06 14:51:19 -04001033void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
1034 Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -08001035 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
1036 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
1037 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
1038 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001039 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001040 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001041 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -04001042 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001043 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001044 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001045 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001046 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001047 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001048 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -08001049 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001050 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001051 }
1052}
1053
Stephen Whited26b4d82018-07-26 10:02:27 -04001054bool top_collinear(Edge* left, Edge* right) {
1055 if (!left || !right) {
1056 return false;
1057 }
1058 return left->fTop->fPoint == right->fTop->fPoint ||
1059 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
1060}
1061
1062bool bottom_collinear(Edge* left, Edge* right) {
1063 if (!left || !right) {
1064 return false;
1065 }
1066 return left->fBottom->fPoint == right->fBottom->fPoint ||
1067 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
1068}
1069
Stephen White3b5a3fa2017-06-06 14:51:19 -04001070void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001071 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -04001072 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001073 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001074 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -04001075 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001076 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -04001077 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -04001078 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -04001079 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001080 } else {
1081 break;
1082 }
ethannicholase9709e82016-01-07 13:34:16 -08001083 }
Stephen Whited26b4d82018-07-26 10:02:27 -04001084 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
1085 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
1086 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
1087 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -08001088}
1089
Stephen White89042d52018-06-08 12:18:22 -04001090bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001091 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001092 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001093 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001094 }
ethannicholase9709e82016-01-07 13:34:16 -08001095 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1096 edge->fTop->fID, edge->fBottom->fID,
1097 v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001098 Vertex* top;
1099 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001100 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001101 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001102 top = v;
1103 bottom = edge->fTop;
1104 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001105 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001106 top = edge->fBottom;
1107 bottom = v;
1108 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001109 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001110 top = v;
1111 bottom = edge->fBottom;
1112 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001113 }
Stephen White531a48e2018-06-01 09:49:39 -04001114 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001115 insert_edge_below(newEdge, top, c);
1116 insert_edge_above(newEdge, bottom, c);
1117 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001118 return true;
1119}
1120
1121bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1122 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1123 return false;
1124 }
Stephen White1c5fd182018-07-12 15:54:05 -04001125 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
1126 return false;
1127 }
Stephen White89042d52018-06-08 12:18:22 -04001128 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1129 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001130 rewind(activeEdges, current, right->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001131 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1132 }
1133 } else {
1134 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001135 rewind(activeEdges, current, left->fTop, c);
Stephen White89042d52018-06-08 12:18:22 -04001136 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1137 }
1138 }
1139 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1140 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001141 rewind(activeEdges, current, right->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001142 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1143 }
1144 } else {
1145 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -04001146 rewind(activeEdges, current, left->fBottom, c);
Stephen White89042d52018-06-08 12:18:22 -04001147 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1148 }
1149 }
1150 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001151}
1152
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001153Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001154 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001155 if (!prev || !next || prev->fPoint == next->fPoint) {
1156 return nullptr;
1157 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001158 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001159 insert_edge_below(edge, edge->fTop, c);
1160 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001161 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001162 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001163 return edge;
1164}
1165
Stephen Whitebf6137e2017-01-04 15:43:26 -05001166void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001167 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001168 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1169 src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001170 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001171 if (src->fPartner) {
1172 src->fPartner->fPartner = dst;
1173 }
Stephen White7b376942018-05-22 11:51:32 -04001174 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001175 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001176 }
Stephen White7b376942018-05-22 11:51:32 -04001177 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001178 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001179 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001180 mesh->remove(src);
ethannicholase9709e82016-01-07 13:34:16 -08001181}
1182
Stephen White95152e12017-12-18 10:52:44 -05001183Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1184 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1185 Vertex* prevV = reference;
1186 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1187 prevV = prevV->fPrev;
1188 }
1189 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1190 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1191 prevV = nextV;
1192 nextV = nextV->fNext;
1193 }
1194 Vertex* v;
1195 if (prevV && coincident(prevV->fPoint, p)) {
1196 v = prevV;
1197 } else if (nextV && coincident(nextV->fPoint, p)) {
1198 v = nextV;
1199 } else {
1200 v = alloc.make<Vertex>(p, alpha);
1201#if LOGGING_ENABLED
1202 if (!prevV) {
1203 v->fID = mesh->fHead->fID - 1.0f;
1204 } else if (!nextV) {
1205 v->fID = mesh->fTail->fID + 1.0f;
1206 } else {
1207 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1208 }
1209#endif
1210 mesh->insert(v, prevV, nextV);
1211 }
1212 return v;
1213}
1214
Stephen White53a02982018-05-30 22:47:46 -04001215// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1216// sort criterion, it may not be possible to split correctly, since there is no point which is
1217// below the top and above the bottom. This function detects that case.
1218bool nearly_flat(Comparator& c, Edge* edge) {
1219 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1220 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001221 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001222}
1223
Stephen Whitee62999f2018-06-05 18:45:07 -04001224SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1225 if (c.sweep_lt(p, min)) {
1226 return min;
1227 } else if (c.sweep_lt(max, p)) {
1228 return max;
1229 } else {
1230 return p;
1231 }
1232}
1233
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001234bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001235 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001236 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001237 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001238 }
Stephen White56158ae2017-01-30 14:31:31 -05001239 SkPoint p;
1240 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001241 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001242 Vertex* v;
ethannicholase9709e82016-01-07 13:34:16 -08001243 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001244 Vertex* top = *current;
1245 // If the intersection point is above the current vertex, rewind to the vertex above the
1246 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001247 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001248 top = top->fPrev;
1249 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001250 if (!nearly_flat(c, left)) {
1251 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001252 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001253 if (!nearly_flat(c, right)) {
1254 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001255 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001256 if (p == left->fTop->fPoint) {
1257 v = left->fTop;
1258 } else if (p == left->fBottom->fPoint) {
1259 v = left->fBottom;
1260 } else if (p == right->fTop->fPoint) {
1261 v = right->fTop;
1262 } else if (p == right->fBottom->fPoint) {
1263 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001264 } else {
Stephen White95152e12017-12-18 10:52:44 -05001265 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001266 if (left->fTop->fPartner) {
1267 Line line1 = left->fLine;
1268 Line line2 = right->fLine;
1269 int dir = left->fType == Edge::Type::kOuter ? -1 : 1;
1270 line1.fC += sqrt(left->fLine.magSq()) * (left->fWinding > 0 ? 1 : -1) * dir;
1271 line2.fC += sqrt(right->fLine.magSq()) * (right->fWinding > 0 ? 1 : -1) * dir;
Stephen Whitee260c462017-12-19 18:09:54 -05001272 SkPoint p;
1273 if (line1.intersect(line2, &p)) {
1274 LOG("synthesizing partner (%g,%g) for intersection vertex %g\n",
1275 p.fX, p.fY, v->fID);
1276 v->fPartner = alloc.make<Vertex>(p, 255 - v->fAlpha);
1277 }
1278 }
ethannicholase9709e82016-01-07 13:34:16 -08001279 }
Stephen White0cb31672017-06-08 14:41:01 -04001280 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001281 split_edge(left, v, activeEdges, current, c, alloc);
1282 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001283 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001284 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001285 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001286 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001287}
1288
Stephen White3a9aab92017-03-07 14:07:18 -05001289void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
1290 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1291 SkASSERT(contour->fHead);
1292 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001293 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001294 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001295 }
Stephen White3a9aab92017-03-07 14:07:18 -05001296 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001297 if (approximate) {
1298 round(&v->fPoint);
1299 }
Stephen White3a9aab92017-03-07 14:07:18 -05001300 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001301 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001302 if (coincident(prev->fPoint, v->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -08001303 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001304 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001305 } else if (!v->fPoint.isFinite()) {
1306 LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
1307 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001308 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Stephen White06768ca2018-05-25 14:50:56 -04001309 LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
1310 contour->remove(v);
1311 } else {
1312 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001313 }
Stephen White3a9aab92017-03-07 14:07:18 -05001314 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001315 }
1316 }
1317}
1318
Stephen Whitee260c462017-12-19 18:09:54 -05001319bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001320 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001321 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001322 }
Stephen Whitee260c462017-12-19 18:09:54 -05001323 bool merged = false;
1324 for (Vertex* v = mesh->fHead->fNext; v;) {
1325 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001326 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1327 v->fPoint = v->fPrev->fPoint;
1328 }
1329 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001330 merge_vertices(v, v->fPrev, mesh, c, alloc);
1331 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001332 }
Stephen Whitee260c462017-12-19 18:09:54 -05001333 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001334 }
Stephen Whitee260c462017-12-19 18:09:54 -05001335 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001336}
1337
1338// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1339
Stephen White3a9aab92017-03-07 14:07:18 -05001340void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001341 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001342 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1343 Vertex* prev = contour->fTail;
1344 for (Vertex* v = contour->fHead; v;) {
1345 Vertex* next = v->fNext;
1346 connect(prev, v, Edge::Type::kInner, c, alloc);
1347 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001348 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001349 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001350 }
1351 }
ethannicholase9709e82016-01-07 13:34:16 -08001352}
1353
Stephen Whitee260c462017-12-19 18:09:54 -05001354void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1355 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001356 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001357 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1358 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1359 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1360 // number.
1361 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1362 inner->fPartner = outer->fPartner = nullptr;
1363 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001364 }
1365 }
1366}
1367
1368template <CompareFunc sweep_lt>
1369void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1370 Vertex* a = front->fHead;
1371 Vertex* b = back->fHead;
1372 while (a && b) {
1373 if (sweep_lt(a->fPoint, b->fPoint)) {
1374 front->remove(a);
1375 result->append(a);
1376 a = front->fHead;
1377 } else {
1378 back->remove(b);
1379 result->append(b);
1380 b = back->fHead;
1381 }
1382 }
1383 result->append(*front);
1384 result->append(*back);
1385}
1386
1387void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1388 if (c.fDirection == Comparator::Direction::kHorizontal) {
1389 sorted_merge<sweep_lt_horiz>(front, back, result);
1390 } else {
1391 sorted_merge<sweep_lt_vert>(front, back, result);
1392 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001393#if LOGGING_ENABLED
1394 float id = 0.0f;
1395 for (Vertex* v = result->fHead; v; v = v->fNext) {
1396 v->fID = id++;
1397 }
1398#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001399}
1400
ethannicholase9709e82016-01-07 13:34:16 -08001401// Stage 3: sort the vertices by increasing sweep direction.
1402
Stephen White16a40cb2017-02-23 11:10:01 -05001403template <CompareFunc sweep_lt>
1404void merge_sort(VertexList* vertices) {
1405 Vertex* slow = vertices->fHead;
1406 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001407 return;
1408 }
Stephen White16a40cb2017-02-23 11:10:01 -05001409 Vertex* fast = slow->fNext;
1410 if (!fast) {
1411 return;
1412 }
1413 do {
1414 fast = fast->fNext;
1415 if (fast) {
1416 fast = fast->fNext;
1417 slow = slow->fNext;
1418 }
1419 } while (fast);
1420 VertexList front(vertices->fHead, slow);
1421 VertexList back(slow->fNext, vertices->fTail);
1422 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001423
Stephen White16a40cb2017-02-23 11:10:01 -05001424 merge_sort<sweep_lt>(&front);
1425 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001426
Stephen White16a40cb2017-02-23 11:10:01 -05001427 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001428 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001429}
1430
Stephen White95152e12017-12-18 10:52:44 -05001431void dump_mesh(const VertexList& mesh) {
1432#if LOGGING_ENABLED
1433 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
1434 LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1435 if (Vertex* p = v->fPartner) {
1436 LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
1437 } else {
1438 LOG(", null partner\n");
1439 }
1440 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1441 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1442 }
1443 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1444 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1445 }
1446 }
1447#endif
1448}
1449
Stephen White89042d52018-06-08 12:18:22 -04001450#ifdef SK_DEBUG
1451void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1452 if (!left || !right) {
1453 return;
1454 }
1455 if (left->fTop == right->fTop) {
1456 SkASSERT(left->isLeftOf(right->fBottom));
1457 SkASSERT(right->isRightOf(left->fBottom));
1458 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1459 SkASSERT(left->isLeftOf(right->fTop));
1460 } else {
1461 SkASSERT(right->isRightOf(left->fTop));
1462 }
1463 if (left->fBottom == right->fBottom) {
1464 SkASSERT(left->isLeftOf(right->fTop));
1465 SkASSERT(right->isRightOf(left->fTop));
1466 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1467 SkASSERT(left->isLeftOf(right->fBottom));
1468 } else {
1469 SkASSERT(right->isRightOf(left->fBottom));
1470 }
1471}
1472
1473void validate_edge_list(EdgeList* edges, Comparator& c) {
1474 Edge* left = edges->fHead;
1475 if (!left) {
1476 return;
1477 }
1478 for (Edge* right = left->fRight; right; right = right->fRight) {
1479 validate_edge_pair(left, right, c);
1480 left = right;
1481 }
1482}
1483#endif
1484
ethannicholase9709e82016-01-07 13:34:16 -08001485// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1486
Stephen Whitee260c462017-12-19 18:09:54 -05001487bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001488 LOG("simplifying complex polygons\n");
1489 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001490 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001491 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001492 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1493 continue;
1494 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001495 Edge* leftEnclosingEdge;
1496 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001497 bool restartChecks;
1498 do {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001499 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001500 restartChecks = false;
1501 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001502 v->fLeftEnclosingEdge = leftEnclosingEdge;
1503 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001504 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001505 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001506 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001507 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001508 restartChecks = true;
1509 break;
1510 }
Stephen White0cb31672017-06-08 14:41:01 -04001511 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001512 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001513 restartChecks = true;
1514 break;
1515 }
1516 }
1517 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001518 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001519 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001520 restartChecks = true;
1521 }
1522
1523 }
Stephen Whitee260c462017-12-19 18:09:54 -05001524 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001525 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001526#ifdef SK_DEBUG
1527 validate_edge_list(&activeEdges, c);
1528#endif
ethannicholase9709e82016-01-07 13:34:16 -08001529 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1530 remove_edge(e, &activeEdges);
1531 }
1532 Edge* leftEdge = leftEnclosingEdge;
1533 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1534 insert_edge(e, leftEdge, &activeEdges);
1535 leftEdge = e;
1536 }
ethannicholase9709e82016-01-07 13:34:16 -08001537 }
Stephen Whitee260c462017-12-19 18:09:54 -05001538 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1539 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001540}
1541
1542// Stage 5: Tessellate the simplified mesh into monotone polygons.
1543
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001544Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001545 LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001546 EdgeList activeEdges;
1547 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001548 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001549 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1550 continue;
1551 }
1552#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001553 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001554#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001555 Edge* leftEnclosingEdge;
1556 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001557 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001558 Poly* leftPoly;
1559 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001560 if (v->fFirstEdgeAbove) {
1561 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1562 rightPoly = v->fLastEdgeAbove->fRightPoly;
1563 } else {
1564 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1565 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1566 }
1567#if LOGGING_ENABLED
1568 LOG("edges above:\n");
1569 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1570 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1571 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1572 }
1573 LOG("edges below:\n");
1574 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1575 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1576 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1577 }
1578#endif
1579 if (v->fFirstEdgeAbove) {
1580 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001581 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001582 }
1583 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001584 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001585 }
1586 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001587 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001588 remove_edge(e, &activeEdges);
1589 if (e->fRightPoly) {
1590 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001591 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001592 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001593 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001594 }
1595 }
1596 remove_edge(v->fLastEdgeAbove, &activeEdges);
1597 if (!v->fFirstEdgeBelow) {
1598 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1599 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1600 rightPoly->fPartner = leftPoly;
1601 leftPoly->fPartner = rightPoly;
1602 }
1603 }
1604 }
1605 if (v->fFirstEdgeBelow) {
1606 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001607 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001608 if (leftPoly == rightPoly) {
1609 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1610 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1611 leftPoly->fWinding, alloc);
1612 leftEnclosingEdge->fRightPoly = leftPoly;
1613 } else {
1614 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1615 rightPoly->fWinding, alloc);
1616 rightEnclosingEdge->fLeftPoly = rightPoly;
1617 }
ethannicholase9709e82016-01-07 13:34:16 -08001618 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001619 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001620 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1621 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001622 }
1623 }
1624 Edge* leftEdge = v->fFirstEdgeBelow;
1625 leftEdge->fLeftPoly = leftPoly;
1626 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1627 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1628 rightEdge = rightEdge->fNextEdgeBelow) {
1629 insert_edge(rightEdge, leftEdge, &activeEdges);
1630 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1631 winding += leftEdge->fWinding;
1632 if (winding != 0) {
1633 Poly* poly = new_poly(&polys, v, winding, alloc);
1634 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1635 }
1636 leftEdge = rightEdge;
1637 }
1638 v->fLastEdgeBelow->fRightPoly = rightPoly;
1639 }
1640#if LOGGING_ENABLED
1641 LOG("\nactive edges:\n");
1642 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1643 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1644 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1645 }
1646#endif
1647 }
1648 return polys;
1649}
1650
Stephen Whitebf6137e2017-01-04 15:43:26 -05001651void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001652 SkArenaAlloc& alloc) {
Stephen White49789062017-02-21 10:35:49 -05001653 LOG("removing non-boundary edges\n");
1654 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001655 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen White49789062017-02-21 10:35:49 -05001656 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1657 continue;
1658 }
1659 Edge* leftEnclosingEdge;
1660 Edge* rightEnclosingEdge;
1661 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1662 bool prevFilled = leftEnclosingEdge &&
1663 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1664 for (Edge* e = v->fFirstEdgeAbove; e;) {
1665 Edge* next = e->fNextEdgeAbove;
1666 remove_edge(e, &activeEdges);
1667 bool filled = apply_fill_type(fillType, e->fWinding);
1668 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001669 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001670 }
Stephen White49789062017-02-21 10:35:49 -05001671 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001672 e = next;
1673 }
Stephen White49789062017-02-21 10:35:49 -05001674 Edge* prev = leftEnclosingEdge;
1675 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1676 if (prev) {
1677 e->fWinding += prev->fWinding;
1678 }
1679 insert_edge(e, prev, &activeEdges);
1680 prev = e;
1681 }
senorblancof57372d2016-08-31 10:36:19 -07001682 }
senorblancof57372d2016-08-31 10:36:19 -07001683}
1684
Stephen White66412122017-03-01 11:48:27 -05001685// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001686void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001687 normal->set(SkDoubleToScalar(e->fLine.fA),
1688 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001689}
1690
Stephen Whitee260c462017-12-19 18:09:54 -05001691void reconnect(Edge* edge, Vertex* src, Vertex* dst, Comparator& c) {
1692 disconnect(edge);
1693 if (src == edge->fTop) {
1694 edge->fTop = dst;
1695 } else {
1696 SkASSERT(src == edge->fBottom);
1697 edge->fBottom = dst;
1698 }
1699 if (edge->fEvent) {
1700 edge->fEvent->fEdge = nullptr;
1701 }
1702 if (edge->fTop == edge->fBottom) {
1703 return;
1704 }
1705 if (c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001706 using std::swap;
1707 swap(edge->fTop, edge->fBottom);
Stephen Whitee260c462017-12-19 18:09:54 -05001708 edge->fWinding *= -1;
1709 }
1710 edge->recompute();
1711 insert_edge_below(edge, edge->fTop, c);
1712 insert_edge_above(edge, edge->fBottom, c);
1713 merge_collinear_edges(edge, nullptr, nullptr, c);
1714}
Stephen Whitee260c462017-12-19 18:09:54 -05001715
senorblancof57372d2016-08-31 10:36:19 -07001716// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1717// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1718// invert on stroking.
1719
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001720void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001721 Edge* prevEdge = boundary->fTail;
1722 SkVector prevNormal;
1723 get_edge_normal(prevEdge, &prevNormal);
1724 for (Edge* e = boundary->fHead; e != nullptr;) {
1725 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1726 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1727 double dist = e->dist(prev->fPoint);
1728 SkVector normal;
1729 get_edge_normal(e, &normal);
Stephen Whitee260c462017-12-19 18:09:54 -05001730 double denom = 0.0625f;
senorblancof57372d2016-08-31 10:36:19 -07001731 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001732 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001733 if (prev->fPoint != next->fPoint) {
1734 join->fLine.normalize();
1735 join->fLine = join->fLine * join->fWinding;
1736 }
senorblancof57372d2016-08-31 10:36:19 -07001737 insert_edge(join, e, boundary);
1738 remove_edge(prevEdge, boundary);
1739 remove_edge(e, boundary);
1740 if (join->fLeft && join->fRight) {
1741 prevEdge = join->fLeft;
1742 e = join;
1743 } else {
1744 prevEdge = boundary->fTail;
1745 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1746 }
1747 get_edge_normal(prevEdge, &prevNormal);
1748 } else {
1749 prevEdge = e;
1750 prevNormal = normal;
1751 e = e->fRight;
1752 }
1753 }
1754}
1755
Stephen Whitee260c462017-12-19 18:09:54 -05001756void reconnect_all_overlap_edges(Vertex* src, Vertex* dst, Edge* current, Comparator& c) {
1757 if (src->fPartner) {
1758 src->fPartner->fPartner = dst;
1759 }
1760 for (Edge* e = src->fFirstEdgeAbove; e; ) {
1761 Edge* next = e->fNextEdgeAbove;
1762 if (e->fOverlap && e != current) {
1763 reconnect(e, src, dst, c);
1764 }
1765 e = next;
1766 }
1767 for (Edge* e = src->fFirstEdgeBelow; e; ) {
1768 Edge* next = e->fNextEdgeBelow;
1769 if (e->fOverlap && e != current) {
1770 reconnect(e, src, dst, c);
1771 }
1772 e = next;
1773 }
1774}
1775
1776void Event::apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen White24289e02018-06-29 17:02:21 -04001777 if (!fEdge || !fEdge->fTop || !fEdge->fBottom) {
Stephen Whitee260c462017-12-19 18:09:54 -05001778 return;
1779 }
1780 Vertex* top = fEdge->fTop;
1781 Vertex* bottom = fEdge->fBottom;
1782 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, fEdge->fTop, c, alloc);
1783 LOG("collapsing edge %g -> %g to %g (%g, %g) alpha %d\n",
1784 top->fID, bottom->fID, dest->fID, fPoint.fX, fPoint.fY, fAlpha);
1785 reconnect_all_overlap_edges(top, dest, fEdge, c);
1786 reconnect_all_overlap_edges(bottom, dest, fEdge, c);
1787
1788 // Since the destination has multiple partners, give it none.
1789 dest->fPartner = nullptr;
Stephen White77169c82018-06-05 09:15:59 -04001790
1791 // Disconnect all collapsed edges except outer boundaries.
1792 // Those are required to preserve shape coverage and winding correctness.
1793 if (!fIsOuterBoundary) {
1794 disconnect(fEdge);
1795 } else {
1796 LOG("edge %g -> %g is outer boundary; not disconnecting.\n",
1797 fEdge->fTop->fID, fEdge->fBottom->fID);
Stephen White85dcf6b2018-07-17 16:14:31 -04001798 fEdge->fWinding = fEdge->fWinding >= 0 ? 1 : -1;
Stephen White77169c82018-06-05 09:15:59 -04001799 }
Stephen Whitee260c462017-12-19 18:09:54 -05001800
1801 // If top still has some connected edges, set its partner to dest.
1802 top->fPartner = top->fFirstEdgeAbove || top->fFirstEdgeBelow ? dest : nullptr;
1803
1804 // If bottom still has some connected edges, set its partner to dest.
1805 bottom->fPartner = bottom->fFirstEdgeAbove || bottom->fFirstEdgeBelow ? dest : nullptr;
1806}
1807
1808bool is_overlap_edge(Edge* e) {
1809 if (e->fType == Edge::Type::kOuter) {
1810 return e->fWinding != 0 && e->fWinding != 1;
1811 } else if (e->fType == Edge::Type::kInner) {
1812 return e->fWinding != 0 && e->fWinding != -2;
1813 } else {
1814 return false;
1815 }
1816}
1817
1818// This is a stripped-down version of tessellate() which computes edges which
1819// join two filled regions, which represent overlap regions, and collapses them.
1820bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1821 LOG("\nfinding overlap regions\n");
1822 EdgeList activeEdges;
1823 EventList events;
1824 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
1825 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1826 continue;
1827 }
1828 Edge* leftEnclosingEdge;
1829 Edge* rightEnclosingEdge;
1830 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1831 for (Edge* e = v->fLastEdgeAbove; e; e = e->fPrevEdgeAbove) {
1832 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1833 remove_edge(e, &activeEdges);
1834 if (prev) {
1835 e->fWinding -= prev->fWinding;
1836 }
1837 }
1838 Edge* prev = leftEnclosingEdge;
1839 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1840 if (prev) {
1841 e->fWinding += prev->fWinding;
1842 e->fOverlap = e->fOverlap || is_overlap_edge(prev);
1843 }
1844 e->fOverlap = e->fOverlap || is_overlap_edge(e);
1845 if (e->fOverlap) {
Stephen White77169c82018-06-05 09:15:59 -04001846 // If this edge borders a zero-winding area, it's a boundary; don't disconnect it.
1847 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1848 (!prev || prev->fWinding == 0 || e->fWinding == 0);
1849 create_event(e, isOuterBoundary, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001850 }
1851 insert_edge(e, prev, &activeEdges);
1852 prev = e;
1853 }
1854 }
1855 LOG("\ncollapsing overlap regions\n");
1856 if (events.count() == 0) {
1857 return false;
1858 }
1859 while (events.count() > 0) {
1860 Event* event = events.peek();
1861 events.pop();
1862 event->apply(mesh, c, alloc);
1863 }
1864 return true;
1865}
1866
1867bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1868 if (!prev || !next) {
1869 return true;
1870 }
1871 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1872 return winding != origEdge->fWinding;
1873}
Stephen White92eba8a2017-02-06 09:50:27 -05001874
senorblancof57372d2016-08-31 10:36:19 -07001875// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1876// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1877// new antialiased mesh from those vertices.
1878
Stephen Whitee260c462017-12-19 18:09:54 -05001879void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1880 Comparator& c, SkArenaAlloc& alloc) {
1881 LOG("\nstroking boundary\n");
1882 // A boundary with fewer than 3 edges is degenerate.
1883 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1884 return;
1885 }
1886 Edge* prevEdge = boundary->fTail;
1887 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1888 SkVector prevNormal;
1889 get_edge_normal(prevEdge, &prevNormal);
1890 double radius = 0.5;
1891 Line prevInner(prevEdge->fLine);
1892 prevInner.fC -= radius;
1893 Line prevOuter(prevEdge->fLine);
1894 prevOuter.fC += radius;
1895 VertexList innerVertices;
1896 VertexList outerVertices;
1897 bool innerInversion = true;
1898 bool outerInversion = true;
1899 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1900 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1901 SkVector normal;
1902 get_edge_normal(e, &normal);
1903 Line inner(e->fLine);
1904 inner.fC -= radius;
1905 Line outer(e->fLine);
1906 outer.fC += radius;
1907 SkPoint innerPoint, outerPoint;
1908 LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1909 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1910 prevOuter.intersect(outer, &outerPoint)) {
1911 float cosAngle = normal.dot(prevNormal);
1912 if (cosAngle < -kCosMiterAngle) {
1913 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1914
1915 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
1916 Line bisector(innerPoint, outerPoint);
1917 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
1918 if (tangent.fA == 0 && tangent.fB == 0) {
1919 continue;
1920 }
1921 tangent.normalize();
1922 Line innerTangent(tangent);
1923 Line outerTangent(tangent);
1924 innerTangent.fC -= 0.5;
1925 outerTangent.fC += 0.5;
1926 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
1927 if (prevNormal.cross(normal) > 0) {
1928 // Miter inner points
1929 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
1930 !innerTangent.intersect(inner, &innerPoint2) ||
1931 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001932 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001933 }
1934 Line prevTangent(prevV->fPoint,
1935 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
1936 Line nextTangent(nextV->fPoint,
1937 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001938 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001939 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001940 }
1941 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001942 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001943 }
1944 outerPoint1 = outerPoint2 = outerPoint;
1945 } else {
1946 // Miter outer points
1947 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
1948 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001949 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001950 }
1951 Line prevTangent(prevV->fPoint,
1952 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
1953 Line nextTangent(nextV->fPoint,
1954 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001955 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001956 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001957 }
1958 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001959 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001960 }
1961 innerPoint1 = innerPoint2 = innerPoint;
1962 }
Stephen Whiteea495232018-04-03 11:28:15 -04001963 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
1964 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
1965 continue;
1966 }
Stephen Whitee260c462017-12-19 18:09:54 -05001967 LOG("inner (%g, %g), (%g, %g), ",
1968 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
1969 LOG("outer (%g, %g), (%g, %g)\n",
1970 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
1971 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
1972 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
1973 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
1974 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
1975 innerVertex1->fPartner = outerVertex1;
1976 innerVertex2->fPartner = outerVertex2;
1977 outerVertex1->fPartner = innerVertex1;
1978 outerVertex2->fPartner = innerVertex2;
1979 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
1980 innerInversion = false;
1981 }
1982 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
1983 outerInversion = false;
1984 }
1985 innerVertices.append(innerVertex1);
1986 innerVertices.append(innerVertex2);
1987 outerVertices.append(outerVertex1);
1988 outerVertices.append(outerVertex2);
1989 } else {
1990 LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
1991 LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
1992 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
1993 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
1994 innerVertex->fPartner = outerVertex;
1995 outerVertex->fPartner = innerVertex;
1996 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
1997 innerInversion = false;
1998 }
1999 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
2000 outerInversion = false;
2001 }
2002 innerVertices.append(innerVertex);
2003 outerVertices.append(outerVertex);
2004 }
2005 }
2006 prevInner = inner;
2007 prevOuter = outer;
2008 prevV = v;
2009 prevEdge = e;
2010 prevNormal = normal;
2011 }
2012 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
2013 innerInversion = false;
2014 }
2015 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
2016 outerInversion = false;
2017 }
2018 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
2019 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2020 // interior inverts).
2021 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2022 // so it will be detected during collapse_overlap_regions().
2023 int innerWinding = innerInversion ? 2 : -2;
2024 int outerWinding = outerInversion ? -1 : 1;
2025 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2026 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2027 }
2028 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2029 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2030 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2031 }
2032 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2033 innerMesh->append(innerVertices);
2034 outerMesh->append(outerVertices);
2035}
senorblancof57372d2016-08-31 10:36:19 -07002036
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002037void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05002038 LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002039 bool down = apply_fill_type(fillType, e->fWinding);
senorblancof57372d2016-08-31 10:36:19 -07002040 while (e) {
2041 e->fWinding = down ? 1 : -1;
2042 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002043 e->fLine.normalize();
2044 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002045 boundary->append(e);
2046 if (down) {
2047 // Find outgoing edge, in clockwise order.
2048 if ((next = e->fNextEdgeAbove)) {
2049 down = false;
2050 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2051 down = true;
2052 } else if ((next = e->fPrevEdgeAbove)) {
2053 down = false;
2054 }
2055 } else {
2056 // Find outgoing edge, in counter-clockwise order.
2057 if ((next = e->fPrevEdgeBelow)) {
2058 down = true;
2059 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2060 down = false;
2061 } else if ((next = e->fNextEdgeBelow)) {
2062 down = true;
2063 }
2064 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002065 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002066 e = next;
2067 }
2068}
2069
Stephen White5ad721e2017-02-23 16:50:47 -05002070// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002071
Stephen Whitebda29c02017-03-13 15:10:13 -04002072void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
2073 VertexList* outerVertices, SkPath::FillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002074 Comparator& c, SkArenaAlloc& alloc) {
2075 remove_non_boundary_edges(inMesh, fillType, alloc);
2076 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002077 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002078 EdgeList boundary;
2079 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2080 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002081 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002082 }
2083 }
senorblancof57372d2016-08-31 10:36:19 -07002084}
2085
Stephen Whitebda29c02017-03-13 15:10:13 -04002086// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002087
Stephen White3a9aab92017-03-07 14:07:18 -05002088void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002089 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002090#if LOGGING_ENABLED
2091 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002092 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002093 SkASSERT(v);
2094 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002095 for (v = v->fNext; v; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002096 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
2097 }
2098 }
2099#endif
senorblancof57372d2016-08-31 10:36:19 -07002100 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002101 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002102}
2103
Stephen Whitebda29c02017-03-13 15:10:13 -04002104void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002105 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002106 return;
ethannicholase9709e82016-01-07 13:34:16 -08002107 }
2108
2109 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002110 if (c.fDirection == Comparator::Direction::kHorizontal) {
2111 merge_sort<sweep_lt_horiz>(vertices);
2112 } else {
2113 merge_sort<sweep_lt_vert>(vertices);
2114 }
ethannicholase9709e82016-01-07 13:34:16 -08002115#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002116 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002117 static float gID = 0.0f;
2118 v->fID = gID++;
2119 }
2120#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002121}
2122
Stephen White3a9aab92017-03-07 14:07:18 -05002123Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType,
Stephen Whitebda29c02017-03-13 15:10:13 -04002124 const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002125 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002126 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2127 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002128 VertexList mesh;
2129 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002130 sort_mesh(&mesh, c, alloc);
2131 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002132 simplify(&mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002133 if (antialias) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002134 VertexList innerMesh;
2135 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2136 sort_mesh(&innerMesh, c, alloc);
2137 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002138 merge_coincident_vertices(&innerMesh, c, alloc);
2139 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2140 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2141 was_complex = simplify(outerMesh, c, alloc) || was_complex;
2142 LOG("\ninner mesh before:\n");
2143 dump_mesh(innerMesh);
2144 LOG("\nouter mesh before:\n");
2145 dump_mesh(*outerMesh);
2146 was_complex = collapse_overlap_regions(&innerMesh, c, alloc) || was_complex;
2147 was_complex = collapse_overlap_regions(outerMesh, c, alloc) || was_complex;
2148 if (was_complex) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002149 LOG("found complex mesh; taking slow path\n");
2150 VertexList aaMesh;
Stephen White95152e12017-12-18 10:52:44 -05002151 LOG("\ninner mesh after:\n");
2152 dump_mesh(innerMesh);
2153 LOG("\nouter mesh after:\n");
2154 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002155 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002156 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002157 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2158 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002159 simplify(&aaMesh, c, alloc);
Stephen White95152e12017-12-18 10:52:44 -05002160 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002161 outerMesh->fHead = outerMesh->fTail = nullptr;
2162 return tessellate(aaMesh, alloc);
2163 } else {
2164 LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002165 return tessellate(innerMesh, alloc);
2166 }
Stephen White49789062017-02-21 10:35:49 -05002167 } else {
2168 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002169 }
senorblancof57372d2016-08-31 10:36:19 -07002170}
2171
2172// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2173void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
2174 void* data) {
2175 for (Poly* poly = polys; poly; poly = poly->fNext) {
2176 if (apply_fill_type(fillType, poly)) {
2177 data = poly->emit(aaParams, data);
2178 }
2179 }
2180 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002181}
2182
halcanary9d524f22016-03-29 09:03:52 -07002183Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen Whitebda29c02017-03-13 15:10:13 -04002184 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
2185 VertexList* outerMesh) {
ethannicholase9709e82016-01-07 13:34:16 -08002186 SkPath::FillType fillType = path.getFillType();
2187 if (SkPath::IsInverseFillType(fillType)) {
2188 contourCnt++;
2189 }
Stephen White3a9aab92017-03-07 14:07:18 -05002190 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002191
2192 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07002193 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Stephen Whitebda29c02017-03-13 15:10:13 -04002194 antialias, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002195}
2196
Stephen White11f65e02017-02-16 19:00:39 -05002197int get_contour_count(const SkPath& path, SkScalar tolerance) {
2198 int contourCnt;
2199 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002200 if (maxPts <= 0) {
Stephen White11f65e02017-02-16 19:00:39 -05002201 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002202 }
Stephen White11f65e02017-02-16 19:00:39 -05002203 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002204}
2205
Greg Danield5b45932018-06-07 13:15:10 -04002206int64_t count_points(Poly* polys, SkPath::FillType fillType) {
2207 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002208 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002209 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002210 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2211 }
2212 }
2213 return count;
2214}
2215
Greg Danield5b45932018-06-07 13:15:10 -04002216int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2217 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002218 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2219 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2220 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2221 }
2222 }
2223 return count;
2224}
2225
2226void* outer_mesh_to_triangles(const VertexList& outerMesh, const AAParams* aaParams, void* data) {
2227 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2228 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2229 Vertex* v0 = e->fTop;
2230 Vertex* v1 = e->fBottom;
2231 Vertex* v2 = e->fBottom->fPartner;
2232 Vertex* v3 = e->fTop->fPartner;
2233 data = emit_triangle(v0, v1, v2, aaParams, data);
2234 data = emit_triangle(v0, v2, v3, aaParams, data);
2235 }
2236 }
2237 return data;
2238}
2239
ethannicholase9709e82016-01-07 13:34:16 -08002240} // namespace
2241
2242namespace GrTessellator {
2243
2244// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2245
halcanary9d524f22016-03-29 09:03:52 -07002246int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
senorblancof57372d2016-08-31 10:36:19 -07002247 VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
2248 bool canTweakAlphaForCoverage, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002249 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002250 if (contourCnt <= 0) {
2251 *isLinear = true;
2252 return 0;
2253 }
Stephen White11f65e02017-02-16 19:00:39 -05002254 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002255 VertexList outerMesh;
senorblancof57372d2016-08-31 10:36:19 -07002256 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
Stephen Whitebda29c02017-03-13 15:10:13 -04002257 isLinear, &outerMesh);
senorblanco7ab96e92016-10-12 06:47:44 -07002258 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002259 int64_t count64 = count_points(polys, fillType);
Stephen Whitebda29c02017-03-13 15:10:13 -04002260 if (antialias) {
Greg Danield5b45932018-06-07 13:15:10 -04002261 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002262 }
Greg Danield5b45932018-06-07 13:15:10 -04002263 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002264 return 0;
2265 }
Greg Danield5b45932018-06-07 13:15:10 -04002266 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002267
senorblancof57372d2016-08-31 10:36:19 -07002268 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08002269 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002270 SkDebugf("Could not allocate vertices\n");
2271 return 0;
2272 }
senorblancof57372d2016-08-31 10:36:19 -07002273
2274 LOG("emitting %d verts\n", count);
2275 AAParams aaParams;
2276 aaParams.fTweakAlpha = canTweakAlphaForCoverage;
2277 aaParams.fColor = color;
2278
2279 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
Stephen Whitebda29c02017-03-13 15:10:13 -04002280 end = outer_mesh_to_triangles(outerMesh, &aaParams, end);
senorblancof57372d2016-08-31 10:36:19 -07002281 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
2282 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08002283 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002284 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002285 return actualCount;
2286}
2287
halcanary9d524f22016-03-29 09:03:52 -07002288int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002289 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002290 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002291 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002292 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002293 return 0;
2294 }
Stephen White11f65e02017-02-16 19:00:39 -05002295 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002296 bool isLinear;
Stephen Whitebda29c02017-03-13 15:10:13 -04002297 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
2298 nullptr);
ethannicholase9709e82016-01-07 13:34:16 -08002299 SkPath::FillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002300 int64_t count64 = count_points(polys, fillType);
2301 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002302 *verts = nullptr;
2303 return 0;
2304 }
Greg Danield5b45932018-06-07 13:15:10 -04002305 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002306
2307 *verts = new GrTessellator::WindingVertex[count];
2308 GrTessellator::WindingVertex* vertsEnd = *verts;
2309 SkPoint* points = new SkPoint[count];
2310 SkPoint* pointsEnd = points;
2311 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002312 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002313 SkPoint* start = pointsEnd;
senorblancof57372d2016-08-31 10:36:19 -07002314 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002315 while (start != pointsEnd) {
2316 vertsEnd->fPos = *start;
2317 vertsEnd->fWinding = poly->fWinding;
2318 ++start;
2319 ++vertsEnd;
2320 }
2321 }
2322 }
2323 int actualCount = static_cast<int>(vertsEnd - *verts);
2324 SkASSERT(actualCount <= count);
2325 SkASSERT(pointsEnd - points == actualCount);
2326 delete[] points;
2327 return actualCount;
2328}
2329
2330} // namespace