blob: e8ccc2ddbf4ad49bb02c944b31e454305f1eb2fa [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.
Derek Sollenberger396cd1d2021-04-02 15:44:36 +000048 * 'clippedDrawBounds' will be intersected with 'irect'. This returns false if the clip becomes
49 * empty or the draw no longer intersects the clip. In either case the draw can be skipped.
csmartdalton28341fa2016-08-17 10:00:21 -070050 */
Derek Sollenberger396cd1d2021-04-02 15:44:36 +000051 bool addScissor(const SkIRect& irect, SkRect* clippedDrawBounds) {
52 return fScissorState.intersect(irect) && clippedDrawBounds->intersect(SkRect::Make(irect));
csmartdalton28341fa2016-08-17 10:00:21 -070053 }
54
Michael Ludwig6397e802020-08-05 15:50:01 -040055 void setScissor(const SkIRect& irect) {
56 fScissorState.set(irect);
57 }
58
csmartdaltonbf4a8f92016-09-06 10:01:06 -070059 void addWindowRectangles(const GrWindowRectsState& windowState) {
60 SkASSERT(!fWindowRectsState.enabled());
61 fWindowRectsState = windowState;
62 }
63
Brian Salomon9a767722017-03-13 17:57:28 -040064 void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070065 SkASSERT(!fWindowRectsState.enabled());
Brian Salomon9a767722017-03-13 17:57:28 -040066 fWindowRectsState.set(windows, mode);
csmartdalton28341fa2016-08-17 10:00:21 -070067 }
68
Brian Salomonc3833b42018-07-09 18:23:58 +000069 void addStencilClip(uint32_t stencilStackID) {
70 SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID);
71 fStencilStackID = stencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070072 }
73
74 bool doesClip() const {
Brian Salomond818ebf2018-07-02 14:08:49 +000075 return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled();
Chris Daltonbbfd5162017-11-07 13:35:22 -070076 }
77
78 bool operator==(const GrAppliedHardClip& that) const {
Brian Salomonc3833b42018-07-09 18:23:58 +000079 return fScissorState == that.fScissorState &&
80 fWindowRectsState == that.fWindowRectsState &&
81 fStencilStackID == that.fStencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070082 }
83 bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); }
84
85private:
86 GrScissorState fScissorState;
87 GrWindowRectsState fWindowRectsState;
Brian Salomonc3833b42018-07-09 18:23:58 +000088 uint32_t fStencilStackID = SkClipStack::kInvalidGenID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070089};
90
91/**
92 * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip.
93 */
94class GrAppliedClip {
95public:
Michael Ludwigd1d997e2020-06-04 15:52:44 -040096 static GrAppliedClip Disabled() {
97 return GrAppliedClip({1 << 29, 1 << 29});
98 }
99
100 GrAppliedClip(const SkISize& rtDims) : fHardClip(rtDims) {}
101 GrAppliedClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims)
102 : fHardClip(logicalRTDims, backingStoreDims) {}
103
Chris Daltonbbfd5162017-11-07 13:35:22 -0700104 GrAppliedClip(GrAppliedClip&& that) = default;
105 GrAppliedClip(const GrAppliedClip&) = delete;
106
107 const GrScissorState& scissorState() const { return fHardClip.scissorState(); }
108 const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); }
Brian Salomonc3833b42018-07-09 18:23:58 +0000109 uint32_t stencilStackID() const { return fHardClip.stencilStackID(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700110 bool hasStencilClip() const { return fHardClip.hasStencilClip(); }
John Stiles59e18dc2020-07-22 18:18:12 -0400111 int hasCoverageFragmentProcessor() const { return fCoverageFP != nullptr; }
112 const GrFragmentProcessor* coverageFragmentProcessor() const {
113 SkASSERT(fCoverageFP != nullptr);
114 return fCoverageFP.get();
Chris Daltonbbfd5162017-11-07 13:35:22 -0700115 }
John Stilesd3feb6f2020-07-23 18:18:12 -0400116 std::unique_ptr<GrFragmentProcessor> detachCoverageFragmentProcessor() {
John Stiles59e18dc2020-07-22 18:18:12 -0400117 SkASSERT(fCoverageFP != nullptr);
118 return std::move(fCoverageFP);
Chris Daltonbbfd5162017-11-07 13:35:22 -0700119 }
120
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600121 const GrAppliedHardClip& hardClip() const { return fHardClip; }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700122 GrAppliedHardClip& hardClip() { return fHardClip; }
123
Brian Salomonaff329b2017-08-11 09:40:37 -0400124 void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) {
John Stiles59e18dc2020-07-22 18:18:12 -0400125 if (fCoverageFP == nullptr) {
126 fCoverageFP = std::move(fp);
127 } else {
John Stiles024d7452020-07-22 19:07:15 -0400128 // Compose this coverage FP with the previously-added coverage.
Brian Osmanc5422d02021-03-12 16:06:35 -0500129 fCoverageFP = GrFragmentProcessor::Compose(std::move(fp), std::move(fCoverageFP));
John Stiles59e18dc2020-07-22 18:18:12 -0400130 }
csmartdalton28341fa2016-08-17 10:00:21 -0700131 }
132
Brian Salomon54d212e2017-03-21 14:22:38 -0400133 bool doesClip() const {
John Stiles59e18dc2020-07-22 18:18:12 -0400134 return fHardClip.doesClip() || fCoverageFP != nullptr;
Brian Salomon54d212e2017-03-21 14:22:38 -0400135 }
136
137 bool operator==(const GrAppliedClip& that) const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700138 if (fHardClip != that.fHardClip ||
John Stiles59e18dc2020-07-22 18:18:12 -0400139 this->hasCoverageFragmentProcessor() != that.hasCoverageFragmentProcessor()) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400140 return false;
141 }
John Stiles59e18dc2020-07-22 18:18:12 -0400142 if (fCoverageFP != nullptr && !fCoverageFP->isEqual(*that.fCoverageFP)) {
143 return false;
Brian Salomon54d212e2017-03-21 14:22:38 -0400144 }
Chris Dalton69824002017-10-31 00:37:52 -0600145 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400146 }
147 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
148
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600149 void visitProxies(const GrOp::VisitProxyFunc& func) const {
John Stiles59e18dc2020-07-22 18:18:12 -0400150 if (fCoverageFP != nullptr) {
151 fCoverageFP->visitProxies(func);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400152 }
153 }
154
csmartdalton28341fa2016-08-17 10:00:21 -0700155private:
Chris Daltonbbfd5162017-11-07 13:35:22 -0700156 GrAppliedHardClip fHardClip;
John Stiles59e18dc2020-07-22 18:18:12 -0400157 std::unique_ptr<GrFragmentProcessor> fCoverageFP;
csmartdalton28341fa2016-08-17 10:00:21 -0700158};
159
160#endif