blob: 7de6c10dd9b91cc447736e8f2fff35b6cdebdf71 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrClip_DEFINED
12#define GrClip_DEFINED
13
14#include "GrClipIterator.h"
15#include "GrRect.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000016#include "GrPath.h"
17#include "GrTArray.h"
bsalomon@google.coma55847b2011-04-20 15:47:04 +000018#include "GrTemplates.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000019
reed@google.comac10a2d2010-12-22 21:39:39 +000020
21class GrClip {
22public:
23 GrClip();
24 GrClip(const GrClip& src);
reed@google.com6f8f2922011-03-04 22:27:10 +000025 /**
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000026 * If specified, the conservativeBounds parameter already takes (tx,ty)
27 * into account.
reed@google.com6f8f2922011-03-04 22:27:10 +000028 */
29 GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty,
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000030 const GrRect* conservativeBounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000031 GrClip(const GrIRect& rect);
32 GrClip(const GrRect& rect);
33
reed@google.comac10a2d2010-12-22 21:39:39 +000034 ~GrClip();
35
36 GrClip& operator=(const GrClip& src);
37
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000038 bool hasConservativeBounds() const { return fConservativeBoundsValid; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000039
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000040 const GrRect& getConservativeBounds() const { return fConservativeBounds; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000041
42 int getElementCount() const { return fList.count(); }
43
44 GrClipType getElementType(int i) const { return fList[i].fType; }
45
46 const GrPath& getPath(int i) const {
47 GrAssert(kPath_ClipType == fList[i].fType);
48 return fList[i].fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000049 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000050
51 GrPathFill getPathFill(int i) const {
52 GrAssert(kPath_ClipType == fList[i].fType);
53 return fList[i].fPathFill;
54 }
55
56 const GrRect& getRect(int i) const {
57 GrAssert(kRect_ClipType == fList[i].fType);
58 return fList[i].fRect;
59 }
60
djsollen@google.comcd9d69b2011-03-14 20:30:14 +000061 GrSetOp getOp(int i) const { return fList[i].fOp; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000062
63 bool isRect() const {
64 if (1 == fList.count() && kRect_ClipType == fList[0].fType) {
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000065 // if we determined that the clip is a single rect
66 // we ought to have also used that rect as the bounds.
67 GrAssert(fConservativeBoundsValid);
68 GrAssert(fConservativeBounds == fList[0].fRect);
bsalomon@google.comd302f142011-03-03 13:54:13 +000069 return true;
70 } else {
71 return false;
72 }
73 }
74
75 bool isEmpty() const { return 0 == fList.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000076
77 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000078 * Resets this clip to be empty
reed@google.comac10a2d2010-12-22 21:39:39 +000079 */
80 void setEmpty();
reed@google.com6f8f2922011-03-04 22:27:10 +000081
82 /**
83 * If specified, the bounds parameter already takes (tx,ty) into account.
84 */
85 void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty,
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000086 const GrRect* conservativeBounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000087 void setFromRect(const GrRect& rect);
88 void setFromIRect(const GrIRect& rect);
reed@google.comac10a2d2010-12-22 21:39:39 +000089
90 friend bool operator==(const GrClip& a, const GrClip& b) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000091 if (a.fList.count() != b.fList.count()) {
92 return false;
93 }
94 int count = a.fList.count();
95 for (int i = 0; i < count; ++i) {
96 if (a.fList[i] != b.fList[i]) {
97 return false;
98 }
99 }
100 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 }
102 friend bool operator!=(const GrClip& a, const GrClip& b) {
103 return !(a == b);
104 }
105
reed@google.comac10a2d2010-12-22 21:39:39 +0000106private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000107 struct Element {
108 GrClipType fType;
109 GrRect fRect;
110 GrPath fPath;
111 GrPathFill fPathFill;
112 GrSetOp fOp;
113 bool operator ==(const Element& e) const {
114 if (e.fType != fType || e.fOp != fOp) {
115 return false;
116 }
117 switch (fType) {
118 case kRect_ClipType:
119 return fRect == e.fRect;
120 break;
121 case kPath_ClipType:
122 return fPath == e.fPath;
123 default:
124 GrCrash("Unknown clip element type.");
125 return false; // suppress warning
126 }
127 }
128 bool operator !=(const Element& e) const { return !(*this == e); }
129 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000131 GrRect fConservativeBounds;
132 bool fConservativeBoundsValid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
bsalomon@google.comd302f142011-03-03 13:54:13 +0000134 enum {
135 kPreAllocElements = 4,
136 };
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000137 GrAlignedSTStorage<kPreAllocElements, Element> fListStorage;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000138 GrTArray<Element> fList;
139};
reed@google.comac10a2d2010-12-22 21:39:39 +0000140#endif
141