blob: 53aa978f0f31a2dd172204aa212a51ba1ff03ec7 [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 Ludwig3b923a82020-06-04 13:47:58 -040024 GrAppliedHardClip(const SkISize& rtDims) : fScissorState(rtDims) {}
25 GrAppliedHardClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims)
26 : fScissorState(backingStoreDims) {
27 fScissorState.set(SkIRect::MakeSize(logicalRTDims));
Chris Daltonaa0e45c2020-03-16 10:05:11 -060028 }
29
Chris Daltonbbfd5162017-11-07 13:35:22 -070030 GrAppliedHardClip(GrAppliedHardClip&& that) = default;
31 GrAppliedHardClip(const GrAppliedHardClip&) = delete;
Brian Salomon54d212e2017-03-21 14:22:38 -040032
csmartdalton28341fa2016-08-17 10:00:21 -070033 const GrScissorState& scissorState() const { return fScissorState; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -070034 const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; }
Brian Salomonc3833b42018-07-09 18:23:58 +000035 uint32_t stencilStackID() const { return fStencilStackID; }
36 bool hasStencilClip() const { return SkClipStack::kInvalidGenID != fStencilStackID; }
csmartdalton28341fa2016-08-17 10:00:21 -070037
38 /**
39 * Intersects the applied clip with the provided rect. Returns false if the draw became empty.
Brian Salomon97180af2017-03-14 13:42:58 -040040 * '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.
csmartdalton28341fa2016-08-17 10:00:21 -070042 */
Brian Salomon97180af2017-03-14 13:42:58 -040043 bool addScissor(const SkIRect& irect, SkRect* clippedDrawBounds) {
44 return fScissorState.intersect(irect) && clippedDrawBounds->intersect(SkRect::Make(irect));
csmartdalton28341fa2016-08-17 10:00:21 -070045 }
46
csmartdaltonbf4a8f92016-09-06 10:01:06 -070047 void addWindowRectangles(const GrWindowRectsState& windowState) {
48 SkASSERT(!fWindowRectsState.enabled());
49 fWindowRectsState = windowState;
50 }
51
Brian Salomon9a767722017-03-13 17:57:28 -040052 void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070053 SkASSERT(!fWindowRectsState.enabled());
Brian Salomon9a767722017-03-13 17:57:28 -040054 fWindowRectsState.set(windows, mode);
csmartdalton28341fa2016-08-17 10:00:21 -070055 }
56
Brian Salomonc3833b42018-07-09 18:23:58 +000057 void addStencilClip(uint32_t stencilStackID) {
58 SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID);
59 fStencilStackID = stencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070060 }
61
62 bool doesClip() const {
Brian Salomond818ebf2018-07-02 14:08:49 +000063 return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled();
Chris Daltonbbfd5162017-11-07 13:35:22 -070064 }
65
66 bool operator==(const GrAppliedHardClip& that) const {
Brian Salomonc3833b42018-07-09 18:23:58 +000067 return fScissorState == that.fScissorState &&
68 fWindowRectsState == that.fWindowRectsState &&
69 fStencilStackID == that.fStencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070070 }
71 bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); }
72
73private:
74 GrScissorState fScissorState;
75 GrWindowRectsState fWindowRectsState;
Brian Salomonc3833b42018-07-09 18:23:58 +000076 uint32_t fStencilStackID = SkClipStack::kInvalidGenID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070077};
78
79/**
80 * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip.
81 */
82class GrAppliedClip {
83public:
Michael Ludwig3b923a82020-06-04 13:47:58 -040084 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 Daltonbbfd5162017-11-07 13:35:22 -070095 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 Salomonc3833b42018-07-09 18:23:58 +0000100 uint32_t stencilStackID() const { return fHardClip.stencilStackID(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700101 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 Daltonaa0e45c2020-03-16 10:05:11 -0600112 const GrAppliedHardClip& hardClip() const { return fHardClip; }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700113 GrAppliedHardClip& hardClip() { return fHardClip; }
114
Brian Salomonaff329b2017-08-11 09:40:37 -0400115 void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) {
Chris Dalton69824002017-10-31 00:37:52 -0600116 SkASSERT(fp);
117 fClipCoverageFPs.push_back(std::move(fp));
csmartdalton28341fa2016-08-17 10:00:21 -0700118 }
119
Brian Salomon54d212e2017-03-21 14:22:38 -0400120 bool doesClip() const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700121 return fHardClip.doesClip() || !fClipCoverageFPs.empty();
Brian Salomon54d212e2017-03-21 14:22:38 -0400122 }
123
124 bool operator==(const GrAppliedClip& that) const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700125 if (fHardClip != that.fHardClip ||
126 fClipCoverageFPs.count() != that.fClipCoverageFPs.count()) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400127 return false;
128 }
Chris Dalton69824002017-10-31 00:37:52 -0600129 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 Salomon54d212e2017-03-21 14:22:38 -0400134 return false;
135 }
Chris Dalton69824002017-10-31 00:37:52 -0600136 if (!fClipCoverageFPs[i]->isEqual(*that.fClipCoverageFPs[i])) {
137 return false;
138 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400139 }
Chris Dalton69824002017-10-31 00:37:52 -0600140 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400141 }
142 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
143
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600144 void visitProxies(const GrOp::VisitProxyFunc& func) const {
Chris Dalton69824002017-10-31 00:37:52 -0600145 for (const std::unique_ptr<GrFragmentProcessor>& fp : fClipCoverageFPs) {
146 if (fp) { // This might be called after detach.
147 fp->visitProxies(func);
148 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400149 }
150 }
151
csmartdalton28341fa2016-08-17 10:00:21 -0700152private:
Chris Daltonbbfd5162017-11-07 13:35:22 -0700153 GrAppliedHardClip fHardClip;
Chris Dalton69824002017-10-31 00:37:52 -0600154 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fClipCoverageFPs;
csmartdalton28341fa2016-08-17 10:00:21 -0700155};
156
157#endif