blob: 4d240d050d07ee3d67655c4666f19a679beef6f6 [file] [log] [blame]
Chris Dalton47114db2021-01-06 00:35:20 -07001/*
2 * Copyright 2020 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#ifndef GrAATriangulator_DEFINED
9#define GrAATriangulator_DEFINED
10
11#include "src/gpu/GrTriangulator.h"
12
13// Triangulates the given path in device space with a mesh of alpha ramps for antialiasing.
Chris Daltond5384792021-01-20 15:43:24 -070014class GrAATriangulator : private GrTriangulator {
Chris Dalton47114db2021-01-06 00:35:20 -070015public:
Chris Daltond5384792021-01-20 15:43:24 -070016 static int PathToAATriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
17 GrEagerVertexAllocator* vertexAllocator) {
Chris Dalton47114db2021-01-06 00:35:20 -070018 GrAATriangulator aaTriangulator(path);
19 aaTriangulator.fRoundVerticesToQuarterPixel = true;
20 aaTriangulator.fEmitCoverage = true;
Chris Daltond5384792021-01-20 15:43:24 -070021 Poly* polys = aaTriangulator.pathToPolys(tolerance, clipBounds);
22 return aaTriangulator.polysToAATriangles(polys, vertexAllocator);
Chris Dalton47114db2021-01-06 00:35:20 -070023 }
24
25 // Structs used by GrAATriangulator internals.
26 struct SSEdge;
27 struct EventList;
28 struct Event {
29 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
30 : fEdge(edge), fPoint(point), fAlpha(alpha) {}
31 SSEdge* fEdge;
32 SkPoint fPoint;
33 uint8_t fAlpha;
34 void apply(VertexList* mesh, const Comparator&, EventList* events, GrAATriangulator*);
35 };
36 struct EventComparator {
37 enum class Op { kLessThan, kGreaterThan };
38 EventComparator(Op op) : fOp(op) {}
39 bool operator() (Event* const &e1, Event* const &e2) {
40 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
41 : e1->fAlpha > e2->fAlpha;
42 }
43 Op fOp;
44 };
45
46private:
47 GrAATriangulator(const SkPath& path) : GrTriangulator(path) {}
48
49 // For screenspace antialiasing, the algorithm is modified as follows:
50 //
51 // Run steps 1-5 above to produce polygons.
52 // 5b) Apply fill rules to extract boundary contours from the polygons:
53 void extractBoundary(EdgeList* boundary, Edge* e);
54 void extractBoundaries(const VertexList& inMesh, VertexList* innerVertices, const Comparator&);
55
56 // 5c) Simplify boundaries to remove "pointy" vertices that cause inversions:
57 void simplifyBoundary(EdgeList* boundary, const Comparator&);
58
59 // 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
60 // new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
61 // new antialiased mesh from those vertices:
62 void strokeBoundary(EdgeList* boundary, VertexList* innerMesh, const Comparator&);
63
64 // Run steps 3-6 above on the new mesh, and produce antialiased triangles.
65 Poly* tessellate(const VertexList& mesh, const Comparator&) override;
Chris Daltond5384792021-01-20 15:43:24 -070066 int polysToAATriangles(Poly*, GrEagerVertexAllocator*);
Chris Dalton47114db2021-01-06 00:35:20 -070067
68 // Additional helpers and driver functions.
69 void makeEvent(SSEdge*, EventList* events);
70 void makeEvent(SSEdge*, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
71 const Comparator&);
72 void connectPartners(VertexList* mesh, const Comparator&);
73 void removeNonBoundaryEdges(const VertexList& mesh);
74 void connectSSEdge(Vertex* v, Vertex* dest, const Comparator&);
75 bool collapseOverlapRegions(VertexList* mesh, const Comparator&, EventComparator comp);
76
77 VertexList fOuterMesh;
78};
79
80#endif