Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 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 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #ifndef GrGrCCGeometry_DEFINED |
| 9 | #define GrGrCCGeometry_DEFINED |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 10 | |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 11 | #include "SkGeometry.h" |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 12 | #include "SkNx.h" |
| 13 | #include "SkPoint.h" |
| 14 | #include "SkTArray.h" |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 15 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 16 | /** |
| 17 | * This class chops device-space contours up into a series of segments that CCPR knows how to |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 18 | * render. (See GrCCGeometry::Verb.) |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 19 | * |
| 20 | * NOTE: This must be done in device space, since an affine transformation can change whether a |
| 21 | * curve is monotonic. |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 22 | */ |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 23 | class GrCCGeometry { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 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]. |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 33 | kMonotonicCubicTo, |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 34 | kMonotonicConicTo, |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 35 | kEndClosedContour, // endPt == startPt. |
| 36 | kEndOpenContour // endPt != startPt. |
| 37 | }; |
| 38 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 39 | // These tallies track numbers of CCPR primitives that are required to draw a contour. |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 40 | struct PrimitiveTallies { |
| 41 | int fTriangles; // Number of triangles in the contour's fan. |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 42 | int fWeightedTriangles; // Triangles (from the tessellator) whose winding magnitude > 1. |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 43 | int fQuadratics; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 44 | int fCubics; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 45 | int fConics; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 46 | |
| 47 | void operator+=(const PrimitiveTallies&); |
| 48 | PrimitiveTallies operator-(const PrimitiveTallies&) const; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 49 | bool operator==(const PrimitiveTallies&); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 50 | }; |
| 51 | |
Chris Dalton | 3917b1e | 2018-05-09 00:40:52 -0600 | [diff] [blame^] | 52 | GrCCGeometry(int numSkPoints = 0, int numSkVerbs = 0, int numConicWeights = 0) |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 53 | : fPoints(numSkPoints * 3) // Reserve for a 3x expansion in points and verbs. |
Chris Dalton | 3917b1e | 2018-05-09 00:40:52 -0600 | [diff] [blame^] | 54 | , fVerbs(numSkVerbs * 3) |
| 55 | , fConicWeights(numConicWeights * 3/2) {} |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 56 | |
| 57 | const SkTArray<SkPoint, true>& points() const { SkASSERT(!fBuildingContour); return fPoints; } |
| 58 | const SkTArray<Verb, true>& verbs() const { SkASSERT(!fBuildingContour); return fVerbs; } |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 59 | float getConicWeight(int idx) const { SkASSERT(!fBuildingContour); return fConicWeights[idx]; } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 60 | |
| 61 | void reset() { |
| 62 | SkASSERT(!fBuildingContour); |
| 63 | fPoints.reset(); |
| 64 | fVerbs.reset(); |
| 65 | } |
| 66 | |
| 67 | // This is included in case the caller needs to discard previously added contours. It is up to |
| 68 | // the caller to track counts and ensure we don't pop back into the middle of a different |
| 69 | // contour. |
| 70 | void resize_back(int numPoints, int numVerbs) { |
| 71 | SkASSERT(!fBuildingContour); |
| 72 | fPoints.resize_back(numPoints); |
| 73 | fVerbs.resize_back(numVerbs); |
| 74 | SkASSERT(fVerbs.empty() || fVerbs.back() == Verb::kEndOpenContour || |
| 75 | fVerbs.back() == Verb::kEndClosedContour); |
| 76 | } |
| 77 | |
| 78 | void beginPath(); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 79 | void beginContour(const SkPoint&); |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 80 | void lineTo(const SkPoint P[2]); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 81 | void quadraticTo(const SkPoint[3]); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 82 | |
| 83 | // We pass through inflection points and loop intersections using a line and quadratic(s) |
| 84 | // respectively. 'inflectPad' and 'loopIntersectPad' specify how close (in pixels) cubic |
| 85 | // segments are allowed to get to these points. For normal rendering you will want to use the |
| 86 | // default values, but these can be overridden for testing purposes. |
| 87 | // |
| 88 | // NOTE: loops do appear to require two full pixels of padding around the intersection point. |
| 89 | // With just one pixel-width of pad, we start to see bad pixels. Ultimately this has a |
| 90 | // minimal effect on the total amount of segments produced. Most sections that pass |
| 91 | // through the loop intersection can be approximated with a single quadratic anyway, |
| 92 | // regardless of whether we are use one pixel of pad or two (1.622 avg. quads per loop |
| 93 | // intersection vs. 1.489 on the tiger). |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 94 | void cubicTo(const SkPoint[4], float inflectPad = 0.55f, float loopIntersectPad = 2); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 95 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 96 | void conicTo(const SkPoint[3], float w); |
| 97 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 98 | PrimitiveTallies endContour(); // Returns the numbers of primitives needed to draw the contour. |
| 99 | |
| 100 | private: |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 101 | inline void appendLine(const Sk2f& p0, const Sk2f& p1); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 102 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 103 | inline void appendQuadratics(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2); |
| 104 | inline void appendMonotonicQuadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 105 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 106 | enum class AppendCubicMode : bool { |
| 107 | kLiteral, |
| 108 | kApproximate |
| 109 | }; |
| 110 | void appendCubics(AppendCubicMode, const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 111 | const Sk2f& p3, const float chops[], int numChops, float localT0 = 0, |
| 112 | float localT1 = 1); |
| 113 | void appendCubics(AppendCubicMode, const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, |
| 114 | const Sk2f& p3, int maxSubdivisions = 2); |
| 115 | void chopAndAppendCubicAtMidTangent(AppendCubicMode, const Sk2f& p0, const Sk2f& p1, |
| 116 | const Sk2f& p2, const Sk2f& p3, const Sk2f& tan0, |
| 117 | const Sk2f& tan1, int maxFutureSubdivisions); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 118 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 119 | void appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w); |
| 120 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 121 | // Transient state used while building a contour. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 122 | SkPoint fCurrAnchorPoint; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 123 | PrimitiveTallies fCurrContourTallies; |
| 124 | SkCubicType fCurrCubicType; |
| 125 | SkDEBUGCODE(bool fBuildingContour = false); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 126 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 127 | SkSTArray<128, SkPoint, true> fPoints; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 128 | SkSTArray<128, Verb, true> fVerbs; |
Chris Dalton | 3917b1e | 2018-05-09 00:40:52 -0600 | [diff] [blame^] | 129 | SkSTArray<32, float, true> fConicWeights; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 130 | }; |
| 131 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 132 | inline void GrCCGeometry::PrimitiveTallies::operator+=(const PrimitiveTallies& b) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 133 | fTriangles += b.fTriangles; |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 134 | fWeightedTriangles += b.fWeightedTriangles; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 135 | fQuadratics += b.fQuadratics; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 136 | fCubics += b.fCubics; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 137 | fConics += b.fConics; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 138 | } |
| 139 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 140 | GrCCGeometry::PrimitiveTallies |
| 141 | inline GrCCGeometry::PrimitiveTallies::operator-(const PrimitiveTallies& b) const { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 142 | return {fTriangles - b.fTriangles, |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 143 | fWeightedTriangles - b.fWeightedTriangles, |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 144 | fQuadratics - b.fQuadratics, |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 145 | fCubics - b.fCubics, |
| 146 | fConics - b.fConics}; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 147 | } |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 148 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 149 | inline bool GrCCGeometry::PrimitiveTallies::operator==(const PrimitiveTallies& b) { |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 150 | return fTriangles == b.fTriangles && fWeightedTriangles == b.fWeightedTriangles && |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 151 | fQuadratics == b.fQuadratics && fCubics == b.fCubics && fConics == b.fConics; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 154 | #endif |