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