GrTessellator.cpp revision 5926f2da752d1bff0051fda4137bb967f9e91d5f
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 if (approximate) { 1153 round(&contours[i]->fPrev->fPoint); 1154 } 1155 for (Vertex* v = contours[i];;) { 1156 if (approximate) { 1157 round(&v->fPoint); 1158 } 1159 if (coincident(v->fPrev->fPoint, v->fPoint)) { 1160 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); 1161 if (v->fPrev == v) { 1162 contours[i] = nullptr; 1163 break; 1164 } 1165 v->fPrev->fNext = v->fNext; 1166 v->fNext->fPrev = v->fPrev; 1167 if (contours[i] == v) { 1168 contours[i] = v->fPrev; 1169 } 1170 v = v->fPrev; 1171 } else { 1172 v = v->fNext; 1173 if (v == contours[i]) break; 1174 } 1175 } 1176 } 1177} 1178 1179void merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1180 for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) { 1181 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { 1182 v->fPoint = v->fPrev->fPoint; 1183 } 1184 if (coincident(v->fPrev->fPoint, v->fPoint)) { 1185 merge_vertices(v->fPrev, v, mesh, c, alloc); 1186 } 1187 } 1188} 1189 1190// Stage 2: convert the contours to a mesh of edges connecting the vertices. 1191 1192void build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c, 1193 SkArenaAlloc& alloc) { 1194 Vertex* prev = nullptr; 1195 for (int i = 0; i < contourCnt; ++i) { 1196 for (Vertex* v = contours[i]; v != nullptr;) { 1197 Vertex* vNext = v->fNext; 1198 connect(v->fPrev, v, Edge::Type::kInner, c, alloc); 1199 if (prev) { 1200 prev->fNext = v; 1201 v->fPrev = prev; 1202 } else { 1203 mesh->fHead = v; 1204 } 1205 prev = v; 1206 v = vNext; 1207 if (v == contours[i]) break; 1208 } 1209 } 1210 if (prev) { 1211 prev->fNext = mesh->fHead->fPrev = nullptr; 1212 } 1213 mesh->fTail = prev; 1214} 1215 1216// Stage 3: sort the vertices by increasing sweep direction. 1217 1218void sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c); 1219 1220void front_back_split(VertexList* v, VertexList* front, VertexList* back) { 1221 Vertex* fast; 1222 Vertex* slow; 1223 if (!v->fHead || !v->fHead->fNext) { 1224 *front = *v; 1225 } else { 1226 slow = v->fHead; 1227 fast = v->fHead->fNext; 1228 1229 while (fast != nullptr) { 1230 fast = fast->fNext; 1231 if (fast != nullptr) { 1232 slow = slow->fNext; 1233 fast = fast->fNext; 1234 } 1235 } 1236 front->fHead = v->fHead; 1237 front->fTail = slow; 1238 back->fHead = slow->fNext; 1239 back->fTail = v->fTail; 1240 slow->fNext->fPrev = nullptr; 1241 slow->fNext = nullptr; 1242 } 1243 v->fHead = v->fTail = nullptr; 1244} 1245 1246void merge_sort(VertexList* mesh, Comparator& c) { 1247 if (!mesh->fHead || !mesh->fHead->fNext) { 1248 return; 1249 } 1250 1251 VertexList a; 1252 VertexList b; 1253 front_back_split(mesh, &a, &b); 1254 1255 merge_sort(&a, c); 1256 merge_sort(&b, c); 1257 1258 sorted_merge(a.fHead, b.fHead, mesh, c); 1259} 1260 1261void sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c) { 1262 VertexList vertices; 1263 while (a && b) { 1264 if (c.sweep_lt(a->fPoint, b->fPoint)) { 1265 Vertex* next = a->fNext; 1266 vertices.append(a); 1267 a = next; 1268 } else { 1269 Vertex* next = b->fNext; 1270 vertices.append(b); 1271 b = next; 1272 } 1273 } 1274 if (a) { 1275 vertices.insert(a, vertices.fTail, a->fNext); 1276 } 1277 if (b) { 1278 vertices.insert(b, vertices.fTail, b->fNext); 1279 } 1280 *result = vertices; 1281} 1282 1283// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. 1284 1285void simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) { 1286 LOG("simplifying complex polygons\n"); 1287 EdgeList activeEdges; 1288 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { 1289 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1290 continue; 1291 } 1292#if LOGGING_ENABLED 1293 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1294#endif 1295 Edge* leftEnclosingEdge = nullptr; 1296 Edge* rightEnclosingEdge = nullptr; 1297 bool restartChecks; 1298 do { 1299 restartChecks = false; 1300 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1301 if (v->fFirstEdgeBelow) { 1302 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) { 1303 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) { 1304 restartChecks = true; 1305 break; 1306 } 1307 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) { 1308 restartChecks = true; 1309 break; 1310 } 1311 } 1312 } else { 1313 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, 1314 &activeEdges, c, alloc)) { 1315 if (c.sweep_lt(pv->fPoint, v->fPoint)) { 1316 v = pv; 1317 } 1318 restartChecks = true; 1319 } 1320 1321 } 1322 } while (restartChecks); 1323 if (v->fAlpha == 0) { 1324 if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) && 1325 (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) { 1326 v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge); 1327 } 1328 } 1329 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1330 remove_edge(e, &activeEdges); 1331 } 1332 Edge* leftEdge = leftEnclosingEdge; 1333 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1334 insert_edge(e, leftEdge, &activeEdges); 1335 leftEdge = e; 1336 } 1337 v->fProcessed = true; 1338 } 1339} 1340 1341// Stage 5: Tessellate the simplified mesh into monotone polygons. 1342 1343Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) { 1344 LOG("tessellating simple polygons\n"); 1345 EdgeList activeEdges; 1346 Poly* polys = nullptr; 1347 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { 1348 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1349 continue; 1350 } 1351#if LOGGING_ENABLED 1352 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1353#endif 1354 Edge* leftEnclosingEdge = nullptr; 1355 Edge* rightEnclosingEdge = nullptr; 1356 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1357 Poly* leftPoly = nullptr; 1358 Poly* rightPoly = nullptr; 1359 if (v->fFirstEdgeAbove) { 1360 leftPoly = v->fFirstEdgeAbove->fLeftPoly; 1361 rightPoly = v->fLastEdgeAbove->fRightPoly; 1362 } else { 1363 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; 1364 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; 1365 } 1366#if LOGGING_ENABLED 1367 LOG("edges above:\n"); 1368 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1369 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1370 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1371 } 1372 LOG("edges below:\n"); 1373 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1374 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1375 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1376 } 1377#endif 1378 if (v->fFirstEdgeAbove) { 1379 if (leftPoly) { 1380 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc); 1381 } 1382 if (rightPoly) { 1383 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc); 1384 } 1385 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { 1386 Edge* leftEdge = e; 1387 Edge* rightEdge = e->fNextEdgeAbove; 1388 SkASSERT(rightEdge->isRightOf(leftEdge->fTop)); 1389 remove_edge(leftEdge, &activeEdges); 1390 if (leftEdge->fRightPoly) { 1391 leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc); 1392 } 1393 if (rightEdge->fLeftPoly) { 1394 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc); 1395 } 1396 } 1397 remove_edge(v->fLastEdgeAbove, &activeEdges); 1398 if (!v->fFirstEdgeBelow) { 1399 if (leftPoly && rightPoly && leftPoly != rightPoly) { 1400 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); 1401 rightPoly->fPartner = leftPoly; 1402 leftPoly->fPartner = rightPoly; 1403 } 1404 } 1405 } 1406 if (v->fFirstEdgeBelow) { 1407 if (!v->fFirstEdgeAbove) { 1408 if (leftPoly && rightPoly) { 1409 if (leftPoly == rightPoly) { 1410 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) { 1411 leftPoly = new_poly(&polys, leftPoly->lastVertex(), 1412 leftPoly->fWinding, alloc); 1413 leftEnclosingEdge->fRightPoly = leftPoly; 1414 } else { 1415 rightPoly = new_poly(&polys, rightPoly->lastVertex(), 1416 rightPoly->fWinding, alloc); 1417 rightEnclosingEdge->fLeftPoly = rightPoly; 1418 } 1419 } 1420 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner); 1421 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc); 1422 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc); 1423 } 1424 } 1425 Edge* leftEdge = v->fFirstEdgeBelow; 1426 leftEdge->fLeftPoly = leftPoly; 1427 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges); 1428 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; 1429 rightEdge = rightEdge->fNextEdgeBelow) { 1430 insert_edge(rightEdge, leftEdge, &activeEdges); 1431 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; 1432 winding += leftEdge->fWinding; 1433 if (winding != 0) { 1434 Poly* poly = new_poly(&polys, v, winding, alloc); 1435 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; 1436 } 1437 leftEdge = rightEdge; 1438 } 1439 v->fLastEdgeBelow->fRightPoly = rightPoly; 1440 } 1441#if LOGGING_ENABLED 1442 LOG("\nactive edges:\n"); 1443 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { 1444 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1445 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1446 } 1447#endif 1448 } 1449 return polys; 1450} 1451 1452bool is_boundary_edge(Edge* edge, SkPath::FillType fillType) { 1453 return apply_fill_type(fillType, edge->fLeftPoly) != 1454 apply_fill_type(fillType, edge->fRightPoly); 1455} 1456 1457bool is_boundary_start(Edge* edge, SkPath::FillType fillType) { 1458 return !apply_fill_type(fillType, edge->fLeftPoly) && 1459 apply_fill_type(fillType, edge->fRightPoly); 1460} 1461 1462void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType, 1463 SkArenaAlloc& alloc) { 1464 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) { 1465 for (Edge* e = v->fFirstEdgeBelow; e != nullptr;) { 1466 Edge* next = e->fNextEdgeBelow; 1467 if (!is_boundary_edge(e, fillType)) { 1468 disconnect(e); 1469 } 1470 e = next; 1471 } 1472 } 1473} 1474 1475void get_edge_normal(const Edge* e, SkVector* normal) { 1476 normal->setNormalize(SkDoubleToScalar(e->fLine.fA) * e->fWinding, 1477 SkDoubleToScalar(e->fLine.fB) * e->fWinding); 1478} 1479 1480// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions 1481// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to 1482// invert on stroking. 1483 1484void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) { 1485 Edge* prevEdge = boundary->fTail; 1486 SkVector prevNormal; 1487 get_edge_normal(prevEdge, &prevNormal); 1488 for (Edge* e = boundary->fHead; e != nullptr;) { 1489 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom; 1490 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop; 1491 double dist = e->dist(prev->fPoint); 1492 SkVector normal; 1493 get_edge_normal(e, &normal); 1494 float denom = 0.0625f * static_cast<float>(e->fLine.magSq()); 1495 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) { 1496 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc); 1497 insert_edge(join, e, boundary); 1498 remove_edge(prevEdge, boundary); 1499 remove_edge(e, boundary); 1500 if (join->fLeft && join->fRight) { 1501 prevEdge = join->fLeft; 1502 e = join; 1503 } else { 1504 prevEdge = boundary->fTail; 1505 e = boundary->fHead; // join->fLeft ? join->fLeft : join; 1506 } 1507 get_edge_normal(prevEdge, &prevNormal); 1508 } else { 1509 prevEdge = e; 1510 prevNormal = normal; 1511 e = e->fRight; 1512 } 1513 } 1514} 1515 1516void fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBisector, 1517 Edge* prevEdge, Comparator& c) { 1518 if (!prev || !next) { 1519 return; 1520 } 1521 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; 1522 SkPoint p; 1523 uint8_t alpha; 1524 if (winding != prevEdge->fWinding && prevBisector->intersect(*nextBisector, &p, &alpha)) { 1525 prev->fPoint = next->fPoint = p; 1526 prev->fAlpha = next->fAlpha = alpha; 1527 } 1528} 1529 1530// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to 1531// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a 1532// new antialiased mesh from those vertices. 1533 1534void boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1535 Edge* prevEdge = boundary->fTail; 1536 float radius = 0.5f; 1537 double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding; 1538 Line prevInner(prevEdge->fTop, prevEdge->fBottom); 1539 prevInner.fC -= offset; 1540 Line prevOuter(prevEdge->fTop, prevEdge->fBottom); 1541 prevOuter.fC += offset; 1542 VertexList innerVertices; 1543 VertexList outerVertices; 1544 Edge* prevBisector = nullptr; 1545 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) { 1546 double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding; 1547 Line inner(e->fTop, e->fBottom); 1548 inner.fC -= offset; 1549 Line outer(e->fTop, e->fBottom); 1550 outer.fC += offset; 1551 SkPoint innerPoint, outerPoint; 1552 SkVector normal; 1553 get_edge_normal(e, &normal); 1554 if (prevInner.intersect(inner, &innerPoint) && 1555 prevOuter.intersect(outer, &outerPoint)) { 1556 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255); 1557 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0); 1558 Edge* bisector = new_edge(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc); 1559 fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c); 1560 fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c); 1561 innerVertices.append(innerVertex); 1562 outerVertices.append(outerVertex); 1563 prevBisector = bisector; 1564 } 1565 prevInner = inner; 1566 prevOuter = outer; 1567 prevEdge = e; 1568 } 1569 innerVertices.close(); 1570 outerVertices.close(); 1571 1572 Vertex* innerVertex = innerVertices.fHead; 1573 Vertex* outerVertex = outerVertices.fHead; 1574 if (!innerVertex || !outerVertex) { 1575 return; 1576 } 1577 Edge* bisector = new_edge(outerVertices.fHead, innerVertices.fHead, Edge::Type::kConnector, c, 1578 alloc); 1579 fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c); 1580 fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c); 1581 do { 1582 // Connect vertices into a quad mesh. Outer edges get default (1) winding. 1583 // Inner edges get -2 winding. This ensures that the interior is always filled 1584 // (-1 winding number for normal cases, 3 for thin features where the interior inverts). 1585 // Connector edges get zero winding, since they're only structural (i.e., to ensure 1586 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding number. 1587 connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc); 1588 connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc, -2); 1589 connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc, 0); 1590 Vertex* innerNext = innerVertex->fNext; 1591 Vertex* outerNext = outerVertex->fNext; 1592 mesh->append(innerVertex); 1593 mesh->append(outerVertex); 1594 innerVertex = innerNext; 1595 outerVertex = outerNext; 1596 } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead); 1597} 1598 1599void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) { 1600 bool down = is_boundary_start(e, fillType); 1601 while (e) { 1602 e->fWinding = down ? 1 : -1; 1603 Edge* next; 1604 boundary->append(e); 1605 if (down) { 1606 // Find outgoing edge, in clockwise order. 1607 if ((next = e->fNextEdgeAbove)) { 1608 down = false; 1609 } else if ((next = e->fBottom->fLastEdgeBelow)) { 1610 down = true; 1611 } else if ((next = e->fPrevEdgeAbove)) { 1612 down = false; 1613 } 1614 } else { 1615 // Find outgoing edge, in counter-clockwise order. 1616 if ((next = e->fPrevEdgeBelow)) { 1617 down = true; 1618 } else if ((next = e->fTop->fFirstEdgeAbove)) { 1619 down = false; 1620 } else if ((next = e->fNextEdgeBelow)) { 1621 down = true; 1622 } 1623 } 1624 disconnect(e); 1625 e = next; 1626 } 1627} 1628 1629// Stage 5b: Extract boundary edges. 1630 1631EdgeList* extract_boundaries(const VertexList& mesh, SkPath::FillType fillType, 1632 SkArenaAlloc& alloc) { 1633 LOG("extracting boundaries\n"); 1634 remove_non_boundary_edges(mesh, fillType, alloc); 1635 EdgeList* boundaries = nullptr; 1636 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) { 1637 while (v->fFirstEdgeBelow) { 1638 EdgeList* boundary = new_contour(&boundaries, alloc); 1639 extract_boundary(boundary, v->fFirstEdgeBelow, fillType, alloc); 1640 } 1641 } 1642 return boundaries; 1643} 1644 1645// This is a driver function which calls stages 2-5 in turn. 1646 1647void contours_to_mesh(Vertex** contours, int contourCnt, bool antialias, 1648 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1649#if LOGGING_ENABLED 1650 for (int i = 0; i < contourCnt; ++i) { 1651 Vertex* v = contours[i]; 1652 SkASSERT(v); 1653 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1654 for (v = v->fNext; v != contours[i]; v = v->fNext) { 1655 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1656 } 1657 } 1658#endif 1659 sanitize_contours(contours, contourCnt, antialias); 1660 build_edges(contours, contourCnt, mesh, c, alloc); 1661} 1662 1663void sort_and_simplify(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) { 1664 if (!vertices || !vertices->fHead) { 1665 return; 1666 } 1667 1668 // Sort vertices in Y (secondarily in X). 1669 merge_sort(vertices, c); 1670 merge_coincident_vertices(vertices, c, alloc); 1671#if LOGGING_ENABLED 1672 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) { 1673 static float gID = 0.0f; 1674 v->fID = gID++; 1675 } 1676#endif 1677 simplify(*vertices, c, alloc); 1678} 1679 1680Poly* mesh_to_polys(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) { 1681 sort_and_simplify(vertices, c, alloc); 1682 return tessellate(*vertices, alloc); 1683} 1684 1685Poly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType, 1686 const SkRect& pathBounds, bool antialias, 1687 SkArenaAlloc& alloc) { 1688 Comparator c; 1689 if (pathBounds.width() > pathBounds.height()) { 1690 c.sweep_lt = sweep_lt_horiz; 1691 c.sweep_gt = sweep_gt_horiz; 1692 } else { 1693 c.sweep_lt = sweep_lt_vert; 1694 c.sweep_gt = sweep_gt_vert; 1695 } 1696 VertexList mesh; 1697 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc); 1698 Poly* polys = mesh_to_polys(&mesh, c, alloc); 1699 if (antialias) { 1700 EdgeList* boundaries = extract_boundaries(mesh, fillType, alloc); 1701 VertexList aaMesh; 1702 for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = boundary->fNext) { 1703 simplify_boundary(boundary, c, alloc); 1704 if (boundary->fCount > 2) { 1705 boundary_to_aa_mesh(boundary, &aaMesh, c, alloc); 1706 } 1707 } 1708 sort_and_simplify(&aaMesh, c, alloc); 1709 return tessellate(aaMesh, alloc); 1710 } 1711 return polys; 1712} 1713 1714// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1715void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams, 1716 void* data) { 1717 for (Poly* poly = polys; poly; poly = poly->fNext) { 1718 if (apply_fill_type(fillType, poly)) { 1719 data = poly->emit(aaParams, data); 1720 } 1721 } 1722 return data; 1723} 1724 1725Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1726 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear) { 1727 SkPath::FillType fillType = path.getFillType(); 1728 if (SkPath::IsInverseFillType(fillType)) { 1729 contourCnt++; 1730 } 1731 std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]); 1732 1733 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); 1734 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(), 1735 antialias, alloc); 1736} 1737 1738void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt, 1739 int* sizeEstimate) { 1740 int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance); 1741 if (maxPts <= 0) { 1742 *contourCnt = 0; 1743 return; 1744 } 1745 if (maxPts > ((int)SK_MaxU16 + 1)) { 1746 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); 1747 *contourCnt = 0; 1748 return; 1749 } 1750 // For the initial size of the chunk allocator, estimate based on the point count: 1751 // one vertex per point for the initial passes, plus two for the vertices in the 1752 // resulting Polys, since the same point may end up in two Polys. Assume minimal 1753 // connectivity of one Edge per Vertex (will grow for intersections). 1754 *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge)); 1755} 1756 1757int count_points(Poly* polys, SkPath::FillType fillType) { 1758 int count = 0; 1759 for (Poly* poly = polys; poly; poly = poly->fNext) { 1760 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) { 1761 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); 1762 } 1763 } 1764 return count; 1765} 1766 1767} // namespace 1768 1769namespace GrTessellator { 1770 1771// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1772 1773int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1774 VertexAllocator* vertexAllocator, bool antialias, const GrColor& color, 1775 bool canTweakAlphaForCoverage, bool* isLinear) { 1776 int contourCnt; 1777 int sizeEstimate; 1778 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate); 1779 if (contourCnt <= 0) { 1780 *isLinear = true; 1781 return 0; 1782 } 1783 SkArenaAlloc alloc(sizeEstimate); 1784 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias, 1785 isLinear); 1786 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType(); 1787 int count = count_points(polys, fillType); 1788 if (0 == count) { 1789 return 0; 1790 } 1791 1792 void* verts = vertexAllocator->lock(count); 1793 if (!verts) { 1794 SkDebugf("Could not allocate vertices\n"); 1795 return 0; 1796 } 1797 1798 LOG("emitting %d verts\n", count); 1799 AAParams aaParams; 1800 aaParams.fTweakAlpha = canTweakAlphaForCoverage; 1801 aaParams.fColor = color; 1802 1803 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts); 1804 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts)) 1805 / vertexAllocator->stride()); 1806 SkASSERT(actualCount <= count); 1807 vertexAllocator->unlock(actualCount); 1808 return actualCount; 1809} 1810 1811int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1812 GrTessellator::WindingVertex** verts) { 1813 int contourCnt; 1814 int sizeEstimate; 1815 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate); 1816 if (contourCnt <= 0) { 1817 return 0; 1818 } 1819 SkArenaAlloc alloc(sizeEstimate); 1820 bool isLinear; 1821 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear); 1822 SkPath::FillType fillType = path.getFillType(); 1823 int count = count_points(polys, fillType); 1824 if (0 == count) { 1825 *verts = nullptr; 1826 return 0; 1827 } 1828 1829 *verts = new GrTessellator::WindingVertex[count]; 1830 GrTessellator::WindingVertex* vertsEnd = *verts; 1831 SkPoint* points = new SkPoint[count]; 1832 SkPoint* pointsEnd = points; 1833 for (Poly* poly = polys; poly; poly = poly->fNext) { 1834 if (apply_fill_type(fillType, poly)) { 1835 SkPoint* start = pointsEnd; 1836 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd)); 1837 while (start != pointsEnd) { 1838 vertsEnd->fPos = *start; 1839 vertsEnd->fWinding = poly->fWinding; 1840 ++start; 1841 ++vertsEnd; 1842 } 1843 } 1844 } 1845 int actualCount = static_cast<int>(vertsEnd - *verts); 1846 SkASSERT(actualCount <= count); 1847 SkASSERT(pointsEnd - points == actualCount); 1848 delete[] points; 1849 return actualCount; 1850} 1851 1852} // namespace 1853