blob: aadc5724570dec4a6eb4fc5f3f76996e69ad2c80 [file] [log] [blame]
ethannicholase9709e82016-01-07 13:34:16 -08001/*
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
Chris Dalton17dc4182020-03-25 16:18:16 -06008#ifndef GrTriangulator_DEFINED
9#define GrTriangulator_DEFINED
ethannicholase9709e82016-01-07 13:34:16 -080010
Chris Dalton57ea1fc2021-01-05 13:37:44 -070011#include "include/core/SkPath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkColorData.h"
Chris Dalton57ea1fc2021-01-05 13:37:44 -070014#include "src/core/SkArenaAlloc.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrColor.h"
senorblanco6599eff2016-03-10 08:38:45 -080016
Chris Daltond081dce2020-01-23 12:09:04 -070017class GrEagerVertexAllocator;
senorblanco6599eff2016-03-10 08:38:45 -080018struct SkRect;
ethannicholase9709e82016-01-07 13:34:16 -080019
Chris Dalton5045de32021-01-07 19:09:01 -070020#define TRIANGULATOR_LOGGING 0
Chris Dalton57ea1fc2021-01-05 13:37:44 -070021#define TRIANGULATOR_WIREFRAME 0
22
ethannicholase9709e82016-01-07 13:34:16 -080023/**
24 * Provides utility functions for converting paths to a collection of triangles.
25 */
Chris Dalton57ea1fc2021-01-05 13:37:44 -070026class GrTriangulator {
27public:
Chris Dalton854ee852021-01-05 15:12:59 -070028 static int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
29 GrEagerVertexAllocator* vertexAllocator, bool* isLinear) {
30 GrTriangulator triangulator(path);
31 int count = triangulator.pathToTriangles(tolerance, clipBounds, vertexAllocator,
32 path.getFillType());
33 *isLinear = triangulator.fIsLinear;
34 return count;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070035 }
ethannicholase9709e82016-01-07 13:34:16 -080036
Chris Dalton854ee852021-01-05 15:12:59 -070037 static int TriangulateSimpleInnerPolygons(const SkPath& path,
38 GrEagerVertexAllocator* vertexAllocator,
39 bool *isLinear) {
40 GrTriangulator triangulator(path);
41 triangulator.fCullCollinearVertices = false;
42 triangulator.fSimpleInnerPolygons = true;
43 int count = triangulator.pathToTriangles(0, SkRect::MakeEmpty(), vertexAllocator,
44 path.getFillType());
45 *isLinear = triangulator.fIsLinear;
46 return count;
47 }
ethannicholase9709e82016-01-07 13:34:16 -080048
Chris Dalton57ea1fc2021-01-05 13:37:44 -070049 struct WindingVertex {
50 SkPoint fPos;
51 int fWinding;
52 };
Chris Dalton6ccc0322020-01-29 11:38:16 -070053
Chris Dalton57ea1fc2021-01-05 13:37:44 -070054 // *DEPRECATED*: Once CCPR is removed this method will go away.
Chris Dalton6ccc0322020-01-29 11:38:16 -070055 //
Chris Dalton57ea1fc2021-01-05 13:37:44 -070056 // Triangulates a path to an array of vertices. Each triangle is represented as a set of three
57 // WindingVertex entries, each of which contains the position and winding count (which is the
58 // same for all three vertices of a triangle). The 'verts' out parameter is set to point to the
59 // resultant vertex array. CALLER IS RESPONSIBLE for deleting this buffer to avoid a memory
60 // leak!
61 static int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
62 WindingVertex** verts);
63
Chris Dalton17ce8c52021-01-07 18:08:46 -070064 // Enums used by GrTriangulator internals.
65 typedef enum { kLeft_Side, kRight_Side } Side;
66 enum class EdgeType { kInner, kOuter, kConnector };
67
Chris Dalton57ea1fc2021-01-05 13:37:44 -070068 // Structs used by GrTriangulator internals.
69 struct Vertex;
70 struct VertexList;
Chris Dalton17ce8c52021-01-07 18:08:46 -070071 struct Line;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070072 struct Edge;
73 struct EdgeList;
Chris Dalton17ce8c52021-01-07 18:08:46 -070074 struct MonotonePoly;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070075 struct Poly;
76 struct Comparator;
77
Chris Dalton7cf3add2021-01-11 18:33:28 -070078protected:
Chris Dalton854ee852021-01-05 15:12:59 -070079 GrTriangulator(const SkPath& path) : fPath(path) {}
Chris Dalton7cf3add2021-01-11 18:33:28 -070080 virtual ~GrTriangulator() {}
Chris Dalton57ea1fc2021-01-05 13:37:44 -070081
82 // There are six stages to the basic algorithm:
83 //
84 // 1) Linearize the path contours into piecewise linear segments:
85 void pathToContours(float tolerance, const SkRect& clipBounds, VertexList* contours);
86
87 // 2) Build a mesh of edges connecting the vertices:
Chris Dalton811dc6a2021-01-07 16:40:32 -070088 void contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -070089
90 // 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
91 static void SortMesh(VertexList* vertices, const Comparator&);
92
93 // 4) Simplify the mesh by inserting new vertices at intersecting edges:
94 enum class SimplifyResult {
95 kAlreadySimple,
96 kFoundSelfIntersection,
97 kAbort
98 };
99
Chris Dalton811dc6a2021-01-07 16:40:32 -0700100 SimplifyResult simplify(VertexList* mesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700101
102 // 5) Tessellate the simplified mesh into monotone polygons:
Chris Dalton7cf3add2021-01-11 18:33:28 -0700103 virtual Poly* tessellate(const VertexList& vertices, VertexList* outerMesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700104
105 // 6) Triangulate the monotone polygons directly into a vertex buffer:
106 void* polysToTriangles(Poly* polys, void* data, SkPathFillType overrideFillType);
107
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700108 // The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
109 // of vertices (and the necessity of inserting new vertices on intersection).
110 //
111 // Stages (4) and (5) use an active edge list -- a list of all edges for which the
112 // sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
113 // left-to-right based on the point where both edges are active (when both top vertices
114 // have been seen, so the "lower" top vertex of the two). If the top vertices are equal
115 // (shared), it's sorted based on the last point where both edges are active, so the
116 // "upper" bottom vertex.
117 //
118 // The most complex step is the simplification (4). It's based on the Bentley-Ottman
119 // line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
120 // not exact and may violate the mesh topology or active edge list ordering. We
121 // accommodate this by adjusting the topology of the mesh and AEL to match the intersection
122 // points. This occurs in two ways:
123 //
124 // A) Intersections may cause a shortened edge to no longer be ordered with respect to its
125 // neighbouring edges at the top or bottom vertex. This is handled by merging the
126 // edges (merge_collinear_edges()).
127 // B) Intersections may cause an edge to violate the left-to-right ordering of the
128 // active edge list. This is handled by detecting potential violations and rewinding
129 // the active edge list to the vertex before they occur (rewind() during merging,
130 // rewind_if_necessary() during splitting).
131 //
132 // The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
133 // Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
134 // currently uses a linked list for the active edge list, rather than a 2-3 tree as the
135 // paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
136 // become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
137 // insertions and removals was greater than the cost of infrequent O(N) lookups with the
138 // linked list implementation. With the latter, all removals are O(1), and most insertions
139 // are O(1), since we know the adjacent edge in the active edge list based on the topology.
140 // Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
141 // frequent. There may be other data structures worth investigating, however.
142 //
143 // Note that the orientation of the line sweep algorithms is determined by the aspect ratio of
144 // the path bounds. When the path is taller than it is wide, we sort vertices based on
145 // increasing Y coordinate, and secondarily by increasing X coordinate. When the path is wider
146 // than it is tall, we sort by increasing X coordinate, but secondarily by *decreasing* Y
147 // coordinate. This is so that the "left" and "right" orientation in the code remains correct
148 // (edges to the left are increasing in Y; edges to the right are decreasing in Y). That is, the
149 // setting rotates 90 degrees counterclockwise, rather that transposing.
150
151 // Additional helpers and driver functions.
Chris Dalton9a4904f2021-01-07 19:10:14 -0700152 void* emitMonotonePoly(const MonotonePoly*, void* data);
153 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, int winding, void* data) const;
154 void* emitPoly(const Poly*, void *data);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700155 Poly* makePoly(Poly** head, Vertex* v, int winding);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700156 void appendPointToContour(const SkPoint& p, VertexList* contour);
157 void appendQuadraticToContour(const SkPoint[3], SkScalar toleranceSqd, VertexList* contour);
158 void generateCubicPoints(const SkPoint&, const SkPoint&, const SkPoint&, const SkPoint&,
159 SkScalar tolSqd, VertexList* contour, int pointsLeft);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700160 Edge* makeEdge(Vertex* prev, Vertex* next, EdgeType type, const Comparator&);
161 Edge* makeConnectingEdge(Vertex* prev, Vertex* next, EdgeType, const Comparator&,
162 int windingScale = 1);
Chris Dalton811dc6a2021-01-07 16:40:32 -0700163 bool splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
164 const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700165 bool intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700166 const Comparator&);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700167 Vertex* makeSortedVertex(const SkPoint&, uint8_t alpha, VertexList* mesh, Vertex* reference,
168 const Comparator&);
169 void computeBisector(Edge* edge1, Edge* edge2, Vertex*);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700170 bool checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700171 VertexList* mesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700172 void sanitizeContours(VertexList* contours, int contourCnt);
Chris Dalton811dc6a2021-01-07 16:40:32 -0700173 bool mergeCoincidentVertices(VertexList* mesh, const Comparator&);
174 void buildEdges(VertexList* contours, int contourCnt, VertexList* mesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700175 Poly* contoursToPolys(VertexList* contours, int contourCnt, VertexList* outerMesh);
176 Poly* pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt,
177 VertexList* outerMesh);
178 int pathToTriangles(float tolerance, const SkRect& clipBounds, GrEagerVertexAllocator*,
179 SkPathFillType overrideFillType);
180
181 constexpr static int kArenaChunkSize = 16 * 1024;
182 SkArenaAlloc fAlloc{kArenaChunkSize};
183 const SkPath fPath;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700184 bool fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700185
186 // Flags.
187 bool fRoundVerticesToQuarterPixel = false;
188 bool fEmitCoverage = false;
189 bool fCullCollinearVertices = true;
190 bool fSimpleInnerPolygons = false;
Chris Daltondcc8c542020-01-28 17:55:56 -0700191};
192
Chris Dalton5045de32021-01-07 19:09:01 -0700193/**
194 * Vertices are used in three ways: first, the path contours are converted into a
195 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
196 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
197 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
198 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
199 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
200 * an individual Vertex from the path mesh may belong to multiple
201 * MonotonePolys, so the original Vertices cannot be re-used.
202 */
203
204struct GrTriangulator::Vertex {
205 Vertex(const SkPoint& point, uint8_t alpha)
206 : fPoint(point), fPrev(nullptr), fNext(nullptr)
207 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
208 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
209 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
210 , fPartner(nullptr)
211 , fAlpha(alpha)
212 , fSynthetic(false)
213#if TRIANGULATOR_LOGGING
214 , fID (-1.0f)
215#endif
216 {}
217 SkPoint fPoint; // Vertex position
218 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
219 Vertex* fNext; // "
220 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
221 Edge* fLastEdgeAbove; // "
222 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
223 Edge* fLastEdgeBelow; // "
224 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
225 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
226 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
227 uint8_t fAlpha;
228 bool fSynthetic; // Is this a synthetic vertex?
229#if TRIANGULATOR_LOGGING
230 float fID; // Identifier used for logging.
231#endif
232};
233
234struct GrTriangulator::VertexList {
235 VertexList() : fHead(nullptr), fTail(nullptr) {}
236 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
237 Vertex* fHead;
238 Vertex* fTail;
239 void insert(Vertex* v, Vertex* prev, Vertex* next);
240 void append(Vertex* v) { insert(v, fTail, nullptr); }
241 void append(const VertexList& list) {
242 if (!list.fHead) {
243 return;
244 }
245 if (fTail) {
246 fTail->fNext = list.fHead;
247 list.fHead->fPrev = fTail;
248 } else {
249 fHead = list.fHead;
250 }
251 fTail = list.fTail;
252 }
253 void prepend(Vertex* v) { insert(v, nullptr, fHead); }
254 void remove(Vertex* v);
255 void close() {
256 if (fHead && fTail) {
257 fTail->fNext = fHead;
258 fHead->fPrev = fTail;
259 }
260 }
261};
262
263// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
264struct GrTriangulator::Line {
265 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
266 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
267 Line(const SkPoint& p, const SkPoint& q)
268 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
269 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
270 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
271 static_cast<double>(p.fX) * q.fY) {}
272 double dist(const SkPoint& p) const { return fA * p.fX + fB * p.fY + fC; }
273 Line operator*(double v) const { return Line(fA * v, fB * v, fC * v); }
274 double magSq() const { return fA * fA + fB * fB; }
275 void normalize() {
276 double len = sqrt(this->magSq());
277 if (len == 0.0) {
278 return;
279 }
280 double scale = 1.0f / len;
281 fA *= scale;
282 fB *= scale;
283 fC *= scale;
284 }
285 bool nearParallel(const Line& o) const {
286 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
287 }
288
289 // Compute the intersection of two (infinite) Lines.
290 bool intersect(const Line& other, SkPoint* point) const;
291 double fA, fB, fC;
292};
293
294/**
295 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
296 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
297 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
298 * point). For speed, that case is only tested by the callers that require it (e.g.,
299 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
300 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
301 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
302 * a lot faster in the "not found" case.
303 *
304 * The coefficients of the line equation stored in double precision to avoid catastrophic
305 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
306 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
307 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
308 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
309 * this file).
310 */
311
312struct GrTriangulator::Edge {
313 Edge(Vertex* top, Vertex* bottom, int winding, EdgeType type)
314 : fWinding(winding)
315 , fTop(top)
316 , fBottom(bottom)
317 , fType(type)
318 , fLeft(nullptr)
319 , fRight(nullptr)
320 , fPrevEdgeAbove(nullptr)
321 , fNextEdgeAbove(nullptr)
322 , fPrevEdgeBelow(nullptr)
323 , fNextEdgeBelow(nullptr)
324 , fLeftPoly(nullptr)
325 , fRightPoly(nullptr)
326 , fLeftPolyPrev(nullptr)
327 , fLeftPolyNext(nullptr)
328 , fRightPolyPrev(nullptr)
329 , fRightPolyNext(nullptr)
330 , fUsedInLeftPoly(false)
331 , fUsedInRightPoly(false)
332 , fLine(top, bottom) {
333 }
334 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
335 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
336 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
337 EdgeType fType;
338 Edge* fLeft; // The linked list of edges in the active edge list.
339 Edge* fRight; // "
340 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
341 Edge* fNextEdgeAbove; // "
342 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
343 Edge* fNextEdgeBelow; // "
344 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
345 Poly* fRightPoly; // The Poly to the right of this edge, if any.
346 Edge* fLeftPolyPrev;
347 Edge* fLeftPolyNext;
348 Edge* fRightPolyPrev;
349 Edge* fRightPolyNext;
350 bool fUsedInLeftPoly;
351 bool fUsedInRightPoly;
352 Line fLine;
353 double dist(const SkPoint& p) const { return fLine.dist(p); }
354 bool isRightOf(Vertex* v) const { return fLine.dist(v->fPoint) < 0.0; }
355 bool isLeftOf(Vertex* v) const { return fLine.dist(v->fPoint) > 0.0; }
356 void recompute() { fLine = Line(fTop, fBottom); }
357 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const;
358};
359
360struct GrTriangulator::EdgeList {
361 EdgeList() : fHead(nullptr), fTail(nullptr) {}
362 Edge* fHead;
363 Edge* fTail;
364 void insert(Edge* edge, Edge* prev, Edge* next);
365 void append(Edge* e) { insert(e, fTail, nullptr); }
366 void remove(Edge* edge);
367 void removeAll() {
368 while (fHead) {
369 this->remove(fHead);
370 }
371 }
372 void close() {
373 if (fHead && fTail) {
374 fTail->fRight = fHead;
375 fHead->fLeft = fTail;
376 }
377 }
378 bool contains(Edge* edge) const { return edge->fLeft || edge->fRight || fHead == edge; }
379};
380
381struct GrTriangulator::MonotonePoly {
382 MonotonePoly(Edge* edge, Side side, int winding)
383 : fSide(side)
384 , fFirstEdge(nullptr)
385 , fLastEdge(nullptr)
386 , fPrev(nullptr)
387 , fNext(nullptr)
388 , fWinding(winding) {
389 this->addEdge(edge);
390 }
391 Side fSide;
392 Edge* fFirstEdge;
393 Edge* fLastEdge;
394 MonotonePoly* fPrev;
395 MonotonePoly* fNext;
396 int fWinding;
397 void addEdge(Edge*);
398 void* emit(bool emitCoverage, void* data);
399 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, bool emitCoverage,
400 void* data) const;
401};
402
403struct GrTriangulator::Poly {
404 Poly(Vertex* v, int winding)
405 : fFirstVertex(v)
406 , fWinding(winding)
407 , fHead(nullptr)
408 , fTail(nullptr)
409 , fNext(nullptr)
410 , fPartner(nullptr)
411 , fCount(0)
412 {
413#if TRIANGULATOR_LOGGING
414 static int gID = 0;
415 fID = gID++;
416 TESS_LOG("*** created Poly %d\n", fID);
417#endif
418 }
419 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc);
420 void* emit(bool emitCoverage, void *data);
421 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
422 Vertex* fFirstVertex;
423 int fWinding;
424 MonotonePoly* fHead;
425 MonotonePoly* fTail;
426 Poly* fNext;
427 Poly* fPartner;
428 int fCount;
429#if TRIANGULATOR_LOGGING
430 int fID;
431#endif
432};
433
434struct GrTriangulator::Comparator {
435 enum class Direction { kVertical, kHorizontal };
436 Comparator(Direction direction) : fDirection(direction) {}
437 bool sweep_lt(const SkPoint& a, const SkPoint& b) const;
438 Direction fDirection;
439};
440
Chris Dalton7cf3add2021-01-11 18:33:28 -0700441// Triangulates the given path in device space with a mesh of alpha ramps for antialiasing.
442class GrAATriangulator : public GrTriangulator {
443public:
444 static int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
445 GrEagerVertexAllocator* vertexAllocator) {
446 GrAATriangulator aaTriangulator(path);
447 aaTriangulator.fRoundVerticesToQuarterPixel = true;
448 aaTriangulator.fEmitCoverage = true;
449 return aaTriangulator.pathToTriangles(tolerance, clipBounds, vertexAllocator,
450 SkPathFillType::kWinding);
451 }
452
453 // Structs used by GrAATriangulator internals.
454 struct SSEdge;
455 struct EventList;
456 struct Event {
457 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
458 : fEdge(edge), fPoint(point), fAlpha(alpha) {}
459 SSEdge* fEdge;
460 SkPoint fPoint;
461 uint8_t fAlpha;
462 void apply(VertexList* mesh, const Comparator&, EventList* events, GrAATriangulator*);
463 };
464 struct EventComparator {
465 enum class Op { kLessThan, kGreaterThan };
466 EventComparator(Op op) : fOp(op) {}
467 bool operator() (Event* const &e1, Event* const &e2) {
468 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
469 : e1->fAlpha > e2->fAlpha;
470 }
471 Op fOp;
472 };
473
474private:
475 GrAATriangulator(const SkPath& path) : GrTriangulator(path) {}
476
477 // For screenspace antialiasing, the algorithm is modified as follows:
478 //
479 // Run steps 1-5 above to produce polygons.
480 // 5b) Apply fill rules to extract boundary contours from the polygons:
481 void extractBoundary(EdgeList* boundary, Edge* e);
482 void extractBoundaries(const VertexList& inMesh, VertexList* innerVertices,
483 VertexList* outerMesh, const Comparator&);
484
485 // 5c) Simplify boundaries to remove "pointy" vertices that cause inversions:
486 void simplifyBoundary(EdgeList* boundary, const Comparator&);
487
488 // 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
489 // new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
490 // new antialiased mesh from those vertices:
491 void strokeBoundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
492 const Comparator&);
493
494 // Run steps 3-6 above on the new mesh, and produce antialiased triangles.
495 Poly* tessellate(const VertexList& mesh, VertexList* outerMesh, const Comparator&) override;
496
497 // Additional helpers and driver functions.
498 void makeEvent(SSEdge*, EventList* events);
499 void makeEvent(SSEdge*, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
500 const Comparator&);
501 void connectPartners(VertexList* mesh, const Comparator&);
502 void removeNonBoundaryEdges(const VertexList& mesh);
503 void connectSSEdge(Vertex* v, Vertex* dest, const Comparator&);
504 bool collapseOverlapRegions(VertexList* mesh, const Comparator&, EventComparator comp);
505};
506
ethannicholase9709e82016-01-07 13:34:16 -0800507#endif