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