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