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