blob: 0fc2b9edbe545e2cd195b2b4ea8f04b13bc69acf [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"
joshualitt2fbd4062015-05-07 13:06:41 -070012#include "GrBatchTest.h"
senorblancod6ed19c2015-02-26 06:58:17 -080013#include "GrDefaultGeoProcFactory.h"
14#include "GrPathUtils.h"
bsalomoncb8979d2015-05-05 09:51:38 -070015#include "GrVertices.h"
senorblancod6ed19c2015-02-26 06:58:17 -080016#include "SkChunkAlloc.h"
17#include "SkGeometry.h"
18
19#include <stdio.h>
20
21/*
22 * This path renderer tessellates the path into triangles, uploads the triangles to a
23 * vertex buffer, and renders them with a single draw call. It does not currently do
24 * antialiasing, so it must be used in conjunction with multisampling.
25 *
26 * There are six stages to the algorithm:
27 *
28 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
29 * 2) Build a mesh of edges connecting the vertices (build_edges()).
30 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
31 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
32 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
33 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
34 *
35 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
36 * of vertices (and the necessity of inserting new vertices on intersection).
37 *
38 * Stages (4) and (5) use an active edge list, which a list of all edges for which the
39 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
40 * left-to-right based on the point where both edges are active (when both top vertices
41 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
42 * (shared), it's sorted based on the last point where both edges are active, so the
43 * "upper" bottom vertex.
44 *
45 * The most complex step is the simplification (4). It's based on the Bentley-Ottman
46 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
47 * not exact and may violate the mesh topology or active edge list ordering. We
48 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
49 * points. This occurs in three ways:
50 *
51 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
52 * neighbouring edges at the top or bottom vertex. This is handled by merging the
53 * edges (merge_collinear_edges()).
54 * B) Intersections may cause an edge to violate the left-to-right ordering of the
55 * active edge list. This is handled by splitting the neighbour edge on the
56 * intersected vertex (cleanup_active_edges()).
57 * C) Shortening an edge may cause an active edge to become inactive or an inactive edge
58 * to become active. This is handled by removing or inserting the edge in the active
59 * edge list (fix_active_state()).
60 *
61 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
62 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
63 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
64 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
65 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
66 * insertions and removals was greater than the cost of infrequent O(N) lookups with the
67 * linked list implementation. With the latter, all removals are O(1), and most insertions
68 * are O(1), since we know the adjacent edge in the active edge list based on the topology.
69 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
70 * frequent. There may be other data structures worth investigating, however.
71 *
senorblanco6bd51372015-04-15 07:32:27 -070072 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
73 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
74 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
75 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
76 * that the "left" and "right" orientation in the code remains correct (edges to the left are
77 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
78 * degrees counterclockwise, rather that transposing.
senorblancod6ed19c2015-02-26 06:58:17 -080079 */
80#define LOGGING_ENABLED 0
81#define WIREFRAME 0
senorblancod6ed19c2015-02-26 06:58:17 -080082
83#if LOGGING_ENABLED
84#define LOG printf
85#else
86#define LOG(...)
87#endif
88
89#define ALLOC_NEW(Type, args, alloc) \
90 SkNEW_PLACEMENT_ARGS(alloc.allocThrow(sizeof(Type)), Type, args)
91
92namespace {
93
94struct Vertex;
95struct Edge;
96struct Poly;
97
98template <class T, T* T::*Prev, T* T::*Next>
99void insert(T* t, T* prev, T* next, T** head, T** tail) {
100 t->*Prev = prev;
101 t->*Next = next;
102 if (prev) {
103 prev->*Next = t;
104 } else if (head) {
105 *head = t;
106 }
107 if (next) {
108 next->*Prev = t;
109 } else if (tail) {
110 *tail = t;
111 }
112}
113
114template <class T, T* T::*Prev, T* T::*Next>
115void remove(T* t, T** head, T** tail) {
116 if (t->*Prev) {
117 t->*Prev->*Next = t->*Next;
118 } else if (head) {
119 *head = t->*Next;
120 }
121 if (t->*Next) {
122 t->*Next->*Prev = t->*Prev;
123 } else if (tail) {
124 *tail = t->*Prev;
125 }
126 t->*Prev = t->*Next = NULL;
127}
128
129/**
130 * Vertices are used in three ways: first, the path contours are converted into a
131 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
132 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
133 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
134 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
135 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
136 * an individual Vertex from the path mesh may belong to multiple
137 * MonotonePolys, so the original Vertices cannot be re-used.
138 */
139
140struct Vertex {
141 Vertex(const SkPoint& point)
142 : fPoint(point), fPrev(NULL), fNext(NULL)
143 , fFirstEdgeAbove(NULL), fLastEdgeAbove(NULL)
144 , fFirstEdgeBelow(NULL), fLastEdgeBelow(NULL)
145 , fProcessed(false)
146#if LOGGING_ENABLED
147 , fID (-1.0f)
148#endif
149 {}
150 SkPoint fPoint; // Vertex position
151 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
152 Vertex* fNext; // "
153 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
154 Edge* fLastEdgeAbove; // "
155 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
156 Edge* fLastEdgeBelow; // "
157 bool fProcessed; // Has this vertex been seen in simplify()?
158#if LOGGING_ENABLED
159 float fID; // Identifier used for logging.
160#endif
161};
162
163/***************************************************************************************/
164
senorblanco6bd51372015-04-15 07:32:27 -0700165typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
166
167struct Comparator {
168 CompareFunc sweep_lt;
169 CompareFunc sweep_gt;
170};
171
172bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
senorblancod6ed19c2015-02-26 06:58:17 -0800173 return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX;
senorblancod6ed19c2015-02-26 06:58:17 -0800174}
175
senorblanco6bd51372015-04-15 07:32:27 -0700176bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
177 return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY;
178}
179
180bool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) {
senorblancod6ed19c2015-02-26 06:58:17 -0800181 return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX;
senorblanco6bd51372015-04-15 07:32:27 -0700182}
183
184bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
senorblancod6ed19c2015-02-26 06:58:17 -0800185 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
senorblancod6ed19c2015-02-26 06:58:17 -0800186}
187
188inline void* emit_vertex(Vertex* v, void* data) {
189 SkPoint* d = static_cast<SkPoint*>(data);
190 *d++ = v->fPoint;
191 return d;
192}
193
194void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, void* data) {
195#if WIREFRAME
196 data = emit_vertex(v0, data);
197 data = emit_vertex(v1, data);
198 data = emit_vertex(v1, data);
199 data = emit_vertex(v2, data);
200 data = emit_vertex(v2, data);
201 data = emit_vertex(v0, data);
202#else
203 data = emit_vertex(v0, data);
204 data = emit_vertex(v1, data);
205 data = emit_vertex(v2, data);
206#endif
207 return data;
208}
209
senorblanco5b9f42c2015-04-16 11:47:18 -0700210struct EdgeList {
211 EdgeList() : fHead(NULL), fTail(NULL) {}
212 Edge* fHead;
213 Edge* fTail;
214};
215
senorblancod6ed19c2015-02-26 06:58:17 -0800216/**
217 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
218 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
219 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
220 * point). For speed, that case is only tested by the callers which require it (e.g.,
221 * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
222 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
223 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
224 * a lot faster in the "not found" case.
225 *
226 * The coefficients of the line equation stored in double precision to avoid catastrphic
227 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
228 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
229 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
230 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
231 * this file).
232 */
233
234struct Edge {
235 Edge(Vertex* top, Vertex* bottom, int winding)
236 : fWinding(winding)
237 , fTop(top)
238 , fBottom(bottom)
239 , fLeft(NULL)
240 , fRight(NULL)
241 , fPrevEdgeAbove(NULL)
242 , fNextEdgeAbove(NULL)
243 , fPrevEdgeBelow(NULL)
244 , fNextEdgeBelow(NULL)
245 , fLeftPoly(NULL)
246 , fRightPoly(NULL) {
247 recompute();
248 }
249 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
250 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
251 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
252 Edge* fLeft; // The linked list of edges in the active edge list.
253 Edge* fRight; // "
254 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
255 Edge* fNextEdgeAbove; // "
256 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
257 Edge* fNextEdgeBelow; // "
258 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
259 Poly* fRightPoly; // The Poly to the right of this edge, if any.
260 double fDX; // The line equation for this edge, in implicit form.
261 double fDY; // fDY * x + fDX * y + fC = 0, for point (x, y) on the line.
262 double fC;
263 double dist(const SkPoint& p) const {
264 return fDY * p.fX - fDX * p.fY + fC;
265 }
266 bool isRightOf(Vertex* v) const {
267 return dist(v->fPoint) < 0.0;
268 }
269 bool isLeftOf(Vertex* v) const {
270 return dist(v->fPoint) > 0.0;
271 }
272 void recompute() {
273 fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX;
274 fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY;
275 fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX -
276 static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY;
277 }
278 bool intersect(const Edge& other, SkPoint* p) {
279 LOG("intersecting %g -> %g with %g -> %g\n",
280 fTop->fID, fBottom->fID,
281 other.fTop->fID, other.fBottom->fID);
282 if (fTop == other.fTop || fBottom == other.fBottom) {
283 return false;
284 }
285 double denom = fDX * other.fDY - fDY * other.fDX;
286 if (denom == 0.0) {
287 return false;
288 }
289 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
290 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
291 double sNumer = dy * other.fDX - dx * other.fDY;
292 double tNumer = dy * fDX - dx * fDY;
293 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
294 // This saves us doing the divide below unless absolutely necessary.
295 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
296 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
297 return false;
298 }
299 double s = sNumer / denom;
300 SkASSERT(s >= 0.0 && s <= 1.0);
301 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
302 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
303 return true;
304 }
senorblanco5b9f42c2015-04-16 11:47:18 -0700305 bool isActive(EdgeList* activeEdges) const {
306 return activeEdges && (fLeft || fRight || activeEdges->fHead == this);
senorblancod6ed19c2015-02-26 06:58:17 -0800307 }
308};
309
310/***************************************************************************************/
311
312struct Poly {
313 Poly(int winding)
314 : fWinding(winding)
315 , fHead(NULL)
316 , fTail(NULL)
317 , fActive(NULL)
318 , fNext(NULL)
319 , fPartner(NULL)
320 , fCount(0)
321 {
322#if LOGGING_ENABLED
323 static int gID = 0;
324 fID = gID++;
325 LOG("*** created Poly %d\n", fID);
326#endif
327 }
328 typedef enum { kNeither_Side, kLeft_Side, kRight_Side } Side;
329 struct MonotonePoly {
330 MonotonePoly()
331 : fSide(kNeither_Side)
332 , fHead(NULL)
333 , fTail(NULL)
334 , fPrev(NULL)
335 , fNext(NULL) {}
336 Side fSide;
337 Vertex* fHead;
338 Vertex* fTail;
339 MonotonePoly* fPrev;
340 MonotonePoly* fNext;
341 bool addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) {
342 Vertex* newV = ALLOC_NEW(Vertex, (v->fPoint), alloc);
343 bool done = false;
344 if (fSide == kNeither_Side) {
345 fSide = side;
346 } else {
347 done = side != fSide;
348 }
349 if (fHead == NULL) {
350 fHead = fTail = newV;
351 } else if (fSide == kRight_Side) {
352 newV->fPrev = fTail;
353 fTail->fNext = newV;
354 fTail = newV;
355 } else {
356 newV->fNext = fHead;
357 fHead->fPrev = newV;
358 fHead = newV;
359 }
360 return done;
361 }
362
363 void* emit(void* data) {
364 Vertex* first = fHead;
365 Vertex* v = first->fNext;
366 while (v != fTail) {
367 SkASSERT(v && v->fPrev && v->fNext);
senorblancod6ed19c2015-02-26 06:58:17 -0800368 Vertex* prev = v->fPrev;
369 Vertex* curr = v;
370 Vertex* next = v->fNext;
371 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
372 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
373 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
374 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
375 if (ax * by - ay * bx >= 0.0) {
376 data = emit_triangle(prev, curr, next, data);
377 v->fPrev->fNext = v->fNext;
378 v->fNext->fPrev = v->fPrev;
379 if (v->fPrev == first) {
380 v = v->fNext;
381 } else {
382 v = v->fPrev;
383 }
384 } else {
385 v = v->fNext;
senorblancod6ed19c2015-02-26 06:58:17 -0800386 }
387 }
388 return data;
389 }
senorblancod6ed19c2015-02-26 06:58:17 -0800390 };
391 Poly* addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) {
392 LOG("addVertex() to %d at %g (%g, %g), %s side\n", fID, v->fID, v->fPoint.fX, v->fPoint.fY,
393 side == kLeft_Side ? "left" : side == kRight_Side ? "right" : "neither");
394 Poly* partner = fPartner;
395 Poly* poly = this;
396 if (partner) {
397 fPartner = partner->fPartner = NULL;
398 }
399 if (!fActive) {
400 fActive = ALLOC_NEW(MonotonePoly, (), alloc);
401 }
402 if (fActive->addVertex(v, side, alloc)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800403 if (fTail) {
404 fActive->fPrev = fTail;
405 fTail->fNext = fActive;
406 fTail = fActive;
407 } else {
408 fHead = fTail = fActive;
409 }
410 if (partner) {
411 partner->addVertex(v, side, alloc);
412 poly = partner;
413 } else {
414 Vertex* prev = fActive->fSide == Poly::kLeft_Side ?
415 fActive->fHead->fNext : fActive->fTail->fPrev;
416 fActive = ALLOC_NEW(MonotonePoly, , alloc);
417 fActive->addVertex(prev, Poly::kNeither_Side, alloc);
418 fActive->addVertex(v, side, alloc);
419 }
420 }
421 fCount++;
422 return poly;
423 }
424 void end(Vertex* v, SkChunkAlloc& alloc) {
425 LOG("end() %d at %g, %g\n", fID, v->fPoint.fX, v->fPoint.fY);
426 if (fPartner) {
427 fPartner = fPartner->fPartner = NULL;
428 }
429 addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, alloc);
430 }
431 void* emit(void *data) {
432 if (fCount < 3) {
433 return data;
434 }
435 LOG("emit() %d, size %d\n", fID, fCount);
436 for (MonotonePoly* m = fHead; m != NULL; m = m->fNext) {
437 data = m->emit(data);
438 }
439 return data;
440 }
441 int fWinding;
442 MonotonePoly* fHead;
443 MonotonePoly* fTail;
444 MonotonePoly* fActive;
445 Poly* fNext;
446 Poly* fPartner;
447 int fCount;
448#if LOGGING_ENABLED
449 int fID;
450#endif
451};
452
453/***************************************************************************************/
454
455bool coincident(const SkPoint& a, const SkPoint& b) {
456 return a == b;
457}
458
459Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
460 Poly* poly = ALLOC_NEW(Poly, (winding), alloc);
461 poly->addVertex(v, Poly::kNeither_Side, alloc);
462 poly->fNext = *head;
463 *head = poly;
464 return poly;
465}
466
senorblancod6ed19c2015-02-26 06:58:17 -0800467Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
468 SkChunkAlloc& alloc) {
469 Vertex* v = ALLOC_NEW(Vertex, (p), alloc);
470#if LOGGING_ENABLED
471 static float gID = 0.0f;
472 v->fID = gID++;
473#endif
474 if (prev) {
475 prev->fNext = v;
476 v->fPrev = prev;
477 } else {
478 *head = v;
479 }
480 return v;
481}
482
483Vertex* generate_quadratic_points(const SkPoint& p0,
484 const SkPoint& p1,
485 const SkPoint& p2,
486 SkScalar tolSqd,
487 Vertex* prev,
488 Vertex** head,
489 int pointsLeft,
490 SkChunkAlloc& alloc) {
491 SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
492 if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
493 return append_point_to_contour(p2, prev, head, alloc);
494 }
495
496 const SkPoint q[] = {
497 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
498 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
499 };
500 const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
501
502 pointsLeft >>= 1;
503 prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
504 prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
505 return prev;
506}
507
508Vertex* generate_cubic_points(const SkPoint& p0,
509 const SkPoint& p1,
510 const SkPoint& p2,
511 const SkPoint& p3,
512 SkScalar tolSqd,
513 Vertex* prev,
514 Vertex** head,
515 int pointsLeft,
516 SkChunkAlloc& alloc) {
517 SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
518 SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
519 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
520 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
521 return append_point_to_contour(p3, prev, head, alloc);
522 }
523 const SkPoint q[] = {
524 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
525 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
526 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
527 };
528 const SkPoint r[] = {
529 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
530 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
531 };
532 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
533 pointsLeft >>= 1;
534 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
535 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
536 return prev;
537}
538
539// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
540
541void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
542 Vertex** contours, SkChunkAlloc& alloc) {
543
544 SkScalar toleranceSqd = tolerance * tolerance;
545
546 SkPoint pts[4];
547 bool done = false;
548 SkPath::Iter iter(path, false);
549 Vertex* prev = NULL;
550 Vertex* head = NULL;
551 if (path.isInverseFillType()) {
552 SkPoint quad[4];
553 clipBounds.toQuad(quad);
554 for (int i = 3; i >= 0; i--) {
555 prev = append_point_to_contour(quad[i], prev, &head, alloc);
556 }
557 head->fPrev = prev;
558 prev->fNext = head;
559 *contours++ = head;
560 head = prev = NULL;
561 }
562 SkAutoConicToQuads converter;
563 while (!done) {
564 SkPath::Verb verb = iter.next(pts);
565 switch (verb) {
566 case SkPath::kConic_Verb: {
567 SkScalar weight = iter.conicWeight();
568 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
569 for (int i = 0; i < converter.countQuads(); ++i) {
senorblanco6d88bdb2015-04-20 05:41:48 -0700570 int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
senorblancod6ed19c2015-02-26 06:58:17 -0800571 prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
572 toleranceSqd, prev, &head, pointsLeft, alloc);
573 quadPts += 2;
574 }
575 break;
576 }
577 case SkPath::kMove_Verb:
578 if (head) {
579 head->fPrev = prev;
580 prev->fNext = head;
581 *contours++ = head;
582 }
583 head = prev = NULL;
584 prev = append_point_to_contour(pts[0], prev, &head, alloc);
585 break;
586 case SkPath::kLine_Verb: {
587 prev = append_point_to_contour(pts[1], prev, &head, alloc);
588 break;
589 }
590 case SkPath::kQuad_Verb: {
senorblanco6d88bdb2015-04-20 05:41:48 -0700591 int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
senorblancod6ed19c2015-02-26 06:58:17 -0800592 prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
593 &head, pointsLeft, alloc);
594 break;
595 }
596 case SkPath::kCubic_Verb: {
senorblanco6d88bdb2015-04-20 05:41:48 -0700597 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
senorblancod6ed19c2015-02-26 06:58:17 -0800598 prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
599 toleranceSqd, prev, &head, pointsLeft, alloc);
600 break;
601 }
602 case SkPath::kClose_Verb:
603 if (head) {
604 head->fPrev = prev;
605 prev->fNext = head;
606 *contours++ = head;
607 }
608 head = prev = NULL;
609 break;
610 case SkPath::kDone_Verb:
611 if (head) {
612 head->fPrev = prev;
613 prev->fNext = head;
614 *contours++ = head;
615 }
616 done = true;
617 break;
618 }
619 }
620}
621
622inline bool apply_fill_type(SkPath::FillType fillType, int winding) {
623 switch (fillType) {
624 case SkPath::kWinding_FillType:
625 return winding != 0;
626 case SkPath::kEvenOdd_FillType:
627 return (winding & 1) != 0;
628 case SkPath::kInverseWinding_FillType:
629 return winding == 1;
630 case SkPath::kInverseEvenOdd_FillType:
631 return (winding & 1) == 1;
632 default:
633 SkASSERT(false);
634 return false;
635 }
636}
637
senorblanco6bd51372015-04-15 07:32:27 -0700638Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c) {
639 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
senorblancod6ed19c2015-02-26 06:58:17 -0800640 Vertex* top = winding < 0 ? next : prev;
641 Vertex* bottom = winding < 0 ? prev : next;
642 return ALLOC_NEW(Edge, (top, bottom, winding), alloc);
643}
644
senorblanco5b9f42c2015-04-16 11:47:18 -0700645void remove_edge(Edge* edge, EdgeList* edges) {
senorblancod6ed19c2015-02-26 06:58:17 -0800646 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblanco5b9f42c2015-04-16 11:47:18 -0700647 SkASSERT(edge->isActive(edges));
648 remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &edges->fHead, &edges->fTail);
senorblancod6ed19c2015-02-26 06:58:17 -0800649}
650
senorblanco5b9f42c2015-04-16 11:47:18 -0700651void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
senorblancod6ed19c2015-02-26 06:58:17 -0800652 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
senorblanco5b9f42c2015-04-16 11:47:18 -0700653 SkASSERT(!edge->isActive(edges));
654 Edge* next = prev ? prev->fRight : edges->fHead;
655 insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &edges->fHead, &edges->fTail);
senorblancod6ed19c2015-02-26 06:58:17 -0800656}
657
senorblanco5b9f42c2015-04-16 11:47:18 -0700658void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
senorblancod6ed19c2015-02-26 06:58:17 -0800659 if (v->fFirstEdgeAbove) {
660 *left = v->fFirstEdgeAbove->fLeft;
661 *right = v->fLastEdgeAbove->fRight;
662 return;
663 }
senorblanco5b9f42c2015-04-16 11:47:18 -0700664 Edge* next = NULL;
665 Edge* prev;
666 for (prev = edges->fTail; prev != NULL; prev = prev->fLeft) {
667 if (prev->isLeftOf(v)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800668 break;
669 }
senorblanco5b9f42c2015-04-16 11:47:18 -0700670 next = prev;
senorblancod6ed19c2015-02-26 06:58:17 -0800671 }
672 *left = prev;
673 *right = next;
674 return;
675}
676
senorblanco5b9f42c2015-04-16 11:47:18 -0700677void find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
senorblancod6ed19c2015-02-26 06:58:17 -0800678 Edge* prev = NULL;
679 Edge* next;
senorblanco5b9f42c2015-04-16 11:47:18 -0700680 for (next = edges->fHead; next != NULL; next = next->fRight) {
senorblanco6bd51372015-04-15 07:32:27 -0700681 if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
682 (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
683 (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800684 next->isRightOf(edge->fBottom)) ||
senorblanco6bd51372015-04-15 07:32:27 -0700685 (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800686 edge->isLeftOf(next->fBottom))) {
687 break;
688 }
689 prev = next;
690 }
691 *left = prev;
692 *right = next;
693 return;
694}
695
senorblanco5b9f42c2015-04-16 11:47:18 -0700696void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800697 if (edge->isActive(activeEdges)) {
698 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
699 remove_edge(edge, activeEdges);
700 }
701 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
702 Edge* left;
703 Edge* right;
senorblanco5b9f42c2015-04-16 11:47:18 -0700704 find_enclosing_edges(edge, activeEdges, c, &left, &right);
senorblancod6ed19c2015-02-26 06:58:17 -0800705 insert_edge(edge, left, activeEdges);
706 }
707}
708
senorblanco6bd51372015-04-15 07:32:27 -0700709void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800710 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
senorblanco6bd51372015-04-15 07:32:27 -0700711 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800712 return;
713 }
714 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
715 Edge* prev = NULL;
716 Edge* next;
717 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
718 if (next->isRightOf(edge->fTop)) {
719 break;
720 }
721 prev = next;
722 }
723 insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
724 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
725}
726
senorblanco6bd51372015-04-15 07:32:27 -0700727void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800728 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
senorblanco6bd51372015-04-15 07:32:27 -0700729 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800730 return;
731 }
732 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
733 Edge* prev = NULL;
734 Edge* next;
735 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
736 if (next->isRightOf(edge->fBottom)) {
737 break;
738 }
739 prev = next;
740 }
741 insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
742 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
743}
744
745void remove_edge_above(Edge* edge) {
746 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
747 edge->fBottom->fID);
748 remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
749 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
750}
751
752void remove_edge_below(Edge* edge) {
753 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
754 edge->fTop->fID);
755 remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
756 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
757}
758
senorblanco5b9f42c2015-04-16 11:47:18 -0700759void erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) {
senorblancod6ed19c2015-02-26 06:58:17 -0800760 if (edge->fWinding != 0) {
761 return;
762 }
763 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
764 remove_edge_above(edge);
765 remove_edge_below(edge);
senorblanco5b9f42c2015-04-16 11:47:18 -0700766 if (edge->isActive(edges)) {
767 remove_edge(edge, edges);
senorblancod6ed19c2015-02-26 06:58:17 -0800768 }
769}
770
senorblanco5b9f42c2015-04-16 11:47:18 -0700771void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
senorblancod6ed19c2015-02-26 06:58:17 -0800772
senorblanco5b9f42c2015-04-16 11:47:18 -0700773void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800774 remove_edge_below(edge);
775 edge->fTop = v;
776 edge->recompute();
senorblanco6bd51372015-04-15 07:32:27 -0700777 insert_edge_below(edge, v, c);
778 fix_active_state(edge, activeEdges, c);
779 merge_collinear_edges(edge, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800780}
781
senorblanco5b9f42c2015-04-16 11:47:18 -0700782void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800783 remove_edge_above(edge);
784 edge->fBottom = v;
785 edge->recompute();
senorblanco6bd51372015-04-15 07:32:27 -0700786 insert_edge_above(edge, v, c);
787 fix_active_state(edge, activeEdges, c);
788 merge_collinear_edges(edge, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800789}
790
senorblanco5b9f42c2015-04-16 11:47:18 -0700791void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800792 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
793 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
794 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
795 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
796 other->fWinding += edge->fWinding;
797 erase_edge_if_zero_winding(other, activeEdges);
798 edge->fWinding = 0;
799 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700800 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800801 other->fWinding += edge->fWinding;
802 erase_edge_if_zero_winding(other, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700803 set_bottom(edge, other->fTop, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800804 } else {
805 edge->fWinding += other->fWinding;
806 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700807 set_bottom(other, edge->fTop, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800808 }
809}
810
senorblanco5b9f42c2015-04-16 11:47:18 -0700811void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800812 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
813 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
814 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
815 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
816 other->fWinding += edge->fWinding;
817 erase_edge_if_zero_winding(other, activeEdges);
818 edge->fWinding = 0;
819 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700820 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800821 edge->fWinding += other->fWinding;
822 erase_edge_if_zero_winding(edge, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700823 set_top(other, edge->fBottom, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800824 } else {
825 other->fWinding += edge->fWinding;
826 erase_edge_if_zero_winding(other, activeEdges);
senorblanco6bd51372015-04-15 07:32:27 -0700827 set_top(edge, other->fBottom, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800828 }
829}
830
senorblanco5b9f42c2015-04-16 11:47:18 -0700831void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -0800832 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
833 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
senorblanco6bd51372015-04-15 07:32:27 -0700834 merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800835 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
836 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
senorblanco6bd51372015-04-15 07:32:27 -0700837 merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800838 }
839 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
840 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
senorblanco6bd51372015-04-15 07:32:27 -0700841 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800842 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
843 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
senorblanco6bd51372015-04-15 07:32:27 -0700844 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800845 }
846}
847
senorblanco5b9f42c2015-04-16 11:47:18 -0700848void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800849
senorblanco5b9f42c2015-04-16 11:47:18 -0700850void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800851 Vertex* top = edge->fTop;
852 Vertex* bottom = edge->fBottom;
853 if (edge->fLeft) {
854 Vertex* leftTop = edge->fLeft->fTop;
855 Vertex* leftBottom = edge->fLeft->fBottom;
senorblanco6bd51372015-04-15 07:32:27 -0700856 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
857 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
858 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
859 split_edge(edge, leftTop, activeEdges, c, alloc);
860 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
861 !edge->fLeft->isLeftOf(bottom)) {
862 split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
863 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
864 split_edge(edge, leftBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800865 }
866 }
867 if (edge->fRight) {
868 Vertex* rightTop = edge->fRight->fTop;
869 Vertex* rightBottom = edge->fRight->fBottom;
senorblanco6bd51372015-04-15 07:32:27 -0700870 if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
871 split_edge(edge->fRight, top, activeEdges, c, alloc);
872 } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
873 split_edge(edge, rightTop, activeEdges, c, alloc);
874 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800875 !edge->fRight->isRightOf(bottom)) {
senorblanco6bd51372015-04-15 07:32:27 -0700876 split_edge(edge->fRight, bottom, activeEdges, c, alloc);
877 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
senorblancod6ed19c2015-02-26 06:58:17 -0800878 !edge->isLeftOf(rightBottom)) {
senorblanco6bd51372015-04-15 07:32:27 -0700879 split_edge(edge, rightBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800880 }
881 }
882}
883
senorblanco5b9f42c2015-04-16 11:47:18 -0700884void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800885 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
886 edge->fTop->fID, edge->fBottom->fID,
887 v->fID, v->fPoint.fX, v->fPoint.fY);
senorblanco6bd51372015-04-15 07:32:27 -0700888 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
889 set_top(edge, v, activeEdges, c);
890 } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
891 set_bottom(edge, v, activeEdges, c);
senorblancoa2b6d282015-03-02 09:34:13 -0800892 } else {
893 Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), alloc);
senorblanco6bd51372015-04-15 07:32:27 -0700894 insert_edge_below(newEdge, v, c);
895 insert_edge_above(newEdge, edge->fBottom, c);
896 set_bottom(edge, v, activeEdges, c);
897 cleanup_active_edges(edge, activeEdges, c, alloc);
898 fix_active_state(newEdge, activeEdges, c);
899 merge_collinear_edges(newEdge, activeEdges, c);
senorblancoa2b6d282015-03-02 09:34:13 -0800900 }
senorblancod6ed19c2015-02-26 06:58:17 -0800901}
902
senorblanco6bd51372015-04-15 07:32:27 -0700903void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800904 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
905 src->fID, dst->fID);
906 for (Edge* edge = src->fFirstEdgeAbove; edge;) {
907 Edge* next = edge->fNextEdgeAbove;
senorblanco6bd51372015-04-15 07:32:27 -0700908 set_bottom(edge, dst, NULL, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800909 edge = next;
910 }
911 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
912 Edge* next = edge->fNextEdgeBelow;
senorblanco6bd51372015-04-15 07:32:27 -0700913 set_top(edge, dst, NULL, c);
senorblancod6ed19c2015-02-26 06:58:17 -0800914 edge = next;
915 }
916 remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, NULL);
917}
918
senorblanco5b9f42c2015-04-16 11:47:18 -0700919Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
senorblanco6bd51372015-04-15 07:32:27 -0700920 SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800921 SkPoint p;
922 if (!edge || !other) {
923 return NULL;
924 }
925 if (edge->intersect(*other, &p)) {
926 Vertex* v;
927 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
senorblanco6bd51372015-04-15 07:32:27 -0700928 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
929 split_edge(other, edge->fTop, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800930 v = edge->fTop;
senorblanco6bd51372015-04-15 07:32:27 -0700931 } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) {
932 split_edge(other, edge->fBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800933 v = edge->fBottom;
senorblanco6bd51372015-04-15 07:32:27 -0700934 } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
935 split_edge(edge, other->fTop, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800936 v = other->fTop;
senorblanco6bd51372015-04-15 07:32:27 -0700937 } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) {
938 split_edge(edge, other->fBottom, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800939 v = other->fBottom;
940 } else {
941 Vertex* nextV = edge->fTop;
senorblanco6bd51372015-04-15 07:32:27 -0700942 while (c.sweep_lt(p, nextV->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800943 nextV = nextV->fPrev;
944 }
senorblanco6bd51372015-04-15 07:32:27 -0700945 while (c.sweep_lt(nextV->fPoint, p)) {
senorblancod6ed19c2015-02-26 06:58:17 -0800946 nextV = nextV->fNext;
947 }
948 Vertex* prevV = nextV->fPrev;
949 if (coincident(prevV->fPoint, p)) {
950 v = prevV;
951 } else if (coincident(nextV->fPoint, p)) {
952 v = nextV;
953 } else {
954 v = ALLOC_NEW(Vertex, (p), alloc);
955 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
956 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
957 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
958#if LOGGING_ENABLED
959 v->fID = (nextV->fID + prevV->fID) * 0.5f;
960#endif
961 v->fPrev = prevV;
962 v->fNext = nextV;
963 prevV->fNext = v;
964 nextV->fPrev = v;
965 }
senorblanco6bd51372015-04-15 07:32:27 -0700966 split_edge(edge, v, activeEdges, c, alloc);
967 split_edge(other, v, activeEdges, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -0800968 }
senorblancod6ed19c2015-02-26 06:58:17 -0800969 return v;
970 }
971 return NULL;
972}
973
974void sanitize_contours(Vertex** contours, int contourCnt) {
975 for (int i = 0; i < contourCnt; ++i) {
976 SkASSERT(contours[i]);
977 for (Vertex* v = contours[i];;) {
978 if (coincident(v->fPrev->fPoint, v->fPoint)) {
979 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
980 if (v->fPrev == v) {
981 contours[i] = NULL;
982 break;
983 }
984 v->fPrev->fNext = v->fNext;
985 v->fNext->fPrev = v->fPrev;
986 if (contours[i] == v) {
987 contours[i] = v->fNext;
988 }
989 v = v->fPrev;
990 } else {
991 v = v->fNext;
992 if (v == contours[i]) break;
993 }
994 }
995 }
996}
997
senorblanco6bd51372015-04-15 07:32:27 -0700998void merge_coincident_vertices(Vertex** vertices, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -0800999 for (Vertex* v = (*vertices)->fNext; v != NULL; v = v->fNext) {
senorblanco6bd51372015-04-15 07:32:27 -07001000 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -08001001 v->fPoint = v->fPrev->fPoint;
1002 }
1003 if (coincident(v->fPrev->fPoint, v->fPoint)) {
senorblanco6bd51372015-04-15 07:32:27 -07001004 merge_vertices(v->fPrev, v, vertices, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001005 }
1006 }
1007}
1008
1009// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1010
senorblanco6bd51372015-04-15 07:32:27 -07001011Vertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -08001012 Vertex* vertices = NULL;
1013 Vertex* prev = NULL;
1014 for (int i = 0; i < contourCnt; ++i) {
1015 for (Vertex* v = contours[i]; v != NULL;) {
1016 Vertex* vNext = v->fNext;
senorblanco6bd51372015-04-15 07:32:27 -07001017 Edge* edge = new_edge(v->fPrev, v, alloc, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001018 if (edge->fWinding > 0) {
senorblanco6bd51372015-04-15 07:32:27 -07001019 insert_edge_below(edge, v->fPrev, c);
1020 insert_edge_above(edge, v, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001021 } else {
senorblanco6bd51372015-04-15 07:32:27 -07001022 insert_edge_below(edge, v, c);
1023 insert_edge_above(edge, v->fPrev, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001024 }
senorblanco6bd51372015-04-15 07:32:27 -07001025 merge_collinear_edges(edge, NULL, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001026 if (prev) {
1027 prev->fNext = v;
1028 v->fPrev = prev;
1029 } else {
1030 vertices = v;
1031 }
1032 prev = v;
1033 v = vNext;
1034 if (v == contours[i]) break;
1035 }
1036 }
1037 if (prev) {
1038 prev->fNext = vertices->fPrev = NULL;
1039 }
1040 return vertices;
1041}
1042
senorblanco6bd51372015-04-15 07:32:27 -07001043// Stage 3: sort the vertices by increasing sweep direction.
senorblancod6ed19c2015-02-26 06:58:17 -08001044
senorblanco6bd51372015-04-15 07:32:27 -07001045Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c);
senorblancod6ed19c2015-02-26 06:58:17 -08001046
1047void front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) {
1048 Vertex* fast;
1049 Vertex* slow;
1050 if (!v || !v->fNext) {
1051 *pFront = v;
1052 *pBack = NULL;
1053 } else {
1054 slow = v;
1055 fast = v->fNext;
1056
1057 while (fast != NULL) {
1058 fast = fast->fNext;
1059 if (fast != NULL) {
1060 slow = slow->fNext;
1061 fast = fast->fNext;
1062 }
1063 }
1064
1065 *pFront = v;
1066 *pBack = slow->fNext;
1067 slow->fNext->fPrev = NULL;
1068 slow->fNext = NULL;
1069 }
1070}
1071
senorblanco6bd51372015-04-15 07:32:27 -07001072void merge_sort(Vertex** head, Comparator& c) {
senorblancod6ed19c2015-02-26 06:58:17 -08001073 if (!*head || !(*head)->fNext) {
1074 return;
1075 }
1076
1077 Vertex* a;
1078 Vertex* b;
1079 front_back_split(*head, &a, &b);
1080
senorblanco6bd51372015-04-15 07:32:27 -07001081 merge_sort(&a, c);
1082 merge_sort(&b, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001083
senorblanco6bd51372015-04-15 07:32:27 -07001084 *head = sorted_merge(a, b, c);
senorblancod6ed19c2015-02-26 06:58:17 -08001085}
1086
senorblanco7ef63c82015-04-13 14:27:37 -07001087inline void append_vertex(Vertex* v, Vertex** head, Vertex** tail) {
1088 insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, NULL, head, tail);
1089}
1090
1091inline void append_vertex_list(Vertex* v, Vertex** head, Vertex** tail) {
1092 insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, v->fNext, head, tail);
1093}
1094
senorblanco6bd51372015-04-15 07:32:27 -07001095Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c) {
senorblanco7ef63c82015-04-13 14:27:37 -07001096 Vertex* head = NULL;
1097 Vertex* tail = NULL;
senorblancod6ed19c2015-02-26 06:58:17 -08001098
senorblanco7ef63c82015-04-13 14:27:37 -07001099 while (a && b) {
senorblanco6bd51372015-04-15 07:32:27 -07001100 if (c.sweep_lt(a->fPoint, b->fPoint)) {
senorblanco7ef63c82015-04-13 14:27:37 -07001101 Vertex* next = a->fNext;
1102 append_vertex(a, &head, &tail);
1103 a = next;
1104 } else {
1105 Vertex* next = b->fNext;
1106 append_vertex(b, &head, &tail);
1107 b = next;
1108 }
senorblancod6ed19c2015-02-26 06:58:17 -08001109 }
senorblanco7ef63c82015-04-13 14:27:37 -07001110 if (a) {
1111 append_vertex_list(a, &head, &tail);
1112 }
1113 if (b) {
1114 append_vertex_list(b, &head, &tail);
1115 }
1116 return head;
senorblancod6ed19c2015-02-26 06:58:17 -08001117}
1118
1119// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1120
senorblanco6bd51372015-04-15 07:32:27 -07001121void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -08001122 LOG("simplifying complex polygons\n");
senorblanco5b9f42c2015-04-16 11:47:18 -07001123 EdgeList activeEdges;
senorblancod6ed19c2015-02-26 06:58:17 -08001124 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1125 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1126 continue;
1127 }
1128#if LOGGING_ENABLED
1129 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1130#endif
senorblancod6ed19c2015-02-26 06:58:17 -08001131 Edge* leftEnclosingEdge = NULL;
1132 Edge* rightEnclosingEdge = NULL;
1133 bool restartChecks;
1134 do {
1135 restartChecks = false;
senorblanco5b9f42c2015-04-16 11:47:18 -07001136 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
senorblancod6ed19c2015-02-26 06:58:17 -08001137 if (v->fFirstEdgeBelow) {
1138 for (Edge* edge = v->fFirstEdgeBelow; edge != NULL; edge = edge->fNextEdgeBelow) {
senorblanco6bd51372015-04-15 07:32:27 -07001139 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
senorblancod6ed19c2015-02-26 06:58:17 -08001140 restartChecks = true;
1141 break;
1142 }
senorblanco6bd51372015-04-15 07:32:27 -07001143 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
senorblancod6ed19c2015-02-26 06:58:17 -08001144 restartChecks = true;
1145 break;
1146 }
1147 }
1148 } else {
1149 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
senorblanco6bd51372015-04-15 07:32:27 -07001150 &activeEdges, c, alloc)) {
1151 if (c.sweep_lt(pv->fPoint, v->fPoint)) {
senorblancod6ed19c2015-02-26 06:58:17 -08001152 v = pv;
1153 }
1154 restartChecks = true;
1155 }
1156
1157 }
1158 } while (restartChecks);
senorblancod6ed19c2015-02-26 06:58:17 -08001159 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1160 remove_edge(e, &activeEdges);
1161 }
1162 Edge* leftEdge = leftEnclosingEdge;
1163 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1164 insert_edge(e, leftEdge, &activeEdges);
1165 leftEdge = e;
1166 }
1167 v->fProcessed = true;
1168 }
1169}
1170
1171// Stage 5: Tessellate the simplified mesh into monotone polygons.
1172
1173Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) {
1174 LOG("tessellating simple polygons\n");
senorblanco5b9f42c2015-04-16 11:47:18 -07001175 EdgeList activeEdges;
senorblancod6ed19c2015-02-26 06:58:17 -08001176 Poly* polys = NULL;
1177 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1178 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1179 continue;
1180 }
1181#if LOGGING_ENABLED
1182 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1183#endif
senorblancod6ed19c2015-02-26 06:58:17 -08001184 Edge* leftEnclosingEdge = NULL;
1185 Edge* rightEnclosingEdge = NULL;
senorblanco5b9f42c2015-04-16 11:47:18 -07001186 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
senorblancod6ed19c2015-02-26 06:58:17 -08001187 Poly* leftPoly = NULL;
1188 Poly* rightPoly = NULL;
1189 if (v->fFirstEdgeAbove) {
1190 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1191 rightPoly = v->fLastEdgeAbove->fRightPoly;
1192 } else {
1193 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : NULL;
1194 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : NULL;
1195 }
1196#if LOGGING_ENABLED
1197 LOG("edges above:\n");
1198 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1199 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1200 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1201 }
1202 LOG("edges below:\n");
1203 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1204 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1205 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1206 }
1207#endif
1208 if (v->fFirstEdgeAbove) {
1209 if (leftPoly) {
1210 leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1211 }
1212 if (rightPoly) {
1213 rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1214 }
1215 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1216 Edge* leftEdge = e;
1217 Edge* rightEdge = e->fNextEdgeAbove;
1218 SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
1219 remove_edge(leftEdge, &activeEdges);
1220 if (leftEdge->fRightPoly) {
1221 leftEdge->fRightPoly->end(v, alloc);
1222 }
1223 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != leftEdge->fRightPoly) {
1224 rightEdge->fLeftPoly->end(v, alloc);
1225 }
1226 }
1227 remove_edge(v->fLastEdgeAbove, &activeEdges);
1228 if (!v->fFirstEdgeBelow) {
1229 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1230 SkASSERT(leftPoly->fPartner == NULL && rightPoly->fPartner == NULL);
1231 rightPoly->fPartner = leftPoly;
1232 leftPoly->fPartner = rightPoly;
1233 }
1234 }
1235 }
1236 if (v->fFirstEdgeBelow) {
1237 if (!v->fFirstEdgeAbove) {
1238 if (leftPoly && leftPoly == rightPoly) {
1239 // Split the poly.
1240 if (leftPoly->fActive->fSide == Poly::kLeft_Side) {
1241 leftPoly = new_poly(&polys, leftEnclosingEdge->fTop, leftPoly->fWinding,
1242 alloc);
1243 leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1244 rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1245 leftEnclosingEdge->fRightPoly = leftPoly;
1246 } else {
1247 rightPoly = new_poly(&polys, rightEnclosingEdge->fTop, rightPoly->fWinding,
1248 alloc);
1249 rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1250 leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1251 rightEnclosingEdge->fLeftPoly = rightPoly;
1252 }
1253 } else {
1254 if (leftPoly) {
1255 leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc);
1256 }
1257 if (rightPoly) {
1258 rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc);
1259 }
1260 }
1261 }
1262 Edge* leftEdge = v->fFirstEdgeBelow;
1263 leftEdge->fLeftPoly = leftPoly;
1264 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1265 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1266 rightEdge = rightEdge->fNextEdgeBelow) {
1267 insert_edge(rightEdge, leftEdge, &activeEdges);
1268 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1269 winding += leftEdge->fWinding;
1270 if (winding != 0) {
1271 Poly* poly = new_poly(&polys, v, winding, alloc);
1272 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1273 }
1274 leftEdge = rightEdge;
1275 }
1276 v->fLastEdgeBelow->fRightPoly = rightPoly;
1277 }
senorblancod6ed19c2015-02-26 06:58:17 -08001278#if LOGGING_ENABLED
1279 LOG("\nactive edges:\n");
senorblanco5b9f42c2015-04-16 11:47:18 -07001280 for (Edge* e = activeEdges->fHead; e != NULL; e = e->fRight) {
senorblancod6ed19c2015-02-26 06:58:17 -08001281 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1282 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1283 }
1284#endif
1285 }
1286 return polys;
1287}
1288
1289// This is a driver function which calls stages 2-5 in turn.
1290
senorblanco6bd51372015-04-15 07:32:27 -07001291Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) {
senorblancod6ed19c2015-02-26 06:58:17 -08001292#if LOGGING_ENABLED
1293 for (int i = 0; i < contourCnt; ++i) {
1294 Vertex* v = contours[i];
1295 SkASSERT(v);
1296 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1297 for (v = v->fNext; v != contours[i]; v = v->fNext) {
1298 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1299 }
1300 }
1301#endif
1302 sanitize_contours(contours, contourCnt);
senorblanco6bd51372015-04-15 07:32:27 -07001303 Vertex* vertices = build_edges(contours, contourCnt, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001304 if (!vertices) {
1305 return NULL;
1306 }
1307
1308 // Sort vertices in Y (secondarily in X).
senorblanco6bd51372015-04-15 07:32:27 -07001309 merge_sort(&vertices, c);
1310 merge_coincident_vertices(&vertices, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001311#if LOGGING_ENABLED
1312 for (Vertex* v = vertices; v != NULL; v = v->fNext) {
1313 static float gID = 0.0f;
1314 v->fID = gID++;
1315 }
1316#endif
senorblanco6bd51372015-04-15 07:32:27 -07001317 simplify(vertices, c, alloc);
senorblancod6ed19c2015-02-26 06:58:17 -08001318 return tessellate(vertices, alloc);
1319}
1320
1321// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1322
1323void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, void* data) {
1324 void* d = data;
1325 for (Poly* poly = polys; poly; poly = poly->fNext) {
1326 if (apply_fill_type(fillType, poly->fWinding)) {
1327 d = poly->emit(d);
1328 }
1329 }
1330 return d;
1331}
1332
1333};
1334
1335GrTessellatingPathRenderer::GrTessellatingPathRenderer() {
1336}
1337
1338GrPathRenderer::StencilSupport GrTessellatingPathRenderer::onGetStencilSupport(
1339 const GrDrawTarget*,
1340 const GrPipelineBuilder*,
1341 const SkPath&,
kkinnunen18996512015-04-26 23:18:49 -07001342 const GrStrokeInfo&) const {
senorblancod6ed19c2015-02-26 06:58:17 -08001343 return GrPathRenderer::kNoSupport_StencilSupport;
1344}
1345
1346bool GrTessellatingPathRenderer::canDrawPath(const GrDrawTarget* target,
1347 const GrPipelineBuilder* pipelineBuilder,
1348 const SkMatrix& viewMatrix,
1349 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -07001350 const GrStrokeInfo& stroke,
senorblancod6ed19c2015-02-26 06:58:17 -08001351 bool antiAlias) const {
1352 // This path renderer can draw all fill styles, but does not do antialiasing. It can do convex
1353 // and concave paths, but we'll leave the convex ones to simpler algorithms.
1354 return stroke.isFillStyle() && !antiAlias && !path.isConvex();
1355}
1356
senorblanco9ba39722015-03-05 07:13:42 -08001357class TessellatingPathBatch : public GrBatch {
1358public:
1359
1360 static GrBatch* Create(const GrColor& color,
1361 const SkPath& path,
1362 const SkMatrix& viewMatrix,
1363 SkRect clipBounds) {
1364 return SkNEW_ARGS(TessellatingPathBatch, (color, path, viewMatrix, clipBounds));
1365 }
1366
mtklein36352bf2015-03-25 18:17:31 -07001367 const char* name() const override { return "TessellatingPathBatch"; }
senorblanco9ba39722015-03-05 07:13:42 -08001368
mtklein36352bf2015-03-25 18:17:31 -07001369 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
senorblanco9ba39722015-03-05 07:13:42 -08001370 out->setKnownFourComponents(fColor);
1371 }
1372
mtklein36352bf2015-03-25 18:17:31 -07001373 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
senorblanco9ba39722015-03-05 07:13:42 -08001374 out->setUnknownSingleComponent();
1375 }
1376
mtklein36352bf2015-03-25 18:17:31 -07001377 void initBatchTracker(const GrPipelineInfo& init) override {
senorblanco9ba39722015-03-05 07:13:42 -08001378 // Handle any color overrides
1379 if (init.fColorIgnored) {
1380 fColor = GrColor_ILLEGAL;
1381 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
1382 fColor = init.fOverrideColor;
1383 }
1384 fPipelineInfo = init;
1385 }
1386
mtklein36352bf2015-03-25 18:17:31 -07001387 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
senorblanco6bd51372015-04-15 07:32:27 -07001388 SkRect pathBounds = fPath.getBounds();
1389 Comparator c;
1390 if (pathBounds.width() > pathBounds.height()) {
1391 c.sweep_lt = sweep_lt_horiz;
1392 c.sweep_gt = sweep_gt_horiz;
1393 } else {
1394 c.sweep_lt = sweep_lt_vert;
1395 c.sweep_gt = sweep_gt_vert;
1396 }
senorblanco2b4bb072015-04-22 13:45:18 -07001397 SkScalar screenSpaceTol = GrPathUtils::kDefaultTolerance;
1398 SkScalar tol = GrPathUtils::scaleToleranceToSrc(screenSpaceTol, fViewMatrix, pathBounds);
senorblanco9ba39722015-03-05 07:13:42 -08001399 int contourCnt;
1400 int maxPts = GrPathUtils::worstCasePointCount(fPath, &contourCnt, tol);
1401 if (maxPts <= 0) {
1402 return;
1403 }
1404 if (maxPts > ((int)SK_MaxU16 + 1)) {
1405 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1406 return;
1407 }
1408 SkPath::FillType fillType = fPath.getFillType();
1409 if (SkPath::IsInverseFillType(fillType)) {
1410 contourCnt++;
1411 }
1412
1413 LOG("got %d pts, %d contours\n", maxPts, contourCnt);
1414 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
1415 SkAutoTUnref<const GrGeometryProcessor> gp(
1416 GrDefaultGeoProcFactory::Create(flags, fColor, fViewMatrix, SkMatrix::I()));
1417 batchTarget->initDraw(gp, pipeline);
1418 gp->initBatchTracker(batchTarget->currentBatchTracker(), fPipelineInfo);
1419
1420 SkAutoTDeleteArray<Vertex*> contours(SkNEW_ARRAY(Vertex *, contourCnt));
1421
1422 // For the initial size of the chunk allocator, estimate based on the point count:
1423 // one vertex per point for the initial passes, plus two for the vertices in the
1424 // resulting Polys, since the same point may end up in two Polys. Assume minimal
1425 // connectivity of one Edge per Vertex (will grow for intersections).
1426 SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge)));
1427 path_to_contours(fPath, tol, fClipBounds, contours.get(), alloc);
1428 Poly* polys;
senorblanco6bd51372015-04-15 07:32:27 -07001429 polys = contours_to_polys(contours.get(), contourCnt, c, alloc);
senorblanco9ba39722015-03-05 07:13:42 -08001430 int count = 0;
1431 for (Poly* poly = polys; poly; poly = poly->fNext) {
1432 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1433 count += (poly->fCount - 2) * (WIREFRAME ? 6 : 3);
1434 }
1435 }
senorblancoe8331072015-03-26 14:52:45 -07001436 if (0 == count) {
1437 return;
1438 }
senorblanco9ba39722015-03-05 07:13:42 -08001439
1440 size_t stride = gp->getVertexStride();
1441 const GrVertexBuffer* vertexBuffer;
1442 int firstVertex;
robertphillipse40d3972015-05-07 09:51:43 -07001443 void* verts = batchTarget->makeVertSpace(stride, count, &vertexBuffer, &firstVertex);
bsalomoncb8979d2015-05-05 09:51:38 -07001444 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001445 SkDebugf("Could not allocate vertices\n");
1446 return;
1447 }
1448
senorblanco9ba39722015-03-05 07:13:42 -08001449 LOG("emitting %d verts\n", count);
bsalomoncb8979d2015-05-05 09:51:38 -07001450 void* end = polys_to_triangles(polys, fillType, verts);
senorblanco9ba39722015-03-05 07:13:42 -08001451 int actualCount = static_cast<int>(
bsalomoncb8979d2015-05-05 09:51:38 -07001452 (static_cast<char*>(end) - static_cast<char*>(verts)) / stride);
senorblanco9ba39722015-03-05 07:13:42 -08001453 LOG("actual count: %d\n", actualCount);
1454 SkASSERT(actualCount <= count);
1455
1456 GrPrimitiveType primitiveType = WIREFRAME ? kLines_GrPrimitiveType
1457 : kTriangles_GrPrimitiveType;
bsalomoncb8979d2015-05-05 09:51:38 -07001458 GrVertices vertices;
1459 vertices.init(primitiveType, vertexBuffer, firstVertex, actualCount);
1460 batchTarget->draw(vertices);
senorblanco9ba39722015-03-05 07:13:42 -08001461
1462 batchTarget->putBackVertices((size_t)(count - actualCount), stride);
1463 return;
1464 }
1465
mtklein36352bf2015-03-25 18:17:31 -07001466 bool onCombineIfPossible(GrBatch*) override {
senorblanco9ba39722015-03-05 07:13:42 -08001467 return false;
1468 }
1469
1470private:
1471 TessellatingPathBatch(const GrColor& color,
1472 const SkPath& path,
1473 const SkMatrix& viewMatrix,
1474 const SkRect& clipBounds)
1475 : fColor(color)
1476 , fPath(path)
1477 , fViewMatrix(viewMatrix)
1478 , fClipBounds(clipBounds) {
1479 this->initClassID<TessellatingPathBatch>();
joshualitt99c7c072015-05-01 13:43:30 -07001480
1481 fBounds = path.getBounds();
1482 viewMatrix.mapRect(&fBounds);
senorblanco9ba39722015-03-05 07:13:42 -08001483 }
1484
1485 GrColor fColor;
1486 SkPath fPath;
1487 SkMatrix fViewMatrix;
1488 SkRect fClipBounds; // in source space
1489 GrPipelineInfo fPipelineInfo;
1490};
1491
senorblancod6ed19c2015-02-26 06:58:17 -08001492bool GrTessellatingPathRenderer::onDrawPath(GrDrawTarget* target,
1493 GrPipelineBuilder* pipelineBuilder,
1494 GrColor color,
1495 const SkMatrix& viewM,
1496 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -07001497 const GrStrokeInfo&,
senorblancod6ed19c2015-02-26 06:58:17 -08001498 bool antiAlias) {
1499 SkASSERT(!antiAlias);
1500 const GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
1501 if (NULL == rt) {
1502 return false;
1503 }
1504
senorblancod6ed19c2015-02-26 06:58:17 -08001505 SkIRect clipBoundsI;
1506 pipelineBuilder->clip().getConservativeBounds(rt, &clipBoundsI);
1507 SkRect clipBounds = SkRect::Make(clipBoundsI);
1508 SkMatrix vmi;
1509 if (!viewM.invert(&vmi)) {
1510 return false;
1511 }
1512 vmi.mapRect(&clipBounds);
senorblanco9ba39722015-03-05 07:13:42 -08001513 SkAutoTUnref<GrBatch> batch(TessellatingPathBatch::Create(color, path, viewM, clipBounds));
1514 target->drawBatch(pipelineBuilder, batch);
senorblancod6ed19c2015-02-26 06:58:17 -08001515
1516 return true;
1517}
joshualitt2fbd4062015-05-07 13:06:41 -07001518
1519///////////////////////////////////////////////////////////////////////////////////////////////////
1520
1521#ifdef GR_TEST_UTILS
1522
joshualitt6c891102015-05-13 08:51:49 -07001523BATCH_TEST_DEFINE(TesselatingPathBatch) {
joshualitt2fbd4062015-05-07 13:06:41 -07001524 GrColor color = GrRandomColor(random);
1525 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
1526 SkPath path = GrTest::TestPath(random);
1527 SkRect clipBounds = GrTest::TestRect(random);
1528 SkMatrix vmi;
1529 bool result = viewMatrix.invert(&vmi);
1530 if (!result) {
1531 SkFAIL("Cannot invert matrix\n");
1532 }
1533 vmi.mapRect(&clipBounds);
1534 return TessellatingPathBatch::Create(color, path, viewMatrix, clipBounds);
1535}
1536
1537#endif