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