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 | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 8 | #ifndef GrGrCCFillGeometry_DEFINED |
| 9 | #define GrGrCCFillGeometry_DEFINED |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPoint.h" |
| 12 | #include "include/private/SkNx.h" |
| 13 | #include "include/private/SkTArray.h" |
| 14 | #include "src/core/SkGeometry.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 | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 18 | * fill. (See GrCCFillGeometry::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 | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 23 | class GrCCFillGeometry { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 24 | public: |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 25 | // These are the verbs that CCPR knows how to fill. If a path has any segments that don't map to |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 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 | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 52 | GrCCFillGeometry(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 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 67 | void beginPath(); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 68 | void beginContour(const SkPoint&); |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 69 | void lineTo(const SkPoint P[2]); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 70 | void quadraticTo(const SkPoint[3]); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 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). |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 83 | void cubicTo(const SkPoint[4], float inflectPad = 0.55f, float loopIntersectPad = 2); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 84 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 85 | void conicTo(const SkPoint[3], float w); |
| 86 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 87 | PrimitiveTallies endContour(); // Returns the numbers of primitives needed to draw the contour. |
| 88 | |
| 89 | private: |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 90 | inline void appendLine(const Sk2f& p0, const Sk2f& p1); |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 91 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 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); |
Chris Dalton | 7f578bf | 2017-09-05 16:46:48 -0600 | [diff] [blame] | 94 | |
Chris Dalton | b3a6959 | 2018-04-18 14:10:22 -0600 | [diff] [blame] | 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); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 107 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 108 | void appendMonotonicConic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2, float w); |
| 109 | |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 110 | // Transient state used while building a contour. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 111 | SkPoint fCurrAnchorPoint; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 112 | PrimitiveTallies fCurrContourTallies; |
| 113 | SkCubicType fCurrCubicType; |
| 114 | SkDEBUGCODE(bool fBuildingContour = false); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 115 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 116 | SkSTArray<128, SkPoint, true> fPoints; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 117 | SkSTArray<128, Verb, true> fVerbs; |
Chris Dalton | 3917b1e | 2018-05-09 00:40:52 -0600 | [diff] [blame] | 118 | SkSTArray<32, float, true> fConicWeights; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 119 | }; |
| 120 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 121 | inline void GrCCFillGeometry::PrimitiveTallies::operator+=(const PrimitiveTallies& b) { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 122 | fTriangles += b.fTriangles; |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 123 | fWeightedTriangles += b.fWeightedTriangles; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 124 | fQuadratics += b.fQuadratics; |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 125 | fCubics += b.fCubics; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 126 | fConics += b.fConics; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 129 | GrCCFillGeometry::PrimitiveTallies |
| 130 | inline GrCCFillGeometry::PrimitiveTallies::operator-(const PrimitiveTallies& b) const { |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 131 | return {fTriangles - b.fTriangles, |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 132 | fWeightedTriangles - b.fWeightedTriangles, |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 133 | fQuadratics - b.fQuadratics, |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 134 | fCubics - b.fCubics, |
| 135 | fConics - b.fConics}; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 136 | } |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 137 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 138 | inline bool GrCCFillGeometry::PrimitiveTallies::operator==(const PrimitiveTallies& b) { |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 139 | return fTriangles == b.fTriangles && fWeightedTriangles == b.fWeightedTriangles && |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 140 | fQuadratics == b.fQuadratics && fCubics == b.fCubics && fConics == b.fConics; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Chris Dalton | 419a94d | 2017-08-28 10:24:22 -0600 | [diff] [blame] | 143 | #endif |