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