blob: 883ed5d956db41bcb9bf4d5b7b16c77007a59d50 [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 Dalton584a79a2017-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 Dalton584a79a2017-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 Dalton584a79a2017-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 Dalton584a79a2017-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 Dalton584a79a2017-11-15 13:14:01 -0700110 if (SK_InvalidGenID != fAAClipRectGenID && // Is there an AA clip rect?
Chris Dalton3b51df12017-11-27 14:33:06 -0700111 ClipResult::kNotClipped == this->addAnalyticFP(fAAClipRect, Invert::kNo, GrAA::kYes)) {
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 Dalton584a79a2017-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 Dalton584a79a2017-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 Dalton584a79a2017-11-15 13:14:01 -0700487 return this->addAnalyticFP(element->getDeviceSpaceRRect(), Invert::kNo,
Chris Dalton3b51df12017-11-27 14:33:06 -0700488 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700489
Chris Dalton79471932017-10-27 01:50:57 -0600490 case Element::DeviceSpaceType::kPath:
Chris Dalton3b51df12017-11-27 14:33:06 -0700491 return this->addAnalyticFP(element->getDeviceSpacePath(), Invert::kNo,
492 GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600493 }
494
495 SK_ABORT("Unexpected DeviceSpaceType");
496 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700497}
498
Chris Dalton584a79a2017-11-15 13:14:01 -0700499GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600500 switch (element->getDeviceSpaceType()) {
501 case Element::DeviceSpaceType::kEmpty:
502 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700503
Chris Dalton79471932017-10-27 01:50:57 -0600504 case Element::DeviceSpaceType::kRect:
Chris Dalton584a79a2017-11-15 13:14:01 -0700505 if (fWindowRects.count() < fMaxWindowRectangles) {
506 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones,
507 // but it saves processing time.
508 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
509 if (!element->isAA()) {
510 return ClipResult::kClipped;
511 }
512 }
513 return this->addAnalyticFP(element->getDeviceSpaceRect(), Invert::kYes,
Chris Dalton3b51df12017-11-27 14:33:06 -0700514 GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600515
516 case Element::DeviceSpaceType::kRRect: {
Brian Osman554c1f02017-11-16 13:56:47 +0000517 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
Chris Dalton3b51df12017-11-27 14:33:06 -0700518 ClipResult clipResult = this->addAnalyticFP(clipRRect, Invert::kYes,
519 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700520 if (fWindowRects.count() >= fMaxWindowRectangles) {
521 return clipResult;
522 }
523
524 // Clip out the interiors of round rects with two window rectangles in the shape of a
525 // "plus". This doesn't let us skip the clip element, but still saves processing time.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700526 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
527 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
528 if (SkRRect::kComplex_Type == clipRRect.getType()) {
529 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
530 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
531 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
532 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
533 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
534 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
535 }
536 const SkRect& bounds = clipRRect.getBounds();
537 if (insetTL.x() + insetBR.x() >= bounds.width() ||
538 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700539 return clipResult; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700540 }
541
542 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
543 bounds.right(), bounds.bottom() - insetBR.y());
544 this->addWindowRectangle(horzRect, element->isAA());
Chris Dalton584a79a2017-11-15 13:14:01 -0700545
546 if (fWindowRects.count() < fMaxWindowRectangles) {
547 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
548 bounds.right() - insetBR.x(), bounds.bottom());
549 this->addWindowRectangle(vertRect, element->isAA());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700550 }
551
Chris Dalton584a79a2017-11-15 13:14:01 -0700552 return clipResult;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700553 }
Chris Dalton79471932017-10-27 01:50:57 -0600554
555 case Element::DeviceSpaceType::kPath:
Chris Dalton584a79a2017-11-15 13:14:01 -0700556 return this->addAnalyticFP(element->getDeviceSpacePath(), Invert::kYes,
Chris Dalton3b51df12017-11-27 14:33:06 -0700557 GrAA(element->isAA()));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700558 }
Chris Dalton79471932017-10-27 01:50:57 -0600559
560 SK_ABORT("Unexpected DeviceSpaceType");
561 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700562}
563
564inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
565 SkIRect window;
566 if (!elementIsAA) {
567 elementInteriorRect.round(&window);
568 } else {
569 elementInteriorRect.roundIn(&window);
570 }
571 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
572 fWindowRects.addWindow(window);
573 }
574}
575
Chris Dalton584a79a2017-11-15 13:14:01 -0700576template<typename T>
577inline GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const T& deviceSpaceShape,
Chris Dalton3b51df12017-11-27 14:33:06 -0700578 Invert invert, GrAA aa) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700579 if (fAnalyticFPs.count() >= fMaxAnalyticFPs) {
580 return ClipResult::kNotClipped;
581 }
582
583 GrClipEdgeType edgeType;
584 if (Invert::kNo == invert) {
Chris Dalton3b51df12017-11-27 14:33:06 -0700585 edgeType = (GrAA::kYes == aa) ? GrClipEdgeType::kFillAA : GrClipEdgeType::kFillBW;
Chris Dalton584a79a2017-11-15 13:14:01 -0700586 } else {
Chris Dalton3b51df12017-11-27 14:33:06 -0700587 edgeType = (GrAA::kYes == aa) ? GrClipEdgeType::kInverseFillAA
588 : GrClipEdgeType::kInverseFillBW;
Chris Dalton584a79a2017-11-15 13:14:01 -0700589 }
590
591 if (auto fp = make_analytic_clip_fp(edgeType, deviceSpaceShape)) {
592 fAnalyticFPs.push_back(std::move(fp));
593 return ClipResult::kClipped;
594 }
595
596 return ClipResult::kNotClipped;
597}
598
599std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
600 const SkRect& deviceSpaceRect) {
601 return GrConvexPolyEffect::Make(edgeType, deviceSpaceRect);
602}
603
604std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
605 const SkRRect& deviceSpaceRRect) {
606 return GrRRectEffect::Make(edgeType, deviceSpaceRRect);
607}
608
609std::unique_ptr<GrFragmentProcessor> make_analytic_clip_fp(GrClipEdgeType edgeType,
610 const SkPath& deviceSpacePath) {
611 return GrConvexPolyEffect::Make(edgeType, deviceSpacePath);
612}
613
Chris Dalton79471932017-10-27 01:50:57 -0600614void GrReducedClip::makeEmpty() {
615 fHasScissor = false;
616 fAAClipRectGenID = SK_InvalidGenID;
617 fWindowRects.reset();
618 fMaskElements.reset();
619 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000620}
csmartdaltonbde96c62016-08-31 12:54:46 -0700621
622////////////////////////////////////////////////////////////////////////////////
623// Create a 8-bit clip mask in alpha
624
Brian Osman11052242016-10-27 14:47:55 -0400625static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700626 const GrFixedClip& clip,
627 const GrUserStencilSettings* ss,
628 const SkMatrix& viewMatrix,
629 const SkClipStack::Element* element) {
Chris Dalton3b51df12017-11-27 14:33:06 -0700630 GrAA aa = GrAA(element->isAA());
Brian Salomonf3b46e52017-08-30 11:37:57 -0400631 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600632 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700633 SkDEBUGFAIL("Should never get here with an empty element.");
634 break;
Chris Dalton79471932017-10-27 01:50:57 -0600635 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400636 return rtc->priv().drawAndStencilRect(clip, ss, (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500637 element->isInverseFilled(), aa, viewMatrix,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400638 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700639 break;
640 default: {
641 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400642 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700643 if (path.isInverseFillType()) {
644 path.toggleInverseFillType();
645 }
646
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500647 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
648 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700649 break;
650 }
651 }
652
653 return false;
654}
655
Brian Osman11052242016-10-27 14:47:55 -0400656static void draw_element(GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500657 const GrClip& clip, // TODO: can this just always be WideOpen?
658 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500659 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700660 const SkMatrix& viewMatrix,
661 const SkClipStack::Element* element) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700662 // TODO: Draw rrects directly here.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400663 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600664 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700665 SkDEBUGFAIL("Should never get here with an empty element.");
666 break;
Chris Dalton79471932017-10-27 01:50:57 -0600667 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400668 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700669 break;
670 default: {
671 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400672 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700673 if (path.isInverseFillType()) {
674 path.toggleInverseFillType();
675 }
676
Brian Salomon82f44312017-01-11 13:42:54 -0500677 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700678 break;
679 }
680 }
681}
682
Brian Osman11052242016-10-27 14:47:55 -0400683bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700684 // The texture may be larger than necessary, this rect represents the part of the texture
685 // we populate with a rasterization of the clip.
Chris Dalton79471932017-10-27 01:50:57 -0600686 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700687
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700688 if (!fWindowRects.empty()) {
Chris Dalton79471932017-10-27 01:50:57 -0600689 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700690 GrWindowRectsState::Mode::kExclusive);
691 }
692
csmartdaltonbde96c62016-08-31 12:54:46 -0700693 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
694 // clear the part that we care about.
695 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400696 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700697
698 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
699 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600700 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700701
702 // walk through each clip element and perform its set op
Chris Dalton79471932017-10-27 01:50:57 -0600703 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700704 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700705 SkRegion::Op op = (SkRegion::Op)element->getOp();
Chris Dalton3b51df12017-11-27 14:33:06 -0700706 GrAA aa = GrAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700707 bool invert = element->isInverseFilled();
708 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
709 // draw directly into the result with the stencil set to make the pixels affected
710 // by the clip shape be non-zero.
711 static constexpr GrUserStencilSettings kStencilInElement(
712 GrUserStencilSettings::StaticInit<
713 0xffff,
714 GrUserStencilTest::kAlways,
715 0xffff,
716 GrUserStencilOp::kReplace,
717 GrUserStencilOp::kReplace,
718 0xffff>()
719 );
Brian Osman11052242016-10-27 14:47:55 -0400720 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700721 return false;
722 }
723
724 // Draw to the exterior pixels (those with a zero stencil value).
725 static constexpr GrUserStencilSettings kDrawOutsideElement(
726 GrUserStencilSettings::StaticInit<
727 0x0000,
728 GrUserStencilTest::kEqual,
729 0xffff,
730 GrUserStencilOp::kZero,
731 GrUserStencilOp::kZero,
732 0xffff>()
733 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500734 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Chris Dalton79471932017-10-27 01:50:57 -0600735 translate, SkRect::Make(fScissor))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700736 return false;
737 }
738 } else {
739 // all the remaining ops can just be directly draw into the accumulation buffer
740 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700741 paint.setCoverageSetOpXPFactory(op, false);
742
Brian Salomon82f44312017-01-11 13:42:54 -0500743 draw_element(rtc, clip, std::move(paint), aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700744 }
745 }
746
747 return true;
748}
749
750////////////////////////////////////////////////////////////////////////////////
751// Create a 1-bit clip mask in the stencil buffer.
752
csmartdaltonbde96c62016-08-31 12:54:46 -0700753bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400754 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700755 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Chris Daltonbbfd5162017-11-07 13:35:22 -0700756 GrStencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700757
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700758 if (!fWindowRects.empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700759 stencilClip.fixedClip().setWindowRectangles(fWindowRects,
760 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700761 }
762
csmartdaltonbde96c62016-08-31 12:54:46 -0700763 bool initialState = InitialState::kAllIn == this->initialState();
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000764 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700765
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500766 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600767 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700768 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500769 GrAAType aaType = GrAAType::kNone;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400770 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500771 aaType = GrAAType::kMSAA;
772 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700773
774 bool fillInverted = false;
775
776 // This will be used to determine whether the clip shape can be rendered into the
777 // stencil with arbitrary stencil settings.
778 GrPathRenderer::StencilSupport stencilSupport;
779
reed73603f32016-09-20 08:42:38 -0700780 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700781
782 GrPathRenderer* pr = nullptr;
783 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400784 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700785 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
786 fillInverted = false;
787 } else {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400788 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700789 fillInverted = clipPath.isInverseFillType();
790 if (fillInverted) {
791 clipPath.toggleInverseFillType();
792 }
793
794 GrShape shape(clipPath, GrStyle::SimpleFill());
795 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700796 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600797 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400798 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700799 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500800 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700801 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700802
803 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500804 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700805 &stencilSupport);
806 if (!pr) {
807 return false;
808 }
809 }
810
811 bool canRenderDirectToStencil =
812 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
813 bool drawDirectToClip; // Given the renderer, the element,
814 // fill rule, and set operation should
815 // we render the element directly to
816 // stencil bit used for clipping.
817 GrUserStencilSettings const* const* stencilPasses =
818 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
819 &drawDirectToClip);
820
821 // draw the element to the client stencil bits if necessary
822 if (!drawDirectToClip) {
823 static constexpr GrUserStencilSettings kDrawToStencil(
824 GrUserStencilSettings::StaticInit<
825 0x0000,
826 GrUserStencilTest::kAlways,
827 0xffff,
828 GrUserStencilOp::kIncMaybeClamp,
829 GrUserStencilOp::kIncMaybeClamp,
830 0xffff>()
831 );
Brian Salomonf3b46e52017-08-30 11:37:57 -0400832 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400833 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400834 aaType, SkMatrix::I(),
835 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700836 } else {
837 if (!clipPath.isEmpty()) {
838 GrShape shape(clipPath, GrStyle::SimpleFill());
839 if (canRenderDirectToStencil) {
840 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500841 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700842
Robert Phillips256c37b2017-03-01 14:32:46 -0500843 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500844 std::move(paint),
845 &kDrawToStencil,
846 renderTargetContext,
847 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600848 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400849 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500850 &shape,
851 aaType,
852 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700853 pr->drawPath(args);
854 } else {
855 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500856 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400857 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700858 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600859 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400860 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500861 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700862 args.fShape = &shape;
863 pr->stencilPath(args);
864 }
865 }
866 }
867 }
868
869 // now we modify the clip bit by rendering either the clip
870 // element directly or a bounding rect of the entire clip.
871 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
872 if (drawDirectToClip) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400873 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400874 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400875 SkMatrix::I(),
876 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700877 } else {
878 GrShape shape(clipPath, GrStyle::SimpleFill());
879 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500880 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500881 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500882 std::move(paint),
883 *pass,
884 renderTargetContext,
885 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600886 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400887 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500888 &shape,
889 aaType,
890 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700891 pr->drawPath(args);
892 }
893 } else {
894 // The view matrix is setup to do clip space -> stencil space translation, so
895 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400896 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600897 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700898 }
899 }
900 }
901 return true;
902}