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