blob: 4fd8ddd17690adab283f771979594820ee664a23 [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
bsalomon@google.com8d033a12012-04-27 15:52:53 +000017#include "SkPath.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000018#include "SkTArray.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
20class GrClip {
21public:
22 GrClip();
23 GrClip(const GrClip& src);
reed@google.com6f8f2922011-03-04 22:27:10 +000024 /**
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000025 * The conservativeBounds parameter already takes (tx,ty) into account.
reed@google.com6f8f2922011-03-04 22:27:10 +000026 */
27 GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty,
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000028 const GrRect& conservativeBounds);
29 explicit GrClip(const GrIRect& rect);
30 explicit GrClip(const GrRect& rect);
bsalomon@google.comd302f142011-03-03 13:54:13 +000031
reed@google.comac10a2d2010-12-22 21:39:39 +000032 ~GrClip();
33
34 GrClip& operator=(const GrClip& src);
35
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000036 const GrRect& getConservativeBounds() const {
37 GrAssert(fConservativeBoundsValid);
38 return fConservativeBounds;
39 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000040
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000041 bool requiresAA() const { return fRequiresAA; }
42
bsalomon@google.comd302f142011-03-03 13:54:13 +000043 int getElementCount() const { return fList.count(); }
44
45 GrClipType getElementType(int i) const { return fList[i].fType; }
46
bsalomon@google.com8d033a12012-04-27 15:52:53 +000047 const SkPath& getPath(int i) const {
bsalomon@google.comd302f142011-03-03 13:54:13 +000048 GrAssert(kPath_ClipType == fList[i].fType);
49 return fList[i].fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000050 }
bsalomon@google.comd302f142011-03-03 13:54:13 +000051
52 GrPathFill getPathFill(int i) const {
53 GrAssert(kPath_ClipType == fList[i].fType);
54 return fList[i].fPathFill;
55 }
56
57 const GrRect& getRect(int i) const {
58 GrAssert(kRect_ClipType == fList[i].fType);
59 return fList[i].fRect;
60 }
61
robertphillips@google.com0f191f32012-04-25 15:23:36 +000062 SkRegion::Op getOp(int i) const { return fList[i].fOp; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000063
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000064 bool getDoAA(int i) const { return fList[i].fDoAA; }
65
bsalomon@google.comd302f142011-03-03 13:54:13 +000066 bool isRect() const {
bsalomon@google.comab3dee52011-08-29 15:18:41 +000067 if (1 == fList.count() && kRect_ClipType == fList[0].fType &&
robertphillips@google.com0f191f32012-04-25 15:23:36 +000068 (SkRegion::kIntersect_Op == fList[0].fOp ||
69 SkRegion::kReplace_Op == fList[0].fOp)) {
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +000070 // if we determined that the clip is a single rect
71 // we ought to have also used that rect as the bounds.
72 GrAssert(fConservativeBoundsValid);
73 GrAssert(fConservativeBounds == fList[0].fRect);
bsalomon@google.comd302f142011-03-03 13:54:13 +000074 return true;
75 } else {
76 return false;
77 }
78 }
79
bsalomon@google.coma3201942012-06-21 19:58:20 +000080 // FIXME: This word "empty" is confusing. It means that the clip has no
81 // elements (it is the infinite plane) not that it has no area.
bsalomon@google.comd302f142011-03-03 13:54:13 +000082 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,
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000093 const GrRect& conservativeBounds);
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 {
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000115 GrClipType fType;
116 GrRect fRect;
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000117 SkPath fPath;
robertphillips@google.com0f191f32012-04-25 15:23:36 +0000118 GrPathFill fPathFill;
119 SkRegion::Op fOp;
120 bool fDoAA;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000121 bool operator ==(const Element& e) const {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +0000122 if (e.fType != fType || e.fOp != fOp || e.fDoAA != fDoAA) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000123 return false;
124 }
125 switch (fType) {
126 case kRect_ClipType:
127 return fRect == e.fRect;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000128 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
robertphillips@google.comfa1d2912012-04-16 14:49:14 +0000141 bool fRequiresAA;
142
bsalomon@google.comd302f142011-03-03 13:54:13 +0000143 enum {
144 kPreAllocElements = 4,
145 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000146 SkSTArray<kPreAllocElements, Element> fList;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000147};
reed@google.comac10a2d2010-12-22 21:39:39 +0000148#endif
149