| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrClip_DEFINED |
| 12 | #define GrClip_DEFINED |
| 13 | |
| 14 | #include "GrClipIterator.h" |
| 15 | #include "GrRect.h" |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 16 | |
| bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 17 | #include "SkPath.h" |
| bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 18 | #include "SkTArray.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | |
| 20 | class GrClip { |
| 21 | public: |
| 22 | GrClip(); |
| 23 | GrClip(const GrClip& src); |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 24 | /** |
| robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 25 | * The conservativeBounds parameter already takes (tx,ty) into account. |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 26 | */ |
| 27 | GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
| robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 28 | const GrRect& conservativeBounds); |
| 29 | explicit GrClip(const GrIRect& rect); |
| 30 | explicit GrClip(const GrRect& rect); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 31 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | ~GrClip(); |
| 33 | |
| 34 | GrClip& operator=(const GrClip& src); |
| 35 | |
| robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 36 | const GrRect& getConservativeBounds() const { |
| 37 | GrAssert(fConservativeBoundsValid); |
| 38 | return fConservativeBounds; |
| 39 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 40 | |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 41 | bool requiresAA() const { return fRequiresAA; } |
| 42 | |
| robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame^] | 43 | class Iter { |
| 44 | public: |
| 45 | enum IterStart { |
| 46 | kBottom_IterStart, |
| 47 | kTop_IterStart |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Creates an uninitialized iterator. Must be reset() |
| 52 | */ |
| 53 | Iter(); |
| 54 | |
| 55 | Iter(const GrClip& stack, IterStart startLoc); |
| 56 | |
| 57 | struct Clip { |
| 58 | Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op), |
| 59 | fDoAA(false) {} |
| 60 | |
| 61 | const SkRect* fRect; // if non-null, this is a rect clip |
| 62 | const SkPath* fPath; // if non-null, this is a path clip |
| 63 | SkRegion::Op fOp; |
| 64 | bool fDoAA; |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Return the clip for this element in the iterator. If next() returns |
| 69 | * NULL, then the iterator is done. The type of clip is determined by |
| 70 | * the pointers fRect and fPath: |
| 71 | * |
| 72 | * fRect==NULL fPath!=NULL path clip |
| 73 | * fRect!=NULL fPath==NULL rect clip |
| 74 | * fRect==NULL fPath==NULL empty clip |
| 75 | */ |
| 76 | const Clip* next(); |
| 77 | const Clip* prev(); |
| 78 | |
| 79 | /** |
| 80 | * Moves the iterator to the topmost clip with the specified RegionOp |
| 81 | * and returns that clip. If no clip with that op is found, |
| 82 | * returns NULL. |
| 83 | */ |
| 84 | const Clip* skipToTopmost(SkRegion::Op op); |
| 85 | |
| 86 | /** |
| 87 | * Restarts the iterator on a clip stack. |
| 88 | */ |
| 89 | void reset(const GrClip& stack, IterStart startLoc); |
| 90 | |
| 91 | private: |
| 92 | const GrClip* fStack; |
| 93 | Clip fClip; |
| 94 | int fCurIndex; |
| 95 | |
| 96 | /** |
| 97 | * updateClip updates fClip to represent the clip in the index slot of |
| 98 | * GrClip's list. * It unifies functionality needed by both next() and |
| 99 | * prev(). |
| 100 | */ |
| 101 | const Clip* updateClip(int index); |
| 102 | }; |
| 103 | |
| 104 | private: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 105 | int getElementCount() const { return fList.count(); } |
| 106 | |
| 107 | GrClipType getElementType(int i) const { return fList[i].fType; } |
| 108 | |
| bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 109 | const SkPath& getPath(int i) const { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 110 | GrAssert(kPath_ClipType == fList[i].fType); |
| 111 | return fList[i].fPath; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 112 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 113 | |
| 114 | GrPathFill getPathFill(int i) const { |
| 115 | GrAssert(kPath_ClipType == fList[i].fType); |
| 116 | return fList[i].fPathFill; |
| 117 | } |
| 118 | |
| 119 | const GrRect& getRect(int i) const { |
| 120 | GrAssert(kRect_ClipType == fList[i].fType); |
| 121 | return fList[i].fRect; |
| 122 | } |
| 123 | |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 124 | SkRegion::Op getOp(int i) const { return fList[i].fOp; } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 125 | |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 126 | bool getDoAA(int i) const { return fList[i].fDoAA; } |
| 127 | |
| robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame^] | 128 | public: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 129 | bool isRect() const { |
| bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 130 | if (1 == fList.count() && kRect_ClipType == fList[0].fType && |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 131 | (SkRegion::kIntersect_Op == fList[0].fOp || |
| 132 | SkRegion::kReplace_Op == fList[0].fOp)) { |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 133 | // if we determined that the clip is a single rect |
| 134 | // we ought to have also used that rect as the bounds. |
| 135 | GrAssert(fConservativeBoundsValid); |
| 136 | GrAssert(fConservativeBounds == fList[0].fRect); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 137 | return true; |
| 138 | } else { |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 143 | // FIXME: This word "empty" is confusing. It means that the clip has no |
| 144 | // elements (it is the infinite plane) not that it has no area. |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 145 | bool isEmpty() const { return 0 == fList.count(); } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | |
| 147 | /** |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 148 | * Resets this clip to be empty |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | */ |
| 150 | void setEmpty(); |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * If specified, the bounds parameter already takes (tx,ty) into account. |
| 154 | */ |
| 155 | void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
| robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 156 | const GrRect& conservativeBounds); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 157 | void setFromRect(const GrRect& rect); |
| 158 | void setFromIRect(const GrIRect& rect); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | |
| 160 | friend bool operator==(const GrClip& a, const GrClip& b) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 161 | if (a.fList.count() != b.fList.count()) { |
| 162 | return false; |
| 163 | } |
| 164 | int count = a.fList.count(); |
| 165 | for (int i = 0; i < count; ++i) { |
| 166 | if (a.fList[i] != b.fList[i]) { |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | return true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 171 | } |
| 172 | friend bool operator!=(const GrClip& a, const GrClip& b) { |
| 173 | return !(a == b); |
| 174 | } |
| 175 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | private: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 177 | struct Element { |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 178 | GrClipType fType; |
| 179 | GrRect fRect; |
| bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 180 | SkPath fPath; |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 181 | GrPathFill fPathFill; |
| 182 | SkRegion::Op fOp; |
| 183 | bool fDoAA; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 184 | bool operator ==(const Element& e) const { |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 185 | if (e.fType != fType || e.fOp != fOp || e.fDoAA != fDoAA) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | switch (fType) { |
| 189 | case kRect_ClipType: |
| 190 | return fRect == e.fRect; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 191 | case kPath_ClipType: |
| 192 | return fPath == e.fPath; |
| 193 | default: |
| 194 | GrCrash("Unknown clip element type."); |
| 195 | return false; // suppress warning |
| 196 | } |
| 197 | } |
| 198 | bool operator !=(const Element& e) const { return !(*this == e); } |
| 199 | }; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 201 | GrRect fConservativeBounds; |
| 202 | bool fConservativeBoundsValid; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 203 | |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 204 | bool fRequiresAA; |
| 205 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 206 | enum { |
| 207 | kPreAllocElements = 4, |
| 208 | }; |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 209 | SkSTArray<kPreAllocElements, Element> fList; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 210 | }; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |