blob: 89690d3663e6049b505769cde0bb32b78612551a [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"
14#include "GrDrawContext.h"
15#include "GrDrawContextPriv.h"
16#include "GrDrawingManager.h"
17#include "GrFixedClip.h"
18#include "GrPathRenderer.h"
19#include "GrStyle.h"
20#include "GrUserStencilSettings.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070021
bsalomon@google.com170bd792012-12-05 22:26:11 +000022typedef SkClipStack::Element Element;
bsalomon@google.com170bd792012-12-05 22:26:11 +000023
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());
csmartdaltond211e782016-08-15 11:17:19 -070034 fHasIBounds = false;
csmartdaltoncbecb082016-07-22 08:59:08 -070035
bsalomon@google.com170bd792012-12-05 22:26:11 +000036 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070037 fInitialState = InitialState::kAllIn;
38 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000039 }
40
41 SkClipStack::BoundsType stackBoundsType;
42 SkRect stackBounds;
43 bool iior;
44 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
45
csmartdaltoncbecb082016-07-22 08:59:08 -070046 if (stackBounds.isEmpty() || GrClip::IsOutsideClip(stackBounds, queryBounds)) {
47 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070048 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
49 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000050 }
51
csmartdaltoncbecb082016-07-22 08:59:08 -070052 if (iior) {
53 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
54 // This should only be true if aa/non-aa status matches among all elements.
55 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
56 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
57 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
58 // The clip is a non-aa rect. This is the one spot where we can actually implement the
csmartdalton77f2fae2016-08-08 09:55:06 -070059 // clip (using fIBounds) rather than just telling the caller what it should be.
60 stackBounds.round(&fIBounds);
csmartdaltond211e782016-08-15 11:17:19 -070061 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070062 fInitialState = fIBounds.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
63 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));
72 fIBounds = GrClip::GetPixelIBounds(tightBounds);
csmartdalton77f2fae2016-08-08 09:55:06 -070073 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070074 fHasIBounds = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070075
csmartdalton77f2fae2016-08-08 09:55:06 -070076 // Implement the clip with an AA rect element.
77 fElements.addToHead(stackBounds, SkRegion::kReplace_Op, true/*doAA*/);
csmartdalton8d3f92a2016-08-17 09:39:38 -070078 fElementsGenID = stack.getTopmostGenID();
csmartdalton77f2fae2016-08-08 09:55:06 -070079 fRequiresAA = true;
80
81 fInitialState = InitialState::kAllOut;
82 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070083 }
84
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 new
csmartdalton77f2fae2016-08-08 09:55:06 -070088 // clip will be enforced by the scissor through fIBounds.)
csmartdaltoncbecb082016-07-22 08:59:08 -070089 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
csmartdaltoncbecb082016-07-22 08:59:08 -070090 }
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000091
csmartdaltond211e782016-08-15 11:17:19 -070092 fIBounds = GrClip::GetPixelIBounds(tighterQuery);
csmartdalton77f2fae2016-08-08 09:55:06 -070093 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070094 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070095
bsalomon@google.com34cd70a2012-12-06 14:23:20 +000096 // Now that we have determined the bounds to use and filtered out the trivial cases, call the
97 // helper that actually walks the stack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -070098 this->walkStack(stack, tighterQuery, maxWindowRectangles);
99
100 if (fWindowRects.count() < maxWindowRectangles) {
101 this->addInteriorWindowRectangles(maxWindowRectangles);
102 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700103}
104
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700105void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
106 int maxWindowRectangles) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700107 // walk backwards until we get to:
108 // a) the beginning
109 // b) an operation that is known to make the bounds all inside/outside
110 // c) a replace operation
111
112 enum class InitialTriState {
113 kUnknown = -1,
114 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
115 kAllOut = (int)GrReducedClip::InitialState::kAllOut
116 } initialTriState = InitialTriState::kUnknown;
117
118 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
119 // TODO: track these per saved clip so that we can consider them on the forward pass.
120 bool embiggens = false;
121 bool emsmallens = false;
122
123 // We use a slightly relaxed set of query bounds for element containment tests. This is to
124 // account for floating point rounding error that may have occurred during coord transforms.
125 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
126 GrClip::kBoundsTolerance);
127
128 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
129 int numAAElements = 0;
130 while (InitialTriState::kUnknown == initialTriState) {
131 const Element* element = iter.prev();
132 if (nullptr == element) {
133 initialTriState = InitialTriState::kAllIn;
134 break;
135 }
136 if (SkClipStack::kEmptyGenID == element->getGenID()) {
137 initialTriState = InitialTriState::kAllOut;
138 break;
139 }
140 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
141 initialTriState = InitialTriState::kAllIn;
142 break;
143 }
144
145 bool skippable = false;
146 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
147
148 switch (element->getOp()) {
149 case SkRegion::kDifference_Op:
150 // check if the shape subtracted either contains the entire bounds (and makes
151 // the clip empty) or is outside the bounds and therefore can be skipped.
152 if (element->isInverseFilled()) {
153 if (element->contains(relaxedQueryBounds)) {
154 skippable = true;
155 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
156 initialTriState = InitialTriState::kAllOut;
157 skippable = true;
158 }
159 } else {
160 if (element->contains(relaxedQueryBounds)) {
161 initialTriState = InitialTriState::kAllOut;
162 skippable = true;
163 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
164 skippable = true;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700165 } else if (fWindowRects.count() < maxWindowRectangles && !embiggens &&
166 !element->isAA() && Element::kRect_Type == element->getType()) {
167 this->addWindowRectangle(element->getRect(), false);
168 skippable = true;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700169 }
170 }
171 if (!skippable) {
172 emsmallens = true;
173 }
174 break;
175 case SkRegion::kIntersect_Op:
176 // check if the shape intersected contains the entire bounds and therefore can
177 // be skipped or it is outside the entire bounds and therefore makes the clip
178 // empty.
179 if (element->isInverseFilled()) {
180 if (element->contains(relaxedQueryBounds)) {
181 initialTriState = InitialTriState::kAllOut;
182 skippable = true;
183 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
184 skippable = true;
185 }
186 } else {
187 if (element->contains(relaxedQueryBounds)) {
188 skippable = true;
189 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
190 initialTriState = InitialTriState::kAllOut;
191 skippable = true;
192 } else if (!embiggens && !element->isAA() &&
193 Element::kRect_Type == element->getType()) {
194 // fIBounds and queryBounds have already acccounted for this element via
195 // clip stack bounds; here we just apply the non-aa rounding effect.
196 SkIRect nonaaRect;
197 element->getRect().round(&nonaaRect);
198 if (!this->intersectIBounds(nonaaRect)) {
199 return;
200 }
201 skippable = true;
202 }
203 }
204 if (!skippable) {
205 emsmallens = true;
206 }
207 break;
208 case SkRegion::kUnion_Op:
209 // If the union-ed shape contains the entire bounds then after this element
210 // the bounds is entirely inside the clip. If the union-ed shape is outside the
211 // bounds then this op can be skipped.
212 if (element->isInverseFilled()) {
213 if (element->contains(relaxedQueryBounds)) {
214 skippable = true;
215 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
216 initialTriState = InitialTriState::kAllIn;
217 skippable = true;
218 }
219 } else {
220 if (element->contains(relaxedQueryBounds)) {
221 initialTriState = InitialTriState::kAllIn;
222 skippable = true;
223 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
224 skippable = true;
225 }
226 }
227 if (!skippable) {
228 embiggens = true;
229 }
230 break;
231 case SkRegion::kXOR_Op:
232 // If the bounds is entirely inside the shape being xor-ed then the effect is
233 // to flip the inside/outside state of every point in the bounds. We may be
234 // able to take advantage of this in the forward pass. If the xor-ed shape
235 // doesn't intersect the bounds then it can be skipped.
236 if (element->isInverseFilled()) {
237 if (element->contains(relaxedQueryBounds)) {
238 skippable = true;
239 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
240 isFlip = true;
241 }
242 } else {
243 if (element->contains(relaxedQueryBounds)) {
244 isFlip = true;
245 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
246 skippable = true;
247 }
248 }
249 if (!skippable) {
250 emsmallens = embiggens = true;
251 }
252 break;
253 case SkRegion::kReverseDifference_Op:
254 // When the bounds is entirely within the rev-diff shape then this behaves like xor
255 // and reverses every point inside the bounds. If the shape is completely outside
256 // the bounds then we know after this element is applied that the bounds will be
257 // all outside the current clip.B
258 if (element->isInverseFilled()) {
259 if (element->contains(relaxedQueryBounds)) {
260 initialTriState = InitialTriState::kAllOut;
261 skippable = true;
262 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
263 isFlip = true;
264 }
265 } else {
266 if (element->contains(relaxedQueryBounds)) {
267 isFlip = true;
268 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
269 initialTriState = InitialTriState::kAllOut;
270 skippable = true;
271 }
272 }
273 if (!skippable) {
274 emsmallens = embiggens = true;
275 }
276 break;
277
278 case SkRegion::kReplace_Op:
279 // Replace will always terminate our walk. We will either begin the forward walk
280 // at the replace op or detect here than the shape is either completely inside
281 // or completely outside the bounds. In this latter case it can be skipped by
282 // setting the correct value for initialTriState.
283 if (element->isInverseFilled()) {
284 if (element->contains(relaxedQueryBounds)) {
285 initialTriState = InitialTriState::kAllOut;
286 skippable = true;
287 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
288 initialTriState = InitialTriState::kAllIn;
289 skippable = true;
290 }
291 } else {
292 if (element->contains(relaxedQueryBounds)) {
293 initialTriState = InitialTriState::kAllIn;
294 skippable = true;
295 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
296 initialTriState = InitialTriState::kAllOut;
297 skippable = true;
298 } else if (!embiggens && !element->isAA() &&
299 Element::kRect_Type == element->getType()) {
300 // fIBounds and queryBounds have already acccounted for this element via
301 // clip stack bounds; here we just apply the non-aa rounding effect.
302 SkIRect nonaaRect;
303 element->getRect().round(&nonaaRect);
304 if (!this->intersectIBounds(nonaaRect)) {
305 return;
306 }
307 initialTriState = InitialTriState::kAllIn;
308 skippable = true;
309 }
310 }
311 if (!skippable) {
312 initialTriState = InitialTriState::kAllOut;
313 embiggens = emsmallens = true;
314 }
315 break;
316 default:
317 SkDEBUGFAIL("Unexpected op.");
318 break;
319 }
320 if (!skippable) {
321 if (0 == fElements.count()) {
322 // This will be the last element. Record the stricter genID.
323 fElementsGenID = element->getGenID();
324 }
325
326 // if it is a flip, change it to a bounds-filling rect
327 if (isFlip) {
328 SkASSERT(SkRegion::kXOR_Op == element->getOp() ||
329 SkRegion::kReverseDifference_Op == element->getOp());
330 fElements.addToHead(SkRect::Make(fIBounds), SkRegion::kReverseDifference_Op, false);
331 } else {
332 Element* newElement = fElements.addToHead(*element);
333 if (newElement->isAA()) {
334 ++numAAElements;
335 }
336 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
337 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
338 // differencing the non-inverse shape.
339 bool isReplace = SkRegion::kReplace_Op == newElement->getOp();
340 if (newElement->isInverseFilled() &&
341 (SkRegion::kIntersect_Op == newElement->getOp() || isReplace)) {
342 newElement->invertShapeFillType();
343 newElement->setOp(SkRegion::kDifference_Op);
344 if (isReplace) {
345 SkASSERT(InitialTriState::kAllOut == initialTriState);
346 initialTriState = InitialTriState::kAllIn;
347 }
348 }
349 }
350 }
351 }
352
353 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
354 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
355 fElements.reset();
356 numAAElements = 0;
357 } else {
358 Element* element = fElements.headIter().get();
359 while (element) {
360 bool skippable = false;
361 switch (element->getOp()) {
362 case SkRegion::kDifference_Op:
363 // subtracting from the empty set yields the empty set.
364 skippable = InitialTriState::kAllOut == initialTriState;
365 break;
366 case SkRegion::kIntersect_Op:
367 // intersecting with the empty set yields the empty set
368 if (InitialTriState::kAllOut == initialTriState) {
369 skippable = true;
370 } else {
371 // We can clear to zero and then simply draw the clip element.
372 initialTriState = InitialTriState::kAllOut;
373 element->setOp(SkRegion::kReplace_Op);
374 }
375 break;
376 case SkRegion::kUnion_Op:
377 if (InitialTriState::kAllIn == initialTriState) {
378 // unioning the infinite plane with anything is a no-op.
379 skippable = true;
380 } else {
381 // unioning the empty set with a shape is the shape.
382 element->setOp(SkRegion::kReplace_Op);
383 }
384 break;
385 case SkRegion::kXOR_Op:
386 if (InitialTriState::kAllOut == initialTriState) {
387 // xor could be changed to diff in the kAllIn case, not sure it's a win.
388 element->setOp(SkRegion::kReplace_Op);
389 }
390 break;
391 case SkRegion::kReverseDifference_Op:
392 if (InitialTriState::kAllIn == initialTriState) {
393 // subtracting the whole plane will yield the empty set.
394 skippable = true;
395 initialTriState = InitialTriState::kAllOut;
396 } else {
397 // this picks up flips inserted in the backwards pass.
398 skippable = element->isInverseFilled() ?
399 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
400 element->contains(relaxedQueryBounds);
401 if (skippable) {
402 initialTriState = InitialTriState::kAllIn;
403 } else {
404 element->setOp(SkRegion::kReplace_Op);
405 }
406 }
407 break;
408 case SkRegion::kReplace_Op:
409 skippable = false; // we would have skipped it in the backwards walk if we
410 // could've.
411 break;
412 default:
413 SkDEBUGFAIL("Unexpected op.");
414 break;
415 }
416 if (!skippable) {
417 break;
418 } else {
419 if (element->isAA()) {
420 --numAAElements;
421 }
422 fElements.popHead();
423 element = fElements.headIter().get();
424 }
425 }
426 }
427 fRequiresAA = numAAElements > 0;
428
429 SkASSERT(InitialTriState::kUnknown != initialTriState);
430 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
431}
432
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700433static bool element_is_pure_subtract(SkRegion::Op op) {
434 SkASSERT(op >= 0);
435 return op <= SkRegion::kIntersect_Op;
436
437 GR_STATIC_ASSERT(0 == SkRegion::kDifference_Op);
438 GR_STATIC_ASSERT(1 == SkRegion::kIntersect_Op);
439}
440
441void GrReducedClip::addInteriorWindowRectangles(int maxWindowRectangles) {
442 SkASSERT(fWindowRects.count() < maxWindowRectangles);
443 // Walk backwards through the element list and add window rectangles to the interiors of
444 // "difference" elements. Quit if we encounter an element that may grow the clip.
445 ElementList::Iter iter(fElements, ElementList::Iter::kTail_IterStart);
446 for (; iter.get() && element_is_pure_subtract(iter.get()->getOp()); iter.prev()) {
447 const Element* element = iter.get();
448 if (SkRegion::kDifference_Op != element->getOp()) {
449 continue;
450 }
451
452 if (Element::kRect_Type == element->getType()) {
453 SkASSERT(element->isAA());
454 this->addWindowRectangle(element->getRect(), true);
455 if (fWindowRects.count() >= maxWindowRectangles) {
456 return;
457 }
458 continue;
459 }
460
461 if (Element::kRRect_Type == element->getType()) {
462 // For round rects we add two overlapping windows in the shape of a plus.
463 const SkRRect& clipRRect = element->getRRect();
464 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
465 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
466 if (SkRRect::kComplex_Type == clipRRect.getType()) {
467 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
468 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
469 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
470 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
471 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
472 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
473 }
474 const SkRect& bounds = clipRRect.getBounds();
475 if (insetTL.x() + insetBR.x() >= bounds.width() ||
476 insetTL.y() + insetBR.y() >= bounds.height()) {
477 continue; // The interior "plus" is empty.
478 }
479
480 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
481 bounds.right(), bounds.bottom() - insetBR.y());
482 this->addWindowRectangle(horzRect, element->isAA());
483 if (fWindowRects.count() >= maxWindowRectangles) {
484 return;
485 }
486
487 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
488 bounds.right() - insetBR.x(), bounds.bottom());
489 this->addWindowRectangle(vertRect, element->isAA());
490 if (fWindowRects.count() >= maxWindowRectangles) {
491 return;
492 }
493 continue;
494 }
495 }
496}
497
498inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
499 SkIRect window;
500 if (!elementIsAA) {
501 elementInteriorRect.round(&window);
502 } else {
503 elementInteriorRect.roundIn(&window);
504 }
505 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
506 fWindowRects.addWindow(window);
507 }
508}
509
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700510inline bool GrReducedClip::intersectIBounds(const SkIRect& irect) {
511 SkASSERT(fHasIBounds);
512 if (!fIBounds.intersect(irect)) {
513 fHasIBounds = false;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700514 fWindowRects.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700515 fElements.reset();
516 fRequiresAA = false;
517 fInitialState = InitialState::kAllOut;
518 return false;
519 }
520 return true;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000521}
csmartdaltonbde96c62016-08-31 12:54:46 -0700522
523////////////////////////////////////////////////////////////////////////////////
524// Create a 8-bit clip mask in alpha
525
526static bool stencil_element(GrDrawContext* dc,
527 const GrFixedClip& clip,
528 const GrUserStencilSettings* ss,
529 const SkMatrix& viewMatrix,
530 const SkClipStack::Element* element) {
531
532 // TODO: Draw rrects directly here.
533 switch (element->getType()) {
534 case Element::kEmpty_Type:
535 SkDEBUGFAIL("Should never get here with an empty element.");
536 break;
537 case Element::kRect_Type:
538 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
539 element->getOp(),
540 element->isInverseFilled(),
541 element->isAA(),
542 viewMatrix, element->getRect());
543 break;
544 default: {
545 SkPath path;
546 element->asPath(&path);
547 if (path.isInverseFillType()) {
548 path.toggleInverseFillType();
549 }
550
551 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
552 element->getOp(),
553 element->isInverseFilled(),
554 element->isAA(), viewMatrix, path);
555 break;
556 }
557 }
558
559 return false;
560}
561
562static void draw_element(GrDrawContext* dc,
563 const GrClip& clip, // TODO: can this just always be WideOpen?
564 const GrPaint &paint,
565 const SkMatrix& viewMatrix,
566 const SkClipStack::Element* element) {
567
568 // TODO: Draw rrects directly here.
569 switch (element->getType()) {
570 case Element::kEmpty_Type:
571 SkDEBUGFAIL("Should never get here with an empty element.");
572 break;
573 case Element::kRect_Type:
574 dc->drawRect(clip, paint, viewMatrix, element->getRect());
575 break;
576 default: {
577 SkPath path;
578 element->asPath(&path);
579 if (path.isInverseFillType()) {
580 path.toggleInverseFillType();
581 }
582
583 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
584 break;
585 }
586 }
587}
588
589bool GrReducedClip::drawAlphaClipMask(GrDrawContext* dc) const {
590 // The texture may be larger than necessary, this rect represents the part of the texture
591 // we populate with a rasterization of the clip.
592 GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height()));
593
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700594 if (!fWindowRects.empty()) {
595 clip.setWindowRectangles(fWindowRects, {fIBounds.left(), fIBounds.top()},
596 GrWindowRectsState::Mode::kExclusive);
597 }
598
csmartdaltonbde96c62016-08-31 12:54:46 -0700599 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
600 // clear the part that we care about.
601 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
602 dc->drawContextPriv().clear(clip, initialCoverage, true);
603
604 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
605 SkMatrix translate;
606 translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top()));
607
608 // walk through each clip element and perform its set op
609 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
610 const Element* element = iter.get();
611 SkRegion::Op op = element->getOp();
612 bool invert = element->isInverseFilled();
613 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
614 // draw directly into the result with the stencil set to make the pixels affected
615 // by the clip shape be non-zero.
616 static constexpr GrUserStencilSettings kStencilInElement(
617 GrUserStencilSettings::StaticInit<
618 0xffff,
619 GrUserStencilTest::kAlways,
620 0xffff,
621 GrUserStencilOp::kReplace,
622 GrUserStencilOp::kReplace,
623 0xffff>()
624 );
625 if (!stencil_element(dc, clip, &kStencilInElement, translate, element)) {
626 return false;
627 }
628
629 // Draw to the exterior pixels (those with a zero stencil value).
630 static constexpr GrUserStencilSettings kDrawOutsideElement(
631 GrUserStencilSettings::StaticInit<
632 0x0000,
633 GrUserStencilTest::kEqual,
634 0xffff,
635 GrUserStencilOp::kZero,
636 GrUserStencilOp::kZero,
637 0xffff>()
638 );
639 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
640 op, !invert, false,
641 translate,
642 SkRect::Make(fIBounds))) {
643 return false;
644 }
645 } else {
646 // all the remaining ops can just be directly draw into the accumulation buffer
647 GrPaint paint;
648 paint.setAntiAlias(element->isAA());
649 paint.setCoverageSetOpXPFactory(op, false);
650
651 draw_element(dc, clip, paint, translate, element);
652 }
653 }
654
655 return true;
656}
657
658////////////////////////////////////////////////////////////////////////////////
659// Create a 1-bit clip mask in the stencil buffer.
660
661class StencilClip final : public GrClip {
662public:
663 StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {}
664 const GrFixedClip& fixedClip() const { return fFixedClip; }
665
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700666 void setWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin,
667 GrWindowRectsState::Mode mode) {
668 fFixedClip.setWindowRectangles(windows, origin, mode);
669 }
670
csmartdaltonbde96c62016-08-31 12:54:46 -0700671private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700672 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700673 return false;
674 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700675 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
676 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700677 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700678 bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700679 return false;
680 }
681 bool apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700682 bool hasUserStencilSettings, GrAppliedClip* out) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700683 if (!fFixedClip.apply(context, drawContext, useHWAA, hasUserStencilSettings, out)) {
684 return false;
685 }
686 out->addStencilClip();
687 return true;
688 }
689
690 GrFixedClip fFixedClip;
691
692 typedef GrClip INHERITED;
693};
694
695bool GrReducedClip::drawStencilClipMask(GrContext* context,
696 GrDrawContext* drawContext,
697 const SkIPoint& clipOrigin) const {
698 // We set the current clip to the bounds so that our recursive draws are scissored to them.
699 StencilClip stencilClip(fIBounds.makeOffset(-clipOrigin.x(), -clipOrigin.y()));
700
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700701 if (!fWindowRects.empty()) {
702 stencilClip.setWindowRectangles(fWindowRects, clipOrigin,
703 GrWindowRectsState::Mode::kExclusive);
704 }
705
csmartdaltonbde96c62016-08-31 12:54:46 -0700706 bool initialState = InitialState::kAllIn == this->initialState();
707 drawContext->drawContextPriv().clearStencilClip(stencilClip.fixedClip(), initialState);
708
709 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
710 SkMatrix viewMatrix;
711 viewMatrix.setTranslate(SkIntToScalar(-clipOrigin.x()), SkIntToScalar(-clipOrigin.y()));
712
713 // walk through each clip element and perform its set op
714 // with the existing clip.
715 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
716 const Element* element = iter.get();
717 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
718
719 bool fillInverted = false;
720
721 // This will be used to determine whether the clip shape can be rendered into the
722 // stencil with arbitrary stencil settings.
723 GrPathRenderer::StencilSupport stencilSupport;
724
725 SkRegion::Op op = element->getOp();
726
727 GrPathRenderer* pr = nullptr;
728 SkPath clipPath;
729 if (Element::kRect_Type == element->getType()) {
730 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
731 fillInverted = false;
732 } else {
733 element->asPath(&clipPath);
734 fillInverted = clipPath.isInverseFillType();
735 if (fillInverted) {
736 clipPath.toggleInverseFillType();
737 }
738
739 GrShape shape(clipPath, GrStyle::SimpleFill());
740 GrPathRenderer::CanDrawPathArgs canDrawArgs;
741 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
742 canDrawArgs.fViewMatrix = &viewMatrix;
743 canDrawArgs.fShape = &shape;
744 canDrawArgs.fAntiAlias = false;
745 canDrawArgs.fHasUserStencilSettings = false;
746 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
747
748 GrDrawingManager* dm = context->contextPriv().drawingManager();
749 pr = dm->getPathRenderer(canDrawArgs, false,
750 GrPathRendererChain::kStencilOnly_DrawType,
751 &stencilSupport);
752 if (!pr) {
753 return false;
754 }
755 }
756
757 bool canRenderDirectToStencil =
758 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
759 bool drawDirectToClip; // Given the renderer, the element,
760 // fill rule, and set operation should
761 // we render the element directly to
762 // stencil bit used for clipping.
763 GrUserStencilSettings const* const* stencilPasses =
764 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
765 &drawDirectToClip);
766
767 // draw the element to the client stencil bits if necessary
768 if (!drawDirectToClip) {
769 static constexpr GrUserStencilSettings kDrawToStencil(
770 GrUserStencilSettings::StaticInit<
771 0x0000,
772 GrUserStencilTest::kAlways,
773 0xffff,
774 GrUserStencilOp::kIncMaybeClamp,
775 GrUserStencilOp::kIncMaybeClamp,
776 0xffff>()
777 );
778 if (Element::kRect_Type == element->getType()) {
779 drawContext->drawContextPriv().stencilRect(stencilClip.fixedClip(),
780 &kDrawToStencil, useHWAA,
781 viewMatrix, element->getRect());
782 } else {
783 if (!clipPath.isEmpty()) {
784 GrShape shape(clipPath, GrStyle::SimpleFill());
785 if (canRenderDirectToStencil) {
786 GrPaint paint;
787 paint.setXPFactory(GrDisableColorXPFactory::Make());
788 paint.setAntiAlias(element->isAA());
789
790 GrPathRenderer::DrawPathArgs args;
791 args.fResourceProvider = context->resourceProvider();
792 args.fPaint = &paint;
793 args.fUserStencilSettings = &kDrawToStencil;
794 args.fDrawContext = drawContext;
795 args.fClip = &stencilClip.fixedClip();
796 args.fViewMatrix = &viewMatrix;
797 args.fShape = &shape;
798 args.fAntiAlias = false;
799 args.fGammaCorrect = false;
800 pr->drawPath(args);
801 } else {
802 GrPathRenderer::StencilPathArgs args;
803 args.fResourceProvider = context->resourceProvider();
804 args.fDrawContext = drawContext;
805 args.fClip = &stencilClip.fixedClip();
806 args.fViewMatrix = &viewMatrix;
807 args.fIsAA = element->isAA();
808 args.fShape = &shape;
809 pr->stencilPath(args);
810 }
811 }
812 }
813 }
814
815 // now we modify the clip bit by rendering either the clip
816 // element directly or a bounding rect of the entire clip.
817 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
818 if (drawDirectToClip) {
819 if (Element::kRect_Type == element->getType()) {
820 drawContext->drawContextPriv().stencilRect(stencilClip, *pass, useHWAA,
821 viewMatrix, element->getRect());
822 } else {
823 GrShape shape(clipPath, GrStyle::SimpleFill());
824 GrPaint paint;
825 paint.setXPFactory(GrDisableColorXPFactory::Make());
826 paint.setAntiAlias(element->isAA());
827 GrPathRenderer::DrawPathArgs args;
828 args.fResourceProvider = context->resourceProvider();
829 args.fPaint = &paint;
830 args.fUserStencilSettings = *pass;
831 args.fDrawContext = drawContext;
832 args.fClip = &stencilClip;
833 args.fViewMatrix = &viewMatrix;
834 args.fShape = &shape;
835 args.fAntiAlias = false;
836 args.fGammaCorrect = false;
837 pr->drawPath(args);
838 }
839 } else {
840 // The view matrix is setup to do clip space -> stencil space translation, so
841 // draw rect in clip space.
842 drawContext->drawContextPriv().stencilRect(stencilClip, *pass,
843 false, viewMatrix,
844 SkRect::Make(fIBounds));
845 }
846 }
847 }
848 return true;
849}