blob: 60c4421103062ce938f9d72426c72df2c3184e2c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 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.
7 */
reed@google.com5c3d1472011-02-22 19:12:23 +00008#ifndef SkClipStack_DEFINED
9#define SkClipStack_DEFINED
10
11#include "SkDeque.h"
12#include "SkRegion.h"
13
bsalomon@google.com57788b52011-02-22 21:00:31 +000014struct SkRect;
reed@google.com5c3d1472011-02-22 19:12:23 +000015class SkPath;
16
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000017class SK_API SkClipStack {
reed@google.com5c3d1472011-02-22 19:12:23 +000018public:
19 SkClipStack();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000020 SkClipStack(const SkClipStack& b);
vandebo@chromium.org610f7162012-03-14 18:34:15 +000021 ~SkClipStack();
reed@google.com5c3d1472011-02-22 19:12:23 +000022
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000023 SkClipStack& operator=(const SkClipStack& b);
24 bool operator==(const SkClipStack& b) const;
25 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
26
reed@google.com5c3d1472011-02-22 19:12:23 +000027 void reset();
28
29 int getSaveCount() const { return fSaveCount; }
30 void save();
31 void restore();
32
reed@google.com115d9312012-05-16 18:50:40 +000033 void clipDevRect(const SkIRect& ir, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +000034 SkRect r;
35 r.set(ir);
reed@google.com00177082011-10-12 14:34:30 +000036 this->clipDevRect(r, op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +000037 }
reed@google.com00177082011-10-12 14:34:30 +000038 void clipDevRect(const SkRect&, SkRegion::Op, bool doAA);
39 void clipDevPath(const SkPath&, SkRegion::Op, bool doAA);
reed@google.com5c3d1472011-02-22 19:12:23 +000040
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000041private:
42 struct Rec;
43
44public:
45 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +000046 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000047 enum IterStart {
48 kFront_IterStart = SkDeque::Iter::kFront_IterStart,
49 kBack_IterStart = SkDeque::Iter::kBack_IterStart
50 };
51
bsalomon@google.comd302f142011-03-03 13:54:13 +000052 /**
53 * Creates an uninitialized iterator. Must be reset()
54 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000055 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +000056
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000057 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +000058
59 struct Clip {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000060 Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op),
61 fDoAA(false) {}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000062 friend bool operator==(const Clip& a, const Clip& b);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +000063 friend bool operator!=(const Clip& a, const Clip& b);
reed@google.com5c3d1472011-02-22 19:12:23 +000064 const SkRect* fRect; // if non-null, this is a rect clip
65 const SkPath* fPath; // if non-null, this is a path clip
66 SkRegion::Op fOp;
reed@google.com00177082011-10-12 14:34:30 +000067 bool fDoAA;
reed@google.com5c3d1472011-02-22 19:12:23 +000068 };
69
70 /**
71 * Return the clip for this element in the iterator. If next() returns
72 * NULL, then the iterator is done. The type of clip is determined by
73 * the pointers fRect and fPath:
74 *
75 * fRect==NULL fPath!=NULL path clip
76 * fRect!=NULL fPath==NULL rect clip
77 * fRect==NULL fPath==NULL empty clip
78 */
79 const Clip* next();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000080 const Clip* prev();
reed@google.com5c3d1472011-02-22 19:12:23 +000081
bsalomon@google.comd302f142011-03-03 13:54:13 +000082 /**
robertphillips@google.com5836b6d2012-07-18 12:06:15 +000083 * Moves the iterator to the last clip with the specified RegionOp
84 * and returns that clip. If no clip with that op is found,
85 * returns NULL.
86 */
87 const Clip* skipToLast(SkRegion::Op op);
88
89 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +000090 * Restarts the iterator on a clip stack.
91 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000092 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +000093
reed@google.com5c3d1472011-02-22 19:12:23 +000094 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +000095 const SkClipStack* fStack;
96 Clip fClip;
97 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000098
99 /**
100 * updateClip updates fClip to the current state of fIter. It unifies
101 * functionality needed by both next() and prev().
102 */
103 const Clip* updateClip(const SkClipStack::Rec* rec);
104 };
105
106 // Inherit privately from Iter to prevent access to reverse iteration
107 class B2FIter : private Iter {
108 public:
109 B2FIter() {}
110
111 /**
112 * Wrap Iter's 2 parameter ctor to force initialization to the
113 * beginning of the deque
114 */
115 B2FIter(const SkClipStack& stack)
116 : INHERITED(stack, kFront_IterStart) {
117 }
118
119 using Iter::Clip;
120 using Iter::next;
121
122 /**
123 * Wrap Iter::reset to force initialization to the
124 * beginning of the deque
125 */
126 void reset(const SkClipStack& stack) {
127 this->INHERITED::reset(stack, kFront_IterStart);
128 }
129
130 private:
131
132 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000133 };
134
135private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000136 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000137
138 SkDeque fDeque;
139 int fSaveCount;
140};
141
142#endif
143