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