blob: fbdcb36c844fcd8ed60abee5fefe4e32c875bc77 [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
robertphillips@google.com607fe072012-07-24 13:54:00 +000033 enum BoundsType {
34 // The bounding box contains all the pixels that can be written to
35 kNormal_BoundsType,
36 // The bounding box contains all the pixels that cannot be written to.
37 // The real bound extends out to infinity and all the pixels outside
38 // of the bound can be written to. Note that some of the pixels inside
39 // the bound may also be writeable but all pixels that cannot be
40 // written to are guaranteed to be inside.
41 kInsideOut_BoundsType
42 };
43
44 /**
45 * getBounds places the current finite bound in its first parameter. In its
46 * second, it indicates which kind of bound is being returned. If
47 * 'finiteBound' is a normal bounding box then it encloses are writeable
48 * pixels. If 'finiteBound' is an inside out bounding box then it
49 * encloses all the un-writeable pixels and the true/normal bound is the
50 * infinite plane.
51 */
52 void getBounds(SkRect* finiteBound, BoundsType* boundType) const;
53
reed@google.com115d9312012-05-16 18:50:40 +000054 void clipDevRect(const SkIRect& ir, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +000055 SkRect r;
56 r.set(ir);
reed@google.com00177082011-10-12 14:34:30 +000057 this->clipDevRect(r, op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +000058 }
reed@google.com00177082011-10-12 14:34:30 +000059 void clipDevRect(const SkRect&, SkRegion::Op, bool doAA);
60 void clipDevPath(const SkPath&, SkRegion::Op, bool doAA);
reed@google.com5c3d1472011-02-22 19:12:23 +000061
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000062private:
63 struct Rec;
64
65public:
66 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +000067 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000068 enum IterStart {
robertphillips@google.com80214e22012-07-20 15:33:18 +000069 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
70 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000071 };
72
bsalomon@google.comd302f142011-03-03 13:54:13 +000073 /**
74 * Creates an uninitialized iterator. Must be reset()
75 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000076 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +000077
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000078 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +000079
80 struct Clip {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000081 Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op),
82 fDoAA(false) {}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000083 friend bool operator==(const Clip& a, const Clip& b);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +000084 friend bool operator!=(const Clip& a, const Clip& b);
reed@google.com5c3d1472011-02-22 19:12:23 +000085 const SkRect* fRect; // if non-null, this is a rect clip
86 const SkPath* fPath; // if non-null, this is a path clip
87 SkRegion::Op fOp;
reed@google.com00177082011-10-12 14:34:30 +000088 bool fDoAA;
reed@google.com5c3d1472011-02-22 19:12:23 +000089 };
90
91 /**
92 * Return the clip for this element in the iterator. If next() returns
93 * NULL, then the iterator is done. The type of clip is determined by
94 * the pointers fRect and fPath:
95 *
96 * fRect==NULL fPath!=NULL path clip
97 * fRect!=NULL fPath==NULL rect clip
98 * fRect==NULL fPath==NULL empty clip
99 */
100 const Clip* next();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000101 const Clip* prev();
reed@google.com5c3d1472011-02-22 19:12:23 +0000102
bsalomon@google.comd302f142011-03-03 13:54:13 +0000103 /**
robertphillips@google.com80214e22012-07-20 15:33:18 +0000104 * Moves the iterator to the topmost clip with the specified RegionOp
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000105 * and returns that clip. If no clip with that op is found,
106 * returns NULL.
107 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000108 const Clip* skipToTopmost(SkRegion::Op op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000109
110 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000111 * Restarts the iterator on a clip stack.
112 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000113 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000114
reed@google.com5c3d1472011-02-22 19:12:23 +0000115 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000116 const SkClipStack* fStack;
117 Clip fClip;
118 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000119
120 /**
121 * updateClip updates fClip to the current state of fIter. It unifies
122 * functionality needed by both next() and prev().
123 */
124 const Clip* updateClip(const SkClipStack::Rec* rec);
125 };
126
robertphillips@google.com80214e22012-07-20 15:33:18 +0000127 /**
128 * The B2TIter iterates from the bottom of the stack to the top.
129 * It inherits privately from Iter to prevent access to reverse iteration.
130 */
131 class B2TIter : private Iter {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000132 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000133 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000134
135 /**
136 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000137 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000138 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000139 B2TIter(const SkClipStack& stack)
140 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000141 }
142
143 using Iter::Clip;
144 using Iter::next;
145
146 /**
147 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000148 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000149 */
150 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000151 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000152 }
153
154 private:
155
156 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000157 };
158
robertphillips@google.com607fe072012-07-24 13:54:00 +0000159 /**
160 * GetConservativeBounds returns a conservative bound of the current clip.
161 * Since this could be the infinite plane (if inverse fills were involved) the
162 * maxWidth and maxHeight parameters can be used to limit the returned bound
163 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
164 * allow the caller to offset the returned bound to account for translated
165 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
166 * the translation (+offsetX, +offsetY) is applied before the clamp to the
167 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
168 */
169 void getConservativeBounds(int offsetX,
170 int offsetY,
171 int maxWidth,
172 int maxHeight,
173 SkRect* bounds) const;
174
reed@google.com5c3d1472011-02-22 19:12:23 +0000175private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000176 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000177
178 SkDeque fDeque;
179 int fSaveCount;
180};
181
182#endif
183