blob: 54082b72df18c30da7c4329af95ae5c447db79f5 [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 /**
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000032 * If specified, the conservativeBounds parameter already takes (tx,ty)
33 * into account.
reed@google.com6f8f2922011-03-04 22:27:10 +000034 */
35 GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty,
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000036 const GrRect* conservativeBounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000037 GrClip(const GrIRect& rect);
38 GrClip(const GrRect& rect);
39
reed@google.comac10a2d2010-12-22 21:39:39 +000040 ~GrClip();
41
42 GrClip& operator=(const GrClip& src);
43
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000044 bool hasConservativeBounds() const { return fConservativeBoundsValid; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000045
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000046 const GrRect& getConservativeBounds() const { return fConservativeBounds; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000047
48 int getElementCount() const { return fList.count(); }
49
50 GrClipType getElementType(int i) const { return fList[i].fType; }
51
52 const GrPath& getPath(int i) const {
53 GrAssert(kPath_ClipType == fList[i].fType);
54 return fList[i].fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000055 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000056
57 GrPathFill getPathFill(int i) const {
58 GrAssert(kPath_ClipType == fList[i].fType);
59 return fList[i].fPathFill;
60 }
61
62 const GrRect& getRect(int i) const {
63 GrAssert(kRect_ClipType == fList[i].fType);
64 return fList[i].fRect;
65 }
66
djsollen@google.comcd9d69b2011-03-14 20:30:14 +000067 GrSetOp getOp(int i) const { return fList[i].fOp; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000068
69 bool isRect() const {
70 if (1 == fList.count() && kRect_ClipType == fList[0].fType) {
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000071 // if we determined that the clip is a single rect
72 // we ought to have also used that rect as the bounds.
73 GrAssert(fConservativeBoundsValid);
74 GrAssert(fConservativeBounds == fList[0].fRect);
bsalomon@google.comd302f142011-03-03 13:54:13 +000075 return true;
76 } else {
77 return false;
78 }
79 }
80
81 bool isEmpty() const { return 0 == fList.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000082
83 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000084 * Resets this clip to be empty
reed@google.comac10a2d2010-12-22 21:39:39 +000085 */
86 void setEmpty();
reed@google.com6f8f2922011-03-04 22:27:10 +000087
88 /**
89 * If specified, the bounds parameter already takes (tx,ty) into account.
90 */
91 void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty,
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000092 const GrRect* conservativeBounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000093 void setFromRect(const GrRect& rect);
94 void setFromIRect(const GrIRect& rect);
reed@google.comac10a2d2010-12-22 21:39:39 +000095
96 friend bool operator==(const GrClip& a, const GrClip& b) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000097 if (a.fList.count() != b.fList.count()) {
98 return false;
99 }
100 int count = a.fList.count();
101 for (int i = 0; i < count; ++i) {
102 if (a.fList[i] != b.fList[i]) {
103 return false;
104 }
105 }
106 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 }
108 friend bool operator!=(const GrClip& a, const GrClip& b) {
109 return !(a == b);
110 }
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000113 struct Element {
114 GrClipType fType;
115 GrRect fRect;
116 GrPath fPath;
117 GrPathFill fPathFill;
118 GrSetOp fOp;
119 bool operator ==(const Element& e) const {
120 if (e.fType != fType || e.fOp != fOp) {
121 return false;
122 }
123 switch (fType) {
124 case kRect_ClipType:
125 return fRect == e.fRect;
126 break;
127 case kPath_ClipType:
128 return fPath == e.fPath;
129 default:
130 GrCrash("Unknown clip element type.");
131 return false; // suppress warning
132 }
133 }
134 bool operator !=(const Element& e) const { return !(*this == e); }
135 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000137 GrRect fConservativeBounds;
138 bool fConservativeBoundsValid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
bsalomon@google.comd302f142011-03-03 13:54:13 +0000140 enum {
141 kPreAllocElements = 4,
142 };
143 uint8_t fListMemory[sizeof(Element) * kPreAllocElements];
144 GrTArray<Element> fList;
145};
reed@google.comac10a2d2010-12-22 21:39:39 +0000146#endif
147