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