senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 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 "GrTessellatingPathRenderer.h" |
| 9 | |
| 10 | #include "GrDefaultGeoProcFactory.h" |
| 11 | #include "GrPathUtils.h" |
| 12 | #include "SkChunkAlloc.h" |
| 13 | #include "SkGeometry.h" |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | |
| 17 | /* |
| 18 | * This path renderer tessellates the path into triangles, uploads the triangles to a |
| 19 | * vertex buffer, and renders them with a single draw call. It does not currently do |
| 20 | * antialiasing, so it must be used in conjunction with multisampling. |
| 21 | * |
| 22 | * There are six stages to the algorithm: |
| 23 | * |
| 24 | * 1) Linearize the path contours into piecewise linear segments (path_to_contours()). |
| 25 | * 2) Build a mesh of edges connecting the vertices (build_edges()). |
| 26 | * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). |
| 27 | * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()). |
| 28 | * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). |
| 29 | * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()). |
| 30 | * |
| 31 | * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list |
| 32 | * of vertices (and the necessity of inserting new vertices on intersection). |
| 33 | * |
| 34 | * Stages (4) and (5) use an active edge list, which a list of all edges for which the |
| 35 | * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted |
| 36 | * left-to-right based on the point where both edges are active (when both top vertices |
| 37 | * have been seen, so the "lower" top vertex of the two). If the top vertices are equal |
| 38 | * (shared), it's sorted based on the last point where both edges are active, so the |
| 39 | * "upper" bottom vertex. |
| 40 | * |
| 41 | * The most complex step is the simplification (4). It's based on the Bentley-Ottman |
| 42 | * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are |
| 43 | * not exact and may violate the mesh topology or active edge list ordering. We |
| 44 | * accommodate this by adjusting the topology of the mesh and AEL to match the intersection |
| 45 | * points. This occurs in three ways: |
| 46 | * |
| 47 | * A) Intersections may cause a shortened edge to no longer be ordered with respect to its |
| 48 | * neighbouring edges at the top or bottom vertex. This is handled by merging the |
| 49 | * edges (merge_collinear_edges()). |
| 50 | * B) Intersections may cause an edge to violate the left-to-right ordering of the |
| 51 | * active edge list. This is handled by splitting the neighbour edge on the |
| 52 | * intersected vertex (cleanup_active_edges()). |
| 53 | * C) Shortening an edge may cause an active edge to become inactive or an inactive edge |
| 54 | * to become active. This is handled by removing or inserting the edge in the active |
| 55 | * edge list (fix_active_state()). |
| 56 | * |
| 57 | * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and |
| 58 | * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it |
| 59 | * currently uses a linked list for the active edge list, rather than a 2-3 tree as the |
| 60 | * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also |
| 61 | * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N) |
| 62 | * insertions and removals was greater than the cost of infrequent O(N) lookups with the |
| 63 | * linked list implementation. With the latter, all removals are O(1), and most insertions |
| 64 | * are O(1), since we know the adjacent edge in the active edge list based on the topology. |
| 65 | * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less |
| 66 | * frequent. There may be other data structures worth investigating, however. |
| 67 | * |
| 68 | * Note that there is a compile-time flag (SWEEP_IN_X) which changes the orientation of the |
| 69 | * line sweep algorithms. When SWEEP_IN_X is unset, we sort vertices based on increasing |
| 70 | * Y coordinate, and secondarily by increasing X coordinate. When SWEEP_IN_X is set, we sort by |
| 71 | * increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so that the |
| 72 | * "left" and "right" orientation in the code remains correct (edges to the left are increasing |
| 73 | * in Y; edges to the right are decreasing in Y). That is, the setting rotates 90 degrees |
| 74 | * counterclockwise, rather that transposing. |
| 75 | * |
| 76 | * The choice is arbitrary, but most test cases are wider than they are tall, so the |
| 77 | * default is to sweep in X. In the future, we may want to make this a runtime parameter |
| 78 | * and base it on the aspect ratio of the clip bounds. |
| 79 | */ |
| 80 | #define LOGGING_ENABLED 0 |
| 81 | #define WIREFRAME 0 |
| 82 | #define SWEEP_IN_X 1 |
| 83 | |
| 84 | #if LOGGING_ENABLED |
| 85 | #define LOG printf |
| 86 | #else |
| 87 | #define LOG(...) |
| 88 | #endif |
| 89 | |
| 90 | #define ALLOC_NEW(Type, args, alloc) \ |
| 91 | SkNEW_PLACEMENT_ARGS(alloc.allocThrow(sizeof(Type)), Type, args) |
| 92 | |
| 93 | namespace { |
| 94 | |
| 95 | struct Vertex; |
| 96 | struct Edge; |
| 97 | struct Poly; |
| 98 | |
| 99 | template <class T, T* T::*Prev, T* T::*Next> |
| 100 | void insert(T* t, T* prev, T* next, T** head, T** tail) { |
| 101 | t->*Prev = prev; |
| 102 | t->*Next = next; |
| 103 | if (prev) { |
| 104 | prev->*Next = t; |
| 105 | } else if (head) { |
| 106 | *head = t; |
| 107 | } |
| 108 | if (next) { |
| 109 | next->*Prev = t; |
| 110 | } else if (tail) { |
| 111 | *tail = t; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | template <class T, T* T::*Prev, T* T::*Next> |
| 116 | void remove(T* t, T** head, T** tail) { |
| 117 | if (t->*Prev) { |
| 118 | t->*Prev->*Next = t->*Next; |
| 119 | } else if (head) { |
| 120 | *head = t->*Next; |
| 121 | } |
| 122 | if (t->*Next) { |
| 123 | t->*Next->*Prev = t->*Prev; |
| 124 | } else if (tail) { |
| 125 | *tail = t->*Prev; |
| 126 | } |
| 127 | t->*Prev = t->*Next = NULL; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Vertices are used in three ways: first, the path contours are converted into a |
| 132 | * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices |
| 133 | * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing |
| 134 | * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid |
| 135 | * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of |
| 136 | * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since |
| 137 | * an individual Vertex from the path mesh may belong to multiple |
| 138 | * MonotonePolys, so the original Vertices cannot be re-used. |
| 139 | */ |
| 140 | |
| 141 | struct Vertex { |
| 142 | Vertex(const SkPoint& point) |
| 143 | : fPoint(point), fPrev(NULL), fNext(NULL) |
| 144 | , fFirstEdgeAbove(NULL), fLastEdgeAbove(NULL) |
| 145 | , fFirstEdgeBelow(NULL), fLastEdgeBelow(NULL) |
| 146 | , fProcessed(false) |
| 147 | #if LOGGING_ENABLED |
| 148 | , fID (-1.0f) |
| 149 | #endif |
| 150 | {} |
| 151 | SkPoint fPoint; // Vertex position |
| 152 | Vertex* fPrev; // Linked list of contours, then Y-sorted vertices. |
| 153 | Vertex* fNext; // " |
| 154 | Edge* fFirstEdgeAbove; // Linked list of edges above this vertex. |
| 155 | Edge* fLastEdgeAbove; // " |
| 156 | Edge* fFirstEdgeBelow; // Linked list of edges below this vertex. |
| 157 | Edge* fLastEdgeBelow; // " |
| 158 | bool fProcessed; // Has this vertex been seen in simplify()? |
| 159 | #if LOGGING_ENABLED |
| 160 | float fID; // Identifier used for logging. |
| 161 | #endif |
| 162 | }; |
| 163 | |
| 164 | /***************************************************************************************/ |
| 165 | |
| 166 | bool sweep_lt(const SkPoint& a, const SkPoint& b) { |
| 167 | #if SWEEP_IN_X |
| 168 | return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX; |
| 169 | #else |
| 170 | return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY; |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | bool sweep_gt(const SkPoint& a, const SkPoint& b) { |
| 175 | #if SWEEP_IN_X |
| 176 | return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX; |
| 177 | #else |
| 178 | return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY; |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | inline void* emit_vertex(Vertex* v, void* data) { |
| 183 | SkPoint* d = static_cast<SkPoint*>(data); |
| 184 | *d++ = v->fPoint; |
| 185 | return d; |
| 186 | } |
| 187 | |
| 188 | void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, void* data) { |
| 189 | #if WIREFRAME |
| 190 | data = emit_vertex(v0, data); |
| 191 | data = emit_vertex(v1, data); |
| 192 | data = emit_vertex(v1, data); |
| 193 | data = emit_vertex(v2, data); |
| 194 | data = emit_vertex(v2, data); |
| 195 | data = emit_vertex(v0, data); |
| 196 | #else |
| 197 | data = emit_vertex(v0, data); |
| 198 | data = emit_vertex(v1, data); |
| 199 | data = emit_vertex(v2, data); |
| 200 | #endif |
| 201 | return data; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and |
| 206 | * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf(). |
| 207 | * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating |
| 208 | * point). For speed, that case is only tested by the callers which require it (e.g., |
| 209 | * cleanup_active_edges()). Edges also handle checking for intersection with other edges. |
| 210 | * Currently, this converts the edges to the parametric form, in order to avoid doing a division |
| 211 | * until an intersection has been confirmed. This is slightly slower in the "found" case, but |
| 212 | * a lot faster in the "not found" case. |
| 213 | * |
| 214 | * The coefficients of the line equation stored in double precision to avoid catastrphic |
| 215 | * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is |
| 216 | * correct in float, since it's a polynomial of degree 2. The intersect() function, being |
| 217 | * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its |
| 218 | * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of |
| 219 | * this file). |
| 220 | */ |
| 221 | |
| 222 | struct Edge { |
| 223 | Edge(Vertex* top, Vertex* bottom, int winding) |
| 224 | : fWinding(winding) |
| 225 | , fTop(top) |
| 226 | , fBottom(bottom) |
| 227 | , fLeft(NULL) |
| 228 | , fRight(NULL) |
| 229 | , fPrevEdgeAbove(NULL) |
| 230 | , fNextEdgeAbove(NULL) |
| 231 | , fPrevEdgeBelow(NULL) |
| 232 | , fNextEdgeBelow(NULL) |
| 233 | , fLeftPoly(NULL) |
| 234 | , fRightPoly(NULL) { |
| 235 | recompute(); |
| 236 | } |
| 237 | int fWinding; // 1 == edge goes downward; -1 = edge goes upward. |
| 238 | Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt). |
| 239 | Vertex* fBottom; // The bottom vertex in vertex-sort-order. |
| 240 | Edge* fLeft; // The linked list of edges in the active edge list. |
| 241 | Edge* fRight; // " |
| 242 | Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above". |
| 243 | Edge* fNextEdgeAbove; // " |
| 244 | Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below". |
| 245 | Edge* fNextEdgeBelow; // " |
| 246 | Poly* fLeftPoly; // The Poly to the left of this edge, if any. |
| 247 | Poly* fRightPoly; // The Poly to the right of this edge, if any. |
| 248 | double fDX; // The line equation for this edge, in implicit form. |
| 249 | double fDY; // fDY * x + fDX * y + fC = 0, for point (x, y) on the line. |
| 250 | double fC; |
| 251 | double dist(const SkPoint& p) const { |
| 252 | return fDY * p.fX - fDX * p.fY + fC; |
| 253 | } |
| 254 | bool isRightOf(Vertex* v) const { |
| 255 | return dist(v->fPoint) < 0.0; |
| 256 | } |
| 257 | bool isLeftOf(Vertex* v) const { |
| 258 | return dist(v->fPoint) > 0.0; |
| 259 | } |
| 260 | void recompute() { |
| 261 | fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX; |
| 262 | fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY; |
| 263 | fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX - |
| 264 | static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY; |
| 265 | } |
| 266 | bool intersect(const Edge& other, SkPoint* p) { |
| 267 | LOG("intersecting %g -> %g with %g -> %g\n", |
| 268 | fTop->fID, fBottom->fID, |
| 269 | other.fTop->fID, other.fBottom->fID); |
| 270 | if (fTop == other.fTop || fBottom == other.fBottom) { |
| 271 | return false; |
| 272 | } |
| 273 | double denom = fDX * other.fDY - fDY * other.fDX; |
| 274 | if (denom == 0.0) { |
| 275 | return false; |
| 276 | } |
| 277 | double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX; |
| 278 | double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY; |
| 279 | double sNumer = dy * other.fDX - dx * other.fDY; |
| 280 | double tNumer = dy * fDX - dx * fDY; |
| 281 | // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early. |
| 282 | // This saves us doing the divide below unless absolutely necessary. |
| 283 | if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom) |
| 284 | : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { |
| 285 | return false; |
| 286 | } |
| 287 | double s = sNumer / denom; |
| 288 | SkASSERT(s >= 0.0 && s <= 1.0); |
| 289 | p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX); |
| 290 | p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY); |
| 291 | return true; |
| 292 | } |
| 293 | bool isActive(Edge** activeEdges) const { |
| 294 | return activeEdges && (fLeft || fRight || *activeEdges == this); |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | /***************************************************************************************/ |
| 299 | |
| 300 | struct Poly { |
| 301 | Poly(int winding) |
| 302 | : fWinding(winding) |
| 303 | , fHead(NULL) |
| 304 | , fTail(NULL) |
| 305 | , fActive(NULL) |
| 306 | , fNext(NULL) |
| 307 | , fPartner(NULL) |
| 308 | , fCount(0) |
| 309 | { |
| 310 | #if LOGGING_ENABLED |
| 311 | static int gID = 0; |
| 312 | fID = gID++; |
| 313 | LOG("*** created Poly %d\n", fID); |
| 314 | #endif |
| 315 | } |
| 316 | typedef enum { kNeither_Side, kLeft_Side, kRight_Side } Side; |
| 317 | struct MonotonePoly { |
| 318 | MonotonePoly() |
| 319 | : fSide(kNeither_Side) |
| 320 | , fHead(NULL) |
| 321 | , fTail(NULL) |
| 322 | , fPrev(NULL) |
| 323 | , fNext(NULL) {} |
| 324 | Side fSide; |
| 325 | Vertex* fHead; |
| 326 | Vertex* fTail; |
| 327 | MonotonePoly* fPrev; |
| 328 | MonotonePoly* fNext; |
| 329 | bool addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) { |
| 330 | Vertex* newV = ALLOC_NEW(Vertex, (v->fPoint), alloc); |
| 331 | bool done = false; |
| 332 | if (fSide == kNeither_Side) { |
| 333 | fSide = side; |
| 334 | } else { |
| 335 | done = side != fSide; |
| 336 | } |
| 337 | if (fHead == NULL) { |
| 338 | fHead = fTail = newV; |
| 339 | } else if (fSide == kRight_Side) { |
| 340 | newV->fPrev = fTail; |
| 341 | fTail->fNext = newV; |
| 342 | fTail = newV; |
| 343 | } else { |
| 344 | newV->fNext = fHead; |
| 345 | fHead->fPrev = newV; |
| 346 | fHead = newV; |
| 347 | } |
| 348 | return done; |
| 349 | } |
| 350 | |
| 351 | void* emit(void* data) { |
| 352 | Vertex* first = fHead; |
| 353 | Vertex* v = first->fNext; |
| 354 | while (v != fTail) { |
| 355 | SkASSERT(v && v->fPrev && v->fNext); |
| 356 | #ifdef SK_DEBUG |
| 357 | validate(); |
| 358 | #endif |
| 359 | Vertex* prev = v->fPrev; |
| 360 | Vertex* curr = v; |
| 361 | Vertex* next = v->fNext; |
| 362 | double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; |
| 363 | double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; |
| 364 | double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; |
| 365 | double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; |
| 366 | if (ax * by - ay * bx >= 0.0) { |
| 367 | data = emit_triangle(prev, curr, next, data); |
| 368 | v->fPrev->fNext = v->fNext; |
| 369 | v->fNext->fPrev = v->fPrev; |
| 370 | if (v->fPrev == first) { |
| 371 | v = v->fNext; |
| 372 | } else { |
| 373 | v = v->fPrev; |
| 374 | } |
| 375 | } else { |
| 376 | v = v->fNext; |
| 377 | SkASSERT(v != fTail); |
| 378 | } |
| 379 | } |
| 380 | return data; |
| 381 | } |
| 382 | |
| 383 | #ifdef SK_DEBUG |
| 384 | void validate() { |
| 385 | int winding = sweep_lt(fHead->fPoint, fTail->fPoint) ? 1 : -1; |
| 386 | Vertex* top = winding < 0 ? fTail : fHead; |
| 387 | Vertex* bottom = winding < 0 ? fHead : fTail; |
| 388 | Edge e(top, bottom, winding); |
| 389 | for (Vertex* v = fHead->fNext; v != fTail; v = v->fNext) { |
| 390 | if (fSide == kRight_Side) { |
| 391 | SkASSERT(!e.isRightOf(v)); |
| 392 | } else if (fSide == Poly::kLeft_Side) { |
| 393 | SkASSERT(!e.isLeftOf(v)); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | #endif |
| 398 | }; |
| 399 | Poly* addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) { |
| 400 | LOG("addVertex() to %d at %g (%g, %g), %s side\n", fID, v->fID, v->fPoint.fX, v->fPoint.fY, |
| 401 | side == kLeft_Side ? "left" : side == kRight_Side ? "right" : "neither"); |
| 402 | Poly* partner = fPartner; |
| 403 | Poly* poly = this; |
| 404 | if (partner) { |
| 405 | fPartner = partner->fPartner = NULL; |
| 406 | } |
| 407 | if (!fActive) { |
| 408 | fActive = ALLOC_NEW(MonotonePoly, (), alloc); |
| 409 | } |
| 410 | if (fActive->addVertex(v, side, alloc)) { |
| 411 | #ifdef SK_DEBUG |
| 412 | fActive->validate(); |
| 413 | #endif |
| 414 | if (fTail) { |
| 415 | fActive->fPrev = fTail; |
| 416 | fTail->fNext = fActive; |
| 417 | fTail = fActive; |
| 418 | } else { |
| 419 | fHead = fTail = fActive; |
| 420 | } |
| 421 | if (partner) { |
| 422 | partner->addVertex(v, side, alloc); |
| 423 | poly = partner; |
| 424 | } else { |
| 425 | Vertex* prev = fActive->fSide == Poly::kLeft_Side ? |
| 426 | fActive->fHead->fNext : fActive->fTail->fPrev; |
| 427 | fActive = ALLOC_NEW(MonotonePoly, , alloc); |
| 428 | fActive->addVertex(prev, Poly::kNeither_Side, alloc); |
| 429 | fActive->addVertex(v, side, alloc); |
| 430 | } |
| 431 | } |
| 432 | fCount++; |
| 433 | return poly; |
| 434 | } |
| 435 | void end(Vertex* v, SkChunkAlloc& alloc) { |
| 436 | LOG("end() %d at %g, %g\n", fID, v->fPoint.fX, v->fPoint.fY); |
| 437 | if (fPartner) { |
| 438 | fPartner = fPartner->fPartner = NULL; |
| 439 | } |
| 440 | addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, alloc); |
| 441 | } |
| 442 | void* emit(void *data) { |
| 443 | if (fCount < 3) { |
| 444 | return data; |
| 445 | } |
| 446 | LOG("emit() %d, size %d\n", fID, fCount); |
| 447 | for (MonotonePoly* m = fHead; m != NULL; m = m->fNext) { |
| 448 | data = m->emit(data); |
| 449 | } |
| 450 | return data; |
| 451 | } |
| 452 | int fWinding; |
| 453 | MonotonePoly* fHead; |
| 454 | MonotonePoly* fTail; |
| 455 | MonotonePoly* fActive; |
| 456 | Poly* fNext; |
| 457 | Poly* fPartner; |
| 458 | int fCount; |
| 459 | #if LOGGING_ENABLED |
| 460 | int fID; |
| 461 | #endif |
| 462 | }; |
| 463 | |
| 464 | /***************************************************************************************/ |
| 465 | |
| 466 | bool coincident(const SkPoint& a, const SkPoint& b) { |
| 467 | return a == b; |
| 468 | } |
| 469 | |
| 470 | Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) { |
| 471 | Poly* poly = ALLOC_NEW(Poly, (winding), alloc); |
| 472 | poly->addVertex(v, Poly::kNeither_Side, alloc); |
| 473 | poly->fNext = *head; |
| 474 | *head = poly; |
| 475 | return poly; |
| 476 | } |
| 477 | |
| 478 | #ifdef SK_DEBUG |
| 479 | void validate_edges(Edge* head) { |
| 480 | for (Edge* e = head; e != NULL; e = e->fRight) { |
| 481 | SkASSERT(e->fTop != e->fBottom); |
| 482 | if (e->fLeft) { |
| 483 | SkASSERT(e->fLeft->fRight == e); |
| 484 | if (sweep_gt(e->fTop->fPoint, e->fLeft->fTop->fPoint)) { |
| 485 | SkASSERT(e->fLeft->isLeftOf(e->fTop)); |
| 486 | } |
| 487 | if (sweep_lt(e->fBottom->fPoint, e->fLeft->fBottom->fPoint)) { |
| 488 | SkASSERT(e->fLeft->isLeftOf(e->fBottom)); |
| 489 | } |
| 490 | } else { |
| 491 | SkASSERT(e == head); |
| 492 | } |
| 493 | if (e->fRight) { |
| 494 | SkASSERT(e->fRight->fLeft == e); |
| 495 | if (sweep_gt(e->fTop->fPoint, e->fRight->fTop->fPoint)) { |
| 496 | SkASSERT(e->fRight->isRightOf(e->fTop)); |
| 497 | } |
| 498 | if (sweep_lt(e->fBottom->fPoint, e->fRight->fBottom->fPoint)) { |
| 499 | SkASSERT(e->fRight->isRightOf(e->fBottom)); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | void validate_connectivity(Vertex* v) { |
| 506 | for (Edge* e = v->fFirstEdgeAbove; e != NULL; e = e->fNextEdgeAbove) { |
| 507 | SkASSERT(e->fBottom == v); |
| 508 | if (e->fPrevEdgeAbove) { |
| 509 | SkASSERT(e->fPrevEdgeAbove->fNextEdgeAbove == e); |
| 510 | SkASSERT(e->fPrevEdgeAbove->isLeftOf(e->fTop)); |
| 511 | } else { |
| 512 | SkASSERT(e == v->fFirstEdgeAbove); |
| 513 | } |
| 514 | if (e->fNextEdgeAbove) { |
| 515 | SkASSERT(e->fNextEdgeAbove->fPrevEdgeAbove == e); |
| 516 | SkASSERT(e->fNextEdgeAbove->isRightOf(e->fTop)); |
| 517 | } else { |
| 518 | SkASSERT(e == v->fLastEdgeAbove); |
| 519 | } |
| 520 | } |
| 521 | for (Edge* e = v->fFirstEdgeBelow; e != NULL; e = e->fNextEdgeBelow) { |
| 522 | SkASSERT(e->fTop == v); |
| 523 | if (e->fPrevEdgeBelow) { |
| 524 | SkASSERT(e->fPrevEdgeBelow->fNextEdgeBelow == e); |
| 525 | SkASSERT(e->fPrevEdgeBelow->isLeftOf(e->fBottom)); |
| 526 | } else { |
| 527 | SkASSERT(e == v->fFirstEdgeBelow); |
| 528 | } |
| 529 | if (e->fNextEdgeBelow) { |
| 530 | SkASSERT(e->fNextEdgeBelow->fPrevEdgeBelow == e); |
| 531 | SkASSERT(e->fNextEdgeBelow->isRightOf(e->fBottom)); |
| 532 | } else { |
| 533 | SkASSERT(e == v->fLastEdgeBelow); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | #endif |
| 538 | |
| 539 | Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, |
| 540 | SkChunkAlloc& alloc) { |
| 541 | Vertex* v = ALLOC_NEW(Vertex, (p), alloc); |
| 542 | #if LOGGING_ENABLED |
| 543 | static float gID = 0.0f; |
| 544 | v->fID = gID++; |
| 545 | #endif |
| 546 | if (prev) { |
| 547 | prev->fNext = v; |
| 548 | v->fPrev = prev; |
| 549 | } else { |
| 550 | *head = v; |
| 551 | } |
| 552 | return v; |
| 553 | } |
| 554 | |
| 555 | Vertex* generate_quadratic_points(const SkPoint& p0, |
| 556 | const SkPoint& p1, |
| 557 | const SkPoint& p2, |
| 558 | SkScalar tolSqd, |
| 559 | Vertex* prev, |
| 560 | Vertex** head, |
| 561 | int pointsLeft, |
| 562 | SkChunkAlloc& alloc) { |
| 563 | SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2); |
| 564 | if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) { |
| 565 | return append_point_to_contour(p2, prev, head, alloc); |
| 566 | } |
| 567 | |
| 568 | const SkPoint q[] = { |
| 569 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 570 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 571 | }; |
| 572 | const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; |
| 573 | |
| 574 | pointsLeft >>= 1; |
| 575 | prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc); |
| 576 | prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc); |
| 577 | return prev; |
| 578 | } |
| 579 | |
| 580 | Vertex* generate_cubic_points(const SkPoint& p0, |
| 581 | const SkPoint& p1, |
| 582 | const SkPoint& p2, |
| 583 | const SkPoint& p3, |
| 584 | SkScalar tolSqd, |
| 585 | Vertex* prev, |
| 586 | Vertex** head, |
| 587 | int pointsLeft, |
| 588 | SkChunkAlloc& alloc) { |
| 589 | SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3); |
| 590 | SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3); |
| 591 | if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || |
| 592 | !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) { |
| 593 | return append_point_to_contour(p3, prev, head, alloc); |
| 594 | } |
| 595 | const SkPoint q[] = { |
| 596 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 597 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 598 | { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
| 599 | }; |
| 600 | const SkPoint r[] = { |
| 601 | { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
| 602 | { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } |
| 603 | }; |
| 604 | const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
| 605 | pointsLeft >>= 1; |
| 606 | prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc); |
| 607 | prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc); |
| 608 | return prev; |
| 609 | } |
| 610 | |
| 611 | // Stage 1: convert the input path to a set of linear contours (linked list of Vertices). |
| 612 | |
| 613 | void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
| 614 | Vertex** contours, SkChunkAlloc& alloc) { |
| 615 | |
| 616 | SkScalar toleranceSqd = tolerance * tolerance; |
| 617 | |
| 618 | SkPoint pts[4]; |
| 619 | bool done = false; |
| 620 | SkPath::Iter iter(path, false); |
| 621 | Vertex* prev = NULL; |
| 622 | Vertex* head = NULL; |
| 623 | if (path.isInverseFillType()) { |
| 624 | SkPoint quad[4]; |
| 625 | clipBounds.toQuad(quad); |
| 626 | for (int i = 3; i >= 0; i--) { |
| 627 | prev = append_point_to_contour(quad[i], prev, &head, alloc); |
| 628 | } |
| 629 | head->fPrev = prev; |
| 630 | prev->fNext = head; |
| 631 | *contours++ = head; |
| 632 | head = prev = NULL; |
| 633 | } |
| 634 | SkAutoConicToQuads converter; |
| 635 | while (!done) { |
| 636 | SkPath::Verb verb = iter.next(pts); |
| 637 | switch (verb) { |
| 638 | case SkPath::kConic_Verb: { |
| 639 | SkScalar weight = iter.conicWeight(); |
| 640 | const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd); |
| 641 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 642 | int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, toleranceSqd); |
| 643 | prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2], |
| 644 | toleranceSqd, prev, &head, pointsLeft, alloc); |
| 645 | quadPts += 2; |
| 646 | } |
| 647 | break; |
| 648 | } |
| 649 | case SkPath::kMove_Verb: |
| 650 | if (head) { |
| 651 | head->fPrev = prev; |
| 652 | prev->fNext = head; |
| 653 | *contours++ = head; |
| 654 | } |
| 655 | head = prev = NULL; |
| 656 | prev = append_point_to_contour(pts[0], prev, &head, alloc); |
| 657 | break; |
| 658 | case SkPath::kLine_Verb: { |
| 659 | prev = append_point_to_contour(pts[1], prev, &head, alloc); |
| 660 | break; |
| 661 | } |
| 662 | case SkPath::kQuad_Verb: { |
| 663 | int pointsLeft = GrPathUtils::quadraticPointCount(pts, toleranceSqd); |
| 664 | prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev, |
| 665 | &head, pointsLeft, alloc); |
| 666 | break; |
| 667 | } |
| 668 | case SkPath::kCubic_Verb: { |
| 669 | int pointsLeft = GrPathUtils::cubicPointCount(pts, toleranceSqd); |
| 670 | prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3], |
| 671 | toleranceSqd, prev, &head, pointsLeft, alloc); |
| 672 | break; |
| 673 | } |
| 674 | case SkPath::kClose_Verb: |
| 675 | if (head) { |
| 676 | head->fPrev = prev; |
| 677 | prev->fNext = head; |
| 678 | *contours++ = head; |
| 679 | } |
| 680 | head = prev = NULL; |
| 681 | break; |
| 682 | case SkPath::kDone_Verb: |
| 683 | if (head) { |
| 684 | head->fPrev = prev; |
| 685 | prev->fNext = head; |
| 686 | *contours++ = head; |
| 687 | } |
| 688 | done = true; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | inline bool apply_fill_type(SkPath::FillType fillType, int winding) { |
| 695 | switch (fillType) { |
| 696 | case SkPath::kWinding_FillType: |
| 697 | return winding != 0; |
| 698 | case SkPath::kEvenOdd_FillType: |
| 699 | return (winding & 1) != 0; |
| 700 | case SkPath::kInverseWinding_FillType: |
| 701 | return winding == 1; |
| 702 | case SkPath::kInverseEvenOdd_FillType: |
| 703 | return (winding & 1) == 1; |
| 704 | default: |
| 705 | SkASSERT(false); |
| 706 | return false; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc) { |
| 711 | int winding = sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
| 712 | Vertex* top = winding < 0 ? next : prev; |
| 713 | Vertex* bottom = winding < 0 ? prev : next; |
| 714 | return ALLOC_NEW(Edge, (top, bottom, winding), alloc); |
| 715 | } |
| 716 | |
| 717 | void remove_edge(Edge* edge, Edge** head) { |
| 718 | LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
| 719 | SkASSERT(edge->isActive(head)); |
| 720 | remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, head, NULL); |
| 721 | } |
| 722 | |
| 723 | void insert_edge(Edge* edge, Edge* prev, Edge** head) { |
| 724 | LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
| 725 | SkASSERT(!edge->isActive(head)); |
| 726 | Edge* next = prev ? prev->fRight : *head; |
| 727 | insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, head, NULL); |
| 728 | } |
| 729 | |
| 730 | void find_enclosing_edges(Vertex* v, Edge* head, Edge** left, Edge** right) { |
| 731 | if (v->fFirstEdgeAbove) { |
| 732 | *left = v->fFirstEdgeAbove->fLeft; |
| 733 | *right = v->fLastEdgeAbove->fRight; |
| 734 | return; |
| 735 | } |
| 736 | Edge* prev = NULL; |
| 737 | Edge* next; |
| 738 | for (next = head; next != NULL; next = next->fRight) { |
| 739 | if (next->isRightOf(v)) { |
| 740 | break; |
| 741 | } |
| 742 | prev = next; |
| 743 | } |
| 744 | *left = prev; |
| 745 | *right = next; |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | void find_enclosing_edges(Edge* edge, Edge* head, Edge** left, Edge** right) { |
| 750 | Edge* prev = NULL; |
| 751 | Edge* next; |
| 752 | for (next = head; next != NULL; next = next->fRight) { |
| 753 | if ((sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) || |
| 754 | (sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) || |
| 755 | (sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) && |
| 756 | next->isRightOf(edge->fBottom)) || |
| 757 | (sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) && |
| 758 | edge->isLeftOf(next->fBottom))) { |
| 759 | break; |
| 760 | } |
| 761 | prev = next; |
| 762 | } |
| 763 | *left = prev; |
| 764 | *right = next; |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | void fix_active_state(Edge* edge, Edge** activeEdges) { |
| 769 | if (edge->isActive(activeEdges)) { |
| 770 | if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) { |
| 771 | remove_edge(edge, activeEdges); |
| 772 | } |
| 773 | } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) { |
| 774 | Edge* left; |
| 775 | Edge* right; |
| 776 | find_enclosing_edges(edge, *activeEdges, &left, &right); |
| 777 | insert_edge(edge, left, activeEdges); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | void insert_edge_above(Edge* edge, Vertex* v) { |
| 782 | if (edge->fTop->fPoint == edge->fBottom->fPoint || |
| 783 | sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { |
| 784 | SkASSERT(false); |
| 785 | return; |
| 786 | } |
| 787 | LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); |
| 788 | Edge* prev = NULL; |
| 789 | Edge* next; |
| 790 | for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { |
| 791 | if (next->isRightOf(edge->fTop)) { |
| 792 | break; |
| 793 | } |
| 794 | prev = next; |
| 795 | } |
| 796 | insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
| 797 | edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); |
| 798 | } |
| 799 | |
| 800 | void insert_edge_below(Edge* edge, Vertex* v) { |
| 801 | if (edge->fTop->fPoint == edge->fBottom->fPoint || |
| 802 | sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { |
| 803 | SkASSERT(false); |
| 804 | return; |
| 805 | } |
| 806 | LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); |
| 807 | Edge* prev = NULL; |
| 808 | Edge* next; |
| 809 | for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { |
| 810 | if (next->isRightOf(edge->fBottom)) { |
| 811 | break; |
| 812 | } |
| 813 | prev = next; |
| 814 | } |
| 815 | insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
| 816 | edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); |
| 817 | } |
| 818 | |
| 819 | void remove_edge_above(Edge* edge) { |
| 820 | LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 821 | edge->fBottom->fID); |
| 822 | remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
| 823 | edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); |
| 824 | } |
| 825 | |
| 826 | void remove_edge_below(Edge* edge) { |
| 827 | LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 828 | edge->fTop->fID); |
| 829 | remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
| 830 | edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); |
| 831 | } |
| 832 | |
| 833 | void erase_edge_if_zero_winding(Edge* edge, Edge** head) { |
| 834 | if (edge->fWinding != 0) { |
| 835 | return; |
| 836 | } |
| 837 | LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID); |
| 838 | remove_edge_above(edge); |
| 839 | remove_edge_below(edge); |
| 840 | if (edge->isActive(head)) { |
| 841 | remove_edge(edge, head); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void merge_collinear_edges(Edge* edge, Edge** activeEdges); |
| 846 | |
| 847 | void set_top(Edge* edge, Vertex* v, Edge** activeEdges) { |
| 848 | remove_edge_below(edge); |
| 849 | edge->fTop = v; |
| 850 | edge->recompute(); |
| 851 | insert_edge_below(edge, v); |
| 852 | fix_active_state(edge, activeEdges); |
| 853 | merge_collinear_edges(edge, activeEdges); |
| 854 | } |
| 855 | |
| 856 | void set_bottom(Edge* edge, Vertex* v, Edge** activeEdges) { |
| 857 | remove_edge_above(edge); |
| 858 | edge->fBottom = v; |
| 859 | edge->recompute(); |
| 860 | insert_edge_above(edge, v); |
| 861 | fix_active_state(edge, activeEdges); |
| 862 | merge_collinear_edges(edge, activeEdges); |
| 863 | } |
| 864 | |
| 865 | void merge_edges_above(Edge* edge, Edge* other, Edge** activeEdges) { |
| 866 | if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { |
| 867 | LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", |
| 868 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 869 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
| 870 | other->fWinding += edge->fWinding; |
| 871 | erase_edge_if_zero_winding(other, activeEdges); |
| 872 | edge->fWinding = 0; |
| 873 | erase_edge_if_zero_winding(edge, activeEdges); |
| 874 | } else if (sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { |
| 875 | other->fWinding += edge->fWinding; |
| 876 | erase_edge_if_zero_winding(other, activeEdges); |
| 877 | set_bottom(edge, other->fTop, activeEdges); |
| 878 | } else { |
| 879 | edge->fWinding += other->fWinding; |
| 880 | erase_edge_if_zero_winding(edge, activeEdges); |
| 881 | set_bottom(other, edge->fTop, activeEdges); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void merge_edges_below(Edge* edge, Edge* other, Edge** activeEdges) { |
| 886 | if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
| 887 | LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", |
| 888 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 889 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
| 890 | other->fWinding += edge->fWinding; |
| 891 | erase_edge_if_zero_winding(other, activeEdges); |
| 892 | edge->fWinding = 0; |
| 893 | erase_edge_if_zero_winding(edge, activeEdges); |
| 894 | } else if (sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
| 895 | edge->fWinding += other->fWinding; |
| 896 | erase_edge_if_zero_winding(edge, activeEdges); |
| 897 | set_top(other, edge->fBottom, activeEdges); |
| 898 | } else { |
| 899 | other->fWinding += edge->fWinding; |
| 900 | erase_edge_if_zero_winding(other, activeEdges); |
| 901 | set_top(edge, other->fBottom, activeEdges); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | void merge_collinear_edges(Edge* edge, Edge** activeEdges) { |
| 906 | if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop || |
| 907 | !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) { |
| 908 | merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges); |
| 909 | } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop || |
| 910 | !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) { |
| 911 | merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges); |
| 912 | } |
| 913 | if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom || |
| 914 | !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) { |
| 915 | merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges); |
| 916 | } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom || |
| 917 | !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) { |
| 918 | merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | void split_edge(Edge* edge, Vertex* v, Edge** activeEdges, SkChunkAlloc& alloc); |
| 923 | |
| 924 | void cleanup_active_edges(Edge* edge, Edge** activeEdges, SkChunkAlloc& alloc) { |
| 925 | Vertex* top = edge->fTop; |
| 926 | Vertex* bottom = edge->fBottom; |
| 927 | if (edge->fLeft) { |
| 928 | Vertex* leftTop = edge->fLeft->fTop; |
| 929 | Vertex* leftBottom = edge->fLeft->fBottom; |
| 930 | if (sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) { |
| 931 | split_edge(edge->fLeft, edge->fTop, activeEdges, alloc); |
| 932 | } else if (sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) { |
| 933 | split_edge(edge, leftTop, activeEdges, alloc); |
| 934 | } else if (sweep_lt(bottom->fPoint, leftBottom->fPoint) && !edge->fLeft->isLeftOf(bottom)) { |
| 935 | split_edge(edge->fLeft, bottom, activeEdges, alloc); |
| 936 | } else if (sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) { |
| 937 | split_edge(edge, leftBottom, activeEdges, alloc); |
| 938 | } |
| 939 | } |
| 940 | if (edge->fRight) { |
| 941 | Vertex* rightTop = edge->fRight->fTop; |
| 942 | Vertex* rightBottom = edge->fRight->fBottom; |
| 943 | if (sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) { |
| 944 | split_edge(edge->fRight, top, activeEdges, alloc); |
| 945 | } else if (sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) { |
| 946 | split_edge(edge, rightTop, activeEdges, alloc); |
| 947 | } else if (sweep_lt(bottom->fPoint, rightBottom->fPoint) && |
| 948 | !edge->fRight->isRightOf(bottom)) { |
| 949 | split_edge(edge->fRight, bottom, activeEdges, alloc); |
| 950 | } else if (sweep_lt(rightBottom->fPoint, bottom->fPoint) && |
| 951 | !edge->isLeftOf(rightBottom)) { |
| 952 | split_edge(edge, rightBottom, activeEdges, alloc); |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | void split_edge(Edge* edge, Vertex* v, Edge** activeEdges, SkChunkAlloc& alloc) { |
| 958 | LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", |
| 959 | edge->fTop->fID, edge->fBottom->fID, |
| 960 | v->fID, v->fPoint.fX, v->fPoint.fY); |
senorblanco | a2b6d28 | 2015-03-02 09:34:13 -0800 | [diff] [blame^] | 961 | if (sweep_lt(v->fPoint, edge->fTop->fPoint)) { |
| 962 | set_top(edge, v, activeEdges); |
| 963 | } else if (sweep_gt(v->fPoint, edge->fBottom->fPoint)) { |
| 964 | set_bottom(edge, v, activeEdges); |
| 965 | } else { |
| 966 | Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), alloc); |
| 967 | insert_edge_below(newEdge, v); |
| 968 | insert_edge_above(newEdge, edge->fBottom); |
| 969 | set_bottom(edge, v, activeEdges); |
| 970 | cleanup_active_edges(edge, activeEdges, alloc); |
| 971 | fix_active_state(newEdge, activeEdges); |
| 972 | merge_collinear_edges(newEdge, activeEdges); |
| 973 | } |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, SkChunkAlloc& alloc) { |
| 977 | LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY, |
| 978 | src->fID, dst->fID); |
| 979 | for (Edge* edge = src->fFirstEdgeAbove; edge;) { |
| 980 | Edge* next = edge->fNextEdgeAbove; |
| 981 | set_bottom(edge, dst, NULL); |
| 982 | edge = next; |
| 983 | } |
| 984 | for (Edge* edge = src->fFirstEdgeBelow; edge;) { |
| 985 | Edge* next = edge->fNextEdgeBelow; |
| 986 | set_top(edge, dst, NULL); |
| 987 | edge = next; |
| 988 | } |
| 989 | remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, NULL); |
| 990 | } |
| 991 | |
| 992 | Vertex* check_for_intersection(Edge* edge, Edge* other, Edge** activeEdges, SkChunkAlloc& alloc) { |
| 993 | SkPoint p; |
| 994 | if (!edge || !other) { |
| 995 | return NULL; |
| 996 | } |
| 997 | if (edge->intersect(*other, &p)) { |
| 998 | Vertex* v; |
| 999 | LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); |
| 1000 | if (p == edge->fTop->fPoint || sweep_lt(p, edge->fTop->fPoint)) { |
| 1001 | split_edge(other, edge->fTop, activeEdges, alloc); |
| 1002 | v = edge->fTop; |
| 1003 | } else if (p == edge->fBottom->fPoint || sweep_gt(p, edge->fBottom->fPoint)) { |
| 1004 | split_edge(other, edge->fBottom, activeEdges, alloc); |
| 1005 | v = edge->fBottom; |
| 1006 | } else if (p == other->fTop->fPoint || sweep_lt(p, other->fTop->fPoint)) { |
| 1007 | split_edge(edge, other->fTop, activeEdges, alloc); |
| 1008 | v = other->fTop; |
| 1009 | } else if (p == other->fBottom->fPoint || sweep_gt(p, other->fBottom->fPoint)) { |
| 1010 | split_edge(edge, other->fBottom, activeEdges, alloc); |
| 1011 | v = other->fBottom; |
| 1012 | } else { |
| 1013 | Vertex* nextV = edge->fTop; |
| 1014 | while (sweep_lt(p, nextV->fPoint)) { |
| 1015 | nextV = nextV->fPrev; |
| 1016 | } |
| 1017 | while (sweep_lt(nextV->fPoint, p)) { |
| 1018 | nextV = nextV->fNext; |
| 1019 | } |
| 1020 | Vertex* prevV = nextV->fPrev; |
| 1021 | if (coincident(prevV->fPoint, p)) { |
| 1022 | v = prevV; |
| 1023 | } else if (coincident(nextV->fPoint, p)) { |
| 1024 | v = nextV; |
| 1025 | } else { |
| 1026 | v = ALLOC_NEW(Vertex, (p), alloc); |
| 1027 | LOG("inserting between %g (%g, %g) and %g (%g, %g)\n", |
| 1028 | prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY, |
| 1029 | nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY); |
| 1030 | #if LOGGING_ENABLED |
| 1031 | v->fID = (nextV->fID + prevV->fID) * 0.5f; |
| 1032 | #endif |
| 1033 | v->fPrev = prevV; |
| 1034 | v->fNext = nextV; |
| 1035 | prevV->fNext = v; |
| 1036 | nextV->fPrev = v; |
| 1037 | } |
| 1038 | split_edge(edge, v, activeEdges, alloc); |
| 1039 | split_edge(other, v, activeEdges, alloc); |
| 1040 | } |
| 1041 | #ifdef SK_DEBUG |
| 1042 | validate_connectivity(v); |
| 1043 | #endif |
| 1044 | return v; |
| 1045 | } |
| 1046 | return NULL; |
| 1047 | } |
| 1048 | |
| 1049 | void sanitize_contours(Vertex** contours, int contourCnt) { |
| 1050 | for (int i = 0; i < contourCnt; ++i) { |
| 1051 | SkASSERT(contours[i]); |
| 1052 | for (Vertex* v = contours[i];;) { |
| 1053 | if (coincident(v->fPrev->fPoint, v->fPoint)) { |
| 1054 | LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1055 | if (v->fPrev == v) { |
| 1056 | contours[i] = NULL; |
| 1057 | break; |
| 1058 | } |
| 1059 | v->fPrev->fNext = v->fNext; |
| 1060 | v->fNext->fPrev = v->fPrev; |
| 1061 | if (contours[i] == v) { |
| 1062 | contours[i] = v->fNext; |
| 1063 | } |
| 1064 | v = v->fPrev; |
| 1065 | } else { |
| 1066 | v = v->fNext; |
| 1067 | if (v == contours[i]) break; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | void merge_coincident_vertices(Vertex** vertices, SkChunkAlloc& alloc) { |
| 1074 | for (Vertex* v = (*vertices)->fNext; v != NULL; v = v->fNext) { |
| 1075 | if (sweep_lt(v->fPoint, v->fPrev->fPoint)) { |
| 1076 | v->fPoint = v->fPrev->fPoint; |
| 1077 | } |
| 1078 | if (coincident(v->fPrev->fPoint, v->fPoint)) { |
| 1079 | merge_vertices(v->fPrev, v, vertices, alloc); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | // Stage 2: convert the contours to a mesh of edges connecting the vertices. |
| 1085 | |
| 1086 | Vertex* build_edges(Vertex** contours, int contourCnt, SkChunkAlloc& alloc) { |
| 1087 | Vertex* vertices = NULL; |
| 1088 | Vertex* prev = NULL; |
| 1089 | for (int i = 0; i < contourCnt; ++i) { |
| 1090 | for (Vertex* v = contours[i]; v != NULL;) { |
| 1091 | Vertex* vNext = v->fNext; |
| 1092 | Edge* edge = new_edge(v->fPrev, v, alloc); |
| 1093 | if (edge->fWinding > 0) { |
| 1094 | insert_edge_below(edge, v->fPrev); |
| 1095 | insert_edge_above(edge, v); |
| 1096 | } else { |
| 1097 | insert_edge_below(edge, v); |
| 1098 | insert_edge_above(edge, v->fPrev); |
| 1099 | } |
| 1100 | merge_collinear_edges(edge, NULL); |
| 1101 | if (prev) { |
| 1102 | prev->fNext = v; |
| 1103 | v->fPrev = prev; |
| 1104 | } else { |
| 1105 | vertices = v; |
| 1106 | } |
| 1107 | prev = v; |
| 1108 | v = vNext; |
| 1109 | if (v == contours[i]) break; |
| 1110 | } |
| 1111 | } |
| 1112 | if (prev) { |
| 1113 | prev->fNext = vertices->fPrev = NULL; |
| 1114 | } |
| 1115 | return vertices; |
| 1116 | } |
| 1117 | |
| 1118 | // Stage 3: sort the vertices by increasing Y (or X if SWEEP_IN_X is on). |
| 1119 | |
| 1120 | Vertex* sorted_merge(Vertex* a, Vertex* b); |
| 1121 | |
| 1122 | void front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) { |
| 1123 | Vertex* fast; |
| 1124 | Vertex* slow; |
| 1125 | if (!v || !v->fNext) { |
| 1126 | *pFront = v; |
| 1127 | *pBack = NULL; |
| 1128 | } else { |
| 1129 | slow = v; |
| 1130 | fast = v->fNext; |
| 1131 | |
| 1132 | while (fast != NULL) { |
| 1133 | fast = fast->fNext; |
| 1134 | if (fast != NULL) { |
| 1135 | slow = slow->fNext; |
| 1136 | fast = fast->fNext; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | *pFront = v; |
| 1141 | *pBack = slow->fNext; |
| 1142 | slow->fNext->fPrev = NULL; |
| 1143 | slow->fNext = NULL; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void merge_sort(Vertex** head) { |
| 1148 | if (!*head || !(*head)->fNext) { |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | Vertex* a; |
| 1153 | Vertex* b; |
| 1154 | front_back_split(*head, &a, &b); |
| 1155 | |
| 1156 | merge_sort(&a); |
| 1157 | merge_sort(&b); |
| 1158 | |
| 1159 | *head = sorted_merge(a, b); |
| 1160 | } |
| 1161 | |
| 1162 | Vertex* sorted_merge(Vertex* a, Vertex* b) { |
| 1163 | if (!a) { |
| 1164 | return b; |
| 1165 | } else if (!b) { |
| 1166 | return a; |
| 1167 | } |
| 1168 | |
| 1169 | Vertex* result = NULL; |
| 1170 | |
| 1171 | if (sweep_lt(a->fPoint, b->fPoint)) { |
| 1172 | result = a; |
| 1173 | result->fNext = sorted_merge(a->fNext, b); |
| 1174 | } else { |
| 1175 | result = b; |
| 1176 | result->fNext = sorted_merge(a, b->fNext); |
| 1177 | } |
| 1178 | result->fNext->fPrev = result; |
| 1179 | return result; |
| 1180 | } |
| 1181 | |
| 1182 | // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. |
| 1183 | |
| 1184 | void simplify(Vertex* vertices, SkChunkAlloc& alloc) { |
| 1185 | LOG("simplifying complex polygons\n"); |
| 1186 | Edge* activeEdges = NULL; |
| 1187 | for (Vertex* v = vertices; v != NULL; v = v->fNext) { |
| 1188 | if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| 1189 | continue; |
| 1190 | } |
| 1191 | #if LOGGING_ENABLED |
| 1192 | LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); |
| 1193 | #endif |
| 1194 | #ifdef SK_DEBUG |
| 1195 | validate_connectivity(v); |
| 1196 | #endif |
| 1197 | Edge* leftEnclosingEdge = NULL; |
| 1198 | Edge* rightEnclosingEdge = NULL; |
| 1199 | bool restartChecks; |
| 1200 | do { |
| 1201 | restartChecks = false; |
| 1202 | find_enclosing_edges(v, activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
| 1203 | if (v->fFirstEdgeBelow) { |
| 1204 | for (Edge* edge = v->fFirstEdgeBelow; edge != NULL; edge = edge->fNextEdgeBelow) { |
| 1205 | if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, alloc)) { |
| 1206 | restartChecks = true; |
| 1207 | break; |
| 1208 | } |
| 1209 | if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, alloc)) { |
| 1210 | restartChecks = true; |
| 1211 | break; |
| 1212 | } |
| 1213 | } |
| 1214 | } else { |
| 1215 | if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, |
| 1216 | &activeEdges, alloc)) { |
| 1217 | if (sweep_lt(pv->fPoint, v->fPoint)) { |
| 1218 | v = pv; |
| 1219 | } |
| 1220 | restartChecks = true; |
| 1221 | } |
| 1222 | |
| 1223 | } |
| 1224 | } while (restartChecks); |
| 1225 | SkASSERT(!leftEnclosingEdge || leftEnclosingEdge->isLeftOf(v)); |
| 1226 | SkASSERT(!rightEnclosingEdge || rightEnclosingEdge->isRightOf(v)); |
| 1227 | #ifdef SK_DEBUG |
| 1228 | validate_edges(activeEdges); |
| 1229 | #endif |
| 1230 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1231 | remove_edge(e, &activeEdges); |
| 1232 | } |
| 1233 | Edge* leftEdge = leftEnclosingEdge; |
| 1234 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1235 | insert_edge(e, leftEdge, &activeEdges); |
| 1236 | leftEdge = e; |
| 1237 | } |
| 1238 | v->fProcessed = true; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | // Stage 5: Tessellate the simplified mesh into monotone polygons. |
| 1243 | |
| 1244 | Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) { |
| 1245 | LOG("tessellating simple polygons\n"); |
| 1246 | Edge* activeEdges = NULL; |
| 1247 | Poly* polys = NULL; |
| 1248 | for (Vertex* v = vertices; v != NULL; v = v->fNext) { |
| 1249 | if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| 1250 | continue; |
| 1251 | } |
| 1252 | #if LOGGING_ENABLED |
| 1253 | LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); |
| 1254 | #endif |
| 1255 | #ifdef SK_DEBUG |
| 1256 | validate_connectivity(v); |
| 1257 | #endif |
| 1258 | Edge* leftEnclosingEdge = NULL; |
| 1259 | Edge* rightEnclosingEdge = NULL; |
| 1260 | find_enclosing_edges(v, activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
| 1261 | SkASSERT(!leftEnclosingEdge || leftEnclosingEdge->isLeftOf(v)); |
| 1262 | SkASSERT(!rightEnclosingEdge || rightEnclosingEdge->isRightOf(v)); |
| 1263 | #ifdef SK_DEBUG |
| 1264 | validate_edges(activeEdges); |
| 1265 | #endif |
| 1266 | Poly* leftPoly = NULL; |
| 1267 | Poly* rightPoly = NULL; |
| 1268 | if (v->fFirstEdgeAbove) { |
| 1269 | leftPoly = v->fFirstEdgeAbove->fLeftPoly; |
| 1270 | rightPoly = v->fLastEdgeAbove->fRightPoly; |
| 1271 | } else { |
| 1272 | leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : NULL; |
| 1273 | rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : NULL; |
| 1274 | } |
| 1275 | #if LOGGING_ENABLED |
| 1276 | LOG("edges above:\n"); |
| 1277 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1278 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1279 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1280 | } |
| 1281 | LOG("edges below:\n"); |
| 1282 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1283 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1284 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1285 | } |
| 1286 | #endif |
| 1287 | if (v->fFirstEdgeAbove) { |
| 1288 | if (leftPoly) { |
| 1289 | leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc); |
| 1290 | } |
| 1291 | if (rightPoly) { |
| 1292 | rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc); |
| 1293 | } |
| 1294 | for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { |
| 1295 | Edge* leftEdge = e; |
| 1296 | Edge* rightEdge = e->fNextEdgeAbove; |
| 1297 | SkASSERT(rightEdge->isRightOf(leftEdge->fTop)); |
| 1298 | remove_edge(leftEdge, &activeEdges); |
| 1299 | if (leftEdge->fRightPoly) { |
| 1300 | leftEdge->fRightPoly->end(v, alloc); |
| 1301 | } |
| 1302 | if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != leftEdge->fRightPoly) { |
| 1303 | rightEdge->fLeftPoly->end(v, alloc); |
| 1304 | } |
| 1305 | } |
| 1306 | remove_edge(v->fLastEdgeAbove, &activeEdges); |
| 1307 | if (!v->fFirstEdgeBelow) { |
| 1308 | if (leftPoly && rightPoly && leftPoly != rightPoly) { |
| 1309 | SkASSERT(leftPoly->fPartner == NULL && rightPoly->fPartner == NULL); |
| 1310 | rightPoly->fPartner = leftPoly; |
| 1311 | leftPoly->fPartner = rightPoly; |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | if (v->fFirstEdgeBelow) { |
| 1316 | if (!v->fFirstEdgeAbove) { |
| 1317 | if (leftPoly && leftPoly == rightPoly) { |
| 1318 | // Split the poly. |
| 1319 | if (leftPoly->fActive->fSide == Poly::kLeft_Side) { |
| 1320 | leftPoly = new_poly(&polys, leftEnclosingEdge->fTop, leftPoly->fWinding, |
| 1321 | alloc); |
| 1322 | leftPoly->addVertex(v, Poly::kRight_Side, alloc); |
| 1323 | rightPoly->addVertex(v, Poly::kLeft_Side, alloc); |
| 1324 | leftEnclosingEdge->fRightPoly = leftPoly; |
| 1325 | } else { |
| 1326 | rightPoly = new_poly(&polys, rightEnclosingEdge->fTop, rightPoly->fWinding, |
| 1327 | alloc); |
| 1328 | rightPoly->addVertex(v, Poly::kLeft_Side, alloc); |
| 1329 | leftPoly->addVertex(v, Poly::kRight_Side, alloc); |
| 1330 | rightEnclosingEdge->fLeftPoly = rightPoly; |
| 1331 | } |
| 1332 | } else { |
| 1333 | if (leftPoly) { |
| 1334 | leftPoly = leftPoly->addVertex(v, Poly::kRight_Side, alloc); |
| 1335 | } |
| 1336 | if (rightPoly) { |
| 1337 | rightPoly = rightPoly->addVertex(v, Poly::kLeft_Side, alloc); |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | Edge* leftEdge = v->fFirstEdgeBelow; |
| 1342 | leftEdge->fLeftPoly = leftPoly; |
| 1343 | insert_edge(leftEdge, leftEnclosingEdge, &activeEdges); |
| 1344 | for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; |
| 1345 | rightEdge = rightEdge->fNextEdgeBelow) { |
| 1346 | insert_edge(rightEdge, leftEdge, &activeEdges); |
| 1347 | int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; |
| 1348 | winding += leftEdge->fWinding; |
| 1349 | if (winding != 0) { |
| 1350 | Poly* poly = new_poly(&polys, v, winding, alloc); |
| 1351 | leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; |
| 1352 | } |
| 1353 | leftEdge = rightEdge; |
| 1354 | } |
| 1355 | v->fLastEdgeBelow->fRightPoly = rightPoly; |
| 1356 | } |
| 1357 | #ifdef SK_DEBUG |
| 1358 | validate_edges(activeEdges); |
| 1359 | #endif |
| 1360 | #if LOGGING_ENABLED |
| 1361 | LOG("\nactive edges:\n"); |
| 1362 | for (Edge* e = activeEdges; e != NULL; e = e->fRight) { |
| 1363 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1364 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1365 | } |
| 1366 | #endif |
| 1367 | } |
| 1368 | return polys; |
| 1369 | } |
| 1370 | |
| 1371 | // This is a driver function which calls stages 2-5 in turn. |
| 1372 | |
| 1373 | Poly* contours_to_polys(Vertex** contours, int contourCnt, SkChunkAlloc& alloc) { |
| 1374 | #if LOGGING_ENABLED |
| 1375 | for (int i = 0; i < contourCnt; ++i) { |
| 1376 | Vertex* v = contours[i]; |
| 1377 | SkASSERT(v); |
| 1378 | LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
| 1379 | for (v = v->fNext; v != contours[i]; v = v->fNext) { |
| 1380 | LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
| 1381 | } |
| 1382 | } |
| 1383 | #endif |
| 1384 | sanitize_contours(contours, contourCnt); |
| 1385 | Vertex* vertices = build_edges(contours, contourCnt, alloc); |
| 1386 | if (!vertices) { |
| 1387 | return NULL; |
| 1388 | } |
| 1389 | |
| 1390 | // Sort vertices in Y (secondarily in X). |
| 1391 | merge_sort(&vertices); |
| 1392 | merge_coincident_vertices(&vertices, alloc); |
| 1393 | #if LOGGING_ENABLED |
| 1394 | for (Vertex* v = vertices; v != NULL; v = v->fNext) { |
| 1395 | static float gID = 0.0f; |
| 1396 | v->fID = gID++; |
| 1397 | } |
| 1398 | #endif |
| 1399 | simplify(vertices, alloc); |
| 1400 | return tessellate(vertices, alloc); |
| 1401 | } |
| 1402 | |
| 1403 | // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| 1404 | |
| 1405 | void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, void* data) { |
| 1406 | void* d = data; |
| 1407 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 1408 | if (apply_fill_type(fillType, poly->fWinding)) { |
| 1409 | d = poly->emit(d); |
| 1410 | } |
| 1411 | } |
| 1412 | return d; |
| 1413 | } |
| 1414 | |
| 1415 | }; |
| 1416 | |
| 1417 | GrTessellatingPathRenderer::GrTessellatingPathRenderer() { |
| 1418 | } |
| 1419 | |
| 1420 | GrPathRenderer::StencilSupport GrTessellatingPathRenderer::onGetStencilSupport( |
| 1421 | const GrDrawTarget*, |
| 1422 | const GrPipelineBuilder*, |
| 1423 | const SkPath&, |
| 1424 | const SkStrokeRec&) const { |
| 1425 | return GrPathRenderer::kNoSupport_StencilSupport; |
| 1426 | } |
| 1427 | |
| 1428 | bool GrTessellatingPathRenderer::canDrawPath(const GrDrawTarget* target, |
| 1429 | const GrPipelineBuilder* pipelineBuilder, |
| 1430 | const SkMatrix& viewMatrix, |
| 1431 | const SkPath& path, |
| 1432 | const SkStrokeRec& stroke, |
| 1433 | bool antiAlias) const { |
| 1434 | // This path renderer can draw all fill styles, but does not do antialiasing. It can do convex |
| 1435 | // and concave paths, but we'll leave the convex ones to simpler algorithms. |
| 1436 | return stroke.isFillStyle() && !antiAlias && !path.isConvex(); |
| 1437 | } |
| 1438 | |
| 1439 | bool GrTessellatingPathRenderer::onDrawPath(GrDrawTarget* target, |
| 1440 | GrPipelineBuilder* pipelineBuilder, |
| 1441 | GrColor color, |
| 1442 | const SkMatrix& viewM, |
| 1443 | const SkPath& path, |
| 1444 | const SkStrokeRec& stroke, |
| 1445 | bool antiAlias) { |
| 1446 | SkASSERT(!antiAlias); |
| 1447 | const GrRenderTarget* rt = pipelineBuilder->getRenderTarget(); |
| 1448 | if (NULL == rt) { |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | SkScalar tol = GrPathUtils::scaleToleranceToSrc(SK_Scalar1, viewM, path.getBounds()); |
| 1453 | |
| 1454 | int contourCnt; |
| 1455 | int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tol); |
| 1456 | if (maxPts <= 0) { |
| 1457 | return false; |
| 1458 | } |
| 1459 | if (maxPts > ((int)SK_MaxU16 + 1)) { |
| 1460 | SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); |
| 1461 | return false; |
| 1462 | } |
| 1463 | SkPath::FillType fillType = path.getFillType(); |
| 1464 | if (SkPath::IsInverseFillType(fillType)) { |
| 1465 | contourCnt++; |
| 1466 | } |
| 1467 | |
| 1468 | LOG("got %d pts, %d contours\n", maxPts, contourCnt); |
| 1469 | |
| 1470 | SkAutoTDeleteArray<Vertex*> contours(SkNEW_ARRAY(Vertex *, contourCnt)); |
| 1471 | |
| 1472 | // For the initial size of the chunk allocator, estimate based on the point count: |
| 1473 | // one vertex per point for the initial passes, plus two for the vertices in the |
| 1474 | // resulting Polys, since the same point may end up in two Polys. Assume minimal |
| 1475 | // connectivity of one Edge per Vertex (will grow for intersections). |
| 1476 | SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge))); |
| 1477 | SkIRect clipBoundsI; |
| 1478 | pipelineBuilder->clip().getConservativeBounds(rt, &clipBoundsI); |
| 1479 | SkRect clipBounds = SkRect::Make(clipBoundsI); |
| 1480 | SkMatrix vmi; |
| 1481 | if (!viewM.invert(&vmi)) { |
| 1482 | return false; |
| 1483 | } |
| 1484 | vmi.mapRect(&clipBounds); |
| 1485 | path_to_contours(path, tol, clipBounds, contours.get(), alloc); |
| 1486 | Poly* polys; |
| 1487 | uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType; |
| 1488 | polys = contours_to_polys(contours.get(), contourCnt, alloc); |
| 1489 | SkAutoTUnref<const GrGeometryProcessor> gp( |
| 1490 | GrDefaultGeoProcFactory::Create(flags, color, viewM, SkMatrix::I())); |
| 1491 | int count = 0; |
| 1492 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 1493 | if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { |
| 1494 | count += (poly->fCount - 2) * (WIREFRAME ? 6 : 3); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Stephen White | 68ccca1 | 2015-02-26 10:11:20 -0500 | [diff] [blame] | 1498 | size_t stride = gp->getVertexStride(); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 1499 | GrDrawTarget::AutoReleaseGeometry arg; |
| 1500 | if (!arg.set(target, count, stride, 0)) { |
| 1501 | return false; |
| 1502 | } |
| 1503 | LOG("emitting %d verts\n", count); |
| 1504 | void* end = polys_to_triangles(polys, fillType, arg.vertices()); |
Stephen White | 68ccca1 | 2015-02-26 10:11:20 -0500 | [diff] [blame] | 1505 | int actualCount = static_cast<int>((static_cast<char*>(end) - static_cast<char*>(arg.vertices())) / stride); |
senorblanco | d6ed19c | 2015-02-26 06:58:17 -0800 | [diff] [blame] | 1506 | LOG("actual count: %d\n", actualCount); |
| 1507 | SkASSERT(actualCount <= count); |
| 1508 | |
| 1509 | GrPrimitiveType primitiveType = WIREFRAME ? kLines_GrPrimitiveType |
| 1510 | : kTriangles_GrPrimitiveType; |
| 1511 | target->drawNonIndexed(pipelineBuilder, gp, primitiveType, 0, actualCount); |
| 1512 | |
| 1513 | return true; |
| 1514 | } |