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