blob: 717dfe659791762df18762a200018505944812d0 [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"
bsalomon@google.coma55847b2011-04-20 15:47:04 +000025#include "GrTemplates.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28class GrClip {
29public:
30 GrClip();
31 GrClip(const GrClip& src);
reed@google.com6f8f2922011-03-04 22:27:10 +000032 /**
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000033 * If specified, the conservativeBounds parameter already takes (tx,ty)
34 * into account.
reed@google.com6f8f2922011-03-04 22:27:10 +000035 */
36 GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty,
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000037 const GrRect* conservativeBounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000038 GrClip(const GrIRect& rect);
39 GrClip(const GrRect& rect);
40
reed@google.comac10a2d2010-12-22 21:39:39 +000041 ~GrClip();
42
43 GrClip& operator=(const GrClip& src);
44
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000045 bool hasConservativeBounds() const { return fConservativeBoundsValid; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000046
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000047 const GrRect& getConservativeBounds() const { return fConservativeBounds; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000048
49 int getElementCount() const { return fList.count(); }
50
51 GrClipType getElementType(int i) const { return fList[i].fType; }
52
53 const GrPath& getPath(int i) const {
54 GrAssert(kPath_ClipType == fList[i].fType);
55 return fList[i].fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000056 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000057
58 GrPathFill getPathFill(int i) const {
59 GrAssert(kPath_ClipType == fList[i].fType);
60 return fList[i].fPathFill;
61 }
62
63 const GrRect& getRect(int i) const {
64 GrAssert(kRect_ClipType == fList[i].fType);
65 return fList[i].fRect;
66 }
67
djsollen@google.comcd9d69b2011-03-14 20:30:14 +000068 GrSetOp getOp(int i) const { return fList[i].fOp; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000069
70 bool isRect() const {
71 if (1 == fList.count() && kRect_ClipType == fList[0].fType) {
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000072 // if we determined that the clip is a single rect
73 // we ought to have also used that rect as the bounds.
74 GrAssert(fConservativeBoundsValid);
75 GrAssert(fConservativeBounds == fList[0].fRect);
bsalomon@google.comd302f142011-03-03 13:54:13 +000076 return true;
77 } else {
78 return false;
79 }
80 }
81
82 bool isEmpty() const { return 0 == fList.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000083
84 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000085 * Resets this clip to be empty
reed@google.comac10a2d2010-12-22 21:39:39 +000086 */
87 void setEmpty();
reed@google.com6f8f2922011-03-04 22:27:10 +000088
89 /**
90 * If specified, the bounds parameter already takes (tx,ty) into account.
91 */
92 void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty,
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000093 const GrRect* conservativeBounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000094 void setFromRect(const GrRect& rect);
95 void setFromIRect(const GrIRect& rect);
reed@google.comac10a2d2010-12-22 21:39:39 +000096
97 friend bool operator==(const GrClip& a, const GrClip& b) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000098 if (a.fList.count() != b.fList.count()) {
99 return false;
100 }
101 int count = a.fList.count();
102 for (int i = 0; i < count; ++i) {
103 if (a.fList[i] != b.fList[i]) {
104 return false;
105 }
106 }
107 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 }
109 friend bool operator!=(const GrClip& a, const GrClip& b) {
110 return !(a == b);
111 }
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000114 struct Element {
115 GrClipType fType;
116 GrRect fRect;
117 GrPath fPath;
118 GrPathFill fPathFill;
119 GrSetOp fOp;
120 bool operator ==(const Element& e) const {
121 if (e.fType != fType || e.fOp != fOp) {
122 return false;
123 }
124 switch (fType) {
125 case kRect_ClipType:
126 return fRect == e.fRect;
127 break;
128 case kPath_ClipType:
129 return fPath == e.fPath;
130 default:
131 GrCrash("Unknown clip element type.");
132 return false; // suppress warning
133 }
134 }
135 bool operator !=(const Element& e) const { return !(*this == e); }
136 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000137
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000138 GrRect fConservativeBounds;
139 bool fConservativeBoundsValid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000140
bsalomon@google.comd302f142011-03-03 13:54:13 +0000141 enum {
142 kPreAllocElements = 4,
143 };
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000144 GrAlignedSTStorage<kPreAllocElements, Element> fListStorage;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000145 GrTArray<Element> fList;
146};
reed@google.comac10a2d2010-12-22 21:39:39 +0000147#endif
148