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