blob: 5983d31c0a0613652d15c1ea67163b5d98a3244f [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:
Chris Daltonbbfd5162017-11-07 13:35:22 -070024 GrAppliedHardClip() = default;
25 GrAppliedHardClip(GrAppliedHardClip&& that) = default;
26 GrAppliedHardClip(const GrAppliedHardClip&) = delete;
Brian Salomon54d212e2017-03-21 14:22:38 -040027
csmartdalton28341fa2016-08-17 10:00:21 -070028 const GrScissorState& scissorState() const { return fScissorState; }
Chris Dalton012f8492020-03-05 11:49:15 -070029 const SkIRect* scissorRectIfEnabled() const {
30 return fScissorState.enabled() ? &fScissorState.rect() : nullptr;
31 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -070032 const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; }
Brian Salomonc3833b42018-07-09 18:23:58 +000033 uint32_t stencilStackID() const { return fStencilStackID; }
34 bool hasStencilClip() const { return SkClipStack::kInvalidGenID != fStencilStackID; }
csmartdalton28341fa2016-08-17 10:00:21 -070035
36 /**
37 * Intersects the applied clip with the provided rect. Returns false if the draw became empty.
Brian Salomon97180af2017-03-14 13:42:58 -040038 * 'clippedDrawBounds' will be intersected with 'irect'. This returns false if the clip becomes
39 * empty or the draw no longer intersects the clip. In either case the draw can be skipped.
csmartdalton28341fa2016-08-17 10:00:21 -070040 */
Brian Salomon97180af2017-03-14 13:42:58 -040041 bool addScissor(const SkIRect& irect, SkRect* clippedDrawBounds) {
42 return fScissorState.intersect(irect) && clippedDrawBounds->intersect(SkRect::Make(irect));
csmartdalton28341fa2016-08-17 10:00:21 -070043 }
44
csmartdaltonbf4a8f92016-09-06 10:01:06 -070045 void addWindowRectangles(const GrWindowRectsState& windowState) {
46 SkASSERT(!fWindowRectsState.enabled());
47 fWindowRectsState = windowState;
48 }
49
Brian Salomon9a767722017-03-13 17:57:28 -040050 void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070051 SkASSERT(!fWindowRectsState.enabled());
Brian Salomon9a767722017-03-13 17:57:28 -040052 fWindowRectsState.set(windows, mode);
csmartdalton28341fa2016-08-17 10:00:21 -070053 }
54
Brian Salomonc3833b42018-07-09 18:23:58 +000055 void addStencilClip(uint32_t stencilStackID) {
56 SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID);
57 fStencilStackID = stencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070058 }
59
60 bool doesClip() const {
Brian Salomond818ebf2018-07-02 14:08:49 +000061 return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled();
Chris Daltonbbfd5162017-11-07 13:35:22 -070062 }
63
64 bool operator==(const GrAppliedHardClip& that) const {
Brian Salomonc3833b42018-07-09 18:23:58 +000065 return fScissorState == that.fScissorState &&
66 fWindowRectsState == that.fWindowRectsState &&
67 fStencilStackID == that.fStencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070068 }
69 bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); }
70
71private:
72 GrScissorState fScissorState;
73 GrWindowRectsState fWindowRectsState;
Brian Salomonc3833b42018-07-09 18:23:58 +000074 uint32_t fStencilStackID = SkClipStack::kInvalidGenID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070075};
76
77/**
78 * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip.
79 */
80class GrAppliedClip {
81public:
82 GrAppliedClip() = default;
83 GrAppliedClip(GrAppliedClip&& that) = default;
84 GrAppliedClip(const GrAppliedClip&) = delete;
85
86 const GrScissorState& scissorState() const { return fHardClip.scissorState(); }
Chris Dalton012f8492020-03-05 11:49:15 -070087 const SkIRect* scissorRectIfEnabled() const { return fHardClip.scissorRectIfEnabled(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -070088 const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); }
Brian Salomonc3833b42018-07-09 18:23:58 +000089 uint32_t stencilStackID() const { return fHardClip.stencilStackID(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -070090 bool hasStencilClip() const { return fHardClip.hasStencilClip(); }
91 int numClipCoverageFragmentProcessors() const { return fClipCoverageFPs.count(); }
92 const GrFragmentProcessor* clipCoverageFragmentProcessor(int i) const {
93 SkASSERT(fClipCoverageFPs[i]);
94 return fClipCoverageFPs[i].get();
95 }
96 std::unique_ptr<const GrFragmentProcessor> detachClipCoverageFragmentProcessor(int i) {
97 SkASSERT(fClipCoverageFPs[i]);
98 return std::move(fClipCoverageFPs[i]);
99 }
100
101 GrAppliedHardClip& hardClip() { return fHardClip; }
102
Brian Salomonaff329b2017-08-11 09:40:37 -0400103 void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) {
Chris Dalton69824002017-10-31 00:37:52 -0600104 SkASSERT(fp);
105 fClipCoverageFPs.push_back(std::move(fp));
csmartdalton28341fa2016-08-17 10:00:21 -0700106 }
107
Brian Salomon54d212e2017-03-21 14:22:38 -0400108 bool doesClip() const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700109 return fHardClip.doesClip() || !fClipCoverageFPs.empty();
Brian Salomon54d212e2017-03-21 14:22:38 -0400110 }
111
112 bool operator==(const GrAppliedClip& that) const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700113 if (fHardClip != that.fHardClip ||
114 fClipCoverageFPs.count() != that.fClipCoverageFPs.count()) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400115 return false;
116 }
Chris Dalton69824002017-10-31 00:37:52 -0600117 for (int i = 0; i < fClipCoverageFPs.count(); ++i) {
118 if (!fClipCoverageFPs[i] || !that.fClipCoverageFPs[i]) {
119 if (fClipCoverageFPs[i] == that.fClipCoverageFPs[i]) {
120 continue; // Both are null.
121 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400122 return false;
123 }
Chris Dalton69824002017-10-31 00:37:52 -0600124 if (!fClipCoverageFPs[i]->isEqual(*that.fClipCoverageFPs[i])) {
125 return false;
126 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400127 }
Chris Dalton69824002017-10-31 00:37:52 -0600128 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400129 }
130 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
131
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600132 void visitProxies(const GrOp::VisitProxyFunc& func) const {
Chris Dalton69824002017-10-31 00:37:52 -0600133 for (const std::unique_ptr<GrFragmentProcessor>& fp : fClipCoverageFPs) {
134 if (fp) { // This might be called after detach.
135 fp->visitProxies(func);
136 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400137 }
138 }
139
csmartdalton28341fa2016-08-17 10:00:21 -0700140private:
Chris Daltonbbfd5162017-11-07 13:35:22 -0700141 GrAppliedHardClip fHardClip;
Chris Dalton69824002017-10-31 00:37:52 -0600142 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fClipCoverageFPs;
csmartdalton28341fa2016-08-17 10:00:21 -0700143};
144
145#endif