blob: 992411a4a22d84f917bc8b998793c9580f5915de [file] [log] [blame]
bsalomon@google.com170bd792012-12-05 22:26:11 +00001/*
csmartdalton77f2fae2016-08-08 09:55:06 -07002 * Copyright 2016 Google Inc.
bsalomon@google.com170bd792012-12-05 22:26:11 +00003 *
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
csmartdaltonbde96c62016-08-31 12:54:46 -070010#include "GrAppliedClip.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070011#include "GrClip.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070012#include "GrColor.h"
13#include "GrContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContext.h"
15#include "GrRenderTargetContextPriv.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070016#include "GrDrawingManager.h"
17#include "GrFixedClip.h"
18#include "GrPathRenderer.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070019#include "GrStencilSettings.h"
Chris Daltonbbfd5162017-11-07 13:35:22 -070020#include "GrStencilClip.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070021#include "GrStyle.h"
22#include "GrUserStencilSettings.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050023#include "SkClipOpPriv.h"
Chris Dalton4355b262017-11-15 13:14:01 -070024#include "effects/GrConvexPolyEffect.h"
25#include "effects/GrRRectEffect.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070026
csmartdalton5ecbbbe2016-08-23 13:26:40 -070027/**
28 * There are plenty of optimizations that could be added here. Maybe flips could be folded into
29 * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
30 * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
31 * based on later intersect operations, and perhaps remove intersect-rects. We could optionally
32 * take a rect in case the caller knows a bound on what is to be drawn through this clip.
33 */
csmartdaltonbf4a8f92016-09-06 10:01:06 -070034GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
Chris Dalton4355b262017-11-15 13:14:01 -070035 int maxWindowRectangles, int maxAnalyticFPs)
36 : fMaxWindowRectangles(maxWindowRectangles)
37 , fMaxAnalyticFPs(maxAnalyticFPs) {
csmartdaltoncbecb082016-07-22 08:59:08 -070038 SkASSERT(!queryBounds.isEmpty());
Chris Dalton4355b262017-11-15 13:14:01 -070039 SkASSERT(fMaxWindowRectangles <= GrWindowRectangles::kMaxWindows);
Chris Dalton79471932017-10-27 01:50:57 -060040 fHasScissor = false;
41 fAAClipRectGenID = SK_InvalidGenID;
csmartdaltoncbecb082016-07-22 08:59:08 -070042
bsalomon@google.com170bd792012-12-05 22:26:11 +000043 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070044 fInitialState = InitialState::kAllIn;
45 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000046 }
47
48 SkClipStack::BoundsType stackBoundsType;
49 SkRect stackBounds;
50 bool iior;
51 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
52
Chris Dalton348060f2017-06-05 13:15:37 -060053 if (GrClip::IsOutsideClip(stackBounds, queryBounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -070054 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070055 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
56 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000057 }
58
csmartdaltoncbecb082016-07-22 08:59:08 -070059 if (iior) {
60 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
61 // This should only be true if aa/non-aa status matches among all elements.
62 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
63 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
64 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
Chris Dalton79471932017-10-27 01:50:57 -060065 // The clip is a non-aa rect. Here we just implement the entire thing using fScissor.
Mike Kleine26062a2017-10-31 20:56:54 +000066 stackBounds.round(&fScissor);
Chris Dalton79471932017-10-27 01:50:57 -060067 fHasScissor = true;
68 fInitialState = fScissor.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
csmartdalton77f2fae2016-08-08 09:55:06 -070069 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070070 }
71 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070072 fInitialState = InitialState::kAllIn;
73 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070074 }
75
csmartdaltond211e782016-08-15 11:17:19 -070076 SkRect tightBounds;
77 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
Chris Dalton79471932017-10-27 01:50:57 -060078 fScissor = GrClip::GetPixelIBounds(tightBounds);
79 if (fScissor.isEmpty()) {
Chris Dalton348060f2017-06-05 13:15:37 -060080 fInitialState = InitialState::kAllOut;
81 return;
82 }
Chris Dalton79471932017-10-27 01:50:57 -060083 fHasScissor = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070084
Chris Dalton79471932017-10-27 01:50:57 -060085 fAAClipRect = stackBounds;
86 fAAClipRectGenID = stack.getTopmostGenID();
87 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
csmartdalton77f2fae2016-08-08 09:55:06 -070088
Chris Dalton79471932017-10-27 01:50:57 -060089 fInitialState = InitialState::kAllIn;
90 } else {
91 SkRect tighterQuery = queryBounds;
92 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
93 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This
94 // new clip will be enforced by the scissor.)
95 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
96 }
97
98 fScissor = GrClip::GetPixelIBounds(tighterQuery);
99 if (fScissor.isEmpty()) {
100 fInitialState = InitialState::kAllOut;
101 return;
102 }
103 fHasScissor = true;
104
Chris Dalton4355b262017-11-15 13:14:01 -0700105 // Now that we have determined the bounds to use and filtered out the trivial cases, call
106 // the helper that actually walks the stack.
107 this->walkStack(stack, tighterQuery);
csmartdaltoncbecb082016-07-22 08:59:08 -0700108 }
109
Chris Dalton4355b262017-11-15 13:14:01 -0700110 if (SK_InvalidGenID != fAAClipRectGenID && // Is there an AA clip rect?
111 ClipResult::kNotClipped == this->addAnalyticFP(fAAClipRect, Invert::kNo, true)) {
Chris Dalton79471932017-10-27 01:50:57 -0600112 if (fMaskElements.isEmpty()) {
113 // Use a replace since it is faster than intersect.
114 fMaskElements.addToHead(fAAClipRect, SkMatrix::I(), kReplace_SkClipOp, true /*doAA*/);
115 fInitialState = InitialState::kAllOut;
116 } else {
117 fMaskElements.addToTail(fAAClipRect, SkMatrix::I(), kIntersect_SkClipOp, true /*doAA*/);
118 }
119 fMaskRequiresAA = true;
120 fMaskGenID = fAAClipRectGenID;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700121 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700122}
123
Chris Dalton4355b262017-11-15 13:14:01 -0700124void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700125 // walk backwards until we get to:
126 // a) the beginning
127 // b) an operation that is known to make the bounds all inside/outside
128 // c) a replace operation
129
130 enum class InitialTriState {
131 kUnknown = -1,
132 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
133 kAllOut = (int)GrReducedClip::InitialState::kAllOut
134 } initialTriState = InitialTriState::kUnknown;
135
136 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
137 // TODO: track these per saved clip so that we can consider them on the forward pass.
138 bool embiggens = false;
139 bool emsmallens = false;
140
141 // We use a slightly relaxed set of query bounds for element containment tests. This is to
142 // account for floating point rounding error that may have occurred during coord transforms.
143 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
144 GrClip::kBoundsTolerance);
Chris Dalton69824002017-10-31 00:37:52 -0600145 if (relaxedQueryBounds.isEmpty()) {
146 relaxedQueryBounds = queryBounds;
147 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700148
149 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
150 int numAAElements = 0;
151 while (InitialTriState::kUnknown == initialTriState) {
152 const Element* element = iter.prev();
153 if (nullptr == element) {
154 initialTriState = InitialTriState::kAllIn;
155 break;
156 }
157 if (SkClipStack::kEmptyGenID == element->getGenID()) {
158 initialTriState = InitialTriState::kAllOut;
159 break;
160 }
161 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
162 initialTriState = InitialTriState::kAllIn;
163 break;
164 }
165
166 bool skippable = false;
167 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
168
169 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500170 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700171 // check if the shape subtracted either contains the entire bounds (and makes
172 // the clip empty) or is outside the bounds and therefore can be skipped.
173 if (element->isInverseFilled()) {
174 if (element->contains(relaxedQueryBounds)) {
175 skippable = true;
176 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
177 initialTriState = InitialTriState::kAllOut;
178 skippable = true;
179 }
180 } else {
181 if (element->contains(relaxedQueryBounds)) {
182 initialTriState = InitialTriState::kAllOut;
183 skippable = true;
184 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
185 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600186 } else if (!embiggens) {
Chris Dalton4355b262017-11-15 13:14:01 -0700187 ClipResult result = this->clipOutsideElement(element);
Chris Dalton79471932017-10-27 01:50:57 -0600188 if (ClipResult::kMadeEmpty == result) {
189 return;
190 }
191 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700192 }
193 }
194 if (!skippable) {
195 emsmallens = true;
196 }
197 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500198 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700199 // check if the shape intersected contains the entire bounds and therefore can
200 // be skipped or it is outside the entire bounds and therefore makes the clip
201 // empty.
202 if (element->isInverseFilled()) {
203 if (element->contains(relaxedQueryBounds)) {
204 initialTriState = InitialTriState::kAllOut;
205 skippable = true;
206 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
207 skippable = true;
208 }
209 } else {
210 if (element->contains(relaxedQueryBounds)) {
211 skippable = true;
212 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
213 initialTriState = InitialTriState::kAllOut;
214 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600215 } else if (!embiggens) {
216 ClipResult result = this->clipInsideElement(element);
217 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700218 return;
219 }
Chris Dalton79471932017-10-27 01:50:57 -0600220 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700221 }
222 }
223 if (!skippable) {
224 emsmallens = true;
225 }
226 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500227 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700228 // If the union-ed shape contains the entire bounds then after this element
229 // the bounds is entirely inside the clip. If the union-ed shape is outside the
230 // bounds then this op can be skipped.
231 if (element->isInverseFilled()) {
232 if (element->contains(relaxedQueryBounds)) {
233 skippable = true;
234 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
235 initialTriState = InitialTriState::kAllIn;
236 skippable = true;
237 }
238 } else {
239 if (element->contains(relaxedQueryBounds)) {
240 initialTriState = InitialTriState::kAllIn;
241 skippable = true;
242 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
243 skippable = true;
244 }
245 }
246 if (!skippable) {
247 embiggens = true;
248 }
249 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500250 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700251 // If the bounds is entirely inside the shape being xor-ed then the effect is
252 // to flip the inside/outside state of every point in the bounds. We may be
253 // able to take advantage of this in the forward pass. If the xor-ed shape
254 // doesn't intersect the bounds then it can be skipped.
255 if (element->isInverseFilled()) {
256 if (element->contains(relaxedQueryBounds)) {
257 skippable = true;
258 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
259 isFlip = true;
260 }
261 } else {
262 if (element->contains(relaxedQueryBounds)) {
263 isFlip = true;
264 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
265 skippable = true;
266 }
267 }
268 if (!skippable) {
269 emsmallens = embiggens = true;
270 }
271 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500272 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700273 // When the bounds is entirely within the rev-diff shape then this behaves like xor
274 // and reverses every point inside the bounds. If the shape is completely outside
275 // the bounds then we know after this element is applied that the bounds will be
276 // all outside the current clip.B
277 if (element->isInverseFilled()) {
278 if (element->contains(relaxedQueryBounds)) {
279 initialTriState = InitialTriState::kAllOut;
280 skippable = true;
281 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
282 isFlip = true;
283 }
284 } else {
285 if (element->contains(relaxedQueryBounds)) {
286 isFlip = true;
287 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
288 initialTriState = InitialTriState::kAllOut;
289 skippable = true;
290 }
291 }
292 if (!skippable) {
293 emsmallens = embiggens = true;
294 }
295 break;
296
Mike Reedc1f77742016-12-09 09:00:50 -0500297 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700298 // Replace will always terminate our walk. We will either begin the forward walk
299 // at the replace op or detect here than the shape is either completely inside
300 // or completely outside the bounds. In this latter case it can be skipped by
301 // setting the correct value for initialTriState.
302 if (element->isInverseFilled()) {
303 if (element->contains(relaxedQueryBounds)) {
304 initialTriState = InitialTriState::kAllOut;
305 skippable = true;
306 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
307 initialTriState = InitialTriState::kAllIn;
308 skippable = true;
309 }
310 } else {
311 if (element->contains(relaxedQueryBounds)) {
312 initialTriState = InitialTriState::kAllIn;
313 skippable = true;
314 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
315 initialTriState = InitialTriState::kAllOut;
316 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600317 } else if (!embiggens) {
318 ClipResult result = this->clipInsideElement(element);
319 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700320 return;
321 }
Chris Dalton79471932017-10-27 01:50:57 -0600322 if (ClipResult::kClipped == result) {
323 initialTriState = InitialTriState::kAllIn;
324 skippable = true;
325 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700326 }
327 }
328 if (!skippable) {
329 initialTriState = InitialTriState::kAllOut;
330 embiggens = emsmallens = true;
331 }
332 break;
333 default:
334 SkDEBUGFAIL("Unexpected op.");
335 break;
336 }
337 if (!skippable) {
Chris Dalton79471932017-10-27 01:50:57 -0600338 if (fMaskElements.isEmpty()) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700339 // This will be the last element. Record the stricter genID.
Chris Dalton79471932017-10-27 01:50:57 -0600340 fMaskGenID = element->getGenID();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700341 }
342
343 // if it is a flip, change it to a bounds-filling rect
344 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500345 SkASSERT(kXOR_SkClipOp == element->getOp() ||
346 kReverseDifference_SkClipOp == element->getOp());
Chris Dalton79471932017-10-27 01:50:57 -0600347 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
348 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700349 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600350 Element* newElement = fMaskElements.addToHead(*element);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700351 if (newElement->isAA()) {
352 ++numAAElements;
353 }
354 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
355 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
356 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500357 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700358 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500359 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700360 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500361 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700362 if (isReplace) {
363 SkASSERT(InitialTriState::kAllOut == initialTriState);
364 initialTriState = InitialTriState::kAllIn;
365 }
366 }
367 }
368 }
369 }
370
371 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
372 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
Chris Dalton79471932017-10-27 01:50:57 -0600373 fMaskElements.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700374 numAAElements = 0;
375 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600376 Element* element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700377 while (element) {
378 bool skippable = false;
379 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500380 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700381 // subtracting from the empty set yields the empty set.
382 skippable = InitialTriState::kAllOut == initialTriState;
383 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500384 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700385 // intersecting with the empty set yields the empty set
386 if (InitialTriState::kAllOut == initialTriState) {
387 skippable = true;
388 } else {
389 // We can clear to zero and then simply draw the clip element.
390 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500391 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700392 }
393 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500394 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700395 if (InitialTriState::kAllIn == initialTriState) {
396 // unioning the infinite plane with anything is a no-op.
397 skippable = true;
398 } else {
399 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500400 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700401 }
402 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500403 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700404 if (InitialTriState::kAllOut == initialTriState) {
405 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500406 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700407 }
408 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500409 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700410 if (InitialTriState::kAllIn == initialTriState) {
411 // subtracting the whole plane will yield the empty set.
412 skippable = true;
413 initialTriState = InitialTriState::kAllOut;
414 } else {
415 // this picks up flips inserted in the backwards pass.
416 skippable = element->isInverseFilled() ?
417 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
418 element->contains(relaxedQueryBounds);
419 if (skippable) {
420 initialTriState = InitialTriState::kAllIn;
421 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500422 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700423 }
424 }
425 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500426 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700427 skippable = false; // we would have skipped it in the backwards walk if we
428 // could've.
429 break;
430 default:
431 SkDEBUGFAIL("Unexpected op.");
432 break;
433 }
434 if (!skippable) {
435 break;
436 } else {
437 if (element->isAA()) {
438 --numAAElements;
439 }
Chris Dalton79471932017-10-27 01:50:57 -0600440 fMaskElements.popHead();
441 element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700442 }
443 }
444 }
Chris Dalton79471932017-10-27 01:50:57 -0600445 fMaskRequiresAA = numAAElements > 0;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700446
447 SkASSERT(InitialTriState::kUnknown != initialTriState);
448 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
449}
450
Chris Dalton79471932017-10-27 01:50:57 -0600451GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
452 SkIRect elementIBounds;
453 if (!element->isAA()) {
454 element->getBounds().round(&elementIBounds);
455 } else {
456 elementIBounds = GrClip::GetPixelIBounds(element->getBounds());
457 }
458 SkASSERT(fHasScissor);
459 if (!fScissor.intersect(elementIBounds)) {
460 this->makeEmpty();
461 return ClipResult::kMadeEmpty;
462 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700463
Chris Dalton79471932017-10-27 01:50:57 -0600464 switch (element->getDeviceSpaceType()) {
465 case Element::DeviceSpaceType::kEmpty:
466 return ClipResult::kMadeEmpty;
467
468 case Element::DeviceSpaceType::kRect:
469 SkASSERT(element->getBounds() == element->getDeviceSpaceRect());
470 if (element->isAA()) {
471 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
472 fAAClipRect = element->getDeviceSpaceRect();
473 // fAAClipRectGenID is the value we should use for fMaskGenID if we end up
474 // moving the AA clip rect into the mask. The mask GenID is simply the topmost
475 // element's GenID. And since we walk the stack backwards, this means it's just
476 // the first element we don't skip during our walk.
477 fAAClipRectGenID = fMaskElements.isEmpty() ? element->getGenID() : fMaskGenID;
478 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
479 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
480 this->makeEmpty();
481 return ClipResult::kMadeEmpty;
482 }
483 }
484 return ClipResult::kClipped;
485
486 case Element::DeviceSpaceType::kRRect:
Chris Dalton4355b262017-11-15 13:14:01 -0700487 return this->addAnalyticFP(element->getDeviceSpaceRRect(), Invert::kNo,
488 element->isAA());
489
Chris Dalton79471932017-10-27 01:50:57 -0600490 case Element::DeviceSpaceType::kPath:
Chris Dalton4355b262017-11-15 13:14:01 -0700491 return this->addAnalyticFP(element->getDeviceSpacePath(), Invert::kNo, element->isAA());
Chris Dalton79471932017-10-27 01:50:57 -0600492 }
493
494 SK_ABORT("Unexpected DeviceSpaceType");
495 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700496}
497
Chris Dalton4355b262017-11-15 13:14:01 -0700498GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600499 switch (element->getDeviceSpaceType()) {
500 case Element::DeviceSpaceType::kEmpty:
501 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700502
Chris Dalton79471932017-10-27 01:50:57 -0600503 case Element::DeviceSpaceType::kRect:
Chris Dalton4355b262017-11-15 13:14:01 -0700504 if (fWindowRects.count() < fMaxWindowRectangles) {
505 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones,
506 // but it saves processing time.
507 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
508 if (!element->isAA()) {
509 return ClipResult::kClipped;
510 }
511 }
512 return this->addAnalyticFP(element->getDeviceSpaceRect(), Invert::kYes,
513 element->isAA());
Chris Dalton79471932017-10-27 01:50:57 -0600514
515 case Element::DeviceSpaceType::kRRect: {
Chris Daltond8d15932017-11-01 19:21:24 +0000516 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
Chris Dalton4355b262017-11-15 13:14:01 -0700517 ClipResult clipResult = this->addAnalyticFP(clipRRect, Invert::kYes, element->isAA());
518 if (fWindowRects.count() >= fMaxWindowRectangles) {
519 return clipResult;
520 }
521
522 // Clip out the interiors of round rects with two window rectangles in the shape of a
523 // "plus". This doesn't let us skip the clip element, but still saves processing time.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700524 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
525 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
526 if (SkRRect::kComplex_Type == clipRRect.getType()) {
527 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
528 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
529 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
530 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
531 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
532 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
533 }
534 const SkRect& bounds = clipRRect.getBounds();
535 if (insetTL.x() + insetBR.x() >= bounds.width() ||
536 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Dalton4355b262017-11-15 13:14:01 -0700537 return clipResult; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700538 }
539
540 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
541 bounds.right(), bounds.bottom() - insetBR.y());
542 this->addWindowRectangle(horzRect, element->isAA());
Chris Dalton4355b262017-11-15 13:14:01 -0700543
544 if (fWindowRects.count() < fMaxWindowRectangles) {
545 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
546 bounds.right() - insetBR.x(), bounds.bottom());
547 this->addWindowRectangle(vertRect, element->isAA());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700548 }
549
Chris Dalton4355b262017-11-15 13:14:01 -0700550 return clipResult;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700551 }
Chris Dalton79471932017-10-27 01:50:57 -0600552
553 case Element::DeviceSpaceType::kPath:
Chris Dalton4355b262017-11-15 13:14:01 -0700554 return this->addAnalyticFP(element->getDeviceSpacePath(), Invert::kYes,
555 element->isAA());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700556 }
Chris Dalton79471932017-10-27 01:50:57 -0600557
558 SK_ABORT("Unexpected DeviceSpaceType");
559 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700560}
561
562inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
563 SkIRect window;
564 if (!elementIsAA) {
565 elementInteriorRect.round(&window);
566 } else {
567 elementInteriorRect.roundIn(&window);
568 }
569 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
570 fWindowRects.addWindow(window);
571 }
572}
573
Chris Dalton4355b262017-11-15 13:14:01 -0700574template<typename T>
575inline GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const T& deviceSpaceShape,
576 Invert invert, bool aa) {
577 if (fAnalyticFPs.count() >= fMaxAnalyticFPs) {
578 return ClipResult::kNotClipped;
579 }
580
581 GrClipEdgeType edgeType;
582 if (Invert::kNo == invert) {
583 edgeType = aa ? GrClipEdgeType::kFillAA : GrClipEdgeType::kFillBW;
584 } else {
585 edgeType = aa ? GrClipEdgeType::kInverseFillAA : GrClipEdgeType::kInverseFillBW;
586 }
587
588 if (auto fp = make_analytic_clip_fp(edgeType, deviceSpaceShape)) {
589 fAnalyticFPs.push_back(std::move(fp));
590 return ClipResult::kClipped;
591 }
592
593 return ClipResult::kNotClipped;
594}
595
596std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
597 const SkRect& deviceSpaceRect) {
598 return GrConvexPolyEffect::Make(edgeType, deviceSpaceRect);
599}
600
601std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
602 const SkRRect& deviceSpaceRRect) {
603 return GrRRectEffect::Make(edgeType, deviceSpaceRRect);
604}
605
606std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
607 const SkPath& deviceSpacePath) {
608 return GrConvexPolyEffect::Make(edgeType, deviceSpacePath);
609}
610
Chris Dalton79471932017-10-27 01:50:57 -0600611void GrReducedClip::makeEmpty() {
612 fHasScissor = false;
613 fAAClipRectGenID = SK_InvalidGenID;
614 fWindowRects.reset();
615 fMaskElements.reset();
616 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000617}
csmartdaltonbde96c62016-08-31 12:54:46 -0700618
619////////////////////////////////////////////////////////////////////////////////
620// Create a 8-bit clip mask in alpha
621
Brian Osman11052242016-10-27 14:47:55 -0400622static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700623 const GrFixedClip& clip,
624 const GrUserStencilSettings* ss,
625 const SkMatrix& viewMatrix,
626 const SkClipStack::Element* element) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500627 GrAA aa = GrBoolToAA(element->isAA());
Brian Salomonf3b46e52017-08-30 11:37:57 -0400628 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600629 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700630 SkDEBUGFAIL("Should never get here with an empty element.");
631 break;
Chris Dalton79471932017-10-27 01:50:57 -0600632 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400633 return rtc->priv().drawAndStencilRect(clip, ss, (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500634 element->isInverseFilled(), aa, viewMatrix,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400635 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700636 break;
637 default: {
638 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400639 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700640 if (path.isInverseFillType()) {
641 path.toggleInverseFillType();
642 }
643
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500644 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
645 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700646 break;
647 }
648 }
649
650 return false;
651}
652
Brian Osman11052242016-10-27 14:47:55 -0400653static void draw_element(GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500654 const GrClip& clip, // TODO: can this just always be WideOpen?
655 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500656 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700657 const SkMatrix& viewMatrix,
658 const SkClipStack::Element* element) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700659 // TODO: Draw rrects directly here.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400660 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600661 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700662 SkDEBUGFAIL("Should never get here with an empty element.");
663 break;
Chris Dalton79471932017-10-27 01:50:57 -0600664 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400665 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700666 break;
667 default: {
668 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400669 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700670 if (path.isInverseFillType()) {
671 path.toggleInverseFillType();
672 }
673
Brian Salomon82f44312017-01-11 13:42:54 -0500674 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700675 break;
676 }
677 }
678}
679
Brian Osman11052242016-10-27 14:47:55 -0400680bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700681 // The texture may be larger than necessary, this rect represents the part of the texture
682 // we populate with a rasterization of the clip.
Chris Dalton79471932017-10-27 01:50:57 -0600683 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700684
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700685 if (!fWindowRects.empty()) {
Chris Dalton79471932017-10-27 01:50:57 -0600686 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700687 GrWindowRectsState::Mode::kExclusive);
688 }
689
csmartdaltonbde96c62016-08-31 12:54:46 -0700690 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
691 // clear the part that we care about.
692 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400693 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700694
695 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
696 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600697 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700698
699 // walk through each clip element and perform its set op
Chris Dalton79471932017-10-27 01:50:57 -0600700 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700701 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700702 SkRegion::Op op = (SkRegion::Op)element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500703 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700704 bool invert = element->isInverseFilled();
705 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
706 // draw directly into the result with the stencil set to make the pixels affected
707 // by the clip shape be non-zero.
708 static constexpr GrUserStencilSettings kStencilInElement(
709 GrUserStencilSettings::StaticInit<
710 0xffff,
711 GrUserStencilTest::kAlways,
712 0xffff,
713 GrUserStencilOp::kReplace,
714 GrUserStencilOp::kReplace,
715 0xffff>()
716 );
Brian Osman11052242016-10-27 14:47:55 -0400717 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700718 return false;
719 }
720
721 // Draw to the exterior pixels (those with a zero stencil value).
722 static constexpr GrUserStencilSettings kDrawOutsideElement(
723 GrUserStencilSettings::StaticInit<
724 0x0000,
725 GrUserStencilTest::kEqual,
726 0xffff,
727 GrUserStencilOp::kZero,
728 GrUserStencilOp::kZero,
729 0xffff>()
730 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500731 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Chris Dalton79471932017-10-27 01:50:57 -0600732 translate, SkRect::Make(fScissor))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700733 return false;
734 }
735 } else {
736 // all the remaining ops can just be directly draw into the accumulation buffer
737 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700738 paint.setCoverageSetOpXPFactory(op, false);
739
Brian Salomon82f44312017-01-11 13:42:54 -0500740 draw_element(rtc, clip, std::move(paint), aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700741 }
742 }
743
744 return true;
745}
746
747////////////////////////////////////////////////////////////////////////////////
748// Create a 1-bit clip mask in the stencil buffer.
749
csmartdaltonbde96c62016-08-31 12:54:46 -0700750bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400751 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700752 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Chris Daltonbbfd5162017-11-07 13:35:22 -0700753 GrStencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700754
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700755 if (!fWindowRects.empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700756 stencilClip.fixedClip().setWindowRectangles(fWindowRects,
757 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700758 }
759
csmartdaltonbde96c62016-08-31 12:54:46 -0700760 bool initialState = InitialState::kAllIn == this->initialState();
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000761 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700762
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500763 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600764 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700765 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500766 GrAAType aaType = GrAAType::kNone;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400767 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500768 aaType = GrAAType::kMSAA;
769 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700770
771 bool fillInverted = false;
772
773 // This will be used to determine whether the clip shape can be rendered into the
774 // stencil with arbitrary stencil settings.
775 GrPathRenderer::StencilSupport stencilSupport;
776
reed73603f32016-09-20 08:42:38 -0700777 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700778
779 GrPathRenderer* pr = nullptr;
780 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400781 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700782 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
783 fillInverted = false;
784 } else {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400785 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700786 fillInverted = clipPath.isInverseFillType();
787 if (fillInverted) {
788 clipPath.toggleInverseFillType();
789 }
790
791 GrShape shape(clipPath, GrStyle::SimpleFill());
792 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700793 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600794 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400795 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700796 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500797 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700798 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700799
800 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500801 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700802 &stencilSupport);
803 if (!pr) {
804 return false;
805 }
806 }
807
808 bool canRenderDirectToStencil =
809 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
810 bool drawDirectToClip; // Given the renderer, the element,
811 // fill rule, and set operation should
812 // we render the element directly to
813 // stencil bit used for clipping.
814 GrUserStencilSettings const* const* stencilPasses =
815 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
816 &drawDirectToClip);
817
818 // draw the element to the client stencil bits if necessary
819 if (!drawDirectToClip) {
820 static constexpr GrUserStencilSettings kDrawToStencil(
821 GrUserStencilSettings::StaticInit<
822 0x0000,
823 GrUserStencilTest::kAlways,
824 0xffff,
825 GrUserStencilOp::kIncMaybeClamp,
826 GrUserStencilOp::kIncMaybeClamp,
827 0xffff>()
828 );
Brian Salomonf3b46e52017-08-30 11:37:57 -0400829 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400830 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400831 aaType, SkMatrix::I(),
832 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700833 } else {
834 if (!clipPath.isEmpty()) {
835 GrShape shape(clipPath, GrStyle::SimpleFill());
836 if (canRenderDirectToStencil) {
837 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500838 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700839
Robert Phillips256c37b2017-03-01 14:32:46 -0500840 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500841 std::move(paint),
842 &kDrawToStencil,
843 renderTargetContext,
844 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600845 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400846 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500847 &shape,
848 aaType,
849 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700850 pr->drawPath(args);
851 } else {
852 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500853 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400854 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700855 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600856 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400857 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500858 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700859 args.fShape = &shape;
860 pr->stencilPath(args);
861 }
862 }
863 }
864 }
865
866 // now we modify the clip bit by rendering either the clip
867 // element directly or a bounding rect of the entire clip.
868 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
869 if (drawDirectToClip) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400870 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400871 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400872 SkMatrix::I(),
873 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700874 } else {
875 GrShape shape(clipPath, GrStyle::SimpleFill());
876 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500877 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500878 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500879 std::move(paint),
880 *pass,
881 renderTargetContext,
882 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600883 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400884 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500885 &shape,
886 aaType,
887 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700888 pr->drawPath(args);
889 }
890 } else {
891 // The view matrix is setup to do clip space -> stencil space translation, so
892 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400893 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600894 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700895 }
896 }
897 }
898 return true;
899}