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