blob: d103d609e86d49de994c9e13a891462ae12931b6 [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"
ethannicholase9709e82016-01-07 13:34:16 -080016
17#include <stdio.h>
18
19/*
senorblancof57372d2016-08-31 10:36:19 -070020 * There are six stages to the basic algorithm:
ethannicholase9709e82016-01-07 13:34:16 -080021 *
22 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
23 * 2) Build a mesh of edges connecting the vertices (build_edges()).
24 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
25 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
26 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
27 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
28 *
senorblancof57372d2016-08-31 10:36:19 -070029 * For screenspace antialiasing, the algorithm is modified as follows:
30 *
31 * Run steps 1-5 above to produce polygons.
32 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
33 * 5c) Simplify boundaries to remove "pointy" vertices which cause inversions (simplify_boundary()).
34 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
35 * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
36 * antialiased mesh from those vertices (boundary_to_aa_mesh()).
37 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
38 *
ethannicholase9709e82016-01-07 13:34:16 -080039 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
40 * of vertices (and the necessity of inserting new vertices on intersection).
41 *
42 * Stages (4) and (5) use an active edge list, which a list of all edges for which the
43 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
44 * left-to-right based on the point where both edges are active (when both top vertices
45 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
46 * (shared), it's sorted based on the last point where both edges are active, so the
47 * "upper" bottom vertex.
48 *
49 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
50 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
51 * not exact and may violate the mesh topology or active edge list ordering. We
52 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
53 * points. This occurs in three ways:
54 *
55 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
56 * neighbouring edges at the top or bottom vertex. This is handled by merging the
57 * edges (merge_collinear_edges()).
58 * B) Intersections may cause an edge to violate the left-to-right ordering of the
59 * active edge list. This is handled by splitting the neighbour edge on the
60 * intersected vertex (cleanup_active_edges()).
61 * C) Shortening an edge may cause an active edge to become inactive or an inactive edge
62 * to become active. This is handled by removing or inserting the edge in the active
63 * edge list (fix_active_state()).
64 *
65 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
66 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
67 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
68 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
69 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
70 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
71 * linked list implementation. With the latter, all removals are O(1), and most insertions
72 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
73 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
74 * frequent. There may be other data structures worth investigating, however.
75 *
76 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
77 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
78 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
79 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
80 * that the "left" and "right" orientation in the code remains correct (edges to the left are
81 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
82 * degrees counterclockwise, rather that transposing.
83 */
84
85#define LOGGING_ENABLED 0
86
87#if LOGGING_ENABLED
88#define LOG printf
89#else
90#define LOG(...)
91#endif
92
ethannicholase9709e82016-01-07 13:34:16 -080093namespace {
94
Stephen Whitee595bbf2017-02-16 11:27:01 -050095const int kArenaChunkSize = 16 * 1024;
96
ethannicholase9709e82016-01-07 13:34:16 -080097struct Vertex;
98struct Edge;
99struct Poly;
100
101template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800102void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800103 t->*Prev = prev;
104 t->*Next = next;
105 if (prev) {
106 prev->*Next = t;
107 } else if (head) {
108 *head = t;
109 }
110 if (next) {
111 next->*Prev = t;
112 } else if (tail) {
113 *tail = t;
114 }
115}
116
117template <class T, T* T::*Prev, T* T::*Next>
senorblancoe6eaa322016-03-08 09:06:44 -0800118void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -0800119 if (t->*Prev) {
120 t->*Prev->*Next = t->*Next;
121 } else if (head) {
122 *head = t->*Next;
123 }
124 if (t->*Next) {
125 t->*Next->*Prev = t->*Prev;
126 } else if (tail) {
127 *tail = t->*Prev;
128 }
129 t->*Prev = t->*Next = nullptr;
130}
131
132/**
133 * Vertices are used in three ways: first, the path contours are converted into a
134 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
135 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
136 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
137 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
138 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
139 * an individual Vertex from the path mesh may belong to multiple
140 * MonotonePolys, so the original Vertices cannot be re-used.
141 */
142
143struct Vertex {
senorblancof57372d2016-08-31 10:36:19 -0700144 Vertex(const SkPoint& point, uint8_t alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800145 : fPoint(point), fPrev(nullptr), fNext(nullptr)
146 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
147 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
148 , fProcessed(false)
senorblancof57372d2016-08-31 10:36:19 -0700149 , fAlpha(alpha)
ethannicholase9709e82016-01-07 13:34:16 -0800150#if LOGGING_ENABLED
151 , fID (-1.0f)
152#endif
153 {}
154 SkPoint fPoint; // Vertex position
155 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
156 Vertex* fNext; // "
157 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
158 Edge* fLastEdgeAbove; // "
159 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
160 Edge* fLastEdgeBelow; // "
161 bool fProcessed; // Has this vertex been seen in simplify()?
senorblancof57372d2016-08-31 10:36:19 -0700162 uint8_t fAlpha;
ethannicholase9709e82016-01-07 13:34:16 -0800163#if LOGGING_ENABLED
164 float fID; // Identifier used for logging.
165#endif
166};
167
168/***************************************************************************************/
169
senorblancof57372d2016-08-31 10:36:19 -0700170struct AAParams {
171 bool fTweakAlpha;
172 GrColor fColor;
173};
174
ethannicholase9709e82016-01-07 13:34:16 -0800175typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
176
177struct Comparator {
178 CompareFunc sweep_lt;
179 CompareFunc sweep_gt;
180};
181
182bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
183 return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX;
184}
185
186bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
187 return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY;
188}
189
190bool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) {
191 return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX;
192}
193
194bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
195 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
196}
197
senorblancof57372d2016-08-31 10:36:19 -0700198inline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) {
199 if (!aaParams) {
200 SkPoint* d = static_cast<SkPoint*>(data);
201 *d++ = v->fPoint;
202 return d;
203 }
204 if (aaParams->fTweakAlpha) {
205 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data);
206 d->fPosition = v->fPoint;
lsalzman8c8fcef2016-10-11 12:20:17 -0700207 d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha));
senorblancof57372d2016-08-31 10:36:19 -0700208 d++;
209 return d;
210 }
211 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data);
212 d->fPosition = v->fPoint;
213 d->fColor = aaParams->fColor;
214 d->fCoverage = GrNormalizeByteToFloat(v->fAlpha);
215 d++;
216 return d;
ethannicholase9709e82016-01-07 13:34:16 -0800217}
218
senorblancof57372d2016-08-31 10:36:19 -0700219void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) {
Stephen White92eba8a2017-02-06 09:50:27 -0500220 LOG("emit_triangle (%g, %g) %d\n", v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
221 LOG(" (%g, %g) %d\n", v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
222 LOG(" (%g, %g) %d\n", v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700223#if TESSELLATOR_WIREFRAME
224 data = emit_vertex(v0, aaParams, data);
225 data = emit_vertex(v1, aaParams, data);
226 data = emit_vertex(v1, aaParams, data);
227 data = emit_vertex(v2, aaParams, data);
228 data = emit_vertex(v2, aaParams, data);
229 data = emit_vertex(v0, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800230#else
senorblancof57372d2016-08-31 10:36:19 -0700231 data = emit_vertex(v0, aaParams, data);
232 data = emit_vertex(v1, aaParams, data);
233 data = emit_vertex(v2, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800234#endif
235 return data;
236}
237
senorblancoe6eaa322016-03-08 09:06:44 -0800238struct VertexList {
239 VertexList() : fHead(nullptr), fTail(nullptr) {}
240 Vertex* fHead;
241 Vertex* fTail;
242 void insert(Vertex* v, Vertex* prev, Vertex* next) {
243 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
244 }
245 void append(Vertex* v) {
246 insert(v, fTail, nullptr);
247 }
248 void prepend(Vertex* v) {
249 insert(v, nullptr, fHead);
250 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500251 void remove(Vertex* v) {
252 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
253 }
senorblancof57372d2016-08-31 10:36:19 -0700254 void close() {
255 if (fHead && fTail) {
256 fTail->fNext = fHead;
257 fHead->fPrev = fTail;
258 }
259 }
senorblancoe6eaa322016-03-08 09:06:44 -0800260};
261
senorblancof57372d2016-08-31 10:36:19 -0700262// Round to nearest quarter-pixel. This is used for screenspace tessellation.
263
264inline void round(SkPoint* p) {
265 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
266 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
267}
268
senorblanco49df8d12016-10-07 08:36:56 -0700269// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
270struct Line {
271 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
272 Line(const SkPoint& p, const SkPoint& q)
273 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
274 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
275 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
276 static_cast<double>(p.fX) * q.fY) {}
277 double dist(const SkPoint& p) const {
278 return fA * p.fX + fB * p.fY + fC;
279 }
280 double magSq() const {
281 return fA * fA + fB * fB;
282 }
283
284 // Compute the intersection of two (infinite) Lines.
285 bool intersect(const Line& other, SkPoint* point) {
286 double denom = fA * other.fB - fB * other.fA;
287 if (denom == 0.0) {
288 return false;
289 }
290 double scale = 1.0f / denom;
291 point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale);
292 point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale);
senorblanco49df8d12016-10-07 08:36:56 -0700293 return true;
294 }
295 double fA, fB, fC;
296};
297
ethannicholase9709e82016-01-07 13:34:16 -0800298/**
299 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
300 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
301 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
302 * point). For speed, that case is only tested by the callers which require it (e.g.,
303 * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
304 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
305 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
306 * a lot faster in the "not found" case.
307 *
308 * The coefficients of the line equation stored in double precision to avoid catastrphic
309 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
310 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
311 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
312 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
313 * this file).
314 */
315
316struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500317 enum class Type { kInner, kOuter, kConnector };
318 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800319 : fWinding(winding)
320 , fTop(top)
321 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500322 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800323 , fLeft(nullptr)
324 , fRight(nullptr)
325 , fPrevEdgeAbove(nullptr)
326 , fNextEdgeAbove(nullptr)
327 , fPrevEdgeBelow(nullptr)
328 , fNextEdgeBelow(nullptr)
329 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700330 , fRightPoly(nullptr)
331 , fLeftPolyPrev(nullptr)
332 , fLeftPolyNext(nullptr)
333 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700334 , fRightPolyNext(nullptr)
335 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700336 , fUsedInRightPoly(false)
337 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800338 }
339 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
340 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
341 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500342 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800343 Edge* fLeft; // The linked list of edges in the active edge list.
344 Edge* fRight; // "
345 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
346 Edge* fNextEdgeAbove; // "
347 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
348 Edge* fNextEdgeBelow; // "
349 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
350 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700351 Edge* fLeftPolyPrev;
352 Edge* fLeftPolyNext;
353 Edge* fRightPolyPrev;
354 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700355 bool fUsedInLeftPoly;
356 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700357 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800358 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700359 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800360 }
361 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700362 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800363 }
364 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700365 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800366 }
367 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700368 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800369 }
Stephen Whitee595bbf2017-02-16 11:27:01 -0500370 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const {
ethannicholase9709e82016-01-07 13:34:16 -0800371 LOG("intersecting %g -> %g with %g -> %g\n",
372 fTop->fID, fBottom->fID,
373 other.fTop->fID, other.fBottom->fID);
374 if (fTop == other.fTop || fBottom == other.fBottom) {
375 return false;
376 }
senorblanco49df8d12016-10-07 08:36:56 -0700377 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800378 if (denom == 0.0) {
379 return false;
380 }
381 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
382 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
senorblanco49df8d12016-10-07 08:36:56 -0700383 double sNumer = -dy * other.fLine.fB - dx * other.fLine.fA;
384 double tNumer = -dy * fLine.fB - dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800385 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
386 // This saves us doing the divide below unless absolutely necessary.
387 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
388 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
389 return false;
390 }
391 double s = sNumer / denom;
392 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700393 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
394 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
Stephen White56158ae2017-01-30 14:31:31 -0500395 if (alpha) {
Stephen White92eba8a2017-02-06 09:50:27 -0500396 if (fType == Type::kConnector) {
397 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
398 } else if (other.fType == Type::kConnector) {
399 double t = tNumer / denom;
400 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
Stephen White56158ae2017-01-30 14:31:31 -0500401 } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
402 *alpha = 0;
403 } else {
Stephen White92eba8a2017-02-06 09:50:27 -0500404 *alpha = 255;
Stephen White56158ae2017-01-30 14:31:31 -0500405 }
406 }
ethannicholase9709e82016-01-07 13:34:16 -0800407 return true;
408 }
senorblancof57372d2016-08-31 10:36:19 -0700409};
410
411struct EdgeList {
412 EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr), fCount(0) {}
413 Edge* fHead;
414 Edge* fTail;
415 EdgeList* fNext;
416 int fCount;
417 void insert(Edge* edge, Edge* prev, Edge* next) {
418 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
419 fCount++;
420 }
421 void append(Edge* e) {
422 insert(e, fTail, nullptr);
423 }
424 void remove(Edge* edge) {
425 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
426 fCount--;
427 }
428 void close() {
429 if (fHead && fTail) {
430 fTail->fRight = fHead;
431 fHead->fLeft = fTail;
432 }
433 }
434 bool contains(Edge* edge) const {
435 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800436 }
437};
438
439/***************************************************************************************/
440
441struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700442 Poly(Vertex* v, int winding)
443 : fFirstVertex(v)
444 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800445 , fHead(nullptr)
446 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800447 , fNext(nullptr)
448 , fPartner(nullptr)
449 , fCount(0)
450 {
451#if LOGGING_ENABLED
452 static int gID = 0;
453 fID = gID++;
454 LOG("*** created Poly %d\n", fID);
455#endif
456 }
senorblanco531237e2016-06-02 11:36:48 -0700457 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800458 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700459 MonotonePoly(Edge* edge, Side side)
460 : fSide(side)
461 , fFirstEdge(nullptr)
462 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800463 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700464 , fNext(nullptr) {
465 this->addEdge(edge);
466 }
ethannicholase9709e82016-01-07 13:34:16 -0800467 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700468 Edge* fFirstEdge;
469 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800470 MonotonePoly* fPrev;
471 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700472 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800473 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700474 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700475 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
476 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700477 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800478 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700479 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700480 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
481 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700482 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800483 }
ethannicholase9709e82016-01-07 13:34:16 -0800484 }
485
senorblancof57372d2016-08-31 10:36:19 -0700486 void* emit(const AAParams* aaParams, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700487 Edge* e = fFirstEdge;
488 e->fTop->fPrev = e->fTop->fNext = nullptr;
489 VertexList vertices;
490 vertices.append(e->fTop);
491 while (e != nullptr) {
492 e->fBottom->fPrev = e->fBottom->fNext = nullptr;
493 if (kRight_Side == fSide) {
494 vertices.append(e->fBottom);
495 e = e->fRightPolyNext;
496 } else {
497 vertices.prepend(e->fBottom);
498 e = e->fLeftPolyNext;
499 }
500 }
501 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800502 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700503 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800504 SkASSERT(v && v->fPrev && v->fNext);
505 Vertex* prev = v->fPrev;
506 Vertex* curr = v;
507 Vertex* next = v->fNext;
508 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
509 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
510 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
511 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
512 if (ax * by - ay * bx >= 0.0) {
senorblancof57372d2016-08-31 10:36:19 -0700513 data = emit_triangle(prev, curr, next, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800514 v->fPrev->fNext = v->fNext;
515 v->fNext->fPrev = v->fPrev;
516 if (v->fPrev == first) {
517 v = v->fNext;
518 } else {
519 v = v->fPrev;
520 }
521 } else {
522 v = v->fNext;
523 }
524 }
525 return data;
526 }
527 };
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500528 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
senorblanco70f52512016-08-17 14:56:22 -0700529 LOG("addEdge (%g -> %g) to poly %d, %s side\n",
530 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800531 Poly* partner = fPartner;
532 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700533 if (side == kRight_Side) {
534 if (e->fUsedInRightPoly) {
535 return this;
536 }
537 } else {
538 if (e->fUsedInLeftPoly) {
539 return this;
540 }
541 }
ethannicholase9709e82016-01-07 13:34:16 -0800542 if (partner) {
543 fPartner = partner->fPartner = nullptr;
544 }
senorblanco531237e2016-06-02 11:36:48 -0700545 if (!fTail) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500546 fHead = fTail = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700547 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700548 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
549 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700550 } else if (side == fTail->fSide) {
551 fTail->addEdge(e);
552 fCount++;
553 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500554 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -0700555 fTail->addEdge(e);
556 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800557 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700558 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800559 poly = partner;
560 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500561 MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
senorblanco531237e2016-06-02 11:36:48 -0700562 m->fPrev = fTail;
563 fTail->fNext = m;
564 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800565 }
566 }
ethannicholase9709e82016-01-07 13:34:16 -0800567 return poly;
568 }
senorblancof57372d2016-08-31 10:36:19 -0700569 void* emit(const AAParams* aaParams, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800570 if (fCount < 3) {
571 return data;
572 }
573 LOG("emit() %d, size %d\n", fID, fCount);
574 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
senorblancof57372d2016-08-31 10:36:19 -0700575 data = m->emit(aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800576 }
577 return data;
578 }
senorblanco531237e2016-06-02 11:36:48 -0700579 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
580 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800581 int fWinding;
582 MonotonePoly* fHead;
583 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800584 Poly* fNext;
585 Poly* fPartner;
586 int fCount;
587#if LOGGING_ENABLED
588 int fID;
589#endif
590};
591
592/***************************************************************************************/
593
594bool coincident(const SkPoint& a, const SkPoint& b) {
595 return a == b;
596}
597
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500598Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
599 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800600 poly->fNext = *head;
601 *head = poly;
602 return poly;
603}
604
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500605EdgeList* new_contour(EdgeList** head, SkArenaAlloc& alloc) {
606 EdgeList* contour = alloc.make<EdgeList>();
senorblancof57372d2016-08-31 10:36:19 -0700607 contour->fNext = *head;
608 *head = contour;
609 return contour;
610}
611
ethannicholase9709e82016-01-07 13:34:16 -0800612Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500613 SkArenaAlloc& alloc) {
614 Vertex* v = alloc.make<Vertex>(p, 255);
ethannicholase9709e82016-01-07 13:34:16 -0800615#if LOGGING_ENABLED
616 static float gID = 0.0f;
617 v->fID = gID++;
618#endif
619 if (prev) {
620 prev->fNext = v;
621 v->fPrev = prev;
622 } else {
623 *head = v;
624 }
625 return v;
626}
627
628Vertex* generate_quadratic_points(const SkPoint& p0,
629 const SkPoint& p1,
630 const SkPoint& p2,
631 SkScalar tolSqd,
632 Vertex* prev,
633 Vertex** head,
634 int pointsLeft,
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500635 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -0800636 SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
637 if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
638 return append_point_to_contour(p2, prev, head, alloc);
639 }
640
641 const SkPoint q[] = {
642 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
643 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
644 };
645 const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
646
647 pointsLeft >>= 1;
648 prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
649 prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
650 return prev;
651}
652
653Vertex* generate_cubic_points(const SkPoint& p0,
654 const SkPoint& p1,
655 const SkPoint& p2,
656 const SkPoint& p3,
657 SkScalar tolSqd,
658 Vertex* prev,
659 Vertex** head,
660 int pointsLeft,
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500661 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -0800662 SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
663 SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
664 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
665 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
666 return append_point_to_contour(p3, prev, head, alloc);
667 }
668 const SkPoint q[] = {
669 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
670 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
671 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
672 };
673 const SkPoint r[] = {
674 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
675 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
676 };
677 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
678 pointsLeft >>= 1;
679 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
680 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
681 return prev;
682}
683
684// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
685
686void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500687 Vertex** contours, SkArenaAlloc& alloc, bool *isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -0800688 SkScalar toleranceSqd = tolerance * tolerance;
689
690 SkPoint pts[4];
691 bool done = false;
692 *isLinear = true;
693 SkPath::Iter iter(path, false);
694 Vertex* prev = nullptr;
695 Vertex* head = nullptr;
696 if (path.isInverseFillType()) {
697 SkPoint quad[4];
698 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700699 for (int i = 3; i >= 0; i--) {
ethannicholase9709e82016-01-07 13:34:16 -0800700 prev = append_point_to_contour(quad[i], prev, &head, alloc);
701 }
702 head->fPrev = prev;
703 prev->fNext = head;
704 *contours++ = head;
705 head = prev = nullptr;
706 }
707 SkAutoConicToQuads converter;
708 while (!done) {
709 SkPath::Verb verb = iter.next(pts);
710 switch (verb) {
711 case SkPath::kConic_Verb: {
712 SkScalar weight = iter.conicWeight();
713 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
714 for (int i = 0; i < converter.countQuads(); ++i) {
715 int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
716 prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
717 toleranceSqd, prev, &head, pointsLeft, alloc);
718 quadPts += 2;
719 }
720 *isLinear = false;
721 break;
722 }
723 case SkPath::kMove_Verb:
724 if (head) {
725 head->fPrev = prev;
726 prev->fNext = head;
727 *contours++ = head;
728 }
729 head = prev = nullptr;
730 prev = append_point_to_contour(pts[0], prev, &head, alloc);
731 break;
732 case SkPath::kLine_Verb: {
733 prev = append_point_to_contour(pts[1], prev, &head, alloc);
734 break;
735 }
736 case SkPath::kQuad_Verb: {
737 int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
738 prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
739 &head, pointsLeft, alloc);
740 *isLinear = false;
741 break;
742 }
743 case SkPath::kCubic_Verb: {
744 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
745 prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
746 toleranceSqd, prev, &head, pointsLeft, alloc);
747 *isLinear = false;
748 break;
749 }
750 case SkPath::kClose_Verb:
751 if (head) {
752 head->fPrev = prev;
753 prev->fNext = head;
754 *contours++ = head;
755 }
756 head = prev = nullptr;
757 break;
758 case SkPath::kDone_Verb:
759 if (head) {
760 head->fPrev = prev;
761 prev->fNext = head;
762 *contours++ = head;
763 }
764 done = true;
765 break;
766 }
767 }
768}
769
senorblancof57372d2016-08-31 10:36:19 -0700770inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
771 if (!poly) {
772 return false;
773 }
774 int winding = poly->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800775 switch (fillType) {
776 case SkPath::kWinding_FillType:
777 return winding != 0;
778 case SkPath::kEvenOdd_FillType:
779 return (winding & 1) != 0;
780 case SkPath::kInverseWinding_FillType:
senorblanco7ab96e92016-10-12 06:47:44 -0700781 return winding == 1;
ethannicholase9709e82016-01-07 13:34:16 -0800782 case SkPath::kInverseEvenOdd_FillType:
783 return (winding & 1) == 1;
784 default:
785 SkASSERT(false);
786 return false;
787 }
788}
789
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500790Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500791 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800792 Vertex* top = winding < 0 ? next : prev;
793 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500794 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800795}
796
797void remove_edge(Edge* edge, EdgeList* edges) {
798 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700799 SkASSERT(edges->contains(edge));
800 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800801}
802
803void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
804 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700805 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800806 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700807 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800808}
809
810void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
811 if (v->fFirstEdgeAbove) {
812 *left = v->fFirstEdgeAbove->fLeft;
813 *right = v->fLastEdgeAbove->fRight;
814 return;
815 }
816 Edge* next = nullptr;
817 Edge* prev;
818 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
819 if (prev->isLeftOf(v)) {
820 break;
821 }
822 next = prev;
823 }
824 *left = prev;
825 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800826}
827
828void find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
829 Edge* prev = nullptr;
830 Edge* next;
831 for (next = edges->fHead; next != nullptr; next = next->fRight) {
832 if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
833 (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
834 (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
835 next->isRightOf(edge->fBottom)) ||
836 (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
837 edge->isLeftOf(next->fBottom))) {
838 break;
839 }
840 prev = next;
841 }
842 *left = prev;
843 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800844}
845
846void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
Stephen White2f4686f2017-01-03 16:20:01 -0500847 if (!activeEdges) {
848 return;
849 }
850 if (activeEdges->contains(edge)) {
ethannicholase9709e82016-01-07 13:34:16 -0800851 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
852 remove_edge(edge, activeEdges);
853 }
854 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
855 Edge* left;
856 Edge* right;
857 find_enclosing_edges(edge, activeEdges, c, &left, &right);
858 insert_edge(edge, left, activeEdges);
859 }
860}
861
862void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
863 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
864 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
865 return;
866 }
867 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
868 Edge* prev = nullptr;
869 Edge* next;
870 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
871 if (next->isRightOf(edge->fTop)) {
872 break;
873 }
874 prev = next;
875 }
senorblancoe6eaa322016-03-08 09:06:44 -0800876 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800877 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
878}
879
880void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
881 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
882 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
883 return;
884 }
885 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
886 Edge* prev = nullptr;
887 Edge* next;
888 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
889 if (next->isRightOf(edge->fBottom)) {
890 break;
891 }
892 prev = next;
893 }
senorblancoe6eaa322016-03-08 09:06:44 -0800894 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800895 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
896}
897
898void remove_edge_above(Edge* edge) {
899 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
900 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800901 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800902 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
903}
904
905void remove_edge_below(Edge* edge) {
906 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
907 edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800908 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800909 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
910}
911
Stephen Whitee7a364d2017-01-11 16:19:26 -0500912void disconnect(Edge* edge)
913{
ethannicholase9709e82016-01-07 13:34:16 -0800914 remove_edge_above(edge);
915 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500916}
917
918void erase_edge(Edge* edge, EdgeList* edges) {
919 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
920 disconnect(edge);
senorblancof57372d2016-08-31 10:36:19 -0700921 if (edges && edges->contains(edge)) {
ethannicholase9709e82016-01-07 13:34:16 -0800922 remove_edge(edge, edges);
923 }
924}
925
926void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
927
928void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
929 remove_edge_below(edge);
930 edge->fTop = v;
931 edge->recompute();
932 insert_edge_below(edge, v, c);
933 fix_active_state(edge, activeEdges, c);
934 merge_collinear_edges(edge, activeEdges, c);
935}
936
937void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
938 remove_edge_above(edge);
939 edge->fBottom = v;
940 edge->recompute();
941 insert_edge_above(edge, v, c);
942 fix_active_state(edge, activeEdges, c);
943 merge_collinear_edges(edge, activeEdges, c);
944}
945
946void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
947 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
948 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
949 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
950 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
951 other->fWinding += edge->fWinding;
Stephen Whitee7a364d2017-01-11 16:19:26 -0500952 erase_edge(edge, activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -0800953 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
954 other->fWinding += edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800955 set_bottom(edge, other->fTop, activeEdges, c);
956 } else {
957 edge->fWinding += other->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800958 set_bottom(other, edge->fTop, activeEdges, c);
959 }
960}
961
962void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
963 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
964 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
965 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
966 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
967 other->fWinding += edge->fWinding;
Stephen Whitee7a364d2017-01-11 16:19:26 -0500968 erase_edge(edge, activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -0800969 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
970 edge->fWinding += other->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800971 set_top(other, edge->fBottom, activeEdges, c);
972 } else {
973 other->fWinding += edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800974 set_top(edge, other->fBottom, activeEdges, c);
975 }
976}
977
978void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
979 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
980 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
981 merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
982 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
983 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
984 merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
985 }
986 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
987 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
988 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
989 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
990 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
991 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
992 }
993}
994
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500995void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800996
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500997void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -0800998 Vertex* top = edge->fTop;
999 Vertex* bottom = edge->fBottom;
1000 if (edge->fLeft) {
1001 Vertex* leftTop = edge->fLeft->fTop;
1002 Vertex* leftBottom = edge->fLeft->fBottom;
1003 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
1004 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
1005 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
1006 split_edge(edge, leftTop, activeEdges, c, alloc);
1007 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
1008 !edge->fLeft->isLeftOf(bottom)) {
1009 split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
1010 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
1011 split_edge(edge, leftBottom, activeEdges, c, alloc);
1012 }
1013 }
1014 if (edge->fRight) {
1015 Vertex* rightTop = edge->fRight->fTop;
1016 Vertex* rightBottom = edge->fRight->fBottom;
1017 if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
1018 split_edge(edge->fRight, top, activeEdges, c, alloc);
1019 } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
1020 split_edge(edge, rightTop, activeEdges, c, alloc);
1021 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1022 !edge->fRight->isRightOf(bottom)) {
1023 split_edge(edge->fRight, bottom, activeEdges, c, alloc);
1024 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1025 !edge->isLeftOf(rightBottom)) {
1026 split_edge(edge, rightBottom, activeEdges, c, alloc);
1027 }
1028 }
1029}
1030
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001031void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001032 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1033 edge->fTop->fID, edge->fBottom->fID,
1034 v->fID, v->fPoint.fX, v->fPoint.fY);
1035 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
1036 set_top(edge, v, activeEdges, c);
1037 } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
1038 set_bottom(edge, v, activeEdges, c);
1039 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001040 Edge* newEdge = alloc.make<Edge>(v, edge->fBottom, edge->fWinding, edge->fType);
ethannicholase9709e82016-01-07 13:34:16 -08001041 insert_edge_below(newEdge, v, c);
1042 insert_edge_above(newEdge, edge->fBottom, c);
1043 set_bottom(edge, v, activeEdges, c);
1044 cleanup_active_edges(edge, activeEdges, c, alloc);
1045 fix_active_state(newEdge, activeEdges, c);
1046 merge_collinear_edges(newEdge, activeEdges, c);
1047 }
1048}
1049
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001050Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
Stephen White48ded382017-02-03 10:15:16 -05001051 int winding_scale = 1) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001052 Edge* edge = new_edge(prev, next, type, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001053 if (edge->fWinding > 0) {
1054 insert_edge_below(edge, prev, c);
1055 insert_edge_above(edge, next, c);
1056 } else {
1057 insert_edge_below(edge, next, c);
1058 insert_edge_above(edge, prev, c);
1059 }
Stephen White48ded382017-02-03 10:15:16 -05001060 edge->fWinding *= winding_scale;
senorblancof57372d2016-08-31 10:36:19 -07001061 merge_collinear_edges(edge, nullptr, c);
1062 return edge;
1063}
1064
Stephen Whitebf6137e2017-01-04 15:43:26 -05001065void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001066 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001067 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1068 src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001069 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001070 for (Edge* edge = src->fFirstEdgeAbove; edge;) {
1071 Edge* next = edge->fNextEdgeAbove;
1072 set_bottom(edge, dst, nullptr, c);
1073 edge = next;
1074 }
1075 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
1076 Edge* next = edge->fNextEdgeBelow;
1077 set_top(edge, dst, nullptr, c);
1078 edge = next;
1079 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001080 mesh->remove(src);
ethannicholase9709e82016-01-07 13:34:16 -08001081}
1082
senorblancof57372d2016-08-31 10:36:19 -07001083uint8_t max_edge_alpha(Edge* a, Edge* b) {
Stephen White56158ae2017-01-30 14:31:31 -05001084 if (a->fType == Edge::Type::kInner || b->fType == Edge::Type::kInner) {
Stephen White2f4686f2017-01-03 16:20:01 -05001085 return 255;
1086 } else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) {
1087 return 0;
1088 } else {
1089 return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
1090 SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
1091 }
senorblancof57372d2016-08-31 10:36:19 -07001092}
1093
ethannicholase9709e82016-01-07 13:34:16 -08001094Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001095 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001096 if (!edge || !other) {
1097 return nullptr;
1098 }
Stephen White56158ae2017-01-30 14:31:31 -05001099 SkPoint p;
1100 uint8_t alpha;
1101 if (edge->intersect(*other, &p, &alpha)) {
ethannicholase9709e82016-01-07 13:34:16 -08001102 Vertex* v;
1103 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
1104 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
1105 split_edge(other, edge->fTop, activeEdges, c, alloc);
1106 v = edge->fTop;
1107 } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) {
1108 split_edge(other, edge->fBottom, activeEdges, c, alloc);
1109 v = edge->fBottom;
1110 } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
1111 split_edge(edge, other->fTop, activeEdges, c, alloc);
1112 v = other->fTop;
1113 } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) {
1114 split_edge(edge, other->fBottom, activeEdges, c, alloc);
1115 v = other->fBottom;
1116 } else {
1117 Vertex* nextV = edge->fTop;
1118 while (c.sweep_lt(p, nextV->fPoint)) {
1119 nextV = nextV->fPrev;
1120 }
1121 while (c.sweep_lt(nextV->fPoint, p)) {
1122 nextV = nextV->fNext;
1123 }
1124 Vertex* prevV = nextV->fPrev;
1125 if (coincident(prevV->fPoint, p)) {
1126 v = prevV;
1127 } else if (coincident(nextV->fPoint, p)) {
1128 v = nextV;
1129 } else {
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001130 v = alloc.make<Vertex>(p, alpha);
ethannicholase9709e82016-01-07 13:34:16 -08001131 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
1132 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
1133 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
1134#if LOGGING_ENABLED
1135 v->fID = (nextV->fID + prevV->fID) * 0.5f;
1136#endif
1137 v->fPrev = prevV;
1138 v->fNext = nextV;
1139 prevV->fNext = v;
1140 nextV->fPrev = v;
1141 }
1142 split_edge(edge, v, activeEdges, c, alloc);
1143 split_edge(other, v, activeEdges, c, alloc);
1144 }
Stephen White92eba8a2017-02-06 09:50:27 -05001145 v->fAlpha = SkTMax(v->fAlpha, alpha);
ethannicholase9709e82016-01-07 13:34:16 -08001146 return v;
1147 }
1148 return nullptr;
1149}
1150
senorblancof57372d2016-08-31 10:36:19 -07001151void sanitize_contours(Vertex** contours, int contourCnt, bool approximate) {
ethannicholase9709e82016-01-07 13:34:16 -08001152 for (int i = 0; i < contourCnt; ++i) {
1153 SkASSERT(contours[i]);
Stephen White5926f2d2017-02-13 13:55:42 -05001154 if (approximate) {
1155 round(&contours[i]->fPrev->fPoint);
1156 }
ethannicholase9709e82016-01-07 13:34:16 -08001157 for (Vertex* v = contours[i];;) {
senorblancof57372d2016-08-31 10:36:19 -07001158 if (approximate) {
1159 round(&v->fPoint);
1160 }
ethannicholase9709e82016-01-07 13:34:16 -08001161 if (coincident(v->fPrev->fPoint, v->fPoint)) {
1162 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1163 if (v->fPrev == v) {
1164 contours[i] = nullptr;
1165 break;
1166 }
1167 v->fPrev->fNext = v->fNext;
1168 v->fNext->fPrev = v->fPrev;
1169 if (contours[i] == v) {
Stephen White5926f2d2017-02-13 13:55:42 -05001170 contours[i] = v->fPrev;
ethannicholase9709e82016-01-07 13:34:16 -08001171 }
1172 v = v->fPrev;
1173 } else {
1174 v = v->fNext;
1175 if (v == contours[i]) break;
1176 }
1177 }
1178 }
1179}
1180
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001181void merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001182 for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001183 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1184 v->fPoint = v->fPrev->fPoint;
1185 }
1186 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001187 merge_vertices(v->fPrev, v, mesh, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001188 }
1189 }
1190}
1191
1192// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1193
Stephen Whitebf6137e2017-01-04 15:43:26 -05001194void build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001195 SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001196 Vertex* prev = nullptr;
1197 for (int i = 0; i < contourCnt; ++i) {
1198 for (Vertex* v = contours[i]; v != nullptr;) {
1199 Vertex* vNext = v->fNext;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001200 connect(v->fPrev, v, Edge::Type::kInner, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001201 if (prev) {
1202 prev->fNext = v;
1203 v->fPrev = prev;
1204 } else {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001205 mesh->fHead = v;
ethannicholase9709e82016-01-07 13:34:16 -08001206 }
1207 prev = v;
1208 v = vNext;
1209 if (v == contours[i]) break;
1210 }
1211 }
1212 if (prev) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001213 prev->fNext = mesh->fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001214 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001215 mesh->fTail = prev;
ethannicholase9709e82016-01-07 13:34:16 -08001216}
1217
1218// Stage 3: sort the vertices by increasing sweep direction.
1219
Stephen Whitebf6137e2017-01-04 15:43:26 -05001220void sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c);
ethannicholase9709e82016-01-07 13:34:16 -08001221
Stephen Whitebf6137e2017-01-04 15:43:26 -05001222void front_back_split(VertexList* v, VertexList* front, VertexList* back) {
ethannicholase9709e82016-01-07 13:34:16 -08001223 Vertex* fast;
1224 Vertex* slow;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001225 if (!v->fHead || !v->fHead->fNext) {
1226 *front = *v;
ethannicholase9709e82016-01-07 13:34:16 -08001227 } else {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001228 slow = v->fHead;
1229 fast = v->fHead->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001230
1231 while (fast != nullptr) {
1232 fast = fast->fNext;
1233 if (fast != nullptr) {
1234 slow = slow->fNext;
1235 fast = fast->fNext;
1236 }
1237 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001238 front->fHead = v->fHead;
1239 front->fTail = slow;
1240 back->fHead = slow->fNext;
1241 back->fTail = v->fTail;
ethannicholase9709e82016-01-07 13:34:16 -08001242 slow->fNext->fPrev = nullptr;
1243 slow->fNext = nullptr;
1244 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001245 v->fHead = v->fTail = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001246}
1247
Stephen Whitebf6137e2017-01-04 15:43:26 -05001248void merge_sort(VertexList* mesh, Comparator& c) {
1249 if (!mesh->fHead || !mesh->fHead->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001250 return;
1251 }
1252
Stephen Whitebf6137e2017-01-04 15:43:26 -05001253 VertexList a;
1254 VertexList b;
1255 front_back_split(mesh, &a, &b);
ethannicholase9709e82016-01-07 13:34:16 -08001256
1257 merge_sort(&a, c);
1258 merge_sort(&b, c);
1259
Stephen Whitebf6137e2017-01-04 15:43:26 -05001260 sorted_merge(a.fHead, b.fHead, mesh, c);
ethannicholase9709e82016-01-07 13:34:16 -08001261}
1262
Stephen Whitebf6137e2017-01-04 15:43:26 -05001263void sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c) {
senorblancoe6eaa322016-03-08 09:06:44 -08001264 VertexList vertices;
ethannicholase9709e82016-01-07 13:34:16 -08001265 while (a && b) {
1266 if (c.sweep_lt(a->fPoint, b->fPoint)) {
1267 Vertex* next = a->fNext;
senorblancoe6eaa322016-03-08 09:06:44 -08001268 vertices.append(a);
ethannicholase9709e82016-01-07 13:34:16 -08001269 a = next;
1270 } else {
1271 Vertex* next = b->fNext;
senorblancoe6eaa322016-03-08 09:06:44 -08001272 vertices.append(b);
ethannicholase9709e82016-01-07 13:34:16 -08001273 b = next;
1274 }
1275 }
1276 if (a) {
senorblancoe6eaa322016-03-08 09:06:44 -08001277 vertices.insert(a, vertices.fTail, a->fNext);
ethannicholase9709e82016-01-07 13:34:16 -08001278 }
1279 if (b) {
senorblancoe6eaa322016-03-08 09:06:44 -08001280 vertices.insert(b, vertices.fTail, b->fNext);
ethannicholase9709e82016-01-07 13:34:16 -08001281 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001282 *result = vertices;
ethannicholase9709e82016-01-07 13:34:16 -08001283}
1284
1285// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1286
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001287void simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001288 LOG("simplifying complex polygons\n");
1289 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001290 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001291 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1292 continue;
1293 }
1294#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001295 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001296#endif
1297 Edge* leftEnclosingEdge = nullptr;
1298 Edge* rightEnclosingEdge = nullptr;
1299 bool restartChecks;
1300 do {
1301 restartChecks = false;
1302 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1303 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001304 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
ethannicholase9709e82016-01-07 13:34:16 -08001305 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
1306 restartChecks = true;
1307 break;
1308 }
1309 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
1310 restartChecks = true;
1311 break;
1312 }
1313 }
1314 } else {
1315 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1316 &activeEdges, c, alloc)) {
1317 if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1318 v = pv;
1319 }
1320 restartChecks = true;
1321 }
1322
1323 }
1324 } while (restartChecks);
senorblancof57372d2016-08-31 10:36:19 -07001325 if (v->fAlpha == 0) {
Stephen White48ded382017-02-03 10:15:16 -05001326 if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) &&
1327 (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) {
senorblancof57372d2016-08-31 10:36:19 -07001328 v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge);
1329 }
1330 }
ethannicholase9709e82016-01-07 13:34:16 -08001331 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1332 remove_edge(e, &activeEdges);
1333 }
1334 Edge* leftEdge = leftEnclosingEdge;
1335 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1336 insert_edge(e, leftEdge, &activeEdges);
1337 leftEdge = e;
1338 }
1339 v->fProcessed = true;
1340 }
1341}
1342
1343// Stage 5: Tessellate the simplified mesh into monotone polygons.
1344
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001345Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001346 LOG("tessellating simple polygons\n");
1347 EdgeList activeEdges;
1348 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001349 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001350 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1351 continue;
1352 }
1353#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001354 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001355#endif
1356 Edge* leftEnclosingEdge = nullptr;
1357 Edge* rightEnclosingEdge = nullptr;
1358 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1359 Poly* leftPoly = nullptr;
1360 Poly* rightPoly = nullptr;
1361 if (v->fFirstEdgeAbove) {
1362 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1363 rightPoly = v->fLastEdgeAbove->fRightPoly;
1364 } else {
1365 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1366 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1367 }
1368#if LOGGING_ENABLED
1369 LOG("edges above:\n");
1370 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1371 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1372 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1373 }
1374 LOG("edges below:\n");
1375 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1376 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1377 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1378 }
1379#endif
1380 if (v->fFirstEdgeAbove) {
1381 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001382 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001383 }
1384 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001385 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001386 }
1387 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1388 Edge* leftEdge = e;
1389 Edge* rightEdge = e->fNextEdgeAbove;
1390 SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
1391 remove_edge(leftEdge, &activeEdges);
1392 if (leftEdge->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001393 leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001394 }
senorblanco531237e2016-06-02 11:36:48 -07001395 if (rightEdge->fLeftPoly) {
1396 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001397 }
1398 }
1399 remove_edge(v->fLastEdgeAbove, &activeEdges);
1400 if (!v->fFirstEdgeBelow) {
1401 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1402 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1403 rightPoly->fPartner = leftPoly;
1404 leftPoly->fPartner = rightPoly;
1405 }
1406 }
1407 }
1408 if (v->fFirstEdgeBelow) {
1409 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001410 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001411 if (leftPoly == rightPoly) {
1412 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1413 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1414 leftPoly->fWinding, alloc);
1415 leftEnclosingEdge->fRightPoly = leftPoly;
1416 } else {
1417 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1418 rightPoly->fWinding, alloc);
1419 rightEnclosingEdge->fLeftPoly = rightPoly;
1420 }
ethannicholase9709e82016-01-07 13:34:16 -08001421 }
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001422 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
senorblanco531237e2016-06-02 11:36:48 -07001423 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1424 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001425 }
1426 }
1427 Edge* leftEdge = v->fFirstEdgeBelow;
1428 leftEdge->fLeftPoly = leftPoly;
1429 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1430 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1431 rightEdge = rightEdge->fNextEdgeBelow) {
1432 insert_edge(rightEdge, leftEdge, &activeEdges);
1433 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1434 winding += leftEdge->fWinding;
1435 if (winding != 0) {
1436 Poly* poly = new_poly(&polys, v, winding, alloc);
1437 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1438 }
1439 leftEdge = rightEdge;
1440 }
1441 v->fLastEdgeBelow->fRightPoly = rightPoly;
1442 }
1443#if LOGGING_ENABLED
1444 LOG("\nactive edges:\n");
1445 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1446 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1447 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1448 }
1449#endif
1450 }
1451 return polys;
1452}
1453
senorblancof57372d2016-08-31 10:36:19 -07001454bool is_boundary_edge(Edge* edge, SkPath::FillType fillType) {
1455 return apply_fill_type(fillType, edge->fLeftPoly) !=
1456 apply_fill_type(fillType, edge->fRightPoly);
1457}
1458
1459bool is_boundary_start(Edge* edge, SkPath::FillType fillType) {
1460 return !apply_fill_type(fillType, edge->fLeftPoly) &&
1461 apply_fill_type(fillType, edge->fRightPoly);
1462}
1463
Stephen Whitebf6137e2017-01-04 15:43:26 -05001464void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001465 SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001466 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001467 for (Edge* e = v->fFirstEdgeBelow; e != nullptr;) {
1468 Edge* next = e->fNextEdgeBelow;
1469 if (!is_boundary_edge(e, fillType)) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001470 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001471 }
1472 e = next;
1473 }
1474 }
senorblancof57372d2016-08-31 10:36:19 -07001475}
1476
senorblancof57372d2016-08-31 10:36:19 -07001477void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whiteeaf00792017-01-16 11:47:21 -05001478 normal->setNormalize(SkDoubleToScalar(e->fLine.fA) * e->fWinding,
1479 SkDoubleToScalar(e->fLine.fB) * e->fWinding);
senorblancof57372d2016-08-31 10:36:19 -07001480}
1481
1482// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1483// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1484// invert on stroking.
1485
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001486void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001487 Edge* prevEdge = boundary->fTail;
1488 SkVector prevNormal;
1489 get_edge_normal(prevEdge, &prevNormal);
1490 for (Edge* e = boundary->fHead; e != nullptr;) {
1491 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1492 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1493 double dist = e->dist(prev->fPoint);
1494 SkVector normal;
1495 get_edge_normal(e, &normal);
Stephen Whiteeaf00792017-01-16 11:47:21 -05001496 float denom = 0.0625f * static_cast<float>(e->fLine.magSq());
senorblancof57372d2016-08-31 10:36:19 -07001497 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001498 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001499 insert_edge(join, e, boundary);
1500 remove_edge(prevEdge, boundary);
1501 remove_edge(e, boundary);
1502 if (join->fLeft && join->fRight) {
1503 prevEdge = join->fLeft;
1504 e = join;
1505 } else {
1506 prevEdge = boundary->fTail;
1507 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1508 }
1509 get_edge_normal(prevEdge, &prevNormal);
1510 } else {
1511 prevEdge = e;
1512 prevNormal = normal;
1513 e = e->fRight;
1514 }
1515 }
1516}
1517
Stephen Whitee595bbf2017-02-16 11:27:01 -05001518void fix_inversions(Vertex* prev, Vertex* next, const Edge& prevBisector, const Edge& nextBisector,
Stephen White92eba8a2017-02-06 09:50:27 -05001519 Edge* prevEdge, Comparator& c) {
1520 if (!prev || !next) {
1521 return;
1522 }
1523 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1524 SkPoint p;
1525 uint8_t alpha;
Stephen Whitee595bbf2017-02-16 11:27:01 -05001526 if (winding != prevEdge->fWinding && prevBisector.intersect(nextBisector, &p, &alpha)) {
Stephen White92eba8a2017-02-06 09:50:27 -05001527 prev->fPoint = next->fPoint = p;
1528 prev->fAlpha = next->fAlpha = alpha;
1529 }
1530}
1531
senorblancof57372d2016-08-31 10:36:19 -07001532// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1533// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1534// new antialiased mesh from those vertices.
1535
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001536void boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001537 Edge* prevEdge = boundary->fTail;
1538 float radius = 0.5f;
senorblanco49df8d12016-10-07 08:36:56 -07001539 double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
1540 Line prevInner(prevEdge->fTop, prevEdge->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001541 prevInner.fC -= offset;
senorblanco49df8d12016-10-07 08:36:56 -07001542 Line prevOuter(prevEdge->fTop, prevEdge->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001543 prevOuter.fC += offset;
1544 VertexList innerVertices;
1545 VertexList outerVertices;
Stephen Whitee595bbf2017-02-16 11:27:01 -05001546 Edge prevBisector(*prevEdge);
senorblancof57372d2016-08-31 10:36:19 -07001547 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
senorblanco49df8d12016-10-07 08:36:56 -07001548 double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding;
1549 Line inner(e->fTop, e->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001550 inner.fC -= offset;
senorblanco49df8d12016-10-07 08:36:56 -07001551 Line outer(e->fTop, e->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001552 outer.fC += offset;
1553 SkPoint innerPoint, outerPoint;
senorblanco49df8d12016-10-07 08:36:56 -07001554 if (prevInner.intersect(inner, &innerPoint) &&
1555 prevOuter.intersect(outer, &outerPoint)) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001556 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
1557 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
Stephen Whitee595bbf2017-02-16 11:27:01 -05001558 Edge bisector(outerVertex, innerVertex, 0, Edge::Type::kConnector);
Stephen White92eba8a2017-02-06 09:50:27 -05001559 fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c);
1560 fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c);
1561 innerVertices.append(innerVertex);
1562 outerVertices.append(outerVertex);
1563 prevBisector = bisector;
senorblancof57372d2016-08-31 10:36:19 -07001564 }
1565 prevInner = inner;
1566 prevOuter = outer;
Stephen White86cc8412017-01-27 10:53:15 -05001567 prevEdge = e;
senorblancof57372d2016-08-31 10:36:19 -07001568 }
1569 innerVertices.close();
1570 outerVertices.close();
1571
1572 Vertex* innerVertex = innerVertices.fHead;
1573 Vertex* outerVertex = outerVertices.fHead;
senorblancof57372d2016-08-31 10:36:19 -07001574 if (!innerVertex || !outerVertex) {
1575 return;
1576 }
Stephen Whitee595bbf2017-02-16 11:27:01 -05001577 Edge bisector(outerVertices.fHead, innerVertices.fHead, 0, Edge::Type::kConnector);
Stephen White92eba8a2017-02-06 09:50:27 -05001578 fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c);
1579 fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c);
senorblancof57372d2016-08-31 10:36:19 -07001580 do {
Stephen White48ded382017-02-03 10:15:16 -05001581 // Connect vertices into a quad mesh. Outer edges get default (1) winding.
1582 // Inner edges get -2 winding. This ensures that the interior is always filled
1583 // (-1 winding number for normal cases, 3 for thin features where the interior inverts).
1584 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1585 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding number.
Stephen Whitebf6137e2017-01-04 15:43:26 -05001586 connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc);
Stephen White48ded382017-02-03 10:15:16 -05001587 connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc, -2);
1588 connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc, 0);
senorblancof57372d2016-08-31 10:36:19 -07001589 Vertex* innerNext = innerVertex->fNext;
1590 Vertex* outerNext = outerVertex->fNext;
1591 mesh->append(innerVertex);
1592 mesh->append(outerVertex);
1593 innerVertex = innerNext;
1594 outerVertex = outerNext;
1595 } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead);
1596}
1597
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001598void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001599 bool down = is_boundary_start(e, fillType);
1600 while (e) {
1601 e->fWinding = down ? 1 : -1;
1602 Edge* next;
1603 boundary->append(e);
1604 if (down) {
1605 // Find outgoing edge, in clockwise order.
1606 if ((next = e->fNextEdgeAbove)) {
1607 down = false;
1608 } else if ((next = e->fBottom->fLastEdgeBelow)) {
1609 down = true;
1610 } else if ((next = e->fPrevEdgeAbove)) {
1611 down = false;
1612 }
1613 } else {
1614 // Find outgoing edge, in counter-clockwise order.
1615 if ((next = e->fPrevEdgeBelow)) {
1616 down = true;
1617 } else if ((next = e->fTop->fFirstEdgeAbove)) {
1618 down = false;
1619 } else if ((next = e->fNextEdgeBelow)) {
1620 down = true;
1621 }
1622 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05001623 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001624 e = next;
1625 }
1626}
1627
1628// Stage 5b: Extract boundary edges.
1629
Stephen Whitebf6137e2017-01-04 15:43:26 -05001630EdgeList* extract_boundaries(const VertexList& mesh, SkPath::FillType fillType,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001631 SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001632 LOG("extracting boundaries\n");
Stephen Whitebf6137e2017-01-04 15:43:26 -05001633 remove_non_boundary_edges(mesh, fillType, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001634 EdgeList* boundaries = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001635 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001636 while (v->fFirstEdgeBelow) {
1637 EdgeList* boundary = new_contour(&boundaries, alloc);
1638 extract_boundary(boundary, v->fFirstEdgeBelow, fillType, alloc);
1639 }
1640 }
1641 return boundaries;
1642}
1643
ethannicholase9709e82016-01-07 13:34:16 -08001644// This is a driver function which calls stages 2-5 in turn.
1645
Stephen Whitebf6137e2017-01-04 15:43:26 -05001646void contours_to_mesh(Vertex** contours, int contourCnt, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001647 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001648#if LOGGING_ENABLED
1649 for (int i = 0; i < contourCnt; ++i) {
1650 Vertex* v = contours[i];
1651 SkASSERT(v);
1652 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1653 for (v = v->fNext; v != contours[i]; v = v->fNext) {
1654 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1655 }
1656 }
1657#endif
senorblancof57372d2016-08-31 10:36:19 -07001658 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05001659 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001660}
1661
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001662void sort_and_simplify(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001663 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05001664 return;
ethannicholase9709e82016-01-07 13:34:16 -08001665 }
1666
1667 // Sort vertices in Y (secondarily in X).
senorblancof57372d2016-08-31 10:36:19 -07001668 merge_sort(vertices, c);
1669 merge_coincident_vertices(vertices, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001670#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05001671 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001672 static float gID = 0.0f;
1673 v->fID = gID++;
1674 }
1675#endif
senorblancof57372d2016-08-31 10:36:19 -07001676 simplify(*vertices, c, alloc);
Stephen White2f4686f2017-01-03 16:20:01 -05001677}
1678
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001679Poly* mesh_to_polys(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -05001680 sort_and_simplify(vertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001681 return tessellate(*vertices, alloc);
1682}
1683
1684Poly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType,
1685 const SkRect& pathBounds, bool antialias,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001686 SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001687 Comparator c;
1688 if (pathBounds.width() > pathBounds.height()) {
1689 c.sweep_lt = sweep_lt_horiz;
1690 c.sweep_gt = sweep_gt_horiz;
1691 } else {
1692 c.sweep_lt = sweep_lt_vert;
1693 c.sweep_gt = sweep_gt_vert;
1694 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001695 VertexList mesh;
1696 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
senorblanco7ab96e92016-10-12 06:47:44 -07001697 Poly* polys = mesh_to_polys(&mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001698 if (antialias) {
1699 EdgeList* boundaries = extract_boundaries(mesh, fillType, alloc);
1700 VertexList aaMesh;
1701 for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = boundary->fNext) {
1702 simplify_boundary(boundary, c, alloc);
1703 if (boundary->fCount > 2) {
1704 boundary_to_aa_mesh(boundary, &aaMesh, c, alloc);
1705 }
1706 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001707 sort_and_simplify(&aaMesh, c, alloc);
1708 return tessellate(aaMesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001709 }
1710 return polys;
1711}
1712
1713// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1714void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
1715 void* data) {
1716 for (Poly* poly = polys; poly; poly = poly->fNext) {
1717 if (apply_fill_type(fillType, poly)) {
1718 data = poly->emit(aaParams, data);
1719 }
1720 }
1721 return data;
ethannicholase9709e82016-01-07 13:34:16 -08001722}
1723
halcanary9d524f22016-03-29 09:03:52 -07001724Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
Herb Derby5cdc9dd2017-02-13 12:10:46 -05001725 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -08001726 SkPath::FillType fillType = path.getFillType();
1727 if (SkPath::IsInverseFillType(fillType)) {
1728 contourCnt++;
1729 }
Ben Wagner7ecc5962016-11-02 17:07:33 -04001730 std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08001731
1732 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07001733 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
1734 antialias, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001735}
1736
Stephen Whitee595bbf2017-02-16 11:27:01 -05001737int get_contour_count(const SkPath& path, SkScalar tolerance) {
1738 int contourCnt;
1739 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08001740 if (maxPts <= 0) {
Stephen Whitee595bbf2017-02-16 11:27:01 -05001741 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08001742 }
1743 if (maxPts > ((int)SK_MaxU16 + 1)) {
1744 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
Stephen Whitee595bbf2017-02-16 11:27:01 -05001745 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08001746 }
Stephen Whitee595bbf2017-02-16 11:27:01 -05001747 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08001748}
1749
1750int count_points(Poly* polys, SkPath::FillType fillType) {
1751 int count = 0;
1752 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001753 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08001754 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1755 }
1756 }
1757 return count;
1758}
1759
1760} // namespace
1761
1762namespace GrTessellator {
1763
1764// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1765
halcanary9d524f22016-03-29 09:03:52 -07001766int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
senorblancof57372d2016-08-31 10:36:19 -07001767 VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
1768 bool canTweakAlphaForCoverage, bool* isLinear) {
Stephen Whitee595bbf2017-02-16 11:27:01 -05001769 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08001770 if (contourCnt <= 0) {
1771 *isLinear = true;
1772 return 0;
1773 }
Stephen Whitee595bbf2017-02-16 11:27:01 -05001774 SkArenaAlloc alloc(kArenaChunkSize);
senorblancof57372d2016-08-31 10:36:19 -07001775 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
1776 isLinear);
senorblanco7ab96e92016-10-12 06:47:44 -07001777 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
ethannicholase9709e82016-01-07 13:34:16 -08001778 int count = count_points(polys, fillType);
1779 if (0 == count) {
1780 return 0;
1781 }
1782
senorblancof57372d2016-08-31 10:36:19 -07001783 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08001784 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08001785 SkDebugf("Could not allocate vertices\n");
1786 return 0;
1787 }
senorblancof57372d2016-08-31 10:36:19 -07001788
1789 LOG("emitting %d verts\n", count);
1790 AAParams aaParams;
1791 aaParams.fTweakAlpha = canTweakAlphaForCoverage;
1792 aaParams.fColor = color;
1793
1794 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
1795 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
1796 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08001797 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08001798 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08001799 return actualCount;
1800}
1801
halcanary9d524f22016-03-29 09:03:52 -07001802int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08001803 GrTessellator::WindingVertex** verts) {
Stephen Whitee595bbf2017-02-16 11:27:01 -05001804 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08001805 if (contourCnt <= 0) {
1806 return 0;
1807 }
Stephen Whitee595bbf2017-02-16 11:27:01 -05001808 SkArenaAlloc alloc(kArenaChunkSize);
ethannicholase9709e82016-01-07 13:34:16 -08001809 bool isLinear;
senorblancof57372d2016-08-31 10:36:19 -07001810 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear);
ethannicholase9709e82016-01-07 13:34:16 -08001811 SkPath::FillType fillType = path.getFillType();
1812 int count = count_points(polys, fillType);
1813 if (0 == count) {
1814 *verts = nullptr;
1815 return 0;
1816 }
1817
1818 *verts = new GrTessellator::WindingVertex[count];
1819 GrTessellator::WindingVertex* vertsEnd = *verts;
1820 SkPoint* points = new SkPoint[count];
1821 SkPoint* pointsEnd = points;
1822 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001823 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08001824 SkPoint* start = pointsEnd;
senorblancof57372d2016-08-31 10:36:19 -07001825 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08001826 while (start != pointsEnd) {
1827 vertsEnd->fPos = *start;
1828 vertsEnd->fWinding = poly->fWinding;
1829 ++start;
1830 ++vertsEnd;
1831 }
1832 }
1833 }
1834 int actualCount = static_cast<int>(vertsEnd - *verts);
1835 SkASSERT(actualCount <= count);
1836 SkASSERT(pointsEnd - points == actualCount);
1837 delete[] points;
1838 return actualCount;
1839}
1840
1841} // namespace