bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 1 | /* |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 2 | * Copyright 2016 Google Inc. |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 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 | |
george | 9eb182a | 2014-06-20 12:01:06 -0700 | [diff] [blame] | 8 | #ifndef GrReducedClip_DEFINED |
| 9 | #define GrReducedClip_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkClipStack.h" |
| 12 | #include "src/core/SkTLList.h" |
| 13 | #include "src/gpu/GrFragmentProcessor.h" |
| 14 | #include "src/gpu/GrWindowRectangles.h" |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 15 | |
Brian Osman | fe83ab6 | 2021-06-19 15:13:02 +0000 | [diff] [blame^] | 16 | class GrCoverageCountingPathRenderer; |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 17 | class GrRecordingContext; |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 18 | class GrSurfaceDrawContext; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 19 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 20 | /** |
| 21 | * This class takes a clip stack and produces a reduced set of elements that are equivalent to |
| 22 | * applying that full stack within a specified query rectangle. |
| 23 | */ |
Brian Salomon | 57f211b | 2019-08-21 15:21:09 -0400 | [diff] [blame] | 24 | class GrReducedClip { |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 25 | public: |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 26 | using Element = SkClipStack::Element; |
| 27 | using ElementList = SkTLList<SkClipStack::Element, 16>; |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 28 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 29 | GrReducedClip(const SkClipStack&, const SkRect& queryBounds, const GrCaps* caps, |
Brian Osman | fe83ab6 | 2021-06-19 15:13:02 +0000 | [diff] [blame^] | 30 | int maxWindowRectangles = 0, int maxAnalyticElements = 0, |
| 31 | int maxCCPRClipPaths = 0); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 32 | |
| 33 | enum class InitialState : bool { |
| 34 | kAllIn, |
| 35 | kAllOut |
| 36 | }; |
| 37 | |
| 38 | InitialState initialState() const { return fInitialState; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 41 | * If hasScissor() is true, the clip mask is not valid outside this rect and the caller must |
| 42 | * enforce this scissor during draw. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 43 | */ |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 44 | const SkIRect& scissor() const { SkASSERT(fHasScissor); return fScissor; } |
| 45 | int left() const { return this->scissor().left(); } |
| 46 | int top() const { return this->scissor().top(); } |
| 47 | int width() const { return this->scissor().width(); } |
| 48 | int height() const { return this->scissor().height(); } |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 51 | * Indicates whether scissor() is defined. It will always be defined if the maskElements() are |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 52 | * nonempty. |
| 53 | */ |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 54 | bool hasScissor() const { return fHasScissor; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 55 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 56 | /** |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 57 | * Indicates if there is a clip shader, representing the merge of all shader elements of the |
| 58 | * original stack. |
| 59 | */ |
| 60 | bool hasShader() const { return SkToBool(fShader); } |
| 61 | sk_sp<SkShader> shader() const { SkASSERT(fShader); return fShader; } |
| 62 | |
| 63 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 64 | * If nonempty, the clip mask is not valid inside these windows and the caller must clip them |
| 65 | * out using the window rectangles GPU extension. |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 66 | */ |
| 67 | const GrWindowRectangles& windowRectangles() const { return fWindowRects; } |
| 68 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 69 | /** |
| 70 | * An ordered list of clip elements that could not be skipped or implemented by other means. If |
| 71 | * nonempty, the caller must create an alpha and/or stencil mask for these elements and apply it |
| 72 | * during draw. |
| 73 | */ |
| 74 | const ElementList& maskElements() const { return fMaskElements; } |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 75 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 76 | /** |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 77 | * If maskElements() are nonempty, uniquely identifies the region of the clip mask that falls |
| 78 | * inside of scissor(). |
| 79 | * |
| 80 | * NOTE: since clip elements might fall outside the query bounds, different regions of the same |
| 81 | * clip stack might have more or less restrictive IDs. |
| 82 | * |
| 83 | * FIXME: this prevents us from reusing a sub-rect of a perfectly good mask when that rect has |
| 84 | * been assigned a less restrictive ID. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 85 | */ |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 86 | uint32_t maskGenID() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskGenID; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 89 | * Indicates whether antialiasing is required to process any of the mask elements. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 90 | */ |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 91 | bool maskRequiresAA() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskRequiresAA; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 92 | |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 93 | bool drawAlphaClipMask(GrSurfaceDrawContext*) const; |
| 94 | bool drawStencilClipMask(GrRecordingContext*, GrSurfaceDrawContext*) const; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 95 | |
John Stiles | 9e8f484 | 2020-07-09 11:30:05 -0400 | [diff] [blame] | 96 | int numAnalyticElements() const; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 99 | * Called once the client knows the ID of the opsTask that the clip FPs will operate in. This |
| 100 | * method finishes any outstanding work that was waiting for the opsTask ID, then detaches and |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 101 | * returns this class's list of FPs that complete the clip. |
| 102 | * |
| 103 | * NOTE: this must be called AFTER producing the clip mask (if any) because draw calls on |
| 104 | * the render target context, surface allocations, and even switching render targets (pre MDB) |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 105 | * may cause flushes or otherwise change which opsTask the actual draw is going into. |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 106 | */ |
Derek Sollenberger | 396cd1d | 2021-04-02 15:44:36 +0000 | [diff] [blame] | 107 | GrFPResult finishAndDetachAnalyticElements(GrRecordingContext*, const SkMatrixProvider& |
Brian Osman | fe83ab6 | 2021-06-19 15:13:02 +0000 | [diff] [blame^] | 108 | matrixProvider, GrCoverageCountingPathRenderer*, |
| 109 | uint32_t opsTaskID); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 110 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 111 | private: |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 112 | void walkStack(const SkClipStack&, const SkRect& queryBounds); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 113 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 114 | enum class ClipResult { |
| 115 | kNotClipped, |
| 116 | kClipped, |
| 117 | kMadeEmpty |
| 118 | }; |
| 119 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 120 | // Intersects the clip with the element's interior, regardless of inverse fill type. |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 121 | // NOTE: do not call for elements followed by ops that can grow the clip. |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 122 | ClipResult clipInsideElement(const Element*); |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 123 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 124 | // Intersects the clip with the element's exterior, regardless of inverse fill type. |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 125 | // NOTE: do not call for elements followed by ops that can grow the clip. |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 126 | ClipResult clipOutsideElement(const Element*); |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 127 | |
| 128 | void addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA); |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 129 | |
| 130 | enum class Invert : bool { |
Chris Dalton | 3b51df1 | 2017-11-27 14:33:06 -0700 | [diff] [blame] | 131 | kNo = false, |
| 132 | kYes = true |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 135 | static GrClipEdgeType GetClipEdgeType(Invert, GrAA); |
John Stiles | 9e8f484 | 2020-07-09 11:30:05 -0400 | [diff] [blame] | 136 | ClipResult addAnalyticRect(const SkRect& deviceSpaceRect, Invert, GrAA); |
| 137 | ClipResult addAnalyticRRect(const SkRRect& deviceSpaceRRect, Invert, GrAA); |
| 138 | ClipResult addAnalyticPath(const SkPath& deviceSpacePath, Invert, GrAA); |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 139 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 140 | void makeEmpty(); |
| 141 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 142 | const GrCaps* fCaps; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 143 | const int fMaxWindowRectangles; |
John Stiles | 9e8f484 | 2020-07-09 11:30:05 -0400 | [diff] [blame] | 144 | const int fMaxAnalyticElements; |
Brian Osman | fe83ab6 | 2021-06-19 15:13:02 +0000 | [diff] [blame^] | 145 | const int fMaxCCPRClipPaths; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 146 | |
| 147 | InitialState fInitialState; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 148 | SkIRect fScissor; |
John Stiles | c34c6bc | 2020-07-09 10:16:31 -0400 | [diff] [blame] | 149 | bool fHasScissor = false; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 150 | SkRect fAAClipRect; |
John Stiles | c34c6bc | 2020-07-09 10:16:31 -0400 | [diff] [blame] | 151 | uint32_t fAAClipRectGenID = SK_InvalidGenID; // the GenID that the mask will have if the AA |
| 152 | // clip-rect is included |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 153 | GrWindowRectangles fWindowRects; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 154 | ElementList fMaskElements; |
John Stiles | c34c6bc | 2020-07-09 10:16:31 -0400 | [diff] [blame] | 155 | uint32_t fMaskGenID = SK_InvalidGenID; |
| 156 | bool fMaskRequiresAA = false; |
John Stiles | 777556a | 2020-06-19 10:22:31 -0400 | [diff] [blame] | 157 | std::unique_ptr<GrFragmentProcessor> fAnalyticFP; |
John Stiles | 9e8f484 | 2020-07-09 11:30:05 -0400 | [diff] [blame] | 158 | int fNumAnalyticElements = 0; |
Brian Osman | fe83ab6 | 2021-06-19 15:13:02 +0000 | [diff] [blame^] | 159 | SkSTArray<4, SkPath> fCCPRClipPaths; // Converted to FPs once we have an opsTask ID for CCPR. |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 160 | // Will be the combination of all kShader elements or null if there's no clip shader. |
| 161 | // Does not count against the analytic FP limit. |
| 162 | sk_sp<SkShader> fShader; |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
george | 9eb182a | 2014-06-20 12:01:06 -0700 | [diff] [blame] | 165 | #endif |