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() {} |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * Specialized implementation for no clip. |
| 119 | */ |
| 120 | class GrNoClip final : public GrClip { |
| 121 | private: |
| 122 | bool quickContains(const SkRect&) const final { return true; } |
| 123 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 124 | bool* isIntersectionOfRects) const final; |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 125 | bool apply(GrContext*, |
| 126 | GrDrawContext*, |
| 127 | const SkRect* /* devBounds */, |
| 128 | bool /* useHWAA */, |
| 129 | bool /* hasUserStencilSettings */, |
| 130 | GrAppliedClip* /* out */) const final { return true; } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | /** |
| 134 | * GrFixedClip is a clip that can be represented by fixed-function hardware. It never modifies the |
| 135 | * stencil buffer itself, but can be configured to use whatever clip is already there. |
| 136 | */ |
| 137 | class GrFixedClip final : public GrClip { |
| 138 | public: |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 139 | GrFixedClip() : fDeviceBounds(SkRect::MakeLargest()), fHasStencilClip(false) {} |
| 140 | GrFixedClip(const SkIRect& scissorRect) |
| 141 | : fScissorState(scissorRect) |
| 142 | , fDeviceBounds(SkRect::Make(scissorRect)) |
| 143 | , fHasStencilClip(false) {} |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 144 | |
| 145 | void reset() { |
| 146 | fScissorState.setDisabled(); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 147 | fDeviceBounds.setLargest(); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 148 | fHasStencilClip = false; |
| 149 | } |
| 150 | |
| 151 | void reset(const SkIRect& scissorRect) { |
| 152 | fScissorState.set(scissorRect); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 153 | fDeviceBounds = SkRect::Make(scissorRect); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 154 | fHasStencilClip = false; |
| 155 | } |
| 156 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Enables stenciling. The stencil bounds is the device space bounds where the stencil test |
| 159 | * may pass. |
| 160 | */ |
| 161 | void enableStencilClip(const SkRect& stencilBounds) { |
| 162 | fHasStencilClip = true; |
| 163 | fDeviceBounds = stencilBounds; |
| 164 | if (fScissorState.enabled()) { |
| 165 | const SkIRect& s = fScissorState.rect(); |
| 166 | fDeviceBounds.fLeft = SkTMax(fDeviceBounds.fLeft, SkIntToScalar(s.fLeft)); |
| 167 | fDeviceBounds.fTop = SkTMax(fDeviceBounds.fTop, SkIntToScalar(s.fTop)); |
| 168 | fDeviceBounds.fRight = SkTMin(fDeviceBounds.fRight, SkIntToScalar(s.fRight)); |
| 169 | fDeviceBounds.fBottom = SkTMin(fDeviceBounds.fBottom, SkIntToScalar(s.fBottom)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void disableStencilClip() { |
| 174 | fHasStencilClip = false; |
| 175 | if (fScissorState.enabled()) { |
| 176 | fDeviceBounds = SkRect::Make(fScissorState.rect()); |
| 177 | } else { |
| 178 | fDeviceBounds.setLargest(); |
| 179 | } |
| 180 | } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 181 | |
| 182 | const GrScissorState& scissorState() const { return fScissorState; } |
| 183 | bool hasStencilClip() const { return fHasStencilClip; } |
| 184 | |
| 185 | bool quickContains(const SkRect&) const final; |
| 186 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 187 | bool* isIntersectionOfRects) const final; |
| 188 | |
| 189 | private: |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 190 | bool apply(GrContext*, |
| 191 | GrDrawContext*, |
| 192 | const SkRect* devBounds, |
| 193 | bool useHWAA, |
| 194 | bool hasUserStencilSettings, |
| 195 | GrAppliedClip* out) const final; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 196 | |
| 197 | GrScissorState fScissorState; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 198 | SkRect fDeviceBounds; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 199 | bool fHasStencilClip; |
| 200 | }; |
| 201 | |
| 202 | /** |
| 203 | * GrClipStackClip can apply a generic SkClipStack to the draw state. It may generate clip masks or |
| 204 | * write to the stencil buffer during apply(). |
| 205 | */ |
| 206 | class GrClipStackClip final : public GrClip { |
| 207 | public: |
| 208 | GrClipStackClip(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) { |
| 209 | this->reset(stack, origin); |
| 210 | } |
| 211 | |
| 212 | void reset(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) { |
| 213 | fOrigin = origin ? *origin : SkIPoint::Make(0, 0); |
| 214 | fStack.reset(SkSafeRef(stack)); |
| 215 | } |
| 216 | |
| 217 | const SkIPoint& origin() const { return fOrigin; } |
| 218 | const SkClipStack* clipStack() const { return fStack; } |
| 219 | |
| 220 | bool quickContains(const SkRect&) const final; |
| 221 | void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 222 | bool* isIntersectionOfRects) const final; |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 223 | bool apply(GrContext*, |
| 224 | GrDrawContext*, |
| 225 | const SkRect* devBounds, |
| 226 | bool useHWAA, |
| 227 | bool hasUserStencilSettings, |
| 228 | GrAppliedClip* out) const final; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 229 | |
| 230 | private: |
| 231 | SkIPoint fOrigin; |
| 232 | SkAutoTUnref<const SkClipStack> fStack; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | #endif |