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