blob: daeb82629a66e52920a533ff23823c8aaef7f956 [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 {
robertphillips@google.com80214e22012-07-20 15:33:18 +000048 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
49 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000050 };
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.com80214e22012-07-20 15:33:18 +000083 * Moves the iterator to the topmost clip with the specified RegionOp
robertphillips@google.com5836b6d2012-07-18 12:06:15 +000084 * and returns that clip. If no clip with that op is found,
85 * returns NULL.
86 */
robertphillips@google.com80214e22012-07-20 15:33:18 +000087 const Clip* skipToTopmost(SkRegion::Op op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +000088
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
robertphillips@google.com80214e22012-07-20 15:33:18 +0000106 /**
107 * The B2TIter iterates from the bottom of the stack to the top.
108 * It inherits privately from Iter to prevent access to reverse iteration.
109 */
110 class B2TIter : private Iter {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000111 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000112 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000113
114 /**
115 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000116 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000117 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000118 B2TIter(const SkClipStack& stack)
119 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000120 }
121
122 using Iter::Clip;
123 using Iter::next;
124
125 /**
126 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000127 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000128 */
129 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000130 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000131 }
132
133 private:
134
135 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000136 };
137
138private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000139 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000140
141 SkDeque fDeque;
142 int fSaveCount;
143};
144
145#endif
146