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