Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Side by Side Diff: src/gpu/GrTessellatingPathRenderer.cpp

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

Powered by Google App Engine
This is Rietveld 408576698