blob: 077c4b7eea389485f9c16514821f9440da9a526e [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"
robertphillips@google.com46f93502012-08-07 15:38:08 +000013#include "SkTDArray.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000014
bsalomon@google.com57788b52011-02-22 21:00:31 +000015struct SkRect;
reed@google.com5c3d1472011-02-22 19:12:23 +000016class SkPath;
17
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000018// Because a single save/restore state can have multiple clips, this class
19// stores the stack depth (fSaveCount) and clips (fDeque) separately.
20// Each clip in fDeque stores the stack state to which it belongs
21// (i.e., the fSaveCount in force when it was added). Restores are thus
22// implemented by removing clips from fDeque that have an fSaveCount larger
23// then the freshly decremented count.
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000024class SK_API SkClipStack {
reed@google.com5c3d1472011-02-22 19:12:23 +000025public:
26 SkClipStack();
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000027 SkClipStack(const SkClipStack& b);
robertphillips@google.comcc6493b2012-07-26 18:39:13 +000028 explicit SkClipStack(const SkRect& r);
robertphillips@google.com641f8b12012-07-31 19:15:58 +000029 explicit SkClipStack(const SkIRect& r);
vandebo@chromium.org610f7162012-03-14 18:34:15 +000030 ~SkClipStack();
reed@google.com5c3d1472011-02-22 19:12:23 +000031
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000032 SkClipStack& operator=(const SkClipStack& b);
33 bool operator==(const SkClipStack& b) const;
34 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
35
reed@google.com5c3d1472011-02-22 19:12:23 +000036 void reset();
37
38 int getSaveCount() const { return fSaveCount; }
39 void save();
40 void restore();
41
robertphillips@google.com607fe072012-07-24 13:54:00 +000042 enum BoundsType {
43 // The bounding box contains all the pixels that can be written to
44 kNormal_BoundsType,
45 // The bounding box contains all the pixels that cannot be written to.
46 // The real bound extends out to infinity and all the pixels outside
47 // of the bound can be written to. Note that some of the pixels inside
48 // the bound may also be writeable but all pixels that cannot be
49 // written to are guaranteed to be inside.
50 kInsideOut_BoundsType
51 };
52
53 /**
54 * getBounds places the current finite bound in its first parameter. In its
55 * second, it indicates which kind of bound is being returned. If
robertphillips@google.com7b112892012-07-31 15:18:21 +000056 * 'canvFiniteBound' is a normal bounding box then it encloses all writeable
57 * pixels. If 'canvFiniteBound' is an inside out bounding box then it
robertphillips@google.com607fe072012-07-24 13:54:00 +000058 * encloses all the un-writeable pixels and the true/normal bound is the
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000059 * infinite plane. isIntersectionOfRects is an optional parameter
robertphillips@google.com7b112892012-07-31 15:18:21 +000060 * that is true if 'canvFiniteBound' resulted from an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +000061 */
robertphillips@google.com7b112892012-07-31 15:18:21 +000062 void getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000063 BoundsType* boundType,
64 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +000065
reed@google.com115d9312012-05-16 18:50:40 +000066 void clipDevRect(const SkIRect& ir, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +000067 SkRect r;
68 r.set(ir);
reed@google.com00177082011-10-12 14:34:30 +000069 this->clipDevRect(r, op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +000070 }
reed@google.com00177082011-10-12 14:34:30 +000071 void clipDevRect(const SkRect&, SkRegion::Op, bool doAA);
72 void clipDevPath(const SkPath&, SkRegion::Op, bool doAA);
reed@google.com5c3d1472011-02-22 19:12:23 +000073
robertphillips@google.comcc6493b2012-07-26 18:39:13 +000074 /**
75 * isWideOpen returns true if the clip state corresponds to the infinite
76 * plane (i.e., draws are not limited at all)
77 */
78 bool isWideOpen() const;
79
robertphillips@google.com46f93502012-08-07 15:38:08 +000080 /**
81 * Add a callback function that will be called whenever a clip state
82 * is no longer viable. This will occur whenever restore
83 * is called or when a clipDevRect or clipDevPath call updates the
84 * clip within an existing save/restore state. Each clip state is
85 * represented by a unique generation ID.
86 */
87 typedef void (*PFPurgeClipCB)(int genID, void* data);
88 void addPurgeClipCallback(PFPurgeClipCB callback, void* data) const;
89
90 /**
91 * Remove a callback added earlier via addPurgeClipCallback
92 */
93 void removePurgeClipCallback(PFPurgeClipCB callback, void* data) const;
94
95 /**
96 * The generation ID has three reserved values to indicate special
97 * (potentially ignoreable) cases
98 */
99 static const int32_t kInvalidGenID = 0;
100 static const int32_t kEmptyGenID = 1; // no pixels writeable
101 static const int32_t kWideOpenGenID = 2; // all pixels writeable
102
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000103private:
104 struct Rec;
105
106public:
107 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +0000108 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000109 enum IterStart {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000110 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
111 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000112 };
113
bsalomon@google.comd302f142011-03-03 13:54:13 +0000114 /**
115 * Creates an uninitialized iterator. Must be reset()
116 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000117 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000118
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000119 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +0000120
121 struct Clip {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +0000122 Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op),
123 fDoAA(false) {}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000124 friend bool operator==(const Clip& a, const Clip& b);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000125 friend bool operator!=(const Clip& a, const Clip& b);
reed@google.com5c3d1472011-02-22 19:12:23 +0000126 const SkRect* fRect; // if non-null, this is a rect clip
127 const SkPath* fPath; // if non-null, this is a path clip
128 SkRegion::Op fOp;
reed@google.com00177082011-10-12 14:34:30 +0000129 bool fDoAA;
reed@google.com5c3d1472011-02-22 19:12:23 +0000130 };
131
132 /**
133 * Return the clip for this element in the iterator. If next() returns
134 * NULL, then the iterator is done. The type of clip is determined by
135 * the pointers fRect and fPath:
136 *
137 * fRect==NULL fPath!=NULL path clip
138 * fRect!=NULL fPath==NULL rect clip
139 * fRect==NULL fPath==NULL empty clip
140 */
141 const Clip* next();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000142 const Clip* prev();
reed@google.com5c3d1472011-02-22 19:12:23 +0000143
bsalomon@google.comd302f142011-03-03 13:54:13 +0000144 /**
robertphillips@google.com80214e22012-07-20 15:33:18 +0000145 * Moves the iterator to the topmost clip with the specified RegionOp
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000146 * and returns that clip. If no clip with that op is found,
147 * returns NULL.
148 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000149 const Clip* skipToTopmost(SkRegion::Op op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000150
151 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000152 * Restarts the iterator on a clip stack.
153 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000154 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000155
reed@google.com5c3d1472011-02-22 19:12:23 +0000156 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000157 const SkClipStack* fStack;
158 Clip fClip;
159 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000160
161 /**
162 * updateClip updates fClip to the current state of fIter. It unifies
163 * functionality needed by both next() and prev().
164 */
165 const Clip* updateClip(const SkClipStack::Rec* rec);
166 };
167
robertphillips@google.com80214e22012-07-20 15:33:18 +0000168 /**
169 * The B2TIter iterates from the bottom of the stack to the top.
170 * It inherits privately from Iter to prevent access to reverse iteration.
171 */
172 class B2TIter : private Iter {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000173 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000174 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000175
176 /**
177 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000178 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000179 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000180 B2TIter(const SkClipStack& stack)
181 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000182 }
183
184 using Iter::Clip;
185 using Iter::next;
186
187 /**
188 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000189 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000190 */
191 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000192 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000193 }
194
195 private:
196
197 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000198 };
199
robertphillips@google.com607fe072012-07-24 13:54:00 +0000200 /**
201 * GetConservativeBounds returns a conservative bound of the current clip.
202 * Since this could be the infinite plane (if inverse fills were involved) the
203 * maxWidth and maxHeight parameters can be used to limit the returned bound
204 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
205 * allow the caller to offset the returned bound to account for translated
206 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
207 * the translation (+offsetX, +offsetY) is applied before the clamp to the
208 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000209 * isIntersectionOfRects is an optional parameter that is true when
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000210 * 'devBounds' is the result of an intersection of rects. In this case
211 * 'devBounds' is the exact answer/clip.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000212 */
213 void getConservativeBounds(int offsetX,
214 int offsetY,
215 int maxWidth,
216 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000217 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000218 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000219
reed@google.com5c3d1472011-02-22 19:12:23 +0000220private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000221 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000222
223 SkDeque fDeque;
224 int fSaveCount;
robertphillips@google.com46f93502012-08-07 15:38:08 +0000225
226 // Generation ID for the clip stack. This is incremented for each
227 // clipDevRect and clipDevPath call. 0 is reserved to indicate an
228 // invalid ID.
229 static int32_t gGenID;
230
231 struct ClipCallbackData {
232 PFPurgeClipCB fCallback;
233 void* fData;
234
235 friend bool operator==(const ClipCallbackData& a,
236 const ClipCallbackData& b) {
237 return a.fCallback == b.fCallback && a.fData == b.fData;
238 }
239 };
240
241 mutable SkTDArray<ClipCallbackData> fCallbackData;
242
243 /**
244 * Invoke all the purge callbacks passing in rec's generation ID.
245 */
246 void purgeClip(Rec* rec);
247
248 /**
249 * Return the next unique generation ID.
250 */
251 static int32_t GetNextGenID();
reed@google.com5c3d1472011-02-22 19:12:23 +0000252};
253
254#endif
255