blob: c0fadb1ab1e24f7aed3fc8ca730129e662762504 [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
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000017// Because a single save/restore state can have multiple clips, this class
18// stores the stack depth (fSaveCount) and clips (fDeque) separately.
19// Each clip in fDeque stores the stack state to which it belongs
20// (i.e., the fSaveCount in force when it was added). Restores are thus
21// implemented by removing clips from fDeque that have an fSaveCount larger
22// then the freshly decremented count.
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000023class SK_API SkClipStack {
reed@google.com5c3d1472011-02-22 19:12:23 +000024public:
25 SkClipStack();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000026 SkClipStack(const SkClipStack& b);
robertphillips@google.comcc6493b2012-07-26 18:39:13 +000027 explicit SkClipStack(const SkRect& r);
vandebo@chromium.org610f7162012-03-14 18:34:15 +000028 ~SkClipStack();
reed@google.com5c3d1472011-02-22 19:12:23 +000029
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000030 SkClipStack& operator=(const SkClipStack& b);
31 bool operator==(const SkClipStack& b) const;
32 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
33
reed@google.com5c3d1472011-02-22 19:12:23 +000034 void reset();
35
36 int getSaveCount() const { return fSaveCount; }
37 void save();
38 void restore();
39
robertphillips@google.com607fe072012-07-24 13:54:00 +000040 enum BoundsType {
41 // The bounding box contains all the pixels that can be written to
42 kNormal_BoundsType,
43 // The bounding box contains all the pixels that cannot be written to.
44 // The real bound extends out to infinity and all the pixels outside
45 // of the bound can be written to. Note that some of the pixels inside
46 // the bound may also be writeable but all pixels that cannot be
47 // written to are guaranteed to be inside.
48 kInsideOut_BoundsType
49 };
50
51 /**
52 * getBounds places the current finite bound in its first parameter. In its
53 * second, it indicates which kind of bound is being returned. If
robertphillips@google.com7b112892012-07-31 15:18:21 +000054 * 'canvFiniteBound' is a normal bounding box then it encloses all writeable
55 * pixels. If 'canvFiniteBound' is an inside out bounding box then it
robertphillips@google.com607fe072012-07-24 13:54:00 +000056 * encloses all the un-writeable pixels and the true/normal bound is the
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000057 * infinite plane. isIntersectionOfRects is an optional parameter
robertphillips@google.com7b112892012-07-31 15:18:21 +000058 * that is true if 'canvFiniteBound' resulted from an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +000059 */
robertphillips@google.com7b112892012-07-31 15:18:21 +000060 void getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000061 BoundsType* boundType,
62 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +000063
reed@google.com115d9312012-05-16 18:50:40 +000064 void clipDevRect(const SkIRect& ir, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +000065 SkRect r;
66 r.set(ir);
reed@google.com00177082011-10-12 14:34:30 +000067 this->clipDevRect(r, op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +000068 }
reed@google.com00177082011-10-12 14:34:30 +000069 void clipDevRect(const SkRect&, SkRegion::Op, bool doAA);
70 void clipDevPath(const SkPath&, SkRegion::Op, bool doAA);
reed@google.com5c3d1472011-02-22 19:12:23 +000071
robertphillips@google.comcc6493b2012-07-26 18:39:13 +000072 /**
73 * isWideOpen returns true if the clip state corresponds to the infinite
74 * plane (i.e., draws are not limited at all)
75 */
76 bool isWideOpen() const;
77
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000078private:
79 struct Rec;
80
81public:
82 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +000083 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000084 enum IterStart {
robertphillips@google.com80214e22012-07-20 15:33:18 +000085 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
86 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000087 };
88
bsalomon@google.comd302f142011-03-03 13:54:13 +000089 /**
90 * Creates an uninitialized iterator. Must be reset()
91 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000092 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +000093
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000094 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +000095
96 struct Clip {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000097 Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op),
98 fDoAA(false) {}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000099 friend bool operator==(const Clip& a, const Clip& b);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000100 friend bool operator!=(const Clip& a, const Clip& b);
reed@google.com5c3d1472011-02-22 19:12:23 +0000101 const SkRect* fRect; // if non-null, this is a rect clip
102 const SkPath* fPath; // if non-null, this is a path clip
103 SkRegion::Op fOp;
reed@google.com00177082011-10-12 14:34:30 +0000104 bool fDoAA;
reed@google.com5c3d1472011-02-22 19:12:23 +0000105 };
106
107 /**
108 * Return the clip for this element in the iterator. If next() returns
109 * NULL, then the iterator is done. The type of clip is determined by
110 * the pointers fRect and fPath:
111 *
112 * fRect==NULL fPath!=NULL path clip
113 * fRect!=NULL fPath==NULL rect clip
114 * fRect==NULL fPath==NULL empty clip
115 */
116 const Clip* next();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000117 const Clip* prev();
reed@google.com5c3d1472011-02-22 19:12:23 +0000118
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119 /**
robertphillips@google.com80214e22012-07-20 15:33:18 +0000120 * Moves the iterator to the topmost clip with the specified RegionOp
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000121 * and returns that clip. If no clip with that op is found,
122 * returns NULL.
123 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000124 const Clip* skipToTopmost(SkRegion::Op op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000125
126 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000127 * Restarts the iterator on a clip stack.
128 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000129 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130
reed@google.com5c3d1472011-02-22 19:12:23 +0000131 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000132 const SkClipStack* fStack;
133 Clip fClip;
134 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000135
136 /**
137 * updateClip updates fClip to the current state of fIter. It unifies
138 * functionality needed by both next() and prev().
139 */
140 const Clip* updateClip(const SkClipStack::Rec* rec);
141 };
142
robertphillips@google.com80214e22012-07-20 15:33:18 +0000143 /**
144 * The B2TIter iterates from the bottom of the stack to the top.
145 * It inherits privately from Iter to prevent access to reverse iteration.
146 */
147 class B2TIter : private Iter {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000148 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000149 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000150
151 /**
152 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000153 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000154 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000155 B2TIter(const SkClipStack& stack)
156 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000157 }
158
159 using Iter::Clip;
160 using Iter::next;
161
162 /**
163 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000164 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000165 */
166 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000167 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000168 }
169
170 private:
171
172 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000173 };
174
robertphillips@google.com607fe072012-07-24 13:54:00 +0000175 /**
176 * GetConservativeBounds returns a conservative bound of the current clip.
177 * Since this could be the infinite plane (if inverse fills were involved) the
178 * maxWidth and maxHeight parameters can be used to limit the returned bound
179 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
180 * allow the caller to offset the returned bound to account for translated
181 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
182 * the translation (+offsetX, +offsetY) is applied before the clamp to the
183 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000184 * isIntersectionOfRects is an optional parameter that is true when
185 * 'bounds' is the result of an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000186 */
187 void getConservativeBounds(int offsetX,
188 int offsetY,
189 int maxWidth,
190 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000191 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000192 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000193
reed@google.com5c3d1472011-02-22 19:12:23 +0000194private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000195 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000196
197 SkDeque fDeque;
198 int fSaveCount;
199};
200
201#endif
202