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