bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 1 | /* |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 2 | * Copyright 2016 Google Inc. |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrReducedClip.h" |
| 9 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 10 | #include "GrAppliedClip.h" |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 11 | #include "GrClip.h" |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 12 | #include "GrColor.h" |
| 13 | #include "GrContextPriv.h" |
| 14 | #include "GrDrawContext.h" |
| 15 | #include "GrDrawContextPriv.h" |
| 16 | #include "GrDrawingManager.h" |
| 17 | #include "GrFixedClip.h" |
| 18 | #include "GrPathRenderer.h" |
| 19 | #include "GrStyle.h" |
| 20 | #include "GrUserStencilSettings.h" |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 21 | |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 22 | typedef SkClipStack::Element Element; |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 23 | |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 24 | /** |
| 25 | * There are plenty of optimizations that could be added here. Maybe flips could be folded into |
| 26 | * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps |
| 27 | * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations |
| 28 | * based on later intersect operations, and perhaps remove intersect-rects. We could optionally |
| 29 | * take a rect in case the caller knows a bound on what is to be drawn through this clip. |
| 30 | */ |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 31 | GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds, |
| 32 | int maxWindowRectangles) { |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 33 | SkASSERT(!queryBounds.isEmpty()); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 34 | fHasIBounds = false; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 35 | |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 36 | if (stack.isWideOpen()) { |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 37 | fInitialState = InitialState::kAllIn; |
| 38 | return; |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | SkClipStack::BoundsType stackBoundsType; |
| 42 | SkRect stackBounds; |
| 43 | bool iior; |
| 44 | stack.getBounds(&stackBounds, &stackBoundsType, &iior); |
| 45 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 46 | if (stackBounds.isEmpty() || GrClip::IsOutsideClip(stackBounds, queryBounds)) { |
| 47 | bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType; |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 48 | fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut; |
| 49 | return; |
bsalomon@google.com | 170bd79 | 2012-12-05 22:26:11 +0000 | [diff] [blame] | 50 | } |
| 51 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 52 | if (iior) { |
| 53 | // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds. |
| 54 | // This should only be true if aa/non-aa status matches among all elements. |
| 55 | SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType); |
| 56 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 57 | if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) { |
| 58 | // The clip is a non-aa rect. This is the one spot where we can actually implement the |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 59 | // clip (using fIBounds) rather than just telling the caller what it should be. |
| 60 | stackBounds.round(&fIBounds); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 61 | fHasIBounds = true; |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 62 | fInitialState = fIBounds.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn; |
| 63 | return; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 64 | } |
| 65 | if (GrClip::IsInsideClip(stackBounds, queryBounds)) { |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 66 | fInitialState = InitialState::kAllIn; |
| 67 | return; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 68 | } |
| 69 | |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 70 | SkRect tightBounds; |
| 71 | SkAssertResult(tightBounds.intersect(stackBounds, queryBounds)); |
| 72 | fIBounds = GrClip::GetPixelIBounds(tightBounds); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 73 | SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above. |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 74 | fHasIBounds = true; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 75 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 76 | // Implement the clip with an AA rect element. |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 77 | fElements.addToHead(stackBounds, SkCanvas::kReplace_Op, true/*doAA*/); |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 78 | fElementsGenID = stack.getTopmostGenID(); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 79 | fRequiresAA = true; |
| 80 | |
| 81 | fInitialState = InitialState::kAllOut; |
| 82 | return; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | SkRect tighterQuery = queryBounds; |
| 86 | if (SkClipStack::kNormal_BoundsType == stackBoundsType) { |
| 87 | // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This new |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 88 | // clip will be enforced by the scissor through fIBounds.) |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 89 | SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds))); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 90 | } |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 91 | |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 92 | fIBounds = GrClip::GetPixelIBounds(tighterQuery); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 93 | SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above. |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 94 | fHasIBounds = true; |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 95 | |
bsalomon@google.com | 34cd70a | 2012-12-06 14:23:20 +0000 | [diff] [blame] | 96 | // Now that we have determined the bounds to use and filtered out the trivial cases, call the |
| 97 | // helper that actually walks the stack. |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 98 | this->walkStack(stack, tighterQuery, maxWindowRectangles); |
| 99 | |
| 100 | if (fWindowRects.count() < maxWindowRectangles) { |
| 101 | this->addInteriorWindowRectangles(maxWindowRectangles); |
| 102 | } |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 103 | } |
| 104 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 105 | void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds, |
| 106 | int maxWindowRectangles) { |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 107 | // walk backwards until we get to: |
| 108 | // a) the beginning |
| 109 | // b) an operation that is known to make the bounds all inside/outside |
| 110 | // c) a replace operation |
| 111 | |
| 112 | enum class InitialTriState { |
| 113 | kUnknown = -1, |
| 114 | kAllIn = (int)GrReducedClip::InitialState::kAllIn, |
| 115 | kAllOut = (int)GrReducedClip::InitialState::kAllOut |
| 116 | } initialTriState = InitialTriState::kUnknown; |
| 117 | |
| 118 | // During our backwards walk, track whether we've seen ops that either grow or shrink the clip. |
| 119 | // TODO: track these per saved clip so that we can consider them on the forward pass. |
| 120 | bool embiggens = false; |
| 121 | bool emsmallens = false; |
| 122 | |
| 123 | // We use a slightly relaxed set of query bounds for element containment tests. This is to |
| 124 | // account for floating point rounding error that may have occurred during coord transforms. |
| 125 | SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance, |
| 126 | GrClip::kBoundsTolerance); |
| 127 | |
| 128 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 129 | int numAAElements = 0; |
| 130 | while (InitialTriState::kUnknown == initialTriState) { |
| 131 | const Element* element = iter.prev(); |
| 132 | if (nullptr == element) { |
| 133 | initialTriState = InitialTriState::kAllIn; |
| 134 | break; |
| 135 | } |
| 136 | if (SkClipStack::kEmptyGenID == element->getGenID()) { |
| 137 | initialTriState = InitialTriState::kAllOut; |
| 138 | break; |
| 139 | } |
| 140 | if (SkClipStack::kWideOpenGenID == element->getGenID()) { |
| 141 | initialTriState = InitialTriState::kAllIn; |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | bool skippable = false; |
| 146 | bool isFlip = false; // does this op just flip the in/out state of every point in the bounds |
| 147 | |
| 148 | switch (element->getOp()) { |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 149 | case SkCanvas::kDifference_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 150 | // check if the shape subtracted either contains the entire bounds (and makes |
| 151 | // the clip empty) or is outside the bounds and therefore can be skipped. |
| 152 | if (element->isInverseFilled()) { |
| 153 | if (element->contains(relaxedQueryBounds)) { |
| 154 | skippable = true; |
| 155 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 156 | initialTriState = InitialTriState::kAllOut; |
| 157 | skippable = true; |
| 158 | } |
| 159 | } else { |
| 160 | if (element->contains(relaxedQueryBounds)) { |
| 161 | initialTriState = InitialTriState::kAllOut; |
| 162 | skippable = true; |
| 163 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 164 | skippable = true; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 165 | } else if (fWindowRects.count() < maxWindowRectangles && !embiggens && |
| 166 | !element->isAA() && Element::kRect_Type == element->getType()) { |
| 167 | this->addWindowRectangle(element->getRect(), false); |
| 168 | skippable = true; |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | if (!skippable) { |
| 172 | emsmallens = true; |
| 173 | } |
| 174 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 175 | case SkCanvas::kIntersect_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 176 | // check if the shape intersected contains the entire bounds and therefore can |
| 177 | // be skipped or it is outside the entire bounds and therefore makes the clip |
| 178 | // empty. |
| 179 | if (element->isInverseFilled()) { |
| 180 | if (element->contains(relaxedQueryBounds)) { |
| 181 | initialTriState = InitialTriState::kAllOut; |
| 182 | skippable = true; |
| 183 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 184 | skippable = true; |
| 185 | } |
| 186 | } else { |
| 187 | if (element->contains(relaxedQueryBounds)) { |
| 188 | skippable = true; |
| 189 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 190 | initialTriState = InitialTriState::kAllOut; |
| 191 | skippable = true; |
| 192 | } else if (!embiggens && !element->isAA() && |
| 193 | Element::kRect_Type == element->getType()) { |
| 194 | // fIBounds and queryBounds have already acccounted for this element via |
| 195 | // clip stack bounds; here we just apply the non-aa rounding effect. |
| 196 | SkIRect nonaaRect; |
| 197 | element->getRect().round(&nonaaRect); |
| 198 | if (!this->intersectIBounds(nonaaRect)) { |
| 199 | return; |
| 200 | } |
| 201 | skippable = true; |
| 202 | } |
| 203 | } |
| 204 | if (!skippable) { |
| 205 | emsmallens = true; |
| 206 | } |
| 207 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 208 | case SkCanvas::kUnion_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 209 | // If the union-ed shape contains the entire bounds then after this element |
| 210 | // the bounds is entirely inside the clip. If the union-ed shape is outside the |
| 211 | // bounds then this op can be skipped. |
| 212 | if (element->isInverseFilled()) { |
| 213 | if (element->contains(relaxedQueryBounds)) { |
| 214 | skippable = true; |
| 215 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 216 | initialTriState = InitialTriState::kAllIn; |
| 217 | skippable = true; |
| 218 | } |
| 219 | } else { |
| 220 | if (element->contains(relaxedQueryBounds)) { |
| 221 | initialTriState = InitialTriState::kAllIn; |
| 222 | skippable = true; |
| 223 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 224 | skippable = true; |
| 225 | } |
| 226 | } |
| 227 | if (!skippable) { |
| 228 | embiggens = true; |
| 229 | } |
| 230 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 231 | case SkCanvas::kXOR_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 232 | // If the bounds is entirely inside the shape being xor-ed then the effect is |
| 233 | // to flip the inside/outside state of every point in the bounds. We may be |
| 234 | // able to take advantage of this in the forward pass. If the xor-ed shape |
| 235 | // doesn't intersect the bounds then it can be skipped. |
| 236 | if (element->isInverseFilled()) { |
| 237 | if (element->contains(relaxedQueryBounds)) { |
| 238 | skippable = true; |
| 239 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 240 | isFlip = true; |
| 241 | } |
| 242 | } else { |
| 243 | if (element->contains(relaxedQueryBounds)) { |
| 244 | isFlip = true; |
| 245 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 246 | skippable = true; |
| 247 | } |
| 248 | } |
| 249 | if (!skippable) { |
| 250 | emsmallens = embiggens = true; |
| 251 | } |
| 252 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 253 | case SkCanvas::kReverseDifference_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 254 | // When the bounds is entirely within the rev-diff shape then this behaves like xor |
| 255 | // and reverses every point inside the bounds. If the shape is completely outside |
| 256 | // the bounds then we know after this element is applied that the bounds will be |
| 257 | // all outside the current clip.B |
| 258 | if (element->isInverseFilled()) { |
| 259 | if (element->contains(relaxedQueryBounds)) { |
| 260 | initialTriState = InitialTriState::kAllOut; |
| 261 | skippable = true; |
| 262 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 263 | isFlip = true; |
| 264 | } |
| 265 | } else { |
| 266 | if (element->contains(relaxedQueryBounds)) { |
| 267 | isFlip = true; |
| 268 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 269 | initialTriState = InitialTriState::kAllOut; |
| 270 | skippable = true; |
| 271 | } |
| 272 | } |
| 273 | if (!skippable) { |
| 274 | emsmallens = embiggens = true; |
| 275 | } |
| 276 | break; |
| 277 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 278 | case SkCanvas::kReplace_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 279 | // Replace will always terminate our walk. We will either begin the forward walk |
| 280 | // at the replace op or detect here than the shape is either completely inside |
| 281 | // or completely outside the bounds. In this latter case it can be skipped by |
| 282 | // setting the correct value for initialTriState. |
| 283 | if (element->isInverseFilled()) { |
| 284 | if (element->contains(relaxedQueryBounds)) { |
| 285 | initialTriState = InitialTriState::kAllOut; |
| 286 | skippable = true; |
| 287 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 288 | initialTriState = InitialTriState::kAllIn; |
| 289 | skippable = true; |
| 290 | } |
| 291 | } else { |
| 292 | if (element->contains(relaxedQueryBounds)) { |
| 293 | initialTriState = InitialTriState::kAllIn; |
| 294 | skippable = true; |
| 295 | } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) { |
| 296 | initialTriState = InitialTriState::kAllOut; |
| 297 | skippable = true; |
| 298 | } else if (!embiggens && !element->isAA() && |
| 299 | Element::kRect_Type == element->getType()) { |
| 300 | // fIBounds and queryBounds have already acccounted for this element via |
| 301 | // clip stack bounds; here we just apply the non-aa rounding effect. |
| 302 | SkIRect nonaaRect; |
| 303 | element->getRect().round(&nonaaRect); |
| 304 | if (!this->intersectIBounds(nonaaRect)) { |
| 305 | return; |
| 306 | } |
| 307 | initialTriState = InitialTriState::kAllIn; |
| 308 | skippable = true; |
| 309 | } |
| 310 | } |
| 311 | if (!skippable) { |
| 312 | initialTriState = InitialTriState::kAllOut; |
| 313 | embiggens = emsmallens = true; |
| 314 | } |
| 315 | break; |
| 316 | default: |
| 317 | SkDEBUGFAIL("Unexpected op."); |
| 318 | break; |
| 319 | } |
| 320 | if (!skippable) { |
| 321 | if (0 == fElements.count()) { |
| 322 | // This will be the last element. Record the stricter genID. |
| 323 | fElementsGenID = element->getGenID(); |
| 324 | } |
| 325 | |
| 326 | // if it is a flip, change it to a bounds-filling rect |
| 327 | if (isFlip) { |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 328 | SkASSERT(SkCanvas::kXOR_Op == element->getOp() || |
| 329 | SkCanvas::kReverseDifference_Op == element->getOp()); |
| 330 | fElements.addToHead(SkRect::Make(fIBounds), SkCanvas::kReverseDifference_Op, false); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 331 | } else { |
| 332 | Element* newElement = fElements.addToHead(*element); |
| 333 | if (newElement->isAA()) { |
| 334 | ++numAAElements; |
| 335 | } |
| 336 | // Intersecting an inverse shape is the same as differencing the non-inverse shape. |
| 337 | // Replacing with an inverse shape is the same as setting initialState=kAllIn and |
| 338 | // differencing the non-inverse shape. |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 339 | bool isReplace = SkCanvas::kReplace_Op == newElement->getOp(); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 340 | if (newElement->isInverseFilled() && |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 341 | (SkCanvas::kIntersect_Op == newElement->getOp() || isReplace)) { |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 342 | newElement->invertShapeFillType(); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 343 | newElement->setOp(SkCanvas::kDifference_Op); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 344 | if (isReplace) { |
| 345 | SkASSERT(InitialTriState::kAllOut == initialTriState); |
| 346 | initialTriState = InitialTriState::kAllIn; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if ((InitialTriState::kAllOut == initialTriState && !embiggens) || |
| 354 | (InitialTriState::kAllIn == initialTriState && !emsmallens)) { |
| 355 | fElements.reset(); |
| 356 | numAAElements = 0; |
| 357 | } else { |
| 358 | Element* element = fElements.headIter().get(); |
| 359 | while (element) { |
| 360 | bool skippable = false; |
| 361 | switch (element->getOp()) { |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 362 | case SkCanvas::kDifference_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 363 | // subtracting from the empty set yields the empty set. |
| 364 | skippable = InitialTriState::kAllOut == initialTriState; |
| 365 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 366 | case SkCanvas::kIntersect_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 367 | // intersecting with the empty set yields the empty set |
| 368 | if (InitialTriState::kAllOut == initialTriState) { |
| 369 | skippable = true; |
| 370 | } else { |
| 371 | // We can clear to zero and then simply draw the clip element. |
| 372 | initialTriState = InitialTriState::kAllOut; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 373 | element->setOp(SkCanvas::kReplace_Op); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 374 | } |
| 375 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 376 | case SkCanvas::kUnion_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 377 | if (InitialTriState::kAllIn == initialTriState) { |
| 378 | // unioning the infinite plane with anything is a no-op. |
| 379 | skippable = true; |
| 380 | } else { |
| 381 | // unioning the empty set with a shape is the shape. |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 382 | element->setOp(SkCanvas::kReplace_Op); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 383 | } |
| 384 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 385 | case SkCanvas::kXOR_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 386 | if (InitialTriState::kAllOut == initialTriState) { |
| 387 | // xor could be changed to diff in the kAllIn case, not sure it's a win. |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 388 | element->setOp(SkCanvas::kReplace_Op); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 389 | } |
| 390 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 391 | case SkCanvas::kReverseDifference_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 392 | if (InitialTriState::kAllIn == initialTriState) { |
| 393 | // subtracting the whole plane will yield the empty set. |
| 394 | skippable = true; |
| 395 | initialTriState = InitialTriState::kAllOut; |
| 396 | } else { |
| 397 | // this picks up flips inserted in the backwards pass. |
| 398 | skippable = element->isInverseFilled() ? |
| 399 | GrClip::IsOutsideClip(element->getBounds(), queryBounds) : |
| 400 | element->contains(relaxedQueryBounds); |
| 401 | if (skippable) { |
| 402 | initialTriState = InitialTriState::kAllIn; |
| 403 | } else { |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 404 | element->setOp(SkCanvas::kReplace_Op); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | break; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 408 | case SkCanvas::kReplace_Op: |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 409 | skippable = false; // we would have skipped it in the backwards walk if we |
| 410 | // could've. |
| 411 | break; |
| 412 | default: |
| 413 | SkDEBUGFAIL("Unexpected op."); |
| 414 | break; |
| 415 | } |
| 416 | if (!skippable) { |
| 417 | break; |
| 418 | } else { |
| 419 | if (element->isAA()) { |
| 420 | --numAAElements; |
| 421 | } |
| 422 | fElements.popHead(); |
| 423 | element = fElements.headIter().get(); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | fRequiresAA = numAAElements > 0; |
| 428 | |
| 429 | SkASSERT(InitialTriState::kUnknown != initialTriState); |
| 430 | fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState); |
| 431 | } |
| 432 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 433 | static bool element_is_pure_subtract(SkCanvas::ClipOp op) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 434 | SkASSERT(op >= 0); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 435 | return op <= SkCanvas::kIntersect_Op; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 436 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 437 | GR_STATIC_ASSERT(0 == SkCanvas::kDifference_Op); |
| 438 | GR_STATIC_ASSERT(1 == SkCanvas::kIntersect_Op); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void GrReducedClip::addInteriorWindowRectangles(int maxWindowRectangles) { |
| 442 | SkASSERT(fWindowRects.count() < maxWindowRectangles); |
| 443 | // Walk backwards through the element list and add window rectangles to the interiors of |
| 444 | // "difference" elements. Quit if we encounter an element that may grow the clip. |
| 445 | ElementList::Iter iter(fElements, ElementList::Iter::kTail_IterStart); |
| 446 | for (; iter.get() && element_is_pure_subtract(iter.get()->getOp()); iter.prev()) { |
| 447 | const Element* element = iter.get(); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 448 | if (SkCanvas::kDifference_Op != element->getOp()) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 449 | continue; |
| 450 | } |
| 451 | |
| 452 | if (Element::kRect_Type == element->getType()) { |
| 453 | SkASSERT(element->isAA()); |
| 454 | this->addWindowRectangle(element->getRect(), true); |
| 455 | if (fWindowRects.count() >= maxWindowRectangles) { |
| 456 | return; |
| 457 | } |
| 458 | continue; |
| 459 | } |
| 460 | |
| 461 | if (Element::kRRect_Type == element->getType()) { |
| 462 | // For round rects we add two overlapping windows in the shape of a plus. |
| 463 | const SkRRect& clipRRect = element->getRRect(); |
| 464 | SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner); |
| 465 | SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner); |
| 466 | if (SkRRect::kComplex_Type == clipRRect.getType()) { |
| 467 | const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner); |
| 468 | const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner); |
| 469 | insetTL.fX = SkTMax(insetTL.x(), insetBL.x()); |
| 470 | insetTL.fY = SkTMax(insetTL.y(), insetTR.y()); |
| 471 | insetBR.fX = SkTMax(insetBR.x(), insetTR.x()); |
| 472 | insetBR.fY = SkTMax(insetBR.y(), insetBL.y()); |
| 473 | } |
| 474 | const SkRect& bounds = clipRRect.getBounds(); |
| 475 | if (insetTL.x() + insetBR.x() >= bounds.width() || |
| 476 | insetTL.y() + insetBR.y() >= bounds.height()) { |
| 477 | continue; // The interior "plus" is empty. |
| 478 | } |
| 479 | |
| 480 | SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(), |
| 481 | bounds.right(), bounds.bottom() - insetBR.y()); |
| 482 | this->addWindowRectangle(horzRect, element->isAA()); |
| 483 | if (fWindowRects.count() >= maxWindowRectangles) { |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(), |
| 488 | bounds.right() - insetBR.x(), bounds.bottom()); |
| 489 | this->addWindowRectangle(vertRect, element->isAA()); |
| 490 | if (fWindowRects.count() >= maxWindowRectangles) { |
| 491 | return; |
| 492 | } |
| 493 | continue; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) { |
| 499 | SkIRect window; |
| 500 | if (!elementIsAA) { |
| 501 | elementInteriorRect.round(&window); |
| 502 | } else { |
| 503 | elementInteriorRect.roundIn(&window); |
| 504 | } |
| 505 | if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions. |
| 506 | fWindowRects.addWindow(window); |
| 507 | } |
| 508 | } |
| 509 | |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 510 | inline bool GrReducedClip::intersectIBounds(const SkIRect& irect) { |
| 511 | SkASSERT(fHasIBounds); |
| 512 | if (!fIBounds.intersect(irect)) { |
| 513 | fHasIBounds = false; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 514 | fWindowRects.reset(); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 515 | fElements.reset(); |
| 516 | fRequiresAA = false; |
| 517 | fInitialState = InitialState::kAllOut; |
| 518 | return false; |
| 519 | } |
| 520 | return true; |
bsalomon@google.com | 34cd70a | 2012-12-06 14:23:20 +0000 | [diff] [blame] | 521 | } |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 522 | |
| 523 | //////////////////////////////////////////////////////////////////////////////// |
| 524 | // Create a 8-bit clip mask in alpha |
| 525 | |
| 526 | static bool stencil_element(GrDrawContext* dc, |
| 527 | const GrFixedClip& clip, |
| 528 | const GrUserStencilSettings* ss, |
| 529 | const SkMatrix& viewMatrix, |
| 530 | const SkClipStack::Element* element) { |
| 531 | |
| 532 | // TODO: Draw rrects directly here. |
| 533 | switch (element->getType()) { |
| 534 | case Element::kEmpty_Type: |
| 535 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 536 | break; |
| 537 | case Element::kRect_Type: |
| 538 | return dc->drawContextPriv().drawAndStencilRect(clip, ss, |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 539 | (SkRegion::Op)element->getOp(), |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 540 | element->isInverseFilled(), |
| 541 | element->isAA(), |
| 542 | viewMatrix, element->getRect()); |
| 543 | break; |
| 544 | default: { |
| 545 | SkPath path; |
| 546 | element->asPath(&path); |
| 547 | if (path.isInverseFillType()) { |
| 548 | path.toggleInverseFillType(); |
| 549 | } |
| 550 | |
| 551 | return dc->drawContextPriv().drawAndStencilPath(clip, ss, |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 552 | (SkRegion::Op)element->getOp(), |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 553 | element->isInverseFilled(), |
| 554 | element->isAA(), viewMatrix, path); |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | static void draw_element(GrDrawContext* dc, |
| 563 | const GrClip& clip, // TODO: can this just always be WideOpen? |
| 564 | const GrPaint &paint, |
| 565 | const SkMatrix& viewMatrix, |
| 566 | const SkClipStack::Element* element) { |
| 567 | |
| 568 | // TODO: Draw rrects directly here. |
| 569 | switch (element->getType()) { |
| 570 | case Element::kEmpty_Type: |
| 571 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 572 | break; |
| 573 | case Element::kRect_Type: |
| 574 | dc->drawRect(clip, paint, viewMatrix, element->getRect()); |
| 575 | break; |
| 576 | default: { |
| 577 | SkPath path; |
| 578 | element->asPath(&path); |
| 579 | if (path.isInverseFillType()) { |
| 580 | path.toggleInverseFillType(); |
| 581 | } |
| 582 | |
| 583 | dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill()); |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | bool GrReducedClip::drawAlphaClipMask(GrDrawContext* dc) const { |
| 590 | // The texture may be larger than necessary, this rect represents the part of the texture |
| 591 | // we populate with a rasterization of the clip. |
| 592 | GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height())); |
| 593 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 594 | if (!fWindowRects.empty()) { |
| 595 | clip.setWindowRectangles(fWindowRects, {fIBounds.left(), fIBounds.top()}, |
| 596 | GrWindowRectsState::Mode::kExclusive); |
| 597 | } |
| 598 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 599 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 600 | // clear the part that we care about. |
| 601 | GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0; |
| 602 | dc->drawContextPriv().clear(clip, initialCoverage, true); |
| 603 | |
| 604 | // Set the matrix so that rendered clip elements are transformed to mask space from clip space. |
| 605 | SkMatrix translate; |
| 606 | translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top())); |
| 607 | |
| 608 | // walk through each clip element and perform its set op |
| 609 | for (ElementList::Iter iter(fElements); iter.get(); iter.next()) { |
| 610 | const Element* element = iter.get(); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 611 | SkRegion::Op op = (SkRegion::Op)element->getOp(); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 612 | bool invert = element->isInverseFilled(); |
| 613 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 614 | // draw directly into the result with the stencil set to make the pixels affected |
| 615 | // by the clip shape be non-zero. |
| 616 | static constexpr GrUserStencilSettings kStencilInElement( |
| 617 | GrUserStencilSettings::StaticInit< |
| 618 | 0xffff, |
| 619 | GrUserStencilTest::kAlways, |
| 620 | 0xffff, |
| 621 | GrUserStencilOp::kReplace, |
| 622 | GrUserStencilOp::kReplace, |
| 623 | 0xffff>() |
| 624 | ); |
| 625 | if (!stencil_element(dc, clip, &kStencilInElement, translate, element)) { |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | // Draw to the exterior pixels (those with a zero stencil value). |
| 630 | static constexpr GrUserStencilSettings kDrawOutsideElement( |
| 631 | GrUserStencilSettings::StaticInit< |
| 632 | 0x0000, |
| 633 | GrUserStencilTest::kEqual, |
| 634 | 0xffff, |
| 635 | GrUserStencilOp::kZero, |
| 636 | GrUserStencilOp::kZero, |
| 637 | 0xffff>() |
| 638 | ); |
| 639 | if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement, |
| 640 | op, !invert, false, |
| 641 | translate, |
| 642 | SkRect::Make(fIBounds))) { |
| 643 | return false; |
| 644 | } |
| 645 | } else { |
| 646 | // all the remaining ops can just be directly draw into the accumulation buffer |
| 647 | GrPaint paint; |
| 648 | paint.setAntiAlias(element->isAA()); |
| 649 | paint.setCoverageSetOpXPFactory(op, false); |
| 650 | |
| 651 | draw_element(dc, clip, paint, translate, element); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | //////////////////////////////////////////////////////////////////////////////// |
| 659 | // Create a 1-bit clip mask in the stencil buffer. |
| 660 | |
| 661 | class StencilClip final : public GrClip { |
| 662 | public: |
| 663 | StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {} |
| 664 | const GrFixedClip& fixedClip() const { return fFixedClip; } |
| 665 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 666 | void setWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin, |
| 667 | GrWindowRectsState::Mode mode) { |
| 668 | fFixedClip.setWindowRectangles(windows, origin, mode); |
| 669 | } |
| 670 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 671 | private: |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 672 | bool quickContains(const SkRect&) const override { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 673 | return false; |
| 674 | } |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 675 | void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override { |
| 676 | fFixedClip.getConservativeBounds(width, height, bounds, iior); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 677 | } |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 678 | bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | bool apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA, |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 682 | bool hasUserStencilSettings, GrAppliedClip* out) const override { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 683 | if (!fFixedClip.apply(context, drawContext, useHWAA, hasUserStencilSettings, out)) { |
| 684 | return false; |
| 685 | } |
| 686 | out->addStencilClip(); |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | GrFixedClip fFixedClip; |
| 691 | |
| 692 | typedef GrClip INHERITED; |
| 693 | }; |
| 694 | |
| 695 | bool GrReducedClip::drawStencilClipMask(GrContext* context, |
| 696 | GrDrawContext* drawContext, |
| 697 | const SkIPoint& clipOrigin) const { |
| 698 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 699 | StencilClip stencilClip(fIBounds.makeOffset(-clipOrigin.x(), -clipOrigin.y())); |
| 700 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 701 | if (!fWindowRects.empty()) { |
| 702 | stencilClip.setWindowRectangles(fWindowRects, clipOrigin, |
| 703 | GrWindowRectsState::Mode::kExclusive); |
| 704 | } |
| 705 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 706 | bool initialState = InitialState::kAllIn == this->initialState(); |
| 707 | drawContext->drawContextPriv().clearStencilClip(stencilClip.fixedClip(), initialState); |
| 708 | |
| 709 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 710 | SkMatrix viewMatrix; |
| 711 | viewMatrix.setTranslate(SkIntToScalar(-clipOrigin.x()), SkIntToScalar(-clipOrigin.y())); |
| 712 | |
| 713 | // walk through each clip element and perform its set op |
| 714 | // with the existing clip. |
| 715 | for (ElementList::Iter iter(fElements); iter.get(); iter.next()) { |
| 716 | const Element* element = iter.get(); |
| 717 | bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled(); |
| 718 | |
| 719 | bool fillInverted = false; |
| 720 | |
| 721 | // This will be used to determine whether the clip shape can be rendered into the |
| 722 | // stencil with arbitrary stencil settings. |
| 723 | GrPathRenderer::StencilSupport stencilSupport; |
| 724 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame^] | 725 | SkRegion::Op op = (SkRegion::Op)element->getOp(); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 726 | |
| 727 | GrPathRenderer* pr = nullptr; |
| 728 | SkPath clipPath; |
| 729 | if (Element::kRect_Type == element->getType()) { |
| 730 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
| 731 | fillInverted = false; |
| 732 | } else { |
| 733 | element->asPath(&clipPath); |
| 734 | fillInverted = clipPath.isInverseFillType(); |
| 735 | if (fillInverted) { |
| 736 | clipPath.toggleInverseFillType(); |
| 737 | } |
| 738 | |
| 739 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| 740 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 741 | canDrawArgs.fShaderCaps = context->caps()->shaderCaps(); |
| 742 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 743 | canDrawArgs.fShape = &shape; |
| 744 | canDrawArgs.fAntiAlias = false; |
| 745 | canDrawArgs.fHasUserStencilSettings = false; |
| 746 | canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled(); |
| 747 | |
| 748 | GrDrawingManager* dm = context->contextPriv().drawingManager(); |
| 749 | pr = dm->getPathRenderer(canDrawArgs, false, |
| 750 | GrPathRendererChain::kStencilOnly_DrawType, |
| 751 | &stencilSupport); |
| 752 | if (!pr) { |
| 753 | return false; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | bool canRenderDirectToStencil = |
| 758 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
| 759 | bool drawDirectToClip; // Given the renderer, the element, |
| 760 | // fill rule, and set operation should |
| 761 | // we render the element directly to |
| 762 | // stencil bit used for clipping. |
| 763 | GrUserStencilSettings const* const* stencilPasses = |
| 764 | GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted, |
| 765 | &drawDirectToClip); |
| 766 | |
| 767 | // draw the element to the client stencil bits if necessary |
| 768 | if (!drawDirectToClip) { |
| 769 | static constexpr GrUserStencilSettings kDrawToStencil( |
| 770 | GrUserStencilSettings::StaticInit< |
| 771 | 0x0000, |
| 772 | GrUserStencilTest::kAlways, |
| 773 | 0xffff, |
| 774 | GrUserStencilOp::kIncMaybeClamp, |
| 775 | GrUserStencilOp::kIncMaybeClamp, |
| 776 | 0xffff>() |
| 777 | ); |
| 778 | if (Element::kRect_Type == element->getType()) { |
| 779 | drawContext->drawContextPriv().stencilRect(stencilClip.fixedClip(), |
| 780 | &kDrawToStencil, useHWAA, |
| 781 | viewMatrix, element->getRect()); |
| 782 | } else { |
| 783 | if (!clipPath.isEmpty()) { |
| 784 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| 785 | if (canRenderDirectToStencil) { |
| 786 | GrPaint paint; |
| 787 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
| 788 | paint.setAntiAlias(element->isAA()); |
| 789 | |
| 790 | GrPathRenderer::DrawPathArgs args; |
| 791 | args.fResourceProvider = context->resourceProvider(); |
| 792 | args.fPaint = &paint; |
| 793 | args.fUserStencilSettings = &kDrawToStencil; |
| 794 | args.fDrawContext = drawContext; |
| 795 | args.fClip = &stencilClip.fixedClip(); |
| 796 | args.fViewMatrix = &viewMatrix; |
| 797 | args.fShape = &shape; |
| 798 | args.fAntiAlias = false; |
| 799 | args.fGammaCorrect = false; |
| 800 | pr->drawPath(args); |
| 801 | } else { |
| 802 | GrPathRenderer::StencilPathArgs args; |
| 803 | args.fResourceProvider = context->resourceProvider(); |
| 804 | args.fDrawContext = drawContext; |
| 805 | args.fClip = &stencilClip.fixedClip(); |
| 806 | args.fViewMatrix = &viewMatrix; |
| 807 | args.fIsAA = element->isAA(); |
| 808 | args.fShape = &shape; |
| 809 | pr->stencilPath(args); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // now we modify the clip bit by rendering either the clip |
| 816 | // element directly or a bounding rect of the entire clip. |
| 817 | for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) { |
| 818 | if (drawDirectToClip) { |
| 819 | if (Element::kRect_Type == element->getType()) { |
| 820 | drawContext->drawContextPriv().stencilRect(stencilClip, *pass, useHWAA, |
| 821 | viewMatrix, element->getRect()); |
| 822 | } else { |
| 823 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| 824 | GrPaint paint; |
| 825 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
| 826 | paint.setAntiAlias(element->isAA()); |
| 827 | GrPathRenderer::DrawPathArgs args; |
| 828 | args.fResourceProvider = context->resourceProvider(); |
| 829 | args.fPaint = &paint; |
| 830 | args.fUserStencilSettings = *pass; |
| 831 | args.fDrawContext = drawContext; |
| 832 | args.fClip = &stencilClip; |
| 833 | args.fViewMatrix = &viewMatrix; |
| 834 | args.fShape = &shape; |
| 835 | args.fAntiAlias = false; |
| 836 | args.fGammaCorrect = false; |
| 837 | pr->drawPath(args); |
| 838 | } |
| 839 | } else { |
| 840 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 841 | // draw rect in clip space. |
| 842 | drawContext->drawContextPriv().stencilRect(stencilClip, *pass, |
| 843 | false, viewMatrix, |
| 844 | SkRect::Make(fIBounds)); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | return true; |
| 849 | } |