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