GrTessellator.cpp revision 651cbe9af67b49c5a3ad3788c3a50a003827a8e2
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 177bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { 178 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY); 179} 180 181bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) { 182 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX); 183} 184 185struct Comparator { 186 enum class Direction { kVertical, kHorizontal }; 187 Comparator(Direction direction) : fDirection(direction) {} 188 bool sweep_lt(const SkPoint& a, const SkPoint& b) const { 189 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b); 190 } 191 Direction fDirection; 192}; 193 194inline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) { 195 if (!aaParams) { 196 SkPoint* d = static_cast<SkPoint*>(data); 197 *d++ = v->fPoint; 198 return d; 199 } 200 if (aaParams->fTweakAlpha) { 201 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data); 202 d->fPosition = v->fPoint; 203 d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha)); 204 d++; 205 return d; 206 } 207 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data); 208 d->fPosition = v->fPoint; 209 d->fColor = aaParams->fColor; 210 d->fCoverage = GrNormalizeByteToFloat(v->fAlpha); 211 d++; 212 return d; 213} 214 215void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) { 216 LOG("emit_triangle (%g, %g) %d\n", v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha); 217 LOG(" (%g, %g) %d\n", v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha); 218 LOG(" (%g, %g) %d\n", v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha); 219#if TESSELLATOR_WIREFRAME 220 data = emit_vertex(v0, aaParams, data); 221 data = emit_vertex(v1, aaParams, data); 222 data = emit_vertex(v1, aaParams, data); 223 data = emit_vertex(v2, aaParams, data); 224 data = emit_vertex(v2, aaParams, data); 225 data = emit_vertex(v0, aaParams, data); 226#else 227 data = emit_vertex(v0, aaParams, data); 228 data = emit_vertex(v1, aaParams, data); 229 data = emit_vertex(v2, aaParams, data); 230#endif 231 return data; 232} 233 234struct VertexList { 235 VertexList() : fHead(nullptr), fTail(nullptr) {} 236 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {} 237 Vertex* fHead; 238 Vertex* fTail; 239 void insert(Vertex* v, Vertex* prev, Vertex* next) { 240 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail); 241 } 242 void append(Vertex* v) { 243 insert(v, fTail, nullptr); 244 } 245 void prepend(Vertex* v) { 246 insert(v, nullptr, fHead); 247 } 248 void remove(Vertex* v) { 249 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail); 250 } 251 void close() { 252 if (fHead && fTail) { 253 fTail->fNext = fHead; 254 fHead->fPrev = fTail; 255 } 256 } 257}; 258 259// Round to nearest quarter-pixel. This is used for screenspace tessellation. 260 261inline void round(SkPoint* p) { 262 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); 263 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); 264} 265 266// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line. 267struct Line { 268 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {} 269 Line(const SkPoint& p, const SkPoint& q) 270 : fA(static_cast<double>(q.fY) - p.fY) // a = dY 271 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX 272 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p) 273 static_cast<double>(p.fX) * q.fY) {} 274 double dist(const SkPoint& p) const { 275 return fA * p.fX + fB * p.fY + fC; 276 } 277 double magSq() const { 278 return fA * fA + fB * fB; 279 } 280 281 // Compute the intersection of two (infinite) Lines. 282 bool intersect(const Line& other, SkPoint* point) { 283 double denom = fA * other.fB - fB * other.fA; 284 if (denom == 0.0) { 285 return false; 286 } 287 double scale = 1.0f / denom; 288 point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale); 289 point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale); 290 round(point); 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>(other.fTop->fPoint.fX) - fTop->fPoint.fX; 380 double dy = static_cast<double>(other.fTop->fPoint.fY) - 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) {} 411 Edge* fHead; 412 Edge* fTail; 413 void insert(Edge* edge, Edge* prev, Edge* next) { 414 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail); 415 } 416 void append(Edge* e) { 417 insert(e, fTail, nullptr); 418 } 419 void remove(Edge* edge) { 420 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail); 421 } 422 void close() { 423 if (fHead && fTail) { 424 fTail->fRight = fHead; 425 fHead->fLeft = fTail; 426 } 427 } 428 bool contains(Edge* edge) const { 429 return edge->fLeft || edge->fRight || fHead == edge; 430 } 431}; 432 433/***************************************************************************************/ 434 435struct Poly { 436 Poly(Vertex* v, int winding) 437 : fFirstVertex(v) 438 , fWinding(winding) 439 , fHead(nullptr) 440 , fTail(nullptr) 441 , fNext(nullptr) 442 , fPartner(nullptr) 443 , fCount(0) 444 { 445#if LOGGING_ENABLED 446 static int gID = 0; 447 fID = gID++; 448 LOG("*** created Poly %d\n", fID); 449#endif 450 } 451 typedef enum { kLeft_Side, kRight_Side } Side; 452 struct MonotonePoly { 453 MonotonePoly(Edge* edge, Side side) 454 : fSide(side) 455 , fFirstEdge(nullptr) 456 , fLastEdge(nullptr) 457 , fPrev(nullptr) 458 , fNext(nullptr) { 459 this->addEdge(edge); 460 } 461 Side fSide; 462 Edge* fFirstEdge; 463 Edge* fLastEdge; 464 MonotonePoly* fPrev; 465 MonotonePoly* fNext; 466 void addEdge(Edge* edge) { 467 if (fSide == kRight_Side) { 468 SkASSERT(!edge->fUsedInRightPoly); 469 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>( 470 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 471 edge->fUsedInRightPoly = true; 472 } else { 473 SkASSERT(!edge->fUsedInLeftPoly); 474 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( 475 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 476 edge->fUsedInLeftPoly = true; 477 } 478 } 479 480 void* emit(const AAParams* aaParams, void* data) { 481 Edge* e = fFirstEdge; 482 VertexList vertices; 483 vertices.append(e->fTop); 484 int count = 1; 485 while (e != nullptr) { 486 if (kRight_Side == fSide) { 487 vertices.append(e->fBottom); 488 e = e->fRightPolyNext; 489 } else { 490 vertices.prepend(e->fBottom); 491 e = e->fLeftPolyNext; 492 } 493 count++; 494 } 495 Vertex* first = vertices.fHead; 496 Vertex* v = first->fNext; 497 while (v != vertices.fTail) { 498 SkASSERT(v && v->fPrev && v->fNext); 499 Vertex* prev = v->fPrev; 500 Vertex* curr = v; 501 Vertex* next = v->fNext; 502 if (count == 3) { 503 return emit_triangle(prev, curr, next, aaParams, data); 504 } 505 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; 506 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; 507 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; 508 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; 509 if (ax * by - ay * bx >= 0.0) { 510 data = emit_triangle(prev, curr, next, aaParams, data); 511 v->fPrev->fNext = v->fNext; 512 v->fNext->fPrev = v->fPrev; 513 count--; 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 603Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, 604 SkArenaAlloc& alloc) { 605 Vertex* v = alloc.make<Vertex>(p, 255); 606#if LOGGING_ENABLED 607 static float gID = 0.0f; 608 v->fID = gID++; 609#endif 610 if (prev) { 611 prev->fNext = v; 612 v->fPrev = prev; 613 } else { 614 *head = v; 615 } 616 return v; 617} 618 619Vertex* generate_quadratic_points(const SkPoint& p0, 620 const SkPoint& p1, 621 const SkPoint& p2, 622 SkScalar tolSqd, 623 Vertex* prev, 624 Vertex** head, 625 int pointsLeft, 626 SkArenaAlloc& alloc) { 627 SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2); 628 if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) { 629 return append_point_to_contour(p2, prev, head, alloc); 630 } 631 632 const SkPoint q[] = { 633 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, 634 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, 635 }; 636 const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; 637 638 pointsLeft >>= 1; 639 prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc); 640 prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc); 641 return prev; 642} 643 644Vertex* generate_cubic_points(const SkPoint& p0, 645 const SkPoint& p1, 646 const SkPoint& p2, 647 const SkPoint& p3, 648 SkScalar tolSqd, 649 Vertex* prev, 650 Vertex** head, 651 int pointsLeft, 652 SkArenaAlloc& alloc) { 653 SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3); 654 SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3); 655 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || 656 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) { 657 return append_point_to_contour(p3, prev, head, alloc); 658 } 659 const SkPoint q[] = { 660 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, 661 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, 662 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } 663 }; 664 const SkPoint r[] = { 665 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, 666 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } 667 }; 668 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; 669 pointsLeft >>= 1; 670 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc); 671 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc); 672 return prev; 673} 674 675// Stage 1: convert the input path to a set of linear contours (linked list of Vertices). 676 677void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 678 Vertex** contours, SkArenaAlloc& alloc, bool *isLinear) { 679 SkScalar toleranceSqd = tolerance * tolerance; 680 681 SkPoint pts[4]; 682 bool done = false; 683 *isLinear = true; 684 SkPath::Iter iter(path, false); 685 Vertex* prev = nullptr; 686 Vertex* head = nullptr; 687 if (path.isInverseFillType()) { 688 SkPoint quad[4]; 689 clipBounds.toQuad(quad); 690 for (int i = 3; i >= 0; i--) { 691 prev = append_point_to_contour(quad[i], prev, &head, alloc); 692 } 693 head->fPrev = prev; 694 prev->fNext = head; 695 *contours++ = head; 696 head = prev = nullptr; 697 } 698 SkAutoConicToQuads converter; 699 while (!done) { 700 SkPath::Verb verb = iter.next(pts); 701 switch (verb) { 702 case SkPath::kConic_Verb: { 703 SkScalar weight = iter.conicWeight(); 704 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd); 705 for (int i = 0; i < converter.countQuads(); ++i) { 706 int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance); 707 prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2], 708 toleranceSqd, prev, &head, pointsLeft, alloc); 709 quadPts += 2; 710 } 711 *isLinear = false; 712 break; 713 } 714 case SkPath::kMove_Verb: 715 if (head) { 716 head->fPrev = prev; 717 prev->fNext = head; 718 *contours++ = head; 719 } 720 head = prev = nullptr; 721 prev = append_point_to_contour(pts[0], prev, &head, alloc); 722 break; 723 case SkPath::kLine_Verb: { 724 prev = append_point_to_contour(pts[1], prev, &head, alloc); 725 break; 726 } 727 case SkPath::kQuad_Verb: { 728 int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance); 729 prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev, 730 &head, pointsLeft, alloc); 731 *isLinear = false; 732 break; 733 } 734 case SkPath::kCubic_Verb: { 735 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance); 736 prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3], 737 toleranceSqd, prev, &head, pointsLeft, alloc); 738 *isLinear = false; 739 break; 740 } 741 case SkPath::kClose_Verb: 742 if (head) { 743 head->fPrev = prev; 744 prev->fNext = head; 745 *contours++ = head; 746 } 747 head = prev = nullptr; 748 break; 749 case SkPath::kDone_Verb: 750 if (head) { 751 head->fPrev = prev; 752 prev->fNext = head; 753 *contours++ = head; 754 } 755 done = true; 756 break; 757 } 758 } 759} 760 761inline bool apply_fill_type(SkPath::FillType fillType, int winding) { 762 switch (fillType) { 763 case SkPath::kWinding_FillType: 764 return winding != 0; 765 case SkPath::kEvenOdd_FillType: 766 return (winding & 1) != 0; 767 case SkPath::kInverseWinding_FillType: 768 return winding == 1; 769 case SkPath::kInverseEvenOdd_FillType: 770 return (winding & 1) == 1; 771 default: 772 SkASSERT(false); 773 return false; 774 } 775} 776 777inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) { 778 return poly && apply_fill_type(fillType, poly->fWinding); 779} 780 781Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) { 782 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; 783 Vertex* top = winding < 0 ? next : prev; 784 Vertex* bottom = winding < 0 ? prev : next; 785 return alloc.make<Edge>(top, bottom, winding, type); 786} 787 788void remove_edge(Edge* edge, EdgeList* edges) { 789 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 790 SkASSERT(edges->contains(edge)); 791 edges->remove(edge); 792} 793 794void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) { 795 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 796 SkASSERT(!edges->contains(edge)); 797 Edge* next = prev ? prev->fRight : edges->fHead; 798 edges->insert(edge, prev, next); 799} 800 801void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { 802 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) { 803 *left = v->fFirstEdgeAbove->fLeft; 804 *right = v->fLastEdgeAbove->fRight; 805 return; 806 } 807 Edge* next = nullptr; 808 Edge* prev; 809 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) { 810 if (prev->isLeftOf(v)) { 811 break; 812 } 813 next = prev; 814 } 815 *left = prev; 816 *right = next; 817} 818 819void find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) { 820 Edge* prev = nullptr; 821 Edge* next; 822 for (next = edges->fHead; next != nullptr; next = next->fRight) { 823 if ((c.sweep_lt(next->fTop->fPoint, edge->fTop->fPoint) && next->isRightOf(edge->fTop)) || 824 (c.sweep_lt(edge->fTop->fPoint, next->fTop->fPoint) && edge->isLeftOf(next->fTop)) || 825 (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) && 826 next->isRightOf(edge->fBottom)) || 827 (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) && 828 edge->isLeftOf(next->fBottom))) { 829 break; 830 } 831 prev = next; 832 } 833 *left = prev; 834 *right = next; 835} 836 837void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) { 838 if (!activeEdges) { 839 return; 840 } 841 if (activeEdges->contains(edge)) { 842 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) { 843 remove_edge(edge, activeEdges); 844 } 845 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) { 846 Edge* left; 847 Edge* right; 848 find_enclosing_edges(edge, activeEdges, c, &left, &right); 849 insert_edge(edge, left, activeEdges); 850 } 851} 852 853void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) { 854 if (edge->fTop->fPoint == edge->fBottom->fPoint || 855 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) { 856 return; 857 } 858 LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); 859 Edge* prev = nullptr; 860 Edge* next; 861 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { 862 if (next->isRightOf(edge->fTop)) { 863 break; 864 } 865 prev = next; 866 } 867 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( 868 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); 869} 870 871void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) { 872 if (edge->fTop->fPoint == edge->fBottom->fPoint || 873 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) { 874 return; 875 } 876 LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); 877 Edge* prev = nullptr; 878 Edge* next; 879 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { 880 if (next->isRightOf(edge->fBottom)) { 881 break; 882 } 883 prev = next; 884 } 885 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( 886 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); 887} 888 889void remove_edge_above(Edge* edge) { 890 LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, 891 edge->fBottom->fID); 892 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( 893 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); 894} 895 896void remove_edge_below(Edge* edge) { 897 LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, 898 edge->fTop->fID); 899 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( 900 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); 901} 902 903void disconnect(Edge* edge) 904{ 905 remove_edge_above(edge); 906 remove_edge_below(edge); 907} 908 909void erase_edge(Edge* edge, EdgeList* edges) { 910 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID); 911 disconnect(edge); 912 if (edges && edges->contains(edge)) { 913 remove_edge(edge, edges); 914 } 915} 916 917void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c); 918 919void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { 920 remove_edge_below(edge); 921 edge->fTop = v; 922 edge->recompute(); 923 insert_edge_below(edge, v, c); 924 fix_active_state(edge, activeEdges, c); 925 merge_collinear_edges(edge, activeEdges, c); 926} 927 928void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { 929 remove_edge_above(edge); 930 edge->fBottom = v; 931 edge->recompute(); 932 insert_edge_above(edge, v, c); 933 fix_active_state(edge, activeEdges, c); 934 merge_collinear_edges(edge, activeEdges, c); 935} 936 937void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) { 938 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { 939 LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", 940 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, 941 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); 942 other->fWinding += edge->fWinding; 943 erase_edge(edge, activeEdges); 944 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { 945 other->fWinding += edge->fWinding; 946 set_bottom(edge, other->fTop, activeEdges, c); 947 } else { 948 edge->fWinding += other->fWinding; 949 set_bottom(other, edge->fTop, activeEdges, c); 950 } 951} 952 953void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) { 954 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { 955 LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", 956 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, 957 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); 958 other->fWinding += edge->fWinding; 959 erase_edge(edge, activeEdges); 960 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { 961 edge->fWinding += other->fWinding; 962 set_top(other, edge->fBottom, activeEdges, c); 963 } else { 964 other->fWinding += edge->fWinding; 965 set_top(edge, other->fBottom, activeEdges, c); 966 } 967} 968 969void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) { 970 if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop || 971 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) { 972 merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c); 973 } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop || 974 !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) { 975 merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c); 976 } 977 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom || 978 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) { 979 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c); 980 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom || 981 !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) { 982 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c); 983 } 984} 985 986void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc); 987 988void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) { 989 Vertex* top = edge->fTop; 990 Vertex* bottom = edge->fBottom; 991 if (edge->fLeft) { 992 Vertex* leftTop = edge->fLeft->fTop; 993 Vertex* leftBottom = edge->fLeft->fBottom; 994 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) { 995 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc); 996 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) { 997 split_edge(edge, leftTop, activeEdges, c, alloc); 998 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) && 999 !edge->fLeft->isLeftOf(bottom)) { 1000 split_edge(edge->fLeft, bottom, activeEdges, c, alloc); 1001 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) { 1002 split_edge(edge, leftBottom, activeEdges, c, alloc); 1003 } 1004 } 1005 if (edge->fRight) { 1006 Vertex* rightTop = edge->fRight->fTop; 1007 Vertex* rightBottom = edge->fRight->fBottom; 1008 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) { 1009 split_edge(edge->fRight, top, activeEdges, c, alloc); 1010 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) { 1011 split_edge(edge, rightTop, activeEdges, c, alloc); 1012 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) && 1013 !edge->fRight->isRightOf(bottom)) { 1014 split_edge(edge->fRight, bottom, activeEdges, c, alloc); 1015 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) && 1016 !edge->isLeftOf(rightBottom)) { 1017 split_edge(edge, rightBottom, activeEdges, c, alloc); 1018 } 1019 } 1020} 1021 1022void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) { 1023 LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", 1024 edge->fTop->fID, edge->fBottom->fID, 1025 v->fID, v->fPoint.fX, v->fPoint.fY); 1026 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) { 1027 set_top(edge, v, activeEdges, c); 1028 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) { 1029 set_bottom(edge, v, activeEdges, c); 1030 } else { 1031 Edge* newEdge = alloc.make<Edge>(v, edge->fBottom, edge->fWinding, edge->fType); 1032 insert_edge_below(newEdge, v, c); 1033 insert_edge_above(newEdge, edge->fBottom, c); 1034 set_bottom(edge, v, activeEdges, c); 1035 cleanup_active_edges(edge, activeEdges, c, alloc); 1036 fix_active_state(newEdge, activeEdges, c); 1037 merge_collinear_edges(newEdge, activeEdges, c); 1038 } 1039} 1040 1041Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc, 1042 int winding_scale = 1) { 1043 Edge* edge = new_edge(prev, next, type, c, alloc); 1044 insert_edge_below(edge, edge->fTop, c); 1045 insert_edge_above(edge, edge->fBottom, c); 1046 edge->fWinding *= winding_scale; 1047 merge_collinear_edges(edge, nullptr, c); 1048 return edge; 1049} 1050 1051void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c, 1052 SkArenaAlloc& alloc) { 1053 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY, 1054 src->fID, dst->fID); 1055 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha); 1056 for (Edge* edge = src->fFirstEdgeAbove; edge;) { 1057 Edge* next = edge->fNextEdgeAbove; 1058 set_bottom(edge, dst, nullptr, c); 1059 edge = next; 1060 } 1061 for (Edge* edge = src->fFirstEdgeBelow; edge;) { 1062 Edge* next = edge->fNextEdgeBelow; 1063 set_top(edge, dst, nullptr, c); 1064 edge = next; 1065 } 1066 mesh->remove(src); 1067} 1068 1069uint8_t max_edge_alpha(Edge* a, Edge* b) { 1070 if (a->fType == Edge::Type::kInner || b->fType == Edge::Type::kInner) { 1071 return 255; 1072 } else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) { 1073 return 0; 1074 } else { 1075 return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha), 1076 SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha)); 1077 } 1078} 1079 1080Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c, 1081 SkArenaAlloc& alloc) { 1082 if (!edge || !other) { 1083 return nullptr; 1084 } 1085 SkPoint p; 1086 uint8_t alpha; 1087 if (edge->intersect(*other, &p, &alpha)) { 1088 Vertex* v; 1089 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); 1090 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) { 1091 split_edge(other, edge->fTop, activeEdges, c, alloc); 1092 v = edge->fTop; 1093 } else if (p == edge->fBottom->fPoint || c.sweep_lt(edge->fBottom->fPoint, p)) { 1094 split_edge(other, edge->fBottom, activeEdges, c, alloc); 1095 v = edge->fBottom; 1096 } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) { 1097 split_edge(edge, other->fTop, activeEdges, c, alloc); 1098 v = other->fTop; 1099 } else if (p == other->fBottom->fPoint || c.sweep_lt(other->fBottom->fPoint, p)) { 1100 split_edge(edge, other->fBottom, activeEdges, c, alloc); 1101 v = other->fBottom; 1102 } else { 1103 Vertex* nextV = edge->fTop; 1104 while (c.sweep_lt(p, nextV->fPoint)) { 1105 nextV = nextV->fPrev; 1106 } 1107 while (c.sweep_lt(nextV->fPoint, p)) { 1108 nextV = nextV->fNext; 1109 } 1110 Vertex* prevV = nextV->fPrev; 1111 if (coincident(prevV->fPoint, p)) { 1112 v = prevV; 1113 } else if (coincident(nextV->fPoint, p)) { 1114 v = nextV; 1115 } else { 1116 v = alloc.make<Vertex>(p, alpha); 1117 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n", 1118 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY, 1119 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY); 1120#if LOGGING_ENABLED 1121 v->fID = (nextV->fID + prevV->fID) * 0.5f; 1122#endif 1123 v->fPrev = prevV; 1124 v->fNext = nextV; 1125 prevV->fNext = v; 1126 nextV->fPrev = v; 1127 } 1128 split_edge(edge, v, activeEdges, c, alloc); 1129 split_edge(other, v, activeEdges, c, alloc); 1130 } 1131 v->fAlpha = SkTMax(v->fAlpha, alpha); 1132 return v; 1133 } 1134 return nullptr; 1135} 1136 1137void sanitize_contours(Vertex** contours, int contourCnt, bool approximate) { 1138 for (int i = 0; i < contourCnt; ++i) { 1139 SkASSERT(contours[i]); 1140 if (approximate) { 1141 round(&contours[i]->fPrev->fPoint); 1142 } 1143 for (Vertex* v = contours[i];;) { 1144 if (approximate) { 1145 round(&v->fPoint); 1146 } 1147 if (coincident(v->fPrev->fPoint, v->fPoint)) { 1148 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); 1149 if (v->fPrev == v) { 1150 contours[i] = nullptr; 1151 break; 1152 } 1153 v->fPrev->fNext = v->fNext; 1154 v->fNext->fPrev = v->fPrev; 1155 if (contours[i] == v) { 1156 contours[i] = v->fPrev; 1157 } 1158 v = v->fPrev; 1159 } else { 1160 v = v->fNext; 1161 if (v == contours[i]) break; 1162 } 1163 } 1164 } 1165} 1166 1167void merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1168 for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) { 1169 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { 1170 v->fPoint = v->fPrev->fPoint; 1171 } 1172 if (coincident(v->fPrev->fPoint, v->fPoint)) { 1173 merge_vertices(v->fPrev, v, mesh, c, alloc); 1174 } 1175 } 1176} 1177 1178// Stage 2: convert the contours to a mesh of edges connecting the vertices. 1179 1180void build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c, 1181 SkArenaAlloc& alloc) { 1182 Vertex* prev = nullptr; 1183 for (int i = 0; i < contourCnt; ++i) { 1184 for (Vertex* v = contours[i]; v != nullptr;) { 1185 Vertex* vNext = v->fNext; 1186 connect(v->fPrev, v, Edge::Type::kInner, c, alloc); 1187 if (prev) { 1188 prev->fNext = v; 1189 v->fPrev = prev; 1190 } else { 1191 mesh->fHead = v; 1192 } 1193 prev = v; 1194 v = vNext; 1195 if (v == contours[i]) break; 1196 } 1197 } 1198 if (prev) { 1199 prev->fNext = mesh->fHead->fPrev = nullptr; 1200 } 1201 mesh->fTail = prev; 1202} 1203 1204// Stage 3: sort the vertices by increasing sweep direction. 1205 1206template <CompareFunc sweep_lt> 1207void merge_sort(VertexList* vertices) { 1208 Vertex* slow = vertices->fHead; 1209 if (!slow) { 1210 return; 1211 } 1212 Vertex* fast = slow->fNext; 1213 if (!fast) { 1214 return; 1215 } 1216 do { 1217 fast = fast->fNext; 1218 if (fast) { 1219 fast = fast->fNext; 1220 slow = slow->fNext; 1221 } 1222 } while (fast); 1223 VertexList front(vertices->fHead, slow); 1224 VertexList back(slow->fNext, vertices->fTail); 1225 front.fTail->fNext = back.fHead->fPrev = nullptr; 1226 1227 merge_sort<sweep_lt>(&front); 1228 merge_sort<sweep_lt>(&back); 1229 1230 vertices->fHead = vertices->fTail = nullptr; 1231 Vertex* a = front.fHead; 1232 Vertex* b = back.fHead; 1233 while (a && b) { 1234 if (sweep_lt(a->fPoint, b->fPoint)) { 1235 Vertex* next = a->fNext; 1236 vertices->append(a); 1237 a = next; 1238 } else { 1239 Vertex* next = b->fNext; 1240 vertices->append(b); 1241 b = next; 1242 } 1243 } 1244 if (a) { 1245 vertices->insert(a, vertices->fTail, a->fNext); 1246 } 1247 if (b) { 1248 vertices->insert(b, vertices->fTail, b->fNext); 1249 } 1250} 1251 1252// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. 1253 1254void simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) { 1255 LOG("simplifying complex polygons\n"); 1256 EdgeList activeEdges; 1257 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { 1258 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1259 continue; 1260 } 1261#if LOGGING_ENABLED 1262 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1263#endif 1264 Edge* leftEnclosingEdge; 1265 Edge* rightEnclosingEdge; 1266 bool restartChecks; 1267 do { 1268 restartChecks = false; 1269 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1270 if (v->fFirstEdgeBelow) { 1271 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) { 1272 if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) { 1273 restartChecks = true; 1274 break; 1275 } 1276 if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) { 1277 restartChecks = true; 1278 break; 1279 } 1280 } 1281 } else { 1282 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, 1283 &activeEdges, c, alloc)) { 1284 if (c.sweep_lt(pv->fPoint, v->fPoint)) { 1285 v = pv; 1286 } 1287 restartChecks = true; 1288 } 1289 1290 } 1291 } while (restartChecks); 1292 if (v->fAlpha == 0) { 1293 if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) && 1294 (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) { 1295 v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge); 1296 } 1297 } 1298 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1299 remove_edge(e, &activeEdges); 1300 } 1301 Edge* leftEdge = leftEnclosingEdge; 1302 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1303 insert_edge(e, leftEdge, &activeEdges); 1304 leftEdge = e; 1305 } 1306 v->fProcessed = true; 1307 } 1308} 1309 1310// Stage 5: Tessellate the simplified mesh into monotone polygons. 1311 1312Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) { 1313 LOG("tessellating simple polygons\n"); 1314 EdgeList activeEdges; 1315 Poly* polys = nullptr; 1316 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { 1317 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1318 continue; 1319 } 1320#if LOGGING_ENABLED 1321 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1322#endif 1323 Edge* leftEnclosingEdge; 1324 Edge* rightEnclosingEdge; 1325 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1326 Poly* leftPoly; 1327 Poly* rightPoly; 1328 if (v->fFirstEdgeAbove) { 1329 leftPoly = v->fFirstEdgeAbove->fLeftPoly; 1330 rightPoly = v->fLastEdgeAbove->fRightPoly; 1331 } else { 1332 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; 1333 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; 1334 } 1335#if LOGGING_ENABLED 1336 LOG("edges above:\n"); 1337 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1338 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1339 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1340 } 1341 LOG("edges below:\n"); 1342 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1343 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1344 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1345 } 1346#endif 1347 if (v->fFirstEdgeAbove) { 1348 if (leftPoly) { 1349 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc); 1350 } 1351 if (rightPoly) { 1352 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc); 1353 } 1354 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { 1355 Edge* rightEdge = e->fNextEdgeAbove; 1356 SkASSERT(rightEdge->isRightOf(e->fTop)); 1357 remove_edge(e, &activeEdges); 1358 if (e->fRightPoly) { 1359 e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc); 1360 } 1361 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) { 1362 rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc); 1363 } 1364 } 1365 remove_edge(v->fLastEdgeAbove, &activeEdges); 1366 if (!v->fFirstEdgeBelow) { 1367 if (leftPoly && rightPoly && leftPoly != rightPoly) { 1368 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); 1369 rightPoly->fPartner = leftPoly; 1370 leftPoly->fPartner = rightPoly; 1371 } 1372 } 1373 } 1374 if (v->fFirstEdgeBelow) { 1375 if (!v->fFirstEdgeAbove) { 1376 if (leftPoly && rightPoly) { 1377 if (leftPoly == rightPoly) { 1378 if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) { 1379 leftPoly = new_poly(&polys, leftPoly->lastVertex(), 1380 leftPoly->fWinding, alloc); 1381 leftEnclosingEdge->fRightPoly = leftPoly; 1382 } else { 1383 rightPoly = new_poly(&polys, rightPoly->lastVertex(), 1384 rightPoly->fWinding, alloc); 1385 rightEnclosingEdge->fLeftPoly = rightPoly; 1386 } 1387 } 1388 Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner); 1389 leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc); 1390 rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc); 1391 } 1392 } 1393 Edge* leftEdge = v->fFirstEdgeBelow; 1394 leftEdge->fLeftPoly = leftPoly; 1395 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges); 1396 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; 1397 rightEdge = rightEdge->fNextEdgeBelow) { 1398 insert_edge(rightEdge, leftEdge, &activeEdges); 1399 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; 1400 winding += leftEdge->fWinding; 1401 if (winding != 0) { 1402 Poly* poly = new_poly(&polys, v, winding, alloc); 1403 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; 1404 } 1405 leftEdge = rightEdge; 1406 } 1407 v->fLastEdgeBelow->fRightPoly = rightPoly; 1408 } 1409#if LOGGING_ENABLED 1410 LOG("\nactive edges:\n"); 1411 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { 1412 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1413 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1414 } 1415#endif 1416 } 1417 return polys; 1418} 1419 1420void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType, 1421 SkArenaAlloc& alloc) { 1422 LOG("removing non-boundary edges\n"); 1423 EdgeList activeEdges; 1424 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) { 1425 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1426 continue; 1427 } 1428 Edge* leftEnclosingEdge; 1429 Edge* rightEnclosingEdge; 1430 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1431 bool prevFilled = leftEnclosingEdge && 1432 apply_fill_type(fillType, leftEnclosingEdge->fWinding); 1433 for (Edge* e = v->fFirstEdgeAbove; e;) { 1434 Edge* next = e->fNextEdgeAbove; 1435 remove_edge(e, &activeEdges); 1436 bool filled = apply_fill_type(fillType, e->fWinding); 1437 if (filled == prevFilled) { 1438 disconnect(e); 1439 } 1440 prevFilled = filled; 1441 e = next; 1442 } 1443 Edge* prev = leftEnclosingEdge; 1444 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1445 if (prev) { 1446 e->fWinding += prev->fWinding; 1447 } 1448 insert_edge(e, prev, &activeEdges); 1449 prev = e; 1450 } 1451 } 1452} 1453 1454// Note: this is the normal to the edge, but not necessarily unit length. 1455void get_edge_normal(const Edge* e, SkVector* normal) { 1456 normal->set(SkDoubleToScalar(e->fLine.fA) * e->fWinding, 1457 SkDoubleToScalar(e->fLine.fB) * e->fWinding); 1458} 1459 1460// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions 1461// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to 1462// invert on stroking. 1463 1464void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) { 1465 Edge* prevEdge = boundary->fTail; 1466 SkVector prevNormal; 1467 get_edge_normal(prevEdge, &prevNormal); 1468 for (Edge* e = boundary->fHead; e != nullptr;) { 1469 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom; 1470 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop; 1471 double dist = e->dist(prev->fPoint); 1472 SkVector normal; 1473 get_edge_normal(e, &normal); 1474 double denom = 0.0625f * e->fLine.magSq(); 1475 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) { 1476 Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc); 1477 insert_edge(join, e, boundary); 1478 remove_edge(prevEdge, boundary); 1479 remove_edge(e, boundary); 1480 if (join->fLeft && join->fRight) { 1481 prevEdge = join->fLeft; 1482 e = join; 1483 } else { 1484 prevEdge = boundary->fTail; 1485 e = boundary->fHead; // join->fLeft ? join->fLeft : join; 1486 } 1487 get_edge_normal(prevEdge, &prevNormal); 1488 } else { 1489 prevEdge = e; 1490 prevNormal = normal; 1491 e = e->fRight; 1492 } 1493 } 1494} 1495 1496void fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBisector, 1497 Edge* prevEdge, Comparator& c) { 1498 if (!prev || !next) { 1499 return; 1500 } 1501 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; 1502 SkPoint p; 1503 uint8_t alpha; 1504 if (winding != prevEdge->fWinding && prevBisector->intersect(*nextBisector, &p, &alpha)) { 1505 prev->fPoint = next->fPoint = p; 1506 prev->fAlpha = next->fAlpha = alpha; 1507 } 1508} 1509 1510// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to 1511// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a 1512// new antialiased mesh from those vertices. 1513 1514void boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1515 // A boundary with fewer than 3 edges is degenerate. 1516 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) { 1517 return; 1518 } 1519 Edge* prevEdge = boundary->fTail; 1520 float radius = 0.5f; 1521 double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding; 1522 Line prevInner(prevEdge->fLine); 1523 prevInner.fC -= offset; 1524 Line prevOuter(prevEdge->fLine); 1525 prevOuter.fC += offset; 1526 VertexList innerVertices; 1527 VertexList outerVertices; 1528 Edge* prevBisector = nullptr; 1529 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) { 1530 double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding; 1531 Line inner(e->fLine); 1532 inner.fC -= offset; 1533 Line outer(e->fLine); 1534 outer.fC += offset; 1535 SkPoint innerPoint, outerPoint; 1536 if (prevInner.intersect(inner, &innerPoint) && 1537 prevOuter.intersect(outer, &outerPoint)) { 1538 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255); 1539 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0); 1540 Edge* bisector = new_edge(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc); 1541 fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c); 1542 fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c); 1543 innerVertices.append(innerVertex); 1544 outerVertices.append(outerVertex); 1545 prevBisector = bisector; 1546 } 1547 prevInner = inner; 1548 prevOuter = outer; 1549 prevEdge = e; 1550 } 1551 innerVertices.close(); 1552 outerVertices.close(); 1553 1554 Vertex* innerVertex = innerVertices.fHead; 1555 Vertex* outerVertex = outerVertices.fHead; 1556 if (!innerVertex || !outerVertex) { 1557 return; 1558 } 1559 Edge* bisector = new_edge(outerVertices.fHead, innerVertices.fHead, Edge::Type::kConnector, c, 1560 alloc); 1561 fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c); 1562 fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c); 1563 do { 1564 // Connect vertices into a quad mesh. Outer edges get default (1) winding. 1565 // Inner edges get -2 winding. This ensures that the interior is always filled 1566 // (-1 winding number for normal cases, 3 for thin features where the interior inverts). 1567 // Connector edges get zero winding, since they're only structural (i.e., to ensure 1568 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding number. 1569 connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc); 1570 connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc, -2); 1571 connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc, 0); 1572 Vertex* innerNext = innerVertex->fNext; 1573 Vertex* outerNext = outerVertex->fNext; 1574 mesh->append(innerVertex); 1575 mesh->append(outerVertex); 1576 innerVertex = innerNext; 1577 outerVertex = outerNext; 1578 } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead); 1579} 1580 1581void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) { 1582 bool down = apply_fill_type(fillType, e->fWinding); 1583 while (e) { 1584 e->fWinding = down ? 1 : -1; 1585 Edge* next; 1586 boundary->append(e); 1587 if (down) { 1588 // Find outgoing edge, in clockwise order. 1589 if ((next = e->fNextEdgeAbove)) { 1590 down = false; 1591 } else if ((next = e->fBottom->fLastEdgeBelow)) { 1592 down = true; 1593 } else if ((next = e->fPrevEdgeAbove)) { 1594 down = false; 1595 } 1596 } else { 1597 // Find outgoing edge, in counter-clockwise order. 1598 if ((next = e->fPrevEdgeBelow)) { 1599 down = true; 1600 } else if ((next = e->fTop->fFirstEdgeAbove)) { 1601 down = false; 1602 } else if ((next = e->fNextEdgeBelow)) { 1603 down = true; 1604 } 1605 } 1606 disconnect(e); 1607 e = next; 1608 } 1609} 1610 1611// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh. 1612 1613void extract_boundaries(const VertexList& inMesh, VertexList* outMesh, SkPath::FillType fillType, 1614 Comparator& c, SkArenaAlloc& alloc) { 1615 remove_non_boundary_edges(inMesh, fillType, alloc); 1616 for (Vertex* v = inMesh.fHead; v; v = v->fNext) { 1617 while (v->fFirstEdgeBelow) { 1618 EdgeList boundary; 1619 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc); 1620 simplify_boundary(&boundary, c, alloc); 1621 boundary_to_aa_mesh(&boundary, outMesh, c, alloc); 1622 } 1623 } 1624} 1625 1626// This is a driver function which calls stages 2-5 in turn. 1627 1628void contours_to_mesh(Vertex** contours, int contourCnt, bool antialias, 1629 VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1630#if LOGGING_ENABLED 1631 for (int i = 0; i < contourCnt; ++i) { 1632 Vertex* v = contours[i]; 1633 SkASSERT(v); 1634 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1635 for (v = v->fNext; v != contours[i]; v = v->fNext) { 1636 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1637 } 1638 } 1639#endif 1640 sanitize_contours(contours, contourCnt, antialias); 1641 build_edges(contours, contourCnt, mesh, c, alloc); 1642} 1643 1644void sort_and_simplify(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) { 1645 if (!vertices || !vertices->fHead) { 1646 return; 1647 } 1648 1649 // Sort vertices in Y (secondarily in X). 1650 if (c.fDirection == Comparator::Direction::kHorizontal) { 1651 merge_sort<sweep_lt_horiz>(vertices); 1652 } else { 1653 merge_sort<sweep_lt_vert>(vertices); 1654 } 1655 merge_coincident_vertices(vertices, c, alloc); 1656#if LOGGING_ENABLED 1657 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) { 1658 static float gID = 0.0f; 1659 v->fID = gID++; 1660 } 1661#endif 1662 simplify(*vertices, c, alloc); 1663} 1664 1665Poly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType, 1666 const SkRect& pathBounds, bool antialias, 1667 SkArenaAlloc& alloc) { 1668 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal 1669 : Comparator::Direction::kVertical); 1670 VertexList mesh; 1671 contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc); 1672 sort_and_simplify(&mesh, c, alloc); 1673 if (antialias) { 1674 VertexList aaMesh; 1675 extract_boundaries(mesh, &aaMesh, fillType, c, alloc); 1676 sort_and_simplify(&aaMesh, c, alloc); 1677 return tessellate(aaMesh, alloc); 1678 } else { 1679 return tessellate(mesh, alloc); 1680 } 1681} 1682 1683// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1684void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams, 1685 void* data) { 1686 for (Poly* poly = polys; poly; poly = poly->fNext) { 1687 if (apply_fill_type(fillType, poly)) { 1688 data = poly->emit(aaParams, data); 1689 } 1690 } 1691 return data; 1692} 1693 1694Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1695 int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear) { 1696 SkPath::FillType fillType = path.getFillType(); 1697 if (SkPath::IsInverseFillType(fillType)) { 1698 contourCnt++; 1699 } 1700 std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]); 1701 1702 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); 1703 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(), 1704 antialias, alloc); 1705} 1706 1707int get_contour_count(const SkPath& path, SkScalar tolerance) { 1708 int contourCnt; 1709 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance); 1710 if (maxPts <= 0) { 1711 return 0; 1712 } 1713 if (maxPts > ((int)SK_MaxU16 + 1)) { 1714 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); 1715 return 0; 1716 } 1717 return contourCnt; 1718} 1719 1720int count_points(Poly* polys, SkPath::FillType fillType) { 1721 int count = 0; 1722 for (Poly* poly = polys; poly; poly = poly->fNext) { 1723 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) { 1724 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); 1725 } 1726 } 1727 return count; 1728} 1729 1730} // namespace 1731 1732namespace GrTessellator { 1733 1734// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1735 1736int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1737 VertexAllocator* vertexAllocator, bool antialias, const GrColor& color, 1738 bool canTweakAlphaForCoverage, bool* isLinear) { 1739 int contourCnt = get_contour_count(path, tolerance); 1740 if (contourCnt <= 0) { 1741 *isLinear = true; 1742 return 0; 1743 } 1744 SkArenaAlloc alloc(kArenaChunkSize); 1745 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias, 1746 isLinear); 1747 SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType(); 1748 int count = count_points(polys, fillType); 1749 if (0 == count) { 1750 return 0; 1751 } 1752 1753 void* verts = vertexAllocator->lock(count); 1754 if (!verts) { 1755 SkDebugf("Could not allocate vertices\n"); 1756 return 0; 1757 } 1758 1759 LOG("emitting %d verts\n", count); 1760 AAParams aaParams; 1761 aaParams.fTweakAlpha = canTweakAlphaForCoverage; 1762 aaParams.fColor = color; 1763 1764 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts); 1765 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts)) 1766 / vertexAllocator->stride()); 1767 SkASSERT(actualCount <= count); 1768 vertexAllocator->unlock(actualCount); 1769 return actualCount; 1770} 1771 1772int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1773 GrTessellator::WindingVertex** verts) { 1774 int contourCnt = get_contour_count(path, tolerance); 1775 if (contourCnt <= 0) { 1776 return 0; 1777 } 1778 SkArenaAlloc alloc(kArenaChunkSize); 1779 bool isLinear; 1780 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear); 1781 SkPath::FillType fillType = path.getFillType(); 1782 int count = count_points(polys, fillType); 1783 if (0 == count) { 1784 *verts = nullptr; 1785 return 0; 1786 } 1787 1788 *verts = new GrTessellator::WindingVertex[count]; 1789 GrTessellator::WindingVertex* vertsEnd = *verts; 1790 SkPoint* points = new SkPoint[count]; 1791 SkPoint* pointsEnd = points; 1792 for (Poly* poly = polys; poly; poly = poly->fNext) { 1793 if (apply_fill_type(fillType, poly)) { 1794 SkPoint* start = pointsEnd; 1795 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd)); 1796 while (start != pointsEnd) { 1797 vertsEnd->fPos = *start; 1798 vertsEnd->fWinding = poly->fWinding; 1799 ++start; 1800 ++vertsEnd; 1801 } 1802 } 1803 } 1804 int actualCount = static_cast<int>(vertsEnd - *verts); 1805 SkASSERT(actualCount <= count); 1806 SkASSERT(pointsEnd - points == actualCount); 1807 delete[] points; 1808 return actualCount; 1809} 1810 1811} // namespace 1812