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