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 | #include "SkClipStack.h" |
| 9 | #include "SkPath.h" |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 10 | #include "SkThread.h" |
| 11 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 12 | #include <new> |
| 13 | |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 14 | |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 15 | // 0-2 are reserved for invalid, empty & wide-open |
bsalomon@google.com | edb26fd | 2012-11-28 14:42:41 +0000 | [diff] [blame] | 16 | static const int32_t kFirstUnreservedGenID = 3; |
| 17 | int32_t SkClipStack::gGenID = kFirstUnreservedGenID; |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 19 | void SkClipStack::Element::checkEmpty() const { |
| 20 | SkASSERT(fFiniteBound.isEmpty()); |
| 21 | SkASSERT(kNormal_BoundsType == fFiniteBoundType); |
| 22 | SkASSERT(!fIsIntersectionOfRects); |
| 23 | SkASSERT(kEmptyGenID == fGenID); |
| 24 | SkASSERT(fPath.isEmpty()); |
| 25 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 27 | bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkRegion::Op op) const { |
| 28 | if (kEmpty_Type == fType && |
| 29 | (SkRegion::kDifference_Op == op || SkRegion::kIntersect_Op == op)) { |
| 30 | return true; |
| 31 | } |
| 32 | // Only clips within the same save/restore frame (as captured by |
| 33 | // the save count) can be merged |
| 34 | return fSaveCount == saveCount && |
| 35 | SkRegion::kIntersect_Op == op && |
| 36 | (SkRegion::kIntersect_Op == fOp || SkRegion::kReplace_Op == fOp); |
| 37 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 39 | bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const { |
| 40 | SkASSERT(kRect_Type == fType); |
| 41 | |
| 42 | if (fDoAA == newAA) { |
| 43 | // if the AA setting is the same there is no issue |
| 44 | return true; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 47 | if (!SkRect::Intersects(fRect, newR)) { |
| 48 | // The calling code will correctly set the result to the empty clip |
| 49 | return true; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 50 | } |
| 51 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 52 | if (fRect.contains(newR)) { |
| 53 | // if the new rect carves out a portion of the old one there is no |
| 54 | // issue |
| 55 | return true; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 56 | } |
| 57 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 58 | // So either the two overlap in some complex manner or newR contains oldR. |
| 59 | // In the first, case the edges will require different AA. In the second, |
| 60 | // the AA setting that would be carried forward is incorrect (e.g., oldR |
| 61 | // is AA while newR is BW but since newR contains oldR, oldR will be |
| 62 | // drawn BW) since the new AA setting will predominate. |
| 63 | return false; |
| 64 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 66 | // a mirror of combineBoundsRevDiff |
| 67 | void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) { |
| 68 | switch (combination) { |
| 69 | case kInvPrev_InvCur_FillCombo: |
| 70 | // In this case the only pixels that can remain set |
| 71 | // are inside the current clip rect since the extensions |
| 72 | // to infinity of both clips cancel out and whatever |
| 73 | // is outside of the current clip is removed |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 74 | fFiniteBoundType = kNormal_BoundsType; |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 75 | break; |
| 76 | case kInvPrev_Cur_FillCombo: |
| 77 | // In this case the current op is finite so the only pixels |
| 78 | // that aren't set are whatever isn't set in the previous |
| 79 | // clip and whatever this clip carves out |
| 80 | fFiniteBound.join(prevFinite); |
| 81 | fFiniteBoundType = kInsideOut_BoundsType; |
| 82 | break; |
| 83 | case kPrev_InvCur_FillCombo: |
| 84 | // In this case everything outside of this clip's bound |
| 85 | // is erased, so the only pixels that can remain set |
| 86 | // occur w/in the intersection of the two finite bounds |
| 87 | if (!fFiniteBound.intersect(prevFinite)) { |
| 88 | fFiniteBound.setEmpty(); |
| 89 | fGenID = kEmptyGenID; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 90 | } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 91 | fFiniteBoundType = kNormal_BoundsType; |
| 92 | break; |
| 93 | case kPrev_Cur_FillCombo: |
| 94 | // The most conservative result bound is that of the |
| 95 | // prior clip. This could be wildly incorrect if the |
| 96 | // second clip either exactly matches the first clip |
| 97 | // (which should yield the empty set) or reduces the |
| 98 | // size of the prior bound (e.g., if the second clip |
| 99 | // exactly matched the bottom half of the prior clip). |
| 100 | // We ignore these two possibilities. |
| 101 | fFiniteBound = prevFinite; |
| 102 | break; |
| 103 | default: |
| 104 | SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination"); |
| 105 | break; |
| 106 | } |
| 107 | } |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 109 | void SkClipStack::Element::combineBoundsXOR(int combination, const SkRect& prevFinite) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 110 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 111 | switch (combination) { |
| 112 | case kInvPrev_Cur_FillCombo: // fall through |
| 113 | case kPrev_InvCur_FillCombo: |
| 114 | // With only one of the clips inverted the result will always |
| 115 | // extend to infinity. The only pixels that may be un-writeable |
| 116 | // lie within the union of the two finite bounds |
| 117 | fFiniteBound.join(prevFinite); |
| 118 | fFiniteBoundType = kInsideOut_BoundsType; |
| 119 | break; |
| 120 | case kInvPrev_InvCur_FillCombo: |
| 121 | // The only pixels that can survive are within the |
| 122 | // union of the two bounding boxes since the extensions |
| 123 | // to infinity of both clips cancel out |
| 124 | // fall through! |
| 125 | case kPrev_Cur_FillCombo: |
| 126 | // The most conservative bound for xor is the |
| 127 | // union of the two bounds. If the two clips exactly overlapped |
| 128 | // the xor could yield the empty set. Similarly the xor |
| 129 | // could reduce the size of the original clip's bound (e.g., |
| 130 | // if the second clip exactly matched the bottom half of the |
| 131 | // first clip). We ignore these two cases. |
| 132 | fFiniteBound.join(prevFinite); |
| 133 | fFiniteBoundType = kNormal_BoundsType; |
| 134 | break; |
| 135 | default: |
| 136 | SkDEBUGFAIL("SkClipStack::Element::combineBoundsXOR Invalid fill combination"); |
| 137 | break; |
| 138 | } |
| 139 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 140 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 141 | // a mirror of combineBoundsIntersection |
| 142 | void SkClipStack::Element::combineBoundsUnion(int combination, const SkRect& prevFinite) { |
| 143 | |
| 144 | switch (combination) { |
| 145 | case kInvPrev_InvCur_FillCombo: |
| 146 | if (!fFiniteBound.intersect(prevFinite)) { |
| 147 | fFiniteBound.setEmpty(); |
| 148 | fGenID = kWideOpenGenID; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 149 | } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 150 | fFiniteBoundType = kInsideOut_BoundsType; |
| 151 | break; |
| 152 | case kInvPrev_Cur_FillCombo: |
| 153 | // The only pixels that won't be drawable are inside |
| 154 | // the prior clip's finite bound |
| 155 | fFiniteBound = prevFinite; |
| 156 | fFiniteBoundType = kInsideOut_BoundsType; |
| 157 | break; |
| 158 | case kPrev_InvCur_FillCombo: |
| 159 | // The only pixels that won't be drawable are inside |
| 160 | // this clip's finite bound |
| 161 | break; |
| 162 | case kPrev_Cur_FillCombo: |
| 163 | fFiniteBound.join(prevFinite); |
| 164 | break; |
| 165 | default: |
| 166 | SkDEBUGFAIL("SkClipStack::Element::combineBoundsUnion Invalid fill combination"); |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // a mirror of combineBoundsUnion |
| 172 | void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) { |
| 173 | |
| 174 | switch (combination) { |
| 175 | case kInvPrev_InvCur_FillCombo: |
| 176 | // The only pixels that aren't writable in this case |
| 177 | // occur in the union of the two finite bounds |
| 178 | fFiniteBound.join(prevFinite); |
| 179 | fFiniteBoundType = kInsideOut_BoundsType; |
| 180 | break; |
| 181 | case kInvPrev_Cur_FillCombo: |
| 182 | // In this case the only pixels that will remain writeable |
| 183 | // are within the current clip |
| 184 | break; |
| 185 | case kPrev_InvCur_FillCombo: |
| 186 | // In this case the only pixels that will remain writeable |
| 187 | // are with the previous clip |
| 188 | fFiniteBound = prevFinite; |
| 189 | fFiniteBoundType = kNormal_BoundsType; |
| 190 | break; |
| 191 | case kPrev_Cur_FillCombo: |
| 192 | if (!fFiniteBound.intersect(prevFinite)) { |
| 193 | fFiniteBound.setEmpty(); |
| 194 | fGenID = kEmptyGenID; |
| 195 | } |
| 196 | break; |
| 197 | default: |
| 198 | SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination"); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // a mirror of combineBoundsDiff |
| 204 | void SkClipStack::Element::combineBoundsRevDiff(int combination, const SkRect& prevFinite) { |
| 205 | |
| 206 | switch (combination) { |
| 207 | case kInvPrev_InvCur_FillCombo: |
| 208 | // The only pixels that can survive are in the |
| 209 | // previous bound since the extensions to infinity in |
| 210 | // both clips cancel out |
| 211 | fFiniteBound = prevFinite; |
| 212 | fFiniteBoundType = kNormal_BoundsType; |
| 213 | break; |
| 214 | case kInvPrev_Cur_FillCombo: |
| 215 | if (!fFiniteBound.intersect(prevFinite)) { |
| 216 | fFiniteBound.setEmpty(); |
| 217 | fGenID = kEmptyGenID; |
| 218 | } |
| 219 | fFiniteBoundType = kNormal_BoundsType; |
| 220 | break; |
| 221 | case kPrev_InvCur_FillCombo: |
| 222 | fFiniteBound.join(prevFinite); |
| 223 | fFiniteBoundType = kInsideOut_BoundsType; |
| 224 | break; |
| 225 | case kPrev_Cur_FillCombo: |
| 226 | // Fall through - as with the kDifference_Op case, the |
| 227 | // most conservative result bound is the bound of the |
| 228 | // current clip. The prior clip could reduce the size of this |
| 229 | // bound (as in the kDifference_Op case) but we are ignoring |
| 230 | // those cases. |
| 231 | break; |
| 232 | default: |
| 233 | SkDEBUGFAIL("SkClipStack::Element::combineBoundsRevDiff Invalid fill combination"); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void SkClipStack::Element::updateBoundAndGenID(const Element* prior) { |
| 239 | // We set this first here but we may overwrite it later if we determine that the clip is |
| 240 | // either wide-open or empty. |
| 241 | fGenID = GetNextGenID(); |
| 242 | |
| 243 | // First, optimistically update the current Element's bound information |
| 244 | // with the current clip's bound |
| 245 | fIsIntersectionOfRects = false; |
| 246 | if (kRect_Type == fType) { |
| 247 | fFiniteBound = fRect; |
| 248 | fFiniteBoundType = kNormal_BoundsType; |
| 249 | |
| 250 | if (SkRegion::kReplace_Op == fOp || |
| 251 | (SkRegion::kIntersect_Op == fOp && NULL == prior) || |
| 252 | (SkRegion::kIntersect_Op == fOp && prior->fIsIntersectionOfRects && |
| 253 | prior->rectRectIntersectAllowed(fRect, fDoAA))) { |
| 254 | fIsIntersectionOfRects = true; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 255 | } |
| 256 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 257 | } else { |
| 258 | SkASSERT(kPath_Type == fType); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 259 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 260 | fFiniteBound = fPath.getBounds(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 261 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 262 | if (fPath.isInverseFillType()) { |
| 263 | fFiniteBoundType = kInsideOut_BoundsType; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 264 | } else { |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 265 | fFiniteBoundType = kNormal_BoundsType; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 268 | |
| 269 | if (!fDoAA) { |
| 270 | // Here we mimic a non-anti-aliased scanline system. If there is |
| 271 | // no anti-aliasing we can integerize the bounding box to exclude |
| 272 | // fractional parts that won't be rendered. |
| 273 | // Note: the left edge is handled slightly differently below. We |
| 274 | // are a bit more generous in the rounding since we don't want to |
| 275 | // risk missing the left pixels when fLeft is very close to .5 |
| 276 | fFiniteBound.set(SkIntToScalar(SkScalarFloorToInt(fFiniteBound.fLeft+0.45f)), |
| 277 | SkIntToScalar(SkScalarRound(fFiniteBound.fTop)), |
| 278 | SkIntToScalar(SkScalarRound(fFiniteBound.fRight)), |
| 279 | SkIntToScalar(SkScalarRound(fFiniteBound.fBottom))); |
| 280 | } |
| 281 | |
| 282 | // Now determine the previous Element's bound information taking into |
| 283 | // account that there may be no previous clip |
| 284 | SkRect prevFinite; |
| 285 | SkClipStack::BoundsType prevType; |
| 286 | |
| 287 | if (NULL == prior) { |
| 288 | // no prior clip means the entire plane is writable |
| 289 | prevFinite.setEmpty(); // there are no pixels that cannot be drawn to |
| 290 | prevType = kInsideOut_BoundsType; |
| 291 | } else { |
| 292 | prevFinite = prior->fFiniteBound; |
| 293 | prevType = prior->fFiniteBoundType; |
| 294 | } |
| 295 | |
| 296 | FillCombo combination = kPrev_Cur_FillCombo; |
| 297 | if (kInsideOut_BoundsType == fFiniteBoundType) { |
| 298 | combination = (FillCombo) (combination | 0x01); |
| 299 | } |
| 300 | if (kInsideOut_BoundsType == prevType) { |
| 301 | combination = (FillCombo) (combination | 0x02); |
| 302 | } |
| 303 | |
| 304 | SkASSERT(kInvPrev_InvCur_FillCombo == combination || |
| 305 | kInvPrev_Cur_FillCombo == combination || |
| 306 | kPrev_InvCur_FillCombo == combination || |
| 307 | kPrev_Cur_FillCombo == combination); |
| 308 | |
| 309 | // Now integrate with clip with the prior clips |
| 310 | switch (fOp) { |
| 311 | case SkRegion::kDifference_Op: |
| 312 | this->combineBoundsDiff(combination, prevFinite); |
| 313 | break; |
| 314 | case SkRegion::kXOR_Op: |
| 315 | this->combineBoundsXOR(combination, prevFinite); |
| 316 | break; |
| 317 | case SkRegion::kUnion_Op: |
| 318 | this->combineBoundsUnion(combination, prevFinite); |
| 319 | break; |
| 320 | case SkRegion::kIntersect_Op: |
| 321 | this->combineBoundsIntersection(combination, prevFinite); |
| 322 | break; |
| 323 | case SkRegion::kReverseDifference_Op: |
| 324 | this->combineBoundsRevDiff(combination, prevFinite); |
| 325 | break; |
| 326 | case SkRegion::kReplace_Op: |
| 327 | // Replace just ignores everything prior |
| 328 | // The current clip's bound information is already filled in |
| 329 | // so nothing to do |
| 330 | break; |
| 331 | default: |
| 332 | SkDebugf("SkRegion::Op error/n"); |
| 333 | SkASSERT(0); |
| 334 | break; |
| 335 | } |
| 336 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 337 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 338 | // This constant determines how many Element's are allocated together as a block in |
robertphillips@google.com | f9a9084 | 2012-08-17 14:25:43 +0000 | [diff] [blame] | 339 | // the deque. As such it needs to balance allocating too much memory vs. |
| 340 | // incurring allocation/deallocation thrashing. It should roughly correspond to |
| 341 | // the deepest save/restore stack we expect to see. |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 342 | static const int kDefaultElementAllocCnt = 8; |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 343 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 344 | SkClipStack::SkClipStack() |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 345 | : fDeque(sizeof(Element), kDefaultElementAllocCnt) |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 346 | , fSaveCount(0) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 347 | } |
| 348 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 349 | SkClipStack::SkClipStack(const SkClipStack& b) |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 350 | : fDeque(sizeof(Element), kDefaultElementAllocCnt) { |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 351 | *this = b; |
| 352 | } |
| 353 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 354 | SkClipStack::SkClipStack(const SkRect& r) |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 355 | : fDeque(sizeof(Element), kDefaultElementAllocCnt) |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 356 | , fSaveCount(0) { |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 357 | if (!r.isEmpty()) { |
| 358 | this->clipDevRect(r, SkRegion::kReplace_Op, false); |
| 359 | } |
| 360 | } |
| 361 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 362 | SkClipStack::SkClipStack(const SkIRect& r) |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 363 | : fDeque(sizeof(Element), kDefaultElementAllocCnt) |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 364 | , fSaveCount(0) { |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 365 | if (!r.isEmpty()) { |
| 366 | SkRect temp; |
| 367 | temp.set(r); |
| 368 | this->clipDevRect(temp, SkRegion::kReplace_Op, false); |
| 369 | } |
| 370 | } |
| 371 | |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 372 | SkClipStack::~SkClipStack() { |
| 373 | reset(); |
| 374 | } |
| 375 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 376 | SkClipStack& SkClipStack::operator=(const SkClipStack& b) { |
| 377 | if (this == &b) { |
| 378 | return *this; |
| 379 | } |
| 380 | reset(); |
| 381 | |
| 382 | fSaveCount = b.fSaveCount; |
| 383 | SkDeque::F2BIter recIter(b.fDeque); |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 384 | for (const Element* element = (const Element*)recIter.next(); |
| 385 | element != NULL; |
| 386 | element = (const Element*)recIter.next()) { |
| 387 | new (fDeque.push_back()) Element(*element); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return *this; |
| 391 | } |
| 392 | |
| 393 | bool SkClipStack::operator==(const SkClipStack& b) const { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 394 | if (fSaveCount != b.fSaveCount || |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 395 | fDeque.count() != b.fDeque.count()) { |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | SkDeque::F2BIter myIter(fDeque); |
| 399 | SkDeque::F2BIter bIter(b.fDeque); |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 400 | const Element* myElement = (const Element*)myIter.next(); |
| 401 | const Element* bElement = (const Element*)bIter.next(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 402 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 403 | while (myElement != NULL && bElement != NULL) { |
| 404 | if (*myElement != *bElement) { |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 405 | return false; |
| 406 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 407 | myElement = (const Element*)myIter.next(); |
| 408 | bElement = (const Element*)bIter.next(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 409 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 410 | return myElement == NULL && bElement == NULL; |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 411 | } |
| 412 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 413 | void SkClipStack::reset() { |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 414 | // We used a placement new for each object in fDeque, so we're responsible |
| 415 | // for calling the destructor on each of them as well. |
| 416 | while (!fDeque.empty()) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 417 | Element* element = (Element*)fDeque.back(); |
| 418 | element->~Element(); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 419 | fDeque.pop_back(); |
| 420 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 421 | |
| 422 | fSaveCount = 0; |
| 423 | } |
| 424 | |
| 425 | void SkClipStack::save() { |
| 426 | fSaveCount += 1; |
| 427 | } |
| 428 | |
| 429 | void SkClipStack::restore() { |
| 430 | fSaveCount -= 1; |
| 431 | while (!fDeque.empty()) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 432 | Element* element = (Element*)fDeque.back(); |
| 433 | if (element->fSaveCount <= fSaveCount) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 434 | break; |
| 435 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 436 | this->purgeClip(element); |
| 437 | element->~Element(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 438 | fDeque.pop_back(); |
| 439 | } |
| 440 | } |
| 441 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 442 | void SkClipStack::getBounds(SkRect* canvFiniteBound, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 443 | BoundsType* boundType, |
| 444 | bool* isIntersectionOfRects) const { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 445 | SkASSERT(NULL != canvFiniteBound && NULL != boundType); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 446 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 447 | Element* element = (Element*)fDeque.back(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 448 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 449 | if (NULL == element) { |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 450 | // the clip is wide open - the infinite plane w/ no pixels un-writeable |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 451 | canvFiniteBound->setEmpty(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 452 | *boundType = kInsideOut_BoundsType; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 453 | if (NULL != isIntersectionOfRects) { |
| 454 | *isIntersectionOfRects = false; |
| 455 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 456 | return; |
| 457 | } |
| 458 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 459 | *canvFiniteBound = element->fFiniteBound; |
| 460 | *boundType = element->fFiniteBoundType; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 461 | if (NULL != isIntersectionOfRects) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 462 | *isIntersectionOfRects = element->fIsIntersectionOfRects; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 463 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 464 | } |
| 465 | |
bsalomon@google.com | 3ab43d5 | 2012-10-11 19:39:09 +0000 | [diff] [blame] | 466 | bool SkClipStack::intersectRectWithClip(SkRect* rect) const { |
| 467 | SkASSERT(NULL != rect); |
| 468 | |
| 469 | SkRect bounds; |
| 470 | SkClipStack::BoundsType bt; |
| 471 | this->getBounds(&bounds, &bt); |
| 472 | if (bt == SkClipStack::kInsideOut_BoundsType) { |
| 473 | if (bounds.contains(*rect)) { |
| 474 | return false; |
| 475 | } else { |
| 476 | // If rect's x values are both within bound's x range we |
| 477 | // could clip here. Same for y. But we don't bother to check. |
| 478 | return true; |
| 479 | } |
skia.committer@gmail.com | 5b6f916 | 2012-10-12 02:01:15 +0000 | [diff] [blame] | 480 | } else { |
bsalomon@google.com | 3ab43d5 | 2012-10-11 19:39:09 +0000 | [diff] [blame] | 481 | return rect->intersect(bounds); |
| 482 | } |
| 483 | } |
| 484 | |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 485 | void SkClipStack::clipDevRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 486 | |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 487 | // Use reverse iterator instead of back because Rect path may need previous |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 488 | SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart); |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 489 | Element* element = (Element*) iter.prev(); |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 490 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 491 | if (element && element->canBeIntersectedInPlace(fSaveCount, op)) { |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 492 | switch (element->fType) { |
| 493 | case Element::kEmpty_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 494 | element->checkEmpty(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 495 | return; |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 496 | case Element::kRect_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 497 | if (element->rectRectIntersectAllowed(rect, doAA)) { |
| 498 | this->purgeClip(element); |
| 499 | if (!element->fRect.intersect(rect)) { |
| 500 | element->setEmpty(); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 504 | element->fDoAA = doAA; |
| 505 | Element* prev = (Element*) iter.prev(); |
| 506 | element->updateBoundAndGenID(prev); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 507 | return; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 508 | } |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 509 | break; |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 510 | case Element::kPath_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 511 | if (!SkRect::Intersects(element->fPath.getBounds(), rect)) { |
| 512 | this->purgeClip(element); |
| 513 | element->setEmpty(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 514 | return; |
| 515 | } |
| 516 | break; |
| 517 | } |
| 518 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 519 | new (fDeque.push_back()) Element(fSaveCount, rect, op, doAA); |
| 520 | ((Element*) fDeque.back())->updateBoundAndGenID(element); |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 521 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 522 | if (element && element->fSaveCount == fSaveCount) { |
| 523 | this->purgeClip(element); |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 524 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 525 | } |
| 526 | |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 527 | void SkClipStack::clipDevPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
tomhudson@google.com | 4c43372 | 2012-03-09 16:48:20 +0000 | [diff] [blame] | 528 | SkRect alt; |
| 529 | if (path.isRect(&alt)) { |
| 530 | return this->clipDevRect(alt, op, doAA); |
| 531 | } |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 532 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 533 | Element* element = (Element*)fDeque.back(); |
| 534 | if (element && element->canBeIntersectedInPlace(fSaveCount, op)) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 535 | const SkRect& pathBounds = path.getBounds(); |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 536 | switch (element->fType) { |
| 537 | case Element::kEmpty_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 538 | element->checkEmpty(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 539 | return; |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 540 | case Element::kRect_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 541 | if (!SkRect::Intersects(element->fRect, pathBounds)) { |
| 542 | this->purgeClip(element); |
| 543 | element->setEmpty(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | break; |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 547 | case Element::kPath_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 548 | if (!SkRect::Intersects(element->fPath.getBounds(), pathBounds)) { |
| 549 | this->purgeClip(element); |
| 550 | element->setEmpty(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | break; |
| 554 | } |
| 555 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 556 | new (fDeque.push_back()) Element(fSaveCount, path, op, doAA); |
| 557 | ((Element*) fDeque.back())->updateBoundAndGenID(element); |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 558 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 559 | if (element && element->fSaveCount == fSaveCount) { |
| 560 | this->purgeClip(element); |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 561 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 562 | } |
| 563 | |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 564 | void SkClipStack::clipEmpty() { |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 565 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 566 | Element* element = (Element*) fDeque.back(); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 567 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 568 | if (element && element->canBeIntersectedInPlace(fSaveCount, SkRegion::kIntersect_Op)) { |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 569 | switch (element->fType) { |
| 570 | case Element::kEmpty_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 571 | element->checkEmpty(); |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 572 | return; |
bsalomon@google.com | 417bc13 | 2012-11-29 19:16:32 +0000 | [diff] [blame] | 573 | case Element::kRect_Type: |
| 574 | case Element::kPath_Type: |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 575 | this->purgeClip(element); |
| 576 | element->setEmpty(); |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 580 | new (fDeque.push_back()) Element(fSaveCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 581 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 582 | if (element && element->fSaveCount == fSaveCount) { |
| 583 | this->purgeClip(element); |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 584 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 585 | ((Element*)fDeque.back())->fGenID = kEmptyGenID; |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 586 | } |
| 587 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 588 | bool SkClipStack::isWideOpen() const { |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 589 | if (0 == fDeque.count()) { |
| 590 | return true; |
| 591 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 592 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 593 | const Element* back = (const Element*) fDeque.back(); |
bsalomon@google.com | edb26fd | 2012-11-28 14:42:41 +0000 | [diff] [blame] | 594 | return kWideOpenGenID == back->fGenID || |
| 595 | (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 596 | } |
| 597 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 598 | /////////////////////////////////////////////////////////////////////////////// |
| 599 | |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 600 | SkClipStack::Iter::Iter() : fStack(NULL) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 601 | } |
| 602 | |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 603 | SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc) |
| 604 | : fStack(&stack) { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 605 | this->reset(stack, startLoc); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 606 | } |
| 607 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame^] | 608 | const SkClipStack::Element* SkClipStack::Iter::next() { |
| 609 | return (const SkClipStack::Element*)fIter.next(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 610 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 611 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame^] | 612 | const SkClipStack::Element* SkClipStack::Iter::prev() { |
| 613 | return (const SkClipStack::Element*)fIter.prev(); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 614 | } |
| 615 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame^] | 616 | const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkRegion::Op op) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 617 | |
| 618 | if (NULL == fStack) { |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart); |
| 623 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 624 | const SkClipStack::Element* element = NULL; |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 625 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 626 | for (element = (const SkClipStack::Element*) fIter.prev(); |
| 627 | NULL != element; |
| 628 | element = (const SkClipStack::Element*) fIter.prev()) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 629 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 630 | if (op == element->fOp) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 631 | // The Deque's iterator is actually one pace ahead of the |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 632 | // returned value. So while "element" is the element we want to |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 633 | // return, the iterator is actually pointing at (and will |
| 634 | // return on the next "next" or "prev" call) the element |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 635 | // in front of it in the deque. Bump the iterator forward a |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 636 | // step so we get the expected result. |
| 637 | if (NULL == fIter.next()) { |
| 638 | // The reverse iterator has run off the front of the deque |
| 639 | // (i.e., the "op" clip is the first clip) and can't |
| 640 | // recover. Reset the iterator to start at the front. |
| 641 | fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart); |
| 642 | } |
| 643 | break; |
| 644 | } |
| 645 | } |
| 646 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 647 | if (NULL == element) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 648 | // There were no "op" clips |
| 649 | fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart); |
| 650 | } |
| 651 | |
| 652 | return this->next(); |
| 653 | } |
| 654 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 655 | void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 656 | fStack = &stack; |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 657 | fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 658 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 659 | |
| 660 | // helper method |
| 661 | void SkClipStack::getConservativeBounds(int offsetX, |
| 662 | int offsetY, |
| 663 | int maxWidth, |
| 664 | int maxHeight, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 665 | SkRect* devBounds, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 666 | bool* isIntersectionOfRects) const { |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 667 | SkASSERT(NULL != devBounds); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 668 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 669 | devBounds->setLTRB(0, 0, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 670 | SkIntToScalar(maxWidth), SkIntToScalar(maxHeight)); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 671 | |
| 672 | SkRect temp; |
| 673 | SkClipStack::BoundsType boundType; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 674 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 675 | // temp starts off in canvas space here |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 676 | this->getBounds(&temp, &boundType, isIntersectionOfRects); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 677 | if (SkClipStack::kInsideOut_BoundsType == boundType) { |
| 678 | return; |
| 679 | } |
| 680 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 681 | // but is converted to device space here |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 682 | temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY)); |
| 683 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 684 | if (!devBounds->intersect(temp)) { |
| 685 | devBounds->setEmpty(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 688 | |
| 689 | void SkClipStack::addPurgeClipCallback(PFPurgeClipCB callback, void* data) const { |
| 690 | ClipCallbackData temp = { callback, data }; |
| 691 | fCallbackData.append(1, &temp); |
| 692 | } |
| 693 | |
| 694 | void SkClipStack::removePurgeClipCallback(PFPurgeClipCB callback, void* data) const { |
| 695 | ClipCallbackData temp = { callback, data }; |
| 696 | int index = fCallbackData.find(temp); |
| 697 | if (index >= 0) { |
| 698 | fCallbackData.removeShuffle(index); |
| 699 | } |
| 700 | } |
| 701 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 702 | // The clip state represented by 'element' will never be used again. Purge it. |
| 703 | void SkClipStack::purgeClip(Element* element) { |
| 704 | SkASSERT(NULL != element); |
| 705 | if (element->fGenID >= 0 && element->fGenID < kFirstUnreservedGenID) { |
bsalomon@google.com | edb26fd | 2012-11-28 14:42:41 +0000 | [diff] [blame] | 706 | return; |
| 707 | } |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 708 | |
| 709 | for (int i = 0; i < fCallbackData.count(); ++i) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 710 | (*fCallbackData[i].fCallback)(element->fGenID, fCallbackData[i].fData); |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 711 | } |
| 712 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 713 | // Invalidate element's gen ID so handlers can detect already handled records |
| 714 | element->fGenID = kInvalidGenID; |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | int32_t SkClipStack::GetNextGenID() { |
bsalomon@google.com | edb26fd | 2012-11-28 14:42:41 +0000 | [diff] [blame] | 718 | // TODO: handle overflow. |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 719 | return sk_atomic_inc(&gGenID); |
| 720 | } |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 721 | |
| 722 | int32_t SkClipStack::getTopmostGenID() const { |
| 723 | |
| 724 | if (fDeque.empty()) { |
| 725 | return kInvalidGenID; |
| 726 | } |
| 727 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 728 | Element* element = (Element*)fDeque.back(); |
| 729 | return element->fGenID; |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 730 | } |