blob: 8488dc4d5d6555c08fc55580ece09b6e6f82eb23 [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"
csmartdalton28341fa2016-08-17 10:00:21 -070014
csmartdalton28341fa2016-08-17 10:00:21 -070015/**
16 * Produced by GrClip. It provides a set of modifications to the drawing state that are used to
Brian Salomon25a88092016-12-01 09:36:50 -050017 * create the final GrPipeline for a GrOp.
csmartdalton28341fa2016-08-17 10:00:21 -070018 */
Brian Salomon54d212e2017-03-21 14:22:38 -040019class GrAppliedClip {
csmartdalton28341fa2016-08-17 10:00:21 -070020public:
Brian Salomon54d212e2017-03-21 14:22:38 -040021 GrAppliedClip() = default;
22 GrAppliedClip(GrAppliedClip&& that) = default;
23 GrAppliedClip(const GrAppliedClip&) = delete;
24
csmartdalton28341fa2016-08-17 10:00:21 -070025 const GrScissorState& scissorState() const { return fScissorState; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -070026 const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -070027 GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP.get(); }
28 bool hasStencilClip() const { return fHasStencilClip; }
29
30 /**
31 * Intersects the applied clip with the provided rect. Returns false if the draw became empty.
Brian Salomon97180af2017-03-14 13:42:58 -040032 * 'clippedDrawBounds' will be intersected with 'irect'. This returns false if the clip becomes
33 * empty or the draw no longer intersects the clip. In either case the draw can be skipped.
csmartdalton28341fa2016-08-17 10:00:21 -070034 */
Brian Salomon97180af2017-03-14 13:42:58 -040035 bool addScissor(const SkIRect& irect, SkRect* clippedDrawBounds) {
36 return fScissorState.intersect(irect) && clippedDrawBounds->intersect(SkRect::Make(irect));
csmartdalton28341fa2016-08-17 10:00:21 -070037 }
38
csmartdaltonbf4a8f92016-09-06 10:01:06 -070039 void addWindowRectangles(const GrWindowRectsState& windowState) {
40 SkASSERT(!fWindowRectsState.enabled());
41 fWindowRectsState = windowState;
42 }
43
Brian Salomon9a767722017-03-13 17:57:28 -040044 void addWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070045 SkASSERT(!fWindowRectsState.enabled());
Brian Salomon9a767722017-03-13 17:57:28 -040046 fWindowRectsState.set(windows, mode);
csmartdalton28341fa2016-08-17 10:00:21 -070047 }
48
49 void addCoverageFP(sk_sp<GrFragmentProcessor> fp) {
50 SkASSERT(!fClipCoverageFP);
51 fClipCoverageFP = fp;
52 }
53
54 void addStencilClip() {
55 SkASSERT(!fHasStencilClip);
56 fHasStencilClip = true;
57 }
58
Brian Salomon54d212e2017-03-21 14:22:38 -040059 bool doesClip() const {
60 return fScissorState.enabled() || fClipCoverageFP || fHasStencilClip ||
61 fWindowRectsState.enabled();
62 }
63
64 bool operator==(const GrAppliedClip& that) const {
65 if (fScissorState != that.fScissorState || fHasStencilClip != that.fHasStencilClip) {
66 return false;
67 }
68 if (SkToBool(fClipCoverageFP)) {
69 if (!SkToBool(that.fClipCoverageFP) ||
70 !that.fClipCoverageFP->isEqual(*fClipCoverageFP)) {
71 return false;
72 }
73 } else if (SkToBool(that.fClipCoverageFP)) {
74 return false;
75 }
76 return fWindowRectsState == that.fWindowRectsState;
77 }
78 bool operator!=(const GrAppliedClip& that) const { return !(*this == that); }
79
csmartdalton28341fa2016-08-17 10:00:21 -070080private:
81 GrScissorState fScissorState;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070082 GrWindowRectsState fWindowRectsState;
csmartdalton28341fa2016-08-17 10:00:21 -070083 sk_sp<GrFragmentProcessor> fClipCoverageFP;
Brian Salomon97180af2017-03-14 13:42:58 -040084 bool fHasStencilClip = false;
csmartdalton28341fa2016-08-17 10:00:21 -070085};
86
87#endif