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 | 3b923a8 | 2020-06-04 13:47:58 -0400 | [diff] [blame^] | 24 | GrAppliedHardClip(const SkISize& rtDims) : fScissorState(rtDims) {} |
| 25 | GrAppliedHardClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims) |
| 26 | : fScissorState(backingStoreDims) { |
| 27 | fScissorState.set(SkIRect::MakeSize(logicalRTDims)); |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 28 | } |
| 29 | |
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 | 3b923a8 | 2020-06-04 13:47:58 -0400 | [diff] [blame^] | 84 | static GrAppliedClip Disabled() { |
| 85 | // The size doesn't really matter here since it's returned as const& so an actual scissor |
| 86 | // will never be set on it, and applied clips are not used to query or bounds test like |
| 87 | /// the GrClip is. |
| 88 | return GrAppliedClip({1 << 29, 1 << 29}); |
| 89 | } |
| 90 | |
| 91 | GrAppliedClip(const SkISize& rtDims) : fHardClip(rtDims) {} |
| 92 | GrAppliedClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims) |
| 93 | : fHardClip(logicalRTDims, backingStoreDims) {} |
| 94 | |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 95 | GrAppliedClip(GrAppliedClip&& that) = default; |
| 96 | GrAppliedClip(const GrAppliedClip&) = delete; |
| 97 | |
| 98 | const GrScissorState& scissorState() const { return fHardClip.scissorState(); } |
| 99 | const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); } |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 100 | uint32_t stencilStackID() const { return fHardClip.stencilStackID(); } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 101 | bool hasStencilClip() const { return fHardClip.hasStencilClip(); } |
| 102 | int numClipCoverageFragmentProcessors() const { return fClipCoverageFPs.count(); } |
| 103 | const GrFragmentProcessor* clipCoverageFragmentProcessor(int i) const { |
| 104 | SkASSERT(fClipCoverageFPs[i]); |
| 105 | return fClipCoverageFPs[i].get(); |
| 106 | } |
| 107 | std::unique_ptr<const GrFragmentProcessor> detachClipCoverageFragmentProcessor(int i) { |
| 108 | SkASSERT(fClipCoverageFPs[i]); |
| 109 | return std::move(fClipCoverageFPs[i]); |
| 110 | } |
| 111 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 112 | const GrAppliedHardClip& hardClip() const { return fHardClip; } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 113 | GrAppliedHardClip& hardClip() { return fHardClip; } |
| 114 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 115 | void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) { |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 116 | SkASSERT(fp); |
| 117 | fClipCoverageFPs.push_back(std::move(fp)); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 120 | bool doesClip() const { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 121 | return fHardClip.doesClip() || !fClipCoverageFPs.empty(); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool operator==(const GrAppliedClip& that) const { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 125 | if (fHardClip != that.fHardClip || |
| 126 | fClipCoverageFPs.count() != that.fClipCoverageFPs.count()) { |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 127 | return false; |
| 128 | } |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 129 | for (int i = 0; i < fClipCoverageFPs.count(); ++i) { |
| 130 | if (!fClipCoverageFPs[i] || !that.fClipCoverageFPs[i]) { |
| 131 | if (fClipCoverageFPs[i] == that.fClipCoverageFPs[i]) { |
| 132 | continue; // Both are null. |
| 133 | } |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 134 | return false; |
| 135 | } |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 136 | if (!fClipCoverageFPs[i]->isEqual(*that.fClipCoverageFPs[i])) { |
| 137 | return false; |
| 138 | } |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 139 | } |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 140 | return true; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 141 | } |
| 142 | bool operator!=(const GrAppliedClip& that) const { return !(*this == that); } |
| 143 | |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 144 | void visitProxies(const GrOp::VisitProxyFunc& func) const { |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 145 | for (const std::unique_ptr<GrFragmentProcessor>& fp : fClipCoverageFPs) { |
| 146 | if (fp) { // This might be called after detach. |
| 147 | fp->visitProxies(func); |
| 148 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 152 | private: |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 153 | GrAppliedHardClip fHardClip; |
Chris Dalton | 6982400 | 2017-10-31 00:37:52 -0600 | [diff] [blame] | 154 | SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fClipCoverageFPs; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | #endif |