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