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