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; |
| 39 | GrAppliedHardClip(const GrAppliedHardClip&) = delete; |
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. |
Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [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 | */ |
Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [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 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 55 | void addWindowRectangles(const GrWindowRectsState& windowState) { |
| 56 | SkASSERT(!fWindowRectsState.enabled()); |
| 57 | fWindowRectsState = windowState; |
| 58 | } |
| 59 | |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 60 | void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 61 | SkASSERT(!fWindowRectsState.enabled()); |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 62 | fWindowRectsState.set(windows, mode); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 65 | void addStencilClip(uint32_t stencilStackID) { |
| 66 | SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID); |
| 67 | fStencilStackID = stencilStackID; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool doesClip() const { |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 71 | return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled(); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool operator==(const GrAppliedHardClip& that) const { |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 75 | return fScissorState == that.fScissorState && |
| 76 | fWindowRectsState == that.fWindowRectsState && |
| 77 | fStencilStackID == that.fStencilStackID; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 78 | } |
| 79 | bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); } |
| 80 | |
| 81 | private: |
| 82 | GrScissorState fScissorState; |
| 83 | GrWindowRectsState fWindowRectsState; |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 84 | uint32_t fStencilStackID = SkClipStack::kInvalidGenID; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip. |
| 89 | */ |
| 90 | class GrAppliedClip { |
| 91 | public: |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame^] | 92 | static GrAppliedClip Disabled() { |
| 93 | return GrAppliedClip({1 << 29, 1 << 29}); |
| 94 | } |
| 95 | |
| 96 | GrAppliedClip(const SkISize& rtDims) : fHardClip(rtDims) {} |
| 97 | GrAppliedClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims) |
| 98 | : fHardClip(logicalRTDims, backingStoreDims) {} |
| 99 | |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 100 | GrAppliedClip(GrAppliedClip&& that) = default; |
| 101 | GrAppliedClip(const GrAppliedClip&) = delete; |
| 102 | |
| 103 | const GrScissorState& scissorState() const { return fHardClip.scissorState(); } |
| 104 | const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); } |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 105 | uint32_t stencilStackID() const { return fHardClip.stencilStackID(); } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 106 | bool hasStencilClip() const { return fHardClip.hasStencilClip(); } |
| 107 | int numClipCoverageFragmentProcessors() const { return fClipCoverageFPs.count(); } |
| 108 | const GrFragmentProcessor* clipCoverageFragmentProcessor(int i) const { |
| 109 | SkASSERT(fClipCoverageFPs[i]); |
| 110 | return fClipCoverageFPs[i].get(); |
| 111 | } |
| 112 | std::unique_ptr<const GrFragmentProcessor> detachClipCoverageFragmentProcessor(int i) { |
| 113 | SkASSERT(fClipCoverageFPs[i]); |
| 114 | return std::move(fClipCoverageFPs[i]); |
| 115 | } |
| 116 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 117 | const GrAppliedHardClip& hardClip() const { return fHardClip; } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 118 | GrAppliedHardClip& hardClip() { return fHardClip; } |
| 119 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 120 | void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) { |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 121 | SkASSERT(fp); |
| 122 | fClipCoverageFPs.push_back(std::move(fp)); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 125 | bool doesClip() const { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 126 | return fHardClip.doesClip() || !fClipCoverageFPs.empty(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool operator==(const GrAppliedClip& that) const { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 130 | if (fHardClip != that.fHardClip || |
| 131 | fClipCoverageFPs.count() != that.fClipCoverageFPs.count()) { |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 132 | return false; |
| 133 | } |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 134 | for (int i = 0; i < fClipCoverageFPs.count(); ++i) { |
| 135 | if (!fClipCoverageFPs[i] || !that.fClipCoverageFPs[i]) { |
| 136 | if (fClipCoverageFPs[i] == that.fClipCoverageFPs[i]) { |
| 137 | continue; // Both are null. |
| 138 | } |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 139 | return false; |
| 140 | } |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 141 | if (!fClipCoverageFPs[i]->isEqual(*that.fClipCoverageFPs[i])) { |
| 142 | return false; |
| 143 | } |
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 | |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 149 | void visitProxies(const GrOp::VisitProxyFunc& func) const { |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 150 | for (const std::unique_ptr<GrFragmentProcessor>& fp : fClipCoverageFPs) { |
| 151 | if (fp) { // This might be called after detach. |
| 152 | fp->visitProxies(func); |
| 153 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 157 | private: |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 158 | GrAppliedHardClip fHardClip; |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 159 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fClipCoverageFPs; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | #endif |