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