blob: 414a6d6806f56cac2d64ccdf40e55ee39ac22c96 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
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.comd302f142011-03-03 13:54:13 +000023#include "GrPath.h"
24#include "GrTArray.h"
25
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27class GrClip {
28public:
29 GrClip();
30 GrClip(const GrClip& src);
bsalomon@google.comd302f142011-03-03 13:54:13 +000031 GrClip(GrClipIterator* iter, const GrRect* bounds = NULL);
32 GrClip(const GrIRect& rect);
33 GrClip(const GrRect& rect);
34
reed@google.comac10a2d2010-12-22 21:39:39 +000035 ~GrClip();
36
37 GrClip& operator=(const GrClip& src);
38
bsalomon@google.comd302f142011-03-03 13:54:13 +000039 bool hasBounds() const { return fBoundsValid; }
40
41 const GrRect& getBounds() const { return fBounds; }
42
43 int getElementCount() const { return fList.count(); }
44
45 GrClipType getElementType(int i) const { return fList[i].fType; }
46
47 const GrPath& getPath(int i) const {
48 GrAssert(kPath_ClipType == fList[i].fType);
49 return fList[i].fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000050 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000051
52 GrPathFill getPathFill(int i) const {
53 GrAssert(kPath_ClipType == fList[i].fType);
54 return fList[i].fPathFill;
55 }
56
57 const GrRect& getRect(int i) const {
58 GrAssert(kRect_ClipType == fList[i].fType);
59 return fList[i].fRect;
60 }
61
62 const GrSetOp getOp(int i) const { return fList[i].fOp; }
63
64 bool isRect() const {
65 if (1 == fList.count() && kRect_ClipType == fList[0].fType) {
66 GrAssert(fBoundsValid);
67 GrAssert(fBounds == fList[0].fRect);
68 return true;
69 } else {
70 return false;
71 }
72 }
73
74 bool isEmpty() const { return 0 == fList.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000075
76 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000077 * Resets this clip to be empty
reed@google.comac10a2d2010-12-22 21:39:39 +000078 */
79 void setEmpty();
bsalomon@google.comd302f142011-03-03 13:54:13 +000080 void setFromIterator(GrClipIterator* iter, const GrRect* bounds = NULL);
81 void setFromRect(const GrRect& rect);
82 void setFromIRect(const GrIRect& rect);
reed@google.comac10a2d2010-12-22 21:39:39 +000083
84 friend bool operator==(const GrClip& a, const GrClip& b) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000085 if (a.fList.count() != b.fList.count()) {
86 return false;
87 }
88 int count = a.fList.count();
89 for (int i = 0; i < count; ++i) {
90 if (a.fList[i] != b.fList[i]) {
91 return false;
92 }
93 }
94 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +000095 }
96 friend bool operator!=(const GrClip& a, const GrClip& b) {
97 return !(a == b);
98 }
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000101 struct Element {
102 GrClipType fType;
103 GrRect fRect;
104 GrPath fPath;
105 GrPathFill fPathFill;
106 GrSetOp fOp;
107 bool operator ==(const Element& e) const {
108 if (e.fType != fType || e.fOp != fOp) {
109 return false;
110 }
111 switch (fType) {
112 case kRect_ClipType:
113 return fRect == e.fRect;
114 break;
115 case kPath_ClipType:
116 return fPath == e.fPath;
117 default:
118 GrCrash("Unknown clip element type.");
119 return false; // suppress warning
120 }
121 }
122 bool operator !=(const Element& e) const { return !(*this == e); }
123 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
bsalomon@google.comd302f142011-03-03 13:54:13 +0000125 GrRect fBounds;
126 bool fBoundsValid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
bsalomon@google.comd302f142011-03-03 13:54:13 +0000128 enum {
129 kPreAllocElements = 4,
130 };
131 uint8_t fListMemory[sizeof(Element) * kPreAllocElements];
132 GrTArray<Element> fList;
133};
reed@google.comac10a2d2010-12-22 21:39:39 +0000134#endif
135