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 | |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 17 | class SK_API SkClipStack { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 18 | public: |
| 19 | SkClipStack(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 20 | SkClipStack(const SkClipStack& b); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 21 | ~SkClipStack(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 22 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 23 | SkClipStack& operator=(const SkClipStack& b); |
| 24 | bool operator==(const SkClipStack& b) const; |
| 25 | bool operator!=(const SkClipStack& b) const { return !(*this == b); } |
| 26 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 27 | void reset(); |
| 28 | |
| 29 | int getSaveCount() const { return fSaveCount; } |
| 30 | void save(); |
| 31 | void restore(); |
| 32 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame^] | 33 | enum BoundsType { |
| 34 | // The bounding box contains all the pixels that can be written to |
| 35 | kNormal_BoundsType, |
| 36 | // The bounding box contains all the pixels that cannot be written to. |
| 37 | // The real bound extends out to infinity and all the pixels outside |
| 38 | // of the bound can be written to. Note that some of the pixels inside |
| 39 | // the bound may also be writeable but all pixels that cannot be |
| 40 | // written to are guaranteed to be inside. |
| 41 | kInsideOut_BoundsType |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * getBounds places the current finite bound in its first parameter. In its |
| 46 | * second, it indicates which kind of bound is being returned. If |
| 47 | * 'finiteBound' is a normal bounding box then it encloses are writeable |
| 48 | * pixels. If 'finiteBound' is an inside out bounding box then it |
| 49 | * encloses all the un-writeable pixels and the true/normal bound is the |
| 50 | * infinite plane. |
| 51 | */ |
| 52 | void getBounds(SkRect* finiteBound, BoundsType* boundType) const; |
| 53 | |
reed@google.com | 115d931 | 2012-05-16 18:50:40 +0000 | [diff] [blame] | 54 | void clipDevRect(const SkIRect& ir, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 55 | SkRect r; |
| 56 | r.set(ir); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 57 | this->clipDevRect(r, op, false); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 58 | } |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 59 | void clipDevRect(const SkRect&, SkRegion::Op, bool doAA); |
| 60 | void clipDevPath(const SkPath&, SkRegion::Op, bool doAA); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 61 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 62 | private: |
| 63 | struct Rec; |
| 64 | |
| 65 | public: |
| 66 | class Iter { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 67 | public: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 68 | enum IterStart { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 69 | kBottom_IterStart = SkDeque::Iter::kFront_IterStart, |
| 70 | kTop_IterStart = SkDeque::Iter::kBack_IterStart |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 73 | /** |
| 74 | * Creates an uninitialized iterator. Must be reset() |
| 75 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 76 | Iter(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 77 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 78 | Iter(const SkClipStack& stack, IterStart startLoc); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 79 | |
| 80 | struct Clip { |
robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 81 | Clip() : fRect(NULL), fPath(NULL), fOp(SkRegion::kIntersect_Op), |
| 82 | fDoAA(false) {} |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 83 | friend bool operator==(const Clip& a, const Clip& b); |
vandebo@chromium.org | 8887ede | 2011-05-25 01:27:52 +0000 | [diff] [blame] | 84 | friend bool operator!=(const Clip& a, const Clip& b); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 85 | const SkRect* fRect; // if non-null, this is a rect clip |
| 86 | const SkPath* fPath; // if non-null, this is a path clip |
| 87 | SkRegion::Op fOp; |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 88 | bool fDoAA; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * Return the clip for this element in the iterator. If next() returns |
| 93 | * NULL, then the iterator is done. The type of clip is determined by |
| 94 | * the pointers fRect and fPath: |
| 95 | * |
| 96 | * fRect==NULL fPath!=NULL path clip |
| 97 | * fRect!=NULL fPath==NULL rect clip |
| 98 | * fRect==NULL fPath==NULL empty clip |
| 99 | */ |
| 100 | const Clip* next(); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 101 | const Clip* prev(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 103 | /** |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 104 | * Moves the iterator to the topmost clip with the specified RegionOp |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 105 | * and returns that clip. If no clip with that op is found, |
| 106 | * returns NULL. |
| 107 | */ |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 108 | const Clip* skipToTopmost(SkRegion::Op op); |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 111 | * Restarts the iterator on a clip stack. |
| 112 | */ |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 113 | void reset(const SkClipStack& stack, IterStart startLoc); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 114 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 115 | private: |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 116 | const SkClipStack* fStack; |
| 117 | Clip fClip; |
| 118 | SkDeque::Iter fIter; |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * updateClip updates fClip to the current state of fIter. It unifies |
| 122 | * functionality needed by both next() and prev(). |
| 123 | */ |
| 124 | const Clip* updateClip(const SkClipStack::Rec* rec); |
| 125 | }; |
| 126 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 127 | /** |
| 128 | * The B2TIter iterates from the bottom of the stack to the top. |
| 129 | * It inherits privately from Iter to prevent access to reverse iteration. |
| 130 | */ |
| 131 | class B2TIter : private Iter { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 132 | public: |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 133 | B2TIter() {} |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Wrap Iter's 2 parameter ctor to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 137 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 138 | */ |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 139 | B2TIter(const SkClipStack& stack) |
| 140 | : INHERITED(stack, kBottom_IterStart) { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | using Iter::Clip; |
| 144 | using Iter::next; |
| 145 | |
| 146 | /** |
| 147 | * Wrap Iter::reset to force initialization to the |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 148 | * beginning of the deque/bottom of the stack |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 149 | */ |
| 150 | void reset(const SkClipStack& stack) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 151 | this->INHERITED::reset(stack, kBottom_IterStart); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | private: |
| 155 | |
| 156 | typedef Iter INHERITED; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame^] | 159 | /** |
| 160 | * GetConservativeBounds returns a conservative bound of the current clip. |
| 161 | * Since this could be the infinite plane (if inverse fills were involved) the |
| 162 | * maxWidth and maxHeight parameters can be used to limit the returned bound |
| 163 | * to the expected drawing area. Similarly, the offsetX and offsetY parameters |
| 164 | * allow the caller to offset the returned bound to account for translated |
| 165 | * drawing areas (i.e., those resulting from a saveLayer). For finite bounds, |
| 166 | * the translation (+offsetX, +offsetY) is applied before the clamp to the |
| 167 | * maximum rectangle: [0,maxWidth) x [0,maxHeight). |
| 168 | */ |
| 169 | void getConservativeBounds(int offsetX, |
| 170 | int offsetY, |
| 171 | int maxWidth, |
| 172 | int maxHeight, |
| 173 | SkRect* bounds) const; |
| 174 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 175 | private: |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 176 | friend class Iter; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 177 | |
| 178 | SkDeque fDeque; |
| 179 | int fSaveCount; |
| 180 | }; |
| 181 | |
| 182 | #endif |
| 183 | |