blob: 71b68cebdec25f994c1f70940c33572e8e9abeac [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);
vandebo@chromium.org610f7162012-03-14 18:34:15 +000027 ~SkClipStack();
reed@google.com5c3d1472011-02-22 19:12:23 +000028
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000029 SkClipStack& operator=(const SkClipStack& b);
30 bool operator==(const SkClipStack& b) const;
31 bool operator!=(const SkClipStack& b) const { return !(*this == b); }
32
reed@google.com5c3d1472011-02-22 19:12:23 +000033 void reset();
34
35 int getSaveCount() const { return fSaveCount; }
36 void save();
37 void restore();
38
robertphillips@google.com607fe072012-07-24 13:54:00 +000039 enum BoundsType {
40 // The bounding box contains all the pixels that can be written to
41 kNormal_BoundsType,
42 // The bounding box contains all the pixels that cannot be written to.
43 // The real bound extends out to infinity and all the pixels outside
44 // of the bound can be written to. Note that some of the pixels inside
45 // the bound may also be writeable but all pixels that cannot be
46 // written to are guaranteed to be inside.
47 kInsideOut_BoundsType
48 };
49
50 /**
51 * getBounds places the current finite bound in its first parameter. In its
52 * second, it indicates which kind of bound is being returned. If
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000053 * 'finiteBound' is a normal bounding box then it encloses all writeable
robertphillips@google.com607fe072012-07-24 13:54:00 +000054 * pixels. If 'finiteBound' is an inside out bounding box then it
55 * encloses all the un-writeable pixels and the true/normal bound is the
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000056 * infinite plane. isIntersectionOfRects is an optional parameter
57 * that is true if 'finiteBound' resulted from an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +000058 */
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +000059 void getBounds(SkRect* finiteBound,
60 BoundsType* boundType,
61 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +000062
reed@google.com115d9312012-05-16 18:50:40 +000063 void clipDevRect(const SkIRect& ir, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +000064 SkRect r;
65 r.set(ir);
reed@google.com00177082011-10-12 14:34:30 +000066 this->clipDevRect(r, op, false);
reed@google.com5c3d1472011-02-22 19:12:23 +000067 }
reed@google.com00177082011-10-12 14:34:30 +000068 void clipDevRect(const SkRect&, SkRegion::Op, bool doAA);
69 void clipDevPath(const SkPath&, SkRegion::Op, bool doAA);
reed@google.com5c3d1472011-02-22 19:12:23 +000070
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000071private:
72 struct Rec;
73
74public:
75 class Iter {
reed@google.com5c3d1472011-02-22 19:12:23 +000076 public:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000077 enum IterStart {
robertphillips@google.com80214e22012-07-20 15:33:18 +000078 kBottom_IterStart = SkDeque::Iter::kFront_IterStart,
79 kTop_IterStart = SkDeque::Iter::kBack_IterStart
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000080 };
81
bsalomon@google.comd302f142011-03-03 13:54:13 +000082 /**
83 * Creates an uninitialized iterator. Must be reset()
84 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000085 Iter();
bsalomon@google.comd302f142011-03-03 13:54:13 +000086
robertphillips@google.com52cb2c72012-07-16 18:52:29 +000087 Iter(const SkClipStack& stack, IterStart startLoc);
reed@google.com5c3d1472011-02-22 19:12:23 +000088
89 struct Clip {
robertphillips@google.comfa1d2912012-04-16 14:49:14 +000090 Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op),
91 fDoAA(false) {}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000092 friend bool operator==(const Clip& a, const Clip& b);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +000093 friend bool operator!=(const Clip& a, const Clip& b);
reed@google.com5c3d1472011-02-22 19:12:23 +000094 const SkRect* fRect; // if non-null, this is a rect clip
95 const SkPath* fPath; // if non-null, this is a path clip
96 SkRegion::Op fOp;
reed@google.com00177082011-10-12 14:34:30 +000097 bool fDoAA;
reed@google.com5c3d1472011-02-22 19:12:23 +000098 };
99
100 /**
101 * Return the clip for this element in the iterator. If next() returns
102 * NULL, then the iterator is done. The type of clip is determined by
103 * the pointers fRect and fPath:
104 *
105 * fRect==NULL fPath!=NULL path clip
106 * fRect!=NULL fPath==NULL rect clip
107 * fRect==NULL fPath==NULL empty clip
108 */
109 const Clip* next();
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000110 const Clip* prev();
reed@google.com5c3d1472011-02-22 19:12:23 +0000111
bsalomon@google.comd302f142011-03-03 13:54:13 +0000112 /**
robertphillips@google.com80214e22012-07-20 15:33:18 +0000113 * Moves the iterator to the topmost clip with the specified RegionOp
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000114 * and returns that clip. If no clip with that op is found,
115 * returns NULL.
116 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000117 const Clip* skipToTopmost(SkRegion::Op op);
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000118
119 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000120 * Restarts the iterator on a clip stack.
121 */
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000122 void reset(const SkClipStack& stack, IterStart startLoc);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000123
reed@google.com5c3d1472011-02-22 19:12:23 +0000124 private:
robertphillips@google.com5836b6d2012-07-18 12:06:15 +0000125 const SkClipStack* fStack;
126 Clip fClip;
127 SkDeque::Iter fIter;
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000128
129 /**
130 * updateClip updates fClip to the current state of fIter. It unifies
131 * functionality needed by both next() and prev().
132 */
133 const Clip* updateClip(const SkClipStack::Rec* rec);
134 };
135
robertphillips@google.com80214e22012-07-20 15:33:18 +0000136 /**
137 * The B2TIter iterates from the bottom of the stack to the top.
138 * It inherits privately from Iter to prevent access to reverse iteration.
139 */
140 class B2TIter : private Iter {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000141 public:
robertphillips@google.com80214e22012-07-20 15:33:18 +0000142 B2TIter() {}
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000143
144 /**
145 * Wrap Iter's 2 parameter ctor to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000146 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000147 */
robertphillips@google.com80214e22012-07-20 15:33:18 +0000148 B2TIter(const SkClipStack& stack)
149 : INHERITED(stack, kBottom_IterStart) {
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000150 }
151
152 using Iter::Clip;
153 using Iter::next;
154
155 /**
156 * Wrap Iter::reset to force initialization to the
robertphillips@google.com80214e22012-07-20 15:33:18 +0000157 * beginning of the deque/bottom of the stack
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000158 */
159 void reset(const SkClipStack& stack) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000160 this->INHERITED::reset(stack, kBottom_IterStart);
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000161 }
162
163 private:
164
165 typedef Iter INHERITED;
reed@google.com5c3d1472011-02-22 19:12:23 +0000166 };
167
robertphillips@google.com607fe072012-07-24 13:54:00 +0000168 /**
169 * GetConservativeBounds returns a conservative bound of the current clip.
170 * Since this could be the infinite plane (if inverse fills were involved) the
171 * maxWidth and maxHeight parameters can be used to limit the returned bound
172 * to the expected drawing area. Similarly, the offsetX and offsetY parameters
173 * allow the caller to offset the returned bound to account for translated
174 * drawing areas (i.e., those resulting from a saveLayer). For finite bounds,
175 * the translation (+offsetX, +offsetY) is applied before the clamp to the
176 * maximum rectangle: [0,maxWidth) x [0,maxHeight).
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000177 * isIntersectionOfRects is an optional parameter that is true when
178 * 'bounds' is the result of an intersection of rects.
robertphillips@google.com607fe072012-07-24 13:54:00 +0000179 */
180 void getConservativeBounds(int offsetX,
181 int offsetY,
182 int maxWidth,
183 int maxHeight,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000184 SkRect* bounds,
185 bool* isIntersectionOfRects = NULL) const;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000186
reed@google.com5c3d1472011-02-22 19:12:23 +0000187private:
robertphillips@google.com52cb2c72012-07-16 18:52:29 +0000188 friend class Iter;
reed@google.com5c3d1472011-02-22 19:12:23 +0000189
190 SkDeque fDeque;
191 int fSaveCount;
192};
193
194#endif
195