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" |
| 23 | #include "GrTDArray.h" |
| 24 | |
| 25 | class GrClip { |
| 26 | public: |
| 27 | GrClip(); |
| 28 | GrClip(const GrClip& src); |
| 29 | GrClip(GrClipIterator* iter); |
| 30 | ~GrClip(); |
| 31 | |
| 32 | GrClip& operator=(const GrClip& src); |
| 33 | |
| 34 | bool isEmpty() const { return fBounds.isEmpty(); } |
| 35 | bool isComplex() const { return fList.count() > 0; } |
| 36 | bool isRect() const { |
| 37 | return !this->isEmpty() && !this->isComplex(); |
| 38 | } |
| 39 | |
| 40 | const GrIRect& getBounds() const { return fBounds; } |
| 41 | |
| 42 | /** |
| 43 | * Resets this clip to be empty (fBounds is empty, and fList is empty) |
| 44 | */ |
| 45 | void setEmpty(); |
| 46 | |
| 47 | /** |
| 48 | * Resets this clip to have fBounds == rect, and fList is empty. |
| 49 | */ |
| 50 | void setRect(const GrIRect& rect); |
| 51 | |
| 52 | /** |
| 53 | * Append a rect to an existing clip. The call must ensure that rect does |
| 54 | * not overlap with any previous rect in this clip (either from setRect |
| 55 | * or addRect). fBounds is automatically updated to reflect the union of |
| 56 | * all rects that have been added. |
| 57 | */ |
| 58 | void addRect(const GrIRect&); |
| 59 | |
| 60 | void setFromIterator(GrClipIterator* iter); |
| 61 | |
| 62 | friend bool operator==(const GrClip& a, const GrClip& b) { |
| 63 | return a.fBounds == b.fBounds && a.fList == b.fList; |
| 64 | } |
| 65 | friend bool operator!=(const GrClip& a, const GrClip& b) { |
| 66 | return !(a == b); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Return the number of rects in this clip: 0 for empty, 1 for a rect, |
| 71 | * or N for a complex clip. |
| 72 | */ |
| 73 | int countRects() const { |
| 74 | return this->isEmpty() ? 0 : GrMax<int>(1, fList.count()); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return an array of rects for this clip. Use countRects() to know the |
| 79 | * number of entries. |
| 80 | */ |
| 81 | const GrIRect* getRects() const { |
| 82 | return fList.count() > 0 ? fList.begin() : &fBounds; |
| 83 | } |
| 84 | |
| 85 | #if GR_DEBUG |
| 86 | void validate() const; |
| 87 | #else |
| 88 | void validate() const {} |
| 89 | #endif |
| 90 | |
| 91 | private: |
| 92 | GrTDArray<GrIRect> fList; |
| 93 | GrIRect fBounds; |
| 94 | }; |
| 95 | |
| 96 | class GrClipIter : public GrClipIterator { |
| 97 | public: |
| 98 | GrClipIter(const GrClip& clip) : fClip(&clip), fIndex(0) {} |
| 99 | GrClipIter() : fClip(NULL), fIndex(0) {} |
| 100 | |
| 101 | void reset(const GrClip& clip); |
| 102 | |
| 103 | virtual bool isDone(); |
| 104 | virtual void rewind(); |
| 105 | virtual void getRect(GrIRect* r); |
| 106 | virtual void next(); |
| 107 | virtual void computeBounds(GrIRect* r); |
| 108 | |
| 109 | private: |
| 110 | const GrClip* fClip; |
| 111 | int fIndex; |
| 112 | }; |
| 113 | |
| 114 | #endif |
| 115 | |