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