epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 8 | #ifndef SkClipStack_DEFINED |
| 9 | #define SkClipStack_DEFINED |
| 10 | |
| 11 | #include "SkDeque.h" |
| 12 | #include "SkRegion.h" |
| 13 | |
bsalomon@google.com | 57788b5 | 2011-02-22 21:00:31 +0000 | [diff] [blame] | 14 | struct SkRect; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 15 | class SkPath; |
| 16 | |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame^] | 17 | // 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.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 23 | class SK_API SkClipStack { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 24 | public: |
| 25 | SkClipStack(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 26 | SkClipStack(const SkClipStack& b); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 27 | ~SkClipStack(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 28 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 29 | 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.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 33 | void reset(); |
| 34 | |
| 35 | int getSaveCount() const { return fSaveCount; } |
| 36 | void save(); |
| 37 | void restore(); |
| 38 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 39 | 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.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame^] | 53 | * 'finiteBound' is a normal bounding box then it encloses all writeable |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 54 | * 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.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame^] | 56 | * infinite plane. isIntersectionOfRects is an optional parameter |
| 57 | * that is true if 'finiteBound' resulted from an intersection of rects. |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 58 | */ |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame^] | 59 | void getBounds(SkRect* finiteBound, |
| 60 | BoundsType* boundType, |
| 61 | bool* isIntersectionOfRects = NULL) const; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 62 | |
reed@google.com | 115d931 | 2012-05-16 18:50:40 +0000 | [diff] [blame] | 63 | void clipDevRect(const SkIRect& ir, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 64 | SkRect r; |
| 65 | r.set(ir); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 66 | this->clipDevRect(r, op, false); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 67 | } |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 68 | void clipDevRect(const SkRect&, SkRegion::Op, bool doAA); |
| 69 | void clipDevPath(const SkPath&, SkRegion::Op, bool doAA); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 70 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 71 | private: |
| 72 | struct Rec; |
| 73 | |
| 74 | public: |
| 75 | class Iter { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 76 | public: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 77 | enum IterStart { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 78 | kBottom_IterStart = SkDeque::Iter::kFront_IterStart, |
| 79 | kTop_IterStart = SkDeque::Iter::kBack_IterStart |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 82 | /** |
| 83 | * Creates an uninitialized iterator. Must be reset() |
| 84 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 85 | Iter(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 86 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 87 | Iter(const SkClipStack& stack, IterStart startLoc); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 88 | |
| 89 | struct Clip { |
robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 90 | Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op), |
| 91 | fDoAA(false) {} |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 92 | friend bool operator==(const Clip& a, const Clip& b); |
vandebo@chromium.org | 8887ede | 2011-05-25 01:27:52 +0000 | [diff] [blame] | 93 | friend bool operator!=(const Clip& a, const Clip& b); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 94 | 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.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 97 | bool fDoAA; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 98 | }; |
| 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.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 110 | const Clip* prev(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 111 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 112 | /** |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 113 | * Moves the iterator to the topmost clip with the specified RegionOp |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 114 | * and returns that clip. If no clip with that op is found, |
| 115 | * returns NULL. |
| 116 | */ |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 117 | const Clip* skipToTopmost(SkRegion::Op op); |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 120 | * Restarts the iterator on a clip stack. |
| 121 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 122 | void reset(const SkClipStack& stack, IterStart startLoc); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 123 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 124 | private: |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 125 | const SkClipStack* fStack; |
| 126 | Clip fClip; |
| 127 | SkDeque::Iter fIter; |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 128 | |
| 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.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 136 | /** |
| 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.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 141 | public: |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 142 | B2TIter() {} |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Wrap Iter's 2 parameter ctor to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 146 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 147 | */ |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 148 | B2TIter(const SkClipStack& stack) |
| 149 | : INHERITED(stack, kBottom_IterStart) { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | using Iter::Clip; |
| 153 | using Iter::next; |
| 154 | |
| 155 | /** |
| 156 | * Wrap Iter::reset to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 157 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 158 | */ |
| 159 | void reset(const SkClipStack& stack) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 160 | this->INHERITED::reset(stack, kBottom_IterStart); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private: |
| 164 | |
| 165 | typedef Iter INHERITED; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 168 | /** |
| 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.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame^] | 177 | * isIntersectionOfRects is an optional parameter that is true when |
| 178 | * 'bounds' is the result of an intersection of rects. |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 179 | */ |
| 180 | void getConservativeBounds(int offsetX, |
| 181 | int offsetY, |
| 182 | int maxWidth, |
| 183 | int maxHeight, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame^] | 184 | SkRect* bounds, |
| 185 | bool* isIntersectionOfRects = NULL) const; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 186 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 187 | private: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 188 | friend class Iter; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 189 | |
| 190 | SkDeque fDeque; |
| 191 | int fSaveCount; |
| 192 | }; |
| 193 | |
| 194 | #endif |
| 195 | |