blob: 2e247c82f29fde25f3df2bad79e6edcae17265e6 [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
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050011#include "GrTypes.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "SkRRect.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050013#include "SkRect.h"
joshualitt44701df2015-02-23 14:44:57 -080014
csmartdalton28341fa2016-08-17 10:00:21 -070015class GrAppliedClip;
csmartdalton02fa32c2016-08-19 13:29:27 -070016class GrContext;
Brian Osman11052242016-10-27 14:47:55 -040017class GrRenderTargetContext;
joshualitt44701df2015-02-23 14:44:57 -080018
19/**
cdalton846c0512016-05-13 10:25:00 -070020 * GrClip is an abstract base class for applying a clip. It constructs a clip mask if necessary, and
21 * fills out a GrAppliedClip instructing the caller on how to set up the draw state.
22 */
23class GrClip {
24public:
25 virtual bool quickContains(const SkRect&) const = 0;
bsalomon7f0d9f32016-08-15 14:49:10 -070026 virtual bool quickContains(const SkRRect& rrect) const {
27 return this->quickContains(rrect.getBounds());
28 }
cdalton846c0512016-05-13 10:25:00 -070029 virtual void getConservativeBounds(int width, int height, SkIRect* devResult,
30 bool* isIntersectionOfRects = nullptr) const = 0;
Brian Salomon97180af2017-03-14 13:42:58 -040031 /**
32 * This computes a GrAppliedClip from the clip which in turn can be used to build a GrPipeline.
33 * To determine the appropriate clipping implementation the GrClip subclass must know whether
34 * the draw will enable HW AA or uses the stencil buffer. On input 'bounds' is a conservative
35 * bounds of the draw that is to be clipped. After return 'bounds' has been intersected with a
36 * conservative bounds of the clip. A return value of false indicates that the draw can be
37 * skipped as it is fully clipped out.
38 */
Brian Osman11052242016-10-27 14:47:55 -040039 virtual bool apply(GrContext*, GrRenderTargetContext*, bool useHWAA,
Brian Salomon97180af2017-03-14 13:42:58 -040040 bool hasUserStencilSettings, GrAppliedClip* result,
41 SkRect* bounds) const = 0;
cdalton846c0512016-05-13 10:25:00 -070042
43 virtual ~GrClip() {}
csmartdalton97f6cd52016-07-13 13:37:08 -070044
csmartdalton97f6cd52016-07-13 13:37:08 -070045 /**
bsalomoncb31e512016-08-26 10:48:19 -070046 * This method quickly and conservatively determines whether the entire clip is equivalent to
47 * intersection with a rrect. This will only return true if the rrect does not fully contain
48 * the render target bounds. Moreover, the returned rrect need not be contained by the render
49 * target bounds. We assume all draws will be implicitly clipped by the render target bounds.
50 *
51 * @param rtBounds The bounds of the render target that the clip will be applied to.
52 * @param rrect If return is true rrect will contain the rrect equivalent to the clip within
53 * rtBounds.
54 * @param aa If return is true aa will indicate whether the rrect clip is antialiased.
55 * @return true if the clip is equivalent to a single rrect, false otherwise.
56 *
57 */
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050058 virtual bool isRRect(const SkRect& rtBounds, SkRRect* rrect, GrAA* aa) const = 0;
bsalomoncb31e512016-08-26 10:48:19 -070059
60 /**
csmartdaltoncbecb082016-07-22 08:59:08 -070061 * This is the maximum distance that a draw may extend beyond a clip's boundary and still count
62 * count as "on the other side". We leave some slack because floating point rounding error is
63 * likely to blame. The rationale for 1e-3 is that in the coverage case (and barring unexpected
64 * rounding), as long as coverage stays within 0.5 * 1/256 of its intended value it shouldn't
65 * have any effect on the final pixel values.
csmartdalton97f6cd52016-07-13 13:37:08 -070066 */
csmartdaltoncbecb082016-07-22 08:59:08 -070067 constexpr static SkScalar kBoundsTolerance = 1e-3f;
68
69 /**
70 * Returns true if the given query bounds count as entirely inside the clip.
71 *
72 * @param innerClipBounds device-space rect contained by the clip (SkRect or SkIRect).
73 * @param queryBounds device-space bounds of the query region.
74 */
Brian Salomonc65aec92017-03-09 09:03:58 -050075 template <typename TRect>
76 constexpr static bool IsInsideClip(const TRect& innerClipBounds, const SkRect& queryBounds) {
csmartdalton77f2fae2016-08-08 09:55:06 -070077 return innerClipBounds.fRight - innerClipBounds.fLeft > kBoundsTolerance &&
78 innerClipBounds.fBottom - innerClipBounds.fTop > kBoundsTolerance &&
79 innerClipBounds.fLeft < queryBounds.fLeft + kBoundsTolerance &&
80 innerClipBounds.fTop < queryBounds.fTop + kBoundsTolerance &&
81 innerClipBounds.fRight > queryBounds.fRight - kBoundsTolerance &&
82 innerClipBounds.fBottom > queryBounds.fBottom - kBoundsTolerance;
csmartdalton97f6cd52016-07-13 13:37:08 -070083 }
84
csmartdaltoncbecb082016-07-22 08:59:08 -070085 /**
86 * Returns true if the given query bounds count as entirely outside the clip.
87 *
88 * @param outerClipBounds device-space rect that contains the clip (SkRect or SkIRect).
89 * @param queryBounds device-space bounds of the query region.
90 */
Brian Salomonc65aec92017-03-09 09:03:58 -050091 template <typename TRect>
92 constexpr static bool IsOutsideClip(const TRect& outerClipBounds, const SkRect& queryBounds) {
Chris Dalton348060f2017-06-05 13:15:37 -060093 return
94 // Is the clip so small that it is effectively empty?
95 outerClipBounds.fRight - outerClipBounds.fLeft <= kBoundsTolerance ||
96 outerClipBounds.fBottom - outerClipBounds.fTop <= kBoundsTolerance ||
97
98 // Are the query bounds effectively outside the clip?
99 outerClipBounds.fLeft >= queryBounds.fRight - kBoundsTolerance ||
100 outerClipBounds.fTop >= queryBounds.fBottom - kBoundsTolerance ||
101 outerClipBounds.fRight <= queryBounds.fLeft + kBoundsTolerance ||
102 outerClipBounds.fBottom <= queryBounds.fTop + kBoundsTolerance;
csmartdaltoncbecb082016-07-22 08:59:08 -0700103 }
104
105 /**
106 * Returns the minimal integer rect that counts as containing a given set of bounds.
107 */
108 static SkIRect GetPixelIBounds(const SkRect& bounds) {
109 return SkIRect::MakeLTRB(SkScalarFloorToInt(bounds.fLeft + kBoundsTolerance),
110 SkScalarFloorToInt(bounds.fTop + kBoundsTolerance),
111 SkScalarCeilToInt(bounds.fRight - kBoundsTolerance),
112 SkScalarCeilToInt(bounds.fBottom - kBoundsTolerance));
113 }
114
115 /**
116 * Returns the minimal pixel-aligned rect that counts as containing a given set of bounds.
117 */
118 static SkRect GetPixelBounds(const SkRect& bounds) {
119 return SkRect::MakeLTRB(SkScalarFloorToScalar(bounds.fLeft + kBoundsTolerance),
120 SkScalarFloorToScalar(bounds.fTop + kBoundsTolerance),
121 SkScalarCeilToScalar(bounds.fRight - kBoundsTolerance),
122 SkScalarCeilToScalar(bounds.fBottom - kBoundsTolerance));
123 }
124
125 /**
126 * Returns true if the given rect counts as aligned with pixel boundaries.
127 */
128 static bool IsPixelAligned(const SkRect& rect) {
129 return SkScalarAbs(SkScalarRoundToScalar(rect.fLeft) - rect.fLeft) <= kBoundsTolerance &&
130 SkScalarAbs(SkScalarRoundToScalar(rect.fTop) - rect.fTop) <= kBoundsTolerance &&
131 SkScalarAbs(SkScalarRoundToScalar(rect.fRight) - rect.fRight) <= kBoundsTolerance &&
132 SkScalarAbs(SkScalarRoundToScalar(rect.fBottom) - rect.fBottom) <= kBoundsTolerance;
133 }
cdalton846c0512016-05-13 10:25:00 -0700134};
135
136/**
137 * Specialized implementation for no clip.
138 */
139class GrNoClip final : public GrClip {
140private:
Brian Salomonc65aec92017-03-09 09:03:58 -0500141 bool quickContains(const SkRect&) const final { return true; }
142 bool quickContains(const SkRRect&) const final { return true; }
cdalton846c0512016-05-13 10:25:00 -0700143 void getConservativeBounds(int width, int height, SkIRect* devResult,
csmartdalton02fa32c2016-08-19 13:29:27 -0700144 bool* isIntersectionOfRects) const final {
145 devResult->setXYWH(0, 0, width, height);
146 if (isIntersectionOfRects) {
147 *isIntersectionOfRects = true;
148 }
149 }
Brian Salomon97180af2017-03-14 13:42:58 -0400150 bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip*,
151 SkRect*) const final {
csmartdalton02fa32c2016-08-19 13:29:27 -0700152 return true;
153 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500154 bool isRRect(const SkRect&, SkRRect*, GrAA*) const override { return false; }
cdalton846c0512016-05-13 10:25:00 -0700155};
156
joshualitt44701df2015-02-23 14:44:57 -0800157#endif