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