| 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 | |
| 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), or a full clipstack. |
| 19 | * If the clip is a clipstack than the origin is used to translate the stack with |
| 20 | * respect to device coordinates. This allows us to use a clip stack that is |
| 21 | * specified for a root device with a layer device that is restricted to a subset |
| 22 | * of the original canvas. For other clip types the origin will always be (0,0). |
| 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 = NULL) { |
| 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 = kWideOpen_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()->isWideOpen()) || |
| 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()->isWideOpen()) || |
| 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()->isWideOpen()); |
| 137 | } |
| 138 | |
| 139 | void getConservativeBounds(const GrSurface* surface, |
| 140 | SkIRect* devResult, |
| 141 | bool* isIntersectionOfRects = NULL) const { |
| 142 | this->getConservativeBounds(surface->width(), surface->height(), |
| 143 | devResult, isIntersectionOfRects); |
| 144 | } |
| 145 | |
| 146 | void getConservativeBounds(int width, int height, |
| 147 | SkIRect* devResult, |
| 148 | bool* isIntersectionOfRects = NULL) const; |
| 149 | |
| 150 | static const GrClip& WideOpen() { |
| 151 | static GrClip clip; |
| 152 | return clip; |
| 153 | } |
| 154 | |
| 155 | enum ClipType { |
| 156 | kClipStack_ClipType, |
| 157 | kWideOpen_ClipType, |
| 158 | kIRect_ClipType, |
| 159 | }; |
| 160 | |
| 161 | ClipType clipType() const { return fClipType; } |
| 162 | |
| 163 | private: |
| 164 | union Clip { |
| 165 | struct ClipStack { |
| 166 | const SkClipStack* fStack; |
| 167 | SkIPoint fOrigin; |
| 168 | } fClipStack; |
| 169 | SkIRect fIRect; |
| 170 | } fClip; |
| 171 | |
| 172 | ClipType fClipType; |
| 173 | }; |
| 174 | |
| 175 | #endif |