blob: 1c1663dbf33aebe9c43453e8139bd54242519457 [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 *
senorblanco6bd51372015-04-15 07:32:27 -070070 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
71 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
72 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
73 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
74 * that the "left" and "right" orientation in the code remains correct (edges to the left are
75 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
76 * degrees counterclockwise, rather that transposing.
senorblancod6ed19c2015-02-26 06:58:17 -080077 */
78#define LOGGING_ENABLED 0
79#define WIREFRAME 0
senorblancod6ed19c2015-02-26 06:58:17 -080080
81#if LOGGING_ENABLED
82#define LOG printf
83#else
84#define LOG(...)
85#endif
86
87#define ALLOC_NEW(Type, args, alloc) \
88 SkNEW_PLACEMENT_ARGS(alloc.allocThrow(sizeof(Type)), Type, args)
89
90namespace {
91
92struct Vertex;
93struct Edge;
94struct Poly;
95
96template <class T, T* T::*Prev, T* T::*Next>
97void insert(T* t, T* prev, T* next, T** head, T** tail) {
98 t->*Prev = prev;
99 t->*Next = next;
100 if (prev) {
101 prev->*Next = t;
102 } else if (head) {
103 *head = t;
104 }
105 if (next) {
106 next->*Prev = t;
107 } else if (tail) {
108 *tail = t;
109 }
110}
111
112template <class T, T* T::*Prev, T* T::*Next>
113void remove(T* t, T** head, T** tail) {
114 if (t->*Prev) {
115 t->*Prev->*Next = t->*Next;
116 } else if (head) {
117 *head = t->*Next;
118 }
119 if (t->*Next) {
120 t->*Next->*Prev = t->*Prev;
121 } else if (tail) {
122 *tail = t->*Prev;
123 }
124 t->*Prev = t->*Next = NULL;
125}
126
127/**
128 * Vertices are used in three ways: first, the path contours are converted into a
129 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
130 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
131 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
132 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
133 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
134 * an individual Vertex from the path mesh may belong to multiple
135 * MonotonePolys, so the original Vertices cannot be re-used.
136 */
137
138struct Vertex {
139 Vertex(const SkPoint& point)
140 : fPoint(point), fPrev(NULL), fNext(NULL)
141 , fFirstEdgeAbove(NULL), fLastEdgeAbove(NULL)
142 , fFirstEdgeBelow(NULL), fLastEdgeBelow(NULL)
143 , fProcessed(false)
144#if LOGGING_ENABLED
145 , fID (-1.0f)
146#endif
147 {}
148 SkPoint fPoint; // Vertex position
149 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
150 Vertex* fNext; // "
151 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
152 Edge* fLastEdgeAbove; // "
153 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
154 Edge* fLastEdgeBelow; // "
155 bool fProcessed; // Has this vertex been seen in simplify()?
156#if LOGGING_ENABLED
157 float fID; // Identifier used for logging.
158#endif
159};
160
161/***************************************************************************************/
162
senorblanco6bd51372015-04-15 07:32:27 -0700163typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
164
165struct Comparator {
166 CompareFunc sweep_lt;
167 CompareFunc sweep_gt;
168};
169
170bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
senorblancod6ed19c2015-02-26 06:58:17 -0800171 return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX;
senorblancod6ed19c2015-02-26 06:58:17 -0800172}
173
senorblanco6bd51372015-04-15 07:32:27 -0700174bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
175 return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY;
176}
177
178bool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) {
senorblancod6ed19c2015-02-26 06:58:17 -0800179 return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX;
senorblanco6bd51372015-04-15 07:32:27 -0700180}
181
182bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
senorblancod6ed19c2015-02-26 06:58:17 -0800183 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
senorblancod6ed19c2015-02-26 06:58:17 -0800184}
185
186inline void* emit_vertex(Vertex* v, void* data) {
187 SkPoint* d = static_cast<SkPoint*>(data);
188 *d++ = v->fPoint;
189 return d;
190}
191
192void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, void* data) {
193#if WIREFRAME
194 data = emit_vertex(v0, data);
195 data = emit_vertex(v1, data);
196 data = emit_vertex(v1, data);
197 data = emit_vertex(v2, data);
198 data = emit_vertex(v2, data);
199 data = emit_vertex(v0, data);
200#else
201 data = emit_vertex(v0, data);
202 data = emit_vertex(v1, data);
203 data = emit_vertex(v2, data);
204#endif
205 return data;
206}
207
senorblanco5b9f42c2015-04-16 11:47:18 -0700208struct EdgeList {
209 EdgeList() : fHead(NULL), fTail(NULL) {}
210 Edge* fHead;
211 Edge* fTail;
212};
213
senorblancod6ed19c2015-02-26 06:58:17 -0800214/**
215 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
216 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
217 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
218 * point). For speed, that case is only tested by the callers which require it (e.g.,
219 * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
220 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
221 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
222 * a lot faster in the "not found" case.
223 *
224 * The coefficients of the line equation stored in double precision to avoid catastrphic
225 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
226 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
227 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
228 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
229 * this file).
230 */
231
232struct Edge {
233 Edge(Vertex* top, Vertex* bottom, int winding)
234 : fWinding(winding)
235 , fTop(top)
236 , fBottom(bottom)
237 , fLeft(NULL)
238 , fRight(NULL)
239 , fPrevEdgeAbove(NULL)
240 , fNextEdgeAbove(NULL)
241 , fPrevEdgeBelow(NULL)
242 , fNextEdgeBelow(NULL)
243 , fLeftPoly(NULL)
244 , fRightPoly(NULL) {
245 recompute();
246 }
247 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
248 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
249 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
250 Edge* fLeft; // The linked list of edges in the active edge list.
251 Edge* fRight; // "
252 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
253 Edge* fNextEdgeAbove; // "
254 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
255 Edge* fNextEdgeBelow; // "
256 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
257 Poly* fRightPoly; // The Poly to the right of this edge, if any.
258 double fDX; // The line equation for this edge, in implicit form.
259 double fDY; // fDY * x + fDX * y + fC = 0, for point (x, y) on the line.
260 double fC;
261 double dist(const SkPoint& p) const {
262 return fDY * p.fX - fDX * p.fY + fC;
263 }
264 bool isRightOf(Vertex* v) const {
265 return dist(v->fPoint) < 0.0;
266 }
267 bool isLeftOf(Vertex* v) const {
268 return dist(v->fPoint) > 0.0;
269 }
270 void recompute() {
271 fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX;
272 fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY;
273 fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX -
274 static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY;
275 }
276 bool intersect(const Edge& other, SkPoint* p) {
277 LOG("intersecting %g -> %g with %g -> %g\n",
278 fTop->fID, fBottom->fID,
279 other.fTop->fID, other.fBottom->fID);
280 if (fTop == other.fTop || fBottom == other.fBottom) {
281 return false;
282 }
283 double denom = fDX * other.fDY - fDY * other.fDX;
284 if (denom == 0.0) {
285 return false;
286 }
287 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
288 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
289 double sNumer = dy * other.fDX - dx * other.fDY;
290 double tNumer = dy * fDX - dx * fDY;
291 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
292 // This saves us doing the divide below unless absolutely necessary.
293 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
294 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
295 return false;
296 }
297 double s = sNumer / denom;
298 SkASSERT(s >= 0.0 && s <= 1.0);
299 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
300 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
301 return true;
302 }
senorblanco5b9f42c2015-04-16 11:47:18 -0700303 bool isActive(EdgeList* activeEdges) const {
304 return activeEdges && (fLeft || fRight || activeEdges->fHead == this);
senorblancod6ed19c2015-02-26 06:58:17 -0800305 }
306};
307
308/***************************************************************************************/
309
310struct Poly {
311 Poly(int winding)
312 : fWinding(winding)
313 , fHead(NULL)
314 , fTail(NULL)
315 , fActive(NULL)
316 , fNext(NULL)
317 , fPartner(NULL)
318 , fCount(0)
319 {
320#if LOGGING_ENABLED
321 static int gID = 0;
322 fID = gID++;
323 LOG("*** created Poly %d\n", fID);
324#endif
325 }
326 typedef enum { kNeither_Side, kLeft_Side, kRight_Side } Side;
327 struct MonotonePoly {
328 MonotonePoly()
329 : fSide(kNeither_Side)
330 , fHead(NULL)
331 , fTail(NULL)
332 , fPrev(NULL)
333 , fNext(NULL) {}
334 Side fSide;
335 Vertex* fHead;
336 Vertex* fTail;
337 MonotonePoly* fPrev;
338 MonotonePoly* fNext;
339 bool addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) {
340 Vertex* newV = ALLOC_NEW(Vertex, (v->fPoint), alloc);
341 bool done = false;
342 if (fSide == kNeither_Side) {
343 fSide = side;
344 } else {
345 done = side != fSide;
346 }
347 if (fHead == NULL) {
348 fHead = fTail = newV;
349 } else if (fSide == kRight_Side) {
350 newV->fPrev = fTail;
351 fTail->fNext = newV;
352 fTail = newV;
353 } else {
354 newV->fNext = fHead;
355 fHead->fPrev = newV;
356 fHead = newV;
357 }
358 return done;
359 }
360
361 void* emit(void* data) {
362 Vertex* first = fHead;
363 Vertex* v = first->fNext;
364 while (v != fTail) {
365 SkASSERT(v && v->fPrev && v->fNext);
senorblancod6ed19c2015-02-26 06:58:17 -0800366 Vertex* prev = v->fPrev;
367 Vertex* curr = v;
368 Vertex* next = v->fNext;
369 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
370 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
371 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
372 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
373 if (ax * by - ay * bx >= 0.0) {
374 data = emit_triangle(prev, curr, next, data);
375 v->fPrev->fNext = v->fNext;
376 v->fNext->fPrev = v->fPrev;
377 if (v->fPrev == first) {
378 v = v->fNext;
379 } else {
380 v = v->fPrev;
381 }
382 } else {
383 v = v->fNext;
senorblancod6ed19c2015-02-26 06:58:17 -0800384 }
385 }
386 return data;
387 }
senorblancod6ed19c2015-02-26 06:58:17 -0800388 };
389 Poly* addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) {
390 LOG("addVertex() to %d at %g (%g, %g), %s side\n", fID, v->fID, v->fPoint.fX, v->fPoint.fY,
391 side == kLeft_Side ? "left" : side == kRight_Side ? "right" : "neither");
392 Poly* partner = fPartner;
393 Poly* poly = this;
394 if (partner) {
395 fPartner = partner->fPartner = NULL;
396 }
397 if (!fActive) {
398 fActive = ALLOC_NEW(MonotonePoly, (), alloc);
399 }
400 if (fActive->addVertex(v, side, alloc)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800401 if (fTail) {
402 fActive->fPrev = fTail;
403 fTail->fNext = fActive;
404 fTail = fActive;
405 } else {
406 fHead = fTail = fActive;
407 }
408 if (partner) {
409 partner->addVertex(v, side, alloc);
410 poly = partner;
411 } else {
412 Vertex* prev = fActive->fSide == Poly::kLeft_Side ?
413 fActive->fHead->fNext : fActive->fTail->fPrev;
414 fActive = ALLOC_NEW(MonotonePoly, , alloc);
415 fActive->addVertex(prev, Poly::kNeither_Side, alloc);
416 fActive->addVertex(v, side, alloc);
417 }
418 }
419 fCount++;
420 return poly;
421 }
422 void end(Vertex* v, SkChunkAlloc& alloc) {
423 LOG("end() %d at %g, %g\n", fID, v->fPoint.fX, v->fPoint.fY);
424 if (fPartner) {
425 fPartner = fPartner->fPartner = NULL;
426 }
427 addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, alloc);
428 }
429 void* emit(void *data) {
430 if (fCount < 3) {
431 return data;
432 }
433 LOG("emit() %d, size %d\n", fID, fCount);
434 for (MonotonePoly* m = fHead; m != NULL; m = m->fNext) {
435 data = m->emit(data);
436 }
437 return data;
438 }
439 int fWinding;
440 MonotonePoly* fHead;
441 MonotonePoly* fTail;
442 MonotonePoly* fActive;
443 Poly* fNext;
444 Poly* fPartner;
445 int fCount;
446#if LOGGING_ENABLED
447 int fID;
448#endif
449};
450
451/***************************************************************************************/
452
453bool coincident(const SkPoint& a, const SkPoint& b) {
454 return a == b;
455}
456
457Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
458 Poly* poly = ALLOC_NEW(Poly, (winding), alloc);
459 poly->addVertex(v, Poly::kNeither_Side, alloc);
460 poly->fNext = *head;
461 *head = poly;
462 return poly;
463}
464
senorblancod6ed19c2015-02-26 06:58:17 -0800465Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
466 SkChunkAlloc& alloc) {
467 Vertex* v = ALLOC_NEW(Vertex, (p), alloc);
468#if LOGGING_ENABLED
469 static float gID = 0.0f;
470 v->fID = gID++;
471#endif
472 if (prev) {
473 prev->fNext = v;
474 v->fPrev = prev;
475 } else {
476 *head = v;
477 }
478 return v;
479}
480
481Vertex* generate_quadratic_points(const SkPoint& p0,
482 const SkPoint& p1,
483 const SkPoint& p2,
484 SkScalar tolSqd,
485 Vertex* prev,
486 Vertex** head,
487 int pointsLeft,
488 SkChunkAlloc& alloc) {
489 SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
490 if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
491 return append_point_to_contour(p2, prev, head, alloc);
492 }
493
494 const SkPoint q[] = {
495 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
496 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
497 };
498 const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
499
500 pointsLeft >>= 1;
501 prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
502 prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
503 return prev;
504}
505
506Vertex* generate_cubic_points(const SkPoint& p0,
507 const SkPoint& p1,
508 const SkPoint& p2,
509 const SkPoint& p3,
510 SkScalar tolSqd,
511 Vertex* prev,
512 Vertex** head,
513 int pointsLeft,
514 SkChunkAlloc& alloc) {
515 SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
516 SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
517 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
518 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
519 return append_point_to_contour(p3, prev, head, alloc);
520 }
521 const SkPoint q[] = {
522 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
523 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
524 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
525 };
526 const SkPoint r[] = {
527 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
528 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
529 };
530 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
531 pointsLeft >>= 1;
532 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
533 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
534 return prev;
535}
536
537// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
538
539void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
540 Vertex** contours, SkChunkAlloc& alloc) {
541
542 SkScalar toleranceSqd = tolerance * tolerance;
543
544 SkPoint pts[4];
545 bool done = false;
546 SkPath::Iter iter(path, false);
547 Vertex* prev = NULL;
548 Vertex* head = NULL;
549 if (path.isInverseFillType()) {
550 SkPoint quad[4];
551 clipBounds.toQuad(quad);
552 for (int i = 3; i >= 0; i--) {
553 prev = append_point_to_contour(quad[i], prev, &head, alloc);
554 }
555 head->fPrev = prev;
556 prev->fNext = head;
557 *contours++ = head;
558 head = prev = NULL;
559 }
560 SkAutoConicToQuads converter;
561 while (!done) {
562 SkPath::Verb verb = iter.next(pts);
563 switch (verb) {
564 case SkPath::kConic_Verb: {
565 SkScalar weight = iter.conicWeight();
566 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
567 for (int i = 0; i < converter.countQuads(); ++i) {
senorblanco6d88bdb2015-04-20 05:41:48 -0700568 int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
senorblancod6ed19c2015-02-26 06:58:17 -0800569 prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
570 toleranceSqd, prev, &head, pointsLeft, alloc);
571 quadPts += 2;
572 }
573 break;
574 }
575 case SkPath::kMove_Verb:
576 if (head) {
577 head->fPrev = prev;
578 prev->fNext = head;
579 *contours++ = head;
580 }
581 head = prev = NULL;
582 prev = append_point_to_contour(pts[0], prev, &head, alloc);
583 break;
584 case SkPath::kLine_Verb: {
585 prev = append_point_to_contour(pts[1], prev, &head, alloc);
586 break;
587 }
588 case SkPath::kQuad_Verb: {
senorblanco6d88bdb2015-04-20 05:41:48 -0700589 int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
senorblancod6ed19c2015-02-26 06:58:17 -0800590 prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
591 &head, pointsLeft, alloc);
592 break;
593 }
594 case SkPath::kCubic_Verb: {
senorblanco6d88bdb2015-04-20 05:41:48 -0700595 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
senorblancod6ed19c2015-02-26 06:58:17 -0800596 prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
597 toleranceSqd, prev, &head, pointsLeft, alloc);
598 break;
599 }
600 case SkPath::kClose_Verb:
601 if (head) {
602 head->fPrev = prev;
603 prev->fNext = head;
604 *contours++ = head;
605 }
606 head = prev = NULL;
607 break;
608 case SkPath::kDone_Verb:
609 if (head) {
610 head->fPrev = prev;
611 prev->fNext = head;
612 *contours++ = head;
613 }
614 done = true;
615 break;
616 }
617 }
618}
619
620inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
621 switch (fillType) {
622 case SkPath::kWinding_FillType:
623 return winding != 0;
624 case SkPath::kEvenOdd_FillType:
625 return (winding & 1) != 0;
626 case SkPath::kInverseWinding_FillType:
627 return winding == 1;
628 case SkPath::kInverseEvenOdd_FillType:
629 return (winding & 1) == 1;
630 default:
631 SkASSERT(false);
632 return false;
633 }
634}
635
senorblanco6bd51372015-04-15 07:32:27 -0700636Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c) {
637 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
senorblancod6ed19c2015-02-26 06:58:17 -0800638 Vertex* top = winding < 0 ? next : prev;
639 Vertex* bottom = winding < 0 ? prev : next;
640 return ALLOC_NEW(Edge, (top, bottom, winding), alloc);
641}
642
senorblanco5b9f42c2015-04-16 11:47:18 -0700643void remove_edge(Edge* edge, EdgeList* edges) {
senorblancod6ed19c2015-02-26 06:58:17 -0800644 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblanco5b9f42c2015-04-16 11:47:18 -0700645 SkASSERT(edge->isActive(edges));
646 remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &edges->fHead, &edges->fTail);
senorblancod6ed19c2015-02-26 06:58:17 -0800647}
648
senorblanco5b9f42c2015-04-16 11:47:18 -0700649void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
senorblancod6ed19c2015-02-26 06:58:17 -0800650 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblanco5b9f42c2015-04-16 11:47:18 -0700651 SkASSERT(!edge->isActive(edges));
652 Edge* next = prev ? prev->fRight : edges->fHead;
653 insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &edges->fHead, &edges->fTail);
senorblancod6ed19c2015-02-26 06:58:17 -0800654}
655
senorblanco5b9f42c2015-04-16 11:47:18 -0700656void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
senorblancod6ed19c2015-02-26 06:58:17 -0800657 if (v->fFirstEdgeAbove) {
658 *left = v->fFirstEdgeAbove->fLeft;
659 *right = v->fLastEdgeAbove->fRight;
660 return;
661 }
senorblanco5b9f42c2015-04-16 11:47:18 -0700662 Edge* next = NULL;
663 Edge* prev;
664 for (prev = edges->fTail; prev != NULL; prev = prev->fLeft) {
665 if (prev->isLeftOf(v)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800666 break;
667 }
senorblanco5b9f42c2015-04-16 11:47:18 -0700668 next = prev;
senorblancod6ed19c2015-02-26 06:58:17 -0800669 }
670 *left = prev;
671 *right = next;
672 return;
673}
674
senorblanco5b9f42c2015-04-16 11:47:18 -0700675void find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
senorblancod6ed19c2015-02-26 06:58:17 -0800676 Edge* prev = NULL;
677 Edge* next;
senorblanco5b9f42c2015-04-16 11:47:18 -0700678 for (next = edges->fHead; next != NULL; next = next->fRight) {
senorblanco6bd51372015-04-15 07:32:27 -0700679 if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
680 (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
681 (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800682 next->isRightOf(edge->fBottom)) ||
senorblanco6bd51372015-04-15 07:32:27 -0700683 (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800684 edge->isLeftOf(next->fBottom))) {
685 break;
686 }
687 prev = next;
688 }
689 *left = prev;
690 *right = next;
691 return;
692}
693
senorblanco5b9f42c2015-04-16 11:47:18 -0700694void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800695 if (edge->isActive(activeEdges)) {
696 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
697 remove_edge(edge, activeEdges);
698 }
699 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
700 Edge* left;
701 Edge* right;
senorblanco5b9f42c2015-04-16 11:47:18 -0700702 find_enclosing_edges(edge, activeEdges, c, &left, &right);
senorblancod6ed19c2015-02-26 06:58:17 -0800703 insert_edge(edge, left, activeEdges);
704 }
705}
706
senorblanco6bd51372015-04-15 07:32:27 -0700707void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800708 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
senorblanco6bd51372015-04-15 07:32:27 -0700709 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800710 return;
711 }
712 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
713 Edge* prev = NULL;
714 Edge* next;
715 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
716 if (next->isRightOf(edge->fTop)) {
717 break;
718 }
719 prev = next;
720 }
721 insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
722 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
723}
724
senorblanco6bd51372015-04-15 07:32:27 -0700725void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800726 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
senorblanco6bd51372015-04-15 07:32:27 -0700727 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800728 return;
729 }
730 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
731 Edge* prev = NULL;
732 Edge* next;
733 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
734 if (next->isRightOf(edge->fBottom)) {
735 break;
736 }
737 prev = next;
738 }
739 insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
740 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
741}
742
743void remove_edge_above(Edge* edge) {
744 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
745 edge->fBottom->fID);
746 remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
747 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
748}
749
750void remove_edge_below(Edge* edge) {
751 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
752 edge->fTop->fID);
753 remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
754 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
755}
756
senorblanco5b9f42c2015-04-16 11:47:18 -0700757void erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) {
senorblancod6ed19c2015-02-26 06:58:17 -0800758 if (edge->fWinding != 0) {
759 return;
760 }
761 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
762 remove_edge_above(edge);
763 remove_edge_below(edge);
senorblanco5b9f42c2015-04-16 11:47:18 -0700764 if (edge->isActive(edges)) {
765 remove_edge(edge, edges);
senorblancod6ed19c2015-02-26 06:58:17 -0800766 }
767}
768
senorblanco5b9f42c2015-04-16 11:47:18 -0700769void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
senorblancod6ed19c2015-02-26 06:58:17 -0800770
senorblanco5b9f42c2015-04-16 11:47:18 -0700771void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800772 remove_edge_below(edge);
773 edge->fTop = v;
774 edge->recompute();
senorblanco6bd51372015-04-15 07:32:27 -0700775 insert_edge_below(edge, v, c);
776 fix_active_state(edge, activeEdges, c);
777 merge_collinear_edges(edge, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800778}
779
senorblanco5b9f42c2015-04-16 11:47:18 -0700780void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800781 remove_edge_above(edge);
782 edge->fBottom = v;
783 edge->recompute();
senorblanco6bd51372015-04-15 07:32:27 -0700784 insert_edge_above(edge, v, c);
785 fix_active_state(edge, activeEdges, c);
786 merge_collinear_edges(edge, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800787}
788
senorblanco5b9f42c2015-04-16 11:47:18 -0700789void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800790 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
791 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
792 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
793 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
794 other->fWinding += edge->fWinding;
795 erase_edge_if_zero_winding(other, activeEdges);
796 edge->fWinding = 0;
797 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700798 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800799 other->fWinding += edge->fWinding;
800 erase_edge_if_zero_winding(other, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700801 set_bottom(edge, other->fTop, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800802 } else {
803 edge->fWinding += other->fWinding;
804 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700805 set_bottom(other, edge->fTop, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800806 }
807}
808
senorblanco5b9f42c2015-04-16 11:47:18 -0700809void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800810 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
811 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
812 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
813 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
814 other->fWinding += edge->fWinding;
815 erase_edge_if_zero_winding(other, activeEdges);
816 edge->fWinding = 0;
817 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700818 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800819 edge->fWinding += other->fWinding;
820 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700821 set_top(other, edge->fBottom, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800822 } else {
823 other->fWinding += edge->fWinding;
824 erase_edge_if_zero_winding(other, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700825 set_top(edge, other->fBottom, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800826 }
827}
828
senorblanco5b9f42c2015-04-16 11:47:18 -0700829void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800830 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
831 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
senorblanco6bd51372015-04-15 07:32:27 -0700832 merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800833 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
834 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
senorblanco6bd51372015-04-15 07:32:27 -0700835 merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800836 }
837 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
838 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
senorblanco6bd51372015-04-15 07:32:27 -0700839 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800840 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
841 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
senorblanco6bd51372015-04-15 07:32:27 -0700842 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800843 }
844}
845
senorblanco5b9f42c2015-04-16 11:47:18 -0700846void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800847
senorblanco5b9f42c2015-04-16 11:47:18 -0700848void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800849 Vertex* top = edge->fTop;
850 Vertex* bottom = edge->fBottom;
851 if (edge->fLeft) {
852 Vertex* leftTop = edge->fLeft->fTop;
853 Vertex* leftBottom = edge->fLeft->fBottom;
senorblanco6bd51372015-04-15 07:32:27 -0700854 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
855 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
856 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
857 split_edge(edge, leftTop, activeEdges, c, alloc);
858 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
859 !edge->fLeft->isLeftOf(bottom)) {
860 split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
861 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
862 split_edge(edge, leftBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800863 }
864 }
865 if (edge->fRight) {
866 Vertex* rightTop = edge->fRight->fTop;
867 Vertex* rightBottom = edge->fRight->fBottom;
senorblanco6bd51372015-04-15 07:32:27 -0700868 if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
869 split_edge(edge->fRight, top, activeEdges, c, alloc);
870 } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
871 split_edge(edge, rightTop, activeEdges, c, alloc);
872 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800873 !edge->fRight->isRightOf(bottom)) {
senorblanco6bd51372015-04-15 07:32:27 -0700874 split_edge(edge->fRight, bottom, activeEdges, c, alloc);
875 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800876 !edge->isLeftOf(rightBottom)) {
senorblanco6bd51372015-04-15 07:32:27 -0700877 split_edge(edge, rightBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800878 }
879 }
880}
881
senorblanco5b9f42c2015-04-16 11:47:18 -0700882void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800883 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
884 edge->fTop->fID, edge->fBottom->fID,
885 v->fID, v->fPoint.fX, v->fPoint.fY);
senorblanco6bd51372015-04-15 07:32:27 -0700886 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
887 set_top(edge, v, activeEdges, c);
888 } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
889 set_bottom(edge, v, activeEdges, c);
senorblancoa2b6d282015-03-02 09:34:13 -0800890 } else {
891 Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), alloc);
senorblanco6bd51372015-04-15 07:32:27 -0700892 insert_edge_below(newEdge, v, c);
893 insert_edge_above(newEdge, edge->fBottom, c);
894 set_bottom(edge, v, activeEdges, c);
895 cleanup_active_edges(edge, activeEdges, c, alloc);
896 fix_active_state(newEdge, activeEdges, c);
897 merge_collinear_edges(newEdge, activeEdges, c);
senorblancoa2b6d282015-03-02 09:34:13 -0800898 }
senorblancod6ed19c2015-02-26 06:58:17 -0800899}
900
senorblanco6bd51372015-04-15 07:32:27 -0700901void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800902 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
903 src->fID, dst->fID);
904 for (Edge* edge = src->fFirstEdgeAbove; edge;) {
905 Edge* next = edge->fNextEdgeAbove;
senorblanco6bd51372015-04-15 07:32:27 -0700906 set_bottom(edge, dst, NULL, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800907 edge = next;
908 }
909 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
910 Edge* next = edge->fNextEdgeBelow;
senorblanco6bd51372015-04-15 07:32:27 -0700911 set_top(edge, dst, NULL, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800912 edge = next;
913 }
914 remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, NULL);
915}
916
senorblanco5b9f42c2015-04-16 11:47:18 -0700917Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
senorblanco6bd51372015-04-15 07:32:27 -0700918 SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800919 SkPoint p;
920 if (!edge || !other) {
921 return NULL;
922 }
923 if (edge->intersect(*other, &p)) {
924 Vertex* v;
925 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
senorblanco6bd51372015-04-15 07:32:27 -0700926 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
927 split_edge(other, edge->fTop, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800928 v = edge->fTop;
senorblanco6bd51372015-04-15 07:32:27 -0700929 } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) {
930 split_edge(other, edge->fBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800931 v = edge->fBottom;
senorblanco6bd51372015-04-15 07:32:27 -0700932 } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
933 split_edge(edge, other->fTop, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800934 v = other->fTop;
senorblanco6bd51372015-04-15 07:32:27 -0700935 } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) {
936 split_edge(edge, other->fBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800937 v = other->fBottom;
938 } else {
939 Vertex* nextV = edge->fTop;
senorblanco6bd51372015-04-15 07:32:27 -0700940 while (c.sweep_lt(p, nextV->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800941 nextV = nextV->fPrev;
942 }
senorblanco6bd51372015-04-15 07:32:27 -0700943 while (c.sweep_lt(nextV->fPoint, p)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800944 nextV = nextV->fNext;
945 }
946 Vertex* prevV = nextV->fPrev;
947 if (coincident(prevV->fPoint, p)) {
948 v = prevV;
949 } else if (coincident(nextV->fPoint, p)) {
950 v = nextV;
951 } else {
952 v = ALLOC_NEW(Vertex, (p), alloc);
953 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
954 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
955 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
956#if LOGGING_ENABLED
957 v->fID = (nextV->fID + prevV->fID) * 0.5f;
958#endif
959 v->fPrev = prevV;
960 v->fNext = nextV;
961 prevV->fNext = v;
962 nextV->fPrev = v;
963 }
senorblanco6bd51372015-04-15 07:32:27 -0700964 split_edge(edge, v, activeEdges, c, alloc);
965 split_edge(other, v, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800966 }
senorblancod6ed19c2015-02-26 06:58:17 -0800967 return v;
968 }
969 return NULL;
970}
971
972void sanitize_contours(Vertex** contours, int contourCnt) {
973 for (int i = 0; i < contourCnt; ++i) {
974 SkASSERT(contours[i]);
975 for (Vertex* v = contours[i];;) {
976 if (coincident(v->fPrev->fPoint, v->fPoint)) {
977 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
978 if (v->fPrev == v) {
979 contours[i] = NULL;
980 break;
981 }
982 v->fPrev->fNext = v->fNext;
983 v->fNext->fPrev = v->fPrev;
984 if (contours[i] == v) {
985 contours[i] = v->fNext;
986 }
987 v = v->fPrev;
988 } else {
989 v = v->fNext;
990 if (v == contours[i]) break;
991 }
992 }
993 }
994}
995
senorblanco6bd51372015-04-15 07:32:27 -0700996void merge_coincident_vertices(Vertex** vertices, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800997 for (Vertex* v = (*vertices)->fNext; v != NULL; v = v->fNext) {
senorblanco6bd51372015-04-15 07:32:27 -0700998 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800999 v->fPoint = v->fPrev->fPoint;
1000 }
1001 if (coincident(v->fPrev->fPoint, v->fPoint)) {
senorblanco6bd51372015-04-15 07:32:27 -07001002 merge_vertices(v->fPrev, v, vertices, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001003 }
1004 }
1005}
1006
1007// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1008
senorblanco6bd51372015-04-15 07:32:27 -07001009Vertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -08001010 Vertex* vertices = NULL;
1011 Vertex* prev = NULL;
1012 for (int i = 0; i < contourCnt; ++i) {
1013 for (Vertex* v = contours[i]; v != NULL;) {
1014 Vertex* vNext = v->fNext;
senorblanco6bd51372015-04-15 07:32:27 -07001015 Edge* edge = new_edge(v->fPrev, v, alloc, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001016 if (edge->fWinding > 0) {
senorblanco6bd51372015-04-15 07:32:27 -07001017 insert_edge_below(edge, v->fPrev, c);
1018 insert_edge_above(edge, v, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001019 } else {
senorblanco6bd51372015-04-15 07:32:27 -07001020 insert_edge_below(edge, v, c);
1021 insert_edge_above(edge, v->fPrev, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001022 }
senorblanco6bd51372015-04-15 07:32:27 -07001023 merge_collinear_edges(edge, NULL, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001024 if (prev) {
1025 prev->fNext = v;
1026 v->fPrev = prev;
1027 } else {
1028 vertices = v;
1029 }
1030 prev = v;
1031 v = vNext;
1032 if (v == contours[i]) break;
1033 }
1034 }
1035 if (prev) {
1036 prev->fNext = vertices->fPrev = NULL;
1037 }
1038 return vertices;
1039}
1040
senorblanco6bd51372015-04-15 07:32:27 -07001041// Stage 3: sort the vertices by increasing sweep direction.
senorblancod6ed19c2015-02-26 06:58:17 -08001042
senorblanco6bd51372015-04-15 07:32:27 -07001043Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c);
senorblancod6ed19c2015-02-26 06:58:17 -08001044
1045void front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) {
1046 Vertex* fast;
1047 Vertex* slow;
1048 if (!v || !v->fNext) {
1049 *pFront = v;
1050 *pBack = NULL;
1051 } else {
1052 slow = v;
1053 fast = v->fNext;
1054
1055 while (fast != NULL) {
1056 fast = fast->fNext;
1057 if (fast != NULL) {
1058 slow = slow->fNext;
1059 fast = fast->fNext;
1060 }
1061 }
1062
1063 *pFront = v;
1064 *pBack = slow->fNext;
1065 slow->fNext->fPrev = NULL;
1066 slow->fNext = NULL;
1067 }
1068}
1069
senorblanco6bd51372015-04-15 07:32:27 -07001070void merge_sort(Vertex** head, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -08001071 if (!*head || !(*head)->fNext) {
1072 return;
1073 }
1074
1075 Vertex* a;
1076 Vertex* b;
1077 front_back_split(*head, &a, &b);
1078
senorblanco6bd51372015-04-15 07:32:27 -07001079 merge_sort(&a, c);
1080 merge_sort(&b, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001081
senorblanco6bd51372015-04-15 07:32:27 -07001082 *head = sorted_merge(a, b, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001083}
1084
senorblanco7ef63c82015-04-13 14:27:37 -07001085inline void append_vertex(Vertex* v, Vertex** head, Vertex** tail) {
1086 insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, NULL, head, tail);
1087}
1088
1089inline void append_vertex_list(Vertex* v, Vertex** head, Vertex** tail) {
1090 insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, v->fNext, head, tail);
1091}
1092
senorblanco6bd51372015-04-15 07:32:27 -07001093Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c) {
senorblanco7ef63c82015-04-13 14:27:37 -07001094 Vertex* head = NULL;
1095 Vertex* tail = NULL;
senorblancod6ed19c2015-02-26 06:58:17 -08001096
senorblanco7ef63c82015-04-13 14:27:37 -07001097 while (a && b) {
senorblanco6bd51372015-04-15 07:32:27 -07001098 if (c.sweep_lt(a->fPoint, b->fPoint)) {
senorblanco7ef63c82015-04-13 14:27:37 -07001099 Vertex* next = a->fNext;
1100 append_vertex(a, &head, &tail);
1101 a = next;
1102 } else {
1103 Vertex* next = b->fNext;
1104 append_vertex(b, &head, &tail);
1105 b = next;
1106 }
senorblancod6ed19c2015-02-26 06:58:17 -08001107 }
senorblanco7ef63c82015-04-13 14:27:37 -07001108 if (a) {
1109 append_vertex_list(a, &head, &tail);
1110 }
1111 if (b) {
1112 append_vertex_list(b, &head, &tail);
1113 }
1114 return head;
senorblancod6ed19c2015-02-26 06:58:17 -08001115}
1116
1117// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1118
senorblanco6bd51372015-04-15 07:32:27 -07001119void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -08001120 LOG("simplifying complex polygons\n");
senorblanco5b9f42c2015-04-16 11:47:18 -07001121 EdgeList activeEdges;
senorblancod6ed19c2015-02-26 06:58:17 -08001122 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1123 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1124 continue;
1125 }
1126#if LOGGING_ENABLED
1127 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1128#endif
senorblancod6ed19c2015-02-26 06:58:17 -08001129 Edge* leftEnclosingEdge = NULL;
1130 Edge* rightEnclosingEdge = NULL;
1131 bool restartChecks;
1132 do {
1133 restartChecks = false;
senorblanco5b9f42c2015-04-16 11:47:18 -07001134 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
senorblancod6ed19c2015-02-26 06:58:17 -08001135 if (v->fFirstEdgeBelow) {
1136 for (Edge* edge = v->fFirstEdgeBelow; edge != NULL; edge = edge->fNextEdgeBelow) {
senorblanco6bd51372015-04-15 07:32:27 -07001137 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
senorblancod6ed19c2015-02-26 06:58:17 -08001138 restartChecks = true;
1139 break;
1140 }
senorblanco6bd51372015-04-15 07:32:27 -07001141 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
senorblancod6ed19c2015-02-26 06:58:17 -08001142 restartChecks = true;
1143 break;
1144 }
1145 }
1146 } else {
1147 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
senorblanco6bd51372015-04-15 07:32:27 -07001148 &activeEdges, c, alloc)) {
1149 if (c.sweep_lt(pv->fPoint, v->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -08001150 v = pv;
1151 }
1152 restartChecks = true;
1153 }
1154
1155 }
1156 } while (restartChecks);
senorblancod6ed19c2015-02-26 06:58:17 -08001157 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1158 remove_edge(e, &activeEdges);
1159 }
1160 Edge* leftEdge = leftEnclosingEdge;
1161 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1162 insert_edge(e, leftEdge, &activeEdges);
1163 leftEdge = e;
1164 }
1165 v->fProcessed = true;
1166 }
1167}
1168
1169// Stage 5: Tessellate the simplified mesh into monotone polygons.
1170
1171Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) {
1172 LOG("tessellating simple polygons\n");
senorblanco5b9f42c2015-04-16 11:47:18 -07001173 EdgeList activeEdges;
senorblancod6ed19c2015-02-26 06:58:17 -08001174 Poly* polys = NULL;
1175 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1176 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1177 continue;
1178 }
1179#if LOGGING_ENABLED
1180 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1181#endif
senorblancod6ed19c2015-02-26 06:58:17 -08001182 Edge* leftEnclosingEdge = NULL;
1183 Edge* rightEnclosingEdge = NULL;
senorblanco5b9f42c2015-04-16 11:47:18 -07001184 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
senorblancod6ed19c2015-02-26 06:58:17 -08001185 Poly* leftPoly = NULL;
1186 Poly* rightPoly = NULL;
1187 if (v->fFirstEdgeAbove) {
1188 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1189 rightPoly = v->fLastEdgeAbove->fRightPoly;
1190 } else {
1191 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : NULL;
1192 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : NULL;
1193 }
1194#if LOGGING_ENABLED
1195 LOG("edges above:\n");
1196 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1197 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1198 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1199 }
1200 LOG("edges below:\n");
1201 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1202 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1203 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1204 }
1205#endif
1206 if (v->fFirstEdgeAbove) {
1207 if (leftPoly) {
1208 leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1209 }
1210 if (rightPoly) {
1211 rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1212 }
1213 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1214 Edge* leftEdge = e;
1215 Edge* rightEdge = e->fNextEdgeAbove;
1216 SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
1217 remove_edge(leftEdge, &activeEdges);
1218 if (leftEdge->fRightPoly) {
1219 leftEdge->fRightPoly->end(v, alloc);
1220 }
1221 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != leftEdge->fRightPoly) {
1222 rightEdge->fLeftPoly->end(v, alloc);
1223 }
1224 }
1225 remove_edge(v->fLastEdgeAbove, &activeEdges);
1226 if (!v->fFirstEdgeBelow) {
1227 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1228 SkASSERT(leftPoly->fPartner == NULL && rightPoly->fPartner == NULL);
1229 rightPoly->fPartner = leftPoly;
1230 leftPoly->fPartner = rightPoly;
1231 }
1232 }
1233 }
1234 if (v->fFirstEdgeBelow) {
1235 if (!v->fFirstEdgeAbove) {
1236 if (leftPoly && leftPoly == rightPoly) {
1237 // Split the poly.
1238 if (leftPoly->fActive->fSide == Poly::kLeft_Side) {
1239 leftPoly = new_poly(&polys, leftEnclosingEdge->fTop, leftPoly->fWinding,
1240 alloc);
1241 leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1242 rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1243 leftEnclosingEdge->fRightPoly = leftPoly;
1244 } else {
1245 rightPoly = new_poly(&polys, rightEnclosingEdge->fTop, rightPoly->fWinding,
1246 alloc);
1247 rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1248 leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1249 rightEnclosingEdge->fLeftPoly = rightPoly;
1250 }
1251 } else {
1252 if (leftPoly) {
1253 leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1254 }
1255 if (rightPoly) {
1256 rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1257 }
1258 }
1259 }
1260 Edge* leftEdge = v->fFirstEdgeBelow;
1261 leftEdge->fLeftPoly = leftPoly;
1262 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1263 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1264 rightEdge = rightEdge->fNextEdgeBelow) {
1265 insert_edge(rightEdge, leftEdge, &activeEdges);
1266 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1267 winding += leftEdge->fWinding;
1268 if (winding != 0) {
1269 Poly* poly = new_poly(&polys, v, winding, alloc);
1270 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1271 }
1272 leftEdge = rightEdge;
1273 }
1274 v->fLastEdgeBelow->fRightPoly = rightPoly;
1275 }
senorblancod6ed19c2015-02-26 06:58:17 -08001276#if LOGGING_ENABLED
1277 LOG("\nactive edges:\n");
senorblanco5b9f42c2015-04-16 11:47:18 -07001278 for (Edge* e = activeEdges->fHead; e != NULL; e = e->fRight) {
senorblancod6ed19c2015-02-26 06:58:17 -08001279 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1280 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1281 }
1282#endif
1283 }
1284 return polys;
1285}
1286
1287// This is a driver function which calls stages 2-5 in turn.
1288
senorblanco6bd51372015-04-15 07:32:27 -07001289Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -08001290#if LOGGING_ENABLED
1291 for (int i = 0; i < contourCnt; ++i) {
1292 Vertex* v = contours[i];
1293 SkASSERT(v);
1294 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1295 for (v = v->fNext; v != contours[i]; v = v->fNext) {
1296 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1297 }
1298 }
1299#endif
1300 sanitize_contours(contours, contourCnt);
senorblanco6bd51372015-04-15 07:32:27 -07001301 Vertex* vertices = build_edges(contours, contourCnt, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001302 if (!vertices) {
1303 return NULL;
1304 }
1305
1306 // Sort vertices in Y (secondarily in X).
senorblanco6bd51372015-04-15 07:32:27 -07001307 merge_sort(&vertices, c);
1308 merge_coincident_vertices(&vertices, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001309#if LOGGING_ENABLED
1310 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1311 static float gID = 0.0f;
1312 v->fID = gID++;
1313 }
1314#endif
senorblanco6bd51372015-04-15 07:32:27 -07001315 simplify(vertices, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001316 return tessellate(vertices, alloc);
1317}
1318
1319// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1320
1321void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, void* data) {
1322 void* d = data;
1323 for (Poly* poly = polys; poly; poly = poly->fNext) {
1324 if (apply_fill_type(fillType, poly->fWinding)) {
1325 d = poly->emit(d);
1326 }
1327 }
1328 return d;
1329}
1330
1331};
1332
1333GrTessellatingPathRenderer::GrTessellatingPathRenderer() {
1334}
1335
1336GrPathRenderer::StencilSupport GrTessellatingPathRenderer::onGetStencilSupport(
1337 const GrDrawTarget*,
1338 const GrPipelineBuilder*,
1339 const SkPath&,
1340 const SkStrokeRec&) const {
1341 return GrPathRenderer::kNoSupport_StencilSupport;
1342}
1343
1344bool GrTessellatingPathRenderer::canDrawPath(const GrDrawTarget* target,
1345 const GrPipelineBuilder* pipelineBuilder,
1346 const SkMatrix& viewMatrix,
1347 const SkPath& path,
1348 const SkStrokeRec& stroke,
1349 bool antiAlias) const {
1350 // This path renderer can draw all fill styles, but does not do antialiasing. It can do convex
1351 // and concave paths, but we'll leave the convex ones to simpler algorithms.
1352 return stroke.isFillStyle() && !antiAlias && !path.isConvex();
1353}
1354
senorblanco9ba39722015-03-05 07:13:42 -08001355class TessellatingPathBatch : public GrBatch {
1356public:
1357
1358 static GrBatch* Create(const GrColor& color,
1359 const SkPath& path,
1360 const SkMatrix& viewMatrix,
1361 SkRect clipBounds) {
1362 return SkNEW_ARGS(TessellatingPathBatch, (color, path, viewMatrix, clipBounds));
1363 }
1364
mtklein36352bf2015-03-25 18:17:31 -07001365 const char* name() const override { return "TessellatingPathBatch"; }
senorblanco9ba39722015-03-05 07:13:42 -08001366
mtklein36352bf2015-03-25 18:17:31 -07001367 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
senorblanco9ba39722015-03-05 07:13:42 -08001368 out->setKnownFourComponents(fColor);
1369 }
1370
mtklein36352bf2015-03-25 18:17:31 -07001371 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
senorblanco9ba39722015-03-05 07:13:42 -08001372 out->setUnknownSingleComponent();
1373 }
1374
mtklein36352bf2015-03-25 18:17:31 -07001375 void initBatchTracker(const GrPipelineInfo& init) override {
senorblanco9ba39722015-03-05 07:13:42 -08001376 // Handle any color overrides
1377 if (init.fColorIgnored) {
1378 fColor = GrColor_ILLEGAL;
1379 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
1380 fColor = init.fOverrideColor;
1381 }
1382 fPipelineInfo = init;
1383 }
1384
mtklein36352bf2015-03-25 18:17:31 -07001385 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
senorblanco6bd51372015-04-15 07:32:27 -07001386 SkRect pathBounds = fPath.getBounds();
1387 Comparator c;
1388 if (pathBounds.width() > pathBounds.height()) {
1389 c.sweep_lt = sweep_lt_horiz;
1390 c.sweep_gt = sweep_gt_horiz;
1391 } else {
1392 c.sweep_lt = sweep_lt_vert;
1393 c.sweep_gt = sweep_gt_vert;
1394 }
senorblanco2b4bb072015-04-22 13:45:18 -07001395 SkScalar screenSpaceTol = GrPathUtils::kDefaultTolerance;
1396 SkScalar tol = GrPathUtils::scaleToleranceToSrc(screenSpaceTol, fViewMatrix, pathBounds);
senorblanco9ba39722015-03-05 07:13:42 -08001397 int contourCnt;
1398 int maxPts = GrPathUtils::worstCasePointCount(fPath, &contourCnt, tol);
1399 if (maxPts <= 0) {
1400 return;
1401 }
1402 if (maxPts > ((int)SK_MaxU16 + 1)) {
1403 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1404 return;
1405 }
1406 SkPath::FillType fillType = fPath.getFillType();
1407 if (SkPath::IsInverseFillType(fillType)) {
1408 contourCnt++;
1409 }
1410
1411 LOG("got %d pts, %d contours\n", maxPts, contourCnt);
1412 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
1413 SkAutoTUnref<const GrGeometryProcessor> gp(
1414 GrDefaultGeoProcFactory::Create(flags, fColor, fViewMatrix, SkMatrix::I()));
1415 batchTarget->initDraw(gp, pipeline);
1416 gp->initBatchTracker(batchTarget->currentBatchTracker(), fPipelineInfo);
1417
1418 SkAutoTDeleteArray<Vertex*> contours(SkNEW_ARRAY(Vertex *, contourCnt));
1419
1420 // For the initial size of the chunk allocator, estimate based on the point count:
1421 // one vertex per point for the initial passes, plus two for the vertices in the
1422 // resulting Polys, since the same point may end up in two Polys. Assume minimal
1423 // connectivity of one Edge per Vertex (will grow for intersections).
1424 SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge)));
1425 path_to_contours(fPath, tol, fClipBounds, contours.get(), alloc);
1426 Poly* polys;
senorblanco6bd51372015-04-15 07:32:27 -07001427 polys = contours_to_polys(contours.get(), contourCnt, c, alloc);
senorblanco9ba39722015-03-05 07:13:42 -08001428 int count = 0;
1429 for (Poly* poly = polys; poly; poly = poly->fNext) {
1430 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1431 count += (poly->fCount - 2) * (WIREFRAME ? 6 : 3);
1432 }
1433 }
senorblancoe8331072015-03-26 14:52:45 -07001434 if (0 == count) {
1435 return;
1436 }
senorblanco9ba39722015-03-05 07:13:42 -08001437
1438 size_t stride = gp->getVertexStride();
1439 const GrVertexBuffer* vertexBuffer;
1440 int firstVertex;
1441 void* vertices = batchTarget->vertexPool()->makeSpace(stride,
1442 count,
1443 &vertexBuffer,
1444 &firstVertex);
joshualitt4b31de82015-03-05 14:33:41 -08001445
1446 if (!vertices) {
1447 SkDebugf("Could not allocate vertices\n");
1448 return;
1449 }
1450
senorblanco9ba39722015-03-05 07:13:42 -08001451 LOG("emitting %d verts\n", count);
1452 void* end = polys_to_triangles(polys, fillType, vertices);
1453 int actualCount = static_cast<int>(
1454 (static_cast<char*>(end) - static_cast<char*>(vertices)) / stride);
1455 LOG("actual count: %d\n", actualCount);
1456 SkASSERT(actualCount <= count);
1457
1458 GrPrimitiveType primitiveType = WIREFRAME ? kLines_GrPrimitiveType
1459 : kTriangles_GrPrimitiveType;
1460 GrDrawTarget::DrawInfo drawInfo;
1461 drawInfo.setPrimitiveType(primitiveType);
1462 drawInfo.setVertexBuffer(vertexBuffer);
1463 drawInfo.setStartVertex(firstVertex);
1464 drawInfo.setVertexCount(actualCount);
1465 drawInfo.setStartIndex(0);
1466 drawInfo.setIndexCount(0);
1467 batchTarget->draw(drawInfo);
1468
1469 batchTarget->putBackVertices((size_t)(count - actualCount), stride);
1470 return;
1471 }
1472
mtklein36352bf2015-03-25 18:17:31 -07001473 bool onCombineIfPossible(GrBatch*) override {
senorblanco9ba39722015-03-05 07:13:42 -08001474 return false;
1475 }
1476
1477private:
1478 TessellatingPathBatch(const GrColor& color,
1479 const SkPath& path,
1480 const SkMatrix& viewMatrix,
1481 const SkRect& clipBounds)
1482 : fColor(color)
1483 , fPath(path)
1484 , fViewMatrix(viewMatrix)
1485 , fClipBounds(clipBounds) {
1486 this->initClassID<TessellatingPathBatch>();
1487 }
1488
1489 GrColor fColor;
1490 SkPath fPath;
1491 SkMatrix fViewMatrix;
1492 SkRect fClipBounds; // in source space
1493 GrPipelineInfo fPipelineInfo;
1494};
1495
senorblancod6ed19c2015-02-26 06:58:17 -08001496bool GrTessellatingPathRenderer::onDrawPath(GrDrawTarget* target,
1497 GrPipelineBuilder* pipelineBuilder,
1498 GrColor color,
1499 const SkMatrix& viewM,
1500 const SkPath& path,
1501 const SkStrokeRec& stroke,
1502 bool antiAlias) {
1503 SkASSERT(!antiAlias);
1504 const GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
1505 if (NULL == rt) {
1506 return false;
1507 }
1508
senorblancod6ed19c2015-02-26 06:58:17 -08001509 SkIRect clipBoundsI;
1510 pipelineBuilder->clip().getConservativeBounds(rt, &clipBoundsI);
1511 SkRect clipBounds = SkRect::Make(clipBoundsI);
1512 SkMatrix vmi;
1513 if (!viewM.invert(&vmi)) {
1514 return false;
1515 }
1516 vmi.mapRect(&clipBounds);
senorblanco9ba39722015-03-05 07:13:42 -08001517 SkAutoTUnref<GrBatch> batch(TessellatingPathBatch::Create(color, path, viewM, clipBounds));
1518 target->drawBatch(pipelineBuilder, batch);
senorblancod6ed19c2015-02-26 06:58:17 -08001519
1520 return true;
1521}