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. |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 78 | fElements.addToHead(stackBounds, kReplace_SkClipOp, 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()) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 150 | case kDifference_SkClipOp: |
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; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 176 | case kIntersect_SkClipOp: |
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; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 209 | case kUnion_SkClipOp: |
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; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 232 | case kXOR_SkClipOp: |
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; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 254 | case kReverseDifference_SkClipOp: |
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 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 279 | case kReplace_SkClipOp: |
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) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 329 | SkASSERT(kXOR_SkClipOp == element->getOp() || |
| 330 | kReverseDifference_SkClipOp == element->getOp()); |
| 331 | fElements.addToHead(SkRect::Make(fIBounds), kReverseDifference_SkClipOp, 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. |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 340 | bool isReplace = kReplace_SkClipOp == newElement->getOp(); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 341 | if (newElement->isInverseFilled() && |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 342 | (kIntersect_SkClipOp == newElement->getOp() || isReplace)) { |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 343 | newElement->invertShapeFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 344 | newElement->setOp(kDifference_SkClipOp); |
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()) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 363 | case kDifference_SkClipOp: |
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; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 367 | case kIntersect_SkClipOp: |
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; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 374 | element->setOp(kReplace_SkClipOp); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 375 | } |
| 376 | break; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 377 | case kUnion_SkClipOp: |
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. |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 383 | element->setOp(kReplace_SkClipOp); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 384 | } |
| 385 | break; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 386 | case kXOR_SkClipOp: |
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. |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 389 | element->setOp(kReplace_SkClipOp); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 390 | } |
| 391 | break; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 392 | case kReverseDifference_SkClipOp: |
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 { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 405 | element->setOp(kReplace_SkClipOp); |
csmartdalton | 5ecbbbe | 2016-08-23 13:26:40 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | break; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 409 | case kReplace_SkClipOp: |
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 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 434 | static bool element_is_pure_subtract(SkClipOp op) { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 435 | SkASSERT(op >= 0); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 436 | return op <= kIntersect_SkClipOp; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 437 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 438 | GR_STATIC_ASSERT(0 == kDifference_SkClipOp); |
| 439 | GR_STATIC_ASSERT(1 == kIntersect_SkClipOp); |
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(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 449 | if (kDifference_SkClipOp != 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) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 532 | GrAA aa = GrBoolToAA(element->isAA()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 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: |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 538 | return rtc->priv().drawAndStencilRect(clip, ss, |
| 539 | (SkRegion::Op)element->getOp(), |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 540 | element->isInverseFilled(), aa, viewMatrix, |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 541 | element->getRect()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 542 | break; |
| 543 | default: { |
| 544 | SkPath path; |
| 545 | element->asPath(&path); |
| 546 | if (path.isInverseFillType()) { |
| 547 | path.toggleInverseFillType(); |
| 548 | } |
| 549 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 550 | return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(), |
| 551 | element->isInverseFilled(), aa, viewMatrix, path); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | return false; |
| 557 | } |
| 558 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 559 | static void draw_element(GrRenderTargetContext* rtc, |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 560 | const GrClip& clip, // TODO: can this just always be WideOpen? |
| 561 | const GrPaint &paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 562 | GrAA aa, |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 563 | const SkMatrix& viewMatrix, |
| 564 | const SkClipStack::Element* element) { |
| 565 | |
| 566 | // TODO: Draw rrects directly here. |
| 567 | switch (element->getType()) { |
| 568 | case Element::kEmpty_Type: |
| 569 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 570 | break; |
| 571 | case Element::kRect_Type: |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 572 | rtc->drawRect(clip, paint, aa, viewMatrix, element->getRect()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 573 | break; |
| 574 | default: { |
| 575 | SkPath path; |
| 576 | element->asPath(&path); |
| 577 | if (path.isInverseFillType()) { |
| 578 | path.toggleInverseFillType(); |
| 579 | } |
| 580 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 581 | rtc->drawPath(clip, paint, aa, viewMatrix, path, GrStyle::SimpleFill()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 587 | bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 588 | // The texture may be larger than necessary, this rect represents the part of the texture |
| 589 | // we populate with a rasterization of the clip. |
| 590 | GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height())); |
| 591 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 592 | if (!fWindowRects.empty()) { |
| 593 | clip.setWindowRectangles(fWindowRects, {fIBounds.left(), fIBounds.top()}, |
| 594 | GrWindowRectsState::Mode::kExclusive); |
| 595 | } |
| 596 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 597 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 598 | // clear the part that we care about. |
| 599 | GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0; |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 600 | rtc->priv().clear(clip, initialCoverage, true); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 601 | |
| 602 | // Set the matrix so that rendered clip elements are transformed to mask space from clip space. |
| 603 | SkMatrix translate; |
| 604 | translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top())); |
| 605 | |
| 606 | // walk through each clip element and perform its set op |
| 607 | for (ElementList::Iter iter(fElements); iter.get(); iter.next()) { |
| 608 | const Element* element = iter.get(); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 609 | SkRegion::Op op = (SkRegion::Op)element->getOp(); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 610 | GrAA aa = GrBoolToAA(element->isAA()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 611 | bool invert = element->isInverseFilled(); |
| 612 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 613 | // draw directly into the result with the stencil set to make the pixels affected |
| 614 | // by the clip shape be non-zero. |
| 615 | static constexpr GrUserStencilSettings kStencilInElement( |
| 616 | GrUserStencilSettings::StaticInit< |
| 617 | 0xffff, |
| 618 | GrUserStencilTest::kAlways, |
| 619 | 0xffff, |
| 620 | GrUserStencilOp::kReplace, |
| 621 | GrUserStencilOp::kReplace, |
| 622 | 0xffff>() |
| 623 | ); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 624 | if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 625 | return false; |
| 626 | } |
| 627 | |
| 628 | // Draw to the exterior pixels (those with a zero stencil value). |
| 629 | static constexpr GrUserStencilSettings kDrawOutsideElement( |
| 630 | GrUserStencilSettings::StaticInit< |
| 631 | 0x0000, |
| 632 | GrUserStencilTest::kEqual, |
| 633 | 0xffff, |
| 634 | GrUserStencilOp::kZero, |
| 635 | GrUserStencilOp::kZero, |
| 636 | 0xffff>() |
| 637 | ); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 638 | if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo, |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 639 | translate, SkRect::Make(fIBounds))) { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 640 | return false; |
| 641 | } |
| 642 | } else { |
| 643 | // all the remaining ops can just be directly draw into the accumulation buffer |
| 644 | GrPaint paint; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 645 | paint.setCoverageSetOpXPFactory(op, false); |
| 646 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 647 | draw_element(rtc, clip, paint, aa, translate, element); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | return true; |
| 652 | } |
| 653 | |
| 654 | //////////////////////////////////////////////////////////////////////////////// |
| 655 | // Create a 1-bit clip mask in the stencil buffer. |
| 656 | |
| 657 | class StencilClip final : public GrClip { |
| 658 | public: |
| 659 | StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {} |
| 660 | const GrFixedClip& fixedClip() const { return fFixedClip; } |
| 661 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 662 | void setWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin, |
| 663 | GrWindowRectsState::Mode mode) { |
| 664 | fFixedClip.setWindowRectangles(windows, origin, mode); |
| 665 | } |
| 666 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 667 | private: |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 668 | bool quickContains(const SkRect&) const override { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 669 | return false; |
| 670 | } |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 671 | void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override { |
| 672 | fFixedClip.getConservativeBounds(width, height, bounds, iior); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 673 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 674 | bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const override { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 675 | return false; |
| 676 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 677 | bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA, |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 678 | bool hasUserStencilSettings, GrAppliedClip* out) const override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 679 | if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out)) { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 680 | return false; |
| 681 | } |
| 682 | out->addStencilClip(); |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | GrFixedClip fFixedClip; |
| 687 | |
| 688 | typedef GrClip INHERITED; |
| 689 | }; |
| 690 | |
| 691 | bool GrReducedClip::drawStencilClipMask(GrContext* context, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 692 | GrRenderTargetContext* renderTargetContext, |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 693 | const SkIPoint& clipOrigin) const { |
| 694 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 695 | StencilClip stencilClip(fIBounds.makeOffset(-clipOrigin.x(), -clipOrigin.y())); |
| 696 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 697 | if (!fWindowRects.empty()) { |
| 698 | stencilClip.setWindowRectangles(fWindowRects, clipOrigin, |
| 699 | GrWindowRectsState::Mode::kExclusive); |
| 700 | } |
| 701 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 702 | bool initialState = InitialState::kAllIn == this->initialState(); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 703 | renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 704 | |
| 705 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 706 | SkMatrix viewMatrix; |
| 707 | viewMatrix.setTranslate(SkIntToScalar(-clipOrigin.x()), SkIntToScalar(-clipOrigin.y())); |
| 708 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 709 | // walk through each clip element and perform its set op with the existing clip. |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 710 | for (ElementList::Iter iter(fElements); iter.get(); iter.next()) { |
| 711 | const Element* element = iter.get(); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 712 | GrAAType aaType = GrAAType::kNone; |
| 713 | if (element->isAA() && renderTargetContext->isStencilBufferMultisampled()) { |
| 714 | aaType = GrAAType::kMSAA; |
| 715 | } |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 716 | |
| 717 | bool fillInverted = false; |
| 718 | |
| 719 | // This will be used to determine whether the clip shape can be rendered into the |
| 720 | // stencil with arbitrary stencil settings. |
| 721 | GrPathRenderer::StencilSupport stencilSupport; |
| 722 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 723 | SkRegion::Op op = (SkRegion::Op)element->getOp(); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 724 | |
| 725 | GrPathRenderer* pr = nullptr; |
| 726 | SkPath clipPath; |
| 727 | if (Element::kRect_Type == element->getType()) { |
| 728 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
| 729 | fillInverted = false; |
| 730 | } else { |
| 731 | element->asPath(&clipPath); |
| 732 | fillInverted = clipPath.isInverseFillType(); |
| 733 | if (fillInverted) { |
| 734 | clipPath.toggleInverseFillType(); |
| 735 | } |
| 736 | |
| 737 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| 738 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 739 | canDrawArgs.fShaderCaps = context->caps()->shaderCaps(); |
| 740 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 741 | canDrawArgs.fShape = &shape; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 742 | canDrawArgs.fAAType = aaType; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 743 | canDrawArgs.fHasUserStencilSettings = false; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 744 | |
| 745 | GrDrawingManager* dm = context->contextPriv().drawingManager(); |
| 746 | pr = dm->getPathRenderer(canDrawArgs, false, |
| 747 | GrPathRendererChain::kStencilOnly_DrawType, |
| 748 | &stencilSupport); |
| 749 | if (!pr) { |
| 750 | return false; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | bool canRenderDirectToStencil = |
| 755 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
| 756 | bool drawDirectToClip; // Given the renderer, the element, |
| 757 | // fill rule, and set operation should |
| 758 | // we render the element directly to |
| 759 | // stencil bit used for clipping. |
| 760 | GrUserStencilSettings const* const* stencilPasses = |
| 761 | GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted, |
| 762 | &drawDirectToClip); |
| 763 | |
| 764 | // draw the element to the client stencil bits if necessary |
| 765 | if (!drawDirectToClip) { |
| 766 | static constexpr GrUserStencilSettings kDrawToStencil( |
| 767 | GrUserStencilSettings::StaticInit< |
| 768 | 0x0000, |
| 769 | GrUserStencilTest::kAlways, |
| 770 | 0xffff, |
| 771 | GrUserStencilOp::kIncMaybeClamp, |
| 772 | GrUserStencilOp::kIncMaybeClamp, |
| 773 | 0xffff>() |
| 774 | ); |
| 775 | if (Element::kRect_Type == element->getType()) { |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 776 | renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 777 | aaType, viewMatrix, element->getRect()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 778 | } else { |
| 779 | if (!clipPath.isEmpty()) { |
| 780 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| 781 | if (canRenderDirectToStencil) { |
| 782 | GrPaint paint; |
| 783 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 784 | |
| 785 | GrPathRenderer::DrawPathArgs args; |
| 786 | args.fResourceProvider = context->resourceProvider(); |
| 787 | args.fPaint = &paint; |
| 788 | args.fUserStencilSettings = &kDrawToStencil; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 789 | args.fRenderTargetContext = renderTargetContext; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 790 | args.fClip = &stencilClip.fixedClip(); |
| 791 | args.fViewMatrix = &viewMatrix; |
| 792 | args.fShape = &shape; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 793 | args.fAAType = aaType; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 794 | args.fGammaCorrect = false; |
| 795 | pr->drawPath(args); |
| 796 | } else { |
| 797 | GrPathRenderer::StencilPathArgs args; |
| 798 | args.fResourceProvider = context->resourceProvider(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 799 | args.fRenderTargetContext = renderTargetContext; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 800 | args.fClip = &stencilClip.fixedClip(); |
| 801 | args.fViewMatrix = &viewMatrix; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 802 | args.fAAType = aaType; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 803 | args.fShape = &shape; |
| 804 | pr->stencilPath(args); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | // now we modify the clip bit by rendering either the clip |
| 811 | // element directly or a bounding rect of the entire clip. |
| 812 | for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) { |
| 813 | if (drawDirectToClip) { |
| 814 | if (Element::kRect_Type == element->getType()) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 815 | renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, viewMatrix, |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 816 | element->getRect()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 817 | } else { |
| 818 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| 819 | GrPaint paint; |
| 820 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 821 | GrPathRenderer::DrawPathArgs args; |
| 822 | args.fResourceProvider = context->resourceProvider(); |
| 823 | args.fPaint = &paint; |
| 824 | args.fUserStencilSettings = *pass; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 825 | args.fRenderTargetContext = renderTargetContext; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 826 | args.fClip = &stencilClip; |
| 827 | args.fViewMatrix = &viewMatrix; |
| 828 | args.fShape = &shape; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 829 | args.fAAType = aaType; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 830 | args.fGammaCorrect = false; |
| 831 | pr->drawPath(args); |
| 832 | } |
| 833 | } else { |
| 834 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 835 | // draw rect in clip space. |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame^] | 836 | renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, viewMatrix, |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 837 | SkRect::Make(fIBounds)); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } |
| 841 | return true; |
| 842 | } |