blob: bfe3383e16b9d57fa37f7c79f13dc3aedfc52b9f [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
Hal Canary6b20a552017-02-07 14:09:38 -050011#include "GrFragmentProcessor.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070012#include "GrScissorState.h"
13#include "GrWindowRectsState.h"
Brian Salomond818ebf2018-07-02 14:08:49 +000014
Robert Phillipsa4f792d2017-06-28 08:40:11 -040015#include "SkClipStack.h"
16
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; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -070029 const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; }
Brian Salomonc3833b42018-07-09 18:23:58 +000030 uint32_t stencilStackID() const { return fStencilStackID; }
31 bool hasStencilClip() const { return SkClipStack::kInvalidGenID != fStencilStackID; }
csmartdalton28341fa2016-08-17 10:00:21 -070032
33 /**
34 * Intersects the applied clip with the provided rect. Returns false if the draw became empty.
Brian Salomon97180af2017-03-14 13:42:58 -040035 * 'clippedDrawBounds' will be intersected with 'irect'. This returns false if the clip becomes
36 * empty or the draw no longer intersects the clip. In either case the draw can be skipped.
csmartdalton28341fa2016-08-17 10:00:21 -070037 */
Brian Salomon97180af2017-03-14 13:42:58 -040038 bool addScissor(const SkIRect& irect, SkRect* clippedDrawBounds) {
39 return fScissorState.intersect(irect) && clippedDrawBounds->intersect(SkRect::Make(irect));
csmartdalton28341fa2016-08-17 10:00:21 -070040 }
41
csmartdaltonbf4a8f92016-09-06 10:01:06 -070042 void addWindowRectangles(const GrWindowRectsState& windowState) {
43 SkASSERT(!fWindowRectsState.enabled());
44 fWindowRectsState = windowState;
45 }
46
Brian Salomon9a767722017-03-13 17:57:28 -040047 void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070048 SkASSERT(!fWindowRectsState.enabled());
Brian Salomon9a767722017-03-13 17:57:28 -040049 fWindowRectsState.set(windows, mode);
csmartdalton28341fa2016-08-17 10:00:21 -070050 }
51
Brian Salomonc3833b42018-07-09 18:23:58 +000052 void addStencilClip(uint32_t stencilStackID) {
53 SkASSERT(SkClipStack::kInvalidGenID == fStencilStackID);
54 fStencilStackID = stencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070055 }
56
57 bool doesClip() const {
Brian Salomond818ebf2018-07-02 14:08:49 +000058 return fScissorState.enabled() || this->hasStencilClip() || fWindowRectsState.enabled();
Chris Daltonbbfd5162017-11-07 13:35:22 -070059 }
60
61 bool operator==(const GrAppliedHardClip& that) const {
Brian Salomonc3833b42018-07-09 18:23:58 +000062 return fScissorState == that.fScissorState &&
63 fWindowRectsState == that.fWindowRectsState &&
64 fStencilStackID == that.fStencilStackID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070065 }
66 bool operator!=(const GrAppliedHardClip& that) const { return !(*this == that); }
67
68private:
69 GrScissorState fScissorState;
70 GrWindowRectsState fWindowRectsState;
Brian Salomonc3833b42018-07-09 18:23:58 +000071 uint32_t fStencilStackID = SkClipStack::kInvalidGenID;
Chris Daltonbbfd5162017-11-07 13:35:22 -070072};
73
74/**
75 * Produced by GrClip. It provides a set of modifications to GrPipeline that implement the clip.
76 */
77class GrAppliedClip {
78public:
79 GrAppliedClip() = default;
80 GrAppliedClip(GrAppliedClip&& that) = default;
81 GrAppliedClip(const GrAppliedClip&) = delete;
82
83 const GrScissorState& scissorState() const { return fHardClip.scissorState(); }
84 const GrWindowRectsState& windowRectsState() const { return fHardClip.windowRectsState(); }
Brian Salomonc3833b42018-07-09 18:23:58 +000085 uint32_t stencilStackID() const { return fHardClip.stencilStackID(); }
Chris Daltonbbfd5162017-11-07 13:35:22 -070086 bool hasStencilClip() const { return fHardClip.hasStencilClip(); }
87 int numClipCoverageFragmentProcessors() const { return fClipCoverageFPs.count(); }
88 const GrFragmentProcessor* clipCoverageFragmentProcessor(int i) const {
89 SkASSERT(fClipCoverageFPs[i]);
90 return fClipCoverageFPs[i].get();
91 }
92 std::unique_ptr<const GrFragmentProcessor> detachClipCoverageFragmentProcessor(int i) {
93 SkASSERT(fClipCoverageFPs[i]);
94 return std::move(fClipCoverageFPs[i]);
95 }
96
97 GrAppliedHardClip& hardClip() { return fHardClip; }
98
Brian Salomonaff329b2017-08-11 09:40:37 -040099 void addCoverageFP(std::unique_ptr<GrFragmentProcessor> fp) {
Chris Dalton69824002017-10-31 00:37:52 -0600100 SkASSERT(fp);
101 fClipCoverageFPs.push_back(std::move(fp));
csmartdalton28341fa2016-08-17 10:00:21 -0700102 }
103
Brian Salomon54d212e2017-03-21 14:22:38 -0400104 bool doesClip() const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700105 return fHardClip.doesClip() || !fClipCoverageFPs.empty();
Brian Salomon54d212e2017-03-21 14:22:38 -0400106 }
107
108 bool operator==(const GrAppliedClip& that) const {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700109 if (fHardClip != that.fHardClip ||
110 fClipCoverageFPs.count() != that.fClipCoverageFPs.count()) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400111 return false;
112 }
Chris Dalton69824002017-10-31 00:37:52 -0600113 for (int i = 0; i < fClipCoverageFPs.count(); ++i) {
114 if (!fClipCoverageFPs[i] || !that.fClipCoverageFPs[i]) {
115 if (fClipCoverageFPs[i] == that.fClipCoverageFPs[i]) {
116 continue; // Both are null.
117 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400118 return false;
119 }
Chris Dalton69824002017-10-31 00:37:52 -0600120 if (!fClipCoverageFPs[i]->isEqual(*that.fClipCoverageFPs[i])) {
121 return false;
122 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400123 }
Chris Dalton69824002017-10-31 00:37:52 -0600124 return true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400125 }
126 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
127
Robert Phillipsf1748f52017-09-14 14:11:24 -0400128 void visitProxies(const std::function<void(GrSurfaceProxy*)>& func) const {
Chris Dalton69824002017-10-31 00:37:52 -0600129 for (const std::unique_ptr<GrFragmentProcessor>& fp : fClipCoverageFPs) {
130 if (fp) { // This might be called after detach.
131 fp->visitProxies(func);
132 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400133 }
134 }
135
csmartdalton28341fa2016-08-17 10:00:21 -0700136private:
Chris Daltonbbfd5162017-11-07 13:35:22 -0700137 GrAppliedHardClip fHardClip;
Chris Dalton69824002017-10-31 00:37:52 -0600138 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fClipCoverageFPs;
csmartdalton28341fa2016-08-17 10:00:21 -0700139};
140
141#endif