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