blob: f22444895b89434d7743a61a95c75f3c40d02d34 [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"
csmartdaltonbde96c62016-08-31 12:54:46 -070020#include "GrStyle.h"
21#include "GrUserStencilSettings.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050022#include "SkClipOpPriv.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070023
csmartdalton5ecbbbe2016-08-23 13:26:40 -070024/**
25 * There are plenty of optimizations that could be added here. Maybe flips could be folded into
26 * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
27 * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
28 * based on later intersect operations, and perhaps remove intersect-rects. We could optionally
29 * take a rect in case the caller knows a bound on what is to be drawn through this clip.
30 */
csmartdaltonbf4a8f92016-09-06 10:01:06 -070031GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
32 int maxWindowRectangles) {
csmartdaltoncbecb082016-07-22 08:59:08 -070033 SkASSERT(!queryBounds.isEmpty());
Chris Dalton79471932017-10-27 01:50:57 -060034 fHasScissor = false;
35 fAAClipRectGenID = SK_InvalidGenID;
csmartdaltoncbecb082016-07-22 08:59:08 -070036
bsalomon@google.com170bd792012-12-05 22:26:11 +000037 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070038 fInitialState = InitialState::kAllIn;
39 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000040 }
41
42 SkClipStack::BoundsType stackBoundsType;
43 SkRect stackBounds;
44 bool iior;
45 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
46
Chris Dalton348060f2017-06-05 13:15:37 -060047 if (GrClip::IsOutsideClip(stackBounds, queryBounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -070048 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070049 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
50 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000051 }
52
csmartdaltoncbecb082016-07-22 08:59:08 -070053 if (iior) {
54 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
55 // This should only be true if aa/non-aa status matches among all elements.
56 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
57 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
58 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
Chris Dalton79471932017-10-27 01:50:57 -060059 // The clip is a non-aa rect. Here we just implement the entire thing using fScissor.
Jim Van Verthb1fc3682017-10-31 11:30:46 -040060 SkRect tightBounds;
61 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
62 tightBounds.round(&fScissor);
Chris Dalton79471932017-10-27 01:50:57 -060063 fHasScissor = true;
64 fInitialState = fScissor.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
csmartdalton77f2fae2016-08-08 09:55:06 -070065 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070066 }
67 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070068 fInitialState = InitialState::kAllIn;
69 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070070 }
71
csmartdaltond211e782016-08-15 11:17:19 -070072 SkRect tightBounds;
73 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
Chris Dalton79471932017-10-27 01:50:57 -060074 fScissor = GrClip::GetPixelIBounds(tightBounds);
75 if (fScissor.isEmpty()) {
Chris Dalton348060f2017-06-05 13:15:37 -060076 fInitialState = InitialState::kAllOut;
77 return;
78 }
Chris Dalton79471932017-10-27 01:50:57 -060079 fHasScissor = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070080
Chris Dalton79471932017-10-27 01:50:57 -060081 fAAClipRect = stackBounds;
82 fAAClipRectGenID = stack.getTopmostGenID();
83 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
csmartdalton77f2fae2016-08-08 09:55:06 -070084
Chris Dalton79471932017-10-27 01:50:57 -060085 fInitialState = InitialState::kAllIn;
86 } else {
87 SkRect tighterQuery = queryBounds;
88 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
89 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This
90 // new clip will be enforced by the scissor.)
91 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
92 }
93
94 fScissor = GrClip::GetPixelIBounds(tighterQuery);
95 if (fScissor.isEmpty()) {
96 fInitialState = InitialState::kAllOut;
97 return;
98 }
99 fHasScissor = true;
100
101 // Now that we have determined the bounds to use and filtered out the trivial cases, call the
102 // helper that actually walks the stack.
103 this->walkStack(stack, tighterQuery, maxWindowRectangles);
csmartdaltoncbecb082016-07-22 08:59:08 -0700104 }
105
Chris Dalton79471932017-10-27 01:50:57 -0600106 if (SK_InvalidGenID != fAAClipRectGenID) { // Is there an AA clip rect?
107 if (fMaskElements.isEmpty()) {
108 // Use a replace since it is faster than intersect.
109 fMaskElements.addToHead(fAAClipRect, SkMatrix::I(), kReplace_SkClipOp, true /*doAA*/);
110 fInitialState = InitialState::kAllOut;
111 } else {
112 fMaskElements.addToTail(fAAClipRect, SkMatrix::I(), kIntersect_SkClipOp, true /*doAA*/);
113 }
114 fMaskRequiresAA = true;
115 fMaskGenID = fAAClipRectGenID;
116 fAAClipRectGenID = SK_InvalidGenID;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700117 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700118}
119
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700120void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
121 int maxWindowRectangles) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700122 // walk backwards until we get to:
123 // a) the beginning
124 // b) an operation that is known to make the bounds all inside/outside
125 // c) a replace operation
126
127 enum class InitialTriState {
128 kUnknown = -1,
129 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
130 kAllOut = (int)GrReducedClip::InitialState::kAllOut
131 } initialTriState = InitialTriState::kUnknown;
132
133 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
134 // TODO: track these per saved clip so that we can consider them on the forward pass.
135 bool embiggens = false;
136 bool emsmallens = false;
137
138 // We use a slightly relaxed set of query bounds for element containment tests. This is to
139 // account for floating point rounding error that may have occurred during coord transforms.
140 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
141 GrClip::kBoundsTolerance);
142
143 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
144 int numAAElements = 0;
145 while (InitialTriState::kUnknown == initialTriState) {
146 const Element* element = iter.prev();
147 if (nullptr == element) {
148 initialTriState = InitialTriState::kAllIn;
149 break;
150 }
151 if (SkClipStack::kEmptyGenID == element->getGenID()) {
152 initialTriState = InitialTriState::kAllOut;
153 break;
154 }
155 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
156 initialTriState = InitialTriState::kAllIn;
157 break;
158 }
159
160 bool skippable = false;
161 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
162
163 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500164 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700165 // check if the shape subtracted either contains the entire bounds (and makes
166 // the clip empty) or is outside the bounds and therefore can be skipped.
167 if (element->isInverseFilled()) {
168 if (element->contains(relaxedQueryBounds)) {
169 skippable = true;
170 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
171 initialTriState = InitialTriState::kAllOut;
172 skippable = true;
173 }
174 } else {
175 if (element->contains(relaxedQueryBounds)) {
176 initialTriState = InitialTriState::kAllOut;
177 skippable = true;
178 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
179 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600180 } else if (!embiggens) {
181 ClipResult result = this->clipOutsideElement(element, maxWindowRectangles);
182 if (ClipResult::kMadeEmpty == result) {
183 return;
184 }
185 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700186 }
187 }
188 if (!skippable) {
189 emsmallens = true;
190 }
191 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500192 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700193 // check if the shape intersected contains the entire bounds and therefore can
194 // be skipped or it is outside the entire bounds and therefore makes the clip
195 // empty.
196 if (element->isInverseFilled()) {
197 if (element->contains(relaxedQueryBounds)) {
198 initialTriState = InitialTriState::kAllOut;
199 skippable = true;
200 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
201 skippable = true;
202 }
203 } else {
204 if (element->contains(relaxedQueryBounds)) {
205 skippable = true;
206 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
207 initialTriState = InitialTriState::kAllOut;
208 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600209 } else if (!embiggens) {
210 ClipResult result = this->clipInsideElement(element);
211 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700212 return;
213 }
Chris Dalton79471932017-10-27 01:50:57 -0600214 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700215 }
216 }
217 if (!skippable) {
218 emsmallens = true;
219 }
220 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500221 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700222 // If the union-ed shape contains the entire bounds then after this element
223 // the bounds is entirely inside the clip. If the union-ed shape is outside the
224 // bounds then this op can be skipped.
225 if (element->isInverseFilled()) {
226 if (element->contains(relaxedQueryBounds)) {
227 skippable = true;
228 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
229 initialTriState = InitialTriState::kAllIn;
230 skippable = true;
231 }
232 } else {
233 if (element->contains(relaxedQueryBounds)) {
234 initialTriState = InitialTriState::kAllIn;
235 skippable = true;
236 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
237 skippable = true;
238 }
239 }
240 if (!skippable) {
241 embiggens = true;
242 }
243 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500244 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700245 // If the bounds is entirely inside the shape being xor-ed then the effect is
246 // to flip the inside/outside state of every point in the bounds. We may be
247 // able to take advantage of this in the forward pass. If the xor-ed shape
248 // doesn't intersect the bounds then it can be skipped.
249 if (element->isInverseFilled()) {
250 if (element->contains(relaxedQueryBounds)) {
251 skippable = true;
252 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
253 isFlip = true;
254 }
255 } else {
256 if (element->contains(relaxedQueryBounds)) {
257 isFlip = true;
258 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
259 skippable = true;
260 }
261 }
262 if (!skippable) {
263 emsmallens = embiggens = true;
264 }
265 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500266 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700267 // When the bounds is entirely within the rev-diff shape then this behaves like xor
268 // and reverses every point inside the bounds. If the shape is completely outside
269 // the bounds then we know after this element is applied that the bounds will be
270 // all outside the current clip.B
271 if (element->isInverseFilled()) {
272 if (element->contains(relaxedQueryBounds)) {
273 initialTriState = InitialTriState::kAllOut;
274 skippable = true;
275 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
276 isFlip = true;
277 }
278 } else {
279 if (element->contains(relaxedQueryBounds)) {
280 isFlip = true;
281 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
282 initialTriState = InitialTriState::kAllOut;
283 skippable = true;
284 }
285 }
286 if (!skippable) {
287 emsmallens = embiggens = true;
288 }
289 break;
290
Mike Reedc1f77742016-12-09 09:00:50 -0500291 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700292 // Replace will always terminate our walk. We will either begin the forward walk
293 // at the replace op or detect here than the shape is either completely inside
294 // or completely outside the bounds. In this latter case it can be skipped by
295 // setting the correct value for initialTriState.
296 if (element->isInverseFilled()) {
297 if (element->contains(relaxedQueryBounds)) {
298 initialTriState = InitialTriState::kAllOut;
299 skippable = true;
300 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
301 initialTriState = InitialTriState::kAllIn;
302 skippable = true;
303 }
304 } else {
305 if (element->contains(relaxedQueryBounds)) {
306 initialTriState = InitialTriState::kAllIn;
307 skippable = true;
308 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
309 initialTriState = InitialTriState::kAllOut;
310 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600311 } else if (!embiggens) {
312 ClipResult result = this->clipInsideElement(element);
313 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700314 return;
315 }
Chris Dalton79471932017-10-27 01:50:57 -0600316 if (ClipResult::kClipped == result) {
317 initialTriState = InitialTriState::kAllIn;
318 skippable = true;
319 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700320 }
321 }
322 if (!skippable) {
323 initialTriState = InitialTriState::kAllOut;
324 embiggens = emsmallens = true;
325 }
326 break;
327 default:
328 SkDEBUGFAIL("Unexpected op.");
329 break;
330 }
331 if (!skippable) {
Chris Dalton79471932017-10-27 01:50:57 -0600332 if (fMaskElements.isEmpty()) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700333 // This will be the last element. Record the stricter genID.
Chris Dalton79471932017-10-27 01:50:57 -0600334 fMaskGenID = element->getGenID();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700335 }
336
337 // if it is a flip, change it to a bounds-filling rect
338 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500339 SkASSERT(kXOR_SkClipOp == element->getOp() ||
340 kReverseDifference_SkClipOp == element->getOp());
Chris Dalton79471932017-10-27 01:50:57 -0600341 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
342 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700343 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600344 Element* newElement = fMaskElements.addToHead(*element);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700345 if (newElement->isAA()) {
346 ++numAAElements;
347 }
348 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
349 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
350 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500351 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700352 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500353 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700354 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500355 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700356 if (isReplace) {
357 SkASSERT(InitialTriState::kAllOut == initialTriState);
358 initialTriState = InitialTriState::kAllIn;
359 }
360 }
361 }
362 }
363 }
364
365 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
366 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
Chris Dalton79471932017-10-27 01:50:57 -0600367 fMaskElements.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700368 numAAElements = 0;
369 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600370 Element* element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700371 while (element) {
372 bool skippable = false;
373 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500374 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700375 // subtracting from the empty set yields the empty set.
376 skippable = InitialTriState::kAllOut == initialTriState;
377 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500378 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700379 // intersecting with the empty set yields the empty set
380 if (InitialTriState::kAllOut == initialTriState) {
381 skippable = true;
382 } else {
383 // We can clear to zero and then simply draw the clip element.
384 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500385 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700386 }
387 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500388 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700389 if (InitialTriState::kAllIn == initialTriState) {
390 // unioning the infinite plane with anything is a no-op.
391 skippable = true;
392 } else {
393 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500394 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700395 }
396 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500397 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700398 if (InitialTriState::kAllOut == initialTriState) {
399 // xor could be changed to diff in the kAllIn case, not sure it's a win.
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 kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700404 if (InitialTriState::kAllIn == initialTriState) {
405 // subtracting the whole plane will yield the empty set.
406 skippable = true;
407 initialTriState = InitialTriState::kAllOut;
408 } else {
409 // this picks up flips inserted in the backwards pass.
410 skippable = element->isInverseFilled() ?
411 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
412 element->contains(relaxedQueryBounds);
413 if (skippable) {
414 initialTriState = InitialTriState::kAllIn;
415 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500416 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700417 }
418 }
419 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500420 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700421 skippable = false; // we would have skipped it in the backwards walk if we
422 // could've.
423 break;
424 default:
425 SkDEBUGFAIL("Unexpected op.");
426 break;
427 }
428 if (!skippable) {
429 break;
430 } else {
431 if (element->isAA()) {
432 --numAAElements;
433 }
Chris Dalton79471932017-10-27 01:50:57 -0600434 fMaskElements.popHead();
435 element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700436 }
437 }
438 }
Chris Dalton79471932017-10-27 01:50:57 -0600439 fMaskRequiresAA = numAAElements > 0;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700440
441 SkASSERT(InitialTriState::kUnknown != initialTriState);
442 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
443}
444
Chris Dalton79471932017-10-27 01:50:57 -0600445GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
446 SkIRect elementIBounds;
447 if (!element->isAA()) {
448 element->getBounds().round(&elementIBounds);
449 } else {
450 elementIBounds = GrClip::GetPixelIBounds(element->getBounds());
451 }
452 SkASSERT(fHasScissor);
453 if (!fScissor.intersect(elementIBounds)) {
454 this->makeEmpty();
455 return ClipResult::kMadeEmpty;
456 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700457
Chris Dalton79471932017-10-27 01:50:57 -0600458 switch (element->getDeviceSpaceType()) {
459 case Element::DeviceSpaceType::kEmpty:
460 return ClipResult::kMadeEmpty;
461
462 case Element::DeviceSpaceType::kRect:
463 SkASSERT(element->getBounds() == element->getDeviceSpaceRect());
464 if (element->isAA()) {
465 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
466 fAAClipRect = element->getDeviceSpaceRect();
467 // fAAClipRectGenID is the value we should use for fMaskGenID if we end up
468 // moving the AA clip rect into the mask. The mask GenID is simply the topmost
469 // element's GenID. And since we walk the stack backwards, this means it's just
470 // the first element we don't skip during our walk.
471 fAAClipRectGenID = fMaskElements.isEmpty() ? element->getGenID() : fMaskGenID;
472 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
473 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
474 this->makeEmpty();
475 return ClipResult::kMadeEmpty;
476 }
477 }
478 return ClipResult::kClipped;
479
480 case Element::DeviceSpaceType::kRRect:
481 case Element::DeviceSpaceType::kPath:
482 return ClipResult::kNotClipped;
483 }
484
485 SK_ABORT("Unexpected DeviceSpaceType");
486 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700487}
488
Chris Dalton79471932017-10-27 01:50:57 -0600489GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element,
490 int maxWindowRectangles) {
491 if (fWindowRects.count() >= maxWindowRectangles) {
492 return ClipResult::kNotClipped;
493 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700494
Chris Dalton79471932017-10-27 01:50:57 -0600495 switch (element->getDeviceSpaceType()) {
496 case Element::DeviceSpaceType::kEmpty:
497 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700498
Chris Dalton79471932017-10-27 01:50:57 -0600499 case Element::DeviceSpaceType::kRect:
500 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones, but
501 // it saves processing time.
502 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
503 return !element->isAA() ? ClipResult::kClipped : ClipResult::kNotClipped;
504
505 case Element::DeviceSpaceType::kRRect: {
506 // Clip out the interiors of round rects with two window rectangles in the shape of a
507 // plus. It doesn't allow us to skip the clip element, but still saves processing time.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400508 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700509 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
510 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
511 if (SkRRect::kComplex_Type == clipRRect.getType()) {
512 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
513 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
514 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
515 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
516 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
517 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
518 }
519 const SkRect& bounds = clipRRect.getBounds();
520 if (insetTL.x() + insetBR.x() >= bounds.width() ||
521 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Dalton79471932017-10-27 01:50:57 -0600522 return ClipResult::kNotClipped; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700523 }
524
525 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
526 bounds.right(), bounds.bottom() - insetBR.y());
527 this->addWindowRectangle(horzRect, element->isAA());
528 if (fWindowRects.count() >= maxWindowRectangles) {
Chris Dalton79471932017-10-27 01:50:57 -0600529 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700530 }
531
532 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
533 bounds.right() - insetBR.x(), bounds.bottom());
534 this->addWindowRectangle(vertRect, element->isAA());
Chris Dalton79471932017-10-27 01:50:57 -0600535 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700536 }
Chris Dalton79471932017-10-27 01:50:57 -0600537
538 case Element::DeviceSpaceType::kPath:
539 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700540 }
Chris Dalton79471932017-10-27 01:50:57 -0600541
542 SK_ABORT("Unexpected DeviceSpaceType");
543 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700544}
545
546inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
547 SkIRect window;
548 if (!elementIsAA) {
549 elementInteriorRect.round(&window);
550 } else {
551 elementInteriorRect.roundIn(&window);
552 }
553 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
554 fWindowRects.addWindow(window);
555 }
556}
557
Chris Dalton79471932017-10-27 01:50:57 -0600558void GrReducedClip::makeEmpty() {
559 fHasScissor = false;
560 fAAClipRectGenID = SK_InvalidGenID;
561 fWindowRects.reset();
562 fMaskElements.reset();
563 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000564}
csmartdaltonbde96c62016-08-31 12:54:46 -0700565
566////////////////////////////////////////////////////////////////////////////////
567// Create a 8-bit clip mask in alpha
568
Brian Osman11052242016-10-27 14:47:55 -0400569static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700570 const GrFixedClip& clip,
571 const GrUserStencilSettings* ss,
572 const SkMatrix& viewMatrix,
573 const SkClipStack::Element* element) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500574 GrAA aa = GrBoolToAA(element->isAA());
Brian Salomonf3b46e52017-08-30 11:37:57 -0400575 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600576 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700577 SkDEBUGFAIL("Should never get here with an empty element.");
578 break;
Chris Dalton79471932017-10-27 01:50:57 -0600579 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400580 return rtc->priv().drawAndStencilRect(clip, ss, (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500581 element->isInverseFilled(), aa, viewMatrix,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400582 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700583 break;
584 default: {
585 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400586 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700587 if (path.isInverseFillType()) {
588 path.toggleInverseFillType();
589 }
590
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500591 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
592 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700593 break;
594 }
595 }
596
597 return false;
598}
599
Brian Osman11052242016-10-27 14:47:55 -0400600static void draw_element(GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500601 const GrClip& clip, // TODO: can this just always be WideOpen?
602 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500603 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700604 const SkMatrix& viewMatrix,
605 const SkClipStack::Element* element) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700606 // TODO: Draw rrects directly here.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400607 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600608 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700609 SkDEBUGFAIL("Should never get here with an empty element.");
610 break;
Chris Dalton79471932017-10-27 01:50:57 -0600611 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400612 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700613 break;
614 default: {
615 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400616 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700617 if (path.isInverseFillType()) {
618 path.toggleInverseFillType();
619 }
620
Brian Salomon82f44312017-01-11 13:42:54 -0500621 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700622 break;
623 }
624 }
625}
626
Brian Osman11052242016-10-27 14:47:55 -0400627bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700628 // The texture may be larger than necessary, this rect represents the part of the texture
629 // we populate with a rasterization of the clip.
Chris Dalton79471932017-10-27 01:50:57 -0600630 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700631
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700632 if (!fWindowRects.empty()) {
Chris Dalton79471932017-10-27 01:50:57 -0600633 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700634 GrWindowRectsState::Mode::kExclusive);
635 }
636
csmartdaltonbde96c62016-08-31 12:54:46 -0700637 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
638 // clear the part that we care about.
639 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400640 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700641
642 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
643 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600644 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700645
646 // walk through each clip element and perform its set op
Chris Dalton79471932017-10-27 01:50:57 -0600647 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700648 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700649 SkRegion::Op op = (SkRegion::Op)element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500650 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700651 bool invert = element->isInverseFilled();
652 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
653 // draw directly into the result with the stencil set to make the pixels affected
654 // by the clip shape be non-zero.
655 static constexpr GrUserStencilSettings kStencilInElement(
656 GrUserStencilSettings::StaticInit<
657 0xffff,
658 GrUserStencilTest::kAlways,
659 0xffff,
660 GrUserStencilOp::kReplace,
661 GrUserStencilOp::kReplace,
662 0xffff>()
663 );
Brian Osman11052242016-10-27 14:47:55 -0400664 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700665 return false;
666 }
667
668 // Draw to the exterior pixels (those with a zero stencil value).
669 static constexpr GrUserStencilSettings kDrawOutsideElement(
670 GrUserStencilSettings::StaticInit<
671 0x0000,
672 GrUserStencilTest::kEqual,
673 0xffff,
674 GrUserStencilOp::kZero,
675 GrUserStencilOp::kZero,
676 0xffff>()
677 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500678 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Chris Dalton79471932017-10-27 01:50:57 -0600679 translate, SkRect::Make(fScissor))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700680 return false;
681 }
682 } else {
683 // all the remaining ops can just be directly draw into the accumulation buffer
684 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700685 paint.setCoverageSetOpXPFactory(op, false);
686
Brian Salomon82f44312017-01-11 13:42:54 -0500687 draw_element(rtc, clip, std::move(paint), aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700688 }
689 }
690
691 return true;
692}
693
694////////////////////////////////////////////////////////////////////////////////
695// Create a 1-bit clip mask in the stencil buffer.
696
697class StencilClip final : public GrClip {
698public:
Robert Phillips806be2d2017-06-28 15:23:59 -0400699 StencilClip(const SkIRect& scissorRect, uint32_t clipStackID)
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400700 : fFixedClip(scissorRect)
701 , fClipStackID(clipStackID) {
702 }
703
csmartdaltonbde96c62016-08-31 12:54:46 -0700704 const GrFixedClip& fixedClip() const { return fFixedClip; }
705
Brian Salomon9a767722017-03-13 17:57:28 -0400706 void setWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
707 fFixedClip.setWindowRectangles(windows, mode);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700708 }
709
csmartdaltonbde96c62016-08-31 12:54:46 -0700710private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700711 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700712 return false;
713 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700714 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
715 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700716 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500717 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700718 return false;
719 }
Brian Osman11052242016-10-27 14:47:55 -0400720 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
Brian Salomon97180af2017-03-14 13:42:58 -0400721 bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
722 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out,
723 bounds)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700724 return false;
725 }
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400726 out->addStencilClip(fClipStackID);
csmartdaltonbde96c62016-08-31 12:54:46 -0700727 return true;
728 }
729
730 GrFixedClip fFixedClip;
Robert Phillips806be2d2017-06-28 15:23:59 -0400731 uint32_t fClipStackID;
csmartdaltonbde96c62016-08-31 12:54:46 -0700732
733 typedef GrClip INHERITED;
734};
735
736bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400737 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700738 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Chris Dalton79471932017-10-27 01:50:57 -0600739 StencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700740
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700741 if (!fWindowRects.empty()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400742 stencilClip.setWindowRectangles(fWindowRects, GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700743 }
744
csmartdaltonbde96c62016-08-31 12:54:46 -0700745 bool initialState = InitialState::kAllIn == this->initialState();
Brian Osman693a5402016-10-27 15:13:22 -0400746 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700747
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500748 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600749 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700750 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500751 GrAAType aaType = GrAAType::kNone;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400752 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500753 aaType = GrAAType::kMSAA;
754 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700755
756 bool fillInverted = false;
757
758 // This will be used to determine whether the clip shape can be rendered into the
759 // stencil with arbitrary stencil settings.
760 GrPathRenderer::StencilSupport stencilSupport;
761
reed73603f32016-09-20 08:42:38 -0700762 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700763
764 GrPathRenderer* pr = nullptr;
765 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400766 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700767 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
768 fillInverted = false;
769 } else {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400770 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700771 fillInverted = clipPath.isInverseFillType();
772 if (fillInverted) {
773 clipPath.toggleInverseFillType();
774 }
775
776 GrShape shape(clipPath, GrStyle::SimpleFill());
777 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700778 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600779 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400780 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700781 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500782 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700783 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700784
785 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500786 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700787 &stencilSupport);
788 if (!pr) {
789 return false;
790 }
791 }
792
793 bool canRenderDirectToStencil =
794 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
795 bool drawDirectToClip; // Given the renderer, the element,
796 // fill rule, and set operation should
797 // we render the element directly to
798 // stencil bit used for clipping.
799 GrUserStencilSettings const* const* stencilPasses =
800 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
801 &drawDirectToClip);
802
803 // draw the element to the client stencil bits if necessary
804 if (!drawDirectToClip) {
805 static constexpr GrUserStencilSettings kDrawToStencil(
806 GrUserStencilSettings::StaticInit<
807 0x0000,
808 GrUserStencilTest::kAlways,
809 0xffff,
810 GrUserStencilOp::kIncMaybeClamp,
811 GrUserStencilOp::kIncMaybeClamp,
812 0xffff>()
813 );
Brian Salomonf3b46e52017-08-30 11:37:57 -0400814 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400815 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400816 aaType, SkMatrix::I(),
817 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700818 } else {
819 if (!clipPath.isEmpty()) {
820 GrShape shape(clipPath, GrStyle::SimpleFill());
821 if (canRenderDirectToStencil) {
822 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500823 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700824
Robert Phillips256c37b2017-03-01 14:32:46 -0500825 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500826 std::move(paint),
827 &kDrawToStencil,
828 renderTargetContext,
829 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600830 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400831 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500832 &shape,
833 aaType,
834 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700835 pr->drawPath(args);
836 } else {
837 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500838 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400839 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700840 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600841 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400842 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500843 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700844 args.fShape = &shape;
845 pr->stencilPath(args);
846 }
847 }
848 }
849 }
850
851 // now we modify the clip bit by rendering either the clip
852 // element directly or a bounding rect of the entire clip.
853 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
854 if (drawDirectToClip) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400855 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400856 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400857 SkMatrix::I(),
858 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700859 } else {
860 GrShape shape(clipPath, GrStyle::SimpleFill());
861 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500862 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500863 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500864 std::move(paint),
865 *pass,
866 renderTargetContext,
867 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600868 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400869 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500870 &shape,
871 aaType,
872 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700873 pr->drawPath(args);
874 }
875 } else {
876 // The view matrix is setup to do clip space -> stencil space translation, so
877 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400878 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600879 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700880 }
881 }
882 }
883 return true;
884}