blob: 111ef7f1bf10c1651b9242fb857c1e011516c779 [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"
csmartdaltonbde96c62016-08-31 12:54:46 -07009#include "GrAppliedClip.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070010#include "GrClip.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070011#include "GrColor.h"
12#include "GrContextPriv.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070013#include "GrDrawingManager.h"
14#include "GrFixedClip.h"
15#include "GrPathRenderer.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040016#include "GrRenderTargetContext.h"
17#include "GrRenderTargetContextPriv.h"
18#include "GrShape.h"
Chris Daltonbbfd5162017-11-07 13:35:22 -070019#include "GrStencilClip.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040020#include "GrStencilSettings.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070021#include "GrStyle.h"
22#include "GrUserStencilSettings.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050023#include "SkClipOpPriv.h"
Chris Daltona32a3c32017-12-05 10:05:21 -070024#include "ccpr/GrCoverageCountingPathRenderer.h"
Chris Dalton584a79a2017-11-15 13:14:01 -070025#include "effects/GrConvexPolyEffect.h"
26#include "effects/GrRRectEffect.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070027
csmartdalton5ecbbbe2016-08-23 13:26:40 -070028/**
29 * There are plenty of optimizations that could be added here. Maybe flips could be folded into
30 * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
31 * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
32 * based on later intersect operations, and perhaps remove intersect-rects. We could optionally
33 * take a rect in case the caller knows a bound on what is to be drawn through this clip.
34 */
csmartdaltonbf4a8f92016-09-06 10:01:06 -070035GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
Ethan Nicholas222e2752018-10-11 11:21:34 -040036 GrContext* context, int maxWindowRectangles,
37 int maxAnalyticFPs, int maxCCPRClipPaths)
38 : fContext(context)
Chris Daltona32a3c32017-12-05 10:05:21 -070039 , fMaxWindowRectangles(maxWindowRectangles)
40 , fMaxAnalyticFPs(maxAnalyticFPs)
Chris Dalton1dec19a2018-04-27 13:05:19 -060041 , fMaxCCPRClipPaths(maxCCPRClipPaths) {
csmartdaltoncbecb082016-07-22 08:59:08 -070042 SkASSERT(!queryBounds.isEmpty());
Chris Dalton584a79a2017-11-15 13:14:01 -070043 SkASSERT(fMaxWindowRectangles <= GrWindowRectangles::kMaxWindows);
Chris Dalton1dec19a2018-04-27 13:05:19 -060044 SkASSERT(fMaxCCPRClipPaths <= fMaxAnalyticFPs);
Chris Dalton79471932017-10-27 01:50:57 -060045 fHasScissor = false;
46 fAAClipRectGenID = SK_InvalidGenID;
csmartdaltoncbecb082016-07-22 08:59:08 -070047
bsalomon@google.com170bd792012-12-05 22:26:11 +000048 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070049 fInitialState = InitialState::kAllIn;
50 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000051 }
52
53 SkClipStack::BoundsType stackBoundsType;
54 SkRect stackBounds;
55 bool iior;
56 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
57
Chris Dalton348060f2017-06-05 13:15:37 -060058 if (GrClip::IsOutsideClip(stackBounds, queryBounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -070059 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070060 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
61 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000062 }
63
csmartdaltoncbecb082016-07-22 08:59:08 -070064 if (iior) {
65 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
66 // This should only be true if aa/non-aa status matches among all elements.
67 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
68 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
69 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
Chris Dalton79471932017-10-27 01:50:57 -060070 // The clip is a non-aa rect. Here we just implement the entire thing using fScissor.
Mike Kleine26062a2017-10-31 20:56:54 +000071 stackBounds.round(&fScissor);
Chris Dalton79471932017-10-27 01:50:57 -060072 fHasScissor = true;
73 fInitialState = fScissor.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
csmartdalton77f2fae2016-08-08 09:55:06 -070074 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070075 }
76 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070077 fInitialState = InitialState::kAllIn;
78 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070079 }
80
csmartdaltond211e782016-08-15 11:17:19 -070081 SkRect tightBounds;
82 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
Chris Dalton79471932017-10-27 01:50:57 -060083 fScissor = GrClip::GetPixelIBounds(tightBounds);
84 if (fScissor.isEmpty()) {
Chris Dalton348060f2017-06-05 13:15:37 -060085 fInitialState = InitialState::kAllOut;
86 return;
87 }
Chris Dalton79471932017-10-27 01:50:57 -060088 fHasScissor = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070089
Chris Dalton79471932017-10-27 01:50:57 -060090 fAAClipRect = stackBounds;
Brian Salomonc3833b42018-07-09 18:23:58 +000091 fAAClipRectGenID = stack.getTopmostGenID();
92 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
csmartdalton77f2fae2016-08-08 09:55:06 -070093
Chris Dalton79471932017-10-27 01:50:57 -060094 fInitialState = InitialState::kAllIn;
95 } else {
96 SkRect tighterQuery = queryBounds;
97 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
98 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This
99 // new clip will be enforced by the scissor.)
100 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
101 }
102
103 fScissor = GrClip::GetPixelIBounds(tighterQuery);
104 if (fScissor.isEmpty()) {
105 fInitialState = InitialState::kAllOut;
106 return;
107 }
108 fHasScissor = true;
109
Chris Dalton584a79a2017-11-15 13:14:01 -0700110 // Now that we have determined the bounds to use and filtered out the trivial cases, call
111 // the helper that actually walks the stack.
112 this->walkStack(stack, tighterQuery);
csmartdaltoncbecb082016-07-22 08:59:08 -0700113 }
114
Brian Salomonc3833b42018-07-09 18:23:58 +0000115 if (SK_InvalidGenID != fAAClipRectGenID && // Is there an AA clip rect?
116 ClipResult::kNotClipped == this->addAnalyticFP(fAAClipRect, Invert::kNo, GrAA::kYes)) {
117 if (fMaskElements.isEmpty()) {
118 // Use a replace since it is faster than intersect.
119 fMaskElements.addToHead(fAAClipRect, SkMatrix::I(), kReplace_SkClipOp, true /*doAA*/);
120 fInitialState = InitialState::kAllOut;
Chris Dalton79471932017-10-27 01:50:57 -0600121 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000122 fMaskElements.addToTail(fAAClipRect, SkMatrix::I(), kIntersect_SkClipOp, true /*doAA*/);
Chris Dalton79471932017-10-27 01:50:57 -0600123 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000124 fMaskRequiresAA = true;
125 fMaskGenID = fAAClipRectGenID;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700126 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700127}
128
Chris Dalton584a79a2017-11-15 13:14:01 -0700129void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700130 // walk backwards until we get to:
131 // a) the beginning
132 // b) an operation that is known to make the bounds all inside/outside
133 // c) a replace operation
134
135 enum class InitialTriState {
136 kUnknown = -1,
137 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
138 kAllOut = (int)GrReducedClip::InitialState::kAllOut
139 } initialTriState = InitialTriState::kUnknown;
140
141 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
142 // TODO: track these per saved clip so that we can consider them on the forward pass.
143 bool embiggens = false;
144 bool emsmallens = false;
145
146 // We use a slightly relaxed set of query bounds for element containment tests. This is to
147 // account for floating point rounding error that may have occurred during coord transforms.
148 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
149 GrClip::kBoundsTolerance);
Chris Dalton69824002017-10-31 00:37:52 -0600150 if (relaxedQueryBounds.isEmpty()) {
151 relaxedQueryBounds = queryBounds;
152 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700153
154 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
155 int numAAElements = 0;
156 while (InitialTriState::kUnknown == initialTriState) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000157 const Element* element = iter.prev();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700158 if (nullptr == element) {
159 initialTriState = InitialTriState::kAllIn;
160 break;
161 }
162 if (SkClipStack::kEmptyGenID == element->getGenID()) {
163 initialTriState = InitialTriState::kAllOut;
164 break;
165 }
166 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
167 initialTriState = InitialTriState::kAllIn;
168 break;
169 }
170
171 bool skippable = false;
172 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
173
174 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500175 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700176 // check if the shape subtracted either contains the entire bounds (and makes
177 // the clip empty) or is outside the bounds and therefore can be skipped.
178 if (element->isInverseFilled()) {
179 if (element->contains(relaxedQueryBounds)) {
180 skippable = true;
181 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
182 initialTriState = InitialTriState::kAllOut;
183 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700184 } else if (!embiggens) {
185 ClipResult result = this->clipInsideElement(element);
186 if (ClipResult::kMadeEmpty == result) {
187 return;
188 }
189 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700190 }
191 } else {
192 if (element->contains(relaxedQueryBounds)) {
193 initialTriState = InitialTriState::kAllOut;
194 skippable = true;
195 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
196 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600197 } else if (!embiggens) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700198 ClipResult result = this->clipOutsideElement(element);
Chris Dalton79471932017-10-27 01:50:57 -0600199 if (ClipResult::kMadeEmpty == result) {
200 return;
201 }
202 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700203 }
204 }
205 if (!skippable) {
206 emsmallens = true;
207 }
208 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500209 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700210 // check if the shape intersected contains the entire bounds and therefore can
211 // be skipped or it is outside the entire bounds and therefore makes the clip
212 // empty.
213 if (element->isInverseFilled()) {
214 if (element->contains(relaxedQueryBounds)) {
215 initialTriState = InitialTriState::kAllOut;
216 skippable = true;
217 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
218 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700219 } else if (!embiggens) {
220 ClipResult result = this->clipOutsideElement(element);
221 if (ClipResult::kMadeEmpty == result) {
222 return;
223 }
224 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700225 }
226 } else {
227 if (element->contains(relaxedQueryBounds)) {
228 skippable = true;
229 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
230 initialTriState = InitialTriState::kAllOut;
231 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600232 } else if (!embiggens) {
233 ClipResult result = this->clipInsideElement(element);
234 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700235 return;
236 }
Chris Dalton79471932017-10-27 01:50:57 -0600237 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700238 }
239 }
240 if (!skippable) {
241 emsmallens = true;
242 }
243 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500244 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700245 // If the union-ed shape contains the entire bounds then after this element
246 // the bounds is entirely inside the clip. If the union-ed shape is outside the
247 // bounds then this op can be skipped.
248 if (element->isInverseFilled()) {
249 if (element->contains(relaxedQueryBounds)) {
250 skippable = true;
251 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
252 initialTriState = InitialTriState::kAllIn;
253 skippable = true;
254 }
255 } else {
256 if (element->contains(relaxedQueryBounds)) {
257 initialTriState = InitialTriState::kAllIn;
258 skippable = true;
259 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
260 skippable = true;
261 }
262 }
263 if (!skippable) {
264 embiggens = true;
265 }
266 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500267 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700268 // If the bounds is entirely inside the shape being xor-ed then the effect is
269 // to flip the inside/outside state of every point in the bounds. We may be
270 // able to take advantage of this in the forward pass. If the xor-ed shape
271 // doesn't intersect the bounds then it can be skipped.
272 if (element->isInverseFilled()) {
273 if (element->contains(relaxedQueryBounds)) {
274 skippable = true;
275 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
276 isFlip = true;
277 }
278 } else {
279 if (element->contains(relaxedQueryBounds)) {
280 isFlip = true;
281 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
282 skippable = true;
283 }
284 }
285 if (!skippable) {
286 emsmallens = embiggens = true;
287 }
288 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500289 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700290 // When the bounds is entirely within the rev-diff shape then this behaves like xor
291 // and reverses every point inside the bounds. If the shape is completely outside
292 // the bounds then we know after this element is applied that the bounds will be
293 // all outside the current clip.B
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 isFlip = true;
300 }
301 } else {
302 if (element->contains(relaxedQueryBounds)) {
303 isFlip = true;
304 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
305 initialTriState = InitialTriState::kAllOut;
306 skippable = true;
307 }
308 }
309 if (!skippable) {
310 emsmallens = embiggens = true;
311 }
312 break;
313
Mike Reedc1f77742016-12-09 09:00:50 -0500314 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700315 // Replace will always terminate our walk. We will either begin the forward walk
316 // at the replace op or detect here than the shape is either completely inside
317 // or completely outside the bounds. In this latter case it can be skipped by
318 // setting the correct value for initialTriState.
319 if (element->isInverseFilled()) {
320 if (element->contains(relaxedQueryBounds)) {
321 initialTriState = InitialTriState::kAllOut;
322 skippable = true;
323 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
324 initialTriState = InitialTriState::kAllIn;
325 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700326 } else if (!embiggens) {
327 ClipResult result = this->clipOutsideElement(element);
328 if (ClipResult::kMadeEmpty == result) {
329 return;
330 }
331 if (ClipResult::kClipped == result) {
332 initialTriState = InitialTriState::kAllIn;
333 skippable = true;
334 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700335 }
336 } else {
337 if (element->contains(relaxedQueryBounds)) {
338 initialTriState = InitialTriState::kAllIn;
339 skippable = true;
340 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
341 initialTriState = InitialTriState::kAllOut;
342 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600343 } else if (!embiggens) {
344 ClipResult result = this->clipInsideElement(element);
345 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700346 return;
347 }
Chris Dalton79471932017-10-27 01:50:57 -0600348 if (ClipResult::kClipped == result) {
349 initialTriState = InitialTriState::kAllIn;
350 skippable = true;
351 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700352 }
353 }
354 if (!skippable) {
355 initialTriState = InitialTriState::kAllOut;
356 embiggens = emsmallens = true;
357 }
358 break;
359 default:
360 SkDEBUGFAIL("Unexpected op.");
361 break;
362 }
363 if (!skippable) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000364 if (fMaskElements.isEmpty()) {
365 // This will be the last element. Record the stricter genID.
366 fMaskGenID = element->getGenID();
367 }
368
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700369 // if it is a flip, change it to a bounds-filling rect
370 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500371 SkASSERT(kXOR_SkClipOp == element->getOp() ||
372 kReverseDifference_SkClipOp == element->getOp());
Brian Salomonc3833b42018-07-09 18:23:58 +0000373 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
374 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700375 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000376 Element* newElement = fMaskElements.addToHead(*element);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700377 if (newElement->isAA()) {
378 ++numAAElements;
379 }
380 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
381 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
382 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500383 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700384 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500385 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700386 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500387 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700388 if (isReplace) {
389 SkASSERT(InitialTriState::kAllOut == initialTriState);
390 initialTriState = InitialTriState::kAllIn;
391 }
392 }
393 }
394 }
395 }
396
397 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
398 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
Chris Dalton79471932017-10-27 01:50:57 -0600399 fMaskElements.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700400 numAAElements = 0;
401 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000402 Element* element = fMaskElements.headIter().get();
403 while (element) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700404 bool skippable = false;
405 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500406 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700407 // subtracting from the empty set yields the empty set.
408 skippable = InitialTriState::kAllOut == initialTriState;
409 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500410 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700411 // intersecting with the empty set yields the empty set
412 if (InitialTriState::kAllOut == initialTriState) {
413 skippable = true;
414 } else {
415 // We can clear to zero and then simply draw the clip element.
416 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500417 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700418 }
419 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500420 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700421 if (InitialTriState::kAllIn == initialTriState) {
422 // unioning the infinite plane with anything is a no-op.
423 skippable = true;
424 } else {
425 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500426 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700427 }
428 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500429 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700430 if (InitialTriState::kAllOut == initialTriState) {
431 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500432 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700433 }
434 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500435 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700436 if (InitialTriState::kAllIn == initialTriState) {
437 // subtracting the whole plane will yield the empty set.
438 skippable = true;
439 initialTriState = InitialTriState::kAllOut;
440 } else {
441 // this picks up flips inserted in the backwards pass.
442 skippable = element->isInverseFilled() ?
443 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
444 element->contains(relaxedQueryBounds);
445 if (skippable) {
446 initialTriState = InitialTriState::kAllIn;
447 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500448 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700449 }
450 }
451 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500452 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700453 skippable = false; // we would have skipped it in the backwards walk if we
454 // could've.
455 break;
456 default:
457 SkDEBUGFAIL("Unexpected op.");
458 break;
459 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000460 if (!skippable) {
461 break;
462 } else {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700463 if (element->isAA()) {
464 --numAAElements;
465 }
Chris Dalton79471932017-10-27 01:50:57 -0600466 fMaskElements.popHead();
Brian Salomonc3833b42018-07-09 18:23:58 +0000467 element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700468 }
469 }
470 }
Chris Dalton79471932017-10-27 01:50:57 -0600471 fMaskRequiresAA = numAAElements > 0;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700472
473 SkASSERT(InitialTriState::kUnknown != initialTriState);
474 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
475}
476
Brian Salomonc3833b42018-07-09 18:23:58 +0000477GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600478 SkIRect elementIBounds;
479 if (!element->isAA()) {
480 element->getBounds().round(&elementIBounds);
481 } else {
482 elementIBounds = GrClip::GetPixelIBounds(element->getBounds());
483 }
484 SkASSERT(fHasScissor);
485 if (!fScissor.intersect(elementIBounds)) {
486 this->makeEmpty();
487 return ClipResult::kMadeEmpty;
488 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700489
Chris Dalton79471932017-10-27 01:50:57 -0600490 switch (element->getDeviceSpaceType()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000491 case Element::DeviceSpaceType::kEmpty:
Chris Dalton79471932017-10-27 01:50:57 -0600492 return ClipResult::kMadeEmpty;
493
Brian Salomonc3833b42018-07-09 18:23:58 +0000494 case Element::DeviceSpaceType::kRect:
Chris Dalton79471932017-10-27 01:50:57 -0600495 SkASSERT(element->getBounds() == element->getDeviceSpaceRect());
Chris Daltona32a3c32017-12-05 10:05:21 -0700496 SkASSERT(!element->isInverseFilled());
Chris Dalton79471932017-10-27 01:50:57 -0600497 if (element->isAA()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000498 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
Chris Dalton79471932017-10-27 01:50:57 -0600499 fAAClipRect = element->getDeviceSpaceRect();
Brian Salomonc3833b42018-07-09 18:23:58 +0000500 // fAAClipRectGenID is the value we should use for fMaskGenID if we end up
501 // moving the AA clip rect into the mask. The mask GenID is simply the topmost
502 // element's GenID. And since we walk the stack backwards, this means it's just
503 // the first element we don't skip during our walk.
504 fAAClipRectGenID = fMaskElements.isEmpty() ? element->getGenID() : fMaskGenID;
505 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
Chris Dalton79471932017-10-27 01:50:57 -0600506 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
507 this->makeEmpty();
508 return ClipResult::kMadeEmpty;
509 }
510 }
511 return ClipResult::kClipped;
512
Brian Salomonc3833b42018-07-09 18:23:58 +0000513 case Element::DeviceSpaceType::kRRect:
Chris Daltona32a3c32017-12-05 10:05:21 -0700514 SkASSERT(!element->isInverseFilled());
Chris Dalton584a79a2017-11-15 13:14:01 -0700515 return this->addAnalyticFP(element->getDeviceSpaceRRect(), Invert::kNo,
Chris Dalton3b51df12017-11-27 14:33:06 -0700516 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700517
Brian Salomonc3833b42018-07-09 18:23:58 +0000518 case Element::DeviceSpaceType::kPath:
Chris Daltona32a3c32017-12-05 10:05:21 -0700519 return this->addAnalyticFP(element->getDeviceSpacePath(),
520 Invert(element->isInverseFilled()), GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600521 }
522
523 SK_ABORT("Unexpected DeviceSpaceType");
524 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700525}
526
Brian Salomonc3833b42018-07-09 18:23:58 +0000527GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600528 switch (element->getDeviceSpaceType()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000529 case Element::DeviceSpaceType::kEmpty:
Chris Dalton79471932017-10-27 01:50:57 -0600530 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700531
Brian Salomonc3833b42018-07-09 18:23:58 +0000532 case Element::DeviceSpaceType::kRect:
Chris Daltona32a3c32017-12-05 10:05:21 -0700533 SkASSERT(!element->isInverseFilled());
Chris Dalton584a79a2017-11-15 13:14:01 -0700534 if (fWindowRects.count() < fMaxWindowRectangles) {
535 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones,
536 // but it saves processing time.
537 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
538 if (!element->isAA()) {
539 return ClipResult::kClipped;
540 }
541 }
542 return this->addAnalyticFP(element->getDeviceSpaceRect(), Invert::kYes,
Chris Dalton3b51df12017-11-27 14:33:06 -0700543 GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600544
Brian Salomonc3833b42018-07-09 18:23:58 +0000545 case Element::DeviceSpaceType::kRRect: {
Chris Daltona32a3c32017-12-05 10:05:21 -0700546 SkASSERT(!element->isInverseFilled());
Brian Osman554c1f02017-11-16 13:56:47 +0000547 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
Chris Dalton3b51df12017-11-27 14:33:06 -0700548 ClipResult clipResult = this->addAnalyticFP(clipRRect, Invert::kYes,
549 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700550 if (fWindowRects.count() >= fMaxWindowRectangles) {
551 return clipResult;
552 }
553
554 // Clip out the interiors of round rects with two window rectangles in the shape of a
555 // "plus". This doesn't let us skip the clip element, but still saves processing time.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700556 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
557 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
558 if (SkRRect::kComplex_Type == clipRRect.getType()) {
559 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
560 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
561 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
562 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
563 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
564 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
565 }
566 const SkRect& bounds = clipRRect.getBounds();
567 if (insetTL.x() + insetBR.x() >= bounds.width() ||
568 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700569 return clipResult; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700570 }
571
572 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
573 bounds.right(), bounds.bottom() - insetBR.y());
574 this->addWindowRectangle(horzRect, element->isAA());
Chris Dalton584a79a2017-11-15 13:14:01 -0700575
576 if (fWindowRects.count() < fMaxWindowRectangles) {
577 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
578 bounds.right() - insetBR.x(), bounds.bottom());
579 this->addWindowRectangle(vertRect, element->isAA());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700580 }
581
Chris Dalton584a79a2017-11-15 13:14:01 -0700582 return clipResult;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700583 }
Chris Dalton79471932017-10-27 01:50:57 -0600584
Brian Salomonc3833b42018-07-09 18:23:58 +0000585 case Element::DeviceSpaceType::kPath:
Chris Daltona32a3c32017-12-05 10:05:21 -0700586 return this->addAnalyticFP(element->getDeviceSpacePath(),
587 Invert(!element->isInverseFilled()), GrAA(element->isAA()));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700588 }
Chris Dalton79471932017-10-27 01:50:57 -0600589
590 SK_ABORT("Unexpected DeviceSpaceType");
591 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700592}
593
594inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
595 SkIRect window;
596 if (!elementIsAA) {
597 elementInteriorRect.round(&window);
598 } else {
599 elementInteriorRect.roundIn(&window);
600 }
601 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
602 fWindowRects.addWindow(window);
603 }
604}
605
Chris Daltona32a3c32017-12-05 10:05:21 -0700606GrClipEdgeType GrReducedClip::GetClipEdgeType(Invert invert, GrAA aa) {
607 if (Invert::kNo == invert) {
608 return (GrAA::kYes == aa) ? GrClipEdgeType::kFillAA : GrClipEdgeType::kFillBW;
609 } else {
610 return (GrAA::kYes == aa) ? GrClipEdgeType::kInverseFillAA : GrClipEdgeType::kInverseFillBW;
611 }
612}
613
Ethan Nicholas222e2752018-10-11 11:21:34 -0400614GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkRect& deviceSpaceRect, Invert invert,
615 GrAA aa) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700616 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700617 return ClipResult::kNotClipped;
618 }
619
Ethan Nicholas222e2752018-10-11 11:21:34 -0400620 fAnalyticFPs.push_back(GrConvexPolyEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRect,
621 fContext));
Chris Daltona32a3c32017-12-05 10:05:21 -0700622 SkASSERT(fAnalyticFPs.back());
623
624 return ClipResult::kClipped;
625}
626
627GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkRRect& deviceSpaceRRect,
628 Invert invert, GrAA aa) {
629 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
630 return ClipResult::kNotClipped;
Chris Dalton584a79a2017-11-15 13:14:01 -0700631 }
632
Ethan Nicholas222e2752018-10-11 11:21:34 -0400633 if (auto fp = GrRRectEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRRect, fContext)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700634 fAnalyticFPs.push_back(std::move(fp));
635 return ClipResult::kClipped;
636 }
637
Chris Daltona32a3c32017-12-05 10:05:21 -0700638 SkPath deviceSpacePath;
639 deviceSpacePath.setIsVolatile(true);
640 deviceSpacePath.addRRect(deviceSpaceRRect);
641 return this->addAnalyticFP(deviceSpacePath, invert, aa);
642}
643
644GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkPath& deviceSpacePath,
645 Invert invert, GrAA aa) {
646 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
647 return ClipResult::kNotClipped;
648 }
649
Ethan Nicholas222e2752018-10-11 11:21:34 -0400650 if (auto fp = GrConvexPolyEffect::Make(GetClipEdgeType(invert, aa), deviceSpacePath,
651 fContext)) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700652 fAnalyticFPs.push_back(std::move(fp));
653 return ClipResult::kClipped;
654 }
655
Chris Dalton1dec19a2018-04-27 13:05:19 -0600656 if (fCCPRClipPaths.count() < fMaxCCPRClipPaths && GrAA::kYes == aa) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700657 // Set aside CCPR paths for later. We will create their clip FPs once we know the ID of the
658 // opList they will operate in.
659 SkPath& ccprClipPath = fCCPRClipPaths.push_back(deviceSpacePath);
660 if (Invert::kYes == invert) {
661 ccprClipPath.toggleInverseFillType();
662 }
663 return ClipResult::kClipped;
664 }
665
Chris Dalton584a79a2017-11-15 13:14:01 -0700666 return ClipResult::kNotClipped;
667}
668
Chris Dalton79471932017-10-27 01:50:57 -0600669void GrReducedClip::makeEmpty() {
670 fHasScissor = false;
671 fAAClipRectGenID = SK_InvalidGenID;
672 fWindowRects.reset();
673 fMaskElements.reset();
674 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000675}
csmartdaltonbde96c62016-08-31 12:54:46 -0700676
677////////////////////////////////////////////////////////////////////////////////
Chris Daltonc5348082018-03-30 15:59:38 +0000678// Create a 8-bit clip mask in alpha
679
680static bool stencil_element(GrRenderTargetContext* rtc,
681 const GrFixedClip& clip,
682 const GrUserStencilSettings* ss,
683 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000684 const SkClipStack::Element* element) {
685 GrAA aa = GrAA(element->isAA());
686 switch (element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000687 case SkClipStack::Element::DeviceSpaceType::kEmpty:
688 SkDEBUGFAIL("Should never get here with an empty element.");
689 break;
690 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonc3833b42018-07-09 18:23:58 +0000691 return rtc->priv().drawAndStencilRect(clip, ss, (SkRegion::Op)element->getOp(),
692 element->isInverseFilled(), aa, viewMatrix,
693 element->getDeviceSpaceRect());
Chris Daltonc5348082018-03-30 15:59:38 +0000694 break;
695 default: {
696 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000697 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000698 if (path.isInverseFillType()) {
699 path.toggleInverseFillType();
700 }
701
Brian Salomonc3833b42018-07-09 18:23:58 +0000702 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
703 element->isInverseFilled(), aa, viewMatrix, path);
Chris Daltonc5348082018-03-30 15:59:38 +0000704 break;
705 }
706 }
707
708 return false;
709}
710
711static void draw_element(GrRenderTargetContext* rtc,
712 const GrClip& clip, // TODO: can this just always be WideOpen?
713 GrPaint&& paint,
714 GrAA aa,
715 const SkMatrix& viewMatrix,
Brian Salomonc3833b42018-07-09 18:23:58 +0000716 const SkClipStack::Element* element) {
Chris Daltonc5348082018-03-30 15:59:38 +0000717 // TODO: Draw rrects directly here.
Brian Salomonc3833b42018-07-09 18:23:58 +0000718 switch (element->getDeviceSpaceType()) {
Chris Daltonc5348082018-03-30 15:59:38 +0000719 case SkClipStack::Element::DeviceSpaceType::kEmpty:
720 SkDEBUGFAIL("Should never get here with an empty element.");
721 break;
722 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonc3833b42018-07-09 18:23:58 +0000723 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
Chris Daltonc5348082018-03-30 15:59:38 +0000724 break;
725 default: {
726 SkPath path;
Brian Salomonc3833b42018-07-09 18:23:58 +0000727 element->asDeviceSpacePath(&path);
Chris Daltonc5348082018-03-30 15:59:38 +0000728 if (path.isInverseFillType()) {
729 path.toggleInverseFillType();
730 }
731
732 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
733 break;
734 }
735 }
736}
737
738bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
739 // The texture may be larger than necessary, this rect represents the part of the texture
740 // we populate with a rasterization of the clip.
741 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
742
743 if (!fWindowRects.empty()) {
744 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
745 GrWindowRectsState::Mode::kExclusive);
746 }
747
748 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
749 // clear the part that we care about.
750 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
751 rtc->priv().clear(clip, initialCoverage, GrRenderTargetContext::CanClearFullscreen::kYes);
752
753 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
754 SkMatrix translate;
755 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
756
757 // walk through each clip element and perform its set op
758 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000759 const Element* element = iter.get();
760 SkRegion::Op op = (SkRegion::Op)element->getOp();
761 GrAA aa = GrAA(element->isAA());
762 bool invert = element->isInverseFilled();
Chris Daltonc5348082018-03-30 15:59:38 +0000763 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
764 // draw directly into the result with the stencil set to make the pixels affected
765 // by the clip shape be non-zero.
766 static constexpr GrUserStencilSettings kStencilInElement(
767 GrUserStencilSettings::StaticInit<
768 0xffff,
769 GrUserStencilTest::kAlways,
770 0xffff,
771 GrUserStencilOp::kReplace,
772 GrUserStencilOp::kReplace,
773 0xffff>()
774 );
775 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
776 return false;
777 }
778
779 // Draw to the exterior pixels (those with a zero stencil value).
780 static constexpr GrUserStencilSettings kDrawOutsideElement(
781 GrUserStencilSettings::StaticInit<
782 0x0000,
783 GrUserStencilTest::kEqual,
784 0xffff,
785 GrUserStencilOp::kZero,
786 GrUserStencilOp::kZero,
787 0xffff>()
788 );
789 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
790 translate, SkRect::Make(fScissor))) {
791 return false;
792 }
793 } else {
794 // all the remaining ops can just be directly draw into the accumulation buffer
795 GrPaint paint;
796 paint.setCoverageSetOpXPFactory(op, false);
797
798 draw_element(rtc, clip, std::move(paint), aa, translate, element);
799 }
800 }
801
802 return true;
803}
804
805////////////////////////////////////////////////////////////////////////////////
csmartdaltonbde96c62016-08-31 12:54:46 -0700806// Create a 1-bit clip mask in the stencil buffer.
807
Ethan Nicholas222e2752018-10-11 11:21:34 -0400808bool GrReducedClip::drawStencilClipMask(GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700809 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Brian Salomonc3833b42018-07-09 18:23:58 +0000810 GrStencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700811
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700812 if (!fWindowRects.empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700813 stencilClip.fixedClip().setWindowRectangles(fWindowRects,
814 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700815 }
816
csmartdaltonbde96c62016-08-31 12:54:46 -0700817 bool initialState = InitialState::kAllIn == this->initialState();
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000818 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700819
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500820 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600821 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000822 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500823 GrAAType aaType = GrAAType::kNone;
Brian Salomonc3833b42018-07-09 18:23:58 +0000824 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500825 aaType = GrAAType::kMSAA;
826 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700827
828 bool fillInverted = false;
829
830 // This will be used to determine whether the clip shape can be rendered into the
831 // stencil with arbitrary stencil settings.
832 GrPathRenderer::StencilSupport stencilSupport;
833
Brian Salomonc3833b42018-07-09 18:23:58 +0000834 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700835
836 GrPathRenderer* pr = nullptr;
837 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000838 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700839 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
840 fillInverted = false;
841 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000842 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700843 fillInverted = clipPath.isInverseFillType();
844 if (fillInverted) {
845 clipPath.toggleInverseFillType();
846 }
847
848 GrShape shape(clipPath, GrStyle::SimpleFill());
849 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Ethan Nicholas222e2752018-10-11 11:21:34 -0400850 canDrawArgs.fCaps = fContext->contextPriv().caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600851 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400852 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700853 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500854 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700855 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700856
Ethan Nicholas222e2752018-10-11 11:21:34 -0400857 GrDrawingManager* dm = fContext->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500858 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700859 &stencilSupport);
860 if (!pr) {
861 return false;
862 }
863 }
864
865 bool canRenderDirectToStencil =
866 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
867 bool drawDirectToClip; // Given the renderer, the element,
868 // fill rule, and set operation should
869 // we render the element directly to
870 // stencil bit used for clipping.
871 GrUserStencilSettings const* const* stencilPasses =
872 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
873 &drawDirectToClip);
874
875 // draw the element to the client stencil bits if necessary
876 if (!drawDirectToClip) {
877 static constexpr GrUserStencilSettings kDrawToStencil(
878 GrUserStencilSettings::StaticInit<
879 0x0000,
880 GrUserStencilTest::kAlways,
881 0xffff,
882 GrUserStencilOp::kIncMaybeClamp,
883 GrUserStencilOp::kIncMaybeClamp,
884 0xffff>()
885 );
Brian Salomonc3833b42018-07-09 18:23:58 +0000886 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400887 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400888 aaType, SkMatrix::I(),
Brian Salomonc3833b42018-07-09 18:23:58 +0000889 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700890 } else {
891 if (!clipPath.isEmpty()) {
892 GrShape shape(clipPath, GrStyle::SimpleFill());
893 if (canRenderDirectToStencil) {
894 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500895 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700896
Ethan Nicholas222e2752018-10-11 11:21:34 -0400897 GrPathRenderer::DrawPathArgs args{fContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500898 std::move(paint),
899 &kDrawToStencil,
900 renderTargetContext,
901 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600902 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400903 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500904 &shape,
905 aaType,
906 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700907 pr->drawPath(args);
908 } else {
909 GrPathRenderer::StencilPathArgs args;
Ethan Nicholas222e2752018-10-11 11:21:34 -0400910 args.fContext = fContext;
Brian Osman11052242016-10-27 14:47:55 -0400911 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700912 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600913 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400914 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500915 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700916 args.fShape = &shape;
917 pr->stencilPath(args);
918 }
919 }
920 }
921 }
922
923 // now we modify the clip bit by rendering either the clip
924 // element directly or a bounding rect of the entire clip.
925 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
926 if (drawDirectToClip) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000927 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400928 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400929 SkMatrix::I(),
Brian Salomonc3833b42018-07-09 18:23:58 +0000930 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700931 } else {
932 GrShape shape(clipPath, GrStyle::SimpleFill());
933 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500934 paint.setXPFactory(GrDisableColorXPFactory::Get());
Ethan Nicholas222e2752018-10-11 11:21:34 -0400935 GrPathRenderer::DrawPathArgs args{fContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500936 std::move(paint),
937 *pass,
938 renderTargetContext,
939 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600940 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400941 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500942 &shape,
943 aaType,
944 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700945 pr->drawPath(args);
946 }
947 } else {
948 // The view matrix is setup to do clip space -> stencil space translation, so
949 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400950 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600951 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700952 }
953 }
954 }
955 return true;
956}
Chris Daltona32a3c32017-12-05 10:05:21 -0700957
Robert Phillips777707b2018-01-17 11:40:14 -0500958std::unique_ptr<GrFragmentProcessor> GrReducedClip::finishAndDetachAnalyticFPs(
Chris Dalton4c458b12018-06-16 17:22:59 -0600959 GrCoverageCountingPathRenderer* ccpr, uint32_t opListID, int rtWidth, int rtHeight) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700960 // Make sure finishAndDetachAnalyticFPs hasn't been called already.
961 SkDEBUGCODE(for (const auto& fp : fAnalyticFPs) { SkASSERT(fp); })
962
963 if (!fCCPRClipPaths.empty()) {
964 fAnalyticFPs.reserve(fAnalyticFPs.count() + fCCPRClipPaths.count());
965 for (const SkPath& ccprClipPath : fCCPRClipPaths) {
Chris Dalton1dec19a2018-04-27 13:05:19 -0600966 SkASSERT(ccpr);
Chris Daltona32a3c32017-12-05 10:05:21 -0700967 SkASSERT(fHasScissor);
Chris Dalton4c458b12018-06-16 17:22:59 -0600968 auto fp = ccpr->makeClipProcessor(opListID, ccprClipPath, fScissor, rtWidth, rtHeight,
Ethan Nicholas222e2752018-10-11 11:21:34 -0400969 *fContext->contextPriv().caps());
Chris Daltona32a3c32017-12-05 10:05:21 -0700970 fAnalyticFPs.push_back(std::move(fp));
971 }
972 fCCPRClipPaths.reset();
973 }
974
975 return GrFragmentProcessor::RunInSeries(fAnalyticFPs.begin(), fAnalyticFPs.count());
976}