• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 GrGrCCFillGeometry_DEFINED
9 #define GrGrCCFillGeometry_DEFINED
10 
11 #include "include/core/SkPoint.h"
12 #include "include/private/SkNx.h"
13 #include "include/private/SkTArray.h"
14 #include "src/core/SkGeometry.h"
15 
16 /**
17  * This class chops device-space contours up into a series of segments that CCPR knows how to
18  * fill. (See GrCCFillGeometry::Verb.)
19  *
20  * NOTE: This must be done in device space, since an affine transformation can change whether a
21  * curve is monotonic.
22  */
23 class GrCCFillGeometry {
24 public:
25     // These are the verbs that CCPR knows how to fill. If a path has any segments that don't map to
26     // this list, then they are chopped into smaller ones that do. A list of these comprise a
27     // compact representation of what can later be expanded into GPU instance data.
28     enum class Verb : uint8_t {
29         kBeginPath, // Included only for caller convenience.
30         kBeginContour,
31         kLineTo,
32         kMonotonicQuadraticTo, // Monotonic relative to the vector between its endpoints [P2 - P0].
33         kMonotonicCubicTo,
34         kMonotonicConicTo,
35         kEndClosedContour, // endPt == startPt.
36         kEndOpenContour // endPt != startPt.
37     };
38 
39     // These tallies track numbers of CCPR primitives that are required to draw a contour.
40     struct PrimitiveTallies {
41         int fTriangles; // Number of triangles in the contour's fan.
42         int fWeightedTriangles; // Triangles (from the tessellator) whose winding magnitude > 1.
43         int fQuadratics;
44         int fCubics;
45         int fConics;
46 
47         void operator+=(const PrimitiveTallies&);
48         PrimitiveTallies operator-(const PrimitiveTallies&) const;
49         bool operator==(const PrimitiveTallies&);
50     };
51 
52     GrCCFillGeometry(int numSkPoints = 0, int numSkVerbs = 0, int numConicWeights = 0)
53             : fPoints(numSkPoints * 3) // Reserve for a 3x expansion in points and verbs.
54             , fVerbs(numSkVerbs * 3)
55             , fConicWeights(numConicWeights * 3/2) {}
56 
points()57     const SkTArray<SkPoint, true>& points() const { SkASSERT(!fBuildingContour); return fPoints; }
verbs()58     const SkTArray<Verb, true>& verbs() const { SkASSERT(!fBuildingContour); return fVerbs; }
getConicWeight(int idx)59     float getConicWeight(int idx) const { SkASSERT(!fBuildingContour); return fConicWeights[idx]; }
60 
reset()61     void reset() {
62         SkASSERT(!fBuildingContour);
63         fPoints.reset();
64         fVerbs.reset();
65     }
66 
67     void beginPath();
68     void beginContour(const SkPoint&);
69     void lineTo(const SkPoint P[2]);
70     void quadraticTo(const SkPoint[3]);
71 
72     // We pass through inflection points and loop intersections using a line and quadratic(s)
73     // respectively. 'inflectPad' and 'loopIntersectPad' specify how close (in pixels) cubic
74     // segments are allowed to get to these points. For normal rendering you will want to use the
75     // default values, but these can be overridden for testing purposes.
76     //
77     // NOTE: loops do appear to require two full pixels of padding around the intersection point.
78     //       With just one pixel-width of pad, we start to see bad pixels. Ultimately this has a
79     //       minimal effect on the total amount of segments produced. Most sections that pass
80     //       through the loop intersection can be approximated with a single quadratic anyway,
81     //       regardless of whether we are use one pixel of pad or two (1.622 avg. quads per loop
82     //       intersection vs. 1.489 on the tiger).
83     void cubicTo(const SkPoint[4], float inflectPad = 0.55f, float loopIntersectPad = 2);
84 
85     void conicTo(const SkPoint[3], float w);
86 
87     PrimitiveTallies endContour(); // Returns the numbers of primitives needed to draw the contour.
88 
89 private:
90     inline void appendLine(const Sk2f& p0, const Sk2f& p1);
91 
92     inline void appendQuadratics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2);
93     inline void appendMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2);
94 
95     enum class AppendCubicMode : bool {
96         kLiteral,
97         kApproximate
98     };
99     void appendCubics(AppendCubicMode, const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
100                       const Sk2f& p3, const float chops[], int numChops, float localT0 = 0,
101                      float localT1 = 1);
102     void appendCubics(AppendCubicMode, const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
103                       const Sk2f& p3, int maxSubdivisions = 2);
104     void chopAndAppendCubicAtMidTangent(AppendCubicMode, const Sk2f& p0, const Sk2f& p1,
105                                         const Sk2f& p2, const Sk2f& p3, const Sk2f& tan0,
106                                         const Sk2f& tan1, int maxFutureSubdivisions);
107 
108     void appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w);
109 
110     // Transient state used while building a contour.
111     SkPoint fCurrAnchorPoint;
112     PrimitiveTallies fCurrContourTallies;
113     SkCubicType fCurrCubicType;
114     SkDEBUGCODE(bool fBuildingContour = false);
115 
116     SkSTArray<128, SkPoint, true> fPoints;
117     SkSTArray<128, Verb, true> fVerbs;
118     SkSTArray<32, float, true> fConicWeights;
119 };
120 
121 inline void GrCCFillGeometry::PrimitiveTallies::operator+=(const PrimitiveTallies& b) {
122     fTriangles += b.fTriangles;
123     fWeightedTriangles += b.fWeightedTriangles;
124     fQuadratics += b.fQuadratics;
125     fCubics += b.fCubics;
126     fConics += b.fConics;
127 }
128 
129 GrCCFillGeometry::PrimitiveTallies
130 inline GrCCFillGeometry::PrimitiveTallies::operator-(const PrimitiveTallies& b) const {
131     return {fTriangles - b.fTriangles,
132             fWeightedTriangles - b.fWeightedTriangles,
133             fQuadratics - b.fQuadratics,
134             fCubics - b.fCubics,
135             fConics - b.fConics};
136 }
137 
138 inline bool GrCCFillGeometry::PrimitiveTallies::operator==(const PrimitiveTallies& b) {
139     return fTriangles == b.fTriangles && fWeightedTriangles == b.fWeightedTriangles &&
140            fQuadratics == b.fQuadratics && fCubics == b.fCubics && fConics == b.fConics;
141 }
142 
143 #endif
144