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 GrGrCCGeometry_DEFINED
9 #define GrGrCCGeometry_DEFINED
10 
11 #include "SkGeometry.h"
12 #include "SkNx.h"
13 #include "SkPoint.h"
14 #include "SkTArray.h"
15 
16 /**
17  * This class chops device-space contours up into a series of segments that CCPR knows how to
18  * render. (See GrCCGeometry::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 GrCCGeometry {
24 public:
25     // These are the verbs that CCPR knows how to draw. 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         kEndClosedContour, // endPt == startPt.
35         kEndOpenContour // endPt != startPt.
36     };
37 
38     // These tallies track numbers of CCPR primitives are required to draw a contour.
39     struct PrimitiveTallies {
40         int fTriangles; // Number of triangles in the contour's fan.
41         int fQuadratics;
42         int fCubics;
43 
44         void operator+=(const PrimitiveTallies&);
45         PrimitiveTallies operator-(const PrimitiveTallies&) const;
46         bool operator==(const PrimitiveTallies&);
47     };
48 
49     GrCCGeometry(int numSkPoints = 0, int numSkVerbs = 0)
50             : fPoints(numSkPoints * 3) // Reserve for a 3x expansion in points and verbs.
51             , fVerbs(numSkVerbs * 3) {}
52 
points()53     const SkTArray<SkPoint, true>& points() const { SkASSERT(!fBuildingContour); return fPoints; }
verbs()54     const SkTArray<Verb, true>& verbs() const { SkASSERT(!fBuildingContour); return fVerbs; }
55 
reset()56     void reset() {
57         SkASSERT(!fBuildingContour);
58         fPoints.reset();
59         fVerbs.reset();
60     }
61 
62     // This is included in case the caller needs to discard previously added contours. It is up to
63     // the caller to track counts and ensure we don't pop back into the middle of a different
64     // contour.
resize_back(int numPoints,int numVerbs)65     void resize_back(int numPoints, int numVerbs) {
66         SkASSERT(!fBuildingContour);
67         fPoints.resize_back(numPoints);
68         fVerbs.resize_back(numVerbs);
69         SkASSERT(fVerbs.empty() || fVerbs.back() == Verb::kEndOpenContour ||
70                  fVerbs.back() == Verb::kEndClosedContour);
71     }
72 
73     void beginPath();
74     void beginContour(const SkPoint& devPt);
75     void lineTo(const SkPoint& devPt);
76     void quadraticTo(const SkPoint& devP1, const SkPoint& devP2);
77 
78     // We pass through inflection points and loop intersections using a line and quadratic(s)
79     // respectively. 'inflectPad' and 'loopIntersectPad' specify how close (in pixels) cubic
80     // segments are allowed to get to these points. For normal rendering you will want to use the
81     // default values, but these can be overridden for testing purposes.
82     //
83     // NOTE: loops do appear to require two full pixels of padding around the intersection point.
84     //       With just one pixel-width of pad, we start to see bad pixels. Ultimately this has a
85     //       minimal effect on the total amount of segments produced. Most sections that pass
86     //       through the loop intersection can be approximated with a single quadratic anyway,
87     //       regardless of whether we are use one pixel of pad or two (1.622 avg. quads per loop
88     //       intersection vs. 1.489 on the tiger).
89     void cubicTo(const SkPoint& devP1, const SkPoint& devP2, const SkPoint& devP3,
90                  float inflectPad = 0.55f, float loopIntersectPad = 2);
91 
92     PrimitiveTallies endContour(); // Returns the numbers of primitives needed to draw the contour.
93 
94 private:
95     inline void appendMonotonicQuadratics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2);
96     inline void appendSingleMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2);
97 
98     using AppendCubicFn = void(GrCCGeometry::*)(const Sk2f& p0, const Sk2f& p1,
99                                                 const Sk2f& p2, const Sk2f& p3,
100                                                 int maxSubdivisions);
101     static constexpr int kMaxSubdivionsPerCubicSection = 2;
102 
103     template<AppendCubicFn AppendLeftRight>
104     inline void chopCubicAtMidTangent(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
105                                       const Sk2f& p3, const Sk2f& tan0, const Sk2f& tan3,
106                                       int maxFutureSubdivisions = kMaxSubdivionsPerCubicSection);
107 
108     template<AppendCubicFn AppendLeft, AppendCubicFn AppendRight>
109     inline void chopCubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3,
110                           float T, int maxFutureSubdivisions = kMaxSubdivionsPerCubicSection);
111 
112     void appendMonotonicCubics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3,
113                                int maxSubdivisions = kMaxSubdivionsPerCubicSection);
114     void appendCubicApproximation(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, const Sk2f& p3,
115                                   int maxSubdivisions = kMaxSubdivionsPerCubicSection);
116 
117     // Transient state used while building a contour.
118     SkPoint                         fCurrAnchorPoint;
119     SkPoint                         fCurrFanPoint;
120     PrimitiveTallies                fCurrContourTallies;
121     SkCubicType                     fCurrCubicType;
122     SkDEBUGCODE(bool                fBuildingContour = false);
123 
124     // TODO: These points could eventually be written directly to block-allocated GPU buffers.
125     SkSTArray<128, SkPoint, true>   fPoints;
126     SkSTArray<128, Verb, true>      fVerbs;
127 };
128 
129 inline void GrCCGeometry::PrimitiveTallies::operator+=(const PrimitiveTallies& b) {
130     fTriangles += b.fTriangles;
131     fQuadratics += b.fQuadratics;
132     fCubics += b.fCubics;
133 }
134 
135 GrCCGeometry::PrimitiveTallies
136 inline GrCCGeometry::PrimitiveTallies::operator-(const PrimitiveTallies& b) const {
137     return {fTriangles - b.fTriangles,
138             fQuadratics - b.fQuadratics,
139             fCubics - b.fCubics};
140 }
141 
142 inline bool GrCCGeometry::PrimitiveTallies::operator==(const PrimitiveTallies& b) {
143     return fTriangles == b.fTriangles && fQuadratics == b.fQuadratics && fCubics == b.fCubics;
144 }
145 
146 #endif
147