GrTessellator.cpp revision 7ecc59610de72043e9b7ebaf1ef45c43425e54fc
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 "GrTessellator.h" 9 10#include "GrDefaultGeoProcFactory.h" 11#include "GrPathUtils.h" 12 13#include "SkChunkAlloc.h" 14#include "SkGeometry.h" 15#include "SkPath.h" 16 17#include <stdio.h> 18 19/* 20 * There are six stages to the basic algorithm: 21 * 22 * 1) Linearize the path contours into piecewise linear segments (path_to_contours()). 23 * 2) Build a mesh of edges connecting the vertices (build_edges()). 24 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). 25 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()). 26 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). 27 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()). 28 * 29 * For screenspace antialiasing, the algorithm is modified as follows: 30 * 31 * Run steps 1-5 above to produce polygons. 32 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()). 33 * 5c) Simplify boundaries to remove "pointy" vertices which cause inversions (simplify_boundary()). 34 * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find 35 * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new 36 * antialiased mesh from those vertices (boundary_to_aa_mesh()). 37 * Run steps 3-6 above on the new mesh, and produce antialiased triangles. 38 * 39 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list 40 * of vertices (and the necessity of inserting new vertices on intersection). 41 * 42 * Stages (4) and (5) use an active edge list, which a list of all edges for which the 43 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted 44 * left-to-right based on the point where both edges are active (when both top vertices 45 * have been seen, so the "lower" top vertex of the two). If the top vertices are equal 46 * (shared), it's sorted based on the last point where both edges are active, so the 47 * "upper" bottom vertex. 48 * 49 * The most complex step is the simplification (4). It's based on the Bentley-Ottman 50 * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are 51 * not exact and may violate the mesh topology or active edge list ordering. We 52 * accommodate this by adjusting the topology of the mesh and AEL to match the intersection 53 * points. This occurs in three ways: 54 * 55 * A) Intersections may cause a shortened edge to no longer be ordered with respect to its 56 * neighbouring edges at the top or bottom vertex. This is handled by merging the 57 * edges (merge_collinear_edges()). 58 * B) Intersections may cause an edge to violate the left-to-right ordering of the 59 * active edge list. This is handled by splitting the neighbour edge on the 60 * intersected vertex (cleanup_active_edges()). 61 * C) Shortening an edge may cause an active edge to become inactive or an inactive edge 62 * to become active. This is handled by removing or inserting the edge in the active 63 * edge list (fix_active_state()). 64 * 65 * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and 66 * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it 67 * currently uses a linked list for the active edge list, rather than a 2-3 tree as the 68 * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also 69 * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N) 70 * insertions and removals was greater than the cost of infrequent O(N) lookups with the 71 * linked list implementation. With the latter, all removals are O(1), and most insertions 72 * are O(1), since we know the adjacent edge in the active edge list based on the topology. 73 * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less 74 * frequent. There may be other data structures worth investigating, however. 75 * 76 * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the 77 * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y 78 * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall, 79 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so 80 * that the "left" and "right" orientation in the code remains correct (edges to the left are 81 * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90 82 * degrees counterclockwise, rather that transposing. 83 */ 84 85#define LOGGING_ENABLED 0 86 87#if LOGGING_ENABLED 88#define LOG printf 89#else 90#define LOG(...) 91#endif 92 93#define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type args 94 95namespace { 96 97struct Vertex; 98struct Edge; 99struct Poly; 100 101template <class T, T* T::*Prev, T* T::*Next> 102void list_insert(T* t, T* prev, T* next, T** head, T** tail) { 103 t->*Prev = prev; 104 t->*Next = next; 105 if (prev) { 106 prev->*Next = t; 107 } else if (head) { 108 *head = t; 109 } 110 if (next) { 111 next->*Prev = t; 112 } else if (tail) { 113 *tail = t; 114 } 115} 116 117template <class T, T* T::*Prev, T* T::*Next> 118void list_remove(T* t, T** head, T** tail) { 119 if (t->*Prev) { 120 t->*Prev->*Next = t->*Next; 121 } else if (head) { 122 *head = t->*Next; 123 } 124 if (t->*Next) { 125 t->*Next->*Prev = t->*Prev; 126 } else if (tail) { 127 *tail = t->*Prev; 128 } 129 t->*Prev = t->*Next = nullptr; 130} 131 132/** 133 * Vertices are used in three ways: first, the path contours are converted into a 134 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices 135 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing 136 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid 137 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of 138 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since 139 * an individual Vertex from the path mesh may belong to multiple 140 * MonotonePolys, so the original Vertices cannot be re-used. 141 */ 142 143struct Vertex { 144 Vertex(const SkPoint& point, uint8_t alpha) 145 : fPoint(point), fPrev(nullptr), fNext(nullptr) 146 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr) 147 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr) 148 , fProcessed(false) 149 , fAlpha(alpha) 150#if LOGGING_ENABLED 151 , fID (-1.0f) 152#endif 153 {} 154 SkPoint fPoint; // Vertex position 155 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices. 156 Vertex* fNext; // " 157 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex. 158 Edge* fLastEdgeAbove; // " 159 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex. 160 Edge* fLastEdgeBelow; // " 161 bool fProcessed; // Has this vertex been seen in simplify()? 162 uint8_t fAlpha; 163#if LOGGING_ENABLED 164 float fID; // Identifier used for logging. 165#endif 166}; 167 168/***************************************************************************************/ 169 170struct AAParams { 171 bool fTweakAlpha; 172 GrColor fColor; 173}; 174 175typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); 176 177struct Comparator { 178 CompareFunc sweep_lt; 179 CompareFunc sweep_gt; 180}; 181 182bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { 183 return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX; 184} 185 186bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) { 187 return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY; 188} 189 190bool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) { 191 return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX; 192} 193 194bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) { 195 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY; 196} 197 198inline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) { 199 if (!aaParams) { 200 SkPoint* d = static_cast<SkPoint*>(data); 201 *d++ = v->fPoint; 202 return d; 203 } 204 if (aaParams->fTweakAlpha) { 205 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data); 206 d->fPosition = v->fPoint; 207 d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha)); 208 d++; 209 return d; 210 } 211 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data); 212 d->fPosition = v->fPoint; 213 d->fColor = aaParams->fColor; 214 d->fCoverage = GrNormalizeByteToFloat(v->fAlpha); 215 d++; 216 return d; 217} 218 219void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) { 220#if TESSELLATOR_WIREFRAME 221 data = emit_vertex(v0, aaParams, data); 222 data = emit_vertex(v1, aaParams, data); 223 data = emit_vertex(v1, aaParams, data); 224 data = emit_vertex(v2, aaParams, data); 225 data = emit_vertex(v2, aaParams, data); 226 data = emit_vertex(v0, aaParams, data); 227#else 228 data = emit_vertex(v0, aaParams, data); 229 data = emit_vertex(v1, aaParams, data); 230 data = emit_vertex(v2, aaParams, data); 231#endif 232 return data; 233} 234 235struct VertexList { 236 VertexList() : fHead(nullptr), fTail(nullptr) {} 237 Vertex* fHead; 238 Vertex* fTail; 239 void insert(Vertex* v, Vertex* prev, Vertex* next) { 240 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail); 241 } 242 void append(Vertex* v) { 243 insert(v, fTail, nullptr); 244 } 245 void prepend(Vertex* v) { 246 insert(v, nullptr, fHead); 247 } 248 void close() { 249 if (fHead && fTail) { 250 fTail->fNext = fHead; 251 fHead->fPrev = fTail; 252 } 253 } 254}; 255 256// Round to nearest quarter-pixel. This is used for screenspace tessellation. 257 258inline void round(SkPoint* p) { 259 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); 260 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); 261} 262 263// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line. 264struct Line { 265 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {} 266 Line(const SkPoint& p, const SkPoint& q) 267 : fA(static_cast<double>(q.fY) - p.fY) // a = dY 268 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX 269 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p) 270 static_cast<double>(p.fX) * q.fY) {} 271 double dist(const SkPoint& p) const { 272 return fA * p.fX + fB * p.fY + fC; 273 } 274 double magSq() const { 275 return fA * fA + fB * fB; 276 } 277 278 // Compute the intersection of two (infinite) Lines. 279 bool intersect(const Line& other, SkPoint* point) { 280 double denom = fA * other.fB - fB * other.fA; 281 if (denom == 0.0) { 282 return false; 283 } 284 double scale = 1.0f / denom; 285 point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale); 286 point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale); 287 round(point); 288 return true; 289 } 290 double fA, fB, fC; 291}; 292 293/** 294 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and 295 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf(). 296 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating 297 * point). For speed, that case is only tested by the callers which require it (e.g., 298 * cleanup_active_edges()). Edges also handle checking for intersection with other edges. 299 * Currently, this converts the edges to the parametric form, in order to avoid doing a division 300 * until an intersection has been confirmed. This is slightly slower in the "found" case, but 301 * a lot faster in the "not found" case. 302 * 303 * The coefficients of the line equation stored in double precision to avoid catastrphic 304 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is 305 * correct in float, since it's a polynomial of degree 2. The intersect() function, being 306 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its 307 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of 308 * this file). 309 */ 310 311struct Edge { 312 Edge(Vertex* top, Vertex* bottom, int winding) 313 : fWinding(winding) 314 , fTop(top) 315 , fBottom(bottom) 316 , fLeft(nullptr) 317 , fRight(nullptr) 318 , fPrevEdgeAbove(nullptr) 319 , fNextEdgeAbove(nullptr) 320 , fPrevEdgeBelow(nullptr) 321 , fNextEdgeBelow(nullptr) 322 , fLeftPoly(nullptr) 323 , fRightPoly(nullptr) 324 , fLeftPolyPrev(nullptr) 325 , fLeftPolyNext(nullptr) 326 , fRightPolyPrev(nullptr) 327 , fRightPolyNext(nullptr) 328 , fUsedInLeftPoly(false) 329 , fUsedInRightPoly(false) 330 , fLine(top, bottom) { 331 } 332 int fWinding; // 1 == edge goes downward; -1 = edge goes upward. 333 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt). 334 Vertex* fBottom; // The bottom vertex in vertex-sort-order. 335 Edge* fLeft; // The linked list of edges in the active edge list. 336 Edge* fRight; // " 337 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above". 338 Edge* fNextEdgeAbove; // " 339 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below". 340 Edge* fNextEdgeBelow; // " 341 Poly* fLeftPoly; // The Poly to the left of this edge, if any. 342 Poly* fRightPoly; // The Poly to the right of this edge, if any. 343 Edge* fLeftPolyPrev; 344 Edge* fLeftPolyNext; 345 Edge* fRightPolyPrev; 346 Edge* fRightPolyNext; 347 bool fUsedInLeftPoly; 348 bool fUsedInRightPoly; 349 Line fLine; 350 double dist(const SkPoint& p) const { 351 return fLine.dist(p); 352 } 353 bool isRightOf(Vertex* v) const { 354 return fLine.dist(v->fPoint) < 0.0; 355 } 356 bool isLeftOf(Vertex* v) const { 357 return fLine.dist(v->fPoint) > 0.0; 358 } 359 void recompute() { 360 fLine = Line(fTop, fBottom); 361 } 362 bool intersect(const Edge& other, SkPoint* p) { 363 LOG("intersecting %g -> %g with %g -> %g\n", 364 fTop->fID, fBottom->fID, 365 other.fTop->fID, other.fBottom->fID); 366 if (fTop == other.fTop || fBottom == other.fBottom) { 367 return false; 368 } 369 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA; 370 if (denom == 0.0) { 371 return false; 372 } 373 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX; 374 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY; 375 double sNumer = -dy * other.fLine.fB - dx * other.fLine.fA; 376 double tNumer = -dy * fLine.fB - dx * fLine.fA; 377 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early. 378 // This saves us doing the divide below unless absolutely necessary. 379 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom) 380 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { 381 return false; 382 } 383 double s = sNumer / denom; 384 SkASSERT(s >= 0.0 && s <= 1.0); 385 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB); 386 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA); 387 return true; 388 } 389}; 390 391struct EdgeList { 392 EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr), fCount(0) {} 393 Edge* fHead; 394 Edge* fTail; 395 EdgeList* fNext; 396 int fCount; 397 void insert(Edge* edge, Edge* prev, Edge* next) { 398 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail); 399 fCount++; 400 } 401 void append(Edge* e) { 402 insert(e, fTail, nullptr); 403 } 404 void remove(Edge* edge) { 405 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail); 406 fCount--; 407 } 408 void close() { 409 if (fHead && fTail) { 410 fTail->fRight = fHead; 411 fHead->fLeft = fTail; 412 } 413 } 414 bool contains(Edge* edge) const { 415 return edge->fLeft || edge->fRight || fHead == edge; 416 } 417}; 418 419/***************************************************************************************/ 420 421struct Poly { 422 Poly(Vertex* v, int winding) 423 : fFirstVertex(v) 424 , fWinding(winding) 425 , fHead(nullptr) 426 , fTail(nullptr) 427 , fNext(nullptr) 428 , fPartner(nullptr) 429 , fCount(0) 430 { 431#if LOGGING_ENABLED 432 static int gID = 0; 433 fID = gID++; 434 LOG("*** created Poly %d\n", fID); 435#endif 436 } 437 typedef enum { kLeft_Side, kRight_Side } Side; 438 struct MonotonePoly { 439 MonotonePoly(Edge* edge, Side side) 440 : fSide(side) 441 , fFirstEdge(nullptr) 442 , fLastEdge(nullptr) 443 , fPrev(nullptr) 444 , fNext(nullptr) { 445 this->addEdge(edge); 446 } 447 Side fSide; 448 Edge* fFirstEdge; 449 Edge* fLastEdge; 450 MonotonePoly* fPrev; 451 MonotonePoly* fNext; 452 void addEdge(Edge* edge) { 453 if (fSide == kRight_Side) { 454 SkASSERT(!edge->fUsedInRightPoly); 455 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>( 456 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 457 edge->fUsedInRightPoly = true; 458 } else { 459 SkASSERT(!edge->fUsedInLeftPoly); 460 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( 461 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 462 edge->fUsedInLeftPoly = true; 463 } 464 } 465 466 void* emit(const AAParams* aaParams, void* data) { 467 Edge* e = fFirstEdge; 468 e->fTop->fPrev = e->fTop->fNext = nullptr; 469 VertexList vertices; 470 vertices.append(e->fTop); 471 while (e != nullptr) { 472 e->fBottom->fPrev = e->fBottom->fNext = nullptr; 473 if (kRight_Side == fSide) { 474 vertices.append(e->fBottom); 475 e = e->fRightPolyNext; 476 } else { 477 vertices.prepend(e->fBottom); 478 e = e->fLeftPolyNext; 479 } 480 } 481 Vertex* first = vertices.fHead; 482 Vertex* v = first->fNext; 483 while (v != vertices.fTail) { 484 SkASSERT(v && v->fPrev && v->fNext); 485 Vertex* prev = v->fPrev; 486 Vertex* curr = v; 487 Vertex* next = v->fNext; 488 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; 489 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; 490 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; 491 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; 492 if (ax * by - ay * bx >= 0.0) { 493 data = emit_triangle(prev, curr, next, aaParams, data); 494 v->fPrev->fNext = v->fNext; 495 v->fNext->fPrev = v->fPrev; 496 if (v->fPrev == first) { 497 v = v->fNext; 498 } else { 499 v = v->fPrev; 500 } 501 } else { 502 v = v->fNext; 503 } 504 } 505 return data; 506 } 507 }; 508 Poly* addEdge(Edge* e, Side side, SkChunkAlloc& alloc) { 509 LOG("addEdge (%g -> %g) to poly %d, %s side\n", 510 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right"); 511 Poly* partner = fPartner; 512 Poly* poly = this; 513 if (side == kRight_Side) { 514 if (e->fUsedInRightPoly) { 515 return this; 516 } 517 } else { 518 if (e->fUsedInLeftPoly) { 519 return this; 520 } 521 } 522 if (partner) { 523 fPartner = partner->fPartner = nullptr; 524 } 525 if (!fTail) { 526 fHead = fTail = ALLOC_NEW(MonotonePoly, (e, side), alloc); 527 fCount += 2; 528 } else if (e->fBottom == fTail->fLastEdge->fBottom) { 529 return poly; 530 } else if (side == fTail->fSide) { 531 fTail->addEdge(e); 532 fCount++; 533 } else { 534 e = ALLOC_NEW(Edge, (fTail->fLastEdge->fBottom, e->fBottom, 1), alloc); 535 fTail->addEdge(e); 536 fCount++; 537 if (partner) { 538 partner->addEdge(e, side, alloc); 539 poly = partner; 540 } else { 541 MonotonePoly* m = ALLOC_NEW(MonotonePoly, (e, side), alloc); 542 m->fPrev = fTail; 543 fTail->fNext = m; 544 fTail = m; 545 } 546 } 547 return poly; 548 } 549 void* emit(const AAParams* aaParams, void *data) { 550 if (fCount < 3) { 551 return data; 552 } 553 LOG("emit() %d, size %d\n", fID, fCount); 554 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { 555 data = m->emit(aaParams, data); 556 } 557 return data; 558 } 559 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; } 560 Vertex* fFirstVertex; 561 int fWinding; 562 MonotonePoly* fHead; 563 MonotonePoly* fTail; 564 Poly* fNext; 565 Poly* fPartner; 566 int fCount; 567#if LOGGING_ENABLED 568 int fID; 569#endif 570}; 571 572/***************************************************************************************/ 573 574bool coincident(const SkPoint& a, const SkPoint& b) { 575 return a == b; 576} 577 578Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) { 579 Poly* poly = ALLOC_NEW(Poly, (v, winding), alloc); 580 poly->fNext = *head; 581 *head = poly; 582 return poly; 583} 584 585EdgeList* new_contour(EdgeList** head, SkChunkAlloc& alloc) { 586 EdgeList* contour = ALLOC_NEW(EdgeList, (), alloc); 587 contour->fNext = *head; 588 *head = contour; 589 return contour; 590} 591 592Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, 593 SkChunkAlloc& alloc) { 594 Vertex* v = ALLOC_NEW(Vertex, (p, 255), alloc); 595#if LOGGING_ENABLED 596 static float gID = 0.0f; 597 v->fID = gID++; 598#endif 599 if (prev) { 600 prev->fNext = v; 601 v->fPrev = prev; 602 } else { 603 *head = v; 604 } 605 return v; 606} 607 608Vertex* generate_quadratic_points(const SkPoint& p0, 609 const SkPoint& p1, 610 const SkPoint& p2, 611 SkScalar tolSqd, 612 Vertex* prev, 613 Vertex** head, 614 int pointsLeft, 615 SkChunkAlloc& alloc) { 616 SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2); 617 if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) { 618 return append_point_to_contour(p2, prev, head, alloc); 619 } 620 621 const SkPoint q[] = { 622 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, 623 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, 624 }; 625 const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; 626 627 pointsLeft >>= 1; 628 prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc); 629 prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc); 630 return prev; 631} 632 633Vertex* generate_cubic_points(const SkPoint& p0, 634 const SkPoint& p1, 635 const SkPoint& p2, 636 const SkPoint& p3, 637 SkScalar tolSqd, 638 Vertex* prev, 639 Vertex** head, 640 int pointsLeft, 641 SkChunkAlloc& alloc) { 642 SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3); 643 SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3); 644 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || 645 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) { 646 return append_point_to_contour(p3, prev, head, alloc); 647 } 648 const SkPoint q[] = { 649 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, 650 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, 651 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } 652 }; 653 const SkPoint r[] = { 654 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, 655 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } 656 }; 657 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; 658 pointsLeft >>= 1; 659 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc); 660 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc); 661 return prev; 662} 663 664// Stage 1: convert the input path to a set of linear contours (linked list of Vertices). 665 666void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 667 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { 668 SkScalar toleranceSqd = tolerance * tolerance; 669 670 SkPoint pts[4]; 671 bool done = false; 672 *isLinear = true; 673 SkPath::Iter iter(path, false); 674 Vertex* prev = nullptr; 675 Vertex* head = nullptr; 676 if (path.isInverseFillType()) { 677 SkPoint quad[4]; 678 clipBounds.toQuad(quad); 679 for (int i = 3; i >= 0; i--) { 680 prev = append_point_to_contour(quad[i], prev, &head, alloc); 681 } 682 head->fPrev = prev; 683 prev->fNext = head; 684 *contours++ = head; 685 head = prev = nullptr; 686 } 687 SkAutoConicToQuads converter; 688 while (!done) { 689 SkPath::Verb verb = iter.next(pts); 690 switch (verb) { 691 case SkPath::kConic_Verb: { 692 SkScalar weight = iter.conicWeight(); 693 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd); 694 for (int i = 0; i < converter.countQuads(); ++i) { 695 int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance); 696 prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2], 697 toleranceSqd, prev, &head, pointsLeft, alloc); 698 quadPts += 2; 699 } 700 *isLinear = false; 701 break; 702 } 703 case SkPath::kMove_Verb: 704 if (head) { 705 head->fPrev = prev; 706 prev->fNext = head; 707 *contours++ = head; 708 } 709 head = prev = nullptr; 710 prev = append_point_to_contour(pts[0], prev, &head, alloc); 711 break; 712 case SkPath::kLine_Verb: { 713 prev = append_point_to_contour(pts[1], prev, &head, alloc); 714 break; 715 } 716 case SkPath::kQuad_Verb: { 717 int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance); 718 prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev, 719 &head, pointsLeft, alloc); 720 *isLinear = false; 721 break; 722 } 723 case SkPath::kCubic_Verb: { 724 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance); 725 prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3], 726 toleranceSqd, prev, &head, pointsLeft, alloc); 727 *isLinear = false; 728 break; 729 } 730 case SkPath::kClose_Verb: 731 if (head) { 732 head->fPrev = prev; 733 prev->fNext = head; 734 *contours++ = head; 735 } 736 head = prev = nullptr; 737 break; 738 case SkPath::kDone_Verb: 739 if (head) { 740 head->fPrev = prev; 741 prev->fNext = head; 742 *contours++ = head; 743 } 744 done = true; 745 break; 746 } 747 } 748} 749 750inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) { 751 if (!poly) { 752 return false; 753 } 754 int winding = poly->fWinding; 755 switch (fillType) { 756 case SkPath::kWinding_FillType: 757 return winding != 0; 758 case SkPath::kEvenOdd_FillType: 759 return (winding & 1) != 0; 760 case SkPath::kInverseWinding_FillType: 761 return winding == 1; 762 case SkPath::kInverseEvenOdd_FillType: 763 return (winding & 1) == 1; 764 default: 765 SkASSERT(false); 766 return false; 767 } 768} 769 770Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c, 771 int winding_scale = 1) { 772 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? winding_scale : -winding_scale; 773 Vertex* top = winding < 0 ? next : prev; 774 Vertex* bottom = winding < 0 ? prev : next; 775 return ALLOC_NEW(Edge, (top, bottom, winding), alloc); 776} 777 778void remove_edge(Edge* edge, EdgeList* edges) { 779 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 780 SkASSERT(edges->contains(edge)); 781 edges->remove(edge); 782} 783 784void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) { 785 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 786 SkASSERT(!edges->contains(edge)); 787 Edge* next = prev ? prev->fRight : edges->fHead; 788 edges->insert(edge, prev, next); 789} 790 791void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { 792 if (v->fFirstEdgeAbove) { 793 *left = v->fFirstEdgeAbove->fLeft; 794 *right = v->fLastEdgeAbove->fRight; 795 return; 796 } 797 Edge* next = nullptr; 798 Edge* prev; 799 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) { 800 if (prev->isLeftOf(v)) { 801 break; 802 } 803 next = prev; 804 } 805 *left = prev; 806 *right = next; 807} 808 809void find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) { 810 Edge* prev = nullptr; 811 Edge* next; 812 for (next = edges->fHead; next != nullptr; next = next->fRight) { 813 if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) || 814 (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) || 815 (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) && 816 next->isRightOf(edge->fBottom)) || 817 (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) && 818 edge->isLeftOf(next->fBottom))) { 819 break; 820 } 821 prev = next; 822 } 823 *left = prev; 824 *right = next; 825} 826 827void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) { 828 if (activeEdges && activeEdges->contains(edge)) { 829 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) { 830 remove_edge(edge, activeEdges); 831 } 832 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) { 833 Edge* left; 834 Edge* right; 835 find_enclosing_edges(edge, activeEdges, c, &left, &right); 836 insert_edge(edge, left, activeEdges); 837 } 838} 839 840void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) { 841 if (edge->fTop->fPoint == edge->fBottom->fPoint || 842 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { 843 return; 844 } 845 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); 846 Edge* prev = nullptr; 847 Edge* next; 848 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { 849 if (next->isRightOf(edge->fTop)) { 850 break; 851 } 852 prev = next; 853 } 854 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( 855 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); 856} 857 858void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) { 859 if (edge->fTop->fPoint == edge->fBottom->fPoint || 860 c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { 861 return; 862 } 863 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); 864 Edge* prev = nullptr; 865 Edge* next; 866 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { 867 if (next->isRightOf(edge->fBottom)) { 868 break; 869 } 870 prev = next; 871 } 872 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( 873 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); 874} 875 876void remove_edge_above(Edge* edge) { 877 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, 878 edge->fBottom->fID); 879 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( 880 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); 881} 882 883void remove_edge_below(Edge* edge) { 884 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, 885 edge->fTop->fID); 886 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( 887 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); 888} 889 890void erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) { 891 if (edge->fWinding != 0) { 892 return; 893 } 894 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID); 895 remove_edge_above(edge); 896 remove_edge_below(edge); 897 if (edges && edges->contains(edge)) { 898 remove_edge(edge, edges); 899 } 900} 901 902void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c); 903 904void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { 905 remove_edge_below(edge); 906 edge->fTop = v; 907 edge->recompute(); 908 insert_edge_below(edge, v, c); 909 fix_active_state(edge, activeEdges, c); 910 merge_collinear_edges(edge, activeEdges, c); 911} 912 913void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { 914 remove_edge_above(edge); 915 edge->fBottom = v; 916 edge->recompute(); 917 insert_edge_above(edge, v, c); 918 fix_active_state(edge, activeEdges, c); 919 merge_collinear_edges(edge, activeEdges, c); 920} 921 922void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) { 923 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { 924 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", 925 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, 926 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); 927 other->fWinding += edge->fWinding; 928 erase_edge_if_zero_winding(other, activeEdges); 929 edge->fWinding = 0; 930 erase_edge_if_zero_winding(edge, activeEdges); 931 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { 932 other->fWinding += edge->fWinding; 933 erase_edge_if_zero_winding(other, activeEdges); 934 set_bottom(edge, other->fTop, activeEdges, c); 935 } else { 936 edge->fWinding += other->fWinding; 937 erase_edge_if_zero_winding(edge, activeEdges); 938 set_bottom(other, edge->fTop, activeEdges, c); 939 } 940} 941 942void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) { 943 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { 944 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", 945 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, 946 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); 947 other->fWinding += edge->fWinding; 948 erase_edge_if_zero_winding(other, activeEdges); 949 edge->fWinding = 0; 950 erase_edge_if_zero_winding(edge, activeEdges); 951 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { 952 edge->fWinding += other->fWinding; 953 erase_edge_if_zero_winding(edge, activeEdges); 954 set_top(other, edge->fBottom, activeEdges, c); 955 } else { 956 other->fWinding += edge->fWinding; 957 erase_edge_if_zero_winding(other, activeEdges); 958 set_top(edge, other->fBottom, activeEdges, c); 959 } 960} 961 962void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) { 963 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop || 964 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) { 965 merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c); 966 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop || 967 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) { 968 merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c); 969 } 970 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom || 971 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) { 972 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c); 973 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom || 974 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) { 975 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c); 976 } 977} 978 979void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc); 980 981void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) { 982 Vertex* top = edge->fTop; 983 Vertex* bottom = edge->fBottom; 984 if (edge->fLeft) { 985 Vertex* leftTop = edge->fLeft->fTop; 986 Vertex* leftBottom = edge->fLeft->fBottom; 987 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) { 988 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc); 989 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) { 990 split_edge(edge, leftTop, activeEdges, c, alloc); 991 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) && 992 !edge->fLeft->isLeftOf(bottom)) { 993 split_edge(edge->fLeft, bottom, activeEdges, c, alloc); 994 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) { 995 split_edge(edge, leftBottom, activeEdges, c, alloc); 996 } 997 } 998 if (edge->fRight) { 999 Vertex* rightTop = edge->fRight->fTop; 1000 Vertex* rightBottom = edge->fRight->fBottom; 1001 if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) { 1002 split_edge(edge->fRight, top, activeEdges, c, alloc); 1003 } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) { 1004 split_edge(edge, rightTop, activeEdges, c, alloc); 1005 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) && 1006 !edge->fRight->isRightOf(bottom)) { 1007 split_edge(edge->fRight, bottom, activeEdges, c, alloc); 1008 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) && 1009 !edge->isLeftOf(rightBottom)) { 1010 split_edge(edge, rightBottom, activeEdges, c, alloc); 1011 } 1012 } 1013} 1014 1015void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) { 1016 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", 1017 edge->fTop->fID, edge->fBottom->fID, 1018 v->fID, v->fPoint.fX, v->fPoint.fY); 1019 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) { 1020 set_top(edge, v, activeEdges, c); 1021 } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) { 1022 set_bottom(edge, v, activeEdges, c); 1023 } else { 1024 Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), alloc); 1025 insert_edge_below(newEdge, v, c); 1026 insert_edge_above(newEdge, edge->fBottom, c); 1027 set_bottom(edge, v, activeEdges, c); 1028 cleanup_active_edges(edge, activeEdges, c, alloc); 1029 fix_active_state(newEdge, activeEdges, c); 1030 merge_collinear_edges(newEdge, activeEdges, c); 1031 } 1032} 1033 1034Edge* connect(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator c, 1035 int winding_scale = 1) { 1036 Edge* edge = new_edge(prev, next, alloc, c, winding_scale); 1037 if (edge->fWinding > 0) { 1038 insert_edge_below(edge, prev, c); 1039 insert_edge_above(edge, next, c); 1040 } else { 1041 insert_edge_below(edge, next, c); 1042 insert_edge_above(edge, prev, c); 1043 } 1044 merge_collinear_edges(edge, nullptr, c); 1045 return edge; 1046} 1047 1048void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkChunkAlloc& alloc) { 1049 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY, 1050 src->fID, dst->fID); 1051 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha); 1052 for (Edge* edge = src->fFirstEdgeAbove; edge;) { 1053 Edge* next = edge->fNextEdgeAbove; 1054 set_bottom(edge, dst, nullptr, c); 1055 edge = next; 1056 } 1057 for (Edge* edge = src->fFirstEdgeBelow; edge;) { 1058 Edge* next = edge->fNextEdgeBelow; 1059 set_top(edge, dst, nullptr, c); 1060 edge = next; 1061 } 1062 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr); 1063} 1064 1065uint8_t max_edge_alpha(Edge* a, Edge* b) { 1066 return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha), 1067 SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha)); 1068} 1069 1070Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c, 1071 SkChunkAlloc& alloc) { 1072 SkPoint p; 1073 if (!edge || !other) { 1074 return nullptr; 1075 } 1076 if (edge->intersect(*other, &p)) { 1077 Vertex* v; 1078 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); 1079 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) { 1080 split_edge(other, edge->fTop, activeEdges, c, alloc); 1081 v = edge->fTop; 1082 } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) { 1083 split_edge(other, edge->fBottom, activeEdges, c, alloc); 1084 v = edge->fBottom; 1085 } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) { 1086 split_edge(edge, other->fTop, activeEdges, c, alloc); 1087 v = other->fTop; 1088 } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) { 1089 split_edge(edge, other->fBottom, activeEdges, c, alloc); 1090 v = other->fBottom; 1091 } else { 1092 Vertex* nextV = edge->fTop; 1093 while (c.sweep_lt(p, nextV->fPoint)) { 1094 nextV = nextV->fPrev; 1095 } 1096 while (c.sweep_lt(nextV->fPoint, p)) { 1097 nextV = nextV->fNext; 1098 } 1099 Vertex* prevV = nextV->fPrev; 1100 if (coincident(prevV->fPoint, p)) { 1101 v = prevV; 1102 } else if (coincident(nextV->fPoint, p)) { 1103 v = nextV; 1104 } else { 1105 uint8_t alpha = max_edge_alpha(edge, other); 1106 v = ALLOC_NEW(Vertex, (p, alpha), alloc); 1107 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n", 1108 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY, 1109 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY); 1110#if LOGGING_ENABLED 1111 v->fID = (nextV->fID + prevV->fID) * 0.5f; 1112#endif 1113 v->fPrev = prevV; 1114 v->fNext = nextV; 1115 prevV->fNext = v; 1116 nextV->fPrev = v; 1117 } 1118 split_edge(edge, v, activeEdges, c, alloc); 1119 split_edge(other, v, activeEdges, c, alloc); 1120 } 1121 return v; 1122 } 1123 return nullptr; 1124} 1125 1126void sanitize_contours(Vertex** contours, int contourCnt, bool approximate) { 1127 for (int i = 0; i < contourCnt; ++i) { 1128 SkASSERT(contours[i]); 1129 for (Vertex* v = contours[i];;) { 1130 if (approximate) { 1131 round(&v->fPoint); 1132 } 1133 if (coincident(v->fPrev->fPoint, v->fPoint)) { 1134 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); 1135 if (v->fPrev == v) { 1136 contours[i] = nullptr; 1137 break; 1138 } 1139 v->fPrev->fNext = v->fNext; 1140 v->fNext->fPrev = v->fPrev; 1141 if (contours[i] == v) { 1142 contours[i] = v->fNext; 1143 } 1144 v = v->fPrev; 1145 } else { 1146 v = v->fNext; 1147 if (v == contours[i]) break; 1148 } 1149 } 1150 } 1151} 1152 1153void merge_coincident_vertices(Vertex** vertices, Comparator& c, SkChunkAlloc& alloc) { 1154 for (Vertex* v = (*vertices)->fNext; v != nullptr; v = v->fNext) { 1155 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { 1156 v->fPoint = v->fPrev->fPoint; 1157 } 1158 if (coincident(v->fPrev->fPoint, v->fPoint)) { 1159 merge_vertices(v->fPrev, v, vertices, c, alloc); 1160 } 1161 } 1162} 1163 1164// Stage 2: convert the contours to a mesh of edges connecting the vertices. 1165 1166Vertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) { 1167 Vertex* vertices = nullptr; 1168 Vertex* prev = nullptr; 1169 for (int i = 0; i < contourCnt; ++i) { 1170 for (Vertex* v = contours[i]; v != nullptr;) { 1171 Vertex* vNext = v->fNext; 1172 connect(v->fPrev, v, alloc, c); 1173 if (prev) { 1174 prev->fNext = v; 1175 v->fPrev = prev; 1176 } else { 1177 vertices = v; 1178 } 1179 prev = v; 1180 v = vNext; 1181 if (v == contours[i]) break; 1182 } 1183 } 1184 if (prev) { 1185 prev->fNext = vertices->fPrev = nullptr; 1186 } 1187 return vertices; 1188} 1189 1190// Stage 3: sort the vertices by increasing sweep direction. 1191 1192Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c); 1193 1194void front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) { 1195 Vertex* fast; 1196 Vertex* slow; 1197 if (!v || !v->fNext) { 1198 *pFront = v; 1199 *pBack = nullptr; 1200 } else { 1201 slow = v; 1202 fast = v->fNext; 1203 1204 while (fast != nullptr) { 1205 fast = fast->fNext; 1206 if (fast != nullptr) { 1207 slow = slow->fNext; 1208 fast = fast->fNext; 1209 } 1210 } 1211 1212 *pFront = v; 1213 *pBack = slow->fNext; 1214 slow->fNext->fPrev = nullptr; 1215 slow->fNext = nullptr; 1216 } 1217} 1218 1219void merge_sort(Vertex** head, Comparator& c) { 1220 if (!*head || !(*head)->fNext) { 1221 return; 1222 } 1223 1224 Vertex* a; 1225 Vertex* b; 1226 front_back_split(*head, &a, &b); 1227 1228 merge_sort(&a, c); 1229 merge_sort(&b, c); 1230 1231 *head = sorted_merge(a, b, c); 1232} 1233 1234Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c) { 1235 VertexList vertices; 1236 1237 while (a && b) { 1238 if (c.sweep_lt(a->fPoint, b->fPoint)) { 1239 Vertex* next = a->fNext; 1240 vertices.append(a); 1241 a = next; 1242 } else { 1243 Vertex* next = b->fNext; 1244 vertices.append(b); 1245 b = next; 1246 } 1247 } 1248 if (a) { 1249 vertices.insert(a, vertices.fTail, a->fNext); 1250 } 1251 if (b) { 1252 vertices.insert(b, vertices.fTail, b->fNext); 1253 } 1254 return vertices.fHead; 1255} 1256 1257// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. 1258 1259void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) { 1260 LOG("simplifying complex polygons\n"); 1261 EdgeList activeEdges; 1262 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1263 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1264 continue; 1265 } 1266#if LOGGING_ENABLED 1267 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1268#endif 1269 Edge* leftEnclosingEdge = nullptr; 1270 Edge* rightEnclosingEdge = nullptr; 1271 bool restartChecks; 1272 do { 1273 restartChecks = false; 1274 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1275 if (v->fFirstEdgeBelow) { 1276 for (Edge* edge = v->fFirstEdgeBelow; edge != nullptr; edge = edge->fNextEdgeBelow) { 1277 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) { 1278 restartChecks = true; 1279 break; 1280 } 1281 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) { 1282 restartChecks = true; 1283 break; 1284 } 1285 } 1286 } else { 1287 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, 1288 &activeEdges, c, alloc)) { 1289 if (c.sweep_lt(pv->fPoint, v->fPoint)) { 1290 v = pv; 1291 } 1292 restartChecks = true; 1293 } 1294 1295 } 1296 } while (restartChecks); 1297 if (v->fAlpha == 0) { 1298 if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) && 1299 (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) { 1300 v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge); 1301 } 1302 } 1303 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1304 remove_edge(e, &activeEdges); 1305 } 1306 Edge* leftEdge = leftEnclosingEdge; 1307 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1308 insert_edge(e, leftEdge, &activeEdges); 1309 leftEdge = e; 1310 } 1311 v->fProcessed = true; 1312 } 1313} 1314 1315// Stage 5: Tessellate the simplified mesh into monotone polygons. 1316 1317Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) { 1318 LOG("tessellating simple polygons\n"); 1319 EdgeList activeEdges; 1320 Poly* polys = nullptr; 1321 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1322 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1323 continue; 1324 } 1325#if LOGGING_ENABLED 1326 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1327#endif 1328 Edge* leftEnclosingEdge = nullptr; 1329 Edge* rightEnclosingEdge = nullptr; 1330 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1331 Poly* leftPoly = nullptr; 1332 Poly* rightPoly = nullptr; 1333 if (v->fFirstEdgeAbove) { 1334 leftPoly = v->fFirstEdgeAbove->fLeftPoly; 1335 rightPoly = v->fLastEdgeAbove->fRightPoly; 1336 } else { 1337 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; 1338 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; 1339 } 1340#if LOGGING_ENABLED 1341 LOG("edges above:\n"); 1342 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1343 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1344 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1345 } 1346 LOG("edges below:\n"); 1347 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1348 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1349 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1350 } 1351#endif 1352 if (v->fFirstEdgeAbove) { 1353 if (leftPoly) { 1354 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc); 1355 } 1356 if (rightPoly) { 1357 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc); 1358 } 1359 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { 1360 Edge* leftEdge = e; 1361 Edge* rightEdge = e->fNextEdgeAbove; 1362 SkASSERT(rightEdge->isRightOf(leftEdge->fTop)); 1363 remove_edge(leftEdge, &activeEdges); 1364 if (leftEdge->fRightPoly) { 1365 leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc); 1366 } 1367 if (rightEdge->fLeftPoly) { 1368 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc); 1369 } 1370 } 1371 remove_edge(v->fLastEdgeAbove, &activeEdges); 1372 if (!v->fFirstEdgeBelow) { 1373 if (leftPoly && rightPoly && leftPoly != rightPoly) { 1374 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); 1375 rightPoly->fPartner = leftPoly; 1376 leftPoly->fPartner = rightPoly; 1377 } 1378 } 1379 } 1380 if (v->fFirstEdgeBelow) { 1381 if (!v->fFirstEdgeAbove) { 1382 if (leftPoly && rightPoly) { 1383 if (leftPoly == rightPoly) { 1384 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) { 1385 leftPoly = new_poly(&polys, leftPoly->lastVertex(), 1386 leftPoly->fWinding, alloc); 1387 leftEnclosingEdge->fRightPoly = leftPoly; 1388 } else { 1389 rightPoly = new_poly(&polys, rightPoly->lastVertex(), 1390 rightPoly->fWinding, alloc); 1391 rightEnclosingEdge->fLeftPoly = rightPoly; 1392 } 1393 } 1394 Edge* join = ALLOC_NEW(Edge, (leftPoly->lastVertex(), v, 1), alloc); 1395 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc); 1396 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc); 1397 } 1398 } 1399 Edge* leftEdge = v->fFirstEdgeBelow; 1400 leftEdge->fLeftPoly = leftPoly; 1401 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges); 1402 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; 1403 rightEdge = rightEdge->fNextEdgeBelow) { 1404 insert_edge(rightEdge, leftEdge, &activeEdges); 1405 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; 1406 winding += leftEdge->fWinding; 1407 if (winding != 0) { 1408 Poly* poly = new_poly(&polys, v, winding, alloc); 1409 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; 1410 } 1411 leftEdge = rightEdge; 1412 } 1413 v->fLastEdgeBelow->fRightPoly = rightPoly; 1414 } 1415#if LOGGING_ENABLED 1416 LOG("\nactive edges:\n"); 1417 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { 1418 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1419 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1420 } 1421#endif 1422 } 1423 return polys; 1424} 1425 1426bool is_boundary_edge(Edge* edge, SkPath::FillType fillType) { 1427 return apply_fill_type(fillType, edge->fLeftPoly) != 1428 apply_fill_type(fillType, edge->fRightPoly); 1429} 1430 1431bool is_boundary_start(Edge* edge, SkPath::FillType fillType) { 1432 return !apply_fill_type(fillType, edge->fLeftPoly) && 1433 apply_fill_type(fillType, edge->fRightPoly); 1434} 1435 1436Vertex* remove_non_boundary_edges(Vertex* vertices, SkPath::FillType fillType, 1437 SkChunkAlloc& alloc) { 1438 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1439 for (Edge* e = v->fFirstEdgeBelow; e != nullptr;) { 1440 Edge* next = e->fNextEdgeBelow; 1441 if (!is_boundary_edge(e, fillType)) { 1442 remove_edge_above(e); 1443 remove_edge_below(e); 1444 } 1445 e = next; 1446 } 1447 } 1448 return vertices; 1449} 1450 1451void get_edge_normal(const Edge* e, SkVector* normal) { 1452 normal->setNormalize(SkDoubleToScalar(-e->fLine.fB) * e->fWinding, 1453 SkDoubleToScalar(e->fLine.fA) * e->fWinding); 1454} 1455 1456// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions 1457// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to 1458// invert on stroking. 1459 1460void simplify_boundary(EdgeList* boundary, Comparator& c, SkChunkAlloc& alloc) { 1461 Edge* prevEdge = boundary->fTail; 1462 SkVector prevNormal; 1463 get_edge_normal(prevEdge, &prevNormal); 1464 for (Edge* e = boundary->fHead; e != nullptr;) { 1465 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom; 1466 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop; 1467 double dist = e->dist(prev->fPoint); 1468 SkVector normal; 1469 get_edge_normal(e, &normal); 1470 float denom = 0.25f * static_cast<float>(e->fLine.magSq()); 1471 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) { 1472 Edge* join = new_edge(prev, next, alloc, c); 1473 insert_edge(join, e, boundary); 1474 remove_edge(prevEdge, boundary); 1475 remove_edge(e, boundary); 1476 if (join->fLeft && join->fRight) { 1477 prevEdge = join->fLeft; 1478 e = join; 1479 } else { 1480 prevEdge = boundary->fTail; 1481 e = boundary->fHead; // join->fLeft ? join->fLeft : join; 1482 } 1483 get_edge_normal(prevEdge, &prevNormal); 1484 } else { 1485 prevEdge = e; 1486 prevNormal = normal; 1487 e = e->fRight; 1488 } 1489 } 1490} 1491 1492// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to 1493// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a 1494// new antialiased mesh from those vertices. 1495 1496void boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkChunkAlloc& alloc) { 1497 EdgeList outerContour; 1498 Edge* prevEdge = boundary->fTail; 1499 float radius = 0.5f; 1500 double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding; 1501 Line prevInner(prevEdge->fTop, prevEdge->fBottom); 1502 prevInner.fC -= offset; 1503 Line prevOuter(prevEdge->fTop, prevEdge->fBottom); 1504 prevOuter.fC += offset; 1505 VertexList innerVertices; 1506 VertexList outerVertices; 1507 SkScalar innerCount = SK_Scalar1, outerCount = SK_Scalar1; 1508 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) { 1509 double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding; 1510 Line inner(e->fTop, e->fBottom); 1511 inner.fC -= offset; 1512 Line outer(e->fTop, e->fBottom); 1513 outer.fC += offset; 1514 SkPoint innerPoint, outerPoint; 1515 if (prevInner.intersect(inner, &innerPoint) && 1516 prevOuter.intersect(outer, &outerPoint)) { 1517 Vertex* innerVertex = ALLOC_NEW(Vertex, (innerPoint, 255), alloc); 1518 Vertex* outerVertex = ALLOC_NEW(Vertex, (outerPoint, 0), alloc); 1519 if (innerVertices.fTail && outerVertices.fTail) { 1520 Edge innerEdge(innerVertices.fTail, innerVertex, 1); 1521 Edge outerEdge(outerVertices.fTail, outerVertex, 1); 1522 SkVector innerNormal; 1523 get_edge_normal(&innerEdge, &innerNormal); 1524 SkVector outerNormal; 1525 get_edge_normal(&outerEdge, &outerNormal); 1526 SkVector normal; 1527 get_edge_normal(prevEdge, &normal); 1528 if (normal.dot(innerNormal) < 0) { 1529 innerPoint += innerVertices.fTail->fPoint * innerCount; 1530 innerCount++; 1531 innerPoint *= SkScalarInvert(innerCount); 1532 innerVertices.fTail->fPoint = innerVertex->fPoint = innerPoint; 1533 } else { 1534 innerCount = SK_Scalar1; 1535 } 1536 if (normal.dot(outerNormal) < 0) { 1537 outerPoint += outerVertices.fTail->fPoint * outerCount; 1538 outerCount++; 1539 outerPoint *= SkScalarInvert(outerCount); 1540 outerVertices.fTail->fPoint = outerVertex->fPoint = outerPoint; 1541 } else { 1542 outerCount = SK_Scalar1; 1543 } 1544 } 1545 innerVertices.append(innerVertex); 1546 outerVertices.append(outerVertex); 1547 prevEdge = e; 1548 } 1549 prevInner = inner; 1550 prevOuter = outer; 1551 } 1552 innerVertices.close(); 1553 outerVertices.close(); 1554 1555 Vertex* innerVertex = innerVertices.fHead; 1556 Vertex* outerVertex = outerVertices.fHead; 1557 // Alternate clockwise and counterclockwise polys, so the tesselator 1558 // doesn't cancel out the interior edges. 1559 if (!innerVertex || !outerVertex) { 1560 return; 1561 } 1562 do { 1563 connect(outerVertex->fNext, outerVertex, alloc, c); 1564 connect(innerVertex->fNext, innerVertex, alloc, c, 2); 1565 connect(innerVertex, outerVertex->fNext, alloc, c, 2); 1566 connect(outerVertex, innerVertex, alloc, c, 2); 1567 Vertex* innerNext = innerVertex->fNext; 1568 Vertex* outerNext = outerVertex->fNext; 1569 mesh->append(innerVertex); 1570 mesh->append(outerVertex); 1571 innerVertex = innerNext; 1572 outerVertex = outerNext; 1573 } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead); 1574} 1575 1576void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkChunkAlloc& alloc) { 1577 bool down = is_boundary_start(e, fillType); 1578 while (e) { 1579 e->fWinding = down ? 1 : -1; 1580 Edge* next; 1581 boundary->append(e); 1582 if (down) { 1583 // Find outgoing edge, in clockwise order. 1584 if ((next = e->fNextEdgeAbove)) { 1585 down = false; 1586 } else if ((next = e->fBottom->fLastEdgeBelow)) { 1587 down = true; 1588 } else if ((next = e->fPrevEdgeAbove)) { 1589 down = false; 1590 } 1591 } else { 1592 // Find outgoing edge, in counter-clockwise order. 1593 if ((next = e->fPrevEdgeBelow)) { 1594 down = true; 1595 } else if ((next = e->fTop->fFirstEdgeAbove)) { 1596 down = false; 1597 } else if ((next = e->fNextEdgeBelow)) { 1598 down = true; 1599 } 1600 } 1601 remove_edge_above(e); 1602 remove_edge_below(e); 1603 e = next; 1604 } 1605} 1606 1607// Stage 5b: Extract boundary edges. 1608 1609EdgeList* extract_boundaries(Vertex* vertices, SkPath::FillType fillType, SkChunkAlloc& alloc) { 1610 LOG("extracting boundaries\n"); 1611 vertices = remove_non_boundary_edges(vertices, fillType, alloc); 1612 EdgeList* boundaries = nullptr; 1613 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1614 while (v->fFirstEdgeBelow) { 1615 EdgeList* boundary = new_contour(&boundaries, alloc); 1616 extract_boundary(boundary, v->fFirstEdgeBelow, fillType, alloc); 1617 } 1618 } 1619 return boundaries; 1620} 1621 1622// This is a driver function which calls stages 2-5 in turn. 1623 1624Vertex* contours_to_mesh(Vertex** contours, int contourCnt, bool antialias, 1625 Comparator& c, SkChunkAlloc& alloc) { 1626#if LOGGING_ENABLED 1627 for (int i = 0; i < contourCnt; ++i) { 1628 Vertex* v = contours[i]; 1629 SkASSERT(v); 1630 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1631 for (v = v->fNext; v != contours[i]; v = v->fNext) { 1632 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1633 } 1634 } 1635#endif 1636 sanitize_contours(contours, contourCnt, antialias); 1637 return build_edges(contours, contourCnt, c, alloc); 1638} 1639 1640Poly* mesh_to_polys(Vertex** vertices, Comparator& c, SkChunkAlloc& alloc) { 1641 if (!vertices || !*vertices) { 1642 return nullptr; 1643 } 1644 1645 // Sort vertices in Y (secondarily in X). 1646 merge_sort(vertices, c); 1647 merge_coincident_vertices(vertices, c, alloc); 1648#if LOGGING_ENABLED 1649 for (Vertex* v = *vertices; v != nullptr; v = v->fNext) { 1650 static float gID = 0.0f; 1651 v->fID = gID++; 1652 } 1653#endif 1654 simplify(*vertices, c, alloc); 1655 return tessellate(*vertices, alloc); 1656} 1657 1658Poly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType, 1659 const SkRect& pathBounds, bool antialias, 1660 SkChunkAlloc& alloc) { 1661 Comparator c; 1662 if (pathBounds.width() > pathBounds.height()) { 1663 c.sweep_lt = sweep_lt_horiz; 1664 c.sweep_gt = sweep_gt_horiz; 1665 } else { 1666 c.sweep_lt = sweep_lt_vert; 1667 c.sweep_gt = sweep_gt_vert; 1668 } 1669 Vertex* mesh = contours_to_mesh(contours, contourCnt, antialias, c, alloc); 1670 Poly* polys = mesh_to_polys(&mesh, c, alloc); 1671 if (antialias) { 1672 EdgeList* boundaries = extract_boundaries(mesh, fillType, alloc); 1673 VertexList aaMesh; 1674 for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = boundary->fNext) { 1675 simplify_boundary(boundary, c, alloc); 1676 if (boundary->fCount > 2) { 1677 boundary_to_aa_mesh(boundary, &aaMesh, c, alloc); 1678 } 1679 } 1680 return mesh_to_polys(&aaMesh.fHead, c, alloc); 1681 } 1682 return polys; 1683} 1684 1685// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1686void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams, 1687 void* data) { 1688 for (Poly* poly = polys; poly; poly = poly->fNext) { 1689 if (apply_fill_type(fillType, poly)) { 1690 data = poly->emit(aaParams, data); 1691 } 1692 } 1693 return data; 1694} 1695 1696Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1697 int contourCnt, SkChunkAlloc& alloc, bool antialias, bool* isLinear) { 1698 SkPath::FillType fillType = path.getFillType(); 1699 if (SkPath::IsInverseFillType(fillType)) { 1700 contourCnt++; 1701 } 1702 std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]); 1703 1704 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); 1705 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(), 1706 antialias, alloc); 1707} 1708 1709void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt, 1710 int* sizeEstimate) { 1711 int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance); 1712 if (maxPts <= 0) { 1713 *contourCnt = 0; 1714 return; 1715 } 1716 if (maxPts > ((int)SK_MaxU16 + 1)) { 1717 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); 1718 *contourCnt = 0; 1719 return; 1720 } 1721 // For the initial size of the chunk allocator, estimate based on the point count: 1722 // one vertex per point for the initial passes, plus two for the vertices in the 1723 // resulting Polys, since the same point may end up in two Polys. Assume minimal 1724 // connectivity of one Edge per Vertex (will grow for intersections). 1725 *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge)); 1726} 1727 1728int count_points(Poly* polys, SkPath::FillType fillType) { 1729 int count = 0; 1730 for (Poly* poly = polys; poly; poly = poly->fNext) { 1731 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) { 1732 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); 1733 } 1734 } 1735 return count; 1736} 1737 1738} // namespace 1739 1740namespace GrTessellator { 1741 1742// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1743 1744int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1745 VertexAllocator* vertexAllocator, bool antialias, const GrColor& color, 1746 bool canTweakAlphaForCoverage, bool* isLinear) { 1747 int contourCnt; 1748 int sizeEstimate; 1749 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate); 1750 if (contourCnt <= 0) { 1751 *isLinear = true; 1752 return 0; 1753 } 1754 SkChunkAlloc alloc(sizeEstimate); 1755 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias, 1756 isLinear); 1757 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType(); 1758 int count = count_points(polys, fillType); 1759 if (0 == count) { 1760 return 0; 1761 } 1762 1763 void* verts = vertexAllocator->lock(count); 1764 if (!verts) { 1765 SkDebugf("Could not allocate vertices\n"); 1766 return 0; 1767 } 1768 1769 LOG("emitting %d verts\n", count); 1770 AAParams aaParams; 1771 aaParams.fTweakAlpha = canTweakAlphaForCoverage; 1772 aaParams.fColor = color; 1773 1774 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts); 1775 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts)) 1776 / vertexAllocator->stride()); 1777 SkASSERT(actualCount <= count); 1778 vertexAllocator->unlock(actualCount); 1779 return actualCount; 1780} 1781 1782int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1783 GrTessellator::WindingVertex** verts) { 1784 int contourCnt; 1785 int sizeEstimate; 1786 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate); 1787 if (contourCnt <= 0) { 1788 return 0; 1789 } 1790 SkChunkAlloc alloc(sizeEstimate); 1791 bool isLinear; 1792 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear); 1793 SkPath::FillType fillType = path.getFillType(); 1794 int count = count_points(polys, fillType); 1795 if (0 == count) { 1796 *verts = nullptr; 1797 return 0; 1798 } 1799 1800 *verts = new GrTessellator::WindingVertex[count]; 1801 GrTessellator::WindingVertex* vertsEnd = *verts; 1802 SkPoint* points = new SkPoint[count]; 1803 SkPoint* pointsEnd = points; 1804 for (Poly* poly = polys; poly; poly = poly->fNext) { 1805 if (apply_fill_type(fillType, poly)) { 1806 SkPoint* start = pointsEnd; 1807 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd)); 1808 while (start != pointsEnd) { 1809 vertsEnd->fPos = *start; 1810 vertsEnd->fWinding = poly->fWinding; 1811 ++start; 1812 ++vertsEnd; 1813 } 1814 } 1815 } 1816 int actualCount = static_cast<int>(vertsEnd - *verts); 1817 SkASSERT(actualCount <= count); 1818 SkASSERT(pointsEnd - points == actualCount); 1819 delete[] points; 1820 return actualCount; 1821} 1822 1823} // namespace 1824