blob: a23f99c1ce3ad865e6bd6df34eddc9450da93577 [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
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000042 bool requiresAA() const { return fRequiresAA; }
43
bsalomon@google.comd302f142011-03-03 13:54:13 +000044 int getElementCount() const { return fList.count(); }
45
46 GrClipType getElementType(int i) const { return fList[i].fType; }
47
48 const GrPath& getPath(int i) const {
49 GrAssert(kPath_ClipType == fList[i].fType);
50 return fList[i].fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000051 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000052
53 GrPathFill getPathFill(int i) const {
54 GrAssert(kPath_ClipType == fList[i].fType);
55 return fList[i].fPathFill;
56 }
57
58 const GrRect& getRect(int i) const {
59 GrAssert(kRect_ClipType == fList[i].fType);
60 return fList[i].fRect;
61 }
62
djsollen@google.comcd9d69b2011-03-14 20:30:14 +000063 GrSetOp getOp(int i) const { return fList[i].fOp; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000064
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000065 bool getDoAA(int i) const { return fList[i].fDoAA; }
66
bsalomon@google.comd302f142011-03-03 13:54:13 +000067 bool isRect() const {
bsalomon@google.comab3dee52011-08-29 15:18:41 +000068 if (1 == fList.count() && kRect_ClipType == fList[0].fType &&
69 (kIntersect_SetOp == fList[0].fOp ||
70 kReplace_SetOp == fList[0].fOp)) {
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;
robertphillips@google.comfa1d2912012-04-16 14:49:14 +0000119 bool fDoAA;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000120 bool operator ==(const Element& e) const {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +0000121 if (e.fType != fType || e.fOp != fOp || e.fDoAA != fDoAA) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000122 return false;
123 }
124 switch (fType) {
125 case kRect_ClipType:
126 return fRect == e.fRect;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000127 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
robertphillips@google.comfa1d2912012-04-16 14:49:14 +0000140 bool fRequiresAA;
141
bsalomon@google.comd302f142011-03-03 13:54:13 +0000142 enum {
143 kPreAllocElements = 4,
144 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000145 SkSTArray<kPreAllocElements, Element> fList;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000146};
reed@google.comac10a2d2010-12-22 21:39:39 +0000147#endif
148