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

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

Powered by Google App Engine
This is Rietveld 408576698