joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1 | /* |
| 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 Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 11 | #include "GrTypes.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 12 | #include "SkRect.h" |
| 13 | #include "SkRRect.h" |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 14 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 15 | class GrAppliedClip; |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 16 | class GrContext; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 17 | class GrRenderTargetContext; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 18 | |
| 19 | /** |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 20 | * 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 | */ |
| 23 | class GrClip { |
| 24 | public: |
| 25 | virtual bool quickContains(const SkRect&) const = 0; |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 26 | virtual bool quickContains(const SkRRect& rrect) const { |
| 27 | return this->quickContains(rrect.getBounds()); |
| 28 | } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 29 | virtual void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 30 | bool* isIntersectionOfRects = nullptr) const = 0; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 31 | virtual bool apply(GrContext*, GrRenderTargetContext*, bool useHWAA, |
| 32 | bool hasUserStencilSettings, GrAppliedClip* out) const = 0; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 33 | |
| 34 | virtual ~GrClip() {} |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 35 | |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 36 | /** |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 37 | * This method quickly and conservatively determines whether the entire clip is equivalent to |
| 38 | * intersection with a rrect. This will only return true if the rrect does not fully contain |
| 39 | * the render target bounds. Moreover, the returned rrect need not be contained by the render |
| 40 | * target bounds. We assume all draws will be implicitly clipped by the render target bounds. |
| 41 | * |
| 42 | * @param rtBounds The bounds of the render target that the clip will be applied to. |
| 43 | * @param rrect If return is true rrect will contain the rrect equivalent to the clip within |
| 44 | * rtBounds. |
| 45 | * @param aa If return is true aa will indicate whether the rrect clip is antialiased. |
| 46 | * @return true if the clip is equivalent to a single rrect, false otherwise. |
| 47 | * |
| 48 | */ |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 49 | virtual bool isRRect(const SkRect& rtBounds, SkRRect* rrect, GrAA* aa) const = 0; |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 52 | * This is the maximum distance that a draw may extend beyond a clip's boundary and still count |
| 53 | * count as "on the other side". We leave some slack because floating point rounding error is |
| 54 | * likely to blame. The rationale for 1e-3 is that in the coverage case (and barring unexpected |
| 55 | * rounding), as long as coverage stays within 0.5 * 1/256 of its intended value it shouldn't |
| 56 | * have any effect on the final pixel values. |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 57 | */ |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 58 | constexpr static SkScalar kBoundsTolerance = 1e-3f; |
| 59 | |
| 60 | /** |
| 61 | * Returns true if the given query bounds count as entirely inside the clip. |
| 62 | * |
| 63 | * @param innerClipBounds device-space rect contained by the clip (SkRect or SkIRect). |
| 64 | * @param queryBounds device-space bounds of the query region. |
| 65 | */ |
| 66 | template<typename TRect> constexpr static bool IsInsideClip(const TRect& innerClipBounds, |
| 67 | const SkRect& queryBounds) { |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 68 | return innerClipBounds.fRight - innerClipBounds.fLeft > kBoundsTolerance && |
| 69 | innerClipBounds.fBottom - innerClipBounds.fTop > kBoundsTolerance && |
| 70 | innerClipBounds.fLeft < queryBounds.fLeft + kBoundsTolerance && |
| 71 | innerClipBounds.fTop < queryBounds.fTop + kBoundsTolerance && |
| 72 | innerClipBounds.fRight > queryBounds.fRight - kBoundsTolerance && |
| 73 | innerClipBounds.fBottom > queryBounds.fBottom - kBoundsTolerance; |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 74 | } |
| 75 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Returns true if the given query bounds count as entirely outside the clip. |
| 78 | * |
| 79 | * @param outerClipBounds device-space rect that contains the clip (SkRect or SkIRect). |
| 80 | * @param queryBounds device-space bounds of the query region. |
| 81 | */ |
| 82 | template<typename TRect> constexpr static bool IsOutsideClip(const TRect& outerClipBounds, |
| 83 | const SkRect& queryBounds) { |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 84 | return outerClipBounds.fRight - outerClipBounds.fLeft <= kBoundsTolerance || |
| 85 | outerClipBounds.fBottom - outerClipBounds.fTop <= kBoundsTolerance || |
| 86 | outerClipBounds.fLeft >= queryBounds.fRight - kBoundsTolerance || |
| 87 | outerClipBounds.fTop >= queryBounds.fBottom - kBoundsTolerance || |
| 88 | outerClipBounds.fRight <= queryBounds.fLeft + kBoundsTolerance || |
| 89 | outerClipBounds.fBottom <= queryBounds.fTop + kBoundsTolerance; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the minimal integer rect that counts as containing a given set of bounds. |
| 94 | */ |
| 95 | static SkIRect GetPixelIBounds(const SkRect& bounds) { |
| 96 | return SkIRect::MakeLTRB(SkScalarFloorToInt(bounds.fLeft + kBoundsTolerance), |
| 97 | SkScalarFloorToInt(bounds.fTop + kBoundsTolerance), |
| 98 | SkScalarCeilToInt(bounds.fRight - kBoundsTolerance), |
| 99 | SkScalarCeilToInt(bounds.fBottom - kBoundsTolerance)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the minimal pixel-aligned rect that counts as containing a given set of bounds. |
| 104 | */ |
| 105 | static SkRect GetPixelBounds(const SkRect& bounds) { |
| 106 | return SkRect::MakeLTRB(SkScalarFloorToScalar(bounds.fLeft + kBoundsTolerance), |
| 107 | SkScalarFloorToScalar(bounds.fTop + kBoundsTolerance), |
| 108 | SkScalarCeilToScalar(bounds.fRight - kBoundsTolerance), |
| 109 | SkScalarCeilToScalar(bounds.fBottom - kBoundsTolerance)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns true if the given rect counts as aligned with pixel boundaries. |
| 114 | */ |
| 115 | static bool IsPixelAligned(const SkRect& rect) { |
| 116 | return SkScalarAbs(SkScalarRoundToScalar(rect.fLeft) - rect.fLeft) <= kBoundsTolerance && |
| 117 | SkScalarAbs(SkScalarRoundToScalar(rect.fTop) - rect.fTop) <= kBoundsTolerance && |
| 118 | SkScalarAbs(SkScalarRoundToScalar(rect.fRight) - rect.fRight) <= kBoundsTolerance && |
| 119 | SkScalarAbs(SkScalarRoundToScalar(rect.fBottom) - rect.fBottom) <= kBoundsTolerance; |
| 120 | } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /** |
| 124 | * Specialized implementation for no clip. |
| 125 | */ |
| 126 | class GrNoClip final : public GrClip { |
| 127 | private: |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 128 | bool quickContains(const SkRect&) const final { |
| 129 | return true; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 130 | } |
lsalzman | 9d2b0fe | 2016-10-10 15:36:24 -0700 | [diff] [blame] | 131 | bool quickContains(const SkRRect&) const final { |
| 132 | return true; |
| 133 | } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 134 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 135 | bool* isIntersectionOfRects) const final { |
| 136 | devResult->setXYWH(0, 0, width, height); |
| 137 | if (isIntersectionOfRects) { |
| 138 | *isIntersectionOfRects = true; |
| 139 | } |
| 140 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 141 | bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip*) const final { |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 142 | return true; |
| 143 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 144 | bool isRRect(const SkRect&, SkRRect*, GrAA*) const override { return false; } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 147 | #endif |