blob: bc3286bdf0899926f8c0cce9d05413e336620fdb [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,
Chris Daltond8d15932017-11-01 19:21:24 +000032 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.
Mike Kleine26062a2017-10-31 20:56:54 +000060 stackBounds.round(&fScissor);
Chris Dalton79471932017-10-27 01:50:57 -060061 fHasScissor = true;
62 fInitialState = fScissor.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
csmartdalton77f2fae2016-08-08 09:55:06 -070063 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070064 }
65 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070066 fInitialState = InitialState::kAllIn;
67 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070068 }
69
csmartdaltond211e782016-08-15 11:17:19 -070070 SkRect tightBounds;
71 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
Chris Dalton79471932017-10-27 01:50:57 -060072 fScissor = GrClip::GetPixelIBounds(tightBounds);
73 if (fScissor.isEmpty()) {
Chris Dalton348060f2017-06-05 13:15:37 -060074 fInitialState = InitialState::kAllOut;
75 return;
76 }
Chris Dalton79471932017-10-27 01:50:57 -060077 fHasScissor = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070078
Chris Dalton79471932017-10-27 01:50:57 -060079 fAAClipRect = stackBounds;
80 fAAClipRectGenID = stack.getTopmostGenID();
81 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
csmartdalton77f2fae2016-08-08 09:55:06 -070082
Chris Dalton79471932017-10-27 01:50:57 -060083 fInitialState = InitialState::kAllIn;
84 } else {
85 SkRect tighterQuery = queryBounds;
86 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
87 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This
88 // new clip will be enforced by the scissor.)
89 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
90 }
91
92 fScissor = GrClip::GetPixelIBounds(tighterQuery);
93 if (fScissor.isEmpty()) {
94 fInitialState = InitialState::kAllOut;
95 return;
96 }
97 fHasScissor = true;
98
Chris Daltond8d15932017-11-01 19:21:24 +000099 // Now that we have determined the bounds to use and filtered out the trivial cases, call the
100 // helper that actually walks the stack.
101 this->walkStack(stack, tighterQuery, maxWindowRectangles);
csmartdaltoncbecb082016-07-22 08:59:08 -0700102 }
103
Chris Daltond8d15932017-11-01 19:21:24 +0000104 if (SK_InvalidGenID != fAAClipRectGenID) { // Is there an AA clip rect?
Chris Dalton79471932017-10-27 01:50:57 -0600105 if (fMaskElements.isEmpty()) {
106 // Use a replace since it is faster than intersect.
107 fMaskElements.addToHead(fAAClipRect, SkMatrix::I(), kReplace_SkClipOp, true /*doAA*/);
108 fInitialState = InitialState::kAllOut;
109 } else {
110 fMaskElements.addToTail(fAAClipRect, SkMatrix::I(), kIntersect_SkClipOp, true /*doAA*/);
111 }
112 fMaskRequiresAA = true;
113 fMaskGenID = fAAClipRectGenID;
Chris Daltond8d15932017-11-01 19:21:24 +0000114 fAAClipRectGenID = SK_InvalidGenID;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700115 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700116}
117
Chris Daltond8d15932017-11-01 19:21:24 +0000118void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
119 int maxWindowRectangles) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700120 // walk backwards until we get to:
121 // a) the beginning
122 // b) an operation that is known to make the bounds all inside/outside
123 // c) a replace operation
124
125 enum class InitialTriState {
126 kUnknown = -1,
127 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
128 kAllOut = (int)GrReducedClip::InitialState::kAllOut
129 } initialTriState = InitialTriState::kUnknown;
130
131 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
132 // TODO: track these per saved clip so that we can consider them on the forward pass.
133 bool embiggens = false;
134 bool emsmallens = false;
135
136 // We use a slightly relaxed set of query bounds for element containment tests. This is to
137 // account for floating point rounding error that may have occurred during coord transforms.
138 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
139 GrClip::kBoundsTolerance);
Chris Dalton69824002017-10-31 00:37:52 -0600140 if (relaxedQueryBounds.isEmpty()) {
141 relaxedQueryBounds = queryBounds;
142 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700143
144 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
145 int numAAElements = 0;
146 while (InitialTriState::kUnknown == initialTriState) {
147 const Element* element = iter.prev();
148 if (nullptr == element) {
149 initialTriState = InitialTriState::kAllIn;
150 break;
151 }
152 if (SkClipStack::kEmptyGenID == element->getGenID()) {
153 initialTriState = InitialTriState::kAllOut;
154 break;
155 }
156 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
157 initialTriState = InitialTriState::kAllIn;
158 break;
159 }
160
161 bool skippable = false;
162 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
163
164 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500165 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700166 // check if the shape subtracted either contains the entire bounds (and makes
167 // the clip empty) or is outside the bounds and therefore can be skipped.
168 if (element->isInverseFilled()) {
169 if (element->contains(relaxedQueryBounds)) {
170 skippable = true;
171 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
172 initialTriState = InitialTriState::kAllOut;
173 skippable = true;
174 }
175 } else {
176 if (element->contains(relaxedQueryBounds)) {
177 initialTriState = InitialTriState::kAllOut;
178 skippable = true;
179 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
180 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600181 } else if (!embiggens) {
Chris Daltond8d15932017-11-01 19:21:24 +0000182 ClipResult result = this->clipOutsideElement(element, maxWindowRectangles);
Chris Dalton79471932017-10-27 01:50:57 -0600183 if (ClipResult::kMadeEmpty == result) {
184 return;
185 }
186 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700187 }
188 }
189 if (!skippable) {
190 emsmallens = true;
191 }
192 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500193 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700194 // check if the shape intersected contains the entire bounds and therefore can
195 // be skipped or it is outside the entire bounds and therefore makes the clip
196 // empty.
197 if (element->isInverseFilled()) {
198 if (element->contains(relaxedQueryBounds)) {
199 initialTriState = InitialTriState::kAllOut;
200 skippable = true;
201 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
202 skippable = true;
203 }
204 } else {
205 if (element->contains(relaxedQueryBounds)) {
206 skippable = true;
207 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
208 initialTriState = InitialTriState::kAllOut;
209 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600210 } else if (!embiggens) {
211 ClipResult result = this->clipInsideElement(element);
212 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700213 return;
214 }
Chris Dalton79471932017-10-27 01:50:57 -0600215 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700216 }
217 }
218 if (!skippable) {
219 emsmallens = true;
220 }
221 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500222 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700223 // If the union-ed shape contains the entire bounds then after this element
224 // the bounds is entirely inside the clip. If the union-ed shape is outside the
225 // bounds then this op can be skipped.
226 if (element->isInverseFilled()) {
227 if (element->contains(relaxedQueryBounds)) {
228 skippable = true;
229 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
230 initialTriState = InitialTriState::kAllIn;
231 skippable = true;
232 }
233 } else {
234 if (element->contains(relaxedQueryBounds)) {
235 initialTriState = InitialTriState::kAllIn;
236 skippable = true;
237 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
238 skippable = true;
239 }
240 }
241 if (!skippable) {
242 embiggens = true;
243 }
244 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500245 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700246 // If the bounds is entirely inside the shape being xor-ed then the effect is
247 // to flip the inside/outside state of every point in the bounds. We may be
248 // able to take advantage of this in the forward pass. If the xor-ed shape
249 // doesn't intersect the bounds then it can be skipped.
250 if (element->isInverseFilled()) {
251 if (element->contains(relaxedQueryBounds)) {
252 skippable = true;
253 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
254 isFlip = true;
255 }
256 } else {
257 if (element->contains(relaxedQueryBounds)) {
258 isFlip = true;
259 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
260 skippable = true;
261 }
262 }
263 if (!skippable) {
264 emsmallens = embiggens = true;
265 }
266 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500267 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700268 // When the bounds is entirely within the rev-diff shape then this behaves like xor
269 // and reverses every point inside the bounds. If the shape is completely outside
270 // the bounds then we know after this element is applied that the bounds will be
271 // all outside the current clip.B
272 if (element->isInverseFilled()) {
273 if (element->contains(relaxedQueryBounds)) {
274 initialTriState = InitialTriState::kAllOut;
275 skippable = true;
276 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
277 isFlip = true;
278 }
279 } else {
280 if (element->contains(relaxedQueryBounds)) {
281 isFlip = true;
282 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
283 initialTriState = InitialTriState::kAllOut;
284 skippable = true;
285 }
286 }
287 if (!skippable) {
288 emsmallens = embiggens = true;
289 }
290 break;
291
Mike Reedc1f77742016-12-09 09:00:50 -0500292 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700293 // Replace will always terminate our walk. We will either begin the forward walk
294 // at the replace op or detect here than the shape is either completely inside
295 // or completely outside the bounds. In this latter case it can be skipped by
296 // setting the correct value for initialTriState.
297 if (element->isInverseFilled()) {
298 if (element->contains(relaxedQueryBounds)) {
299 initialTriState = InitialTriState::kAllOut;
300 skippable = true;
301 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
302 initialTriState = InitialTriState::kAllIn;
303 skippable = true;
304 }
305 } else {
306 if (element->contains(relaxedQueryBounds)) {
307 initialTriState = InitialTriState::kAllIn;
308 skippable = true;
309 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
310 initialTriState = InitialTriState::kAllOut;
311 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600312 } else if (!embiggens) {
313 ClipResult result = this->clipInsideElement(element);
314 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700315 return;
316 }
Chris Dalton79471932017-10-27 01:50:57 -0600317 if (ClipResult::kClipped == result) {
318 initialTriState = InitialTriState::kAllIn;
319 skippable = true;
320 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700321 }
322 }
323 if (!skippable) {
324 initialTriState = InitialTriState::kAllOut;
325 embiggens = emsmallens = true;
326 }
327 break;
328 default:
329 SkDEBUGFAIL("Unexpected op.");
330 break;
331 }
332 if (!skippable) {
Chris Dalton79471932017-10-27 01:50:57 -0600333 if (fMaskElements.isEmpty()) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700334 // This will be the last element. Record the stricter genID.
Chris Dalton79471932017-10-27 01:50:57 -0600335 fMaskGenID = element->getGenID();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700336 }
337
338 // if it is a flip, change it to a bounds-filling rect
339 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500340 SkASSERT(kXOR_SkClipOp == element->getOp() ||
341 kReverseDifference_SkClipOp == element->getOp());
Chris Dalton79471932017-10-27 01:50:57 -0600342 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
343 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700344 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600345 Element* newElement = fMaskElements.addToHead(*element);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700346 if (newElement->isAA()) {
347 ++numAAElements;
348 }
349 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
350 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
351 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500352 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700353 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500354 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700355 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500356 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700357 if (isReplace) {
358 SkASSERT(InitialTriState::kAllOut == initialTriState);
359 initialTriState = InitialTriState::kAllIn;
360 }
361 }
362 }
363 }
364 }
365
366 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
367 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
Chris Dalton79471932017-10-27 01:50:57 -0600368 fMaskElements.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700369 numAAElements = 0;
370 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600371 Element* element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700372 while (element) {
373 bool skippable = false;
374 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500375 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700376 // subtracting from the empty set yields the empty set.
377 skippable = InitialTriState::kAllOut == initialTriState;
378 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500379 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700380 // intersecting with the empty set yields the empty set
381 if (InitialTriState::kAllOut == initialTriState) {
382 skippable = true;
383 } else {
384 // We can clear to zero and then simply draw the clip element.
385 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500386 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700387 }
388 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500389 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700390 if (InitialTriState::kAllIn == initialTriState) {
391 // unioning the infinite plane with anything is a no-op.
392 skippable = true;
393 } else {
394 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500395 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700396 }
397 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500398 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700399 if (InitialTriState::kAllOut == initialTriState) {
400 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500401 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700402 }
403 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500404 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700405 if (InitialTriState::kAllIn == initialTriState) {
406 // subtracting the whole plane will yield the empty set.
407 skippable = true;
408 initialTriState = InitialTriState::kAllOut;
409 } else {
410 // this picks up flips inserted in the backwards pass.
411 skippable = element->isInverseFilled() ?
412 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
413 element->contains(relaxedQueryBounds);
414 if (skippable) {
415 initialTriState = InitialTriState::kAllIn;
416 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500417 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700418 }
419 }
420 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500421 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700422 skippable = false; // we would have skipped it in the backwards walk if we
423 // could've.
424 break;
425 default:
426 SkDEBUGFAIL("Unexpected op.");
427 break;
428 }
429 if (!skippable) {
430 break;
431 } else {
432 if (element->isAA()) {
433 --numAAElements;
434 }
Chris Dalton79471932017-10-27 01:50:57 -0600435 fMaskElements.popHead();
436 element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700437 }
438 }
439 }
Chris Dalton79471932017-10-27 01:50:57 -0600440 fMaskRequiresAA = numAAElements > 0;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700441
442 SkASSERT(InitialTriState::kUnknown != initialTriState);
443 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
444}
445
Chris Dalton79471932017-10-27 01:50:57 -0600446GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
447 SkIRect elementIBounds;
448 if (!element->isAA()) {
449 element->getBounds().round(&elementIBounds);
450 } else {
451 elementIBounds = GrClip::GetPixelIBounds(element->getBounds());
452 }
453 SkASSERT(fHasScissor);
454 if (!fScissor.intersect(elementIBounds)) {
455 this->makeEmpty();
456 return ClipResult::kMadeEmpty;
457 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700458
Chris Dalton79471932017-10-27 01:50:57 -0600459 switch (element->getDeviceSpaceType()) {
460 case Element::DeviceSpaceType::kEmpty:
461 return ClipResult::kMadeEmpty;
462
463 case Element::DeviceSpaceType::kRect:
464 SkASSERT(element->getBounds() == element->getDeviceSpaceRect());
465 if (element->isAA()) {
466 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
467 fAAClipRect = element->getDeviceSpaceRect();
468 // fAAClipRectGenID is the value we should use for fMaskGenID if we end up
469 // moving the AA clip rect into the mask. The mask GenID is simply the topmost
470 // element's GenID. And since we walk the stack backwards, this means it's just
471 // the first element we don't skip during our walk.
472 fAAClipRectGenID = fMaskElements.isEmpty() ? element->getGenID() : fMaskGenID;
473 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
474 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
475 this->makeEmpty();
476 return ClipResult::kMadeEmpty;
477 }
478 }
479 return ClipResult::kClipped;
480
481 case Element::DeviceSpaceType::kRRect:
482 case Element::DeviceSpaceType::kPath:
Chris Daltond8d15932017-11-01 19:21:24 +0000483 return ClipResult::kNotClipped;
Chris Dalton79471932017-10-27 01:50:57 -0600484 }
485
486 SK_ABORT("Unexpected DeviceSpaceType");
487 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700488}
489
Chris Daltond8d15932017-11-01 19:21:24 +0000490GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element,
491 int maxWindowRectangles) {
492 if (fWindowRects.count() >= maxWindowRectangles) {
493 return ClipResult::kNotClipped;
494 }
495
Chris Dalton79471932017-10-27 01:50:57 -0600496 switch (element->getDeviceSpaceType()) {
497 case Element::DeviceSpaceType::kEmpty:
498 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700499
Chris Dalton79471932017-10-27 01:50:57 -0600500 case Element::DeviceSpaceType::kRect:
Chris Daltond8d15932017-11-01 19:21:24 +0000501 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones, but
502 // it saves processing time.
503 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
504 return !element->isAA() ? ClipResult::kClipped : ClipResult::kNotClipped;
Chris Dalton79471932017-10-27 01:50:57 -0600505
506 case Element::DeviceSpaceType::kRRect: {
Chris Daltond29e0da2017-11-01 11:40:58 -0600507 // Clip out the interiors of round rects with two window rectangles in the shape of a
Chris Daltond8d15932017-11-01 19:21:24 +0000508 // plus. It doesn't allow us to skip the clip element, but still saves processing time.
509 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700510 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
511 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
512 if (SkRRect::kComplex_Type == clipRRect.getType()) {
513 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
514 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
515 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
516 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
517 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
518 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
519 }
520 const SkRect& bounds = clipRRect.getBounds();
521 if (insetTL.x() + insetBR.x() >= bounds.width() ||
522 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Daltond8d15932017-11-01 19:21:24 +0000523 return ClipResult::kNotClipped; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700524 }
525
526 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
527 bounds.right(), bounds.bottom() - insetBR.y());
528 this->addWindowRectangle(horzRect, element->isAA());
Chris Daltond8d15932017-11-01 19:21:24 +0000529 if (fWindowRects.count() >= maxWindowRectangles) {
530 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700531 }
532
Chris Daltond8d15932017-11-01 19:21:24 +0000533 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
534 bounds.right() - insetBR.x(), bounds.bottom());
535 this->addWindowRectangle(vertRect, element->isAA());
536 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700537 }
Chris Dalton79471932017-10-27 01:50:57 -0600538
539 case Element::DeviceSpaceType::kPath:
Chris Daltond8d15932017-11-01 19:21:24 +0000540 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700541 }
Chris Dalton79471932017-10-27 01:50:57 -0600542
543 SK_ABORT("Unexpected DeviceSpaceType");
544 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700545}
546
547inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
548 SkIRect window;
549 if (!elementIsAA) {
550 elementInteriorRect.round(&window);
551 } else {
552 elementInteriorRect.roundIn(&window);
553 }
554 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
555 fWindowRects.addWindow(window);
556 }
557}
558
Chris Dalton79471932017-10-27 01:50:57 -0600559void GrReducedClip::makeEmpty() {
560 fHasScissor = false;
561 fAAClipRectGenID = SK_InvalidGenID;
562 fWindowRects.reset();
563 fMaskElements.reset();
564 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000565}
csmartdaltonbde96c62016-08-31 12:54:46 -0700566
567////////////////////////////////////////////////////////////////////////////////
568// Create a 8-bit clip mask in alpha
569
Brian Osman11052242016-10-27 14:47:55 -0400570static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700571 const GrFixedClip& clip,
572 const GrUserStencilSettings* ss,
573 const SkMatrix& viewMatrix,
574 const SkClipStack::Element* element) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500575 GrAA aa = GrBoolToAA(element->isAA());
Brian Salomonf3b46e52017-08-30 11:37:57 -0400576 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600577 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700578 SkDEBUGFAIL("Should never get here with an empty element.");
579 break;
Chris Dalton79471932017-10-27 01:50:57 -0600580 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400581 return rtc->priv().drawAndStencilRect(clip, ss, (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500582 element->isInverseFilled(), aa, viewMatrix,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400583 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700584 break;
585 default: {
586 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400587 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700588 if (path.isInverseFillType()) {
589 path.toggleInverseFillType();
590 }
591
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500592 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
593 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700594 break;
595 }
596 }
597
598 return false;
599}
600
Brian Osman11052242016-10-27 14:47:55 -0400601static void draw_element(GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500602 const GrClip& clip, // TODO: can this just always be WideOpen?
603 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500604 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700605 const SkMatrix& viewMatrix,
606 const SkClipStack::Element* element) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700607 // TODO: Draw rrects directly here.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400608 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600609 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700610 SkDEBUGFAIL("Should never get here with an empty element.");
611 break;
Chris Dalton79471932017-10-27 01:50:57 -0600612 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400613 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700614 break;
615 default: {
616 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400617 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700618 if (path.isInverseFillType()) {
619 path.toggleInverseFillType();
620 }
621
Brian Salomon82f44312017-01-11 13:42:54 -0500622 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700623 break;
624 }
625 }
626}
627
Brian Osman11052242016-10-27 14:47:55 -0400628bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700629 // The texture may be larger than necessary, this rect represents the part of the texture
630 // we populate with a rasterization of the clip.
Chris Dalton79471932017-10-27 01:50:57 -0600631 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700632
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700633 if (!fWindowRects.empty()) {
Chris Dalton79471932017-10-27 01:50:57 -0600634 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700635 GrWindowRectsState::Mode::kExclusive);
636 }
637
csmartdaltonbde96c62016-08-31 12:54:46 -0700638 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
639 // clear the part that we care about.
640 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400641 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700642
643 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
644 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600645 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700646
647 // walk through each clip element and perform its set op
Chris Dalton79471932017-10-27 01:50:57 -0600648 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700649 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700650 SkRegion::Op op = (SkRegion::Op)element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500651 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700652 bool invert = element->isInverseFilled();
653 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
654 // draw directly into the result with the stencil set to make the pixels affected
655 // by the clip shape be non-zero.
656 static constexpr GrUserStencilSettings kStencilInElement(
657 GrUserStencilSettings::StaticInit<
658 0xffff,
659 GrUserStencilTest::kAlways,
660 0xffff,
661 GrUserStencilOp::kReplace,
662 GrUserStencilOp::kReplace,
663 0xffff>()
664 );
Brian Osman11052242016-10-27 14:47:55 -0400665 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700666 return false;
667 }
668
669 // Draw to the exterior pixels (those with a zero stencil value).
670 static constexpr GrUserStencilSettings kDrawOutsideElement(
671 GrUserStencilSettings::StaticInit<
672 0x0000,
673 GrUserStencilTest::kEqual,
674 0xffff,
675 GrUserStencilOp::kZero,
676 GrUserStencilOp::kZero,
677 0xffff>()
678 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500679 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Chris Dalton79471932017-10-27 01:50:57 -0600680 translate, SkRect::Make(fScissor))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700681 return false;
682 }
683 } else {
684 // all the remaining ops can just be directly draw into the accumulation buffer
685 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700686 paint.setCoverageSetOpXPFactory(op, false);
687
Brian Salomon82f44312017-01-11 13:42:54 -0500688 draw_element(rtc, clip, std::move(paint), aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700689 }
690 }
691
692 return true;
693}
694
695////////////////////////////////////////////////////////////////////////////////
696// Create a 1-bit clip mask in the stencil buffer.
697
Chris Dalton428e5682017-11-07 18:24:06 +0000698class StencilClip final : public GrClip {
699public:
700 StencilClip(const SkIRect& scissorRect, uint32_t clipStackID)
701 : fFixedClip(scissorRect)
702 , fClipStackID(clipStackID) {
703 }
704
705 const GrFixedClip& fixedClip() const { return fFixedClip; }
706
707 void setWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
708 fFixedClip.setWindowRectangles(windows, mode);
709 }
710
711private:
712 bool quickContains(const SkRect&) const override {
713 return false;
714 }
715 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
716 fFixedClip.getConservativeBounds(width, height, bounds, iior);
717 }
718 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const override {
719 return false;
720 }
721 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
722 bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
723 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out,
724 bounds)) {
725 return false;
726 }
727 out->addStencilClip(fClipStackID);
728 return true;
729 }
730
731 GrFixedClip fFixedClip;
732 uint32_t fClipStackID;
733
734 typedef GrClip INHERITED;
735};
736
csmartdaltonbde96c62016-08-31 12:54:46 -0700737bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400738 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700739 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Chris Dalton428e5682017-11-07 18:24:06 +0000740 StencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700741
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700742 if (!fWindowRects.empty()) {
Chris Dalton428e5682017-11-07 18:24:06 +0000743 stencilClip.setWindowRectangles(fWindowRects, GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700744 }
745
csmartdaltonbde96c62016-08-31 12:54:46 -0700746 bool initialState = InitialState::kAllIn == this->initialState();
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000747 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700748
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500749 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600750 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700751 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500752 GrAAType aaType = GrAAType::kNone;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400753 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500754 aaType = GrAAType::kMSAA;
755 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700756
757 bool fillInverted = false;
758
759 // This will be used to determine whether the clip shape can be rendered into the
760 // stencil with arbitrary stencil settings.
761 GrPathRenderer::StencilSupport stencilSupport;
762
reed73603f32016-09-20 08:42:38 -0700763 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700764
765 GrPathRenderer* pr = nullptr;
766 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400767 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700768 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
769 fillInverted = false;
770 } else {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400771 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700772 fillInverted = clipPath.isInverseFillType();
773 if (fillInverted) {
774 clipPath.toggleInverseFillType();
775 }
776
777 GrShape shape(clipPath, GrStyle::SimpleFill());
778 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700779 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600780 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400781 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700782 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500783 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700784 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700785
786 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500787 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700788 &stencilSupport);
789 if (!pr) {
790 return false;
791 }
792 }
793
794 bool canRenderDirectToStencil =
795 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
796 bool drawDirectToClip; // Given the renderer, the element,
797 // fill rule, and set operation should
798 // we render the element directly to
799 // stencil bit used for clipping.
800 GrUserStencilSettings const* const* stencilPasses =
801 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
802 &drawDirectToClip);
803
804 // draw the element to the client stencil bits if necessary
805 if (!drawDirectToClip) {
806 static constexpr GrUserStencilSettings kDrawToStencil(
807 GrUserStencilSettings::StaticInit<
808 0x0000,
809 GrUserStencilTest::kAlways,
810 0xffff,
811 GrUserStencilOp::kIncMaybeClamp,
812 GrUserStencilOp::kIncMaybeClamp,
813 0xffff>()
814 );
Brian Salomonf3b46e52017-08-30 11:37:57 -0400815 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400816 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400817 aaType, SkMatrix::I(),
818 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700819 } else {
820 if (!clipPath.isEmpty()) {
821 GrShape shape(clipPath, GrStyle::SimpleFill());
822 if (canRenderDirectToStencil) {
823 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500824 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700825
Robert Phillips256c37b2017-03-01 14:32:46 -0500826 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500827 std::move(paint),
828 &kDrawToStencil,
829 renderTargetContext,
830 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600831 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400832 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500833 &shape,
834 aaType,
835 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700836 pr->drawPath(args);
837 } else {
838 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500839 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400840 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700841 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600842 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400843 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500844 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700845 args.fShape = &shape;
846 pr->stencilPath(args);
847 }
848 }
849 }
850 }
851
852 // now we modify the clip bit by rendering either the clip
853 // element directly or a bounding rect of the entire clip.
854 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
855 if (drawDirectToClip) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400856 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400857 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400858 SkMatrix::I(),
859 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700860 } else {
861 GrShape shape(clipPath, GrStyle::SimpleFill());
862 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500863 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500864 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500865 std::move(paint),
866 *pass,
867 renderTargetContext,
868 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600869 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400870 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500871 &shape,
872 aaType,
873 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700874 pr->drawPath(args);
875 }
876 } else {
877 // The view matrix is setup to do clip space -> stencil space translation, so
878 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400879 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600880 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700881 }
882 }
883 }
884 return true;
885}