blob: 64c9bbbb34739168b9560553b6d6dfcc6668302f [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);
reed@google.com6f8f2922011-03-04 22:27:10 +000031 /**
32 * If specified, the bounds parameter already takes (tx,ty) into account.
33 */
34 GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty,
35 const GrRect* bounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000036 GrClip(const GrIRect& rect);
37 GrClip(const GrRect& rect);
38
reed@google.comac10a2d2010-12-22 21:39:39 +000039 ~GrClip();
40
41 GrClip& operator=(const GrClip& src);
42
bsalomon@google.comd302f142011-03-03 13:54:13 +000043 bool hasBounds() const { return fBoundsValid; }
44
45 const GrRect& getBounds() const { return fBounds; }
46
47 int getElementCount() const { return fList.count(); }
48
49 GrClipType getElementType(int i) const { return fList[i].fType; }
50
51 const GrPath& getPath(int i) const {
52 GrAssert(kPath_ClipType == fList[i].fType);
53 return fList[i].fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000054 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000055
56 GrPathFill getPathFill(int i) const {
57 GrAssert(kPath_ClipType == fList[i].fType);
58 return fList[i].fPathFill;
59 }
60
61 const GrRect& getRect(int i) const {
62 GrAssert(kRect_ClipType == fList[i].fType);
63 return fList[i].fRect;
64 }
65
66 const GrSetOp getOp(int i) const { return fList[i].fOp; }
67
68 bool isRect() const {
69 if (1 == fList.count() && kRect_ClipType == fList[0].fType) {
70 GrAssert(fBoundsValid);
71 GrAssert(fBounds == fList[0].fRect);
72 return true;
73 } else {
74 return false;
75 }
76 }
77
78 bool isEmpty() const { return 0 == fList.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000079
80 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000081 * Resets this clip to be empty
reed@google.comac10a2d2010-12-22 21:39:39 +000082 */
83 void setEmpty();
reed@google.com6f8f2922011-03-04 22:27:10 +000084
85 /**
86 * If specified, the bounds parameter already takes (tx,ty) into account.
87 */
88 void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty,
89 const GrRect* bounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000090 void setFromRect(const GrRect& rect);
91 void setFromIRect(const GrIRect& rect);
reed@google.comac10a2d2010-12-22 21:39:39 +000092
93 friend bool operator==(const GrClip& a, const GrClip& b) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000094 if (a.fList.count() != b.fList.count()) {
95 return false;
96 }
97 int count = a.fList.count();
98 for (int i = 0; i < count; ++i) {
99 if (a.fList[i] != b.fList[i]) {
100 return false;
101 }
102 }
103 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 }
105 friend bool operator!=(const GrClip& a, const GrClip& b) {
106 return !(a == b);
107 }
108
reed@google.comac10a2d2010-12-22 21:39:39 +0000109private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000110 struct Element {
111 GrClipType fType;
112 GrRect fRect;
113 GrPath fPath;
114 GrPathFill fPathFill;
115 GrSetOp fOp;
116 bool operator ==(const Element& e) const {
117 if (e.fType != fType || e.fOp != fOp) {
118 return false;
119 }
120 switch (fType) {
121 case kRect_ClipType:
122 return fRect == e.fRect;
123 break;
124 case kPath_ClipType:
125 return fPath == e.fPath;
126 default:
127 GrCrash("Unknown clip element type.");
128 return false; // suppress warning
129 }
130 }
131 bool operator !=(const Element& e) const { return !(*this == e); }
132 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
bsalomon@google.comd302f142011-03-03 13:54:13 +0000134 GrRect fBounds;
135 bool fBoundsValid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
bsalomon@google.comd302f142011-03-03 13:54:13 +0000137 enum {
138 kPreAllocElements = 4,
139 };
140 uint8_t fListMemory[sizeof(Element) * kPreAllocElements];
141 GrTArray<Element> fList;
142};
reed@google.comac10a2d2010-12-22 21:39:39 +0000143#endif
144