blob: 7358e67cb349730b25970360e6f8f0183d6297fb [file] [log] [blame]
joshualitt44701df2015-02-23 14:44:57 -08001/*
2 * Copyright 2010 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 GrClip_DEFINED
9#define GrClip_DEFINED
10
Chris Daltonbbfd5162017-11-07 13:35:22 -070011#include "GrAppliedClip.h"
12#include "GrRenderTargetContext.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "SkRRect.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050014#include "SkRect.h"
joshualitt44701df2015-02-23 14:44:57 -080015
csmartdalton02fa32c2016-08-19 13:29:27 -070016class GrContext;
joshualitt44701df2015-02-23 14:44:57 -080017
18/**
cdalton846c0512016-05-13 10:25:00 -070019 * GrClip is an abstract base class for applying a clip. It constructs a clip mask if necessary, and
20 * fills out a GrAppliedClip instructing the caller on how to set up the draw state.
21 */
22class GrClip {
23public:
24 virtual bool quickContains(const SkRect&) const = 0;
bsalomon7f0d9f32016-08-15 14:49:10 -070025 virtual bool quickContains(const SkRRect& rrect) const {
26 return this->quickContains(rrect.getBounds());
27 }
cdalton846c0512016-05-13 10:25:00 -070028 virtual void getConservativeBounds(int width, int height, SkIRect* devResult,
29 bool* isIntersectionOfRects = nullptr) const = 0;
Brian Salomon97180af2017-03-14 13:42:58 -040030 /**
31 * This computes a GrAppliedClip from the clip which in turn can be used to build a GrPipeline.
32 * To determine the appropriate clipping implementation the GrClip subclass must know whether
33 * the draw will enable HW AA or uses the stencil buffer. On input 'bounds' is a conservative
34 * bounds of the draw that is to be clipped. After return 'bounds' has been intersected with a
35 * conservative bounds of the clip. A return value of false indicates that the draw can be
36 * skipped as it is fully clipped out.
37 */
Brian Osman11052242016-10-27 14:47:55 -040038 virtual bool apply(GrContext*, GrRenderTargetContext*, bool useHWAA,
Chris Daltonbbfd5162017-11-07 13:35:22 -070039 bool hasUserStencilSettings, GrAppliedClip*, SkRect* bounds) const = 0;
cdalton846c0512016-05-13 10:25:00 -070040
41 virtual ~GrClip() {}
csmartdalton97f6cd52016-07-13 13:37:08 -070042
csmartdalton97f6cd52016-07-13 13:37:08 -070043 /**
bsalomoncb31e512016-08-26 10:48:19 -070044 * This method quickly and conservatively determines whether the entire clip is equivalent to
45 * intersection with a rrect. This will only return true if the rrect does not fully contain
46 * the render target bounds. Moreover, the returned rrect need not be contained by the render
47 * target bounds. We assume all draws will be implicitly clipped by the render target bounds.
48 *
49 * @param rtBounds The bounds of the render target that the clip will be applied to.
50 * @param rrect If return is true rrect will contain the rrect equivalent to the clip within
51 * rtBounds.
52 * @param aa If return is true aa will indicate whether the rrect clip is antialiased.
53 * @return true if the clip is equivalent to a single rrect, false otherwise.
54 *
55 */
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050056 virtual bool isRRect(const SkRect& rtBounds, SkRRect* rrect, GrAA* aa) const = 0;
bsalomoncb31e512016-08-26 10:48:19 -070057
58 /**
csmartdaltoncbecb082016-07-22 08:59:08 -070059 * This is the maximum distance that a draw may extend beyond a clip's boundary and still count
60 * count as "on the other side". We leave some slack because floating point rounding error is
61 * likely to blame. The rationale for 1e-3 is that in the coverage case (and barring unexpected
62 * rounding), as long as coverage stays within 0.5 * 1/256 of its intended value it shouldn't
63 * have any effect on the final pixel values.
csmartdalton97f6cd52016-07-13 13:37:08 -070064 */
csmartdaltoncbecb082016-07-22 08:59:08 -070065 constexpr static SkScalar kBoundsTolerance = 1e-3f;
66
67 /**
68 * Returns true if the given query bounds count as entirely inside the clip.
69 *
70 * @param innerClipBounds device-space rect contained by the clip (SkRect or SkIRect).
71 * @param queryBounds device-space bounds of the query region.
72 */
Brian Salomonc65aec92017-03-09 09:03:58 -050073 template <typename TRect>
74 constexpr static bool IsInsideClip(const TRect& innerClipBounds, const SkRect& queryBounds) {
Jim Van Verthba7cf292017-11-02 20:18:56 +000075 return innerClipBounds.fRight > innerClipBounds.fLeft + kBoundsTolerance &&
76 innerClipBounds.fBottom > innerClipBounds.fTop + kBoundsTolerance &&
csmartdalton77f2fae2016-08-08 09:55:06 -070077 innerClipBounds.fLeft < queryBounds.fLeft + kBoundsTolerance &&
78 innerClipBounds.fTop < queryBounds.fTop + kBoundsTolerance &&
79 innerClipBounds.fRight > queryBounds.fRight - kBoundsTolerance &&
80 innerClipBounds.fBottom > queryBounds.fBottom - kBoundsTolerance;
csmartdalton97f6cd52016-07-13 13:37:08 -070081 }
82
csmartdaltoncbecb082016-07-22 08:59:08 -070083 /**
84 * Returns true if the given query bounds count as entirely outside the clip.
85 *
86 * @param outerClipBounds device-space rect that contains the clip (SkRect or SkIRect).
87 * @param queryBounds device-space bounds of the query region.
88 */
Brian Salomonc65aec92017-03-09 09:03:58 -050089 template <typename TRect>
90 constexpr static bool IsOutsideClip(const TRect& outerClipBounds, const SkRect& queryBounds) {
Chris Dalton348060f2017-06-05 13:15:37 -060091 return
92 // Is the clip so small that it is effectively empty?
93 outerClipBounds.fRight - outerClipBounds.fLeft <= kBoundsTolerance ||
94 outerClipBounds.fBottom - outerClipBounds.fTop <= kBoundsTolerance ||
95
96 // Are the query bounds effectively outside the clip?
97 outerClipBounds.fLeft >= queryBounds.fRight - kBoundsTolerance ||
98 outerClipBounds.fTop >= queryBounds.fBottom - kBoundsTolerance ||
99 outerClipBounds.fRight <= queryBounds.fLeft + kBoundsTolerance ||
100 outerClipBounds.fBottom <= queryBounds.fTop + kBoundsTolerance;
csmartdaltoncbecb082016-07-22 08:59:08 -0700101 }
102
103 /**
104 * Returns the minimal integer rect that counts as containing a given set of bounds.
105 */
106 static SkIRect GetPixelIBounds(const SkRect& bounds) {
107 return SkIRect::MakeLTRB(SkScalarFloorToInt(bounds.fLeft + kBoundsTolerance),
108 SkScalarFloorToInt(bounds.fTop + kBoundsTolerance),
109 SkScalarCeilToInt(bounds.fRight - kBoundsTolerance),
110 SkScalarCeilToInt(bounds.fBottom - kBoundsTolerance));
111 }
112
113 /**
114 * Returns the minimal pixel-aligned rect that counts as containing a given set of bounds.
115 */
116 static SkRect GetPixelBounds(const SkRect& bounds) {
117 return SkRect::MakeLTRB(SkScalarFloorToScalar(bounds.fLeft + kBoundsTolerance),
118 SkScalarFloorToScalar(bounds.fTop + kBoundsTolerance),
119 SkScalarCeilToScalar(bounds.fRight - kBoundsTolerance),
120 SkScalarCeilToScalar(bounds.fBottom - kBoundsTolerance));
121 }
122
123 /**
124 * Returns true if the given rect counts as aligned with pixel boundaries.
125 */
126 static bool IsPixelAligned(const SkRect& rect) {
127 return SkScalarAbs(SkScalarRoundToScalar(rect.fLeft) - rect.fLeft) <= kBoundsTolerance &&
128 SkScalarAbs(SkScalarRoundToScalar(rect.fTop) - rect.fTop) <= kBoundsTolerance &&
129 SkScalarAbs(SkScalarRoundToScalar(rect.fRight) - rect.fRight) <= kBoundsTolerance &&
130 SkScalarAbs(SkScalarRoundToScalar(rect.fBottom) - rect.fBottom) <= kBoundsTolerance;
131 }
cdalton846c0512016-05-13 10:25:00 -0700132};
133
Chris Daltonbbfd5162017-11-07 13:35:22 -0700134
135/**
136 * GrHardClip never uses coverage FPs. It can only enforce the clip using the already-existing
137 * stencil buffer contents and/or fixed-function state like scissor. Always aliased if MSAA is off.
138 */
139class GrHardClip : public GrClip {
140public:
141 /**
142 * Sets the appropriate hardware state modifications on GrAppliedHardClip that will implement
143 * the clip. On input 'bounds' is a conservative bounds of the draw that is to be clipped. After
144 * return 'bounds' has been intersected with a conservative bounds of the clip. A return value
145 * of false indicates that the draw can be skipped as it is fully clipped out.
146 */
147 virtual bool apply(int rtWidth, int rtHeight, GrAppliedHardClip* out, SkRect* bounds) const = 0;
148
149private:
150 bool apply(GrContext*, GrRenderTargetContext* rtc, bool useHWAA, bool hasUserStencilSettings,
151 GrAppliedClip* out, SkRect* bounds) const final {
152 return this->apply(rtc->width(), rtc->height(), &out->hardClip(), bounds);
153 }
154};
155
cdalton846c0512016-05-13 10:25:00 -0700156/**
157 * Specialized implementation for no clip.
158 */
Chris Daltonbbfd5162017-11-07 13:35:22 -0700159class GrNoClip final : public GrHardClip {
cdalton846c0512016-05-13 10:25:00 -0700160private:
Brian Salomonc65aec92017-03-09 09:03:58 -0500161 bool quickContains(const SkRect&) const final { return true; }
162 bool quickContains(const SkRRect&) const final { return true; }
cdalton846c0512016-05-13 10:25:00 -0700163 void getConservativeBounds(int width, int height, SkIRect* devResult,
csmartdalton02fa32c2016-08-19 13:29:27 -0700164 bool* isIntersectionOfRects) const final {
165 devResult->setXYWH(0, 0, width, height);
166 if (isIntersectionOfRects) {
167 *isIntersectionOfRects = true;
168 }
169 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700170 bool apply(int rtWidth, int rtHeight, GrAppliedHardClip*, SkRect*) const final { return true; }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500171 bool isRRect(const SkRect&, SkRRect*, GrAA*) const override { return false; }
cdalton846c0512016-05-13 10:25:00 -0700172};
173
joshualitt44701df2015-02-23 14:44:57 -0800174#endif