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 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 11 | #include "GrFragmentProcessor.h" |
| 12 | #include "GrTypesPriv.h" |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 13 | #include "SkClipStack.h" |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 14 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 15 | class GrDrawContext; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 16 | |
| 17 | /** |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 18 | * Produced by GrClip. It provides a set of modifications to the drawing state that are used to |
| 19 | * create the final GrPipeline for a GrBatch. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 20 | */ |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 21 | class GrAppliedClip : public SkNoncopyable { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 22 | public: |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 23 | GrAppliedClip() : fHasStencilClip(false), fDeviceBounds(SkRect::MakeLargest()) {} |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 24 | GrFragmentProcessor* getClipCoverageFragmentProcessor() const { |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 25 | return fClipCoverageFP.get(); |
| 26 | } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 27 | const GrScissorState& scissorState() const { return fScissorState; } |
| 28 | bool hasStencilClip() const { return fHasStencilClip; } |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 29 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 30 | void makeStencil(bool hasStencil, const SkRect& deviceBounds) { |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 31 | fClipCoverageFP = nullptr; |
| 32 | fScissorState.setDisabled(); |
| 33 | fHasStencilClip = hasStencil; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 34 | fDeviceBounds = deviceBounds; |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 35 | } |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 36 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 37 | /** |
| 38 | * The device bounds of the clip defaults to the scissor rect, but a tighter bounds (based |
| 39 | * on the known effect of the stencil values) can be provided. |
| 40 | */ |
| 41 | void makeScissoredStencil(const SkIRect& scissor, const SkRect* deviceBounds = nullptr) { |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 42 | fClipCoverageFP = nullptr; |
| 43 | fScissorState.set(scissor); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 44 | fHasStencilClip = true; |
| 45 | if (deviceBounds) { |
| 46 | fDeviceBounds = *deviceBounds; |
| 47 | SkASSERT(scissor.contains(*deviceBounds)) |
| 48 | } else { |
| 49 | fDeviceBounds = SkRect::Make(scissor); |
| 50 | } |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 53 | void makeFPBased(sk_sp<GrFragmentProcessor> fp, const SkRect& deviceBounds) { |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 54 | fClipCoverageFP = fp; |
| 55 | fScissorState.setDisabled(); |
| 56 | fHasStencilClip = false; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 57 | fDeviceBounds = deviceBounds; |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 58 | } |
| 59 | |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 60 | void makeScissored(SkIRect& scissor) { |
| 61 | fClipCoverageFP.reset(); |
| 62 | fScissorState.set(scissor); |
| 63 | fHasStencilClip = false; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 64 | fDeviceBounds = SkRect::Make(scissor); |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 65 | } |
| 66 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 67 | /** |
| 68 | * The device bounds of the clip defaults to the scissor rect, but a tighter bounds (based |
| 69 | * on the known effect of the fragment processor) can be provided. |
| 70 | */ |
| 71 | void makeScissoredFPBased(sk_sp<GrFragmentProcessor> fp, const SkIRect& scissor, |
| 72 | const SkRect* deviceBounds = nullptr) { |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 73 | fClipCoverageFP = fp; |
| 74 | fScissorState.set(scissor); |
| 75 | fHasStencilClip = false; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 76 | if (deviceBounds) { |
| 77 | fDeviceBounds = *deviceBounds; |
| 78 | SkASSERT(scissor.contains(*deviceBounds)) |
| 79 | } else { |
| 80 | fDeviceBounds = SkRect::Make(scissor); |
| 81 | } |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 82 | } |
| 83 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Returns the device bounds of the applied clip. Ideally this considers the combined effect of |
| 86 | * all clipping techniques in play (scissor, stencil, and/or coverage fp). |
| 87 | */ |
| 88 | const SkRect& deviceBounds() const { return fDeviceBounds; } |
| 89 | |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 90 | private: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 91 | sk_sp<GrFragmentProcessor> fClipCoverageFP; |
| 92 | GrScissorState fScissorState; |
| 93 | bool fHasStencilClip; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 94 | SkRect fDeviceBounds; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 95 | typedef SkNoncopyable INHERITED; |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * GrClip is an abstract base class for applying a clip. It constructs a clip mask if necessary, and |
| 100 | * fills out a GrAppliedClip instructing the caller on how to set up the draw state. |
| 101 | */ |
| 102 | class GrClip { |
| 103 | public: |
| 104 | virtual bool quickContains(const SkRect&) const = 0; |
| 105 | virtual void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 106 | bool* isIntersectionOfRects = nullptr) const = 0; |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 107 | virtual bool apply(GrContext*, |
| 108 | GrDrawContext*, |
| 109 | const SkRect* devBounds, |
| 110 | bool useHWAA, |
| 111 | bool hasUserStencilSettings, |
| 112 | GrAppliedClip* out) const = 0; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 113 | |
| 114 | virtual ~GrClip() {} |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame^] | 115 | |
| 116 | protected: |
| 117 | /** |
| 118 | * Returns true if a clip can safely disable its scissor test for a particular draw. |
| 119 | */ |
| 120 | static bool CanIgnoreScissor(const SkIRect& scissorRect, const SkRect& drawBounds) { |
| 121 | // This is the maximum distance that a draw may extend beyond a clip's scissor and still |
| 122 | // count as inside. We use a sloppy compare because the draw may have chosen its bounds in a |
| 123 | // different coord system. The rationale for 1e-3 is that in the coverage case (and barring |
| 124 | // unexpected rounding), as long as coverage stays below 0.5 * 1/256 we ought to be OK. |
| 125 | constexpr SkScalar fuzz = 1e-3f; |
| 126 | SkASSERT(!scissorRect.isEmpty()); |
| 127 | SkASSERT(!drawBounds.isEmpty()); |
| 128 | return scissorRect.fLeft <= drawBounds.fLeft + fuzz && |
| 129 | scissorRect.fTop <= drawBounds.fTop + fuzz && |
| 130 | scissorRect.fRight >= drawBounds.fRight - fuzz && |
| 131 | scissorRect.fBottom >= drawBounds.fBottom - fuzz; |
| 132 | } |
| 133 | |
| 134 | friend class GrClipMaskManager; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /** |
| 138 | * Specialized implementation for no clip. |
| 139 | */ |
| 140 | class GrNoClip final : public GrClip { |
| 141 | private: |
| 142 | bool quickContains(const SkRect&) const final { return true; } |
| 143 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 144 | bool* isIntersectionOfRects) const final; |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 145 | bool apply(GrContext*, |
| 146 | GrDrawContext*, |
| 147 | const SkRect* /* devBounds */, |
| 148 | bool /* useHWAA */, |
| 149 | bool /* hasUserStencilSettings */, |
| 150 | GrAppliedClip* /* out */) const final { return true; } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * GrFixedClip is a clip that can be represented by fixed-function hardware. It never modifies the |
| 155 | * stencil buffer itself, but can be configured to use whatever clip is already there. |
| 156 | */ |
| 157 | class GrFixedClip final : public GrClip { |
| 158 | public: |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 159 | GrFixedClip() : fDeviceBounds(SkRect::MakeLargest()), fHasStencilClip(false) {} |
| 160 | GrFixedClip(const SkIRect& scissorRect) |
| 161 | : fScissorState(scissorRect) |
| 162 | , fDeviceBounds(SkRect::Make(scissorRect)) |
| 163 | , fHasStencilClip(false) {} |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 164 | |
| 165 | void reset() { |
| 166 | fScissorState.setDisabled(); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 167 | fDeviceBounds.setLargest(); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 168 | fHasStencilClip = false; |
| 169 | } |
| 170 | |
| 171 | void reset(const SkIRect& scissorRect) { |
| 172 | fScissorState.set(scissorRect); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 173 | fDeviceBounds = SkRect::Make(scissorRect); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 174 | fHasStencilClip = false; |
| 175 | } |
| 176 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Enables stenciling. The stencil bounds is the device space bounds where the stencil test |
| 179 | * may pass. |
| 180 | */ |
| 181 | void enableStencilClip(const SkRect& stencilBounds) { |
| 182 | fHasStencilClip = true; |
| 183 | fDeviceBounds = stencilBounds; |
| 184 | if (fScissorState.enabled()) { |
| 185 | const SkIRect& s = fScissorState.rect(); |
| 186 | fDeviceBounds.fLeft = SkTMax(fDeviceBounds.fLeft, SkIntToScalar(s.fLeft)); |
| 187 | fDeviceBounds.fTop = SkTMax(fDeviceBounds.fTop, SkIntToScalar(s.fTop)); |
| 188 | fDeviceBounds.fRight = SkTMin(fDeviceBounds.fRight, SkIntToScalar(s.fRight)); |
| 189 | fDeviceBounds.fBottom = SkTMin(fDeviceBounds.fBottom, SkIntToScalar(s.fBottom)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void disableStencilClip() { |
| 194 | fHasStencilClip = false; |
| 195 | if (fScissorState.enabled()) { |
| 196 | fDeviceBounds = SkRect::Make(fScissorState.rect()); |
| 197 | } else { |
| 198 | fDeviceBounds.setLargest(); |
| 199 | } |
| 200 | } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 201 | |
| 202 | const GrScissorState& scissorState() const { return fScissorState; } |
| 203 | bool hasStencilClip() const { return fHasStencilClip; } |
| 204 | |
| 205 | bool quickContains(const SkRect&) const final; |
| 206 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 207 | bool* isIntersectionOfRects) const final; |
| 208 | |
| 209 | private: |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 210 | bool apply(GrContext*, |
| 211 | GrDrawContext*, |
| 212 | const SkRect* devBounds, |
| 213 | bool useHWAA, |
| 214 | bool hasUserStencilSettings, |
| 215 | GrAppliedClip* out) const final; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 216 | |
| 217 | GrScissorState fScissorState; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 218 | SkRect fDeviceBounds; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 219 | bool fHasStencilClip; |
| 220 | }; |
| 221 | |
| 222 | /** |
| 223 | * GrClipStackClip can apply a generic SkClipStack to the draw state. It may generate clip masks or |
| 224 | * write to the stencil buffer during apply(). |
| 225 | */ |
| 226 | class GrClipStackClip final : public GrClip { |
| 227 | public: |
| 228 | GrClipStackClip(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) { |
| 229 | this->reset(stack, origin); |
| 230 | } |
| 231 | |
| 232 | void reset(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) { |
| 233 | fOrigin = origin ? *origin : SkIPoint::Make(0, 0); |
| 234 | fStack.reset(SkSafeRef(stack)); |
| 235 | } |
| 236 | |
| 237 | const SkIPoint& origin() const { return fOrigin; } |
| 238 | const SkClipStack* clipStack() const { return fStack; } |
| 239 | |
| 240 | bool quickContains(const SkRect&) const final; |
| 241 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 242 | bool* isIntersectionOfRects) const final; |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 243 | bool apply(GrContext*, |
| 244 | GrDrawContext*, |
| 245 | const SkRect* devBounds, |
| 246 | bool useHWAA, |
| 247 | bool hasUserStencilSettings, |
| 248 | GrAppliedClip* out) const final; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 249 | |
| 250 | private: |
| 251 | SkIPoint fOrigin; |
| 252 | SkAutoTUnref<const SkClipStack> fStack; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | #endif |