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