blob: 4c79c2f8244b39a05c125e9fcf572cecad8114a4 [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);
robertphillips@google.com641f8b12012-07-31 19:15:58 +000028 explicit SkClipStack(const SkIRect& r);
vandebo@chromium.org610f7162012-03-14 18:34:15 +000029 ~SkClipStack();
reed@google.com5c3d1472011-02-22 19:12:23 +000030
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000031 SkClipStack& operator=(const SkClipStack& b);
32 bool operator==(const SkClipStack& b) const;
33 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
34
reed@google.com5c3d1472011-02-22 19:12:23 +000035 void reset();
36
37 int getSaveCount() const { return fSaveCount; }
38 void save();
39 void restore();
40
robertphillips@google.com607fe072012-07-24 13:54:00 +000041 enum BoundsType {
42 // The bounding box contains all the pixels that can be written to
43 kNormal_BoundsType,
44 // The bounding box contains all the pixels that cannot be written to.
45 // The real bound extends out to infinity and all the pixels outside
46 // of the bound can be written to. Note that some of the pixels inside
47 // the bound may also be writeable but all pixels that cannot be
48 // written to are guaranteed to be inside.
49 kInsideOut_BoundsType
50 };
51
52 /**
53 * getBounds places the current finite bound in its first parameter. In its
54 * second, it indicates which kind of bound is being returned. If
robertphillips@google.com7b112892012-07-31 15:18:21 +000055 * 'canvFiniteBound' is a normal bounding box then it encloses all writeable
56 * pixels. If 'canvFiniteBound' is an inside out bounding box then it
robertphillips@google.com607fe072012-07-24 13:54:00 +000057 * encloses all the un-writeable pixels and the true/normal bound is the
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000058 * infinite plane. isIntersectionOfRects is an optional parameter
robertphillips@google.com7b112892012-07-31 15:18:21 +000059 * that is true if 'canvFiniteBound' resulted from an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +000060 */
robertphillips@google.com7b112892012-07-31 15:18:21 +000061 void getBounds(SkRect* canvFiniteBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000062 BoundsType* boundType,
63 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +000064
reed@google.com115d9312012-05-16 18:50:40 +000065 void clipDevRect(const SkIRect& ir, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +000066 SkRect r;
67 r.set(ir);
reed@google.com00177082011-10-12 14:34:30 +000068 this->clipDevRect(r, op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +000069 }
reed@google.com00177082011-10-12 14:34:30 +000070 void clipDevRect(const SkRect&, SkRegion::Op, bool doAA);
71 void clipDevPath(const SkPath&, SkRegion::Op, bool doAA);
reed@google.com5c3d1472011-02-22 19:12:23 +000072
robertphillips@google.comcc6493b2012-07-26 18:39:13 +000073 /**
74 * isWideOpen returns true if the clip state corresponds to the infinite
75 * plane (i.e., draws are not limited at all)
76 */
77 bool isWideOpen() const;
78
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000079private:
80 struct Rec;
81
82public:
83 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +000084 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000085 enum IterStart {
robertphillips@google.com80214e22012-07-20 15:33:18 +000086 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
87 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000088 };
89
bsalomon@google.comd302f142011-03-03 13:54:13 +000090 /**
91 * Creates an uninitialized iterator. Must be reset()
92 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000093 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +000094
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000095 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +000096
97 struct Clip {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000098 Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op),
99 fDoAA(false) {}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000100 friend bool operator==(const Clip& a, const Clip& b);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000101 friend bool operator!=(const Clip& a, const Clip& b);
reed@google.com5c3d1472011-02-22 19:12:23 +0000102 const SkRect* fRect; // if non-null, this is a rect clip
103 const SkPath* fPath; // if non-null, this is a path clip
104 SkRegion::Op fOp;
reed@google.com00177082011-10-12 14:34:30 +0000105 bool fDoAA;
reed@google.com5c3d1472011-02-22 19:12:23 +0000106 };
107
108 /**
109 * Return the clip for this element in the iterator. If next() returns
110 * NULL, then the iterator is done. The type of clip is determined by
111 * the pointers fRect and fPath:
112 *
113 * fRect==NULL fPath!=NULL path clip
114 * fRect!=NULL fPath==NULL rect clip
115 * fRect==NULL fPath==NULL empty clip
116 */
117 const Clip* next();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000118 const Clip* prev();
reed@google.com5c3d1472011-02-22 19:12:23 +0000119
bsalomon@google.comd302f142011-03-03 13:54:13 +0000120 /**
robertphillips@google.com80214e22012-07-20 15:33:18 +0000121 * Moves the iterator to the topmost clip with the specified RegionOp
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000122 * and returns that clip. If no clip with that op is found,
123 * returns NULL.
124 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000125 const Clip* skipToTopmost(SkRegion::Op op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000126
127 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000128 * Restarts the iterator on a clip stack.
129 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000130 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000131
reed@google.com5c3d1472011-02-22 19:12:23 +0000132 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000133 const SkClipStack* fStack;
134 Clip fClip;
135 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000136
137 /**
138 * updateClip updates fClip to the current state of fIter. It unifies
139 * functionality needed by both next() and prev().
140 */
141 const Clip* updateClip(const SkClipStack::Rec* rec);
142 };
143
robertphillips@google.com80214e22012-07-20 15:33:18 +0000144 /**
145 * The B2TIter iterates from the bottom of the stack to the top.
146 * It inherits privately from Iter to prevent access to reverse iteration.
147 */
148 class B2TIter : private Iter {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000149 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000150 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000151
152 /**
153 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000154 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000155 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000156 B2TIter(const SkClipStack& stack)
157 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000158 }
159
160 using Iter::Clip;
161 using Iter::next;
162
163 /**
164 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000165 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000166 */
167 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000168 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000169 }
170
171 private:
172
173 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000174 };
175
robertphillips@google.com607fe072012-07-24 13:54:00 +0000176 /**
177 * GetConservativeBounds returns a conservative bound of the current clip.
178 * Since this could be the infinite plane (if inverse fills were involved) the
179 * maxWidth and maxHeight parameters can be used to limit the returned bound
180 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
181 * allow the caller to offset the returned bound to account for translated
182 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
183 * the translation (+offsetX, +offsetY) is applied before the clamp to the
184 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000185 * isIntersectionOfRects is an optional parameter that is true when
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000186 * 'devBounds' is the result of an intersection of rects. In this case
187 * 'devBounds' is the exact answer/clip.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000188 */
189 void getConservativeBounds(int offsetX,
190 int offsetY,
191 int maxWidth,
192 int maxHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000193 SkRect* devBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000194 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000195
reed@google.com5c3d1472011-02-22 19:12:23 +0000196private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000197 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000198
199 SkDeque fDeque;
200 int fSaveCount;
201};
202
203#endif
204