blob: 9e235bfe910eca3938ee89d33180fba03792418d [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"
bsalomon@google.coma55847b2011-04-20 15:47:04 +000017#include "GrTemplates.h"
bsalomon@google.comd302f142011-03-03 13:54:13 +000018
bsalomon@google.com49313f62011-09-14 13:54:05 +000019#include "SkTArray.h"
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 {
bsalomon@google.comab3dee52011-08-29 15:18:41 +000064 if (1 == fList.count() && kRect_ClipType == fList[0].fType &&
65 (kIntersect_SetOp == fList[0].fOp ||
66 kReplace_SetOp == fList[0].fOp)) {
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000067 // if we determined that the clip is a single rect
68 // we ought to have also used that rect as the bounds.
69 GrAssert(fConservativeBoundsValid);
70 GrAssert(fConservativeBounds == fList[0].fRect);
bsalomon@google.comd302f142011-03-03 13:54:13 +000071 return true;
72 } else {
73 return false;
74 }
75 }
76
77 bool isEmpty() const { return 0 == fList.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000078
79 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000080 * Resets this clip to be empty
reed@google.comac10a2d2010-12-22 21:39:39 +000081 */
82 void setEmpty();
reed@google.com6f8f2922011-03-04 22:27:10 +000083
84 /**
85 * If specified, the bounds parameter already takes (tx,ty) into account.
86 */
87 void setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty,
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000088 const GrRect* conservativeBounds = NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +000089 void setFromRect(const GrRect& rect);
90 void setFromIRect(const GrIRect& rect);
reed@google.comac10a2d2010-12-22 21:39:39 +000091
92 friend bool operator==(const GrClip& a, const GrClip& b) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000093 if (a.fList.count() != b.fList.count()) {
94 return false;
95 }
96 int count = a.fList.count();
97 for (int i = 0; i < count; ++i) {
98 if (a.fList[i] != b.fList[i]) {
99 return false;
100 }
101 }
102 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 }
104 friend bool operator!=(const GrClip& a, const GrClip& b) {
105 return !(a == b);
106 }
107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000109 struct Element {
110 GrClipType fType;
111 GrRect fRect;
112 GrPath fPath;
113 GrPathFill fPathFill;
114 GrSetOp fOp;
115 bool operator ==(const Element& e) const {
116 if (e.fType != fType || e.fOp != fOp) {
117 return false;
118 }
119 switch (fType) {
120 case kRect_ClipType:
121 return fRect == e.fRect;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000122 case kPath_ClipType:
123 return fPath == e.fPath;
124 default:
125 GrCrash("Unknown clip element type.");
126 return false; // suppress warning
127 }
128 }
129 bool operator !=(const Element& e) const { return !(*this == e); }
130 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000132 GrRect fConservativeBounds;
133 bool fConservativeBoundsValid;
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
bsalomon@google.comd302f142011-03-03 13:54:13 +0000135 enum {
136 kPreAllocElements = 4,
137 };
bsalomon@google.com49313f62011-09-14 13:54:05 +0000138 SkAlignedSTStorage<kPreAllocElements, Element> fListStorage;
139 SkTArray<Element> fList;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000140};
reed@google.comac10a2d2010-12-22 21:39:39 +0000141#endif
142