blob: 29f483022bbca9b0bbe4f11c5d283b44ce5bad45 [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.
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
99 // 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 Dalton79471932017-10-27 01:50:57 -0600104 if (SK_InvalidGenID != fAAClipRectGenID) { // Is there an AA clip rect?
105 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;
114 fAAClipRectGenID = SK_InvalidGenID;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700115 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700116}
117
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700118void 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);
140
141 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
142 int numAAElements = 0;
143 while (InitialTriState::kUnknown == initialTriState) {
144 const Element* element = iter.prev();
145 if (nullptr == element) {
146 initialTriState = InitialTriState::kAllIn;
147 break;
148 }
149 if (SkClipStack::kEmptyGenID == element->getGenID()) {
150 initialTriState = InitialTriState::kAllOut;
151 break;
152 }
153 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
154 initialTriState = InitialTriState::kAllIn;
155 break;
156 }
157
158 bool skippable = false;
159 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
160
161 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500162 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700163 // check if the shape subtracted either contains the entire bounds (and makes
164 // the clip empty) or is outside the bounds and therefore can be skipped.
165 if (element->isInverseFilled()) {
166 if (element->contains(relaxedQueryBounds)) {
167 skippable = true;
168 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
169 initialTriState = InitialTriState::kAllOut;
170 skippable = true;
171 }
172 } else {
173 if (element->contains(relaxedQueryBounds)) {
174 initialTriState = InitialTriState::kAllOut;
175 skippable = true;
176 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
177 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600178 } else if (!embiggens) {
179 ClipResult result = this->clipOutsideElement(element, maxWindowRectangles);
180 if (ClipResult::kMadeEmpty == result) {
181 return;
182 }
183 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700184 }
185 }
186 if (!skippable) {
187 emsmallens = true;
188 }
189 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500190 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700191 // check if the shape intersected contains the entire bounds and therefore can
192 // be skipped or it is outside the entire bounds and therefore makes the clip
193 // empty.
194 if (element->isInverseFilled()) {
195 if (element->contains(relaxedQueryBounds)) {
196 initialTriState = InitialTriState::kAllOut;
197 skippable = true;
198 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
199 skippable = true;
200 }
201 } else {
202 if (element->contains(relaxedQueryBounds)) {
203 skippable = true;
204 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
205 initialTriState = InitialTriState::kAllOut;
206 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600207 } else if (!embiggens) {
208 ClipResult result = this->clipInsideElement(element);
209 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700210 return;
211 }
Chris Dalton79471932017-10-27 01:50:57 -0600212 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700213 }
214 }
215 if (!skippable) {
216 emsmallens = true;
217 }
218 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500219 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700220 // If the union-ed shape contains the entire bounds then after this element
221 // the bounds is entirely inside the clip. If the union-ed shape is outside the
222 // bounds then this op can be skipped.
223 if (element->isInverseFilled()) {
224 if (element->contains(relaxedQueryBounds)) {
225 skippable = true;
226 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
227 initialTriState = InitialTriState::kAllIn;
228 skippable = true;
229 }
230 } else {
231 if (element->contains(relaxedQueryBounds)) {
232 initialTriState = InitialTriState::kAllIn;
233 skippable = true;
234 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
235 skippable = true;
236 }
237 }
238 if (!skippable) {
239 embiggens = true;
240 }
241 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500242 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700243 // If the bounds is entirely inside the shape being xor-ed then the effect is
244 // to flip the inside/outside state of every point in the bounds. We may be
245 // able to take advantage of this in the forward pass. If the xor-ed shape
246 // doesn't intersect the bounds then it can be skipped.
247 if (element->isInverseFilled()) {
248 if (element->contains(relaxedQueryBounds)) {
249 skippable = true;
250 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
251 isFlip = true;
252 }
253 } else {
254 if (element->contains(relaxedQueryBounds)) {
255 isFlip = true;
256 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
257 skippable = true;
258 }
259 }
260 if (!skippable) {
261 emsmallens = embiggens = true;
262 }
263 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500264 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700265 // When the bounds is entirely within the rev-diff shape then this behaves like xor
266 // and reverses every point inside the bounds. If the shape is completely outside
267 // the bounds then we know after this element is applied that the bounds will be
268 // all outside the current clip.B
269 if (element->isInverseFilled()) {
270 if (element->contains(relaxedQueryBounds)) {
271 initialTriState = InitialTriState::kAllOut;
272 skippable = true;
273 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
274 isFlip = true;
275 }
276 } else {
277 if (element->contains(relaxedQueryBounds)) {
278 isFlip = true;
279 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
280 initialTriState = InitialTriState::kAllOut;
281 skippable = true;
282 }
283 }
284 if (!skippable) {
285 emsmallens = embiggens = true;
286 }
287 break;
288
Mike Reedc1f77742016-12-09 09:00:50 -0500289 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700290 // Replace will always terminate our walk. We will either begin the forward walk
291 // at the replace op or detect here than the shape is either completely inside
292 // or completely outside the bounds. In this latter case it can be skipped by
293 // setting the correct value for initialTriState.
294 if (element->isInverseFilled()) {
295 if (element->contains(relaxedQueryBounds)) {
296 initialTriState = InitialTriState::kAllOut;
297 skippable = true;
298 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
299 initialTriState = InitialTriState::kAllIn;
300 skippable = true;
301 }
302 } else {
303 if (element->contains(relaxedQueryBounds)) {
304 initialTriState = InitialTriState::kAllIn;
305 skippable = true;
306 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
307 initialTriState = InitialTriState::kAllOut;
308 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600309 } else if (!embiggens) {
310 ClipResult result = this->clipInsideElement(element);
311 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700312 return;
313 }
Chris Dalton79471932017-10-27 01:50:57 -0600314 if (ClipResult::kClipped == result) {
315 initialTriState = InitialTriState::kAllIn;
316 skippable = true;
317 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700318 }
319 }
320 if (!skippable) {
321 initialTriState = InitialTriState::kAllOut;
322 embiggens = emsmallens = true;
323 }
324 break;
325 default:
326 SkDEBUGFAIL("Unexpected op.");
327 break;
328 }
329 if (!skippable) {
Chris Dalton79471932017-10-27 01:50:57 -0600330 if (fMaskElements.isEmpty()) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700331 // This will be the last element. Record the stricter genID.
Chris Dalton79471932017-10-27 01:50:57 -0600332 fMaskGenID = element->getGenID();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700333 }
334
335 // if it is a flip, change it to a bounds-filling rect
336 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500337 SkASSERT(kXOR_SkClipOp == element->getOp() ||
338 kReverseDifference_SkClipOp == element->getOp());
Chris Dalton79471932017-10-27 01:50:57 -0600339 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
340 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700341 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600342 Element* newElement = fMaskElements.addToHead(*element);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700343 if (newElement->isAA()) {
344 ++numAAElements;
345 }
346 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
347 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
348 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500349 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700350 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500351 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700352 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500353 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700354 if (isReplace) {
355 SkASSERT(InitialTriState::kAllOut == initialTriState);
356 initialTriState = InitialTriState::kAllIn;
357 }
358 }
359 }
360 }
361 }
362
363 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
364 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
Chris Dalton79471932017-10-27 01:50:57 -0600365 fMaskElements.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700366 numAAElements = 0;
367 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600368 Element* element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700369 while (element) {
370 bool skippable = false;
371 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500372 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700373 // subtracting from the empty set yields the empty set.
374 skippable = InitialTriState::kAllOut == initialTriState;
375 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500376 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700377 // intersecting with the empty set yields the empty set
378 if (InitialTriState::kAllOut == initialTriState) {
379 skippable = true;
380 } else {
381 // We can clear to zero and then simply draw the clip element.
382 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500383 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700384 }
385 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500386 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700387 if (InitialTriState::kAllIn == initialTriState) {
388 // unioning the infinite plane with anything is a no-op.
389 skippable = true;
390 } else {
391 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500392 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700393 }
394 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500395 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700396 if (InitialTriState::kAllOut == initialTriState) {
397 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500398 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700399 }
400 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500401 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700402 if (InitialTriState::kAllIn == initialTriState) {
403 // subtracting the whole plane will yield the empty set.
404 skippable = true;
405 initialTriState = InitialTriState::kAllOut;
406 } else {
407 // this picks up flips inserted in the backwards pass.
408 skippable = element->isInverseFilled() ?
409 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
410 element->contains(relaxedQueryBounds);
411 if (skippable) {
412 initialTriState = InitialTriState::kAllIn;
413 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500414 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700415 }
416 }
417 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500418 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700419 skippable = false; // we would have skipped it in the backwards walk if we
420 // could've.
421 break;
422 default:
423 SkDEBUGFAIL("Unexpected op.");
424 break;
425 }
426 if (!skippable) {
427 break;
428 } else {
429 if (element->isAA()) {
430 --numAAElements;
431 }
Chris Dalton79471932017-10-27 01:50:57 -0600432 fMaskElements.popHead();
433 element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700434 }
435 }
436 }
Chris Dalton79471932017-10-27 01:50:57 -0600437 fMaskRequiresAA = numAAElements > 0;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700438
439 SkASSERT(InitialTriState::kUnknown != initialTriState);
440 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
441}
442
Chris Dalton79471932017-10-27 01:50:57 -0600443GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
444 SkIRect elementIBounds;
445 if (!element->isAA()) {
446 element->getBounds().round(&elementIBounds);
447 } else {
448 elementIBounds = GrClip::GetPixelIBounds(element->getBounds());
449 }
450 SkASSERT(fHasScissor);
451 if (!fScissor.intersect(elementIBounds)) {
452 this->makeEmpty();
453 return ClipResult::kMadeEmpty;
454 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700455
Chris Dalton79471932017-10-27 01:50:57 -0600456 switch (element->getDeviceSpaceType()) {
457 case Element::DeviceSpaceType::kEmpty:
458 return ClipResult::kMadeEmpty;
459
460 case Element::DeviceSpaceType::kRect:
461 SkASSERT(element->getBounds() == element->getDeviceSpaceRect());
462 if (element->isAA()) {
463 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
464 fAAClipRect = element->getDeviceSpaceRect();
465 // fAAClipRectGenID is the value we should use for fMaskGenID if we end up
466 // moving the AA clip rect into the mask. The mask GenID is simply the topmost
467 // element's GenID. And since we walk the stack backwards, this means it's just
468 // the first element we don't skip during our walk.
469 fAAClipRectGenID = fMaskElements.isEmpty() ? element->getGenID() : fMaskGenID;
470 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
471 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
472 this->makeEmpty();
473 return ClipResult::kMadeEmpty;
474 }
475 }
476 return ClipResult::kClipped;
477
478 case Element::DeviceSpaceType::kRRect:
479 case Element::DeviceSpaceType::kPath:
480 return ClipResult::kNotClipped;
481 }
482
483 SK_ABORT("Unexpected DeviceSpaceType");
484 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700485}
486
Chris Dalton79471932017-10-27 01:50:57 -0600487GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element,
488 int maxWindowRectangles) {
489 if (fWindowRects.count() >= maxWindowRectangles) {
490 return ClipResult::kNotClipped;
491 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700492
Chris Dalton79471932017-10-27 01:50:57 -0600493 switch (element->getDeviceSpaceType()) {
494 case Element::DeviceSpaceType::kEmpty:
495 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700496
Chris Dalton79471932017-10-27 01:50:57 -0600497 case Element::DeviceSpaceType::kRect:
498 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones, but
499 // it saves processing time.
500 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
501 return !element->isAA() ? ClipResult::kClipped : ClipResult::kNotClipped;
502
503 case Element::DeviceSpaceType::kRRect: {
504 // Clip out the interiors of round rects with two window rectangles in the shape of a
505 // plus. It doesn't allow us to skip the clip element, but still saves processing time.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400506 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700507 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
508 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
509 if (SkRRect::kComplex_Type == clipRRect.getType()) {
510 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
511 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
512 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
513 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
514 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
515 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
516 }
517 const SkRect& bounds = clipRRect.getBounds();
518 if (insetTL.x() + insetBR.x() >= bounds.width() ||
519 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Dalton79471932017-10-27 01:50:57 -0600520 return ClipResult::kNotClipped; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700521 }
522
523 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
524 bounds.right(), bounds.bottom() - insetBR.y());
525 this->addWindowRectangle(horzRect, element->isAA());
526 if (fWindowRects.count() >= maxWindowRectangles) {
Chris Dalton79471932017-10-27 01:50:57 -0600527 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700528 }
529
530 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
531 bounds.right() - insetBR.x(), bounds.bottom());
532 this->addWindowRectangle(vertRect, element->isAA());
Chris Dalton79471932017-10-27 01:50:57 -0600533 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700534 }
Chris Dalton79471932017-10-27 01:50:57 -0600535
536 case Element::DeviceSpaceType::kPath:
537 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700538 }
Chris Dalton79471932017-10-27 01:50:57 -0600539
540 SK_ABORT("Unexpected DeviceSpaceType");
541 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700542}
543
544inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
545 SkIRect window;
546 if (!elementIsAA) {
547 elementInteriorRect.round(&window);
548 } else {
549 elementInteriorRect.roundIn(&window);
550 }
551 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
552 fWindowRects.addWindow(window);
553 }
554}
555
Chris Dalton79471932017-10-27 01:50:57 -0600556void GrReducedClip::makeEmpty() {
557 fHasScissor = false;
558 fAAClipRectGenID = SK_InvalidGenID;
559 fWindowRects.reset();
560 fMaskElements.reset();
561 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000562}
csmartdaltonbde96c62016-08-31 12:54:46 -0700563
564////////////////////////////////////////////////////////////////////////////////
565// Create a 8-bit clip mask in alpha
566
Brian Osman11052242016-10-27 14:47:55 -0400567static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700568 const GrFixedClip& clip,
569 const GrUserStencilSettings* ss,
570 const SkMatrix& viewMatrix,
571 const SkClipStack::Element* element) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500572 GrAA aa = GrBoolToAA(element->isAA());
Brian Salomonf3b46e52017-08-30 11:37:57 -0400573 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600574 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700575 SkDEBUGFAIL("Should never get here with an empty element.");
576 break;
Chris Dalton79471932017-10-27 01:50:57 -0600577 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400578 return rtc->priv().drawAndStencilRect(clip, ss, (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500579 element->isInverseFilled(), aa, viewMatrix,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400580 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700581 break;
582 default: {
583 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400584 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700585 if (path.isInverseFillType()) {
586 path.toggleInverseFillType();
587 }
588
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500589 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
590 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700591 break;
592 }
593 }
594
595 return false;
596}
597
Brian Osman11052242016-10-27 14:47:55 -0400598static void draw_element(GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500599 const GrClip& clip, // TODO: can this just always be WideOpen?
600 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500601 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700602 const SkMatrix& viewMatrix,
603 const SkClipStack::Element* element) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700604 // TODO: Draw rrects directly here.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400605 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600606 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700607 SkDEBUGFAIL("Should never get here with an empty element.");
608 break;
Chris Dalton79471932017-10-27 01:50:57 -0600609 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400610 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700611 break;
612 default: {
613 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400614 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700615 if (path.isInverseFillType()) {
616 path.toggleInverseFillType();
617 }
618
Brian Salomon82f44312017-01-11 13:42:54 -0500619 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700620 break;
621 }
622 }
623}
624
Brian Osman11052242016-10-27 14:47:55 -0400625bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700626 // The texture may be larger than necessary, this rect represents the part of the texture
627 // we populate with a rasterization of the clip.
Chris Dalton79471932017-10-27 01:50:57 -0600628 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700629
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700630 if (!fWindowRects.empty()) {
Chris Dalton79471932017-10-27 01:50:57 -0600631 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700632 GrWindowRectsState::Mode::kExclusive);
633 }
634
csmartdaltonbde96c62016-08-31 12:54:46 -0700635 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
636 // clear the part that we care about.
637 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400638 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700639
640 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
641 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600642 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700643
644 // walk through each clip element and perform its set op
Chris Dalton79471932017-10-27 01:50:57 -0600645 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700646 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700647 SkRegion::Op op = (SkRegion::Op)element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500648 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700649 bool invert = element->isInverseFilled();
650 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
651 // draw directly into the result with the stencil set to make the pixels affected
652 // by the clip shape be non-zero.
653 static constexpr GrUserStencilSettings kStencilInElement(
654 GrUserStencilSettings::StaticInit<
655 0xffff,
656 GrUserStencilTest::kAlways,
657 0xffff,
658 GrUserStencilOp::kReplace,
659 GrUserStencilOp::kReplace,
660 0xffff>()
661 );
Brian Osman11052242016-10-27 14:47:55 -0400662 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700663 return false;
664 }
665
666 // Draw to the exterior pixels (those with a zero stencil value).
667 static constexpr GrUserStencilSettings kDrawOutsideElement(
668 GrUserStencilSettings::StaticInit<
669 0x0000,
670 GrUserStencilTest::kEqual,
671 0xffff,
672 GrUserStencilOp::kZero,
673 GrUserStencilOp::kZero,
674 0xffff>()
675 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500676 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Chris Dalton79471932017-10-27 01:50:57 -0600677 translate, SkRect::Make(fScissor))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700678 return false;
679 }
680 } else {
681 // all the remaining ops can just be directly draw into the accumulation buffer
682 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700683 paint.setCoverageSetOpXPFactory(op, false);
684
Brian Salomon82f44312017-01-11 13:42:54 -0500685 draw_element(rtc, clip, std::move(paint), aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700686 }
687 }
688
689 return true;
690}
691
692////////////////////////////////////////////////////////////////////////////////
693// Create a 1-bit clip mask in the stencil buffer.
694
695class StencilClip final : public GrClip {
696public:
Robert Phillips806be2d2017-06-28 15:23:59 -0400697 StencilClip(const SkIRect& scissorRect, uint32_t clipStackID)
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400698 : fFixedClip(scissorRect)
699 , fClipStackID(clipStackID) {
700 }
701
csmartdaltonbde96c62016-08-31 12:54:46 -0700702 const GrFixedClip& fixedClip() const { return fFixedClip; }
703
Brian Salomon9a767722017-03-13 17:57:28 -0400704 void setWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
705 fFixedClip.setWindowRectangles(windows, mode);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700706 }
707
csmartdaltonbde96c62016-08-31 12:54:46 -0700708private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700709 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700710 return false;
711 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700712 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
713 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700714 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500715 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700716 return false;
717 }
Brian Osman11052242016-10-27 14:47:55 -0400718 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
Brian Salomon97180af2017-03-14 13:42:58 -0400719 bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
720 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out,
721 bounds)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700722 return false;
723 }
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400724 out->addStencilClip(fClipStackID);
csmartdaltonbde96c62016-08-31 12:54:46 -0700725 return true;
726 }
727
728 GrFixedClip fFixedClip;
Robert Phillips806be2d2017-06-28 15:23:59 -0400729 uint32_t fClipStackID;
csmartdaltonbde96c62016-08-31 12:54:46 -0700730
731 typedef GrClip INHERITED;
732};
733
734bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400735 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700736 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Chris Dalton79471932017-10-27 01:50:57 -0600737 StencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700738
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700739 if (!fWindowRects.empty()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400740 stencilClip.setWindowRectangles(fWindowRects, GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700741 }
742
csmartdaltonbde96c62016-08-31 12:54:46 -0700743 bool initialState = InitialState::kAllIn == this->initialState();
Brian Osman693a5402016-10-27 15:13:22 -0400744 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700745
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500746 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600747 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700748 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500749 GrAAType aaType = GrAAType::kNone;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400750 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500751 aaType = GrAAType::kMSAA;
752 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700753
754 bool fillInverted = false;
755
756 // This will be used to determine whether the clip shape can be rendered into the
757 // stencil with arbitrary stencil settings.
758 GrPathRenderer::StencilSupport stencilSupport;
759
reed73603f32016-09-20 08:42:38 -0700760 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700761
762 GrPathRenderer* pr = nullptr;
763 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400764 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700765 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
766 fillInverted = false;
767 } else {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400768 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700769 fillInverted = clipPath.isInverseFillType();
770 if (fillInverted) {
771 clipPath.toggleInverseFillType();
772 }
773
774 GrShape shape(clipPath, GrStyle::SimpleFill());
775 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700776 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600777 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400778 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700779 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500780 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700781 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700782
783 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500784 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700785 &stencilSupport);
786 if (!pr) {
787 return false;
788 }
789 }
790
791 bool canRenderDirectToStencil =
792 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
793 bool drawDirectToClip; // Given the renderer, the element,
794 // fill rule, and set operation should
795 // we render the element directly to
796 // stencil bit used for clipping.
797 GrUserStencilSettings const* const* stencilPasses =
798 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
799 &drawDirectToClip);
800
801 // draw the element to the client stencil bits if necessary
802 if (!drawDirectToClip) {
803 static constexpr GrUserStencilSettings kDrawToStencil(
804 GrUserStencilSettings::StaticInit<
805 0x0000,
806 GrUserStencilTest::kAlways,
807 0xffff,
808 GrUserStencilOp::kIncMaybeClamp,
809 GrUserStencilOp::kIncMaybeClamp,
810 0xffff>()
811 );
Brian Salomonf3b46e52017-08-30 11:37:57 -0400812 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400813 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400814 aaType, SkMatrix::I(),
815 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700816 } else {
817 if (!clipPath.isEmpty()) {
818 GrShape shape(clipPath, GrStyle::SimpleFill());
819 if (canRenderDirectToStencil) {
820 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500821 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700822
Robert Phillips256c37b2017-03-01 14:32:46 -0500823 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500824 std::move(paint),
825 &kDrawToStencil,
826 renderTargetContext,
827 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600828 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400829 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500830 &shape,
831 aaType,
832 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700833 pr->drawPath(args);
834 } else {
835 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500836 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400837 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700838 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600839 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400840 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500841 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700842 args.fShape = &shape;
843 pr->stencilPath(args);
844 }
845 }
846 }
847 }
848
849 // now we modify the clip bit by rendering either the clip
850 // element directly or a bounding rect of the entire clip.
851 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
852 if (drawDirectToClip) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400853 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400854 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400855 SkMatrix::I(),
856 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700857 } else {
858 GrShape shape(clipPath, GrStyle::SimpleFill());
859 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500860 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500861 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500862 std::move(paint),
863 *pass,
864 renderTargetContext,
865 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600866 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400867 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500868 &shape,
869 aaType,
870 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700871 pr->drawPath(args);
872 }
873 } else {
874 // The view matrix is setup to do clip space -> stencil space translation, so
875 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400876 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600877 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700878 }
879 }
880 }
881 return true;
882}