blob: d193b7486cf0f5ce530c14135543a3297d43de68 [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
senorblanco6599eff2016-03-10 08:38:45 -080013#include "SkChunkAlloc.h"
14#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
93#define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type args
94
95namespace {
96
97struct 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) {
220#if TESSELLATOR_WIREFRAME
221 data = emit_vertex(v0, aaParams, data);
222 data = emit_vertex(v1, aaParams, data);
223 data = emit_vertex(v1, aaParams, data);
224 data = emit_vertex(v2, aaParams, data);
225 data = emit_vertex(v2, aaParams, data);
226 data = emit_vertex(v0, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800227#else
senorblancof57372d2016-08-31 10:36:19 -0700228 data = emit_vertex(v0, aaParams, data);
229 data = emit_vertex(v1, aaParams, data);
230 data = emit_vertex(v2, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800231#endif
232 return data;
233}
234
senorblancoe6eaa322016-03-08 09:06:44 -0800235struct VertexList {
236 VertexList() : fHead(nullptr), fTail(nullptr) {}
237 Vertex* fHead;
238 Vertex* fTail;
239 void insert(Vertex* v, Vertex* prev, Vertex* next) {
240 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
241 }
242 void append(Vertex* v) {
243 insert(v, fTail, nullptr);
244 }
245 void prepend(Vertex* v) {
246 insert(v, nullptr, fHead);
247 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500248 void remove(Vertex* v) {
249 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
250 }
senorblancof57372d2016-08-31 10:36:19 -0700251 void close() {
252 if (fHead && fTail) {
253 fTail->fNext = fHead;
254 fHead->fPrev = fTail;
255 }
256 }
senorblancoe6eaa322016-03-08 09:06:44 -0800257};
258
senorblancof57372d2016-08-31 10:36:19 -0700259// Round to nearest quarter-pixel. This is used for screenspace tessellation.
260
261inline void round(SkPoint* p) {
262 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
263 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
264}
265
senorblanco49df8d12016-10-07 08:36:56 -0700266// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
267struct Line {
268 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
269 Line(const SkPoint& p, const SkPoint& q)
270 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
271 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
272 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
273 static_cast<double>(p.fX) * q.fY) {}
274 double dist(const SkPoint& p) const {
275 return fA * p.fX + fB * p.fY + fC;
276 }
277 double magSq() const {
278 return fA * fA + fB * fB;
279 }
280
281 // Compute the intersection of two (infinite) Lines.
282 bool intersect(const Line& other, SkPoint* point) {
283 double denom = fA * other.fB - fB * other.fA;
284 if (denom == 0.0) {
285 return false;
286 }
287 double scale = 1.0f / denom;
288 point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale);
289 point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale);
290 round(point);
291 return true;
292 }
293 double fA, fB, fC;
294};
295
ethannicholase9709e82016-01-07 13:34:16 -0800296/**
297 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
298 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
299 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
300 * point). For speed, that case is only tested by the callers which require it (e.g.,
301 * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
302 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
303 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
304 * a lot faster in the "not found" case.
305 *
306 * The coefficients of the line equation stored in double precision to avoid catastrphic
307 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
308 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
309 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
310 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
311 * this file).
312 */
313
314struct Edge {
Stephen White2f4686f2017-01-03 16:20:01 -0500315 enum class Type { kInner, kOuter, kConnector };
316 Edge(Vertex* top, Vertex* bottom, int winding, Type type)
ethannicholase9709e82016-01-07 13:34:16 -0800317 : fWinding(winding)
318 , fTop(top)
319 , fBottom(bottom)
Stephen White2f4686f2017-01-03 16:20:01 -0500320 , fType(type)
ethannicholase9709e82016-01-07 13:34:16 -0800321 , fLeft(nullptr)
322 , fRight(nullptr)
323 , fPrevEdgeAbove(nullptr)
324 , fNextEdgeAbove(nullptr)
325 , fPrevEdgeBelow(nullptr)
326 , fNextEdgeBelow(nullptr)
327 , fLeftPoly(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700328 , fRightPoly(nullptr)
329 , fLeftPolyPrev(nullptr)
330 , fLeftPolyNext(nullptr)
331 , fRightPolyPrev(nullptr)
senorblanco70f52512016-08-17 14:56:22 -0700332 , fRightPolyNext(nullptr)
333 , fUsedInLeftPoly(false)
senorblanco49df8d12016-10-07 08:36:56 -0700334 , fUsedInRightPoly(false)
335 , fLine(top, bottom) {
ethannicholase9709e82016-01-07 13:34:16 -0800336 }
337 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
338 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
339 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
Stephen White2f4686f2017-01-03 16:20:01 -0500340 Type fType;
ethannicholase9709e82016-01-07 13:34:16 -0800341 Edge* fLeft; // The linked list of edges in the active edge list.
342 Edge* fRight; // "
343 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
344 Edge* fNextEdgeAbove; // "
345 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
346 Edge* fNextEdgeBelow; // "
347 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
348 Poly* fRightPoly; // The Poly to the right of this edge, if any.
senorblanco531237e2016-06-02 11:36:48 -0700349 Edge* fLeftPolyPrev;
350 Edge* fLeftPolyNext;
351 Edge* fRightPolyPrev;
352 Edge* fRightPolyNext;
senorblanco70f52512016-08-17 14:56:22 -0700353 bool fUsedInLeftPoly;
354 bool fUsedInRightPoly;
senorblanco49df8d12016-10-07 08:36:56 -0700355 Line fLine;
ethannicholase9709e82016-01-07 13:34:16 -0800356 double dist(const SkPoint& p) const {
senorblanco49df8d12016-10-07 08:36:56 -0700357 return fLine.dist(p);
ethannicholase9709e82016-01-07 13:34:16 -0800358 }
359 bool isRightOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700360 return fLine.dist(v->fPoint) < 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800361 }
362 bool isLeftOf(Vertex* v) const {
senorblanco49df8d12016-10-07 08:36:56 -0700363 return fLine.dist(v->fPoint) > 0.0;
ethannicholase9709e82016-01-07 13:34:16 -0800364 }
365 void recompute() {
senorblanco49df8d12016-10-07 08:36:56 -0700366 fLine = Line(fTop, fBottom);
ethannicholase9709e82016-01-07 13:34:16 -0800367 }
368 bool intersect(const Edge& other, SkPoint* p) {
369 LOG("intersecting %g -> %g with %g -> %g\n",
370 fTop->fID, fBottom->fID,
371 other.fTop->fID, other.fBottom->fID);
372 if (fTop == other.fTop || fBottom == other.fBottom) {
373 return false;
374 }
senorblanco49df8d12016-10-07 08:36:56 -0700375 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800376 if (denom == 0.0) {
377 return false;
378 }
379 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
380 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
senorblanco49df8d12016-10-07 08:36:56 -0700381 double sNumer = -dy * other.fLine.fB - dx * other.fLine.fA;
382 double tNumer = -dy * fLine.fB - dx * fLine.fA;
ethannicholase9709e82016-01-07 13:34:16 -0800383 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
384 // This saves us doing the divide below unless absolutely necessary.
385 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
386 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
387 return false;
388 }
389 double s = sNumer / denom;
390 SkASSERT(s >= 0.0 && s <= 1.0);
senorblanco49df8d12016-10-07 08:36:56 -0700391 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
392 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
ethannicholase9709e82016-01-07 13:34:16 -0800393 return true;
394 }
senorblancof57372d2016-08-31 10:36:19 -0700395};
396
397struct EdgeList {
398 EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr), fCount(0) {}
399 Edge* fHead;
400 Edge* fTail;
401 EdgeList* fNext;
402 int fCount;
403 void insert(Edge* edge, Edge* prev, Edge* next) {
404 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
405 fCount++;
406 }
407 void append(Edge* e) {
408 insert(e, fTail, nullptr);
409 }
410 void remove(Edge* edge) {
411 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
412 fCount--;
413 }
414 void close() {
415 if (fHead && fTail) {
416 fTail->fRight = fHead;
417 fHead->fLeft = fTail;
418 }
419 }
420 bool contains(Edge* edge) const {
421 return edge->fLeft || edge->fRight || fHead == edge;
ethannicholase9709e82016-01-07 13:34:16 -0800422 }
423};
424
425/***************************************************************************************/
426
427struct Poly {
senorblanco531237e2016-06-02 11:36:48 -0700428 Poly(Vertex* v, int winding)
429 : fFirstVertex(v)
430 , fWinding(winding)
ethannicholase9709e82016-01-07 13:34:16 -0800431 , fHead(nullptr)
432 , fTail(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800433 , fNext(nullptr)
434 , fPartner(nullptr)
435 , fCount(0)
436 {
437#if LOGGING_ENABLED
438 static int gID = 0;
439 fID = gID++;
440 LOG("*** created Poly %d\n", fID);
441#endif
442 }
senorblanco531237e2016-06-02 11:36:48 -0700443 typedef enum { kLeft_Side, kRight_Side } Side;
ethannicholase9709e82016-01-07 13:34:16 -0800444 struct MonotonePoly {
senorblanco531237e2016-06-02 11:36:48 -0700445 MonotonePoly(Edge* edge, Side side)
446 : fSide(side)
447 , fFirstEdge(nullptr)
448 , fLastEdge(nullptr)
ethannicholase9709e82016-01-07 13:34:16 -0800449 , fPrev(nullptr)
senorblanco531237e2016-06-02 11:36:48 -0700450 , fNext(nullptr) {
451 this->addEdge(edge);
452 }
ethannicholase9709e82016-01-07 13:34:16 -0800453 Side fSide;
senorblanco531237e2016-06-02 11:36:48 -0700454 Edge* fFirstEdge;
455 Edge* fLastEdge;
ethannicholase9709e82016-01-07 13:34:16 -0800456 MonotonePoly* fPrev;
457 MonotonePoly* fNext;
senorblanco531237e2016-06-02 11:36:48 -0700458 void addEdge(Edge* edge) {
senorblancoe6eaa322016-03-08 09:06:44 -0800459 if (fSide == kRight_Side) {
senorblanco212c7c32016-08-18 10:20:47 -0700460 SkASSERT(!edge->fUsedInRightPoly);
senorblanco531237e2016-06-02 11:36:48 -0700461 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
462 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700463 edge->fUsedInRightPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800464 } else {
senorblanco212c7c32016-08-18 10:20:47 -0700465 SkASSERT(!edge->fUsedInLeftPoly);
senorblanco531237e2016-06-02 11:36:48 -0700466 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
467 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
senorblanco70f52512016-08-17 14:56:22 -0700468 edge->fUsedInLeftPoly = true;
ethannicholase9709e82016-01-07 13:34:16 -0800469 }
ethannicholase9709e82016-01-07 13:34:16 -0800470 }
471
senorblancof57372d2016-08-31 10:36:19 -0700472 void* emit(const AAParams* aaParams, void* data) {
senorblanco531237e2016-06-02 11:36:48 -0700473 Edge* e = fFirstEdge;
474 e->fTop->fPrev = e->fTop->fNext = nullptr;
475 VertexList vertices;
476 vertices.append(e->fTop);
477 while (e != nullptr) {
478 e->fBottom->fPrev = e->fBottom->fNext = nullptr;
479 if (kRight_Side == fSide) {
480 vertices.append(e->fBottom);
481 e = e->fRightPolyNext;
482 } else {
483 vertices.prepend(e->fBottom);
484 e = e->fLeftPolyNext;
485 }
486 }
487 Vertex* first = vertices.fHead;
ethannicholase9709e82016-01-07 13:34:16 -0800488 Vertex* v = first->fNext;
senorblanco531237e2016-06-02 11:36:48 -0700489 while (v != vertices.fTail) {
ethannicholase9709e82016-01-07 13:34:16 -0800490 SkASSERT(v && v->fPrev && v->fNext);
491 Vertex* prev = v->fPrev;
492 Vertex* curr = v;
493 Vertex* next = v->fNext;
494 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
495 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
496 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
497 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
498 if (ax * by - ay * bx >= 0.0) {
senorblancof57372d2016-08-31 10:36:19 -0700499 data = emit_triangle(prev, curr, next, aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800500 v->fPrev->fNext = v->fNext;
501 v->fNext->fPrev = v->fPrev;
502 if (v->fPrev == first) {
503 v = v->fNext;
504 } else {
505 v = v->fPrev;
506 }
507 } else {
508 v = v->fNext;
509 }
510 }
511 return data;
512 }
513 };
senorblanco531237e2016-06-02 11:36:48 -0700514 Poly* addEdge(Edge* e, Side side, SkChunkAlloc& alloc) {
senorblanco70f52512016-08-17 14:56:22 -0700515 LOG("addEdge (%g -> %g) to poly %d, %s side\n",
516 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
ethannicholase9709e82016-01-07 13:34:16 -0800517 Poly* partner = fPartner;
518 Poly* poly = this;
senorblanco212c7c32016-08-18 10:20:47 -0700519 if (side == kRight_Side) {
520 if (e->fUsedInRightPoly) {
521 return this;
522 }
523 } else {
524 if (e->fUsedInLeftPoly) {
525 return this;
526 }
527 }
ethannicholase9709e82016-01-07 13:34:16 -0800528 if (partner) {
529 fPartner = partner->fPartner = nullptr;
530 }
senorblanco531237e2016-06-02 11:36:48 -0700531 if (!fTail) {
532 fHead = fTail = ALLOC_NEW(MonotonePoly, (e, side), alloc);
533 fCount += 2;
senorblanco93e3fff2016-06-07 12:36:00 -0700534 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
535 return poly;
senorblanco531237e2016-06-02 11:36:48 -0700536 } else if (side == fTail->fSide) {
537 fTail->addEdge(e);
538 fCount++;
539 } else {
Stephen White2f4686f2017-01-03 16:20:01 -0500540 e = ALLOC_NEW(Edge, (fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner),
541 alloc);
senorblanco531237e2016-06-02 11:36:48 -0700542 fTail->addEdge(e);
543 fCount++;
ethannicholase9709e82016-01-07 13:34:16 -0800544 if (partner) {
senorblanco531237e2016-06-02 11:36:48 -0700545 partner->addEdge(e, side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800546 poly = partner;
547 } else {
senorblanco531237e2016-06-02 11:36:48 -0700548 MonotonePoly* m = ALLOC_NEW(MonotonePoly, (e, side), alloc);
549 m->fPrev = fTail;
550 fTail->fNext = m;
551 fTail = m;
ethannicholase9709e82016-01-07 13:34:16 -0800552 }
553 }
ethannicholase9709e82016-01-07 13:34:16 -0800554 return poly;
555 }
senorblancof57372d2016-08-31 10:36:19 -0700556 void* emit(const AAParams* aaParams, void *data) {
ethannicholase9709e82016-01-07 13:34:16 -0800557 if (fCount < 3) {
558 return data;
559 }
560 LOG("emit() %d, size %d\n", fID, fCount);
561 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
senorblancof57372d2016-08-31 10:36:19 -0700562 data = m->emit(aaParams, data);
ethannicholase9709e82016-01-07 13:34:16 -0800563 }
564 return data;
565 }
senorblanco531237e2016-06-02 11:36:48 -0700566 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
567 Vertex* fFirstVertex;
ethannicholase9709e82016-01-07 13:34:16 -0800568 int fWinding;
569 MonotonePoly* fHead;
570 MonotonePoly* fTail;
ethannicholase9709e82016-01-07 13:34:16 -0800571 Poly* fNext;
572 Poly* fPartner;
573 int fCount;
574#if LOGGING_ENABLED
575 int fID;
576#endif
577};
578
579/***************************************************************************************/
580
581bool coincident(const SkPoint& a, const SkPoint& b) {
582 return a == b;
583}
584
585Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
senorblanco531237e2016-06-02 11:36:48 -0700586 Poly* poly = ALLOC_NEW(Poly, (v, winding), alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800587 poly->fNext = *head;
588 *head = poly;
589 return poly;
590}
591
senorblancof57372d2016-08-31 10:36:19 -0700592EdgeList* new_contour(EdgeList** head, SkChunkAlloc& alloc) {
593 EdgeList* contour = ALLOC_NEW(EdgeList, (), alloc);
594 contour->fNext = *head;
595 *head = contour;
596 return contour;
597}
598
ethannicholase9709e82016-01-07 13:34:16 -0800599Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
600 SkChunkAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -0700601 Vertex* v = ALLOC_NEW(Vertex, (p, 255), alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800602#if LOGGING_ENABLED
603 static float gID = 0.0f;
604 v->fID = gID++;
605#endif
606 if (prev) {
607 prev->fNext = v;
608 v->fPrev = prev;
609 } else {
610 *head = v;
611 }
612 return v;
613}
614
615Vertex* generate_quadratic_points(const SkPoint& p0,
616 const SkPoint& p1,
617 const SkPoint& p2,
618 SkScalar tolSqd,
619 Vertex* prev,
620 Vertex** head,
621 int pointsLeft,
622 SkChunkAlloc& alloc) {
623 SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
624 if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
625 return append_point_to_contour(p2, prev, head, alloc);
626 }
627
628 const SkPoint q[] = {
629 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
630 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
631 };
632 const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
633
634 pointsLeft >>= 1;
635 prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
636 prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
637 return prev;
638}
639
640Vertex* generate_cubic_points(const SkPoint& p0,
641 const SkPoint& p1,
642 const SkPoint& p2,
643 const SkPoint& p3,
644 SkScalar tolSqd,
645 Vertex* prev,
646 Vertex** head,
647 int pointsLeft,
648 SkChunkAlloc& alloc) {
649 SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
650 SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
651 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
652 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
653 return append_point_to_contour(p3, prev, head, alloc);
654 }
655 const SkPoint q[] = {
656 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
657 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
658 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
659 };
660 const SkPoint r[] = {
661 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
662 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
663 };
664 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
665 pointsLeft >>= 1;
666 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
667 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
668 return prev;
669}
670
671// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
672
673void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
674 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) {
675 SkScalar toleranceSqd = tolerance * tolerance;
676
677 SkPoint pts[4];
678 bool done = false;
679 *isLinear = true;
680 SkPath::Iter iter(path, false);
681 Vertex* prev = nullptr;
682 Vertex* head = nullptr;
683 if (path.isInverseFillType()) {
684 SkPoint quad[4];
685 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700686 for (int i = 3; i >= 0; i--) {
ethannicholase9709e82016-01-07 13:34:16 -0800687 prev = append_point_to_contour(quad[i], prev, &head, alloc);
688 }
689 head->fPrev = prev;
690 prev->fNext = head;
691 *contours++ = head;
692 head = prev = nullptr;
693 }
694 SkAutoConicToQuads converter;
695 while (!done) {
696 SkPath::Verb verb = iter.next(pts);
697 switch (verb) {
698 case SkPath::kConic_Verb: {
699 SkScalar weight = iter.conicWeight();
700 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
701 for (int i = 0; i < converter.countQuads(); ++i) {
702 int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
703 prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
704 toleranceSqd, prev, &head, pointsLeft, alloc);
705 quadPts += 2;
706 }
707 *isLinear = false;
708 break;
709 }
710 case SkPath::kMove_Verb:
711 if (head) {
712 head->fPrev = prev;
713 prev->fNext = head;
714 *contours++ = head;
715 }
716 head = prev = nullptr;
717 prev = append_point_to_contour(pts[0], prev, &head, alloc);
718 break;
719 case SkPath::kLine_Verb: {
720 prev = append_point_to_contour(pts[1], prev, &head, alloc);
721 break;
722 }
723 case SkPath::kQuad_Verb: {
724 int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
725 prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
726 &head, pointsLeft, alloc);
727 *isLinear = false;
728 break;
729 }
730 case SkPath::kCubic_Verb: {
731 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
732 prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
733 toleranceSqd, prev, &head, pointsLeft, alloc);
734 *isLinear = false;
735 break;
736 }
737 case SkPath::kClose_Verb:
738 if (head) {
739 head->fPrev = prev;
740 prev->fNext = head;
741 *contours++ = head;
742 }
743 head = prev = nullptr;
744 break;
745 case SkPath::kDone_Verb:
746 if (head) {
747 head->fPrev = prev;
748 prev->fNext = head;
749 *contours++ = head;
750 }
751 done = true;
752 break;
753 }
754 }
755}
756
senorblancof57372d2016-08-31 10:36:19 -0700757inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
758 if (!poly) {
759 return false;
760 }
761 int winding = poly->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800762 switch (fillType) {
763 case SkPath::kWinding_FillType:
764 return winding != 0;
765 case SkPath::kEvenOdd_FillType:
766 return (winding & 1) != 0;
767 case SkPath::kInverseWinding_FillType:
senorblanco7ab96e92016-10-12 06:47:44 -0700768 return winding == 1;
ethannicholase9709e82016-01-07 13:34:16 -0800769 case SkPath::kInverseEvenOdd_FillType:
770 return (winding & 1) == 1;
771 default:
772 SkASSERT(false);
773 return false;
774 }
775}
776
Stephen Whitebf6137e2017-01-04 15:43:26 -0500777Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkChunkAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500778 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800779 Vertex* top = winding < 0 ? next : prev;
780 Vertex* bottom = winding < 0 ? prev : next;
Stephen White2f4686f2017-01-03 16:20:01 -0500781 return ALLOC_NEW(Edge, (top, bottom, winding, type), alloc);
ethannicholase9709e82016-01-07 13:34:16 -0800782}
783
784void remove_edge(Edge* edge, EdgeList* edges) {
785 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700786 SkASSERT(edges->contains(edge));
787 edges->remove(edge);
ethannicholase9709e82016-01-07 13:34:16 -0800788}
789
790void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
791 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblancof57372d2016-08-31 10:36:19 -0700792 SkASSERT(!edges->contains(edge));
ethannicholase9709e82016-01-07 13:34:16 -0800793 Edge* next = prev ? prev->fRight : edges->fHead;
senorblancof57372d2016-08-31 10:36:19 -0700794 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800795}
796
797void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
798 if (v->fFirstEdgeAbove) {
799 *left = v->fFirstEdgeAbove->fLeft;
800 *right = v->fLastEdgeAbove->fRight;
801 return;
802 }
803 Edge* next = nullptr;
804 Edge* prev;
805 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
806 if (prev->isLeftOf(v)) {
807 break;
808 }
809 next = prev;
810 }
811 *left = prev;
812 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800813}
814
815void find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
816 Edge* prev = nullptr;
817 Edge* next;
818 for (next = edges->fHead; next != nullptr; next = next->fRight) {
819 if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
820 (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
821 (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
822 next->isRightOf(edge->fBottom)) ||
823 (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
824 edge->isLeftOf(next->fBottom))) {
825 break;
826 }
827 prev = next;
828 }
829 *left = prev;
830 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800831}
832
833void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
Stephen White2f4686f2017-01-03 16:20:01 -0500834 if (!activeEdges) {
835 return;
836 }
837 if (activeEdges->contains(edge)) {
ethannicholase9709e82016-01-07 13:34:16 -0800838 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
839 remove_edge(edge, activeEdges);
840 }
841 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
842 Edge* left;
843 Edge* right;
844 find_enclosing_edges(edge, activeEdges, c, &left, &right);
845 insert_edge(edge, left, activeEdges);
846 }
847}
848
849void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
850 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
851 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
852 return;
853 }
854 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
855 Edge* prev = nullptr;
856 Edge* next;
857 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
858 if (next->isRightOf(edge->fTop)) {
859 break;
860 }
861 prev = next;
862 }
senorblancoe6eaa322016-03-08 09:06:44 -0800863 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800864 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
865}
866
867void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
868 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
869 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
870 return;
871 }
872 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
873 Edge* prev = nullptr;
874 Edge* next;
875 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
876 if (next->isRightOf(edge->fBottom)) {
877 break;
878 }
879 prev = next;
880 }
senorblancoe6eaa322016-03-08 09:06:44 -0800881 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800882 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
883}
884
885void remove_edge_above(Edge* edge) {
886 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
887 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800888 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800889 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
890}
891
892void remove_edge_below(Edge* edge) {
893 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
894 edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800895 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800896 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
897}
898
Stephen Whitee7a364d2017-01-11 16:19:26 -0500899void disconnect(Edge* edge)
900{
ethannicholase9709e82016-01-07 13:34:16 -0800901 remove_edge_above(edge);
902 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500903}
904
905void erase_edge(Edge* edge, EdgeList* edges) {
906 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
907 disconnect(edge);
senorblancof57372d2016-08-31 10:36:19 -0700908 if (edges && edges->contains(edge)) {
ethannicholase9709e82016-01-07 13:34:16 -0800909 remove_edge(edge, edges);
910 }
911}
912
913void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
914
915void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
916 remove_edge_below(edge);
917 edge->fTop = v;
918 edge->recompute();
919 insert_edge_below(edge, v, c);
920 fix_active_state(edge, activeEdges, c);
921 merge_collinear_edges(edge, activeEdges, c);
922}
923
924void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
925 remove_edge_above(edge);
926 edge->fBottom = v;
927 edge->recompute();
928 insert_edge_above(edge, v, c);
929 fix_active_state(edge, activeEdges, c);
930 merge_collinear_edges(edge, activeEdges, c);
931}
932
933void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
934 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
935 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
936 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
937 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
938 other->fWinding += edge->fWinding;
Stephen Whitee7a364d2017-01-11 16:19:26 -0500939 erase_edge(edge, activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -0800940 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
941 other->fWinding += edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800942 set_bottom(edge, other->fTop, activeEdges, c);
943 } else {
944 edge->fWinding += other->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800945 set_bottom(other, edge->fTop, activeEdges, c);
946 }
947}
948
949void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
950 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
951 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
952 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
953 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
954 other->fWinding += edge->fWinding;
Stephen Whitee7a364d2017-01-11 16:19:26 -0500955 erase_edge(edge, activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -0800956 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
957 edge->fWinding += other->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800958 set_top(other, edge->fBottom, activeEdges, c);
959 } else {
960 other->fWinding += edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800961 set_top(edge, other->fBottom, activeEdges, c);
962 }
963}
964
965void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
966 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
967 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
968 merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
969 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
970 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
971 merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
972 }
973 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
974 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
975 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
976 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
977 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
978 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
979 }
980}
981
982void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc);
983
984void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
985 Vertex* top = edge->fTop;
986 Vertex* bottom = edge->fBottom;
987 if (edge->fLeft) {
988 Vertex* leftTop = edge->fLeft->fTop;
989 Vertex* leftBottom = edge->fLeft->fBottom;
990 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
991 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
992 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
993 split_edge(edge, leftTop, activeEdges, c, alloc);
994 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
995 !edge->fLeft->isLeftOf(bottom)) {
996 split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
997 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
998 split_edge(edge, leftBottom, activeEdges, c, alloc);
999 }
1000 }
1001 if (edge->fRight) {
1002 Vertex* rightTop = edge->fRight->fTop;
1003 Vertex* rightBottom = edge->fRight->fBottom;
1004 if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
1005 split_edge(edge->fRight, top, activeEdges, c, alloc);
1006 } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
1007 split_edge(edge, rightTop, activeEdges, c, alloc);
1008 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1009 !edge->fRight->isRightOf(bottom)) {
1010 split_edge(edge->fRight, bottom, activeEdges, c, alloc);
1011 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1012 !edge->isLeftOf(rightBottom)) {
1013 split_edge(edge, rightBottom, activeEdges, c, alloc);
1014 }
1015 }
1016}
1017
1018void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
1019 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1020 edge->fTop->fID, edge->fBottom->fID,
1021 v->fID, v->fPoint.fX, v->fPoint.fY);
1022 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
1023 set_top(edge, v, activeEdges, c);
1024 } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
1025 set_bottom(edge, v, activeEdges, c);
1026 } else {
Stephen White2f4686f2017-01-03 16:20:01 -05001027 Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding, edge->fType), alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001028 insert_edge_below(newEdge, v, c);
1029 insert_edge_above(newEdge, edge->fBottom, c);
1030 set_bottom(edge, v, activeEdges, c);
1031 cleanup_active_edges(edge, activeEdges, c, alloc);
1032 fix_active_state(newEdge, activeEdges, c);
1033 merge_collinear_edges(newEdge, activeEdges, c);
1034 }
1035}
1036
Stephen Whitebf6137e2017-01-04 15:43:26 -05001037Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkChunkAlloc& alloc) {
1038 Edge* edge = new_edge(prev, next, type, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001039 if (edge->fWinding > 0) {
1040 insert_edge_below(edge, prev, c);
1041 insert_edge_above(edge, next, c);
1042 } else {
1043 insert_edge_below(edge, next, c);
1044 insert_edge_above(edge, prev, c);
1045 }
1046 merge_collinear_edges(edge, nullptr, c);
1047 return edge;
1048}
1049
Stephen Whitebf6137e2017-01-04 15:43:26 -05001050void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
1051 SkChunkAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001052 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1053 src->fID, dst->fID);
senorblancof57372d2016-08-31 10:36:19 -07001054 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001055 for (Edge* edge = src->fFirstEdgeAbove; edge;) {
1056 Edge* next = edge->fNextEdgeAbove;
1057 set_bottom(edge, dst, nullptr, c);
1058 edge = next;
1059 }
1060 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
1061 Edge* next = edge->fNextEdgeBelow;
1062 set_top(edge, dst, nullptr, c);
1063 edge = next;
1064 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001065 mesh->remove(src);
ethannicholase9709e82016-01-07 13:34:16 -08001066}
1067
senorblancof57372d2016-08-31 10:36:19 -07001068uint8_t max_edge_alpha(Edge* a, Edge* b) {
Stephen White2f4686f2017-01-03 16:20:01 -05001069 if (a->fType == Edge::Type::kInner && b->fType == Edge::Type::kInner) {
1070 return 255;
1071 } else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) {
1072 return 0;
1073 } else {
1074 return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
1075 SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
1076 }
senorblancof57372d2016-08-31 10:36:19 -07001077}
1078
ethannicholase9709e82016-01-07 13:34:16 -08001079Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
1080 SkChunkAlloc& alloc) {
1081 SkPoint p;
1082 if (!edge || !other) {
1083 return nullptr;
1084 }
1085 if (edge->intersect(*other, &p)) {
1086 Vertex* v;
1087 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
1088 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
1089 split_edge(other, edge->fTop, activeEdges, c, alloc);
1090 v = edge->fTop;
1091 } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) {
1092 split_edge(other, edge->fBottom, activeEdges, c, alloc);
1093 v = edge->fBottom;
1094 } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
1095 split_edge(edge, other->fTop, activeEdges, c, alloc);
1096 v = other->fTop;
1097 } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) {
1098 split_edge(edge, other->fBottom, activeEdges, c, alloc);
1099 v = other->fBottom;
1100 } else {
1101 Vertex* nextV = edge->fTop;
1102 while (c.sweep_lt(p, nextV->fPoint)) {
1103 nextV = nextV->fPrev;
1104 }
1105 while (c.sweep_lt(nextV->fPoint, p)) {
1106 nextV = nextV->fNext;
1107 }
1108 Vertex* prevV = nextV->fPrev;
1109 if (coincident(prevV->fPoint, p)) {
1110 v = prevV;
1111 } else if (coincident(nextV->fPoint, p)) {
1112 v = nextV;
1113 } else {
senorblancof57372d2016-08-31 10:36:19 -07001114 uint8_t alpha = max_edge_alpha(edge, other);
1115 v = ALLOC_NEW(Vertex, (p, alpha), alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001116 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
1117 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
1118 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
1119#if LOGGING_ENABLED
1120 v->fID = (nextV->fID + prevV->fID) * 0.5f;
1121#endif
1122 v->fPrev = prevV;
1123 v->fNext = nextV;
1124 prevV->fNext = v;
1125 nextV->fPrev = v;
1126 }
1127 split_edge(edge, v, activeEdges, c, alloc);
1128 split_edge(other, v, activeEdges, c, alloc);
1129 }
1130 return v;
1131 }
1132 return nullptr;
1133}
1134
senorblancof57372d2016-08-31 10:36:19 -07001135void sanitize_contours(Vertex** contours, int contourCnt, bool approximate) {
ethannicholase9709e82016-01-07 13:34:16 -08001136 for (int i = 0; i < contourCnt; ++i) {
1137 SkASSERT(contours[i]);
1138 for (Vertex* v = contours[i];;) {
senorblancof57372d2016-08-31 10:36:19 -07001139 if (approximate) {
1140 round(&v->fPoint);
1141 }
ethannicholase9709e82016-01-07 13:34:16 -08001142 if (coincident(v->fPrev->fPoint, v->fPoint)) {
1143 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1144 if (v->fPrev == v) {
1145 contours[i] = nullptr;
1146 break;
1147 }
1148 v->fPrev->fNext = v->fNext;
1149 v->fNext->fPrev = v->fPrev;
1150 if (contours[i] == v) {
1151 contours[i] = v->fNext;
1152 }
1153 v = v->fPrev;
1154 } else {
1155 v = v->fNext;
1156 if (v == contours[i]) break;
1157 }
1158 }
1159 }
1160}
1161
Stephen Whitebf6137e2017-01-04 15:43:26 -05001162void merge_coincident_vertices(VertexList* mesh, Comparator& c, SkChunkAlloc& alloc) {
1163 for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001164 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1165 v->fPoint = v->fPrev->fPoint;
1166 }
1167 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001168 merge_vertices(v->fPrev, v, mesh, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001169 }
1170 }
1171}
1172
1173// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1174
Stephen Whitebf6137e2017-01-04 15:43:26 -05001175void build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c,
1176 SkChunkAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001177 Vertex* prev = nullptr;
1178 for (int i = 0; i < contourCnt; ++i) {
1179 for (Vertex* v = contours[i]; v != nullptr;) {
1180 Vertex* vNext = v->fNext;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001181 connect(v->fPrev, v, Edge::Type::kInner, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001182 if (prev) {
1183 prev->fNext = v;
1184 v->fPrev = prev;
1185 } else {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001186 mesh->fHead = v;
ethannicholase9709e82016-01-07 13:34:16 -08001187 }
1188 prev = v;
1189 v = vNext;
1190 if (v == contours[i]) break;
1191 }
1192 }
1193 if (prev) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001194 prev->fNext = mesh->fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001195 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001196 mesh->fTail = prev;
ethannicholase9709e82016-01-07 13:34:16 -08001197}
1198
1199// Stage 3: sort the vertices by increasing sweep direction.
1200
Stephen Whitebf6137e2017-01-04 15:43:26 -05001201void sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c);
ethannicholase9709e82016-01-07 13:34:16 -08001202
Stephen Whitebf6137e2017-01-04 15:43:26 -05001203void front_back_split(VertexList* v, VertexList* front, VertexList* back) {
ethannicholase9709e82016-01-07 13:34:16 -08001204 Vertex* fast;
1205 Vertex* slow;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001206 if (!v->fHead || !v->fHead->fNext) {
1207 *front = *v;
ethannicholase9709e82016-01-07 13:34:16 -08001208 } else {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001209 slow = v->fHead;
1210 fast = v->fHead->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001211
1212 while (fast != nullptr) {
1213 fast = fast->fNext;
1214 if (fast != nullptr) {
1215 slow = slow->fNext;
1216 fast = fast->fNext;
1217 }
1218 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001219 front->fHead = v->fHead;
1220 front->fTail = slow;
1221 back->fHead = slow->fNext;
1222 back->fTail = v->fTail;
ethannicholase9709e82016-01-07 13:34:16 -08001223 slow->fNext->fPrev = nullptr;
1224 slow->fNext = nullptr;
1225 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001226 v->fHead = v->fTail = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001227}
1228
Stephen Whitebf6137e2017-01-04 15:43:26 -05001229void merge_sort(VertexList* mesh, Comparator& c) {
1230 if (!mesh->fHead || !mesh->fHead->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001231 return;
1232 }
1233
Stephen Whitebf6137e2017-01-04 15:43:26 -05001234 VertexList a;
1235 VertexList b;
1236 front_back_split(mesh, &a, &b);
ethannicholase9709e82016-01-07 13:34:16 -08001237
1238 merge_sort(&a, c);
1239 merge_sort(&b, c);
1240
Stephen Whitebf6137e2017-01-04 15:43:26 -05001241 sorted_merge(a.fHead, b.fHead, mesh, c);
ethannicholase9709e82016-01-07 13:34:16 -08001242}
1243
Stephen Whitebf6137e2017-01-04 15:43:26 -05001244void sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c) {
senorblancoe6eaa322016-03-08 09:06:44 -08001245 VertexList vertices;
ethannicholase9709e82016-01-07 13:34:16 -08001246 while (a && b) {
1247 if (c.sweep_lt(a->fPoint, b->fPoint)) {
1248 Vertex* next = a->fNext;
senorblancoe6eaa322016-03-08 09:06:44 -08001249 vertices.append(a);
ethannicholase9709e82016-01-07 13:34:16 -08001250 a = next;
1251 } else {
1252 Vertex* next = b->fNext;
senorblancoe6eaa322016-03-08 09:06:44 -08001253 vertices.append(b);
ethannicholase9709e82016-01-07 13:34:16 -08001254 b = next;
1255 }
1256 }
1257 if (a) {
senorblancoe6eaa322016-03-08 09:06:44 -08001258 vertices.insert(a, vertices.fTail, a->fNext);
ethannicholase9709e82016-01-07 13:34:16 -08001259 }
1260 if (b) {
senorblancoe6eaa322016-03-08 09:06:44 -08001261 vertices.insert(b, vertices.fTail, b->fNext);
ethannicholase9709e82016-01-07 13:34:16 -08001262 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001263 *result = vertices;
ethannicholase9709e82016-01-07 13:34:16 -08001264}
1265
1266// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1267
Stephen Whitebf6137e2017-01-04 15:43:26 -05001268void simplify(const VertexList& vertices, Comparator& c, SkChunkAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001269 LOG("simplifying complex polygons\n");
1270 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001271 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001272 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1273 continue;
1274 }
1275#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001276 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001277#endif
1278 Edge* leftEnclosingEdge = nullptr;
1279 Edge* rightEnclosingEdge = nullptr;
1280 bool restartChecks;
1281 do {
1282 restartChecks = false;
1283 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1284 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001285 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
ethannicholase9709e82016-01-07 13:34:16 -08001286 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
1287 restartChecks = true;
1288 break;
1289 }
1290 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
1291 restartChecks = true;
1292 break;
1293 }
1294 }
1295 } else {
1296 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1297 &activeEdges, c, alloc)) {
1298 if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1299 v = pv;
1300 }
1301 restartChecks = true;
1302 }
1303
1304 }
1305 } while (restartChecks);
senorblancof57372d2016-08-31 10:36:19 -07001306 if (v->fAlpha == 0) {
Stephen White2f4686f2017-01-03 16:20:01 -05001307 if ((leftEnclosingEdge && leftEnclosingEdge->fWinding > 0) &&
1308 (rightEnclosingEdge && rightEnclosingEdge->fWinding < 0)) {
senorblancof57372d2016-08-31 10:36:19 -07001309 v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge);
1310 }
1311 }
ethannicholase9709e82016-01-07 13:34:16 -08001312 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1313 remove_edge(e, &activeEdges);
1314 }
1315 Edge* leftEdge = leftEnclosingEdge;
1316 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1317 insert_edge(e, leftEdge, &activeEdges);
1318 leftEdge = e;
1319 }
1320 v->fProcessed = true;
1321 }
1322}
1323
1324// Stage 5: Tessellate the simplified mesh into monotone polygons.
1325
Stephen Whitebf6137e2017-01-04 15:43:26 -05001326Poly* tessellate(const VertexList& vertices, SkChunkAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001327 LOG("tessellating simple polygons\n");
1328 EdgeList activeEdges;
1329 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001330 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001331 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1332 continue;
1333 }
1334#if LOGGING_ENABLED
senorblancof57372d2016-08-31 10:36:19 -07001335 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001336#endif
1337 Edge* leftEnclosingEdge = nullptr;
1338 Edge* rightEnclosingEdge = nullptr;
1339 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1340 Poly* leftPoly = nullptr;
1341 Poly* rightPoly = nullptr;
1342 if (v->fFirstEdgeAbove) {
1343 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1344 rightPoly = v->fLastEdgeAbove->fRightPoly;
1345 } else {
1346 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1347 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1348 }
1349#if LOGGING_ENABLED
1350 LOG("edges above:\n");
1351 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1352 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1353 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1354 }
1355 LOG("edges below:\n");
1356 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1357 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1358 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1359 }
1360#endif
1361 if (v->fFirstEdgeAbove) {
1362 if (leftPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001363 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001364 }
1365 if (rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001366 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001367 }
1368 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1369 Edge* leftEdge = e;
1370 Edge* rightEdge = e->fNextEdgeAbove;
1371 SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
1372 remove_edge(leftEdge, &activeEdges);
1373 if (leftEdge->fRightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001374 leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001375 }
senorblanco531237e2016-06-02 11:36:48 -07001376 if (rightEdge->fLeftPoly) {
1377 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001378 }
1379 }
1380 remove_edge(v->fLastEdgeAbove, &activeEdges);
1381 if (!v->fFirstEdgeBelow) {
1382 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1383 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1384 rightPoly->fPartner = leftPoly;
1385 leftPoly->fPartner = rightPoly;
1386 }
1387 }
1388 }
1389 if (v->fFirstEdgeBelow) {
1390 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001391 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001392 if (leftPoly == rightPoly) {
1393 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1394 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1395 leftPoly->fWinding, alloc);
1396 leftEnclosingEdge->fRightPoly = leftPoly;
1397 } else {
1398 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1399 rightPoly->fWinding, alloc);
1400 rightEnclosingEdge->fLeftPoly = rightPoly;
1401 }
ethannicholase9709e82016-01-07 13:34:16 -08001402 }
Stephen White2f4686f2017-01-03 16:20:01 -05001403 Edge* join = ALLOC_NEW(Edge,
1404 (leftPoly->lastVertex(), v, 1, Edge::Type::kInner), alloc);
senorblanco531237e2016-06-02 11:36:48 -07001405 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1406 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001407 }
1408 }
1409 Edge* leftEdge = v->fFirstEdgeBelow;
1410 leftEdge->fLeftPoly = leftPoly;
1411 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1412 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1413 rightEdge = rightEdge->fNextEdgeBelow) {
1414 insert_edge(rightEdge, leftEdge, &activeEdges);
1415 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1416 winding += leftEdge->fWinding;
1417 if (winding != 0) {
1418 Poly* poly = new_poly(&polys, v, winding, alloc);
1419 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1420 }
1421 leftEdge = rightEdge;
1422 }
1423 v->fLastEdgeBelow->fRightPoly = rightPoly;
1424 }
1425#if LOGGING_ENABLED
1426 LOG("\nactive edges:\n");
1427 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1428 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1429 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1430 }
1431#endif
1432 }
1433 return polys;
1434}
1435
senorblancof57372d2016-08-31 10:36:19 -07001436bool is_boundary_edge(Edge* edge, SkPath::FillType fillType) {
1437 return apply_fill_type(fillType, edge->fLeftPoly) !=
1438 apply_fill_type(fillType, edge->fRightPoly);
1439}
1440
1441bool is_boundary_start(Edge* edge, SkPath::FillType fillType) {
1442 return !apply_fill_type(fillType, edge->fLeftPoly) &&
1443 apply_fill_type(fillType, edge->fRightPoly);
1444}
1445
Stephen Whitebf6137e2017-01-04 15:43:26 -05001446void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
1447 SkChunkAlloc& alloc) {
1448 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001449 for (Edge* e = v->fFirstEdgeBelow; e != nullptr;) {
1450 Edge* next = e->fNextEdgeBelow;
1451 if (!is_boundary_edge(e, fillType)) {
Stephen Whitee7a364d2017-01-11 16:19:26 -05001452 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001453 }
1454 e = next;
1455 }
1456 }
senorblancof57372d2016-08-31 10:36:19 -07001457}
1458
senorblancof57372d2016-08-31 10:36:19 -07001459void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whiteeaf00792017-01-16 11:47:21 -05001460 normal->setNormalize(SkDoubleToScalar(e->fLine.fA) * e->fWinding,
1461 SkDoubleToScalar(e->fLine.fB) * e->fWinding);
senorblancof57372d2016-08-31 10:36:19 -07001462}
1463
1464// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1465// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1466// invert on stroking.
1467
1468void simplify_boundary(EdgeList* boundary, Comparator& c, SkChunkAlloc& alloc) {
1469 Edge* prevEdge = boundary->fTail;
1470 SkVector prevNormal;
1471 get_edge_normal(prevEdge, &prevNormal);
1472 for (Edge* e = boundary->fHead; e != nullptr;) {
1473 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1474 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1475 double dist = e->dist(prev->fPoint);
1476 SkVector normal;
1477 get_edge_normal(e, &normal);
Stephen Whiteeaf00792017-01-16 11:47:21 -05001478 float denom = 0.0625f * static_cast<float>(e->fLine.magSq());
senorblancof57372d2016-08-31 10:36:19 -07001479 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001480 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001481 insert_edge(join, e, boundary);
1482 remove_edge(prevEdge, boundary);
1483 remove_edge(e, boundary);
1484 if (join->fLeft && join->fRight) {
1485 prevEdge = join->fLeft;
1486 e = join;
1487 } else {
1488 prevEdge = boundary->fTail;
1489 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1490 }
1491 get_edge_normal(prevEdge, &prevNormal);
1492 } else {
1493 prevEdge = e;
1494 prevNormal = normal;
1495 e = e->fRight;
1496 }
1497 }
1498}
1499
1500// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1501// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1502// new antialiased mesh from those vertices.
1503
1504void boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkChunkAlloc& alloc) {
1505 EdgeList outerContour;
1506 Edge* prevEdge = boundary->fTail;
1507 float radius = 0.5f;
senorblanco49df8d12016-10-07 08:36:56 -07001508 double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
1509 Line prevInner(prevEdge->fTop, prevEdge->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001510 prevInner.fC -= offset;
senorblanco49df8d12016-10-07 08:36:56 -07001511 Line prevOuter(prevEdge->fTop, prevEdge->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001512 prevOuter.fC += offset;
1513 VertexList innerVertices;
1514 VertexList outerVertices;
1515 SkScalar innerCount = SK_Scalar1, outerCount = SK_Scalar1;
1516 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
senorblanco49df8d12016-10-07 08:36:56 -07001517 double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding;
1518 Line inner(e->fTop, e->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001519 inner.fC -= offset;
senorblanco49df8d12016-10-07 08:36:56 -07001520 Line outer(e->fTop, e->fBottom);
senorblancof57372d2016-08-31 10:36:19 -07001521 outer.fC += offset;
1522 SkPoint innerPoint, outerPoint;
senorblanco49df8d12016-10-07 08:36:56 -07001523 if (prevInner.intersect(inner, &innerPoint) &&
1524 prevOuter.intersect(outer, &outerPoint)) {
senorblancof57372d2016-08-31 10:36:19 -07001525 Vertex* innerVertex = ALLOC_NEW(Vertex, (innerPoint, 255), alloc);
1526 Vertex* outerVertex = ALLOC_NEW(Vertex, (outerPoint, 0), alloc);
1527 if (innerVertices.fTail && outerVertices.fTail) {
Stephen White2f4686f2017-01-03 16:20:01 -05001528 Edge innerEdge(innerVertices.fTail, innerVertex, 1, Edge::Type::kInner);
1529 Edge outerEdge(outerVertices.fTail, outerVertex, 1, Edge::Type::kInner);
senorblancof57372d2016-08-31 10:36:19 -07001530 SkVector innerNormal;
1531 get_edge_normal(&innerEdge, &innerNormal);
1532 SkVector outerNormal;
1533 get_edge_normal(&outerEdge, &outerNormal);
1534 SkVector normal;
1535 get_edge_normal(prevEdge, &normal);
1536 if (normal.dot(innerNormal) < 0) {
1537 innerPoint += innerVertices.fTail->fPoint * innerCount;
1538 innerCount++;
1539 innerPoint *= SkScalarInvert(innerCount);
1540 innerVertices.fTail->fPoint = innerVertex->fPoint = innerPoint;
1541 } else {
1542 innerCount = SK_Scalar1;
1543 }
1544 if (normal.dot(outerNormal) < 0) {
1545 outerPoint += outerVertices.fTail->fPoint * outerCount;
1546 outerCount++;
1547 outerPoint *= SkScalarInvert(outerCount);
1548 outerVertices.fTail->fPoint = outerVertex->fPoint = outerPoint;
1549 } else {
1550 outerCount = SK_Scalar1;
1551 }
1552 }
1553 innerVertices.append(innerVertex);
1554 outerVertices.append(outerVertex);
1555 prevEdge = e;
1556 }
1557 prevInner = inner;
1558 prevOuter = outer;
1559 }
1560 innerVertices.close();
1561 outerVertices.close();
1562
1563 Vertex* innerVertex = innerVertices.fHead;
1564 Vertex* outerVertex = outerVertices.fHead;
1565 // Alternate clockwise and counterclockwise polys, so the tesselator
1566 // doesn't cancel out the interior edges.
1567 if (!innerVertex || !outerVertex) {
1568 return;
1569 }
1570 do {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001571 connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc);
1572 connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc);
1573 connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc)->fWinding = 0;
senorblancof57372d2016-08-31 10:36:19 -07001574 Vertex* innerNext = innerVertex->fNext;
1575 Vertex* outerNext = outerVertex->fNext;
1576 mesh->append(innerVertex);
1577 mesh->append(outerVertex);
1578 innerVertex = innerNext;
1579 outerVertex = outerNext;
1580 } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead);
1581}
1582
1583void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkChunkAlloc& alloc) {
1584 bool down = is_boundary_start(e, fillType);
1585 while (e) {
1586 e->fWinding = down ? 1 : -1;
1587 Edge* next;
1588 boundary->append(e);
1589 if (down) {
1590 // Find outgoing edge, in clockwise order.
1591 if ((next = e->fNextEdgeAbove)) {
1592 down = false;
1593 } else if ((next = e->fBottom->fLastEdgeBelow)) {
1594 down = true;
1595 } else if ((next = e->fPrevEdgeAbove)) {
1596 down = false;
1597 }
1598 } else {
1599 // Find outgoing edge, in counter-clockwise order.
1600 if ((next = e->fPrevEdgeBelow)) {
1601 down = true;
1602 } else if ((next = e->fTop->fFirstEdgeAbove)) {
1603 down = false;
1604 } else if ((next = e->fNextEdgeBelow)) {
1605 down = true;
1606 }
1607 }
Stephen Whitee7a364d2017-01-11 16:19:26 -05001608 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001609 e = next;
1610 }
1611}
1612
1613// Stage 5b: Extract boundary edges.
1614
Stephen Whitebf6137e2017-01-04 15:43:26 -05001615EdgeList* extract_boundaries(const VertexList& mesh, SkPath::FillType fillType,
1616 SkChunkAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001617 LOG("extracting boundaries\n");
Stephen Whitebf6137e2017-01-04 15:43:26 -05001618 remove_non_boundary_edges(mesh, fillType, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001619 EdgeList* boundaries = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001620 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001621 while (v->fFirstEdgeBelow) {
1622 EdgeList* boundary = new_contour(&boundaries, alloc);
1623 extract_boundary(boundary, v->fFirstEdgeBelow, fillType, alloc);
1624 }
1625 }
1626 return boundaries;
1627}
1628
ethannicholase9709e82016-01-07 13:34:16 -08001629// This is a driver function which calls stages 2-5 in turn.
1630
Stephen Whitebf6137e2017-01-04 15:43:26 -05001631void contours_to_mesh(Vertex** contours, int contourCnt, bool antialias,
1632 VertexList* mesh, Comparator& c, SkChunkAlloc& alloc) {
ethannicholase9709e82016-01-07 13:34:16 -08001633#if LOGGING_ENABLED
1634 for (int i = 0; i < contourCnt; ++i) {
1635 Vertex* v = contours[i];
1636 SkASSERT(v);
1637 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1638 for (v = v->fNext; v != contours[i]; v = v->fNext) {
1639 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1640 }
1641 }
1642#endif
senorblancof57372d2016-08-31 10:36:19 -07001643 sanitize_contours(contours, contourCnt, antialias);
Stephen Whitebf6137e2017-01-04 15:43:26 -05001644 build_edges(contours, contourCnt, mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001645}
1646
Stephen Whitebf6137e2017-01-04 15:43:26 -05001647void sort_and_simplify(VertexList* vertices, Comparator& c, SkChunkAlloc& alloc) {
1648 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05001649 return;
ethannicholase9709e82016-01-07 13:34:16 -08001650 }
1651
1652 // Sort vertices in Y (secondarily in X).
senorblancof57372d2016-08-31 10:36:19 -07001653 merge_sort(vertices, c);
1654 merge_coincident_vertices(vertices, c, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001655#if LOGGING_ENABLED
Stephen White2e2cb9b2017-01-09 13:11:18 -05001656 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001657 static float gID = 0.0f;
1658 v->fID = gID++;
1659 }
1660#endif
senorblancof57372d2016-08-31 10:36:19 -07001661 simplify(*vertices, c, alloc);
Stephen White2f4686f2017-01-03 16:20:01 -05001662}
1663
Stephen Whitebf6137e2017-01-04 15:43:26 -05001664Poly* mesh_to_polys(VertexList* vertices, Comparator& c, SkChunkAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -05001665 sort_and_simplify(vertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001666 return tessellate(*vertices, alloc);
1667}
1668
1669Poly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType,
1670 const SkRect& pathBounds, bool antialias,
1671 SkChunkAlloc& alloc) {
1672 Comparator c;
1673 if (pathBounds.width() > pathBounds.height()) {
1674 c.sweep_lt = sweep_lt_horiz;
1675 c.sweep_gt = sweep_gt_horiz;
1676 } else {
1677 c.sweep_lt = sweep_lt_vert;
1678 c.sweep_gt = sweep_gt_vert;
1679 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001680 VertexList mesh;
1681 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
senorblanco7ab96e92016-10-12 06:47:44 -07001682 Poly* polys = mesh_to_polys(&mesh, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001683 if (antialias) {
1684 EdgeList* boundaries = extract_boundaries(mesh, fillType, alloc);
1685 VertexList aaMesh;
1686 for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = boundary->fNext) {
1687 simplify_boundary(boundary, c, alloc);
1688 if (boundary->fCount > 2) {
1689 boundary_to_aa_mesh(boundary, &aaMesh, c, alloc);
1690 }
1691 }
Stephen Whitebf6137e2017-01-04 15:43:26 -05001692 sort_and_simplify(&aaMesh, c, alloc);
1693 return tessellate(aaMesh, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001694 }
1695 return polys;
1696}
1697
1698// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1699void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
1700 void* data) {
1701 for (Poly* poly = polys; poly; poly = poly->fNext) {
1702 if (apply_fill_type(fillType, poly)) {
1703 data = poly->emit(aaParams, data);
1704 }
1705 }
1706 return data;
ethannicholase9709e82016-01-07 13:34:16 -08001707}
1708
halcanary9d524f22016-03-29 09:03:52 -07001709Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
senorblancof57372d2016-08-31 10:36:19 -07001710 int contourCnt, SkChunkAlloc& alloc, bool antialias, bool* isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -08001711 SkPath::FillType fillType = path.getFillType();
1712 if (SkPath::IsInverseFillType(fillType)) {
1713 contourCnt++;
1714 }
Ben Wagner7ecc5962016-11-02 17:07:33 -04001715 std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08001716
1717 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
senorblancof57372d2016-08-31 10:36:19 -07001718 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
1719 antialias, alloc);
ethannicholase9709e82016-01-07 13:34:16 -08001720}
1721
halcanary9d524f22016-03-29 09:03:52 -07001722void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt,
ethannicholase9709e82016-01-07 13:34:16 -08001723 int* sizeEstimate) {
1724 int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance);
1725 if (maxPts <= 0) {
1726 *contourCnt = 0;
1727 return;
1728 }
1729 if (maxPts > ((int)SK_MaxU16 + 1)) {
1730 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1731 *contourCnt = 0;
1732 return;
1733 }
1734 // For the initial size of the chunk allocator, estimate based on the point count:
1735 // one vertex per point for the initial passes, plus two for the vertices in the
1736 // resulting Polys, since the same point may end up in two Polys. Assume minimal
1737 // connectivity of one Edge per Vertex (will grow for intersections).
1738 *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge));
1739}
1740
1741int count_points(Poly* polys, SkPath::FillType fillType) {
1742 int count = 0;
1743 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001744 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
ethannicholase9709e82016-01-07 13:34:16 -08001745 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1746 }
1747 }
1748 return count;
1749}
1750
1751} // namespace
1752
1753namespace GrTessellator {
1754
1755// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1756
halcanary9d524f22016-03-29 09:03:52 -07001757int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
senorblancof57372d2016-08-31 10:36:19 -07001758 VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
1759 bool canTweakAlphaForCoverage, bool* isLinear) {
ethannicholase9709e82016-01-07 13:34:16 -08001760 int contourCnt;
1761 int sizeEstimate;
1762 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
1763 if (contourCnt <= 0) {
1764 *isLinear = true;
1765 return 0;
1766 }
1767 SkChunkAlloc alloc(sizeEstimate);
senorblancof57372d2016-08-31 10:36:19 -07001768 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
1769 isLinear);
senorblanco7ab96e92016-10-12 06:47:44 -07001770 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
ethannicholase9709e82016-01-07 13:34:16 -08001771 int count = count_points(polys, fillType);
1772 if (0 == count) {
1773 return 0;
1774 }
1775
senorblancof57372d2016-08-31 10:36:19 -07001776 void* verts = vertexAllocator->lock(count);
senorblanco6599eff2016-03-10 08:38:45 -08001777 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08001778 SkDebugf("Could not allocate vertices\n");
1779 return 0;
1780 }
senorblancof57372d2016-08-31 10:36:19 -07001781
1782 LOG("emitting %d verts\n", count);
1783 AAParams aaParams;
1784 aaParams.fTweakAlpha = canTweakAlphaForCoverage;
1785 aaParams.fColor = color;
1786
1787 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
1788 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
1789 / vertexAllocator->stride());
ethannicholase9709e82016-01-07 13:34:16 -08001790 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08001791 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08001792 return actualCount;
1793}
1794
halcanary9d524f22016-03-29 09:03:52 -07001795int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
ethannicholase9709e82016-01-07 13:34:16 -08001796 GrTessellator::WindingVertex** verts) {
1797 int contourCnt;
1798 int sizeEstimate;
1799 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
1800 if (contourCnt <= 0) {
1801 return 0;
1802 }
1803 SkChunkAlloc alloc(sizeEstimate);
1804 bool isLinear;
senorblancof57372d2016-08-31 10:36:19 -07001805 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear);
ethannicholase9709e82016-01-07 13:34:16 -08001806 SkPath::FillType fillType = path.getFillType();
1807 int count = count_points(polys, fillType);
1808 if (0 == count) {
1809 *verts = nullptr;
1810 return 0;
1811 }
1812
1813 *verts = new GrTessellator::WindingVertex[count];
1814 GrTessellator::WindingVertex* vertsEnd = *verts;
1815 SkPoint* points = new SkPoint[count];
1816 SkPoint* pointsEnd = points;
1817 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001818 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08001819 SkPoint* start = pointsEnd;
senorblancof57372d2016-08-31 10:36:19 -07001820 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08001821 while (start != pointsEnd) {
1822 vertsEnd->fPos = *start;
1823 vertsEnd->fWinding = poly->fWinding;
1824 ++start;
1825 ++vertsEnd;
1826 }
1827 }
1828 }
1829 int actualCount = static_cast<int>(vertsEnd - *verts);
1830 SkASSERT(actualCount <= count);
1831 SkASSERT(pointsEnd - points == actualCount);
1832 delete[] points;
1833 return actualCount;
1834}
1835
1836} // namespace