Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrClip_DEFINED | 8 #ifndef GrClip_DEFINED |
| 9 #define GrClip_DEFINED | 9 #define GrClip_DEFINED |
| 10 | 10 |
| 11 #include "GrFragmentProcessor.h" | |
| 12 #include "GrTypesPriv.h" | |
| 11 #include "SkClipStack.h" | 13 #include "SkClipStack.h" |
| 12 | 14 |
| 13 struct SkIRect; | 15 class GrClipMaskManager; |
| 16 class GrPipelineBuilder; | |
| 14 | 17 |
| 15 /** | 18 /** |
| 16 * GrClip encapsulates the information required to construct the clip | 19 * Produced by GrClip. It provides a set of modifications to the drawing state t hat are used to |
| 17 * masks. 'A GrClip is either wide open, just an IRect, just a Rect, or a full c lipstack. | 20 * create the final GrPipeline for a GrBatch. |
| 18 * If the clip is a clipstack than the origin is used to translate the stack wit h | |
| 19 * respect to device coordinates. This allows us to use a clip stack that is | |
| 20 * specified for a root device with a layer device that is restricted to a subse t | |
| 21 * of the original canvas. For other clip types the origin will always be (0,0). | |
| 22 * | |
| 23 * NOTE: GrClip *must* point to a const clipstack | |
| 24 */ | 21 */ |
| 25 class GrClip : SkNoncopyable { | 22 class GrAppliedClip { |
| 26 public: | 23 public: |
| 27 GrClip() : fClipType(kWideOpen_ClipType) { | 24 GrAppliedClip() : fHasStencilClip(false) {} |
| 28 fOrigin.setZero(); | 25 const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fC lipCoverageFP; } |
| 26 const GrScissorState& scissorState() const { return fScissorState; } | |
| 27 bool hasStencilClip() const { return fHasStencilClip; } | |
| 28 | |
| 29 private: | |
| 30 SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP; | |
| 31 GrScissorState fScissorState; | |
| 32 bool fHasStencilClip; | |
| 33 | |
| 34 friend class GrFixedClip; | |
| 35 friend class GrClipMaskManager; | |
| 36 | |
| 37 typedef SkNoncopyable INHERITED; | |
| 38 }; | |
| 39 | |
| 40 /** | |
| 41 * GrClip is an abstract base class for applying a clip. It constructs a clip ma sk if necessary, and | |
| 42 * fills out a GrAppliedClip instructing the caller on how to set up the draw st ate. | |
| 43 */ | |
| 44 class GrClip { | |
| 45 public: | |
| 46 virtual bool quickContains(const SkRect&) const = 0; | |
| 47 virtual void getConservativeBounds(int width, int height, SkIRect* devResult , | |
| 48 bool* isIntersectionOfRects = nullptr) co nst = 0; | |
| 49 virtual bool apply(GrClipMaskManager*, const GrPipelineBuilder&, const SkRec t* devBounds, | |
| 50 GrAppliedClip*) const = 0; | |
| 51 | |
| 52 virtual ~GrClip() {} | |
| 53 }; | |
| 54 | |
| 55 /** | |
| 56 * Special clip implementation for a wide open clip (aka no clip). | |
| 57 */ | |
| 58 class GrWideOpenClip final : public GrClip { | |
|
bsalomon
2016/05/13 02:34:12
I've never been crazy about "WideOpen", what if we
Chris Dalton
2016/05/13 03:53:46
Done. I like GrNoClip.
| |
| 59 private: | |
| 60 bool quickContains(const SkRect& rect) const override final { return true; } | |
|
bsalomon
2016/05/13 02:34:11
Can we have just one of final/override? Either see
Chris Dalton
2016/05/13 03:53:46
Done. Good point.
| |
| 61 void getConservativeBounds(int width, int height, SkIRect* devResult, | |
| 62 bool* isIntersectionOfRects) const override final ; | |
| 63 bool apply(GrClipMaskManager*, const GrPipelineBuilder&, | |
| 64 const SkRect*, GrAppliedClip*) const override final { return true ; } | |
| 65 }; | |
| 66 | |
| 67 /** | |
| 68 * GrFixedClip is a clip that can be represented by fixed-function hardware. It does not write a | |
| 69 * clip to the stencil buffer, but can be configured to use whatever clip is alr eady there. | |
| 70 */ | |
| 71 class GrFixedClip final : public GrClip { | |
| 72 public: | |
| 73 GrFixedClip() : fHasStencilClip(false) {} | |
| 74 GrFixedClip(const SkIRect& scissorRect) : fScissorState(scissorRect), fHasSt encilClip(false) {} | |
| 75 | |
| 76 void reset() { | |
| 77 fScissorState.setDisabled(); | |
| 78 fHasStencilClip = false; | |
| 29 } | 79 } |
| 30 | 80 |
| 31 GrClip(const SkIRect& rect) : fClipType(kIRect_ClipType) { | 81 void reset(const SkIRect& scissorRect) { |
| 32 fOrigin.setZero(); | 82 fScissorState.set(scissorRect); |
| 33 fClip.fIRect = rect; | 83 fHasStencilClip = false; |
| 34 } | 84 } |
| 35 | 85 |
| 36 GrClip(const SkRect& rect) : fClipType(kIRect_ClipType) { | 86 void resetNonAA(const SkRect& clipRect) { |
| 37 fOrigin.setZero(); | 87 fScissorState.setNonAA(clipRect); |
| 38 fClip.fIRect.fLeft = SkScalarRoundToInt(rect.fLeft); | 88 fHasStencilClip = false; |
| 39 fClip.fIRect.fTop = SkScalarRoundToInt(rect.fTop); | |
| 40 fClip.fIRect.fRight = SkScalarRoundToInt(rect.fRight); | |
| 41 fClip.fIRect.fBottom = SkScalarRoundToInt(rect.fBottom); | |
| 42 } | 89 } |
| 43 | 90 |
| 44 ~GrClip() { this->reset(); } | 91 void enableStencilClip(bool enable) { fHasStencilClip = enable; } |
| 45 | 92 |
| 46 const GrClip& operator=(const GrClip& other) { | 93 const GrScissorState& scissorState() const { return fScissorState; } |
| 47 this->reset(); | 94 bool hasStencilClip() const { return fHasStencilClip; } |
| 48 fClipType = other.fClipType; | 95 |
| 49 switch (other.fClipType) { | 96 bool quickContains(const SkRect& rect) const override final; |
| 50 case kWideOpen_ClipType: | 97 void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 51 fOrigin.setZero(); | 98 bool* isIntersectionOfRects) const override final ; |
| 52 break; | 99 |
| 53 case kClipStack_ClipType: | 100 private: |
| 54 fClip.fStack = SkRef(other.clipStack()); | 101 bool apply(GrClipMaskManager*, const GrPipelineBuilder&, |
| 55 fOrigin = other.origin(); | 102 const SkRect* devBounds, GrAppliedClip* out) const override final ; |
| 56 break; | 103 |
| 57 case kIRect_ClipType: | 104 GrScissorState fScissorState; |
| 58 fClip.fIRect = other.irect(); | 105 bool fHasStencilClip; |
| 59 fOrigin.setZero(); | 106 }; |
| 60 break; | 107 |
| 61 } | 108 /** |
| 62 return *this; | 109 * GrClipStackClip can apply a generic SkClipStack to the draw state. It may gen erate clip masks or |
| 110 * write to the stencil buffer during apply(). | |
| 111 */ | |
| 112 class GrClipStackClip final : public GrClip { | |
| 113 public: | |
| 114 GrClipStackClip(const SkClipStack* stack = nullptr, const SkIPoint* origin = nullptr) { | |
| 115 this->reset(stack, origin); | |
| 63 } | 116 } |
| 64 | 117 |
| 65 bool operator==(const GrClip& other) const { | 118 void reset(const SkClipStack* stack = nullptr, const SkIPoint* origin = null ptr) { |
| 66 if (this->clipType() != other.clipType()) { | 119 fOrigin = origin ? *origin : SkIPoint::Make(0, 0); |
| 67 return false; | 120 fStack.reset(SkSafeRef(stack)); |
| 68 } | |
| 69 | |
| 70 switch (fClipType) { | |
| 71 case kWideOpen_ClipType: | |
| 72 return true; | |
| 73 case kClipStack_ClipType: | |
| 74 if (this->origin() != other.origin()) { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 if (this->clipStack() && other.clipStack()) { | |
| 79 return *this->clipStack() == *other.clipStack(); | |
| 80 } else { | |
| 81 return this->clipStack() == other.clipStack(); | |
| 82 } | |
| 83 break; | |
| 84 case kIRect_ClipType: | |
| 85 return this->irect() == other.irect(); | |
| 86 break; | |
| 87 } | |
| 88 SkFAIL("This should not occur\n"); | |
| 89 return false; | |
| 90 } | 121 } |
| 91 | 122 |
| 92 bool operator!=(const GrClip& other) const { | 123 const SkIPoint& origin() const { return fOrigin; } |
| 93 return !(*this == other); | 124 const SkClipStack* clipStack() const { return fStack; } |
| 94 } | |
| 95 | 125 |
| 96 const SkClipStack* clipStack() const { | 126 bool quickContains(const SkRect& rect) const override final; |
| 97 SkASSERT(kClipStack_ClipType == fClipType); | 127 void getConservativeBounds(int width, int height, SkIRect* devResult, |
| 98 return fClip.fStack; | 128 bool* isIntersectionOfRects) const override final ; |
| 99 } | 129 bool apply(GrClipMaskManager*, const GrPipelineBuilder&, |
| 100 | 130 const SkRect* devBounds, GrAppliedClip*) const override final; |
| 101 void setClipStack(const SkClipStack* clipStack, const SkIPoint* origin = NUL L) { | |
| 102 this->reset(); | |
| 103 if (clipStack->isWideOpen()) { | |
| 104 fClipType = kWideOpen_ClipType; | |
| 105 fOrigin.setZero(); | |
| 106 } else { | |
| 107 fClipType = kClipStack_ClipType; | |
| 108 fClip.fStack = SkRef(clipStack); | |
| 109 if (origin) { | |
| 110 fOrigin = *origin; | |
| 111 } else { | |
| 112 fOrigin.setZero(); | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void setIRect(const SkIRect& irect) { | |
| 118 this->reset(); | |
| 119 fClipType = kIRect_ClipType; | |
| 120 fOrigin.setZero(); | |
| 121 fClip.fIRect = irect; | |
| 122 } | |
| 123 | |
| 124 const SkIRect& irect() const { | |
| 125 SkASSERT(kIRect_ClipType == fClipType); | |
| 126 return fClip.fIRect; | |
| 127 } | |
| 128 | |
| 129 void reset() { | |
| 130 if (kClipStack_ClipType == fClipType) { | |
| 131 fClip.fStack->unref(); | |
| 132 fClip.fStack = NULL; | |
| 133 } | |
| 134 fClipType = kWideOpen_ClipType; | |
| 135 fOrigin.setZero(); | |
| 136 } | |
| 137 | |
| 138 // We support this for all cliptypes to simplify the logic a bit in clip mas k manager. | |
| 139 // non clipstack clip types MUST have a (0,0) origin | |
| 140 const SkIPoint& origin() const { | |
| 141 SkASSERT(fClipType == kClipStack_ClipType || (fOrigin.fX == 0 && fOrigin .fY == 0)); | |
| 142 return fOrigin; | |
| 143 } | |
| 144 | |
| 145 bool isWideOpen(const SkRect& rect) const { | |
| 146 return (kWideOpen_ClipType == fClipType) || | |
| 147 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe n()) || | |
| 148 (kIRect_ClipType == fClipType && this->irect().contains(rect)); | |
| 149 } | |
| 150 | |
| 151 bool isWideOpen(const SkIRect& rect) const { | |
| 152 return (kWideOpen_ClipType == fClipType) || | |
| 153 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe n()) || | |
| 154 (kIRect_ClipType == fClipType && this->irect().contains(rect)); | |
| 155 } | |
| 156 | |
| 157 bool isWideOpen() const { | |
| 158 return (kWideOpen_ClipType == fClipType) || | |
| 159 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe n()); | |
| 160 } | |
| 161 | |
| 162 bool quickContains(const SkRect& rect) const { | |
| 163 return (kWideOpen_ClipType == fClipType) || | |
| 164 (kClipStack_ClipType == fClipType && this->clipStack()->quickCont ains(rect)) || | |
| 165 (kIRect_ClipType == fClipType && this->irect().contains(rect)); | |
| 166 } | |
| 167 | |
| 168 void getConservativeBounds(int width, int height, | |
| 169 SkIRect* devResult, | |
| 170 bool* isIntersectionOfRects = NULL) const; | |
| 171 | |
| 172 static const GrClip& WideOpen(); | |
| 173 | |
| 174 enum ClipType { | |
| 175 kClipStack_ClipType, | |
| 176 kWideOpen_ClipType, | |
| 177 kIRect_ClipType, | |
| 178 }; | |
| 179 | |
| 180 ClipType clipType() const { return fClipType; } | |
| 181 | 131 |
| 182 private: | 132 private: |
| 183 union Clip { | 133 SkIPoint fOrigin; |
| 184 const SkClipStack* fStack; | 134 SkAutoTUnref<const SkClipStack> fStack; |
| 185 SkIRect fIRect; | |
| 186 } fClip; | |
| 187 | |
| 188 SkIPoint fOrigin; | |
| 189 ClipType fClipType; | |
| 190 }; | 135 }; |
| 191 | 136 |
| 192 #endif | 137 #endif |
| OLD | NEW |