blob: 9224e005c6886358c2c6eb037c786bc5281bcdeb [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/core/SkClipOpPriv.h"
9#include "src/gpu/GrAppliedClip.h"
10#include "src/gpu/GrClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrDrawingManager.h"
13#include "src/gpu/GrFixedClip.h"
14#include "src/gpu/GrPathRenderer.h"
15#include "src/gpu/GrRecordingContextPriv.h"
16#include "src/gpu/GrReducedClip.h"
17#include "src/gpu/GrRenderTargetContext.h"
18#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrStencilClip.h"
20#include "src/gpu/GrStencilSettings.h"
21#include "src/gpu/GrStyle.h"
22#include "src/gpu/GrUserStencilSettings.h"
23#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
24#include "src/gpu/effects/GrConvexPolyEffect.h"
25#include "src/gpu/effects/GrRRectEffect.h"
26#include "src/gpu/effects/generated/GrAARectEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040027#include "src/gpu/geometry/GrShape.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070028
csmartdalton5ecbbbe2016-08-23 13:26:40 -070029/**
30 * There are plenty of optimizations that could be added here. Maybe flips could be folded into
31 * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
32 * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
33 * based on later intersect operations, and perhaps remove intersect-rects. We could optionally
34 * take a rect in case the caller knows a bound on what is to be drawn through this clip.
35 */
csmartdaltonbf4a8f92016-09-06 10:01:06 -070036GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
Ethan Nicholaseace9352018-10-15 20:09:54 +000037 const GrCaps* caps, int maxWindowRectangles, int maxAnalyticFPs,
38 int maxCCPRClipPaths)
39 : fCaps(caps)
Chris Daltona32a3c32017-12-05 10:05:21 -070040 , fMaxWindowRectangles(maxWindowRectangles)
41 , fMaxAnalyticFPs(maxAnalyticFPs)
Chris Dalton1dec19a2018-04-27 13:05:19 -060042 , fMaxCCPRClipPaths(maxCCPRClipPaths) {
csmartdaltoncbecb082016-07-22 08:59:08 -070043 SkASSERT(!queryBounds.isEmpty());
Chris Dalton584a79a2017-11-15 13:14:01 -070044 SkASSERT(fMaxWindowRectangles <= GrWindowRectangles::kMaxWindows);
Chris Dalton1dec19a2018-04-27 13:05:19 -060045 SkASSERT(fMaxCCPRClipPaths <= fMaxAnalyticFPs);
Chris Dalton79471932017-10-27 01:50:57 -060046 fHasScissor = false;
47 fAAClipRectGenID = SK_InvalidGenID;
csmartdaltoncbecb082016-07-22 08:59:08 -070048
bsalomon@google.com170bd792012-12-05 22:26:11 +000049 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070050 fInitialState = InitialState::kAllIn;
51 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000052 }
53
54 SkClipStack::BoundsType stackBoundsType;
55 SkRect stackBounds;
56 bool iior;
57 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
58
Chris Dalton348060f2017-06-05 13:15:37 -060059 if (GrClip::IsOutsideClip(stackBounds, queryBounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -070060 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070061 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
62 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000063 }
64
csmartdaltoncbecb082016-07-22 08:59:08 -070065 if (iior) {
66 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
67 // This should only be true if aa/non-aa status matches among all elements.
68 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
Robert Phillips9548c3b422019-01-08 12:35:43 -050069
70 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
71 fInitialState = InitialState::kAllIn;
72 return;
73 }
74
csmartdaltoncbecb082016-07-22 08:59:08 -070075 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
Robert Phillips9548c3b422019-01-08 12:35:43 -050076
csmartdaltoncbecb082016-07-22 08:59:08 -070077 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
Chris Dalton79471932017-10-27 01:50:57 -060078 // The clip is a non-aa rect. Here we just implement the entire thing using fScissor.
Mike Kleine26062a2017-10-31 20:56:54 +000079 stackBounds.round(&fScissor);
Chris Dalton79471932017-10-27 01:50:57 -060080 fHasScissor = true;
81 fInitialState = fScissor.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
csmartdalton77f2fae2016-08-08 09:55:06 -070082 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070083 }
csmartdaltoncbecb082016-07-22 08:59:08 -070084
csmartdaltond211e782016-08-15 11:17:19 -070085 SkRect tightBounds;
86 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
Chris Dalton79471932017-10-27 01:50:57 -060087 fScissor = GrClip::GetPixelIBounds(tightBounds);
88 if (fScissor.isEmpty()) {
Chris Dalton348060f2017-06-05 13:15:37 -060089 fInitialState = InitialState::kAllOut;
90 return;
91 }
Chris Dalton79471932017-10-27 01:50:57 -060092 fHasScissor = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070093
Chris Dalton79471932017-10-27 01:50:57 -060094 fAAClipRect = stackBounds;
Brian Salomonc3833b42018-07-09 18:23:58 +000095 fAAClipRectGenID = stack.getTopmostGenID();
96 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
csmartdalton77f2fae2016-08-08 09:55:06 -070097
Chris Dalton79471932017-10-27 01:50:57 -060098 fInitialState = InitialState::kAllIn;
99 } else {
100 SkRect tighterQuery = queryBounds;
101 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
102 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This
103 // new clip will be enforced by the scissor.)
104 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
105 }
106
107 fScissor = GrClip::GetPixelIBounds(tighterQuery);
108 if (fScissor.isEmpty()) {
109 fInitialState = InitialState::kAllOut;
110 return;
111 }
112 fHasScissor = true;
113
Chris Dalton584a79a2017-11-15 13:14:01 -0700114 // Now that we have determined the bounds to use and filtered out the trivial cases, call
115 // the helper that actually walks the stack.
116 this->walkStack(stack, tighterQuery);
csmartdaltoncbecb082016-07-22 08:59:08 -0700117 }
118
Brian Salomonc3833b42018-07-09 18:23:58 +0000119 if (SK_InvalidGenID != fAAClipRectGenID && // Is there an AA clip rect?
120 ClipResult::kNotClipped == this->addAnalyticFP(fAAClipRect, Invert::kNo, GrAA::kYes)) {
121 if (fMaskElements.isEmpty()) {
122 // Use a replace since it is faster than intersect.
123 fMaskElements.addToHead(fAAClipRect, SkMatrix::I(), kReplace_SkClipOp, true /*doAA*/);
124 fInitialState = InitialState::kAllOut;
Chris Dalton79471932017-10-27 01:50:57 -0600125 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000126 fMaskElements.addToTail(fAAClipRect, SkMatrix::I(), kIntersect_SkClipOp, true /*doAA*/);
Chris Dalton79471932017-10-27 01:50:57 -0600127 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000128 fMaskRequiresAA = true;
129 fMaskGenID = fAAClipRectGenID;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700130 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700131}
132
Chris Dalton584a79a2017-11-15 13:14:01 -0700133void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700134 // walk backwards until we get to:
135 // a) the beginning
136 // b) an operation that is known to make the bounds all inside/outside
137 // c) a replace operation
138
139 enum class InitialTriState {
140 kUnknown = -1,
141 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
142 kAllOut = (int)GrReducedClip::InitialState::kAllOut
143 } initialTriState = InitialTriState::kUnknown;
144
145 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
146 // TODO: track these per saved clip so that we can consider them on the forward pass.
147 bool embiggens = false;
148 bool emsmallens = false;
149
150 // We use a slightly relaxed set of query bounds for element containment tests. This is to
151 // account for floating point rounding error that may have occurred during coord transforms.
152 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
153 GrClip::kBoundsTolerance);
Chris Dalton69824002017-10-31 00:37:52 -0600154 if (relaxedQueryBounds.isEmpty()) {
155 relaxedQueryBounds = queryBounds;
156 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700157
158 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
159 int numAAElements = 0;
160 while (InitialTriState::kUnknown == initialTriState) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000161 const Element* element = iter.prev();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700162 if (nullptr == element) {
163 initialTriState = InitialTriState::kAllIn;
164 break;
165 }
166 if (SkClipStack::kEmptyGenID == element->getGenID()) {
167 initialTriState = InitialTriState::kAllOut;
168 break;
169 }
170 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
171 initialTriState = InitialTriState::kAllIn;
172 break;
173 }
174
175 bool skippable = false;
176 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
177
178 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500179 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700180 // check if the shape subtracted either contains the entire bounds (and makes
181 // the clip empty) or is outside the bounds and therefore can be skipped.
182 if (element->isInverseFilled()) {
183 if (element->contains(relaxedQueryBounds)) {
184 skippable = true;
185 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
186 initialTriState = InitialTriState::kAllOut;
187 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700188 } else if (!embiggens) {
189 ClipResult result = this->clipInsideElement(element);
190 if (ClipResult::kMadeEmpty == result) {
191 return;
192 }
193 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700194 }
195 } else {
196 if (element->contains(relaxedQueryBounds)) {
197 initialTriState = InitialTriState::kAllOut;
198 skippable = true;
199 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
200 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600201 } else if (!embiggens) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700202 ClipResult result = this->clipOutsideElement(element);
Chris Dalton79471932017-10-27 01:50:57 -0600203 if (ClipResult::kMadeEmpty == result) {
204 return;
205 }
206 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700207 }
208 }
209 if (!skippable) {
210 emsmallens = true;
211 }
212 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500213 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700214 // check if the shape intersected contains the entire bounds and therefore can
215 // be skipped or it is outside the entire bounds and therefore makes the clip
216 // empty.
217 if (element->isInverseFilled()) {
218 if (element->contains(relaxedQueryBounds)) {
219 initialTriState = InitialTriState::kAllOut;
220 skippable = true;
221 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
222 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700223 } else if (!embiggens) {
224 ClipResult result = this->clipOutsideElement(element);
225 if (ClipResult::kMadeEmpty == result) {
226 return;
227 }
228 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700229 }
230 } else {
231 if (element->contains(relaxedQueryBounds)) {
232 skippable = true;
233 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
234 initialTriState = InitialTriState::kAllOut;
235 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600236 } else if (!embiggens) {
237 ClipResult result = this->clipInsideElement(element);
238 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700239 return;
240 }
Chris Dalton79471932017-10-27 01:50:57 -0600241 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700242 }
243 }
244 if (!skippable) {
245 emsmallens = true;
246 }
247 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500248 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700249 // If the union-ed shape contains the entire bounds then after this element
250 // the bounds is entirely inside the clip. If the union-ed shape is outside the
251 // bounds then this op can be skipped.
252 if (element->isInverseFilled()) {
253 if (element->contains(relaxedQueryBounds)) {
254 skippable = true;
255 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
256 initialTriState = InitialTriState::kAllIn;
257 skippable = true;
258 }
259 } else {
260 if (element->contains(relaxedQueryBounds)) {
261 initialTriState = InitialTriState::kAllIn;
262 skippable = true;
263 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
264 skippable = true;
265 }
266 }
267 if (!skippable) {
268 embiggens = true;
269 }
270 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500271 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700272 // If the bounds is entirely inside the shape being xor-ed then the effect is
273 // to flip the inside/outside state of every point in the bounds. We may be
274 // able to take advantage of this in the forward pass. If the xor-ed shape
275 // doesn't intersect the bounds then it can be skipped.
276 if (element->isInverseFilled()) {
277 if (element->contains(relaxedQueryBounds)) {
278 skippable = true;
279 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
280 isFlip = true;
281 }
282 } else {
283 if (element->contains(relaxedQueryBounds)) {
284 isFlip = true;
285 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
286 skippable = true;
287 }
288 }
289 if (!skippable) {
290 emsmallens = embiggens = true;
291 }
292 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500293 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700294 // When the bounds is entirely within the rev-diff shape then this behaves like xor
295 // and reverses every point inside the bounds. If the shape is completely outside
296 // the bounds then we know after this element is applied that the bounds will be
297 // all outside the current clip.B
298 if (element->isInverseFilled()) {
299 if (element->contains(relaxedQueryBounds)) {
300 initialTriState = InitialTriState::kAllOut;
301 skippable = true;
302 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
303 isFlip = true;
304 }
305 } else {
306 if (element->contains(relaxedQueryBounds)) {
307 isFlip = true;
308 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
309 initialTriState = InitialTriState::kAllOut;
310 skippable = true;
311 }
312 }
313 if (!skippable) {
314 emsmallens = embiggens = true;
315 }
316 break;
317
Mike Reedc1f77742016-12-09 09:00:50 -0500318 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700319 // Replace will always terminate our walk. We will either begin the forward walk
320 // at the replace op or detect here than the shape is either completely inside
321 // or completely outside the bounds. In this latter case it can be skipped by
322 // setting the correct value for initialTriState.
323 if (element->isInverseFilled()) {
324 if (element->contains(relaxedQueryBounds)) {
325 initialTriState = InitialTriState::kAllOut;
326 skippable = true;
327 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
328 initialTriState = InitialTriState::kAllIn;
329 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700330 } else if (!embiggens) {
331 ClipResult result = this->clipOutsideElement(element);
332 if (ClipResult::kMadeEmpty == result) {
333 return;
334 }
335 if (ClipResult::kClipped == result) {
336 initialTriState = InitialTriState::kAllIn;
337 skippable = true;
338 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700339 }
340 } else {
341 if (element->contains(relaxedQueryBounds)) {
342 initialTriState = InitialTriState::kAllIn;
343 skippable = true;
344 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
345 initialTriState = InitialTriState::kAllOut;
346 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600347 } else if (!embiggens) {
348 ClipResult result = this->clipInsideElement(element);
349 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700350 return;
351 }
Chris Dalton79471932017-10-27 01:50:57 -0600352 if (ClipResult::kClipped == result) {
353 initialTriState = InitialTriState::kAllIn;
354 skippable = true;
355 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700356 }
357 }
358 if (!skippable) {
359 initialTriState = InitialTriState::kAllOut;
360 embiggens = emsmallens = true;
361 }
362 break;
363 default:
364 SkDEBUGFAIL("Unexpected op.");
365 break;
366 }
367 if (!skippable) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000368 if (fMaskElements.isEmpty()) {
369 // This will be the last element. Record the stricter genID.
370 fMaskGenID = element->getGenID();
371 }
372
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700373 // if it is a flip, change it to a bounds-filling rect
374 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500375 SkASSERT(kXOR_SkClipOp == element->getOp() ||
376 kReverseDifference_SkClipOp == element->getOp());
Brian Salomonc3833b42018-07-09 18:23:58 +0000377 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
378 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700379 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000380 Element* newElement = fMaskElements.addToHead(*element);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700381 if (newElement->isAA()) {
382 ++numAAElements;
383 }
384 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
385 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
386 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500387 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700388 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500389 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700390 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500391 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700392 if (isReplace) {
393 SkASSERT(InitialTriState::kAllOut == initialTriState);
394 initialTriState = InitialTriState::kAllIn;
395 }
396 }
397 }
398 }
399 }
400
401 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
402 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
Chris Dalton79471932017-10-27 01:50:57 -0600403 fMaskElements.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700404 numAAElements = 0;
405 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000406 Element* element = fMaskElements.headIter().get();
407 while (element) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700408 bool skippable = false;
409 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500410 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700411 // subtracting from the empty set yields the empty set.
412 skippable = InitialTriState::kAllOut == initialTriState;
413 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500414 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700415 // intersecting with the empty set yields the empty set
416 if (InitialTriState::kAllOut == initialTriState) {
417 skippable = true;
418 } else {
419 // We can clear to zero and then simply draw the clip element.
420 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500421 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700422 }
423 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500424 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700425 if (InitialTriState::kAllIn == initialTriState) {
426 // unioning the infinite plane with anything is a no-op.
427 skippable = true;
428 } else {
429 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500430 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700431 }
432 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500433 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700434 if (InitialTriState::kAllOut == initialTriState) {
435 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500436 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700437 }
438 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500439 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700440 if (InitialTriState::kAllIn == initialTriState) {
441 // subtracting the whole plane will yield the empty set.
442 skippable = true;
443 initialTriState = InitialTriState::kAllOut;
444 } else {
445 // this picks up flips inserted in the backwards pass.
446 skippable = element->isInverseFilled() ?
447 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
448 element->contains(relaxedQueryBounds);
449 if (skippable) {
450 initialTriState = InitialTriState::kAllIn;
451 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500452 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700453 }
454 }
455 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500456 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700457 skippable = false; // we would have skipped it in the backwards walk if we
458 // could've.
459 break;
460 default:
461 SkDEBUGFAIL("Unexpected op.");
462 break;
463 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000464 if (!skippable) {
465 break;
466 } else {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700467 if (element->isAA()) {
468 --numAAElements;
469 }
Chris Dalton79471932017-10-27 01:50:57 -0600470 fMaskElements.popHead();
Brian Salomonc3833b42018-07-09 18:23:58 +0000471 element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700472 }
473 }
474 }
Chris Dalton79471932017-10-27 01:50:57 -0600475 fMaskRequiresAA = numAAElements > 0;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700476
477 SkASSERT(InitialTriState::kUnknown != initialTriState);
478 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
479}
480
Brian Salomonc3833b42018-07-09 18:23:58 +0000481GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600482 SkIRect elementIBounds;
483 if (!element->isAA()) {
484 element->getBounds().round(&elementIBounds);
485 } else {
486 elementIBounds = GrClip::GetPixelIBounds(element->getBounds());
487 }
488 SkASSERT(fHasScissor);
489 if (!fScissor.intersect(elementIBounds)) {
490 this->makeEmpty();
491 return ClipResult::kMadeEmpty;
492 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700493
Chris Dalton79471932017-10-27 01:50:57 -0600494 switch (element->getDeviceSpaceType()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000495 case Element::DeviceSpaceType::kEmpty:
Chris Dalton79471932017-10-27 01:50:57 -0600496 return ClipResult::kMadeEmpty;
497
Brian Salomonc3833b42018-07-09 18:23:58 +0000498 case Element::DeviceSpaceType::kRect:
Chris Dalton79471932017-10-27 01:50:57 -0600499 SkASSERT(element->getBounds() == element->getDeviceSpaceRect());
Chris Daltona32a3c32017-12-05 10:05:21 -0700500 SkASSERT(!element->isInverseFilled());
Chris Dalton79471932017-10-27 01:50:57 -0600501 if (element->isAA()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000502 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
Chris Dalton79471932017-10-27 01:50:57 -0600503 fAAClipRect = element->getDeviceSpaceRect();
Brian Salomonc3833b42018-07-09 18:23:58 +0000504 // fAAClipRectGenID is the value we should use for fMaskGenID if we end up
505 // moving the AA clip rect into the mask. The mask GenID is simply the topmost
506 // element's GenID. And since we walk the stack backwards, this means it's just
507 // the first element we don't skip during our walk.
508 fAAClipRectGenID = fMaskElements.isEmpty() ? element->getGenID() : fMaskGenID;
509 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
Chris Dalton79471932017-10-27 01:50:57 -0600510 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
511 this->makeEmpty();
512 return ClipResult::kMadeEmpty;
513 }
514 }
515 return ClipResult::kClipped;
516
Brian Salomonc3833b42018-07-09 18:23:58 +0000517 case Element::DeviceSpaceType::kRRect:
Chris Daltona32a3c32017-12-05 10:05:21 -0700518 SkASSERT(!element->isInverseFilled());
Chris Dalton584a79a2017-11-15 13:14:01 -0700519 return this->addAnalyticFP(element->getDeviceSpaceRRect(), Invert::kNo,
Chris Dalton3b51df12017-11-27 14:33:06 -0700520 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700521
Brian Salomonc3833b42018-07-09 18:23:58 +0000522 case Element::DeviceSpaceType::kPath:
Chris Daltona32a3c32017-12-05 10:05:21 -0700523 return this->addAnalyticFP(element->getDeviceSpacePath(),
524 Invert(element->isInverseFilled()), GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600525 }
526
527 SK_ABORT("Unexpected DeviceSpaceType");
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700528}
529
Brian Salomonc3833b42018-07-09 18:23:58 +0000530GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600531 switch (element->getDeviceSpaceType()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000532 case Element::DeviceSpaceType::kEmpty:
Chris Dalton79471932017-10-27 01:50:57 -0600533 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700534
Brian Salomonc3833b42018-07-09 18:23:58 +0000535 case Element::DeviceSpaceType::kRect:
Chris Daltona32a3c32017-12-05 10:05:21 -0700536 SkASSERT(!element->isInverseFilled());
Chris Dalton584a79a2017-11-15 13:14:01 -0700537 if (fWindowRects.count() < fMaxWindowRectangles) {
538 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones,
539 // but it saves processing time.
540 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
541 if (!element->isAA()) {
542 return ClipResult::kClipped;
543 }
544 }
545 return this->addAnalyticFP(element->getDeviceSpaceRect(), Invert::kYes,
Chris Dalton3b51df12017-11-27 14:33:06 -0700546 GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600547
Brian Salomonc3833b42018-07-09 18:23:58 +0000548 case Element::DeviceSpaceType::kRRect: {
Chris Daltona32a3c32017-12-05 10:05:21 -0700549 SkASSERT(!element->isInverseFilled());
Brian Osman554c1f02017-11-16 13:56:47 +0000550 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
Chris Dalton3b51df12017-11-27 14:33:06 -0700551 ClipResult clipResult = this->addAnalyticFP(clipRRect, Invert::kYes,
552 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700553 if (fWindowRects.count() >= fMaxWindowRectangles) {
554 return clipResult;
555 }
556
557 // Clip out the interiors of round rects with two window rectangles in the shape of a
558 // "plus". This doesn't let us skip the clip element, but still saves processing time.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700559 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
560 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
561 if (SkRRect::kComplex_Type == clipRRect.getType()) {
562 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
563 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
564 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
565 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
566 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
567 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
568 }
569 const SkRect& bounds = clipRRect.getBounds();
570 if (insetTL.x() + insetBR.x() >= bounds.width() ||
571 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700572 return clipResult; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700573 }
574
575 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
576 bounds.right(), bounds.bottom() - insetBR.y());
577 this->addWindowRectangle(horzRect, element->isAA());
Chris Dalton584a79a2017-11-15 13:14:01 -0700578
579 if (fWindowRects.count() < fMaxWindowRectangles) {
580 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
581 bounds.right() - insetBR.x(), bounds.bottom());
582 this->addWindowRectangle(vertRect, element->isAA());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700583 }
584
Chris Dalton584a79a2017-11-15 13:14:01 -0700585 return clipResult;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700586 }
Chris Dalton79471932017-10-27 01:50:57 -0600587
Brian Salomonc3833b42018-07-09 18:23:58 +0000588 case Element::DeviceSpaceType::kPath:
Chris Daltona32a3c32017-12-05 10:05:21 -0700589 return this->addAnalyticFP(element->getDeviceSpacePath(),
590 Invert(!element->isInverseFilled()), GrAA(element->isAA()));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700591 }
Chris Dalton79471932017-10-27 01:50:57 -0600592
593 SK_ABORT("Unexpected DeviceSpaceType");
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700594}
595
596inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
597 SkIRect window;
598 if (!elementIsAA) {
599 elementInteriorRect.round(&window);
600 } else {
601 elementInteriorRect.roundIn(&window);
602 }
603 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
604 fWindowRects.addWindow(window);
605 }
606}
607
Chris Daltona32a3c32017-12-05 10:05:21 -0700608GrClipEdgeType GrReducedClip::GetClipEdgeType(Invert invert, GrAA aa) {
609 if (Invert::kNo == invert) {
610 return (GrAA::kYes == aa) ? GrClipEdgeType::kFillAA : GrClipEdgeType::kFillBW;
611 } else {
612 return (GrAA::kYes == aa) ? GrClipEdgeType::kInverseFillAA : GrClipEdgeType::kInverseFillBW;
613 }
614}
615
Ethan Nicholaseace9352018-10-15 20:09:54 +0000616GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkRect& deviceSpaceRect,
617 Invert invert, GrAA aa) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700618 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700619 return ClipResult::kNotClipped;
620 }
621
Ethan Nicholaseace9352018-10-15 20:09:54 +0000622 fAnalyticFPs.push_back(GrAARectEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRect));
Chris Daltona32a3c32017-12-05 10:05:21 -0700623 SkASSERT(fAnalyticFPs.back());
624
625 return ClipResult::kClipped;
626}
627
628GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkRRect& deviceSpaceRRect,
629 Invert invert, GrAA aa) {
630 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
631 return ClipResult::kNotClipped;
Chris Dalton584a79a2017-11-15 13:14:01 -0700632 }
633
Ethan Nicholaseace9352018-10-15 20:09:54 +0000634 if (auto fp = GrRRectEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRRect,
635 *fCaps->shaderCaps())) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700636 fAnalyticFPs.push_back(std::move(fp));
637 return ClipResult::kClipped;
638 }
639
Chris Daltona32a3c32017-12-05 10:05:21 -0700640 SkPath deviceSpacePath;
641 deviceSpacePath.setIsVolatile(true);
642 deviceSpacePath.addRRect(deviceSpaceRRect);
643 return this->addAnalyticFP(deviceSpacePath, invert, aa);
644}
645
646GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkPath& deviceSpacePath,
647 Invert invert, GrAA aa) {
648 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
649 return ClipResult::kNotClipped;
650 }
651
Ethan Nicholaseace9352018-10-15 20:09:54 +0000652 if (auto fp = GrConvexPolyEffect::Make(GetClipEdgeType(invert, aa), deviceSpacePath)) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700653 fAnalyticFPs.push_back(std::move(fp));
654 return ClipResult::kClipped;
655 }
656
Chris Dalton1dec19a2018-04-27 13:05:19 -0600657 if (fCCPRClipPaths.count() < fMaxCCPRClipPaths && GrAA::kYes == aa) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700658 // Set aside CCPR paths for later. We will create their clip FPs once we know the ID of the
Greg Danielf41b2bd2019-08-22 16:19:24 -0400659 // opsTask they will operate in.
Chris Daltona32a3c32017-12-05 10:05:21 -0700660 SkPath& ccprClipPath = fCCPRClipPaths.push_back(deviceSpacePath);
661 if (Invert::kYes == invert) {
662 ccprClipPath.toggleInverseFillType();
663 }
664 return ClipResult::kClipped;
665 }
666
Chris Dalton584a79a2017-11-15 13:14:01 -0700667 return ClipResult::kNotClipped;
668}
669
Chris Dalton79471932017-10-27 01:50:57 -0600670void GrReducedClip::makeEmpty() {
671 fHasScissor = false;
672 fAAClipRectGenID = SK_InvalidGenID;
673 fWindowRects.reset();
674 fMaskElements.reset();
675 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000676}
csmartdaltonbde96c62016-08-31 12:54:46 -0700677
678////////////////////////////////////////////////////////////////////////////////
Chris Daltonc5348082018-03-30 15:59:38 +0000679// Create a 8-bit clip mask in alpha
680
681static bool stencil_element(GrRenderTargetContext* rtc,
682 const GrFixedClip& clip,
683 const GrUserStencilSettings* ss,
684 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000685 const SkClipStack::Element* element) {
686 GrAA aa = GrAA(element->isAA());
687 switch (element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000688 case SkClipStack::Element::DeviceSpaceType::kEmpty:
689 SkDEBUGFAIL("Should never get here with an empty element.");
690 break;
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400691 case SkClipStack::Element::DeviceSpaceType::kRect: {
692 GrPaint paint;
693 paint.setCoverageSetOpXPFactory((SkRegion::Op)element->getOp(),
694 element->isInverseFilled());
695 rtc->priv().stencilRect(clip, ss, std::move(paint), aa, viewMatrix,
696 element->getDeviceSpaceRect());
697 return true;
698 }
Chris Daltonc5348082018-03-30 15:59:38 +0000699 default: {
700 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000701 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000702 if (path.isInverseFillType()) {
703 path.toggleInverseFillType();
704 }
705
Brian Salomonc3833b42018-07-09 18:23:58 +0000706 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
707 element->isInverseFilled(), aa, viewMatrix, path);
Chris Daltonc5348082018-03-30 15:59:38 +0000708 }
709 }
710
711 return false;
712}
713
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400714static void stencil_device_rect(GrRenderTargetContext* rtc,
715 const GrHardClip& clip,
716 const GrUserStencilSettings* ss,
717 GrAA aa,
718 const SkRect& rect) {
719 GrPaint paint;
720 paint.setXPFactory(GrDisableColorXPFactory::Get());
721 rtc->priv().stencilRect(clip, ss, std::move(paint), aa, SkMatrix::I(), rect);
722}
723
Chris Daltonc5348082018-03-30 15:59:38 +0000724static void draw_element(GrRenderTargetContext* rtc,
725 const GrClip& clip, // TODO: can this just always be WideOpen?
726 GrPaint&& paint,
727 GrAA aa,
728 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000729 const SkClipStack::Element* element) {
Chris Daltonc5348082018-03-30 15:59:38 +0000730 // TODO: Draw rrects directly here.
Brian Salomonc3833b42018-07-09 18:23:58 +0000731 switch (element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000732 case SkClipStack::Element::DeviceSpaceType::kEmpty:
733 SkDEBUGFAIL("Should never get here with an empty element.");
734 break;
735 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonc3833b42018-07-09 18:23:58 +0000736 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
Chris Daltonc5348082018-03-30 15:59:38 +0000737 break;
738 default: {
739 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000740 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000741 if (path.isInverseFillType()) {
742 path.toggleInverseFillType();
743 }
744
745 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
746 break;
747 }
748 }
749}
750
751bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
752 // The texture may be larger than necessary, this rect represents the part of the texture
753 // we populate with a rasterization of the clip.
754 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
755
756 if (!fWindowRects.empty()) {
757 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
758 GrWindowRectsState::Mode::kExclusive);
759 }
760
761 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
762 // clear the part that we care about.
Brian Osman9a9baae2018-11-05 15:06:26 -0500763 SkPMColor4f initialCoverage =
764 InitialState::kAllIn == this->initialState() ? SK_PMColor4fWHITE : SK_PMColor4fTRANSPARENT;
Chris Daltonc5348082018-03-30 15:59:38 +0000765 rtc->priv().clear(clip, initialCoverage, GrRenderTargetContext::CanClearFullscreen::kYes);
766
767 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
768 SkMatrix translate;
769 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
770
771 // walk through each clip element and perform its set op
772 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000773 const Element* element = iter.get();
774 SkRegion::Op op = (SkRegion::Op)element->getOp();
775 GrAA aa = GrAA(element->isAA());
776 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000777 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
778 // draw directly into the result with the stencil set to make the pixels affected
779 // by the clip shape be non-zero.
780 static constexpr GrUserStencilSettings kStencilInElement(
781 GrUserStencilSettings::StaticInit<
782 0xffff,
783 GrUserStencilTest::kAlways,
784 0xffff,
785 GrUserStencilOp::kReplace,
786 GrUserStencilOp::kReplace,
787 0xffff>()
788 );
789 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
790 return false;
791 }
792
793 // Draw to the exterior pixels (those with a zero stencil value).
794 static constexpr GrUserStencilSettings kDrawOutsideElement(
795 GrUserStencilSettings::StaticInit<
796 0x0000,
797 GrUserStencilTest::kEqual,
798 0xffff,
799 GrUserStencilOp::kZero,
800 GrUserStencilOp::kZero,
801 0xffff>()
802 );
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400803
804 GrPaint paint;
805 paint.setCoverageSetOpXPFactory(op, !invert);
806 rtc->priv().stencilRect(clip, &kDrawOutsideElement, std::move(paint), GrAA::kNo,
807 translate, SkRect::Make(fScissor));
Chris Daltonc5348082018-03-30 15:59:38 +0000808 } else {
809 // all the remaining ops can just be directly draw into the accumulation buffer
810 GrPaint paint;
811 paint.setCoverageSetOpXPFactory(op, false);
812
813 draw_element(rtc, clip, std::move(paint), aa, translate, element);
814 }
815 }
816
817 return true;
818}
819
820////////////////////////////////////////////////////////////////////////////////
csmartdaltonbde96c62016-08-31 12:54:46 -0700821// Create a 1-bit clip mask in the stencil buffer.
822
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500823bool GrReducedClip::drawStencilClipMask(GrRecordingContext* context,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000824 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700825 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Brian Salomonc3833b42018-07-09 18:23:58 +0000826 GrStencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700827
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700828 if (!fWindowRects.empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700829 stencilClip.fixedClip().setWindowRectangles(fWindowRects,
830 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700831 }
832
csmartdaltonbde96c62016-08-31 12:54:46 -0700833 bool initialState = InitialState::kAllIn == this->initialState();
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000834 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700835
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500836 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600837 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000838 const Element* element = iter.get();
Chris Dalton6ce447a2019-06-23 18:07:38 -0600839 // MIXED SAMPLES TODO: We can use stencil with mixed samples as well.
840 bool doStencilMSAA = element->isAA() && renderTargetContext->numSamples() > 1;
Chris Dalton09e56892019-03-13 00:22:01 -0600841 // Since we are only drawing to the stencil buffer, we can use kMSAA even if the render
842 // target is mixed sampled.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600843 auto pathAAType = (doStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
csmartdaltonbde96c62016-08-31 12:54:46 -0700844 bool fillInverted = false;
845
846 // This will be used to determine whether the clip shape can be rendered into the
847 // stencil with arbitrary stencil settings.
848 GrPathRenderer::StencilSupport stencilSupport;
849
Brian Salomonc3833b42018-07-09 18:23:58 +0000850 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700851
852 GrPathRenderer* pr = nullptr;
853 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000854 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700855 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
856 fillInverted = false;
857 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000858 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700859 fillInverted = clipPath.isInverseFillType();
860 if (fillInverted) {
861 clipPath.toggleInverseFillType();
862 }
863
864 GrShape shape(clipPath, GrStyle::SimpleFill());
865 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -0500866 canDrawArgs.fCaps = context->priv().caps();
Chris Daltoneffee202019-07-01 22:28:03 -0600867 canDrawArgs.fProxy = renderTargetContext->proxy();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600868 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400869 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700870 canDrawArgs.fShape = &shape;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600871 canDrawArgs.fAAType = pathAAType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700872 canDrawArgs.fHasUserStencilSettings = false;
Greg Danielbe7fc462019-01-03 16:40:42 -0500873 canDrawArgs.fTargetIsWrappedVkSecondaryCB = renderTargetContext->wrapsVkSecondaryCB();
csmartdaltonbde96c62016-08-31 12:54:46 -0700874
Robert Phillips9da87e02019-02-04 13:26:26 -0500875 GrDrawingManager* dm = context->priv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500876 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700877 &stencilSupport);
878 if (!pr) {
879 return false;
880 }
881 }
882
883 bool canRenderDirectToStencil =
884 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
885 bool drawDirectToClip; // Given the renderer, the element,
886 // fill rule, and set operation should
887 // we render the element directly to
888 // stencil bit used for clipping.
889 GrUserStencilSettings const* const* stencilPasses =
890 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
891 &drawDirectToClip);
892
893 // draw the element to the client stencil bits if necessary
894 if (!drawDirectToClip) {
895 static constexpr GrUserStencilSettings kDrawToStencil(
896 GrUserStencilSettings::StaticInit<
897 0x0000,
898 GrUserStencilTest::kAlways,
899 0xffff,
900 GrUserStencilOp::kIncMaybeClamp,
901 GrUserStencilOp::kIncMaybeClamp,
902 0xffff>()
903 );
Brian Salomonc3833b42018-07-09 18:23:58 +0000904 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400905 stencil_device_rect(renderTargetContext, stencilClip.fixedClip(), &kDrawToStencil,
906 GrAA(doStencilMSAA), element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700907 } else {
908 if (!clipPath.isEmpty()) {
909 GrShape shape(clipPath, GrStyle::SimpleFill());
910 if (canRenderDirectToStencil) {
911 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500912 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700913
Ethan Nicholaseace9352018-10-15 20:09:54 +0000914 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500915 std::move(paint),
916 &kDrawToStencil,
917 renderTargetContext,
918 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600919 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400920 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500921 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600922 pathAAType,
Brian Salomon82f44312017-01-11 13:42:54 -0500923 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700924 pr->drawPath(args);
925 } else {
926 GrPathRenderer::StencilPathArgs args;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000927 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400928 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700929 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600930 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400931 args.fViewMatrix = &SkMatrix::I();
Chris Dalton09e56892019-03-13 00:22:01 -0600932 args.fDoStencilMSAA = GrAA(doStencilMSAA);
csmartdaltonbde96c62016-08-31 12:54:46 -0700933 args.fShape = &shape;
934 pr->stencilPath(args);
935 }
936 }
937 }
938 }
939
940 // now we modify the clip bit by rendering either the clip
941 // element directly or a bounding rect of the entire clip.
942 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
943 if (drawDirectToClip) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000944 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400945 stencil_device_rect(renderTargetContext, stencilClip, *pass,
946 GrAA(doStencilMSAA), element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700947 } else {
948 GrShape shape(clipPath, GrStyle::SimpleFill());
949 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500950 paint.setXPFactory(GrDisableColorXPFactory::Get());
Ethan Nicholaseace9352018-10-15 20:09:54 +0000951 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500952 std::move(paint),
953 *pass,
954 renderTargetContext,
955 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600956 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400957 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500958 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600959 pathAAType,
Brian Salomon82f44312017-01-11 13:42:54 -0500960 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700961 pr->drawPath(args);
962 }
963 } else {
964 // The view matrix is setup to do clip space -> stencil space translation, so
965 // draw rect in clip space.
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400966 stencil_device_rect(renderTargetContext, stencilClip, *pass, GrAA(doStencilMSAA),
967 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700968 }
969 }
970 }
971 return true;
972}
Chris Daltona32a3c32017-12-05 10:05:21 -0700973
Robert Phillips777707b2018-01-17 11:40:14 -0500974std::unique_ptr<GrFragmentProcessor> GrReducedClip::finishAndDetachAnalyticFPs(
Greg Danielf41b2bd2019-08-22 16:19:24 -0400975 GrCoverageCountingPathRenderer* ccpr, uint32_t opsTaskID) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700976 // Make sure finishAndDetachAnalyticFPs hasn't been called already.
977 SkDEBUGCODE(for (const auto& fp : fAnalyticFPs) { SkASSERT(fp); })
978
979 if (!fCCPRClipPaths.empty()) {
980 fAnalyticFPs.reserve(fAnalyticFPs.count() + fCCPRClipPaths.count());
981 for (const SkPath& ccprClipPath : fCCPRClipPaths) {
Chris Dalton1dec19a2018-04-27 13:05:19 -0600982 SkASSERT(ccpr);
Chris Daltona32a3c32017-12-05 10:05:21 -0700983 SkASSERT(fHasScissor);
Greg Danielf41b2bd2019-08-22 16:19:24 -0400984 auto fp = ccpr->makeClipProcessor(opsTaskID, ccprClipPath, fScissor, *fCaps);
Chris Daltona32a3c32017-12-05 10:05:21 -0700985 fAnalyticFPs.push_back(std::move(fp));
986 }
987 fCCPRClipPaths.reset();
988 }
989
990 return GrFragmentProcessor::RunInSeries(fAnalyticFPs.begin(), fAnalyticFPs.count());
991}