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