blob: 4cf34774fe8daa2609ed9c12ed1bf8d57a845fd5 [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"
25#include "effects/GrAARectEffect.h"
Chris Dalton584a79a2017-11-15 13:14:01 -070026#include "effects/GrConvexPolyEffect.h"
27#include "effects/GrRRectEffect.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,
Chris Dalton4c458b12018-06-16 17:22:59 -060037 const GrCaps* caps, int maxWindowRectangles, int maxAnalyticFPs,
Chris Dalton1dec19a2018-04-27 13:05:19 -060038 int maxCCPRClipPaths)
Chris Daltona32a3c32017-12-05 10:05:21 -070039 : fCaps(caps)
40 , 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);
69 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
70 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
Chris Dalton79471932017-10-27 01:50:57 -060071 // The clip is a non-aa rect. Here we just implement the entire thing using fScissor.
Mike Kleine26062a2017-10-31 20:56:54 +000072 stackBounds.round(&fScissor);
Chris Dalton79471932017-10-27 01:50:57 -060073 fHasScissor = true;
74 fInitialState = fScissor.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
csmartdalton77f2fae2016-08-08 09:55:06 -070075 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070076 }
77 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070078 fInitialState = InitialState::kAllIn;
79 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070080 }
81
csmartdaltond211e782016-08-15 11:17:19 -070082 SkRect tightBounds;
83 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
Chris Dalton79471932017-10-27 01:50:57 -060084 fScissor = GrClip::GetPixelIBounds(tightBounds);
85 if (fScissor.isEmpty()) {
Chris Dalton348060f2017-06-05 13:15:37 -060086 fInitialState = InitialState::kAllOut;
87 return;
88 }
Chris Dalton79471932017-10-27 01:50:57 -060089 fHasScissor = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070090
Chris Dalton79471932017-10-27 01:50:57 -060091 fAAClipRect = stackBounds;
Brian Salomonc3833b42018-07-09 18:23:58 +000092 fAAClipRectGenID = stack.getTopmostGenID();
93 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
csmartdalton77f2fae2016-08-08 09:55:06 -070094
Chris Dalton79471932017-10-27 01:50:57 -060095 fInitialState = InitialState::kAllIn;
96 } else {
97 SkRect tighterQuery = queryBounds;
98 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
99 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This
100 // new clip will be enforced by the scissor.)
101 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
102 }
103
104 fScissor = GrClip::GetPixelIBounds(tighterQuery);
105 if (fScissor.isEmpty()) {
106 fInitialState = InitialState::kAllOut;
107 return;
108 }
109 fHasScissor = true;
110
Chris Dalton584a79a2017-11-15 13:14:01 -0700111 // Now that we have determined the bounds to use and filtered out the trivial cases, call
112 // the helper that actually walks the stack.
113 this->walkStack(stack, tighterQuery);
csmartdaltoncbecb082016-07-22 08:59:08 -0700114 }
115
Brian Salomonc3833b42018-07-09 18:23:58 +0000116 if (SK_InvalidGenID != fAAClipRectGenID && // Is there an AA clip rect?
117 ClipResult::kNotClipped == this->addAnalyticFP(fAAClipRect, Invert::kNo, GrAA::kYes)) {
118 if (fMaskElements.isEmpty()) {
119 // Use a replace since it is faster than intersect.
120 fMaskElements.addToHead(fAAClipRect, SkMatrix::I(), kReplace_SkClipOp, true /*doAA*/);
121 fInitialState = InitialState::kAllOut;
Chris Dalton79471932017-10-27 01:50:57 -0600122 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000123 fMaskElements.addToTail(fAAClipRect, SkMatrix::I(), kIntersect_SkClipOp, true /*doAA*/);
Chris Dalton79471932017-10-27 01:50:57 -0600124 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000125 fMaskRequiresAA = true;
126 fMaskGenID = fAAClipRectGenID;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700127 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700128}
129
Chris Dalton584a79a2017-11-15 13:14:01 -0700130void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700131 // walk backwards until we get to:
132 // a) the beginning
133 // b) an operation that is known to make the bounds all inside/outside
134 // c) a replace operation
135
136 enum class InitialTriState {
137 kUnknown = -1,
138 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
139 kAllOut = (int)GrReducedClip::InitialState::kAllOut
140 } initialTriState = InitialTriState::kUnknown;
141
142 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
143 // TODO: track these per saved clip so that we can consider them on the forward pass.
144 bool embiggens = false;
145 bool emsmallens = false;
146
147 // We use a slightly relaxed set of query bounds for element containment tests. This is to
148 // account for floating point rounding error that may have occurred during coord transforms.
149 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
150 GrClip::kBoundsTolerance);
Chris Dalton69824002017-10-31 00:37:52 -0600151 if (relaxedQueryBounds.isEmpty()) {
152 relaxedQueryBounds = queryBounds;
153 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700154
155 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
156 int numAAElements = 0;
157 while (InitialTriState::kUnknown == initialTriState) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000158 const Element* element = iter.prev();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700159 if (nullptr == element) {
160 initialTriState = InitialTriState::kAllIn;
161 break;
162 }
163 if (SkClipStack::kEmptyGenID == element->getGenID()) {
164 initialTriState = InitialTriState::kAllOut;
165 break;
166 }
167 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
168 initialTriState = InitialTriState::kAllIn;
169 break;
170 }
171
172 bool skippable = false;
173 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
174
175 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500176 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700177 // check if the shape subtracted either contains the entire bounds (and makes
178 // the clip empty) or is outside the bounds and therefore can be skipped.
179 if (element->isInverseFilled()) {
180 if (element->contains(relaxedQueryBounds)) {
181 skippable = true;
182 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
183 initialTriState = InitialTriState::kAllOut;
184 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700185 } else if (!embiggens) {
186 ClipResult result = this->clipInsideElement(element);
187 if (ClipResult::kMadeEmpty == result) {
188 return;
189 }
190 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700191 }
192 } else {
193 if (element->contains(relaxedQueryBounds)) {
194 initialTriState = InitialTriState::kAllOut;
195 skippable = true;
196 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
197 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600198 } else if (!embiggens) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700199 ClipResult result = this->clipOutsideElement(element);
Chris Dalton79471932017-10-27 01:50:57 -0600200 if (ClipResult::kMadeEmpty == result) {
201 return;
202 }
203 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700204 }
205 }
206 if (!skippable) {
207 emsmallens = true;
208 }
209 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500210 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700211 // check if the shape intersected contains the entire bounds and therefore can
212 // be skipped or it is outside the entire bounds and therefore makes the clip
213 // empty.
214 if (element->isInverseFilled()) {
215 if (element->contains(relaxedQueryBounds)) {
216 initialTriState = InitialTriState::kAllOut;
217 skippable = true;
218 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
219 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700220 } else if (!embiggens) {
221 ClipResult result = this->clipOutsideElement(element);
222 if (ClipResult::kMadeEmpty == result) {
223 return;
224 }
225 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700226 }
227 } else {
228 if (element->contains(relaxedQueryBounds)) {
229 skippable = true;
230 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
231 initialTriState = InitialTriState::kAllOut;
232 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600233 } else if (!embiggens) {
234 ClipResult result = this->clipInsideElement(element);
235 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700236 return;
237 }
Chris Dalton79471932017-10-27 01:50:57 -0600238 skippable = (ClipResult::kClipped == result);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700239 }
240 }
241 if (!skippable) {
242 emsmallens = true;
243 }
244 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500245 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700246 // If the union-ed shape contains the entire bounds then after this element
247 // the bounds is entirely inside the clip. If the union-ed shape is outside the
248 // bounds then this op can be skipped.
249 if (element->isInverseFilled()) {
250 if (element->contains(relaxedQueryBounds)) {
251 skippable = true;
252 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
253 initialTriState = InitialTriState::kAllIn;
254 skippable = true;
255 }
256 } else {
257 if (element->contains(relaxedQueryBounds)) {
258 initialTriState = InitialTriState::kAllIn;
259 skippable = true;
260 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
261 skippable = true;
262 }
263 }
264 if (!skippable) {
265 embiggens = true;
266 }
267 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500268 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700269 // If the bounds is entirely inside the shape being xor-ed then the effect is
270 // to flip the inside/outside state of every point in the bounds. We may be
271 // able to take advantage of this in the forward pass. If the xor-ed shape
272 // doesn't intersect the bounds then it can be skipped.
273 if (element->isInverseFilled()) {
274 if (element->contains(relaxedQueryBounds)) {
275 skippable = true;
276 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
277 isFlip = true;
278 }
279 } else {
280 if (element->contains(relaxedQueryBounds)) {
281 isFlip = true;
282 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
283 skippable = true;
284 }
285 }
286 if (!skippable) {
287 emsmallens = embiggens = true;
288 }
289 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500290 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700291 // When the bounds is entirely within the rev-diff shape then this behaves like xor
292 // and reverses every point inside the bounds. If the shape is completely outside
293 // the bounds then we know after this element is applied that the bounds will be
294 // all outside the current clip.B
295 if (element->isInverseFilled()) {
296 if (element->contains(relaxedQueryBounds)) {
297 initialTriState = InitialTriState::kAllOut;
298 skippable = true;
299 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
300 isFlip = true;
301 }
302 } else {
303 if (element->contains(relaxedQueryBounds)) {
304 isFlip = true;
305 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
306 initialTriState = InitialTriState::kAllOut;
307 skippable = true;
308 }
309 }
310 if (!skippable) {
311 emsmallens = embiggens = true;
312 }
313 break;
314
Mike Reedc1f77742016-12-09 09:00:50 -0500315 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700316 // Replace will always terminate our walk. We will either begin the forward walk
317 // at the replace op or detect here than the shape is either completely inside
318 // or completely outside the bounds. In this latter case it can be skipped by
319 // setting the correct value for initialTriState.
320 if (element->isInverseFilled()) {
321 if (element->contains(relaxedQueryBounds)) {
322 initialTriState = InitialTriState::kAllOut;
323 skippable = true;
324 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
325 initialTriState = InitialTriState::kAllIn;
326 skippable = true;
Chris Daltona32a3c32017-12-05 10:05:21 -0700327 } else if (!embiggens) {
328 ClipResult result = this->clipOutsideElement(element);
329 if (ClipResult::kMadeEmpty == result) {
330 return;
331 }
332 if (ClipResult::kClipped == result) {
333 initialTriState = InitialTriState::kAllIn;
334 skippable = true;
335 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700336 }
337 } else {
338 if (element->contains(relaxedQueryBounds)) {
339 initialTriState = InitialTriState::kAllIn;
340 skippable = true;
341 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
342 initialTriState = InitialTriState::kAllOut;
343 skippable = true;
Chris Dalton79471932017-10-27 01:50:57 -0600344 } else if (!embiggens) {
345 ClipResult result = this->clipInsideElement(element);
346 if (ClipResult::kMadeEmpty == result) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700347 return;
348 }
Chris Dalton79471932017-10-27 01:50:57 -0600349 if (ClipResult::kClipped == result) {
350 initialTriState = InitialTriState::kAllIn;
351 skippable = true;
352 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700353 }
354 }
355 if (!skippable) {
356 initialTriState = InitialTriState::kAllOut;
357 embiggens = emsmallens = true;
358 }
359 break;
360 default:
361 SkDEBUGFAIL("Unexpected op.");
362 break;
363 }
364 if (!skippable) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000365 if (fMaskElements.isEmpty()) {
366 // This will be the last element. Record the stricter genID.
367 fMaskGenID = element->getGenID();
368 }
369
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700370 // if it is a flip, change it to a bounds-filling rect
371 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500372 SkASSERT(kXOR_SkClipOp == element->getOp() ||
373 kReverseDifference_SkClipOp == element->getOp());
Brian Salomonc3833b42018-07-09 18:23:58 +0000374 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
375 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700376 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000377 Element* newElement = fMaskElements.addToHead(*element);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700378 if (newElement->isAA()) {
379 ++numAAElements;
380 }
381 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
382 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
383 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500384 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700385 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500386 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700387 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500388 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700389 if (isReplace) {
390 SkASSERT(InitialTriState::kAllOut == initialTriState);
391 initialTriState = InitialTriState::kAllIn;
392 }
393 }
394 }
395 }
396 }
397
398 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
399 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
Chris Dalton79471932017-10-27 01:50:57 -0600400 fMaskElements.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700401 numAAElements = 0;
402 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000403 Element* element = fMaskElements.headIter().get();
404 while (element) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700405 bool skippable = false;
406 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500407 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700408 // subtracting from the empty set yields the empty set.
409 skippable = InitialTriState::kAllOut == initialTriState;
410 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500411 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700412 // intersecting with the empty set yields the empty set
413 if (InitialTriState::kAllOut == initialTriState) {
414 skippable = true;
415 } else {
416 // We can clear to zero and then simply draw the clip element.
417 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500418 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700419 }
420 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500421 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700422 if (InitialTriState::kAllIn == initialTriState) {
423 // unioning the infinite plane with anything is a no-op.
424 skippable = true;
425 } else {
426 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500427 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700428 }
429 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500430 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700431 if (InitialTriState::kAllOut == initialTriState) {
432 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500433 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700434 }
435 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500436 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700437 if (InitialTriState::kAllIn == initialTriState) {
438 // subtracting the whole plane will yield the empty set.
439 skippable = true;
440 initialTriState = InitialTriState::kAllOut;
441 } else {
442 // this picks up flips inserted in the backwards pass.
443 skippable = element->isInverseFilled() ?
444 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
445 element->contains(relaxedQueryBounds);
446 if (skippable) {
447 initialTriState = InitialTriState::kAllIn;
448 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500449 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700450 }
451 }
452 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500453 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700454 skippable = false; // we would have skipped it in the backwards walk if we
455 // could've.
456 break;
457 default:
458 SkDEBUGFAIL("Unexpected op.");
459 break;
460 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000461 if (!skippable) {
462 break;
463 } else {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700464 if (element->isAA()) {
465 --numAAElements;
466 }
Chris Dalton79471932017-10-27 01:50:57 -0600467 fMaskElements.popHead();
Brian Salomonc3833b42018-07-09 18:23:58 +0000468 element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700469 }
470 }
471 }
Chris Dalton79471932017-10-27 01:50:57 -0600472 fMaskRequiresAA = numAAElements > 0;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700473
474 SkASSERT(InitialTriState::kUnknown != initialTriState);
475 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
476}
477
Brian Salomonc3833b42018-07-09 18:23:58 +0000478GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600479 SkIRect elementIBounds;
480 if (!element->isAA()) {
481 element->getBounds().round(&elementIBounds);
482 } else {
483 elementIBounds = GrClip::GetPixelIBounds(element->getBounds());
484 }
485 SkASSERT(fHasScissor);
486 if (!fScissor.intersect(elementIBounds)) {
487 this->makeEmpty();
488 return ClipResult::kMadeEmpty;
489 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700490
Chris Dalton79471932017-10-27 01:50:57 -0600491 switch (element->getDeviceSpaceType()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000492 case Element::DeviceSpaceType::kEmpty:
Chris Dalton79471932017-10-27 01:50:57 -0600493 return ClipResult::kMadeEmpty;
494
Brian Salomonc3833b42018-07-09 18:23:58 +0000495 case Element::DeviceSpaceType::kRect:
Chris Dalton79471932017-10-27 01:50:57 -0600496 SkASSERT(element->getBounds() == element->getDeviceSpaceRect());
Chris Daltona32a3c32017-12-05 10:05:21 -0700497 SkASSERT(!element->isInverseFilled());
Chris Dalton79471932017-10-27 01:50:57 -0600498 if (element->isAA()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000499 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
Chris Dalton79471932017-10-27 01:50:57 -0600500 fAAClipRect = element->getDeviceSpaceRect();
Brian Salomonc3833b42018-07-09 18:23:58 +0000501 // fAAClipRectGenID is the value we should use for fMaskGenID if we end up
502 // moving the AA clip rect into the mask. The mask GenID is simply the topmost
503 // element's GenID. And since we walk the stack backwards, this means it's just
504 // the first element we don't skip during our walk.
505 fAAClipRectGenID = fMaskElements.isEmpty() ? element->getGenID() : fMaskGenID;
506 SkASSERT(SK_InvalidGenID != fAAClipRectGenID);
Chris Dalton79471932017-10-27 01:50:57 -0600507 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
508 this->makeEmpty();
509 return ClipResult::kMadeEmpty;
510 }
511 }
512 return ClipResult::kClipped;
513
Brian Salomonc3833b42018-07-09 18:23:58 +0000514 case Element::DeviceSpaceType::kRRect:
Chris Daltona32a3c32017-12-05 10:05:21 -0700515 SkASSERT(!element->isInverseFilled());
Chris Dalton584a79a2017-11-15 13:14:01 -0700516 return this->addAnalyticFP(element->getDeviceSpaceRRect(), Invert::kNo,
Chris Dalton3b51df12017-11-27 14:33:06 -0700517 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700518
Brian Salomonc3833b42018-07-09 18:23:58 +0000519 case Element::DeviceSpaceType::kPath:
Chris Daltona32a3c32017-12-05 10:05:21 -0700520 return this->addAnalyticFP(element->getDeviceSpacePath(),
521 Invert(element->isInverseFilled()), GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600522 }
523
524 SK_ABORT("Unexpected DeviceSpaceType");
525 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700526}
527
Brian Salomonc3833b42018-07-09 18:23:58 +0000528GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600529 switch (element->getDeviceSpaceType()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000530 case Element::DeviceSpaceType::kEmpty:
Chris Dalton79471932017-10-27 01:50:57 -0600531 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700532
Brian Salomonc3833b42018-07-09 18:23:58 +0000533 case Element::DeviceSpaceType::kRect:
Chris Daltona32a3c32017-12-05 10:05:21 -0700534 SkASSERT(!element->isInverseFilled());
Chris Dalton584a79a2017-11-15 13:14:01 -0700535 if (fWindowRects.count() < fMaxWindowRectangles) {
536 // Clip out the inside of every rect. We won't be able to entirely skip the AA ones,
537 // but it saves processing time.
538 this->addWindowRectangle(element->getDeviceSpaceRect(), element->isAA());
539 if (!element->isAA()) {
540 return ClipResult::kClipped;
541 }
542 }
543 return this->addAnalyticFP(element->getDeviceSpaceRect(), Invert::kYes,
Chris Dalton3b51df12017-11-27 14:33:06 -0700544 GrAA(element->isAA()));
Chris Dalton79471932017-10-27 01:50:57 -0600545
Brian Salomonc3833b42018-07-09 18:23:58 +0000546 case Element::DeviceSpaceType::kRRect: {
Chris Daltona32a3c32017-12-05 10:05:21 -0700547 SkASSERT(!element->isInverseFilled());
Brian Osman554c1f02017-11-16 13:56:47 +0000548 const SkRRect& clipRRect = element->getDeviceSpaceRRect();
Chris Dalton3b51df12017-11-27 14:33:06 -0700549 ClipResult clipResult = this->addAnalyticFP(clipRRect, Invert::kYes,
550 GrAA(element->isAA()));
Chris Dalton584a79a2017-11-15 13:14:01 -0700551 if (fWindowRects.count() >= fMaxWindowRectangles) {
552 return clipResult;
553 }
554
555 // Clip out the interiors of round rects with two window rectangles in the shape of a
556 // "plus". This doesn't let us skip the clip element, but still saves processing time.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700557 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
558 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
559 if (SkRRect::kComplex_Type == clipRRect.getType()) {
560 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
561 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
562 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
563 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
564 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
565 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
566 }
567 const SkRect& bounds = clipRRect.getBounds();
568 if (insetTL.x() + insetBR.x() >= bounds.width() ||
569 insetTL.y() + insetBR.y() >= bounds.height()) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700570 return clipResult; // The interior "plus" is empty.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700571 }
572
573 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
574 bounds.right(), bounds.bottom() - insetBR.y());
575 this->addWindowRectangle(horzRect, element->isAA());
Chris Dalton584a79a2017-11-15 13:14:01 -0700576
577 if (fWindowRects.count() < fMaxWindowRectangles) {
578 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
579 bounds.right() - insetBR.x(), bounds.bottom());
580 this->addWindowRectangle(vertRect, element->isAA());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700581 }
582
Chris Dalton584a79a2017-11-15 13:14:01 -0700583 return clipResult;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700584 }
Chris Dalton79471932017-10-27 01:50:57 -0600585
Brian Salomonc3833b42018-07-09 18:23:58 +0000586 case Element::DeviceSpaceType::kPath:
Chris Daltona32a3c32017-12-05 10:05:21 -0700587 return this->addAnalyticFP(element->getDeviceSpacePath(),
588 Invert(!element->isInverseFilled()), GrAA(element->isAA()));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700589 }
Chris Dalton79471932017-10-27 01:50:57 -0600590
591 SK_ABORT("Unexpected DeviceSpaceType");
592 return ClipResult::kNotClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700593}
594
595inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
596 SkIRect window;
597 if (!elementIsAA) {
598 elementInteriorRect.round(&window);
599 } else {
600 elementInteriorRect.roundIn(&window);
601 }
602 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
603 fWindowRects.addWindow(window);
604 }
605}
606
Chris Daltona32a3c32017-12-05 10:05:21 -0700607GrClipEdgeType GrReducedClip::GetClipEdgeType(Invert invert, GrAA aa) {
608 if (Invert::kNo == invert) {
609 return (GrAA::kYes == aa) ? GrClipEdgeType::kFillAA : GrClipEdgeType::kFillBW;
610 } else {
611 return (GrAA::kYes == aa) ? GrClipEdgeType::kInverseFillAA : GrClipEdgeType::kInverseFillBW;
612 }
613}
614
615GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkRect& deviceSpaceRect,
616 Invert invert, GrAA aa) {
617 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700618 return ClipResult::kNotClipped;
619 }
620
Chris Daltona32a3c32017-12-05 10:05:21 -0700621 fAnalyticFPs.push_back(GrAARectEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRect));
622 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
Chris Dalton4c458b12018-06-16 17:22:59 -0600633 if (auto fp = GrRRectEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRRect,
634 *fCaps->shaderCaps())) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700635 fAnalyticFPs.push_back(std::move(fp));
636 return ClipResult::kClipped;
637 }
638
Chris Daltona32a3c32017-12-05 10:05:21 -0700639 SkPath deviceSpacePath;
640 deviceSpacePath.setIsVolatile(true);
641 deviceSpacePath.addRRect(deviceSpaceRRect);
642 return this->addAnalyticFP(deviceSpacePath, invert, aa);
643}
644
645GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkPath& deviceSpacePath,
646 Invert invert, GrAA aa) {
647 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
648 return ClipResult::kNotClipped;
649 }
650
651 if (auto fp = GrConvexPolyEffect::Make(GetClipEdgeType(invert, aa), deviceSpacePath)) {
652 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
csmartdaltonbde96c62016-08-31 12:54:46 -0700808bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400809 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700810 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Brian Salomonc3833b42018-07-09 18:23:58 +0000811 GrStencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700812
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700813 if (!fWindowRects.empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700814 stencilClip.fixedClip().setWindowRectangles(fWindowRects,
815 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700816 }
817
csmartdaltonbde96c62016-08-31 12:54:46 -0700818 bool initialState = InitialState::kAllIn == this->initialState();
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000819 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700820
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500821 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600822 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000823 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500824 GrAAType aaType = GrAAType::kNone;
Brian Salomonc3833b42018-07-09 18:23:58 +0000825 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500826 aaType = GrAAType::kMSAA;
827 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700828
829 bool fillInverted = false;
830
831 // This will be used to determine whether the clip shape can be rendered into the
832 // stencil with arbitrary stencil settings.
833 GrPathRenderer::StencilSupport stencilSupport;
834
Brian Salomonc3833b42018-07-09 18:23:58 +0000835 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700836
837 GrPathRenderer* pr = nullptr;
838 SkPath clipPath;
Brian Salomonc3833b42018-07-09 18:23:58 +0000839 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700840 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
841 fillInverted = false;
842 } else {
Brian Salomonc3833b42018-07-09 18:23:58 +0000843 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700844 fillInverted = clipPath.isInverseFillType();
845 if (fillInverted) {
846 clipPath.toggleInverseFillType();
847 }
848
849 GrShape shape(clipPath, GrStyle::SimpleFill());
850 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400851 canDrawArgs.fCaps = context->contextPriv().caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600852 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400853 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700854 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500855 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700856 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700857
858 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500859 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700860 &stencilSupport);
861 if (!pr) {
862 return false;
863 }
864 }
865
866 bool canRenderDirectToStencil =
867 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
868 bool drawDirectToClip; // Given the renderer, the element,
869 // fill rule, and set operation should
870 // we render the element directly to
871 // stencil bit used for clipping.
872 GrUserStencilSettings const* const* stencilPasses =
873 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
874 &drawDirectToClip);
875
876 // draw the element to the client stencil bits if necessary
877 if (!drawDirectToClip) {
878 static constexpr GrUserStencilSettings kDrawToStencil(
879 GrUserStencilSettings::StaticInit<
880 0x0000,
881 GrUserStencilTest::kAlways,
882 0xffff,
883 GrUserStencilOp::kIncMaybeClamp,
884 GrUserStencilOp::kIncMaybeClamp,
885 0xffff>()
886 );
Brian Salomonc3833b42018-07-09 18:23:58 +0000887 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400888 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400889 aaType, SkMatrix::I(),
Brian Salomonc3833b42018-07-09 18:23:58 +0000890 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700891 } else {
892 if (!clipPath.isEmpty()) {
893 GrShape shape(clipPath, GrStyle::SimpleFill());
894 if (canRenderDirectToStencil) {
895 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500896 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700897
Robert Phillips256c37b2017-03-01 14:32:46 -0500898 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500899 std::move(paint),
900 &kDrawToStencil,
901 renderTargetContext,
902 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600903 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400904 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500905 &shape,
906 aaType,
907 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700908 pr->drawPath(args);
909 } else {
910 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500911 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400912 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700913 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600914 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400915 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500916 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700917 args.fShape = &shape;
918 pr->stencilPath(args);
919 }
920 }
921 }
922 }
923
924 // now we modify the clip bit by rendering either the clip
925 // element directly or a bounding rect of the entire clip.
926 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
927 if (drawDirectToClip) {
Brian Salomonc3833b42018-07-09 18:23:58 +0000928 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400929 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400930 SkMatrix::I(),
Brian Salomonc3833b42018-07-09 18:23:58 +0000931 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700932 } else {
933 GrShape shape(clipPath, GrStyle::SimpleFill());
934 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500935 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500936 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500937 std::move(paint),
938 *pass,
939 renderTargetContext,
940 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600941 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400942 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500943 &shape,
944 aaType,
945 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700946 pr->drawPath(args);
947 }
948 } else {
949 // The view matrix is setup to do clip space -> stencil space translation, so
950 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400951 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600952 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700953 }
954 }
955 }
956 return true;
957}
Chris Daltona32a3c32017-12-05 10:05:21 -0700958
Robert Phillips777707b2018-01-17 11:40:14 -0500959std::unique_ptr<GrFragmentProcessor> GrReducedClip::finishAndDetachAnalyticFPs(
Chris Dalton4c458b12018-06-16 17:22:59 -0600960 GrCoverageCountingPathRenderer* ccpr, uint32_t opListID, int rtWidth, int rtHeight) {
Chris Daltona32a3c32017-12-05 10:05:21 -0700961 // Make sure finishAndDetachAnalyticFPs hasn't been called already.
962 SkDEBUGCODE(for (const auto& fp : fAnalyticFPs) { SkASSERT(fp); })
963
964 if (!fCCPRClipPaths.empty()) {
965 fAnalyticFPs.reserve(fAnalyticFPs.count() + fCCPRClipPaths.count());
966 for (const SkPath& ccprClipPath : fCCPRClipPaths) {
Chris Dalton1dec19a2018-04-27 13:05:19 -0600967 SkASSERT(ccpr);
Chris Daltona32a3c32017-12-05 10:05:21 -0700968 SkASSERT(fHasScissor);
Chris Dalton4c458b12018-06-16 17:22:59 -0600969 auto fp = ccpr->makeClipProcessor(opListID, ccprClipPath, fScissor, rtWidth, rtHeight,
970 *fCaps);
Chris Daltona32a3c32017-12-05 10:05:21 -0700971 fAnalyticFPs.push_back(std::move(fp));
972 }
973 fCCPRClipPaths.reset();
974 }
975
976 return GrFragmentProcessor::RunInSeries(fAnalyticFPs.begin(), fAnalyticFPs.count());
977}