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