blob: 78616d4e2f677bb69035d65945b6f7068a043726 [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 White3b5a3fa2017-06-06 14:51:19 -04001054void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -04001055 for (;;) {
1056 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
1057 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
Stephen White24289e02018-06-29 17:02:21 -04001058 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001059 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
1060 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
Stephen White24289e02018-06-29 17:02:21 -04001061 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001062 } else if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
1063 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
Stephen White24289e02018-06-29 17:02:21 -04001064 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001065 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
1066 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
Stephen White24289e02018-06-29 17:02:21 -04001067 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -04001068 } else {
1069 break;
1070 }
ethannicholase9709e82016-01-07 13:34:16 -08001071 }
Stephen White6eca90f2017-05-25 14:47:11 -04001072 SkASSERT(!edge->fPrevEdgeAbove || edge->fPrevEdgeAbove->isLeftOf(edge->fTop));
1073 SkASSERT(!edge->fPrevEdgeBelow || edge->fPrevEdgeBelow->isLeftOf(edge->fBottom));
1074 SkASSERT(!edge->fNextEdgeAbove || edge->fNextEdgeAbove->isRightOf(edge->fTop));
1075 SkASSERT(!edge->fNextEdgeBelow || edge->fNextEdgeBelow->isRightOf(edge->fBottom));
ethannicholase9709e82016-01-07 13:34:16 -08001076}
1077
Stephen White89042d52018-06-08 12:18:22 -04001078bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001079 SkArenaAlloc& alloc) {
Stephen Whiteec79c392018-05-18 11:49:21 -04001080 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -04001081 return false;
Stephen White0cb31672017-06-08 14:41:01 -04001082 }
ethannicholase9709e82016-01-07 13:34:16 -08001083 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1084 edge->fTop->fID, edge->fBottom->fID,
1085 v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001086 Vertex* top;
1087 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -04001088 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -08001089 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001090 top = v;
1091 bottom = edge->fTop;
1092 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -05001093 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001094 top = edge->fBottom;
1095 bottom = v;
1096 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001097 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001098 top = v;
1099 bottom = edge->fBottom;
1100 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001101 }
Stephen White531a48e2018-06-01 09:49:39 -04001102 Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001103 insert_edge_below(newEdge, top, c);
1104 insert_edge_above(newEdge, bottom, c);
1105 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -04001106 return true;
1107}
1108
1109bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) {
1110 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
1111 return false;
1112 }
1113 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1114 if (!left->isLeftOf(right->fTop)) {
1115 return split_edge(left, right->fTop, activeEdges, current, c, alloc);
1116 }
1117 } else {
1118 if (!right->isRightOf(left->fTop)) {
1119 return split_edge(right, left->fTop, activeEdges, current, c, alloc);
1120 }
1121 }
1122 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1123 if (!left->isLeftOf(right->fBottom)) {
1124 return split_edge(left, right->fBottom, activeEdges, current, c, alloc);
1125 }
1126 } else {
1127 if (!right->isRightOf(left->fBottom)) {
1128 return split_edge(right, left->fBottom, activeEdges, current, c, alloc);
1129 }
1130 }
1131 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001132}
1133
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001134Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001135 int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -05001136 if (!prev || !next || prev->fPoint == next->fPoint) {
1137 return nullptr;
1138 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001139 Edge* edge = new_edge(prev, next, type, c, alloc);
Stephen White8a0bfc52017-02-21 15:24:13 -05001140 insert_edge_below(edge, edge->fTop, c);
1141 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -05001142 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -04001143 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -07001144 return edge;
1145}
1146
Stephen Whitebf6137e2017-01-04 15:43:26 -05001147void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001148 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001149 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1150 src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001151 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -04001152 if (src->fPartner) {
1153 src->fPartner->fPartner = dst;
1154 }
Stephen White7b376942018-05-22 11:51:32 -04001155 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001156 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001157 }
Stephen White7b376942018-05-22 11:51:32 -04001158 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001159 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -08001160 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001161 mesh->remove(src);
ethannicholase9709e82016-01-07 13:34:16 -08001162}
1163
Stephen White95152e12017-12-18 10:52:44 -05001164Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
1165 Vertex* reference, Comparator& c, SkArenaAlloc& alloc) {
1166 Vertex* prevV = reference;
1167 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
1168 prevV = prevV->fPrev;
1169 }
1170 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
1171 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
1172 prevV = nextV;
1173 nextV = nextV->fNext;
1174 }
1175 Vertex* v;
1176 if (prevV && coincident(prevV->fPoint, p)) {
1177 v = prevV;
1178 } else if (nextV && coincident(nextV->fPoint, p)) {
1179 v = nextV;
1180 } else {
1181 v = alloc.make<Vertex>(p, alpha);
1182#if LOGGING_ENABLED
1183 if (!prevV) {
1184 v->fID = mesh->fHead->fID - 1.0f;
1185 } else if (!nextV) {
1186 v->fID = mesh->fTail->fID + 1.0f;
1187 } else {
1188 v->fID = (prevV->fID + nextV->fID) * 0.5f;
1189 }
1190#endif
1191 mesh->insert(v, prevV, nextV);
1192 }
1193 return v;
1194}
1195
Stephen White53a02982018-05-30 22:47:46 -04001196// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
1197// sort criterion, it may not be possible to split correctly, since there is no point which is
1198// below the top and above the bottom. This function detects that case.
1199bool nearly_flat(Comparator& c, Edge* edge) {
1200 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
1201 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -04001202 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -04001203}
1204
Stephen Whitee62999f2018-06-05 18:45:07 -04001205SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) {
1206 if (c.sweep_lt(p, min)) {
1207 return min;
1208 } else if (c.sweep_lt(max, p)) {
1209 return max;
1210 } else {
1211 return p;
1212 }
1213}
1214
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001215bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Stephen White0cb31672017-06-08 14:41:01 -04001216 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001217 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001218 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001219 }
Stephen White56158ae2017-01-30 14:31:31 -05001220 SkPoint p;
1221 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001222 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001223 Vertex* v;
ethannicholase9709e82016-01-07 13:34:16 -08001224 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001225 Vertex* top = *current;
1226 // If the intersection point is above the current vertex, rewind to the vertex above the
1227 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001228 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001229 top = top->fPrev;
1230 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001231 if (!nearly_flat(c, left)) {
1232 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001233 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001234 if (!nearly_flat(c, right)) {
1235 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001236 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001237 if (p == left->fTop->fPoint) {
1238 v = left->fTop;
1239 } else if (p == left->fBottom->fPoint) {
1240 v = left->fBottom;
1241 } else if (p == right->fTop->fPoint) {
1242 v = right->fTop;
1243 } else if (p == right->fBottom->fPoint) {
1244 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001245 } else {
Stephen White95152e12017-12-18 10:52:44 -05001246 v = create_sorted_vertex(p, alpha, mesh, top, c, alloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001247 if (left->fTop->fPartner) {
1248 Line line1 = left->fLine;
1249 Line line2 = right->fLine;
1250 int dir = left->fType == Edge::Type::kOuter ? -1 : 1;
1251 line1.fC += sqrt(left->fLine.magSq()) * (left->fWinding > 0 ? 1 : -1) * dir;
1252 line2.fC += sqrt(right->fLine.magSq()) * (right->fWinding > 0 ? 1 : -1) * dir;
Stephen Whitee260c462017-12-19 18:09:54 -05001253 SkPoint p;
1254 if (line1.intersect(line2, &p)) {
1255 LOG("synthesizing partner (%g,%g) for intersection vertex %g\n",
1256 p.fX, p.fY, v->fID);
1257 v->fPartner = alloc.make<Vertex>(p, 255 - v->fAlpha);
1258 }
1259 }
ethannicholase9709e82016-01-07 13:34:16 -08001260 }
Stephen White0cb31672017-06-08 14:41:01 -04001261 rewind(activeEdges, current, top ? top : v, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001262 split_edge(left, v, activeEdges, current, c, alloc);
1263 split_edge(right, v, activeEdges, current, c, alloc);
Stephen White92eba8a2017-02-06 09:50:27 -05001264 v->fAlpha = SkTMax(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001265 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001266 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001267 return intersect_edge_pair(left, right, activeEdges, current, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001268}
1269
Stephen White3a9aab92017-03-07 14:07:18 -05001270void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) {
1271 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1272 SkASSERT(contour->fHead);
1273 Vertex* prev = contour->fTail;
Stephen White5926f2d2017-02-13 13:55:42 -05001274 if (approximate) {
Stephen White3a9aab92017-03-07 14:07:18 -05001275 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001276 }
Stephen White3a9aab92017-03-07 14:07:18 -05001277 for (Vertex* v = contour->fHead; v;) {
senorblancof57372d2016-08-31 10:36:19 -07001278 if (approximate) {
1279 round(&v->fPoint);
1280 }
Stephen White3a9aab92017-03-07 14:07:18 -05001281 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001282 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001283 if (coincident(prev->fPoint, v->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -08001284 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001285 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001286 } else if (!v->fPoint.isFinite()) {
1287 LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
1288 contour->remove(v);
Stephen White3de40f82018-06-28 09:36:49 -04001289 } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Stephen White06768ca2018-05-25 14:50:56 -04001290 LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
1291 contour->remove(v);
1292 } else {
1293 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001294 }
Stephen White3a9aab92017-03-07 14:07:18 -05001295 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001296 }
1297 }
1298}
1299
Stephen Whitee260c462017-12-19 18:09:54 -05001300bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001301 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001302 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001303 }
Stephen Whitee260c462017-12-19 18:09:54 -05001304 bool merged = false;
1305 for (Vertex* v = mesh->fHead->fNext; v;) {
1306 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001307 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1308 v->fPoint = v->fPrev->fPoint;
1309 }
1310 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001311 merge_vertices(v, v->fPrev, mesh, c, alloc);
1312 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001313 }
Stephen Whitee260c462017-12-19 18:09:54 -05001314 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001315 }
Stephen Whitee260c462017-12-19 18:09:54 -05001316 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001317}
1318
1319// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1320
Stephen White3a9aab92017-03-07 14:07:18 -05001321void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001322 SkArenaAlloc& alloc) {
Stephen White3a9aab92017-03-07 14:07:18 -05001323 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1324 Vertex* prev = contour->fTail;
1325 for (Vertex* v = contour->fHead; v;) {
1326 Vertex* next = v->fNext;
1327 connect(prev, v, Edge::Type::kInner, c, alloc);
1328 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001329 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001330 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001331 }
1332 }
ethannicholase9709e82016-01-07 13:34:16 -08001333}
1334
Stephen Whitee260c462017-12-19 18:09:54 -05001335void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1336 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001337 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001338 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1339 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1340 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1341 // number.
1342 connect(outer, inner, Edge::Type::kConnector, c, alloc, 0);
1343 inner->fPartner = outer->fPartner = nullptr;
1344 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001345 }
1346 }
1347}
1348
1349template <CompareFunc sweep_lt>
1350void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1351 Vertex* a = front->fHead;
1352 Vertex* b = back->fHead;
1353 while (a && b) {
1354 if (sweep_lt(a->fPoint, b->fPoint)) {
1355 front->remove(a);
1356 result->append(a);
1357 a = front->fHead;
1358 } else {
1359 back->remove(b);
1360 result->append(b);
1361 b = back->fHead;
1362 }
1363 }
1364 result->append(*front);
1365 result->append(*back);
1366}
1367
1368void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) {
1369 if (c.fDirection == Comparator::Direction::kHorizontal) {
1370 sorted_merge<sweep_lt_horiz>(front, back, result);
1371 } else {
1372 sorted_merge<sweep_lt_vert>(front, back, result);
1373 }
Stephen White3b5a3fa2017-06-06 14:51:19 -04001374#if LOGGING_ENABLED
1375 float id = 0.0f;
1376 for (Vertex* v = result->fHead; v; v = v->fNext) {
1377 v->fID = id++;
1378 }
1379#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001380}
1381
ethannicholase9709e82016-01-07 13:34:16 -08001382// Stage 3: sort the vertices by increasing sweep direction.
1383
Stephen White16a40cb2017-02-23 11:10:01 -05001384template <CompareFunc sweep_lt>
1385void merge_sort(VertexList* vertices) {
1386 Vertex* slow = vertices->fHead;
1387 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001388 return;
1389 }
Stephen White16a40cb2017-02-23 11:10:01 -05001390 Vertex* fast = slow->fNext;
1391 if (!fast) {
1392 return;
1393 }
1394 do {
1395 fast = fast->fNext;
1396 if (fast) {
1397 fast = fast->fNext;
1398 slow = slow->fNext;
1399 }
1400 } while (fast);
1401 VertexList front(vertices->fHead, slow);
1402 VertexList back(slow->fNext, vertices->fTail);
1403 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001404
Stephen White16a40cb2017-02-23 11:10:01 -05001405 merge_sort<sweep_lt>(&front);
1406 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001407
Stephen White16a40cb2017-02-23 11:10:01 -05001408 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001409 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001410}
1411
Stephen White95152e12017-12-18 10:52:44 -05001412void dump_mesh(const VertexList& mesh) {
1413#if LOGGING_ENABLED
1414 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
1415 LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1416 if (Vertex* p = v->fPartner) {
1417 LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
1418 } else {
1419 LOG(", null partner\n");
1420 }
1421 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1422 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1423 }
1424 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1425 LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1426 }
1427 }
1428#endif
1429}
1430
Stephen White89042d52018-06-08 12:18:22 -04001431#ifdef SK_DEBUG
1432void validate_edge_pair(Edge* left, Edge* right, Comparator& c) {
1433 if (!left || !right) {
1434 return;
1435 }
1436 if (left->fTop == right->fTop) {
1437 SkASSERT(left->isLeftOf(right->fBottom));
1438 SkASSERT(right->isRightOf(left->fBottom));
1439 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1440 SkASSERT(left->isLeftOf(right->fTop));
1441 } else {
1442 SkASSERT(right->isRightOf(left->fTop));
1443 }
1444 if (left->fBottom == right->fBottom) {
1445 SkASSERT(left->isLeftOf(right->fTop));
1446 SkASSERT(right->isRightOf(left->fTop));
1447 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1448 SkASSERT(left->isLeftOf(right->fBottom));
1449 } else {
1450 SkASSERT(right->isRightOf(left->fBottom));
1451 }
1452}
1453
1454void validate_edge_list(EdgeList* edges, Comparator& c) {
1455 Edge* left = edges->fHead;
1456 if (!left) {
1457 return;
1458 }
1459 for (Edge* right = left->fRight; right; right = right->fRight) {
1460 validate_edge_pair(left, right, c);
1461 left = right;
1462 }
1463}
1464#endif
1465
ethannicholase9709e82016-01-07 13:34:16 -08001466// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1467
Stephen Whitee260c462017-12-19 18:09:54 -05001468bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001469 LOG("simplifying complex polygons\n");
1470 EdgeList activeEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001471 bool found = false;
Stephen White0cb31672017-06-08 14:41:01 -04001472 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001473 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1474 continue;
1475 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001476 Edge* leftEnclosingEdge;
1477 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001478 bool restartChecks;
1479 do {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001480 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001481 restartChecks = false;
1482 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001483 v->fLeftEnclosingEdge = leftEnclosingEdge;
1484 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001485 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001486 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Stephen White89042d52018-06-08 12:18:22 -04001487 if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001488 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001489 restartChecks = true;
1490 break;
1491 }
Stephen White0cb31672017-06-08 14:41:01 -04001492 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c,
Stephen White3b5a3fa2017-06-06 14:51:19 -04001493 alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001494 restartChecks = true;
1495 break;
1496 }
1497 }
1498 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001499 if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
Stephen White0cb31672017-06-08 14:41:01 -04001500 &activeEdges, &v, mesh, c, alloc)) {
ethannicholase9709e82016-01-07 13:34:16 -08001501 restartChecks = true;
1502 }
1503
1504 }
Stephen Whitee260c462017-12-19 18:09:54 -05001505 found = found || restartChecks;
ethannicholase9709e82016-01-07 13:34:16 -08001506 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001507#ifdef SK_DEBUG
1508 validate_edge_list(&activeEdges, c);
1509#endif
ethannicholase9709e82016-01-07 13:34:16 -08001510 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1511 remove_edge(e, &activeEdges);
1512 }
1513 Edge* leftEdge = leftEnclosingEdge;
1514 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1515 insert_edge(e, leftEdge, &activeEdges);
1516 leftEdge = e;
1517 }
ethannicholase9709e82016-01-07 13:34:16 -08001518 }
Stephen Whitee260c462017-12-19 18:09:54 -05001519 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1520 return found;
ethannicholase9709e82016-01-07 13:34:16 -08001521}
1522
1523// Stage 5: Tessellate the simplified mesh into monotone polygons.
1524
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001525Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05001526 LOG("\ntessellating simple polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001527 EdgeList activeEdges;
1528 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001529 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001530 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1531 continue;
1532 }
1533#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001534 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001535#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001536 Edge* leftEnclosingEdge;
1537 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001538 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001539 Poly* leftPoly;
1540 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001541 if (v->fFirstEdgeAbove) {
1542 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1543 rightPoly = v->fLastEdgeAbove->fRightPoly;
1544 } else {
1545 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1546 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1547 }
1548#if LOGGING_ENABLED
1549 LOG("edges above:\n");
1550 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1551 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1552 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1553 }
1554 LOG("edges below:\n");
1555 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1556 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1557 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1558 }
1559#endif
1560 if (v->fFirstEdgeAbove) {
1561 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001562 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001563 }
1564 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001565 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001566 }
1567 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001568 Edge* rightEdge = e->fNextEdgeAbove;
Stephen White8a0bfc52017-02-21 15:24:13 -05001569 remove_edge(e, &activeEdges);
1570 if (e->fRightPoly) {
1571 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001572 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001573 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001574 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001575 }
1576 }
1577 remove_edge(v->fLastEdgeAbove, &activeEdges);
1578 if (!v->fFirstEdgeBelow) {
1579 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1580 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1581 rightPoly->fPartner = leftPoly;
1582 leftPoly->fPartner = rightPoly;
1583 }
1584 }
1585 }
1586 if (v->fFirstEdgeBelow) {
1587 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001588 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001589 if (leftPoly == rightPoly) {
1590 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1591 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1592 leftPoly->fWinding, alloc);
1593 leftEnclosingEdge->fRightPoly = leftPoly;
1594 } else {
1595 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1596 rightPoly->fWinding, alloc);
1597 rightEnclosingEdge->fLeftPoly = rightPoly;
1598 }
ethannicholase9709e82016-01-07 13:34:16 -08001599 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001600 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001601 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1602 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001603 }
1604 }
1605 Edge* leftEdge = v->fFirstEdgeBelow;
1606 leftEdge->fLeftPoly = leftPoly;
1607 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1608 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1609 rightEdge = rightEdge->fNextEdgeBelow) {
1610 insert_edge(rightEdge, leftEdge, &activeEdges);
1611 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1612 winding += leftEdge->fWinding;
1613 if (winding != 0) {
1614 Poly* poly = new_poly(&polys, v, winding, alloc);
1615 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1616 }
1617 leftEdge = rightEdge;
1618 }
1619 v->fLastEdgeBelow->fRightPoly = rightPoly;
1620 }
1621#if LOGGING_ENABLED
1622 LOG("\nactive edges:\n");
1623 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1624 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1625 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1626 }
1627#endif
1628 }
1629 return polys;
1630}
1631
Stephen Whitebf6137e2017-01-04 15:43:26 -05001632void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001633 SkArenaAlloc& alloc) {
Stephen White49789062017-02-21 10:35:49 -05001634 LOG("removing non-boundary edges\n");
1635 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001636 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Stephen White49789062017-02-21 10:35:49 -05001637 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1638 continue;
1639 }
1640 Edge* leftEnclosingEdge;
1641 Edge* rightEnclosingEdge;
1642 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1643 bool prevFilled = leftEnclosingEdge &&
1644 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1645 for (Edge* e = v->fFirstEdgeAbove; e;) {
1646 Edge* next = e->fNextEdgeAbove;
1647 remove_edge(e, &activeEdges);
1648 bool filled = apply_fill_type(fillType, e->fWinding);
1649 if (filled == prevFilled) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001650 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001651 }
Stephen White49789062017-02-21 10:35:49 -05001652 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001653 e = next;
1654 }
Stephen White49789062017-02-21 10:35:49 -05001655 Edge* prev = leftEnclosingEdge;
1656 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1657 if (prev) {
1658 e->fWinding += prev->fWinding;
1659 }
1660 insert_edge(e, prev, &activeEdges);
1661 prev = e;
1662 }
senorblancof57372d2016-08-31 10:36:19 -07001663 }
senorblancof57372d2016-08-31 10:36:19 -07001664}
1665
Stephen White66412122017-03-01 11:48:27 -05001666// Note: this is the normal to the edge, but not necessarily unit length.
senorblancof57372d2016-08-31 10:36:19 -07001667void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001668 normal->set(SkDoubleToScalar(e->fLine.fA),
1669 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001670}
1671
Stephen Whitee260c462017-12-19 18:09:54 -05001672void reconnect(Edge* edge, Vertex* src, Vertex* dst, Comparator& c) {
1673 disconnect(edge);
1674 if (src == edge->fTop) {
1675 edge->fTop = dst;
1676 } else {
1677 SkASSERT(src == edge->fBottom);
1678 edge->fBottom = dst;
1679 }
1680 if (edge->fEvent) {
1681 edge->fEvent->fEdge = nullptr;
1682 }
1683 if (edge->fTop == edge->fBottom) {
1684 return;
1685 }
1686 if (c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001687 using std::swap;
1688 swap(edge->fTop, edge->fBottom);
Stephen Whitee260c462017-12-19 18:09:54 -05001689 edge->fWinding *= -1;
1690 }
1691 edge->recompute();
1692 insert_edge_below(edge, edge->fTop, c);
1693 insert_edge_above(edge, edge->fBottom, c);
1694 merge_collinear_edges(edge, nullptr, nullptr, c);
1695}
Stephen Whitee260c462017-12-19 18:09:54 -05001696
senorblancof57372d2016-08-31 10:36:19 -07001697// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1698// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1699// invert on stroking.
1700
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001701void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001702 Edge* prevEdge = boundary->fTail;
1703 SkVector prevNormal;
1704 get_edge_normal(prevEdge, &prevNormal);
1705 for (Edge* e = boundary->fHead; e != nullptr;) {
1706 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1707 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1708 double dist = e->dist(prev->fPoint);
1709 SkVector normal;
1710 get_edge_normal(e, &normal);
Stephen Whitee260c462017-12-19 18:09:54 -05001711 double denom = 0.0625f;
senorblancof57372d2016-08-31 10:36:19 -07001712 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001713 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001714 if (prev->fPoint != next->fPoint) {
1715 join->fLine.normalize();
1716 join->fLine = join->fLine * join->fWinding;
1717 }
senorblancof57372d2016-08-31 10:36:19 -07001718 insert_edge(join, e, boundary);
1719 remove_edge(prevEdge, boundary);
1720 remove_edge(e, boundary);
1721 if (join->fLeft && join->fRight) {
1722 prevEdge = join->fLeft;
1723 e = join;
1724 } else {
1725 prevEdge = boundary->fTail;
1726 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1727 }
1728 get_edge_normal(prevEdge, &prevNormal);
1729 } else {
1730 prevEdge = e;
1731 prevNormal = normal;
1732 e = e->fRight;
1733 }
1734 }
1735}
1736
Stephen Whitee260c462017-12-19 18:09:54 -05001737void reconnect_all_overlap_edges(Vertex* src, Vertex* dst, Edge* current, Comparator& c) {
1738 if (src->fPartner) {
1739 src->fPartner->fPartner = dst;
1740 }
1741 for (Edge* e = src->fFirstEdgeAbove; e; ) {
1742 Edge* next = e->fNextEdgeAbove;
1743 if (e->fOverlap && e != current) {
1744 reconnect(e, src, dst, c);
1745 }
1746 e = next;
1747 }
1748 for (Edge* e = src->fFirstEdgeBelow; e; ) {
1749 Edge* next = e->fNextEdgeBelow;
1750 if (e->fOverlap && e != current) {
1751 reconnect(e, src, dst, c);
1752 }
1753 e = next;
1754 }
1755}
1756
1757void Event::apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen White24289e02018-06-29 17:02:21 -04001758 if (!fEdge || !fEdge->fTop || !fEdge->fBottom) {
Stephen Whitee260c462017-12-19 18:09:54 -05001759 return;
1760 }
1761 Vertex* top = fEdge->fTop;
1762 Vertex* bottom = fEdge->fBottom;
1763 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, fEdge->fTop, c, alloc);
1764 LOG("collapsing edge %g -> %g to %g (%g, %g) alpha %d\n",
1765 top->fID, bottom->fID, dest->fID, fPoint.fX, fPoint.fY, fAlpha);
1766 reconnect_all_overlap_edges(top, dest, fEdge, c);
1767 reconnect_all_overlap_edges(bottom, dest, fEdge, c);
1768
1769 // Since the destination has multiple partners, give it none.
1770 dest->fPartner = nullptr;
Stephen White77169c82018-06-05 09:15:59 -04001771
1772 // Disconnect all collapsed edges except outer boundaries.
1773 // Those are required to preserve shape coverage and winding correctness.
1774 if (!fIsOuterBoundary) {
1775 disconnect(fEdge);
1776 } else {
1777 LOG("edge %g -> %g is outer boundary; not disconnecting.\n",
1778 fEdge->fTop->fID, fEdge->fBottom->fID);
1779 }
Stephen Whitee260c462017-12-19 18:09:54 -05001780
1781 // If top still has some connected edges, set its partner to dest.
1782 top->fPartner = top->fFirstEdgeAbove || top->fFirstEdgeBelow ? dest : nullptr;
1783
1784 // If bottom still has some connected edges, set its partner to dest.
1785 bottom->fPartner = bottom->fFirstEdgeAbove || bottom->fFirstEdgeBelow ? dest : nullptr;
1786}
1787
1788bool is_overlap_edge(Edge* e) {
1789 if (e->fType == Edge::Type::kOuter) {
1790 return e->fWinding != 0 && e->fWinding != 1;
1791 } else if (e->fType == Edge::Type::kInner) {
1792 return e->fWinding != 0 && e->fWinding != -2;
1793 } else {
1794 return false;
1795 }
1796}
1797
1798// This is a stripped-down version of tessellate() which computes edges which
1799// join two filled regions, which represent overlap regions, and collapses them.
1800bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1801 LOG("\nfinding overlap regions\n");
1802 EdgeList activeEdges;
1803 EventList events;
1804 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
1805 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1806 continue;
1807 }
1808 Edge* leftEnclosingEdge;
1809 Edge* rightEnclosingEdge;
1810 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1811 for (Edge* e = v->fLastEdgeAbove; e; e = e->fPrevEdgeAbove) {
1812 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
1813 remove_edge(e, &activeEdges);
1814 if (prev) {
1815 e->fWinding -= prev->fWinding;
1816 }
1817 }
1818 Edge* prev = leftEnclosingEdge;
1819 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1820 if (prev) {
1821 e->fWinding += prev->fWinding;
1822 e->fOverlap = e->fOverlap || is_overlap_edge(prev);
1823 }
1824 e->fOverlap = e->fOverlap || is_overlap_edge(e);
1825 if (e->fOverlap) {
Stephen White77169c82018-06-05 09:15:59 -04001826 // If this edge borders a zero-winding area, it's a boundary; don't disconnect it.
1827 bool isOuterBoundary = e->fType == Edge::Type::kOuter &&
1828 (!prev || prev->fWinding == 0 || e->fWinding == 0);
1829 create_event(e, isOuterBoundary, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001830 }
1831 insert_edge(e, prev, &activeEdges);
1832 prev = e;
1833 }
1834 }
1835 LOG("\ncollapsing overlap regions\n");
1836 if (events.count() == 0) {
1837 return false;
1838 }
1839 while (events.count() > 0) {
1840 Event* event = events.peek();
1841 events.pop();
1842 event->apply(mesh, c, alloc);
1843 }
1844 return true;
1845}
1846
1847bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) {
1848 if (!prev || !next) {
1849 return true;
1850 }
1851 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1852 return winding != origEdge->fWinding;
1853}
Stephen White92eba8a2017-02-06 09:50:27 -05001854
senorblancof57372d2016-08-31 10:36:19 -07001855// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1856// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1857// new antialiased mesh from those vertices.
1858
Stephen Whitee260c462017-12-19 18:09:54 -05001859void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
1860 Comparator& c, SkArenaAlloc& alloc) {
1861 LOG("\nstroking boundary\n");
1862 // A boundary with fewer than 3 edges is degenerate.
1863 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1864 return;
1865 }
1866 Edge* prevEdge = boundary->fTail;
1867 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1868 SkVector prevNormal;
1869 get_edge_normal(prevEdge, &prevNormal);
1870 double radius = 0.5;
1871 Line prevInner(prevEdge->fLine);
1872 prevInner.fC -= radius;
1873 Line prevOuter(prevEdge->fLine);
1874 prevOuter.fC += radius;
1875 VertexList innerVertices;
1876 VertexList outerVertices;
1877 bool innerInversion = true;
1878 bool outerInversion = true;
1879 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1880 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1881 SkVector normal;
1882 get_edge_normal(e, &normal);
1883 Line inner(e->fLine);
1884 inner.fC -= radius;
1885 Line outer(e->fLine);
1886 outer.fC += radius;
1887 SkPoint innerPoint, outerPoint;
1888 LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1889 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1890 prevOuter.intersect(outer, &outerPoint)) {
1891 float cosAngle = normal.dot(prevNormal);
1892 if (cosAngle < -kCosMiterAngle) {
1893 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1894
1895 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
1896 Line bisector(innerPoint, outerPoint);
1897 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
1898 if (tangent.fA == 0 && tangent.fB == 0) {
1899 continue;
1900 }
1901 tangent.normalize();
1902 Line innerTangent(tangent);
1903 Line outerTangent(tangent);
1904 innerTangent.fC -= 0.5;
1905 outerTangent.fC += 0.5;
1906 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
1907 if (prevNormal.cross(normal) > 0) {
1908 // Miter inner points
1909 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
1910 !innerTangent.intersect(inner, &innerPoint2) ||
1911 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001912 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001913 }
1914 Line prevTangent(prevV->fPoint,
1915 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
1916 Line nextTangent(nextV->fPoint,
1917 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001918 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001919 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001920 }
1921 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001922 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001923 }
1924 outerPoint1 = outerPoint2 = outerPoint;
1925 } else {
1926 // Miter outer points
1927 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
1928 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001929 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001930 }
1931 Line prevTangent(prevV->fPoint,
1932 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
1933 Line nextTangent(nextV->fPoint,
1934 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001935 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001936 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001937 }
1938 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001939 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001940 }
1941 innerPoint1 = innerPoint2 = innerPoint;
1942 }
Stephen Whiteea495232018-04-03 11:28:15 -04001943 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
1944 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
1945 continue;
1946 }
Stephen Whitee260c462017-12-19 18:09:54 -05001947 LOG("inner (%g, %g), (%g, %g), ",
1948 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
1949 LOG("outer (%g, %g), (%g, %g)\n",
1950 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
1951 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
1952 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
1953 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
1954 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
1955 innerVertex1->fPartner = outerVertex1;
1956 innerVertex2->fPartner = outerVertex2;
1957 outerVertex1->fPartner = innerVertex1;
1958 outerVertex2->fPartner = innerVertex2;
1959 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
1960 innerInversion = false;
1961 }
1962 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
1963 outerInversion = false;
1964 }
1965 innerVertices.append(innerVertex1);
1966 innerVertices.append(innerVertex2);
1967 outerVertices.append(outerVertex1);
1968 outerVertices.append(outerVertex2);
1969 } else {
1970 LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
1971 LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
1972 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
1973 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
1974 innerVertex->fPartner = outerVertex;
1975 outerVertex->fPartner = innerVertex;
1976 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
1977 innerInversion = false;
1978 }
1979 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
1980 outerInversion = false;
1981 }
1982 innerVertices.append(innerVertex);
1983 outerVertices.append(outerVertex);
1984 }
1985 }
1986 prevInner = inner;
1987 prevOuter = outer;
1988 prevV = v;
1989 prevEdge = e;
1990 prevNormal = normal;
1991 }
1992 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
1993 innerInversion = false;
1994 }
1995 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
1996 outerInversion = false;
1997 }
1998 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
1999 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
2000 // interior inverts).
2001 // For total inversion cases, the shape has now reversed handedness, so invert the winding
2002 // so it will be detected during collapse_overlap_regions().
2003 int innerWinding = innerInversion ? 2 : -2;
2004 int outerWinding = outerInversion ? -1 : 1;
2005 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
2006 connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding);
2007 }
2008 connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding);
2009 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
2010 connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding);
2011 }
2012 connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding);
2013 innerMesh->append(innerVertices);
2014 outerMesh->append(outerVertices);
2015}
senorblancof57372d2016-08-31 10:36:19 -07002016
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002017void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -05002018 LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05002019 bool down = apply_fill_type(fillType, e->fWinding);
senorblancof57372d2016-08-31 10:36:19 -07002020 while (e) {
2021 e->fWinding = down ? 1 : -1;
2022 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05002023 e->fLine.normalize();
2024 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07002025 boundary->append(e);
2026 if (down) {
2027 // Find outgoing edge, in clockwise order.
2028 if ((next = e->fNextEdgeAbove)) {
2029 down = false;
2030 } else if ((next = e->fBottom->fLastEdgeBelow)) {
2031 down = true;
2032 } else if ((next = e->fPrevEdgeAbove)) {
2033 down = false;
2034 }
2035 } else {
2036 // Find outgoing edge, in counter-clockwise order.
2037 if ((next = e->fPrevEdgeBelow)) {
2038 down = true;
2039 } else if ((next = e->fTop->fFirstEdgeAbove)) {
2040 down = false;
2041 } else if ((next = e->fNextEdgeBelow)) {
2042 down = true;
2043 }
2044 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05002045 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07002046 e = next;
2047 }
2048}
2049
Stephen White5ad721e2017-02-23 16:50:47 -05002050// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07002051
Stephen Whitebda29c02017-03-13 15:10:13 -04002052void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
2053 VertexList* outerVertices, SkPath::FillType fillType,
Stephen White5ad721e2017-02-23 16:50:47 -05002054 Comparator& c, SkArenaAlloc& alloc) {
2055 remove_non_boundary_edges(inMesh, fillType, alloc);
2056 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002057 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05002058 EdgeList boundary;
2059 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
2060 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002061 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002062 }
2063 }
senorblancof57372d2016-08-31 10:36:19 -07002064}
2065
Stephen Whitebda29c02017-03-13 15:10:13 -04002066// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08002067
Stephen White3a9aab92017-03-07 14:07:18 -05002068void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002069 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08002070#if LOGGING_ENABLED
2071 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05002072 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08002073 SkASSERT(v);
2074 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05002075 for (v = v->fNext; v; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002076 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
2077 }
2078 }
2079#endif
senorblancof57372d2016-08-31 10:36:19 -07002080 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002081 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002082}
2083
Stephen Whitebda29c02017-03-13 15:10:13 -04002084void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05002085 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05002086 return;
ethannicholase9709e82016-01-07 13:34:16 -08002087 }
2088
2089 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05002090 if (c.fDirection == Comparator::Direction::kHorizontal) {
2091 merge_sort<sweep_lt_horiz>(vertices);
2092 } else {
2093 merge_sort<sweep_lt_vert>(vertices);
2094 }
ethannicholase9709e82016-01-07 13:34:16 -08002095#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05002096 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08002097 static float gID = 0.0f;
2098 v->fID = gID++;
2099 }
2100#endif
Stephen White2f4686f2017-01-03 16:20:01 -05002101}
2102
Stephen White3a9aab92017-03-07 14:07:18 -05002103Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType,
Stephen Whitebda29c02017-03-13 15:10:13 -04002104 const SkRect& pathBounds, bool antialias, VertexList* outerMesh,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05002105 SkArenaAlloc& alloc) {
Stephen White16a40cb2017-02-23 11:10:01 -05002106 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
2107 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05002108 VertexList mesh;
2109 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002110 sort_mesh(&mesh, c, alloc);
2111 merge_coincident_vertices(&mesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002112 simplify(&mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002113 if (antialias) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002114 VertexList innerMesh;
2115 extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc);
2116 sort_mesh(&innerMesh, c, alloc);
2117 sort_mesh(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002118 merge_coincident_vertices(&innerMesh, c, alloc);
2119 bool was_complex = merge_coincident_vertices(outerMesh, c, alloc);
2120 was_complex = simplify(&innerMesh, c, alloc) || was_complex;
2121 was_complex = simplify(outerMesh, c, alloc) || was_complex;
2122 LOG("\ninner mesh before:\n");
2123 dump_mesh(innerMesh);
2124 LOG("\nouter mesh before:\n");
2125 dump_mesh(*outerMesh);
2126 was_complex = collapse_overlap_regions(&innerMesh, c, alloc) || was_complex;
2127 was_complex = collapse_overlap_regions(outerMesh, c, alloc) || was_complex;
2128 if (was_complex) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002129 LOG("found complex mesh; taking slow path\n");
2130 VertexList aaMesh;
Stephen White95152e12017-12-18 10:52:44 -05002131 LOG("\ninner mesh after:\n");
2132 dump_mesh(innerMesh);
2133 LOG("\nouter mesh after:\n");
2134 dump_mesh(*outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002135 connect_partners(outerMesh, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05002136 connect_partners(&innerMesh, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002137 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2138 merge_coincident_vertices(&aaMesh, c, alloc);
Stephen White0cb31672017-06-08 14:41:01 -04002139 simplify(&aaMesh, c, alloc);
Stephen White95152e12017-12-18 10:52:44 -05002140 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002141 outerMesh->fHead = outerMesh->fTail = nullptr;
2142 return tessellate(aaMesh, alloc);
2143 } else {
2144 LOG("no complex polygons; taking fast path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002145 return tessellate(innerMesh, alloc);
2146 }
Stephen White49789062017-02-21 10:35:49 -05002147 } else {
2148 return tessellate(mesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07002149 }
senorblancof57372d2016-08-31 10:36:19 -07002150}
2151
2152// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2153void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
2154 void* data) {
2155 for (Poly* poly = polys; poly; poly = poly->fNext) {
2156 if (apply_fill_type(fillType, poly)) {
2157 data = poly->emit(aaParams, data);
2158 }
2159 }
2160 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002161}
2162
halcanary9d524f22016-03-29 09:03:52 -07002163Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Stephen Whitebda29c02017-03-13 15:10:13 -04002164 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear,
2165 VertexList* outerMesh) {
ethannicholase9709e82016-01-07 13:34:16 -08002166 SkPath::FillType fillType = path.getFillType();
2167 if (SkPath::IsInverseFillType(fillType)) {
2168 contourCnt++;
2169 }
Stephen White3a9aab92017-03-07 14:07:18 -05002170 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002171
2172 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07002173 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
Stephen Whitebda29c02017-03-13 15:10:13 -04002174 antialias, outerMesh, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08002175}
2176
Stephen White11f65e02017-02-16 19:00:39 -05002177int get_contour_count(const SkPath& path, SkScalar tolerance) {
2178 int contourCnt;
2179 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002180 if (maxPts <= 0) {
Stephen White11f65e02017-02-16 19:00:39 -05002181 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002182 }
Stephen White11f65e02017-02-16 19:00:39 -05002183 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002184}
2185
Greg Danield5b45932018-06-07 13:15:10 -04002186int64_t count_points(Poly* polys, SkPath::FillType fillType) {
2187 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002188 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002189 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08002190 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
2191 }
2192 }
2193 return count;
2194}
2195
Greg Danield5b45932018-06-07 13:15:10 -04002196int64_t count_outer_mesh_points(const VertexList& outerMesh) {
2197 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002198 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2199 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2200 count += TESSELLATOR_WIREFRAME ? 12 : 6;
2201 }
2202 }
2203 return count;
2204}
2205
2206void* outer_mesh_to_triangles(const VertexList& outerMesh, const AAParams* aaParams, void* data) {
2207 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2208 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2209 Vertex* v0 = e->fTop;
2210 Vertex* v1 = e->fBottom;
2211 Vertex* v2 = e->fBottom->fPartner;
2212 Vertex* v3 = e->fTop->fPartner;
2213 data = emit_triangle(v0, v1, v2, aaParams, data);
2214 data = emit_triangle(v0, v2, v3, aaParams, data);
2215 }
2216 }
2217 return data;
2218}
2219
ethannicholase9709e82016-01-07 13:34:16 -08002220} // namespace
2221
2222namespace GrTessellator {
2223
2224// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2225
halcanary9d524f22016-03-29 09:03:52 -07002226int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
senorblancof57372d2016-08-31 10:36:19 -07002227 VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
2228 bool canTweakAlphaForCoverage, bool* isLinear) {
Stephen White11f65e02017-02-16 19:00:39 -05002229 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002230 if (contourCnt <= 0) {
2231 *isLinear = true;
2232 return 0;
2233 }
Stephen White11f65e02017-02-16 19:00:39 -05002234 SkArenaAlloc alloc(kArenaChunkSize);
Stephen Whitebda29c02017-03-13 15:10:13 -04002235 VertexList outerMesh;
senorblancof57372d2016-08-31 10:36:19 -07002236 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
Stephen Whitebda29c02017-03-13 15:10:13 -04002237 isLinear, &outerMesh);
senorblanco7ab96e92016-10-12 06:47:44 -07002238 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002239 int64_t count64 = count_points(polys, fillType);
Stephen Whitebda29c02017-03-13 15:10:13 -04002240 if (antialias) {
Greg Danield5b45932018-06-07 13:15:10 -04002241 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002242 }
Greg Danield5b45932018-06-07 13:15:10 -04002243 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002244 return 0;
2245 }
Greg Danield5b45932018-06-07 13:15:10 -04002246 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002247
senorblancof57372d2016-08-31 10:36:19 -07002248 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08002249 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002250 SkDebugf("Could not allocate vertices\n");
2251 return 0;
2252 }
senorblancof57372d2016-08-31 10:36:19 -07002253
2254 LOG("emitting %d verts\n", count);
2255 AAParams aaParams;
2256 aaParams.fTweakAlpha = canTweakAlphaForCoverage;
2257 aaParams.fColor = color;
2258
2259 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
Stephen Whitebda29c02017-03-13 15:10:13 -04002260 end = outer_mesh_to_triangles(outerMesh, &aaParams, end);
senorblancof57372d2016-08-31 10:36:19 -07002261 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
2262 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08002263 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002264 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002265 return actualCount;
2266}
2267
halcanary9d524f22016-03-29 09:03:52 -07002268int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08002269 GrTessellator::WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002270 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002271 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002272 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002273 return 0;
2274 }
Stephen White11f65e02017-02-16 19:00:39 -05002275 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08002276 bool isLinear;
Stephen Whitebda29c02017-03-13 15:10:13 -04002277 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear,
2278 nullptr);
ethannicholase9709e82016-01-07 13:34:16 -08002279 SkPath::FillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002280 int64_t count64 = count_points(polys, fillType);
2281 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002282 *verts = nullptr;
2283 return 0;
2284 }
Greg Danield5b45932018-06-07 13:15:10 -04002285 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002286
2287 *verts = new GrTessellator::WindingVertex[count];
2288 GrTessellator::WindingVertex* vertsEnd = *verts;
2289 SkPoint* points = new SkPoint[count];
2290 SkPoint* pointsEnd = points;
2291 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002292 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002293 SkPoint* start = pointsEnd;
senorblancof57372d2016-08-31 10:36:19 -07002294 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002295 while (start != pointsEnd) {
2296 vertsEnd->fPos = *start;
2297 vertsEnd->fWinding = poly->fWinding;
2298 ++start;
2299 ++vertsEnd;
2300 }
2301 }
2302 }
2303 int actualCount = static_cast<int>(vertsEnd - *verts);
2304 SkASSERT(actualCount <= count);
2305 SkASSERT(pointsEnd - points == actualCount);
2306 delete[] points;
2307 return actualCount;
2308}
2309
2310} // namespace