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