blob: 7d1bbcfef2a514de187ecb1abcc47c0833f9ed3c [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 /**
83 * Restarts the iterator on a clip stack.
84 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000085 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +000086
reed@google.com5c3d1472011-02-22 19:12:23 +000087 private:
88 Clip fClip;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000089 SkDeque::Iter fIter;
90
91 /**
92 * updateClip updates fClip to the current state of fIter. It unifies
93 * functionality needed by both next() and prev().
94 */
95 const Clip* updateClip(const SkClipStack::Rec* rec);
96 };
97
98 // Inherit privately from Iter to prevent access to reverse iteration
99 class B2FIter : private Iter {
100 public:
101 B2FIter() {}
102
103 /**
104 * Wrap Iter's 2 parameter ctor to force initialization to the
105 * beginning of the deque
106 */
107 B2FIter(const SkClipStack& stack)
108 : INHERITED(stack, kFront_IterStart) {
109 }
110
111 using Iter::Clip;
112 using Iter::next;
113
114 /**
115 * Wrap Iter::reset to force initialization to the
116 * beginning of the deque
117 */
118 void reset(const SkClipStack& stack) {
119 this->INHERITED::reset(stack, kFront_IterStart);
120 }
121
122 private:
123
124 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000125 };
126
127private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000128 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000129
130 SkDeque fDeque;
131 int fSaveCount;
132};
133
134#endif
135