blob: 14176db4ecef0e2b2dbd7c31f2c3ed3658e0a41b [file] [log] [blame]
senorblancod6ed19c2015-02-26 06:58:17 -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 "GrTessellatingPathRenderer.h"
9
senorblanco9ba39722015-03-05 07:13:42 -080010#include "GrBatch.h"
11#include "GrBatchTarget.h"
senorblancod6ed19c2015-02-26 06:58:17 -080012#include "GrDefaultGeoProcFactory.h"
13#include "GrPathUtils.h"
14#include "SkChunkAlloc.h"
15#include "SkGeometry.h"
16
17#include <stdio.h>
18
19/*
20 * This path renderer tessellates the path into triangles, uploads the triangles to a
21 * vertex buffer, and renders them with a single draw call. It does not currently do
22 * antialiasing, so it must be used in conjunction with multisampling.
23 *
24 * There are six stages to the algorithm:
25 *
26 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
27 * 2) Build a mesh of edges connecting the vertices (build_edges()).
28 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
29 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
30 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
31 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
32 *
33 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
34 * of vertices (and the necessity of inserting new vertices on intersection).
35 *
36 * Stages (4) and (5) use an active edge list, which a list of all edges for which the
37 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
38 * left-to-right based on the point where both edges are active (when both top vertices
39 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
40 * (shared), it's sorted based on the last point where both edges are active, so the
41 * "upper" bottom vertex.
42 *
43 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
44 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
45 * not exact and may violate the mesh topology or active edge list ordering. We
46 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
47 * points. This occurs in three ways:
48 *
49 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
50 * neighbouring edges at the top or bottom vertex. This is handled by merging the
51 * edges (merge_collinear_edges()).
52 * B) Intersections may cause an edge to violate the left-to-right ordering of the
53 * active edge list. This is handled by splitting the neighbour edge on the
54 * intersected vertex (cleanup_active_edges()).
55 * C) Shortening an edge may cause an active edge to become inactive or an inactive edge
56 * to become active. This is handled by removing or inserting the edge in the active
57 * edge list (fix_active_state()).
58 *
59 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
60 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
61 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
62 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
63 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
64 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
65 * linked list implementation. With the latter, all removals are O(1), and most insertions
66 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
67 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
68 * frequent. There may be other data structures worth investigating, however.
69 *
70 * Note that there is a compile-time flag (SWEEP_IN_X) which changes the orientation of the
71 * line sweep algorithms. When SWEEP_IN_X is unset, we sort vertices based on increasing
72 * Y coordinate, and secondarily by increasing X coordinate. When SWEEP_IN_X is set, we sort by
73 * increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so that the
74 * "left" and "right" orientation in the code remains correct (edges to the left are increasing
75 * in Y; edges to the right are decreasing in Y). That is, the setting rotates 90 degrees
76 * counterclockwise, rather that transposing.
77 *
78 * The choice is arbitrary, but most test cases are wider than they are tall, so the
79 * default is to sweep in X. In the future, we may want to make this a runtime parameter
80 * and base it on the aspect ratio of the clip bounds.
81 */
82#define LOGGING_ENABLED 0
83#define WIREFRAME 0
84#define SWEEP_IN_X 1
85
86#if LOGGING_ENABLED
87#define LOG printf
88#else
89#define LOG(...)
90#endif
91
92#define ALLOC_NEW(Type, args, alloc) \
93 SkNEW_PLACEMENT_ARGS(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>
102void insert(T* t, T* prev, T* next, T** head, T** tail) {
103 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>
118void remove(T* t, T** head, T** tail) {
119 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 = NULL;
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 {
144 Vertex(const SkPoint& point)
145 : fPoint(point), fPrev(NULL), fNext(NULL)
146 , fFirstEdgeAbove(NULL), fLastEdgeAbove(NULL)
147 , fFirstEdgeBelow(NULL), fLastEdgeBelow(NULL)
148 , fProcessed(false)
149#if LOGGING_ENABLED
150 , fID (-1.0f)
151#endif
152 {}
153 SkPoint fPoint; // Vertex position
154 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
155 Vertex* fNext; // "
156 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
157 Edge* fLastEdgeAbove; // "
158 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
159 Edge* fLastEdgeBelow; // "
160 bool fProcessed; // Has this vertex been seen in simplify()?
161#if LOGGING_ENABLED
162 float fID; // Identifier used for logging.
163#endif
164};
165
166/***************************************************************************************/
167
168bool sweep_lt(const SkPoint& a, const SkPoint& b) {
169#if SWEEP_IN_X
170 return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX;
171#else
172 return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY;
173#endif
174}
175
176bool sweep_gt(const SkPoint& a, const SkPoint& b) {
177#if SWEEP_IN_X
178 return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX;
179#else
180 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
181#endif
182}
183
184inline void* emit_vertex(Vertex* v, void* data) {
185 SkPoint* d = static_cast<SkPoint*>(data);
186 *d++ = v->fPoint;
187 return d;
188}
189
190void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, void* data) {
191#if WIREFRAME
192 data = emit_vertex(v0, data);
193 data = emit_vertex(v1, data);
194 data = emit_vertex(v1, data);
195 data = emit_vertex(v2, data);
196 data = emit_vertex(v2, data);
197 data = emit_vertex(v0, data);
198#else
199 data = emit_vertex(v0, data);
200 data = emit_vertex(v1, data);
201 data = emit_vertex(v2, data);
202#endif
203 return data;
204}
205
206/**
207 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
208 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
209 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
210 * point). For speed, that case is only tested by the callers which require it (e.g.,
211 * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
212 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
213 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
214 * a lot faster in the "not found" case.
215 *
216 * The coefficients of the line equation stored in double precision to avoid catastrphic
217 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
218 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
219 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
220 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
221 * this file).
222 */
223
224struct Edge {
225 Edge(Vertex* top, Vertex* bottom, int winding)
226 : fWinding(winding)
227 , fTop(top)
228 , fBottom(bottom)
229 , fLeft(NULL)
230 , fRight(NULL)
231 , fPrevEdgeAbove(NULL)
232 , fNextEdgeAbove(NULL)
233 , fPrevEdgeBelow(NULL)
234 , fNextEdgeBelow(NULL)
235 , fLeftPoly(NULL)
236 , fRightPoly(NULL) {
237 recompute();
238 }
239 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
240 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
241 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
242 Edge* fLeft; // The linked list of edges in the active edge list.
243 Edge* fRight; // "
244 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
245 Edge* fNextEdgeAbove; // "
246 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
247 Edge* fNextEdgeBelow; // "
248 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
249 Poly* fRightPoly; // The Poly to the right of this edge, if any.
250 double fDX; // The line equation for this edge, in implicit form.
251 double fDY; // fDY * x + fDX * y + fC = 0, for point (x, y) on the line.
252 double fC;
253 double dist(const SkPoint& p) const {
254 return fDY * p.fX - fDX * p.fY + fC;
255 }
256 bool isRightOf(Vertex* v) const {
257 return dist(v->fPoint) < 0.0;
258 }
259 bool isLeftOf(Vertex* v) const {
260 return dist(v->fPoint) > 0.0;
261 }
262 void recompute() {
263 fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX;
264 fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY;
265 fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX -
266 static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY;
267 }
268 bool intersect(const Edge& other, SkPoint* p) {
269 LOG("intersecting %g -> %g with %g -> %g\n",
270 fTop->fID, fBottom->fID,
271 other.fTop->fID, other.fBottom->fID);
272 if (fTop == other.fTop || fBottom == other.fBottom) {
273 return false;
274 }
275 double denom = fDX * other.fDY - fDY * other.fDX;
276 if (denom == 0.0) {
277 return false;
278 }
279 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
280 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
281 double sNumer = dy * other.fDX - dx * other.fDY;
282 double tNumer = dy * fDX - dx * fDY;
283 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
284 // This saves us doing the divide below unless absolutely necessary.
285 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
286 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
287 return false;
288 }
289 double s = sNumer / denom;
290 SkASSERT(s >= 0.0 && s <= 1.0);
291 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
292 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
293 return true;
294 }
295 bool isActive(Edge** activeEdges) const {
296 return activeEdges && (fLeft || fRight || *activeEdges == this);
297 }
298};
299
300/***************************************************************************************/
301
302struct Poly {
303 Poly(int winding)
304 : fWinding(winding)
305 , fHead(NULL)
306 , fTail(NULL)
307 , fActive(NULL)
308 , fNext(NULL)
309 , fPartner(NULL)
310 , fCount(0)
311 {
312#if LOGGING_ENABLED
313 static int gID = 0;
314 fID = gID++;
315 LOG("*** created Poly %d\n", fID);
316#endif
317 }
318 typedef enum { kNeither_Side, kLeft_Side, kRight_Side } Side;
319 struct MonotonePoly {
320 MonotonePoly()
321 : fSide(kNeither_Side)
322 , fHead(NULL)
323 , fTail(NULL)
324 , fPrev(NULL)
325 , fNext(NULL) {}
326 Side fSide;
327 Vertex* fHead;
328 Vertex* fTail;
329 MonotonePoly* fPrev;
330 MonotonePoly* fNext;
331 bool addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) {
332 Vertex* newV = ALLOC_NEW(Vertex, (v->fPoint), alloc);
333 bool done = false;
334 if (fSide == kNeither_Side) {
335 fSide = side;
336 } else {
337 done = side != fSide;
338 }
339 if (fHead == NULL) {
340 fHead = fTail = newV;
341 } else if (fSide == kRight_Side) {
342 newV->fPrev = fTail;
343 fTail->fNext = newV;
344 fTail = newV;
345 } else {
346 newV->fNext = fHead;
347 fHead->fPrev = newV;
348 fHead = newV;
349 }
350 return done;
351 }
352
353 void* emit(void* data) {
354 Vertex* first = fHead;
355 Vertex* v = first->fNext;
356 while (v != fTail) {
357 SkASSERT(v && v->fPrev && v->fNext);
senorblancod6ed19c2015-02-26 06:58:17 -0800358 Vertex* prev = v->fPrev;
359 Vertex* curr = v;
360 Vertex* next = v->fNext;
361 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
362 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
363 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
364 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
365 if (ax * by - ay * bx >= 0.0) {
366 data = emit_triangle(prev, curr, next, data);
367 v->fPrev->fNext = v->fNext;
368 v->fNext->fPrev = v->fPrev;
369 if (v->fPrev == first) {
370 v = v->fNext;
371 } else {
372 v = v->fPrev;
373 }
374 } else {
375 v = v->fNext;
senorblancod6ed19c2015-02-26 06:58:17 -0800376 }
377 }
378 return data;
379 }
senorblancod6ed19c2015-02-26 06:58:17 -0800380 };
381 Poly* addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) {
382 LOG("addVertex() to %d at %g (%g, %g), %s side\n", fID, v->fID, v->fPoint.fX, v->fPoint.fY,
383 side == kLeft_Side ? "left" : side == kRight_Side ? "right" : "neither");
384 Poly* partner = fPartner;
385 Poly* poly = this;
386 if (partner) {
387 fPartner = partner->fPartner = NULL;
388 }
389 if (!fActive) {
390 fActive = ALLOC_NEW(MonotonePoly, (), alloc);
391 }
392 if (fActive->addVertex(v, side, alloc)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800393 if (fTail) {
394 fActive->fPrev = fTail;
395 fTail->fNext = fActive;
396 fTail = fActive;
397 } else {
398 fHead = fTail = fActive;
399 }
400 if (partner) {
401 partner->addVertex(v, side, alloc);
402 poly = partner;
403 } else {
404 Vertex* prev = fActive->fSide == Poly::kLeft_Side ?
405 fActive->fHead->fNext : fActive->fTail->fPrev;
406 fActive = ALLOC_NEW(MonotonePoly, , alloc);
407 fActive->addVertex(prev, Poly::kNeither_Side, alloc);
408 fActive->addVertex(v, side, alloc);
409 }
410 }
411 fCount++;
412 return poly;
413 }
414 void end(Vertex* v, SkChunkAlloc& alloc) {
415 LOG("end() %d at %g, %g\n", fID, v->fPoint.fX, v->fPoint.fY);
416 if (fPartner) {
417 fPartner = fPartner->fPartner = NULL;
418 }
419 addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, alloc);
420 }
421 void* emit(void *data) {
422 if (fCount < 3) {
423 return data;
424 }
425 LOG("emit() %d, size %d\n", fID, fCount);
426 for (MonotonePoly* m = fHead; m != NULL; m = m->fNext) {
427 data = m->emit(data);
428 }
429 return data;
430 }
431 int fWinding;
432 MonotonePoly* fHead;
433 MonotonePoly* fTail;
434 MonotonePoly* fActive;
435 Poly* fNext;
436 Poly* fPartner;
437 int fCount;
438#if LOGGING_ENABLED
439 int fID;
440#endif
441};
442
443/***************************************************************************************/
444
445bool coincident(const SkPoint& a, const SkPoint& b) {
446 return a == b;
447}
448
449Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
450 Poly* poly = ALLOC_NEW(Poly, (winding), alloc);
451 poly->addVertex(v, Poly::kNeither_Side, alloc);
452 poly->fNext = *head;
453 *head = poly;
454 return poly;
455}
456
senorblancod6ed19c2015-02-26 06:58:17 -0800457Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
458 SkChunkAlloc& alloc) {
459 Vertex* v = ALLOC_NEW(Vertex, (p), alloc);
460#if LOGGING_ENABLED
461 static float gID = 0.0f;
462 v->fID = gID++;
463#endif
464 if (prev) {
465 prev->fNext = v;
466 v->fPrev = prev;
467 } else {
468 *head = v;
469 }
470 return v;
471}
472
473Vertex* generate_quadratic_points(const SkPoint& p0,
474 const SkPoint& p1,
475 const SkPoint& p2,
476 SkScalar tolSqd,
477 Vertex* prev,
478 Vertex** head,
479 int pointsLeft,
480 SkChunkAlloc& alloc) {
481 SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
482 if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
483 return append_point_to_contour(p2, prev, head, alloc);
484 }
485
486 const SkPoint q[] = {
487 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
488 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
489 };
490 const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
491
492 pointsLeft >>= 1;
493 prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
494 prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
495 return prev;
496}
497
498Vertex* generate_cubic_points(const SkPoint& p0,
499 const SkPoint& p1,
500 const SkPoint& p2,
501 const SkPoint& p3,
502 SkScalar tolSqd,
503 Vertex* prev,
504 Vertex** head,
505 int pointsLeft,
506 SkChunkAlloc& alloc) {
507 SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
508 SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
509 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
510 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
511 return append_point_to_contour(p3, prev, head, alloc);
512 }
513 const SkPoint q[] = {
514 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
515 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
516 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
517 };
518 const SkPoint r[] = {
519 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
520 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
521 };
522 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
523 pointsLeft >>= 1;
524 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
525 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
526 return prev;
527}
528
529// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
530
531void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
532 Vertex** contours, SkChunkAlloc& alloc) {
533
534 SkScalar toleranceSqd = tolerance * tolerance;
535
536 SkPoint pts[4];
537 bool done = false;
538 SkPath::Iter iter(path, false);
539 Vertex* prev = NULL;
540 Vertex* head = NULL;
541 if (path.isInverseFillType()) {
542 SkPoint quad[4];
543 clipBounds.toQuad(quad);
544 for (int i = 3; i >= 0; i--) {
545 prev = append_point_to_contour(quad[i], prev, &head, alloc);
546 }
547 head->fPrev = prev;
548 prev->fNext = head;
549 *contours++ = head;
550 head = prev = NULL;
551 }
552 SkAutoConicToQuads converter;
553 while (!done) {
554 SkPath::Verb verb = iter.next(pts);
555 switch (verb) {
556 case SkPath::kConic_Verb: {
557 SkScalar weight = iter.conicWeight();
558 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
559 for (int i = 0; i < converter.countQuads(); ++i) {
560 int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, toleranceSqd);
561 prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
562 toleranceSqd, prev, &head, pointsLeft, alloc);
563 quadPts += 2;
564 }
565 break;
566 }
567 case SkPath::kMove_Verb:
568 if (head) {
569 head->fPrev = prev;
570 prev->fNext = head;
571 *contours++ = head;
572 }
573 head = prev = NULL;
574 prev = append_point_to_contour(pts[0], prev, &head, alloc);
575 break;
576 case SkPath::kLine_Verb: {
577 prev = append_point_to_contour(pts[1], prev, &head, alloc);
578 break;
579 }
580 case SkPath::kQuad_Verb: {
581 int pointsLeft = GrPathUtils::quadraticPointCount(pts, toleranceSqd);
582 prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
583 &head, pointsLeft, alloc);
584 break;
585 }
586 case SkPath::kCubic_Verb: {
587 int pointsLeft = GrPathUtils::cubicPointCount(pts, toleranceSqd);
588 prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
589 toleranceSqd, prev, &head, pointsLeft, alloc);
590 break;
591 }
592 case SkPath::kClose_Verb:
593 if (head) {
594 head->fPrev = prev;
595 prev->fNext = head;
596 *contours++ = head;
597 }
598 head = prev = NULL;
599 break;
600 case SkPath::kDone_Verb:
601 if (head) {
602 head->fPrev = prev;
603 prev->fNext = head;
604 *contours++ = head;
605 }
606 done = true;
607 break;
608 }
609 }
610}
611
612inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
613 switch (fillType) {
614 case SkPath::kWinding_FillType:
615 return winding != 0;
616 case SkPath::kEvenOdd_FillType:
617 return (winding & 1) != 0;
618 case SkPath::kInverseWinding_FillType:
619 return winding == 1;
620 case SkPath::kInverseEvenOdd_FillType:
621 return (winding & 1) == 1;
622 default:
623 SkASSERT(false);
624 return false;
625 }
626}
627
628Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc) {
629 int winding = sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
630 Vertex* top = winding < 0 ? next : prev;
631 Vertex* bottom = winding < 0 ? prev : next;
632 return ALLOC_NEW(Edge, (top, bottom, winding), alloc);
633}
634
635void remove_edge(Edge* edge, Edge** head) {
636 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
637 SkASSERT(edge->isActive(head));
638 remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, head, NULL);
639}
640
641void insert_edge(Edge* edge, Edge* prev, Edge** head) {
642 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
643 SkASSERT(!edge->isActive(head));
644 Edge* next = prev ? prev->fRight : *head;
645 insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, head, NULL);
646}
647
648void find_enclosing_edges(Vertex* v, Edge* head, Edge** left, Edge** right) {
649 if (v->fFirstEdgeAbove) {
650 *left = v->fFirstEdgeAbove->fLeft;
651 *right = v->fLastEdgeAbove->fRight;
652 return;
653 }
654 Edge* prev = NULL;
655 Edge* next;
656 for (next = head; next != NULL; next = next->fRight) {
657 if (next->isRightOf(v)) {
658 break;
659 }
660 prev = next;
661 }
662 *left = prev;
663 *right = next;
664 return;
665}
666
667void find_enclosing_edges(Edge* edge, Edge* head, Edge** left, Edge** right) {
668 Edge* prev = NULL;
669 Edge* next;
670 for (next = head; next != NULL; next = next->fRight) {
671 if ((sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
672 (sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
673 (sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
674 next->isRightOf(edge->fBottom)) ||
675 (sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
676 edge->isLeftOf(next->fBottom))) {
677 break;
678 }
679 prev = next;
680 }
681 *left = prev;
682 *right = next;
683 return;
684}
685
686void fix_active_state(Edge* edge, Edge** activeEdges) {
687 if (edge->isActive(activeEdges)) {
688 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
689 remove_edge(edge, activeEdges);
690 }
691 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
692 Edge* left;
693 Edge* right;
694 find_enclosing_edges(edge, *activeEdges, &left, &right);
695 insert_edge(edge, left, activeEdges);
696 }
697}
698
699void insert_edge_above(Edge* edge, Vertex* v) {
700 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
701 sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800702 return;
703 }
704 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
705 Edge* prev = NULL;
706 Edge* next;
707 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
708 if (next->isRightOf(edge->fTop)) {
709 break;
710 }
711 prev = next;
712 }
713 insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
714 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
715}
716
717void insert_edge_below(Edge* edge, Vertex* v) {
718 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
719 sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800720 return;
721 }
722 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
723 Edge* prev = NULL;
724 Edge* next;
725 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
726 if (next->isRightOf(edge->fBottom)) {
727 break;
728 }
729 prev = next;
730 }
731 insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
732 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
733}
734
735void remove_edge_above(Edge* edge) {
736 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
737 edge->fBottom->fID);
738 remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
739 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
740}
741
742void remove_edge_below(Edge* edge) {
743 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
744 edge->fTop->fID);
745 remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
746 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
747}
748
749void erase_edge_if_zero_winding(Edge* edge, Edge** head) {
750 if (edge->fWinding != 0) {
751 return;
752 }
753 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
754 remove_edge_above(edge);
755 remove_edge_below(edge);
756 if (edge->isActive(head)) {
757 remove_edge(edge, head);
758 }
759}
760
761void merge_collinear_edges(Edge* edge, Edge** activeEdges);
762
763void set_top(Edge* edge, Vertex* v, Edge** activeEdges) {
764 remove_edge_below(edge);
765 edge->fTop = v;
766 edge->recompute();
767 insert_edge_below(edge, v);
768 fix_active_state(edge, activeEdges);
769 merge_collinear_edges(edge, activeEdges);
770}
771
772void set_bottom(Edge* edge, Vertex* v, Edge** activeEdges) {
773 remove_edge_above(edge);
774 edge->fBottom = v;
775 edge->recompute();
776 insert_edge_above(edge, v);
777 fix_active_state(edge, activeEdges);
778 merge_collinear_edges(edge, activeEdges);
779}
780
781void merge_edges_above(Edge* edge, Edge* other, Edge** activeEdges) {
782 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
783 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
784 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
785 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
786 other->fWinding += edge->fWinding;
787 erase_edge_if_zero_winding(other, activeEdges);
788 edge->fWinding = 0;
789 erase_edge_if_zero_winding(edge, activeEdges);
790 } else if (sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
791 other->fWinding += edge->fWinding;
792 erase_edge_if_zero_winding(other, activeEdges);
793 set_bottom(edge, other->fTop, activeEdges);
794 } else {
795 edge->fWinding += other->fWinding;
796 erase_edge_if_zero_winding(edge, activeEdges);
797 set_bottom(other, edge->fTop, activeEdges);
798 }
799}
800
801void merge_edges_below(Edge* edge, Edge* other, Edge** activeEdges) {
802 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
803 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
804 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
805 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
806 other->fWinding += edge->fWinding;
807 erase_edge_if_zero_winding(other, activeEdges);
808 edge->fWinding = 0;
809 erase_edge_if_zero_winding(edge, activeEdges);
810 } else if (sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
811 edge->fWinding += other->fWinding;
812 erase_edge_if_zero_winding(edge, activeEdges);
813 set_top(other, edge->fBottom, activeEdges);
814 } else {
815 other->fWinding += edge->fWinding;
816 erase_edge_if_zero_winding(other, activeEdges);
817 set_top(edge, other->fBottom, activeEdges);
818 }
819}
820
821void merge_collinear_edges(Edge* edge, Edge** activeEdges) {
822 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
823 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
824 merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges);
825 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
826 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
827 merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges);
828 }
829 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
830 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
831 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges);
832 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
833 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
834 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges);
835 }
836}
837
838void split_edge(Edge* edge, Vertex* v, Edge** activeEdges, SkChunkAlloc& alloc);
839
840void cleanup_active_edges(Edge* edge, Edge** activeEdges, SkChunkAlloc& alloc) {
841 Vertex* top = edge->fTop;
842 Vertex* bottom = edge->fBottom;
843 if (edge->fLeft) {
844 Vertex* leftTop = edge->fLeft->fTop;
845 Vertex* leftBottom = edge->fLeft->fBottom;
846 if (sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
847 split_edge(edge->fLeft, edge->fTop, activeEdges, alloc);
848 } else if (sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
849 split_edge(edge, leftTop, activeEdges, alloc);
850 } else if (sweep_lt(bottom->fPoint, leftBottom->fPoint) && !edge->fLeft->isLeftOf(bottom)) {
851 split_edge(edge->fLeft, bottom, activeEdges, alloc);
852 } else if (sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
853 split_edge(edge, leftBottom, activeEdges, alloc);
854 }
855 }
856 if (edge->fRight) {
857 Vertex* rightTop = edge->fRight->fTop;
858 Vertex* rightBottom = edge->fRight->fBottom;
859 if (sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
860 split_edge(edge->fRight, top, activeEdges, alloc);
861 } else if (sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
862 split_edge(edge, rightTop, activeEdges, alloc);
863 } else if (sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
864 !edge->fRight->isRightOf(bottom)) {
865 split_edge(edge->fRight, bottom, activeEdges, alloc);
866 } else if (sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
867 !edge->isLeftOf(rightBottom)) {
868 split_edge(edge, rightBottom, activeEdges, alloc);
869 }
870 }
871}
872
873void split_edge(Edge* edge, Vertex* v, Edge** activeEdges, SkChunkAlloc& alloc) {
874 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
875 edge->fTop->fID, edge->fBottom->fID,
876 v->fID, v->fPoint.fX, v->fPoint.fY);
senorblancoa2b6d282015-03-02 09:34:13 -0800877 if (sweep_lt(v->fPoint, edge->fTop->fPoint)) {
878 set_top(edge, v, activeEdges);
879 } else if (sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
880 set_bottom(edge, v, activeEdges);
881 } else {
882 Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), alloc);
883 insert_edge_below(newEdge, v);
884 insert_edge_above(newEdge, edge->fBottom);
885 set_bottom(edge, v, activeEdges);
886 cleanup_active_edges(edge, activeEdges, alloc);
887 fix_active_state(newEdge, activeEdges);
888 merge_collinear_edges(newEdge, activeEdges);
889 }
senorblancod6ed19c2015-02-26 06:58:17 -0800890}
891
892void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, SkChunkAlloc& alloc) {
893 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
894 src->fID, dst->fID);
895 for (Edge* edge = src->fFirstEdgeAbove; edge;) {
896 Edge* next = edge->fNextEdgeAbove;
897 set_bottom(edge, dst, NULL);
898 edge = next;
899 }
900 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
901 Edge* next = edge->fNextEdgeBelow;
902 set_top(edge, dst, NULL);
903 edge = next;
904 }
905 remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, NULL);
906}
907
908Vertex* check_for_intersection(Edge* edge, Edge* other, Edge** activeEdges, SkChunkAlloc& alloc) {
909 SkPoint p;
910 if (!edge || !other) {
911 return NULL;
912 }
913 if (edge->intersect(*other, &p)) {
914 Vertex* v;
915 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
916 if (p == edge->fTop->fPoint || sweep_lt(p, edge->fTop->fPoint)) {
917 split_edge(other, edge->fTop, activeEdges, alloc);
918 v = edge->fTop;
919 } else if (p == edge->fBottom->fPoint || sweep_gt(p, edge->fBottom->fPoint)) {
920 split_edge(other, edge->fBottom, activeEdges, alloc);
921 v = edge->fBottom;
922 } else if (p == other->fTop->fPoint || sweep_lt(p, other->fTop->fPoint)) {
923 split_edge(edge, other->fTop, activeEdges, alloc);
924 v = other->fTop;
925 } else if (p == other->fBottom->fPoint || sweep_gt(p, other->fBottom->fPoint)) {
926 split_edge(edge, other->fBottom, activeEdges, alloc);
927 v = other->fBottom;
928 } else {
929 Vertex* nextV = edge->fTop;
930 while (sweep_lt(p, nextV->fPoint)) {
931 nextV = nextV->fPrev;
932 }
933 while (sweep_lt(nextV->fPoint, p)) {
934 nextV = nextV->fNext;
935 }
936 Vertex* prevV = nextV->fPrev;
937 if (coincident(prevV->fPoint, p)) {
938 v = prevV;
939 } else if (coincident(nextV->fPoint, p)) {
940 v = nextV;
941 } else {
942 v = ALLOC_NEW(Vertex, (p), alloc);
943 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
944 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
945 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
946#if LOGGING_ENABLED
947 v->fID = (nextV->fID + prevV->fID) * 0.5f;
948#endif
949 v->fPrev = prevV;
950 v->fNext = nextV;
951 prevV->fNext = v;
952 nextV->fPrev = v;
953 }
954 split_edge(edge, v, activeEdges, alloc);
955 split_edge(other, v, activeEdges, alloc);
956 }
senorblancod6ed19c2015-02-26 06:58:17 -0800957 return v;
958 }
959 return NULL;
960}
961
962void sanitize_contours(Vertex** contours, int contourCnt) {
963 for (int i = 0; i < contourCnt; ++i) {
964 SkASSERT(contours[i]);
965 for (Vertex* v = contours[i];;) {
966 if (coincident(v->fPrev->fPoint, v->fPoint)) {
967 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
968 if (v->fPrev == v) {
969 contours[i] = NULL;
970 break;
971 }
972 v->fPrev->fNext = v->fNext;
973 v->fNext->fPrev = v->fPrev;
974 if (contours[i] == v) {
975 contours[i] = v->fNext;
976 }
977 v = v->fPrev;
978 } else {
979 v = v->fNext;
980 if (v == contours[i]) break;
981 }
982 }
983 }
984}
985
986void merge_coincident_vertices(Vertex** vertices, SkChunkAlloc& alloc) {
987 for (Vertex* v = (*vertices)->fNext; v != NULL; v = v->fNext) {
988 if (sweep_lt(v->fPoint, v->fPrev->fPoint)) {
989 v->fPoint = v->fPrev->fPoint;
990 }
991 if (coincident(v->fPrev->fPoint, v->fPoint)) {
992 merge_vertices(v->fPrev, v, vertices, alloc);
993 }
994 }
995}
996
997// Stage 2: convert the contours to a mesh of edges connecting the vertices.
998
999Vertex* build_edges(Vertex** contours, int contourCnt, SkChunkAlloc& alloc) {
1000 Vertex* vertices = NULL;
1001 Vertex* prev = NULL;
1002 for (int i = 0; i < contourCnt; ++i) {
1003 for (Vertex* v = contours[i]; v != NULL;) {
1004 Vertex* vNext = v->fNext;
1005 Edge* edge = new_edge(v->fPrev, v, alloc);
1006 if (edge->fWinding > 0) {
1007 insert_edge_below(edge, v->fPrev);
1008 insert_edge_above(edge, v);
1009 } else {
1010 insert_edge_below(edge, v);
1011 insert_edge_above(edge, v->fPrev);
1012 }
1013 merge_collinear_edges(edge, NULL);
1014 if (prev) {
1015 prev->fNext = v;
1016 v->fPrev = prev;
1017 } else {
1018 vertices = v;
1019 }
1020 prev = v;
1021 v = vNext;
1022 if (v == contours[i]) break;
1023 }
1024 }
1025 if (prev) {
1026 prev->fNext = vertices->fPrev = NULL;
1027 }
1028 return vertices;
1029}
1030
1031// Stage 3: sort the vertices by increasing Y (or X if SWEEP_IN_X is on).
1032
1033Vertex* sorted_merge(Vertex* a, Vertex* b);
1034
1035void front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) {
1036 Vertex* fast;
1037 Vertex* slow;
1038 if (!v || !v->fNext) {
1039 *pFront = v;
1040 *pBack = NULL;
1041 } else {
1042 slow = v;
1043 fast = v->fNext;
1044
1045 while (fast != NULL) {
1046 fast = fast->fNext;
1047 if (fast != NULL) {
1048 slow = slow->fNext;
1049 fast = fast->fNext;
1050 }
1051 }
1052
1053 *pFront = v;
1054 *pBack = slow->fNext;
1055 slow->fNext->fPrev = NULL;
1056 slow->fNext = NULL;
1057 }
1058}
1059
1060void merge_sort(Vertex** head) {
1061 if (!*head || !(*head)->fNext) {
1062 return;
1063 }
1064
1065 Vertex* a;
1066 Vertex* b;
1067 front_back_split(*head, &a, &b);
1068
1069 merge_sort(&a);
1070 merge_sort(&b);
1071
1072 *head = sorted_merge(a, b);
1073}
1074
senorblanco7ef63c82015-04-13 14:27:37 -07001075inline void append_vertex(Vertex* v, Vertex** head, Vertex** tail) {
1076 insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, NULL, head, tail);
1077}
1078
1079inline void append_vertex_list(Vertex* v, Vertex** head, Vertex** tail) {
1080 insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, v->fNext, head, tail);
1081}
1082
senorblancod6ed19c2015-02-26 06:58:17 -08001083Vertex* sorted_merge(Vertex* a, Vertex* b) {
senorblanco7ef63c82015-04-13 14:27:37 -07001084 Vertex* head = NULL;
1085 Vertex* tail = NULL;
senorblancod6ed19c2015-02-26 06:58:17 -08001086
senorblanco7ef63c82015-04-13 14:27:37 -07001087 while (a && b) {
1088 if (sweep_lt(a->fPoint, b->fPoint)) {
1089 Vertex* next = a->fNext;
1090 append_vertex(a, &head, &tail);
1091 a = next;
1092 } else {
1093 Vertex* next = b->fNext;
1094 append_vertex(b, &head, &tail);
1095 b = next;
1096 }
senorblancod6ed19c2015-02-26 06:58:17 -08001097 }
senorblanco7ef63c82015-04-13 14:27:37 -07001098 if (a) {
1099 append_vertex_list(a, &head, &tail);
1100 }
1101 if (b) {
1102 append_vertex_list(b, &head, &tail);
1103 }
1104 return head;
senorblancod6ed19c2015-02-26 06:58:17 -08001105}
1106
1107// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1108
1109void simplify(Vertex* vertices, SkChunkAlloc& alloc) {
1110 LOG("simplifying complex polygons\n");
1111 Edge* activeEdges = NULL;
1112 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1113 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1114 continue;
1115 }
1116#if LOGGING_ENABLED
1117 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1118#endif
senorblancod6ed19c2015-02-26 06:58:17 -08001119 Edge* leftEnclosingEdge = NULL;
1120 Edge* rightEnclosingEdge = NULL;
1121 bool restartChecks;
1122 do {
1123 restartChecks = false;
1124 find_enclosing_edges(v, activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1125 if (v->fFirstEdgeBelow) {
1126 for (Edge* edge = v->fFirstEdgeBelow; edge != NULL; edge = edge->fNextEdgeBelow) {
1127 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, alloc)) {
1128 restartChecks = true;
1129 break;
1130 }
1131 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, alloc)) {
1132 restartChecks = true;
1133 break;
1134 }
1135 }
1136 } else {
1137 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1138 &activeEdges, alloc)) {
1139 if (sweep_lt(pv->fPoint, v->fPoint)) {
1140 v = pv;
1141 }
1142 restartChecks = true;
1143 }
1144
1145 }
1146 } while (restartChecks);
senorblancod6ed19c2015-02-26 06:58:17 -08001147 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1148 remove_edge(e, &activeEdges);
1149 }
1150 Edge* leftEdge = leftEnclosingEdge;
1151 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1152 insert_edge(e, leftEdge, &activeEdges);
1153 leftEdge = e;
1154 }
1155 v->fProcessed = true;
1156 }
1157}
1158
1159// Stage 5: Tessellate the simplified mesh into monotone polygons.
1160
1161Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) {
1162 LOG("tessellating simple polygons\n");
1163 Edge* activeEdges = NULL;
1164 Poly* polys = NULL;
1165 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1166 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1167 continue;
1168 }
1169#if LOGGING_ENABLED
1170 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1171#endif
senorblancod6ed19c2015-02-26 06:58:17 -08001172 Edge* leftEnclosingEdge = NULL;
1173 Edge* rightEnclosingEdge = NULL;
1174 find_enclosing_edges(v, activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
senorblancod6ed19c2015-02-26 06:58:17 -08001175 Poly* leftPoly = NULL;
1176 Poly* rightPoly = NULL;
1177 if (v->fFirstEdgeAbove) {
1178 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1179 rightPoly = v->fLastEdgeAbove->fRightPoly;
1180 } else {
1181 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : NULL;
1182 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : NULL;
1183 }
1184#if LOGGING_ENABLED
1185 LOG("edges above:\n");
1186 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1187 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1188 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1189 }
1190 LOG("edges below:\n");
1191 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1192 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1193 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1194 }
1195#endif
1196 if (v->fFirstEdgeAbove) {
1197 if (leftPoly) {
1198 leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1199 }
1200 if (rightPoly) {
1201 rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1202 }
1203 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1204 Edge* leftEdge = e;
1205 Edge* rightEdge = e->fNextEdgeAbove;
1206 SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
1207 remove_edge(leftEdge, &activeEdges);
1208 if (leftEdge->fRightPoly) {
1209 leftEdge->fRightPoly->end(v, alloc);
1210 }
1211 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != leftEdge->fRightPoly) {
1212 rightEdge->fLeftPoly->end(v, alloc);
1213 }
1214 }
1215 remove_edge(v->fLastEdgeAbove, &activeEdges);
1216 if (!v->fFirstEdgeBelow) {
1217 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1218 SkASSERT(leftPoly->fPartner == NULL && rightPoly->fPartner == NULL);
1219 rightPoly->fPartner = leftPoly;
1220 leftPoly->fPartner = rightPoly;
1221 }
1222 }
1223 }
1224 if (v->fFirstEdgeBelow) {
1225 if (!v->fFirstEdgeAbove) {
1226 if (leftPoly && leftPoly == rightPoly) {
1227 // Split the poly.
1228 if (leftPoly->fActive->fSide == Poly::kLeft_Side) {
1229 leftPoly = new_poly(&polys, leftEnclosingEdge->fTop, leftPoly->fWinding,
1230 alloc);
1231 leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1232 rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1233 leftEnclosingEdge->fRightPoly = leftPoly;
1234 } else {
1235 rightPoly = new_poly(&polys, rightEnclosingEdge->fTop, rightPoly->fWinding,
1236 alloc);
1237 rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1238 leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1239 rightEnclosingEdge->fLeftPoly = rightPoly;
1240 }
1241 } else {
1242 if (leftPoly) {
1243 leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1244 }
1245 if (rightPoly) {
1246 rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1247 }
1248 }
1249 }
1250 Edge* leftEdge = v->fFirstEdgeBelow;
1251 leftEdge->fLeftPoly = leftPoly;
1252 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1253 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1254 rightEdge = rightEdge->fNextEdgeBelow) {
1255 insert_edge(rightEdge, leftEdge, &activeEdges);
1256 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1257 winding += leftEdge->fWinding;
1258 if (winding != 0) {
1259 Poly* poly = new_poly(&polys, v, winding, alloc);
1260 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1261 }
1262 leftEdge = rightEdge;
1263 }
1264 v->fLastEdgeBelow->fRightPoly = rightPoly;
1265 }
senorblancod6ed19c2015-02-26 06:58:17 -08001266#if LOGGING_ENABLED
1267 LOG("\nactive edges:\n");
1268 for (Edge* e = activeEdges; e != NULL; e = e->fRight) {
1269 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1270 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1271 }
1272#endif
1273 }
1274 return polys;
1275}
1276
1277// This is a driver function which calls stages 2-5 in turn.
1278
1279Poly* contours_to_polys(Vertex** contours, int contourCnt, SkChunkAlloc& alloc) {
1280#if LOGGING_ENABLED
1281 for (int i = 0; i < contourCnt; ++i) {
1282 Vertex* v = contours[i];
1283 SkASSERT(v);
1284 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1285 for (v = v->fNext; v != contours[i]; v = v->fNext) {
1286 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1287 }
1288 }
1289#endif
1290 sanitize_contours(contours, contourCnt);
1291 Vertex* vertices = build_edges(contours, contourCnt, alloc);
1292 if (!vertices) {
1293 return NULL;
1294 }
1295
1296 // Sort vertices in Y (secondarily in X).
1297 merge_sort(&vertices);
1298 merge_coincident_vertices(&vertices, alloc);
1299#if LOGGING_ENABLED
1300 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1301 static float gID = 0.0f;
1302 v->fID = gID++;
1303 }
1304#endif
1305 simplify(vertices, alloc);
1306 return tessellate(vertices, alloc);
1307}
1308
1309// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1310
1311void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, void* data) {
1312 void* d = data;
1313 for (Poly* poly = polys; poly; poly = poly->fNext) {
1314 if (apply_fill_type(fillType, poly->fWinding)) {
1315 d = poly->emit(d);
1316 }
1317 }
1318 return d;
1319}
1320
1321};
1322
1323GrTessellatingPathRenderer::GrTessellatingPathRenderer() {
1324}
1325
1326GrPathRenderer::StencilSupport GrTessellatingPathRenderer::onGetStencilSupport(
1327 const GrDrawTarget*,
1328 const GrPipelineBuilder*,
1329 const SkPath&,
1330 const SkStrokeRec&) const {
1331 return GrPathRenderer::kNoSupport_StencilSupport;
1332}
1333
1334bool GrTessellatingPathRenderer::canDrawPath(const GrDrawTarget* target,
1335 const GrPipelineBuilder* pipelineBuilder,
1336 const SkMatrix& viewMatrix,
1337 const SkPath& path,
1338 const SkStrokeRec& stroke,
1339 bool antiAlias) const {
1340 // This path renderer can draw all fill styles, but does not do antialiasing. It can do convex
1341 // and concave paths, but we'll leave the convex ones to simpler algorithms.
1342 return stroke.isFillStyle() && !antiAlias && !path.isConvex();
1343}
1344
senorblanco9ba39722015-03-05 07:13:42 -08001345class TessellatingPathBatch : public GrBatch {
1346public:
1347
1348 static GrBatch* Create(const GrColor& color,
1349 const SkPath& path,
1350 const SkMatrix& viewMatrix,
1351 SkRect clipBounds) {
1352 return SkNEW_ARGS(TessellatingPathBatch, (color, path, viewMatrix, clipBounds));
1353 }
1354
mtklein36352bf2015-03-25 18:17:31 -07001355 const char* name() const override { return "TessellatingPathBatch"; }
senorblanco9ba39722015-03-05 07:13:42 -08001356
mtklein36352bf2015-03-25 18:17:31 -07001357 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
senorblanco9ba39722015-03-05 07:13:42 -08001358 out->setKnownFourComponents(fColor);
1359 }
1360
mtklein36352bf2015-03-25 18:17:31 -07001361 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
senorblanco9ba39722015-03-05 07:13:42 -08001362 out->setUnknownSingleComponent();
1363 }
1364
mtklein36352bf2015-03-25 18:17:31 -07001365 void initBatchTracker(const GrPipelineInfo& init) override {
senorblanco9ba39722015-03-05 07:13:42 -08001366 // Handle any color overrides
1367 if (init.fColorIgnored) {
1368 fColor = GrColor_ILLEGAL;
1369 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
1370 fColor = init.fOverrideColor;
1371 }
1372 fPipelineInfo = init;
1373 }
1374
mtklein36352bf2015-03-25 18:17:31 -07001375 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
senorblanco9ba39722015-03-05 07:13:42 -08001376 SkScalar tol = GrPathUtils::scaleToleranceToSrc(SK_Scalar1, fViewMatrix, fPath.getBounds());
1377 int contourCnt;
1378 int maxPts = GrPathUtils::worstCasePointCount(fPath, &contourCnt, tol);
1379 if (maxPts <= 0) {
1380 return;
1381 }
1382 if (maxPts > ((int)SK_MaxU16 + 1)) {
1383 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1384 return;
1385 }
1386 SkPath::FillType fillType = fPath.getFillType();
1387 if (SkPath::IsInverseFillType(fillType)) {
1388 contourCnt++;
1389 }
1390
1391 LOG("got %d pts, %d contours\n", maxPts, contourCnt);
1392 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
1393 SkAutoTUnref<const GrGeometryProcessor> gp(
1394 GrDefaultGeoProcFactory::Create(flags, fColor, fViewMatrix, SkMatrix::I()));
1395 batchTarget->initDraw(gp, pipeline);
1396 gp->initBatchTracker(batchTarget->currentBatchTracker(), fPipelineInfo);
1397
1398 SkAutoTDeleteArray<Vertex*> contours(SkNEW_ARRAY(Vertex *, contourCnt));
1399
1400 // For the initial size of the chunk allocator, estimate based on the point count:
1401 // one vertex per point for the initial passes, plus two for the vertices in the
1402 // resulting Polys, since the same point may end up in two Polys. Assume minimal
1403 // connectivity of one Edge per Vertex (will grow for intersections).
1404 SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge)));
1405 path_to_contours(fPath, tol, fClipBounds, contours.get(), alloc);
1406 Poly* polys;
1407 polys = contours_to_polys(contours.get(), contourCnt, alloc);
1408 int count = 0;
1409 for (Poly* poly = polys; poly; poly = poly->fNext) {
1410 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1411 count += (poly->fCount - 2) * (WIREFRAME ? 6 : 3);
1412 }
1413 }
senorblancoe8331072015-03-26 14:52:45 -07001414 if (0 == count) {
1415 return;
1416 }
senorblanco9ba39722015-03-05 07:13:42 -08001417
1418 size_t stride = gp->getVertexStride();
1419 const GrVertexBuffer* vertexBuffer;
1420 int firstVertex;
1421 void* vertices = batchTarget->vertexPool()->makeSpace(stride,
1422 count,
1423 &vertexBuffer,
1424 &firstVertex);
joshualitt4b31de82015-03-05 14:33:41 -08001425
1426 if (!vertices) {
1427 SkDebugf("Could not allocate vertices\n");
1428 return;
1429 }
1430
senorblanco9ba39722015-03-05 07:13:42 -08001431 LOG("emitting %d verts\n", count);
1432 void* end = polys_to_triangles(polys, fillType, vertices);
1433 int actualCount = static_cast<int>(
1434 (static_cast<char*>(end) - static_cast<char*>(vertices)) / stride);
1435 LOG("actual count: %d\n", actualCount);
1436 SkASSERT(actualCount <= count);
1437
1438 GrPrimitiveType primitiveType = WIREFRAME ? kLines_GrPrimitiveType
1439 : kTriangles_GrPrimitiveType;
1440 GrDrawTarget::DrawInfo drawInfo;
1441 drawInfo.setPrimitiveType(primitiveType);
1442 drawInfo.setVertexBuffer(vertexBuffer);
1443 drawInfo.setStartVertex(firstVertex);
1444 drawInfo.setVertexCount(actualCount);
1445 drawInfo.setStartIndex(0);
1446 drawInfo.setIndexCount(0);
1447 batchTarget->draw(drawInfo);
1448
1449 batchTarget->putBackVertices((size_t)(count - actualCount), stride);
1450 return;
1451 }
1452
mtklein36352bf2015-03-25 18:17:31 -07001453 bool onCombineIfPossible(GrBatch*) override {
senorblanco9ba39722015-03-05 07:13:42 -08001454 return false;
1455 }
1456
1457private:
1458 TessellatingPathBatch(const GrColor& color,
1459 const SkPath& path,
1460 const SkMatrix& viewMatrix,
1461 const SkRect& clipBounds)
1462 : fColor(color)
1463 , fPath(path)
1464 , fViewMatrix(viewMatrix)
1465 , fClipBounds(clipBounds) {
1466 this->initClassID<TessellatingPathBatch>();
1467 }
1468
1469 GrColor fColor;
1470 SkPath fPath;
1471 SkMatrix fViewMatrix;
1472 SkRect fClipBounds; // in source space
1473 GrPipelineInfo fPipelineInfo;
1474};
1475
senorblancod6ed19c2015-02-26 06:58:17 -08001476bool GrTessellatingPathRenderer::onDrawPath(GrDrawTarget* target,
1477 GrPipelineBuilder* pipelineBuilder,
1478 GrColor color,
1479 const SkMatrix& viewM,
1480 const SkPath& path,
1481 const SkStrokeRec& stroke,
1482 bool antiAlias) {
1483 SkASSERT(!antiAlias);
1484 const GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
1485 if (NULL == rt) {
1486 return false;
1487 }
1488
senorblancod6ed19c2015-02-26 06:58:17 -08001489 SkIRect clipBoundsI;
1490 pipelineBuilder->clip().getConservativeBounds(rt, &clipBoundsI);
1491 SkRect clipBounds = SkRect::Make(clipBoundsI);
1492 SkMatrix vmi;
1493 if (!viewM.invert(&vmi)) {
1494 return false;
1495 }
1496 vmi.mapRect(&clipBounds);
senorblanco9ba39722015-03-05 07:13:42 -08001497 SkAutoTUnref<GrBatch> batch(TessellatingPathBatch::Create(color, path, viewM, clipBounds));
1498 target->drawBatch(pipelineBuilder, batch);
senorblancod6ed19c2015-02-26 06:58:17 -08001499
1500 return true;
1501}