blob: a7347ea926b44ecf00ebab4128d8f41b104a5c17 [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;
39 GrAppliedHardClip(const GrAppliedHardClip&) = delete;
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.
Brian Salomon97180af2017-03-14 13:42:58 -040048 * '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 */
Brian Salomon97180af2017-03-14 13:42:58 -040051 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
csmartdaltonbf4a8f92016-09-06 10:01:06 -070055 void addWindowRectangles(const GrWindowRectsState& windowState) {
56 SkASSERT(!fWindowRectsState.enabled());
57 fWindowRectsState = windowState;
58 }
59
Brian Salomon9a767722017-03-13 17:57:28 -040060 void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070061 SkASSERT(!fWindowRectsState.enabled());
Brian Salomon9a767722017-03-13 17:57:28 -040062 fWindowRectsState.set(windows, mode);
csmartdalton28341fa2016-08-17 10:00:21 -070063 }
64
Brian Salomonc3833b42018-07-09 18:23:58 +000065 void addStencilClip(uint32_t stencilStackID) {
66 SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID);
67 fStencilStackID = stencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070068 }
69
70 bool doesClip() const {
Brian Salomond818ebf2018-07-02 14:08:49 +000071 return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled();
Chris Daltonbbfd5162017-11-07 13:35:22 -070072 }
73
74 bool operator==(const GrAppliedHardClip& that) const {
Brian Salomonc3833b42018-07-09 18:23:58 +000075 return fScissorState == that.fScissorState &&
76 fWindowRectsState == that.fWindowRectsState &&
77 fStencilStackID == that.fStencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070078 }
79 bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); }
80
81private:
82 GrScissorState fScissorState;
83 GrWindowRectsState fWindowRectsState;
Brian Salomonc3833b42018-07-09 18:23:58 +000084 uint32_t fStencilStackID = SkClipStack::kInvalidGenID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070085};
86
87/**
88 * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip.
89 */
90class GrAppliedClip {
91public:
Michael Ludwigd1d997e2020-06-04 15:52:44 -040092 static GrAppliedClip Disabled() {
93 return GrAppliedClip({1 << 29, 1 << 29});
94 }
95
96 GrAppliedClip(const SkISize& rtDims) : fHardClip(rtDims) {}
97 GrAppliedClip(const SkISize& logicalRTDims, const SkISize& backingStoreDims)
98 : fHardClip(logicalRTDims, backingStoreDims) {}
99
Chris Daltonbbfd5162017-11-07 13:35:22 -0700100 GrAppliedClip(GrAppliedClip&& that) = default;
101 GrAppliedClip(const GrAppliedClip&) = delete;
102
103 const GrScissorState& scissorState() const { return fHardClip.scissorState(); }
104 const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); }
Brian Salomonc3833b42018-07-09 18:23:58 +0000105 uint32_t stencilStackID() const { return fHardClip.stencilStackID(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700106 bool hasStencilClip() const { return fHardClip.hasStencilClip(); }
John Stiles59e18dc2020-07-22 18:18:12 -0400107 int hasCoverageFragmentProcessor() const { return fCoverageFP != nullptr; }
108 const GrFragmentProcessor* coverageFragmentProcessor() const {
109 SkASSERT(fCoverageFP != nullptr);
110 return fCoverageFP.get();
Chris Daltonbbfd5162017-11-07 13:35:22 -0700111 }
John Stiles59e18dc2020-07-22 18:18:12 -0400112 std::unique_ptr<const GrFragmentProcessor> detachCoverageFragmentProcessor() {
113 SkASSERT(fCoverageFP != nullptr);
114 return std::move(fCoverageFP);
Chris Daltonbbfd5162017-11-07 13:35:22 -0700115 }
116
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600117 const GrAppliedHardClip& hardClip() const { return fHardClip; }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700118 GrAppliedHardClip& hardClip() { return fHardClip; }
119
Brian Salomonaff329b2017-08-11 09:40:37 -0400120 void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) {
John Stiles59e18dc2020-07-22 18:18:12 -0400121 if (fCoverageFP == nullptr) {
122 fCoverageFP = std::move(fp);
123 } else {
John Stiles024d7452020-07-22 19:07:15 -0400124 // Compose this coverage FP with the previously-added coverage.
125 fCoverageFP = GrFragmentProcessor::Compose(std::move(fCoverageFP), std::move(fp));
John Stiles59e18dc2020-07-22 18:18:12 -0400126 }
csmartdalton28341fa2016-08-17 10:00:21 -0700127 }
128
Brian Salomon54d212e2017-03-21 14:22:38 -0400129 bool doesClip() const {
John Stiles59e18dc2020-07-22 18:18:12 -0400130 return fHardClip.doesClip() || fCoverageFP != nullptr;
Brian Salomon54d212e2017-03-21 14:22:38 -0400131 }
132
133 bool operator==(const GrAppliedClip& that) const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700134 if (fHardClip != that.fHardClip ||
John Stiles59e18dc2020-07-22 18:18:12 -0400135 this->hasCoverageFragmentProcessor() != that.hasCoverageFragmentProcessor()) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400136 return false;
137 }
John Stiles59e18dc2020-07-22 18:18:12 -0400138 if (fCoverageFP != nullptr && !fCoverageFP->isEqual(*that.fCoverageFP)) {
139 return false;
Brian Salomon54d212e2017-03-21 14:22:38 -0400140 }
Chris Dalton69824002017-10-31 00:37:52 -0600141 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400142 }
143 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
144
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600145 void visitProxies(const GrOp::VisitProxyFunc& func) const {
John Stiles59e18dc2020-07-22 18:18:12 -0400146 if (fCoverageFP != nullptr) {
147 fCoverageFP->visitProxies(func);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400148 }
149 }
150
csmartdalton28341fa2016-08-17 10:00:21 -0700151private:
Chris Daltonbbfd5162017-11-07 13:35:22 -0700152 GrAppliedHardClip fHardClip;
John Stiles59e18dc2020-07-22 18:18:12 -0400153 std::unique_ptr<GrFragmentProcessor> fCoverageFP;
csmartdalton28341fa2016-08-17 10:00:21 -0700154};
155
156#endif