blob: 9a8f8b452239354591474a0f6c196cf60a102699 [file] [log] [blame]
csmartdalton28341fa2016-08-17 10:00:21 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrFragmentProcessor.h"
12#include "src/gpu/GrScissorState.h"
13#include "src/gpu/GrWindowRectsState.h"
Brian Salomond818ebf2018-07-02 14:08:49 +000014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkClipStack.h"
Robert Phillipsa4f792d2017-06-28 08:40:11 -040016
Brian Salomond818ebf2018-07-02 14:08:49 +000017
csmartdalton28341fa2016-08-17 10:00:21 -070018/**
Chris Daltonbbfd5162017-11-07 13:35:22 -070019 * Produced by GrHardClip. It provides a set of modifications to the hardware drawing state that
20 * implement the clip.
csmartdalton28341fa2016-08-17 10:00:21 -070021 */
Chris Daltonbbfd5162017-11-07 13:35:22 -070022class GrAppliedHardClip {
csmartdalton28341fa2016-08-17 10:00:21 -070023public:
Michael Ludwig4926b072020-06-04 19:39:42 +000024 static const GrAppliedHardClip& Disabled() {
Michael Ludwigd1d997e2020-06-04 15:52:44 -040025 // 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 Ludwig4926b072020-06-04 19:39:42 +000029 return kDisabled;
Chris Daltonaa0e45c2020-03-16 10:05:11 -060030 }
31
Michael Ludwigd1d997e2020-06-04 15:52:44 -040032 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 Daltonbbfd5162017-11-07 13:35:22 -070038 GrAppliedHardClip(GrAppliedHardClip&& that) = default;
Chris Dalton5e1545f2020-09-25 16:24:03 -060039 explicit GrAppliedHardClip(const GrAppliedHardClip&) = default;
Brian Salomon54d212e2017-03-21 14:22:38 -040040
csmartdalton28341fa2016-08-17 10:00:21 -070041 const GrScissorState& scissorState() const { return fScissorState; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -070042 const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; }
Brian Salomonc3833b42018-07-09 18:23:58 +000043 uint32_t stencilStackID() const { return fStencilStackID; }
44 bool hasStencilClip() const { return SkClipStack::kInvalidGenID != fStencilStackID; }
csmartdalton28341fa2016-08-17 10:00:21 -070045
46 /**
47 * Intersects the applied clip with the provided rect. Returns false if the draw became empty.
48 */
Chris Dalton92b35672021-04-01 11:39:21 -060049 bool SK_WARN_UNUSED_RESULT addScissor(const SkIRect& scissor) {
50 return fScissorState.intersect(scissor);
csmartdalton28341fa2016-08-17 10:00:21 -070051 }
52
Michael Ludwig6397e802020-08-05 15:50:01 -040053 void setScissor(const SkIRect& irect) {
54 fScissorState.set(irect);
55 }
56
csmartdaltonbf4a8f92016-09-06 10:01:06 -070057 void addWindowRectangles(const GrWindowRectsState& windowState) {
58 SkASSERT(!fWindowRectsState.enabled());
59 fWindowRectsState = windowState;
60 }
61
Brian Salomon9a767722017-03-13 17:57:28 -040062 void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070063 SkASSERT(!fWindowRectsState.enabled());
Brian Salomon9a767722017-03-13 17:57:28 -040064 fWindowRectsState.set(windows, mode);
csmartdalton28341fa2016-08-17 10:00:21 -070065 }
66
Brian Salomonc3833b42018-07-09 18:23:58 +000067 void addStencilClip(uint32_t stencilStackID) {
68 SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID);
69 fStencilStackID = stencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070070 }
71
72 bool doesClip() const {
Brian Salomond818ebf2018-07-02 14:08:49 +000073 return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled();
Chris Daltonbbfd5162017-11-07 13:35:22 -070074 }
75
76 bool operator==(const GrAppliedHardClip& that) const {
Brian Salomonc3833b42018-07-09 18:23:58 +000077 return fScissorState == that.fScissorState &&
78 fWindowRectsState == that.fWindowRectsState &&
79 fStencilStackID == that.fStencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070080 }
81 bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); }
82
83private:
84 GrScissorState fScissorState;
85 GrWindowRectsState fWindowRectsState;
Brian Salomonc3833b42018-07-09 18:23:58 +000086 uint32_t fStencilStackID = SkClipStack::kInvalidGenID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070087};
88
89/**
90 * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip.
91 */
92class GrAppliedClip {
93public:
Michael Ludwigd1d997e2020-06-04 15:52:44 -040094 static GrAppliedClip Disabled() {
95 return GrAppliedClip({1 << 29, 1 << 29});
96 }
97
98 GrAppliedClip(const SkISize& rtDims) : fHardClip(rtDims) {}
99 GrAppliedClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims)
100 : fHardClip(logicalRTDims, backingStoreDims) {}
101
Chris Daltonbbfd5162017-11-07 13:35:22 -0700102 GrAppliedClip(GrAppliedClip&& that) = default;
103 GrAppliedClip(const GrAppliedClip&) = delete;
104
105 const GrScissorState& scissorState() const { return fHardClip.scissorState(); }
106 const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); }
Brian Salomonc3833b42018-07-09 18:23:58 +0000107 uint32_t stencilStackID() const { return fHardClip.stencilStackID(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700108 bool hasStencilClip() const { return fHardClip.hasStencilClip(); }
John Stiles59e18dc2020-07-22 18:18:12 -0400109 int hasCoverageFragmentProcessor() const { return fCoverageFP != nullptr; }
110 const GrFragmentProcessor* coverageFragmentProcessor() const {
111 SkASSERT(fCoverageFP != nullptr);
112 return fCoverageFP.get();
Chris Daltonbbfd5162017-11-07 13:35:22 -0700113 }
John Stilesd3feb6f2020-07-23 18:18:12 -0400114 std::unique_ptr<GrFragmentProcessor> detachCoverageFragmentProcessor() {
John Stiles59e18dc2020-07-22 18:18:12 -0400115 SkASSERT(fCoverageFP != nullptr);
116 return std::move(fCoverageFP);
Chris Daltonbbfd5162017-11-07 13:35:22 -0700117 }
118
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600119 const GrAppliedHardClip& hardClip() const { return fHardClip; }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700120 GrAppliedHardClip& hardClip() { return fHardClip; }
121
Brian Salomonaff329b2017-08-11 09:40:37 -0400122 void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) {
John Stiles59e18dc2020-07-22 18:18:12 -0400123 if (fCoverageFP == nullptr) {
124 fCoverageFP = std::move(fp);
125 } else {
John Stiles024d7452020-07-22 19:07:15 -0400126 // Compose this coverage FP with the previously-added coverage.
Brian Osmanc5422d02021-03-12 16:06:35 -0500127 fCoverageFP = GrFragmentProcessor::Compose(std::move(fp), std::move(fCoverageFP));
John Stiles59e18dc2020-07-22 18:18:12 -0400128 }
csmartdalton28341fa2016-08-17 10:00:21 -0700129 }
130
Brian Salomon54d212e2017-03-21 14:22:38 -0400131 bool doesClip() const {
John Stiles59e18dc2020-07-22 18:18:12 -0400132 return fHardClip.doesClip() || fCoverageFP != nullptr;
Brian Salomon54d212e2017-03-21 14:22:38 -0400133 }
134
135 bool operator==(const GrAppliedClip& that) const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700136 if (fHardClip != that.fHardClip ||
John Stiles59e18dc2020-07-22 18:18:12 -0400137 this->hasCoverageFragmentProcessor() != that.hasCoverageFragmentProcessor()) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400138 return false;
139 }
John Stiles59e18dc2020-07-22 18:18:12 -0400140 if (fCoverageFP != nullptr && !fCoverageFP->isEqual(*that.fCoverageFP)) {
141 return false;
Brian Salomon54d212e2017-03-21 14:22:38 -0400142 }
Chris Dalton69824002017-10-31 00:37:52 -0600143 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400144 }
145 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
146
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600147 void visitProxies(const GrOp::VisitProxyFunc& func) const {
John Stiles59e18dc2020-07-22 18:18:12 -0400148 if (fCoverageFP != nullptr) {
149 fCoverageFP->visitProxies(func);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400150 }
151 }
152
csmartdalton28341fa2016-08-17 10:00:21 -0700153private:
Chris Daltonbbfd5162017-11-07 13:35:22 -0700154 GrAppliedHardClip fHardClip;
John Stiles59e18dc2020-07-22 18:18:12 -0400155 std::unique_ptr<GrFragmentProcessor> fCoverageFP;
csmartdalton28341fa2016-08-17 10:00:21 -0700156};
157
158#endif