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 | |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 11 | #include "GrFragmentProcessor.h" |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 12 | #include "GrWindowRectangles.h" |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 13 | #include "SkClipStack.h" |
| 14 | #include "SkTLList.h" |
| 15 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 16 | class GrContext; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 17 | class GrCoverageCountingPathRenderer; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 18 | class GrRenderTargetContext; |
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 | */ |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 24 | class SK_API GrReducedClip { |
| 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 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 29 | GrReducedClip(const SkClipStack&, const SkRect& queryBounds, const GrCaps* caps, |
Chris Dalton | 1dec19a | 2018-04-27 13:05:19 -0600 | [diff] [blame] | 30 | int maxWindowRectangles = 0, int maxAnalyticFPs = 0, int maxCCPRClipPaths = 0); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 31 | |
| 32 | enum class InitialState : bool { |
| 33 | kAllIn, |
| 34 | kAllOut |
| 35 | }; |
| 36 | |
| 37 | InitialState initialState() const { return fInitialState; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 40 | * If hasScissor() is true, the clip mask is not valid outside this rect and the caller must |
| 41 | * enforce this scissor during draw. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 42 | */ |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 43 | const SkIRect& scissor() const { SkASSERT(fHasScissor); return fScissor; } |
| 44 | int left() const { return this->scissor().left(); } |
| 45 | int top() const { return this->scissor().top(); } |
| 46 | int width() const { return this->scissor().width(); } |
| 47 | int height() const { return this->scissor().height(); } |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 50 | * 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] | 51 | * nonempty. |
| 52 | */ |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 53 | bool hasScissor() const { return fHasScissor; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 54 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 55 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 56 | * If nonempty, the clip mask is not valid inside these windows and the caller must clip them |
| 57 | * out using the window rectangles GPU extension. |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 58 | */ |
| 59 | const GrWindowRectangles& windowRectangles() const { return fWindowRects; } |
| 60 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 61 | /** |
| 62 | * An ordered list of clip elements that could not be skipped or implemented by other means. If |
| 63 | * nonempty, the caller must create an alpha and/or stencil mask for these elements and apply it |
| 64 | * during draw. |
| 65 | */ |
| 66 | const ElementList& maskElements() const { return fMaskElements; } |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 67 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 68 | /** |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame^] | 69 | * If maskElements() are nonempty, uniquely identifies the region of the clip mask that falls |
| 70 | * inside of scissor(). |
| 71 | * |
| 72 | * NOTE: since clip elements might fall outside the query bounds, different regions of the same |
| 73 | * clip stack might have more or less restrictive IDs. |
| 74 | * |
| 75 | * FIXME: this prevents us from reusing a sub-rect of a perfectly good mask when that rect has |
| 76 | * been assigned a less restrictive ID. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 77 | */ |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame^] | 78 | uint32_t maskGenID() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskGenID; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 81 | * Indicates whether antialiasing is required to process any of the mask elements. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 82 | */ |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 83 | bool maskRequiresAA() const { SkASSERT(!fMaskElements.isEmpty()); return fMaskRequiresAA; } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 84 | |
Chris Dalton | c534808 | 2018-03-30 15:59:38 +0000 | [diff] [blame] | 85 | bool drawAlphaClipMask(GrRenderTargetContext*) const; |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 86 | bool drawStencilClipMask(GrContext*, GrRenderTargetContext*) const; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 87 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 88 | int numAnalyticFPs() const { return fAnalyticFPs.count() + fCCPRClipPaths.count(); } |
| 89 | |
| 90 | /** |
| 91 | * Called once the client knows the ID of the opList that the clip FPs will operate in. This |
| 92 | * method finishes any outstanding work that was waiting for the opList ID, then detaches and |
| 93 | * returns this class's list of FPs that complete the clip. |
| 94 | * |
| 95 | * NOTE: this must be called AFTER producing the clip mask (if any) because draw calls on |
| 96 | * the render target context, surface allocations, and even switching render targets (pre MDB) |
| 97 | * may cause flushes or otherwise change which opList the actual draw is going into. |
| 98 | */ |
Chris Dalton | 1dec19a | 2018-04-27 13:05:19 -0600 | [diff] [blame] | 99 | std::unique_ptr<GrFragmentProcessor> finishAndDetachAnalyticFPs(GrCoverageCountingPathRenderer*, |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 100 | uint32_t opListID, int rtWidth, |
| 101 | int rtHeight); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 102 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 103 | private: |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 104 | void walkStack(const SkClipStack&, const SkRect& queryBounds); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 105 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 106 | enum class ClipResult { |
| 107 | kNotClipped, |
| 108 | kClipped, |
| 109 | kMadeEmpty |
| 110 | }; |
| 111 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 112 | // 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] | 113 | // 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^] | 114 | ClipResult clipInsideElement(const Element*); |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 115 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 116 | // 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] | 117 | // 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^] | 118 | ClipResult clipOutsideElement(const Element*); |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 119 | |
| 120 | void addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA); |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 121 | |
| 122 | enum class Invert : bool { |
Chris Dalton | 3b51df1 | 2017-11-27 14:33:06 -0700 | [diff] [blame] | 123 | kNo = false, |
| 124 | kYes = true |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 127 | static GrClipEdgeType GetClipEdgeType(Invert, GrAA); |
| 128 | ClipResult addAnalyticFP(const SkRect& deviceSpaceRect, Invert, GrAA); |
| 129 | ClipResult addAnalyticFP(const SkRRect& deviceSpaceRRect, Invert, GrAA); |
| 130 | ClipResult addAnalyticFP(const SkPath& deviceSpacePath, Invert, GrAA); |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 131 | |
Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 132 | void makeEmpty(); |
| 133 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 134 | const GrCaps* fCaps; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 135 | const int fMaxWindowRectangles; |
| 136 | const int fMaxAnalyticFPs; |
Chris Dalton | 1dec19a | 2018-04-27 13:05:19 -0600 | [diff] [blame] | 137 | const int fMaxCCPRClipPaths; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 138 | |
| 139 | InitialState fInitialState; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 140 | SkIRect fScissor; |
| 141 | bool fHasScissor; |
| 142 | SkRect fAAClipRect; |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame^] | 143 | uint32_t fAAClipRectGenID; // GenID the mask will have if includes the AA clip rect. |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 144 | GrWindowRectangles fWindowRects; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 145 | ElementList fMaskElements; |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame^] | 146 | uint32_t fMaskGenID; |
Chris Dalton | 584a79a | 2017-11-15 13:14:01 -0700 | [diff] [blame] | 147 | bool fMaskRequiresAA; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 148 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fAnalyticFPs; |
| 149 | SkSTArray<4, SkPath> fCCPRClipPaths; // Will convert to FPs once we have an opList ID for CCPR. |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
george | 9eb182a | 2014-06-20 12:01:06 -0700 | [diff] [blame] | 152 | #endif |