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