reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrClip_DEFINED |
| 19 | #define GrClip_DEFINED |
| 20 | |
| 21 | #include "GrClipIterator.h" |
| 22 | #include "GrRect.h" |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 23 | #include "GrPath.h" |
| 24 | #include "GrTArray.h" |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 25 | #include "GrTemplates.h" |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 26 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | |
| 28 | class GrClip { |
| 29 | public: |
| 30 | GrClip(); |
| 31 | GrClip(const GrClip& src); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 32 | /** |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 33 | * If specified, the conservativeBounds parameter already takes (tx,ty) |
| 34 | * into account. |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 35 | */ |
| 36 | GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 37 | const GrRect* conservativeBounds = NULL); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 38 | GrClip(const GrIRect& rect); |
| 39 | GrClip(const GrRect& rect); |
| 40 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | ~GrClip(); |
| 42 | |
| 43 | GrClip& operator=(const GrClip& src); |
| 44 | |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 45 | bool hasConservativeBounds() const { return fConservativeBoundsValid; } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 47 | const GrRect& getConservativeBounds() const { return fConservativeBounds; } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 48 | |
| 49 | int getElementCount() const { return fList.count(); } |
| 50 | |
| 51 | GrClipType getElementType(int i) const { return fList[i].fType; } |
| 52 | |
| 53 | const GrPath& getPath(int i) const { |
| 54 | GrAssert(kPath_ClipType == fList[i].fType); |
| 55 | return fList[i].fPath; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 57 | |
| 58 | GrPathFill getPathFill(int i) const { |
| 59 | GrAssert(kPath_ClipType == fList[i].fType); |
| 60 | return fList[i].fPathFill; |
| 61 | } |
| 62 | |
| 63 | const GrRect& getRect(int i) const { |
| 64 | GrAssert(kRect_ClipType == fList[i].fType); |
| 65 | return fList[i].fRect; |
| 66 | } |
| 67 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 68 | GrSetOp getOp(int i) const { return fList[i].fOp; } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 69 | |
| 70 | bool isRect() const { |
| 71 | if (1 == fList.count() && kRect_ClipType == fList[0].fType) { |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 72 | // if we determined that the clip is a single rect |
| 73 | // we ought to have also used that rect as the bounds. |
| 74 | GrAssert(fConservativeBoundsValid); |
| 75 | GrAssert(fConservativeBounds == fList[0].fRect); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } else { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | bool isEmpty() const { return 0 == fList.count(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 85 | * Resets this clip to be empty |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | */ |
| 87 | void setEmpty(); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * If specified, the bounds parameter already takes (tx,ty) into account. |
| 91 | */ |
| 92 | void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 93 | const GrRect* conservativeBounds = NULL); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 94 | void setFromRect(const GrRect& rect); |
| 95 | void setFromIRect(const GrIRect& rect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 96 | |
| 97 | friend bool operator==(const GrClip& a, const GrClip& b) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 98 | if (a.fList.count() != b.fList.count()) { |
| 99 | return false; |
| 100 | } |
| 101 | int count = a.fList.count(); |
| 102 | for (int i = 0; i < count; ++i) { |
| 103 | if (a.fList[i] != b.fList[i]) { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | } |
| 109 | friend bool operator!=(const GrClip& a, const GrClip& b) { |
| 110 | return !(a == b); |
| 111 | } |
| 112 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | private: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 114 | struct Element { |
| 115 | GrClipType fType; |
| 116 | GrRect fRect; |
| 117 | GrPath fPath; |
| 118 | GrPathFill fPathFill; |
| 119 | GrSetOp fOp; |
| 120 | bool operator ==(const Element& e) const { |
| 121 | if (e.fType != fType || e.fOp != fOp) { |
| 122 | return false; |
| 123 | } |
| 124 | switch (fType) { |
| 125 | case kRect_ClipType: |
| 126 | return fRect == e.fRect; |
| 127 | break; |
| 128 | case kPath_ClipType: |
| 129 | return fPath == e.fPath; |
| 130 | default: |
| 131 | GrCrash("Unknown clip element type."); |
| 132 | return false; // suppress warning |
| 133 | } |
| 134 | } |
| 135 | bool operator !=(const Element& e) const { return !(*this == e); } |
| 136 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 138 | GrRect fConservativeBounds; |
| 139 | bool fConservativeBoundsValid; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 141 | enum { |
| 142 | kPreAllocElements = 4, |
| 143 | }; |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 144 | GrAlignedSTStorage<kPreAllocElements, Element> fListStorage; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 145 | GrTArray<Element> fList; |
| 146 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |