blob: df817db10c30e1ec76b632c8b00ea2cde6c87fbe [file] [log] [blame]
Chris Dalton419a94d2017-08-28 10:24:22 -06001/*
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
Chris Daltone1639692018-08-20 14:00:30 -06008#ifndef GrGrCCFillGeometry_DEFINED
9#define GrGrCCFillGeometry_DEFINED
Chris Dalton419a94d2017-08-28 10:24:22 -060010
Chris Dalton7f578bf2017-09-05 16:46:48 -060011#include "SkGeometry.h"
Chris Daltonc1e59632017-09-05 00:30:07 -060012#include "SkNx.h"
13#include "SkPoint.h"
14#include "SkTArray.h"
Chris Dalton419a94d2017-08-28 10:24:22 -060015
Chris Daltonc1e59632017-09-05 00:30:07 -060016/**
17 * This class chops device-space contours up into a series of segments that CCPR knows how to
Chris Daltone1639692018-08-20 14:00:30 -060018 * fill. (See GrCCFillGeometry::Verb.)
Chris Dalton419a94d2017-08-28 10:24:22 -060019 *
20 * NOTE: This must be done in device space, since an affine transformation can change whether a
21 * curve is monotonic.
Chris Dalton419a94d2017-08-28 10:24:22 -060022 */
Chris Daltone1639692018-08-20 14:00:30 -060023class GrCCFillGeometry {
Chris Daltonc1e59632017-09-05 00:30:07 -060024public:
Chris Daltone1639692018-08-20 14:00:30 -060025 // These are the verbs that CCPR knows how to fill. If a path has any segments that don't map to
Chris Daltonc1e59632017-09-05 00:30:07 -060026 // 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].
Chris Daltonbe4ffab2017-12-08 10:59:58 -070033 kMonotonicCubicTo,
Chris Dalton9f2dab02018-04-18 14:07:03 -060034 kMonotonicConicTo,
Chris Daltonc1e59632017-09-05 00:30:07 -060035 kEndClosedContour, // endPt == startPt.
36 kEndOpenContour // endPt != startPt.
37 };
38
Chris Dalton84403d72018-02-13 21:46:17 -050039 // These tallies track numbers of CCPR primitives that are required to draw a contour.
Chris Daltonc1e59632017-09-05 00:30:07 -060040 struct PrimitiveTallies {
41 int fTriangles; // Number of triangles in the contour's fan.
Chris Dalton703b4762018-04-06 16:11:48 -060042 int fWeightedTriangles; // Triangles (from the tessellator) whose winding magnitude > 1.
Chris Daltonc1e59632017-09-05 00:30:07 -060043 int fQuadratics;
Chris Daltonbe4ffab2017-12-08 10:59:58 -070044 int fCubics;
Chris Dalton9f2dab02018-04-18 14:07:03 -060045 int fConics;
Chris Daltonc1e59632017-09-05 00:30:07 -060046
47 void operator+=(const PrimitiveTallies&);
48 PrimitiveTallies operator-(const PrimitiveTallies&) const;
Chris Dalton9ca27842018-01-18 12:24:50 -070049 bool operator==(const PrimitiveTallies&);
Chris Daltonc1e59632017-09-05 00:30:07 -060050 };
51
Chris Daltone1639692018-08-20 14:00:30 -060052 GrCCFillGeometry(int numSkPoints = 0, int numSkVerbs = 0, int numConicWeights = 0)
Chris Daltonc1e59632017-09-05 00:30:07 -060053 : fPoints(numSkPoints * 3) // Reserve for a 3x expansion in points and verbs.
Chris Dalton3917b1e2018-05-09 00:40:52 -060054 , fVerbs(numSkVerbs * 3)
55 , fConicWeights(numConicWeights * 3/2) {}
Chris Daltonc1e59632017-09-05 00:30:07 -060056
57 const SkTArray<SkPoint, true>& points() const { SkASSERT(!fBuildingContour); return fPoints; }
58 const SkTArray<Verb, true>& verbs() const { SkASSERT(!fBuildingContour); return fVerbs; }
Chris Dalton9f2dab02018-04-18 14:07:03 -060059 float getConicWeight(int idx) const { SkASSERT(!fBuildingContour); return fConicWeights[idx]; }
Chris Daltonc1e59632017-09-05 00:30:07 -060060
61 void reset() {
62 SkASSERT(!fBuildingContour);
63 fPoints.reset();
64 fVerbs.reset();
65 }
66
Chris Daltonc1e59632017-09-05 00:30:07 -060067 void beginPath();
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060068 void beginContour(const SkPoint&);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060069 void lineTo(const SkPoint P[2]);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060070 void quadraticTo(const SkPoint[3]);
Chris Dalton7f578bf2017-09-05 16:46:48 -060071
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).
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060083 void cubicTo(const SkPoint[4], float inflectPad = 0.55f, float loopIntersectPad = 2);
Chris Dalton7f578bf2017-09-05 16:46:48 -060084
Chris Dalton9f2dab02018-04-18 14:07:03 -060085 void conicTo(const SkPoint[3], float w);
86
Chris Daltonc1e59632017-09-05 00:30:07 -060087 PrimitiveTallies endContour(); // Returns the numbers of primitives needed to draw the contour.
88
89private:
Chris Dalton6f5e77a2018-04-23 21:14:42 -060090 inline void appendLine(const Sk2f& p0, const Sk2f& p1);
Chris Dalton7ca3b7b2018-04-10 00:21:19 -060091
Chris Daltonb3a69592018-04-18 14:10:22 -060092 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);
Chris Dalton7f578bf2017-09-05 16:46:48 -060094
Chris Daltonb3a69592018-04-18 14:10:22 -060095 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);
Chris Daltonc1e59632017-09-05 00:30:07 -0600107
Chris Dalton9f2dab02018-04-18 14:07:03 -0600108 void appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w);
109
Chris Daltonc1e59632017-09-05 00:30:07 -0600110 // Transient state used while building a contour.
Chris Dalton84403d72018-02-13 21:46:17 -0500111 SkPoint fCurrAnchorPoint;
Chris Dalton84403d72018-02-13 21:46:17 -0500112 PrimitiveTallies fCurrContourTallies;
113 SkCubicType fCurrCubicType;
114 SkDEBUGCODE(bool fBuildingContour = false);
Chris Daltonc1e59632017-09-05 00:30:07 -0600115
Chris Dalton9f2dab02018-04-18 14:07:03 -0600116 SkSTArray<128, SkPoint, true> fPoints;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600117 SkSTArray<128, Verb, true> fVerbs;
Chris Dalton3917b1e2018-05-09 00:40:52 -0600118 SkSTArray<32, float, true> fConicWeights;
Chris Daltonc1e59632017-09-05 00:30:07 -0600119};
120
Chris Daltone1639692018-08-20 14:00:30 -0600121inline void GrCCFillGeometry::PrimitiveTallies::operator+=(const PrimitiveTallies& b) {
Chris Daltonc1e59632017-09-05 00:30:07 -0600122 fTriangles += b.fTriangles;
Chris Dalton703b4762018-04-06 16:11:48 -0600123 fWeightedTriangles += b.fWeightedTriangles;
Chris Daltonc1e59632017-09-05 00:30:07 -0600124 fQuadratics += b.fQuadratics;
Chris Daltonbe4ffab2017-12-08 10:59:58 -0700125 fCubics += b.fCubics;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600126 fConics += b.fConics;
Chris Daltonc1e59632017-09-05 00:30:07 -0600127}
128
Chris Daltone1639692018-08-20 14:00:30 -0600129GrCCFillGeometry::PrimitiveTallies
130inline GrCCFillGeometry::PrimitiveTallies::operator-(const PrimitiveTallies& b) const {
Chris Daltonc1e59632017-09-05 00:30:07 -0600131 return {fTriangles - b.fTriangles,
Chris Dalton703b4762018-04-06 16:11:48 -0600132 fWeightedTriangles - b.fWeightedTriangles,
Chris Daltonc1e59632017-09-05 00:30:07 -0600133 fQuadratics - b.fQuadratics,
Chris Dalton9f2dab02018-04-18 14:07:03 -0600134 fCubics - b.fCubics,
135 fConics - b.fConics};
Chris Daltonc1e59632017-09-05 00:30:07 -0600136}
Chris Dalton419a94d2017-08-28 10:24:22 -0600137
Chris Daltone1639692018-08-20 14:00:30 -0600138inline bool GrCCFillGeometry::PrimitiveTallies::operator==(const PrimitiveTallies& b) {
Chris Dalton703b4762018-04-06 16:11:48 -0600139 return fTriangles == b.fTriangles && fWeightedTriangles == b.fWeightedTriangles &&
Chris Dalton9f2dab02018-04-18 14:07:03 -0600140 fQuadratics == b.fQuadratics && fCubics == b.fCubics && fConics == b.fConics;
Chris Dalton9ca27842018-01-18 12:24:50 -0700141}
142
Chris Dalton419a94d2017-08-28 10:24:22 -0600143#endif