csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #ifndef GrAppliedClip_DEFINED |
| 9 | #define GrAppliedClip_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrFragmentProcessor.h" |
| 12 | #include "src/gpu/GrScissorState.h" |
| 13 | #include "src/gpu/GrWindowRectsState.h" |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/core/SkClipStack.h" |
Robert Phillips | a4f792d | 2017-06-28 08:40:11 -0400 | [diff] [blame] | 16 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 17 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 18 | /** |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 19 | * Produced by GrHardClip. It provides a set of modifications to the hardware drawing state that |
| 20 | * implement the clip. |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 21 | */ |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 22 | class GrAppliedHardClip { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 23 | public: |
Michael Ludwig | 4926b07 | 2020-06-04 19:39:42 +0000 | [diff] [blame] | 24 | static const GrAppliedHardClip& Disabled() { |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 25 | // The size doesn't really matter here since it's returned as const& so an actual scissor |
| 26 | // will never be set on it, and applied clips are not used to query or bounds test like |
| 27 | // the GrClip is. |
| 28 | static const GrAppliedHardClip kDisabled({1 << 29, 1 << 29}); |
Michael Ludwig | 4926b07 | 2020-06-04 19:39:42 +0000 | [diff] [blame] | 29 | return kDisabled; |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 30 | } |
| 31 | |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 32 | GrAppliedHardClip(const SkISize& rtDims) : fScissorState(rtDims) {} |
| 33 | GrAppliedHardClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims) |
| 34 | : fScissorState(backingStoreDims) { |
| 35 | fScissorState.set(SkIRect::MakeSize(logicalRTDims)); |
| 36 | } |
| 37 | |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 38 | GrAppliedHardClip(GrAppliedHardClip&& that) = default; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 39 | explicit GrAppliedHardClip(const GrAppliedHardClip&) = default; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 40 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 41 | const GrScissorState& scissorState() const { return fScissorState; } |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 42 | const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; } |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 43 | uint32_t stencilStackID() const { return fStencilStackID; } |
| 44 | bool hasStencilClip() const { return SkClipStack::kInvalidGenID != fStencilStackID; } |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Intersects the applied clip with the provided rect. Returns false if the draw became empty. |
Derek Sollenberger | 396cd1d | 2021-04-02 15:44:36 +0000 | [diff] [blame] | 48 | * 'clippedDrawBounds' will be intersected with 'irect'. This returns false if the clip becomes |
| 49 | * empty or the draw no longer intersects the clip. In either case the draw can be skipped. |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 50 | */ |
Derek Sollenberger | 396cd1d | 2021-04-02 15:44:36 +0000 | [diff] [blame] | 51 | bool addScissor(const SkIRect& irect, SkRect* clippedDrawBounds) { |
| 52 | return fScissorState.intersect(irect) && clippedDrawBounds->intersect(SkRect::Make(irect)); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 55 | void setScissor(const SkIRect& irect) { |
| 56 | fScissorState.set(irect); |
| 57 | } |
| 58 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 59 | void addWindowRectangles(const GrWindowRectsState& windowState) { |
| 60 | SkASSERT(!fWindowRectsState.enabled()); |
| 61 | fWindowRectsState = windowState; |
| 62 | } |
| 63 | |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 64 | void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 65 | SkASSERT(!fWindowRectsState.enabled()); |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 66 | fWindowRectsState.set(windows, mode); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 69 | void addStencilClip(uint32_t stencilStackID) { |
| 70 | SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID); |
| 71 | fStencilStackID = stencilStackID; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool doesClip() const { |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 75 | return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled(); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | bool operator==(const GrAppliedHardClip& that) const { |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 79 | return fScissorState == that.fScissorState && |
| 80 | fWindowRectsState == that.fWindowRectsState && |
| 81 | fStencilStackID == that.fStencilStackID; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 82 | } |
| 83 | bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); } |
| 84 | |
| 85 | private: |
| 86 | GrScissorState fScissorState; |
| 87 | GrWindowRectsState fWindowRectsState; |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 88 | uint32_t fStencilStackID = SkClipStack::kInvalidGenID; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip. |
| 93 | */ |
| 94 | class GrAppliedClip { |
| 95 | public: |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 96 | static GrAppliedClip Disabled() { |
| 97 | return GrAppliedClip({1 << 29, 1 << 29}); |
| 98 | } |
| 99 | |
| 100 | GrAppliedClip(const SkISize& rtDims) : fHardClip(rtDims) {} |
| 101 | GrAppliedClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims) |
| 102 | : fHardClip(logicalRTDims, backingStoreDims) {} |
| 103 | |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 104 | GrAppliedClip(GrAppliedClip&& that) = default; |
| 105 | GrAppliedClip(const GrAppliedClip&) = delete; |
| 106 | |
| 107 | const GrScissorState& scissorState() const { return fHardClip.scissorState(); } |
| 108 | const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); } |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 109 | uint32_t stencilStackID() const { return fHardClip.stencilStackID(); } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 110 | bool hasStencilClip() const { return fHardClip.hasStencilClip(); } |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 111 | int hasCoverageFragmentProcessor() const { return fCoverageFP != nullptr; } |
| 112 | const GrFragmentProcessor* coverageFragmentProcessor() const { |
| 113 | SkASSERT(fCoverageFP != nullptr); |
| 114 | return fCoverageFP.get(); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 115 | } |
John Stiles | d3feb6f | 2020-07-23 18:18:12 -0400 | [diff] [blame] | 116 | std::unique_ptr<GrFragmentProcessor> detachCoverageFragmentProcessor() { |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 117 | SkASSERT(fCoverageFP != nullptr); |
| 118 | return std::move(fCoverageFP); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 121 | const GrAppliedHardClip& hardClip() const { return fHardClip; } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 122 | GrAppliedHardClip& hardClip() { return fHardClip; } |
| 123 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 124 | void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) { |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 125 | if (fCoverageFP == nullptr) { |
| 126 | fCoverageFP = std::move(fp); |
| 127 | } else { |
John Stiles | 024d745 | 2020-07-22 19:07:15 -0400 | [diff] [blame] | 128 | // Compose this coverage FP with the previously-added coverage. |
Brian Osman | c5422d0 | 2021-03-12 16:06:35 -0500 | [diff] [blame] | 129 | fCoverageFP = GrFragmentProcessor::Compose(std::move(fp), std::move(fCoverageFP)); |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 130 | } |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 133 | bool doesClip() const { |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 134 | return fHardClip.doesClip() || fCoverageFP != nullptr; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool operator==(const GrAppliedClip& that) const { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 138 | if (fHardClip != that.fHardClip || |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 139 | this->hasCoverageFragmentProcessor() != that.hasCoverageFragmentProcessor()) { |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 140 | return false; |
| 141 | } |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 142 | if (fCoverageFP != nullptr && !fCoverageFP->isEqual(*that.fCoverageFP)) { |
| 143 | return false; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 144 | } |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 145 | return true; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 146 | } |
| 147 | bool operator!=(const GrAppliedClip& that) const { return !(*this == that); } |
| 148 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 149 | void visitProxies(const GrVisitProxyFunc& func) const { |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 150 | if (fCoverageFP != nullptr) { |
| 151 | fCoverageFP->visitProxies(func); |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 155 | private: |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 156 | GrAppliedHardClip fHardClip; |
John Stiles | 59e18dc | 2020-07-22 18:18:12 -0400 | [diff] [blame] | 157 | std::unique_ptr<GrFragmentProcessor> fCoverageFP; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | #endif |