blob: 594a627987d1e668c357c406a6520fa93fe57b53 [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() {
25 static GrAppliedHardClip kDisabled;
26 return kDisabled;
Chris Daltonaa0e45c2020-03-16 10:05:11 -060027 }
28
Michael Ludwig4926b072020-06-04 19:39:42 +000029 GrAppliedHardClip() = default;
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 Ludwig4926b072020-06-04 19:39:42 +000084 GrAppliedClip() = default;
Chris Daltonbbfd5162017-11-07 13:35:22 -070085 GrAppliedClip(GrAppliedClip&& that) = default;
86 GrAppliedClip(const GrAppliedClip&) = delete;
87
88 const GrScissorState& scissorState() const { return fHardClip.scissorState(); }
89 const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); }
Brian Salomonc3833b42018-07-09 18:23:58 +000090 uint32_t stencilStackID() const { return fHardClip.stencilStackID(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -070091 bool hasStencilClip() const { return fHardClip.hasStencilClip(); }
92 int numClipCoverageFragmentProcessors() const { return fClipCoverageFPs.count(); }
93 const GrFragmentProcessor* clipCoverageFragmentProcessor(int i) const {
94 SkASSERT(fClipCoverageFPs[i]);
95 return fClipCoverageFPs[i].get();
96 }
97 std::unique_ptr<const GrFragmentProcessor> detachClipCoverageFragmentProcessor(int i) {
98 SkASSERT(fClipCoverageFPs[i]);
99 return std::move(fClipCoverageFPs[i]);
100 }
101
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600102 const GrAppliedHardClip& hardClip() const { return fHardClip; }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700103 GrAppliedHardClip& hardClip() { return fHardClip; }
104
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) {
Chris Dalton69824002017-10-31 00:37:52 -0600106 SkASSERT(fp);
107 fClipCoverageFPs.push_back(std::move(fp));
csmartdalton28341fa2016-08-17 10:00:21 -0700108 }
109
Brian Salomon54d212e2017-03-21 14:22:38 -0400110 bool doesClip() const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700111 return fHardClip.doesClip() || !fClipCoverageFPs.empty();
Brian Salomon54d212e2017-03-21 14:22:38 -0400112 }
113
114 bool operator==(const GrAppliedClip& that) const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700115 if (fHardClip != that.fHardClip ||
116 fClipCoverageFPs.count() != that.fClipCoverageFPs.count()) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400117 return false;
118 }
Chris Dalton69824002017-10-31 00:37:52 -0600119 for (int i = 0; i < fClipCoverageFPs.count(); ++i) {
120 if (!fClipCoverageFPs[i] || !that.fClipCoverageFPs[i]) {
121 if (fClipCoverageFPs[i] == that.fClipCoverageFPs[i]) {
122 continue; // Both are null.
123 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400124 return false;
125 }
Chris Dalton69824002017-10-31 00:37:52 -0600126 if (!fClipCoverageFPs[i]->isEqual(*that.fClipCoverageFPs[i])) {
127 return false;
128 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400129 }
Chris Dalton69824002017-10-31 00:37:52 -0600130 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400131 }
132 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
133
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600134 void visitProxies(const GrOp::VisitProxyFunc& func) const {
Chris Dalton69824002017-10-31 00:37:52 -0600135 for (const std::unique_ptr<GrFragmentProcessor>& fp : fClipCoverageFPs) {
136 if (fp) { // This might be called after detach.
137 fp->visitProxies(func);
138 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400139 }
140 }
141
csmartdalton28341fa2016-08-17 10:00:21 -0700142private:
Chris Daltonbbfd5162017-11-07 13:35:22 -0700143 GrAppliedHardClip fHardClip;
Chris Dalton69824002017-10-31 00:37:52 -0600144 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fClipCoverageFPs;
csmartdalton28341fa2016-08-17 10:00:21 -0700145};
146
147#endif