OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "SkClipStack.h" |
| 12 #include "GrSurface.h" |
| 13 |
| 14 struct SkIRect; |
| 15 |
| 16 /** |
| 17 * GrClip encapsulates the information required to construct the clip |
| 18 * masks. 'A GrClip is either wide open, just an IRect, just a Rect(TODO), a ful
l clipstack, or |
| 19 * empty. If a clipstack than fOrigin' is only non-zero when saveLayer has been
called |
| 20 * with an offset bounding box. The clips in 'fClipStack' are in |
| 21 * device coordinates (i.e., they have been translated by -fOrigin w.r.t. |
| 22 * the canvas' device coordinates). |
| 23 * |
| 24 * NOTE: GrClip *must* point to a const clipstack |
| 25 */ |
| 26 class GrClip : SkNoncopyable { |
| 27 public: |
| 28 GrClip() : fClipType(kWideOpen_ClipType) {} |
| 29 GrClip(const SkIRect& rect) : fClipType(kIRect_ClipType) { |
| 30 fClip.fIRect = rect; |
| 31 } |
| 32 ~GrClip() { this->reset(); } |
| 33 |
| 34 const GrClip& operator=(const GrClip& other) { |
| 35 this->reset(); |
| 36 fClipType = other.fClipType; |
| 37 switch (other.fClipType) { |
| 38 default: |
| 39 SkFAIL("Incomplete Switch\n"); |
| 40 case kWideOpen_ClipType: |
| 41 break; |
| 42 case kClipStack_ClipType: |
| 43 fClip.fClipStack.fStack = SkRef(other.clipStack()); |
| 44 fClip.fClipStack.fOrigin = other.origin(); |
| 45 break; |
| 46 case kIRect_ClipType: |
| 47 fClip.fIRect = other.irect(); |
| 48 break; |
| 49 } |
| 50 return *this; |
| 51 } |
| 52 |
| 53 bool operator==(const GrClip& other) const { |
| 54 if (this->clipType() != other.clipType()) { |
| 55 return false; |
| 56 } |
| 57 |
| 58 switch (fClipType) { |
| 59 default: |
| 60 SkFAIL("Incomplete Switch\n"); |
| 61 return false; |
| 62 case kWideOpen_ClipType: |
| 63 return true; |
| 64 case kClipStack_ClipType: |
| 65 if (this->origin() != other.origin()) { |
| 66 return false; |
| 67 } |
| 68 |
| 69 if (this->clipStack() && other.clipStack()) { |
| 70 return *this->clipStack() == *other.clipStack(); |
| 71 } else { |
| 72 return this->clipStack() == other.clipStack(); |
| 73 } |
| 74 break; |
| 75 case kIRect_ClipType: |
| 76 return this->irect() == other.irect(); |
| 77 break; |
| 78 } |
| 79 } |
| 80 |
| 81 bool operator!=(const GrClip& other) const { |
| 82 return !(*this == other); |
| 83 } |
| 84 |
| 85 const SkClipStack* clipStack() const { |
| 86 SkASSERT(kClipStack_ClipType == fClipType); |
| 87 return fClip.fClipStack.fStack; |
| 88 } |
| 89 |
| 90 void setClipStack(const SkClipStack* clipStack, const SkIPoint* origin = NUL
L) { |
| 91 if (clipStack->isWideOpen()) { |
| 92 fClipType = kWideOpen_ClipType; |
| 93 } else { |
| 94 fClipType = kClipStack_ClipType; |
| 95 fClip.fClipStack.fStack = SkRef(clipStack); |
| 96 if (origin) { |
| 97 fClip.fClipStack.fOrigin = *origin; |
| 98 } else { |
| 99 fClip.fClipStack.fOrigin.setZero(); |
| 100 } |
| 101 } |
| 102 } |
| 103 |
| 104 const SkIRect& irect() const { |
| 105 SkASSERT(kIRect_ClipType == fClipType); |
| 106 return fClip.fIRect; |
| 107 } |
| 108 |
| 109 void reset() { |
| 110 if (kClipStack_ClipType == fClipType) { |
| 111 fClip.fClipStack.fStack->unref(); |
| 112 fClip.fClipStack.fStack = NULL; |
| 113 } |
| 114 fClipType = kUninitialized_ClipType; |
| 115 } |
| 116 |
| 117 const SkIPoint& origin() const { |
| 118 SkASSERT(kClipStack_ClipType == fClipType); |
| 119 return fClip.fClipStack.fOrigin; |
| 120 } |
| 121 |
| 122 bool isWideOpen(const SkRect& rect) const { |
| 123 return (kWideOpen_ClipType == fClipType) || |
| 124 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe
n()) || |
| 125 (kIRect_ClipType == fClipType && this->irect().contains(rect)); |
| 126 } |
| 127 |
| 128 bool isWideOpen(const SkIRect& rect) const { |
| 129 return (kWideOpen_ClipType == fClipType) || |
| 130 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe
n()) || |
| 131 (kIRect_ClipType == fClipType && this->irect().contains(rect)); |
| 132 } |
| 133 |
| 134 bool isWideOpen() const { |
| 135 return (kWideOpen_ClipType == fClipType) || |
| 136 (kClipStack_ClipType == fClipType && this->clipStack()->isWideOpe
n()) || |
| 137 (kIRect_ClipType == fClipType && this->irect().isEmpty()); |
| 138 } |
| 139 |
| 140 void getConservativeBounds(const GrSurface* surface, |
| 141 SkIRect* devResult, |
| 142 bool* isIntersectionOfRects = NULL) const { |
| 143 this->getConservativeBounds(surface->width(), surface->height(), |
| 144 devResult, isIntersectionOfRects); |
| 145 } |
| 146 |
| 147 void getConservativeBounds(int width, int height, |
| 148 SkIRect* devResult, |
| 149 bool* isIntersectionOfRects = NULL) const; |
| 150 |
| 151 static const GrClip& WideOpen() { |
| 152 static GrClip clip; |
| 153 return clip; |
| 154 } |
| 155 |
| 156 enum ClipType { |
| 157 kClipStack_ClipType, |
| 158 kWideOpen_ClipType, |
| 159 kIRect_ClipType, |
| 160 kUninitialized_ClipType, |
| 161 }; |
| 162 |
| 163 ClipType clipType() const { return fClipType; } |
| 164 |
| 165 private: |
| 166 union Clip { |
| 167 struct ClipStack { |
| 168 const SkClipStack* fStack; |
| 169 SkIPoint fOrigin; |
| 170 } fClipStack; |
| 171 SkIRect fIRect; |
| 172 } fClip; |
| 173 |
| 174 ClipType fClipType; |
| 175 }; |
| 176 |
| 177 #endif |
OLD | NEW |