blob: 9d27f7696eb1c7c79fcc46dd46ce771e474b5b4b [file] [log] [blame]
bsalomon@google.com170bd792012-12-05 22:26:11 +00001/*
csmartdalton77f2fae2016-08-08 09:55:06 -07002 * Copyright 2016 Google Inc.
bsalomon@google.com170bd792012-12-05 22:26:11 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrReducedClip.h"
9
csmartdaltonbde96c62016-08-31 12:54:46 -070010#include "GrAppliedClip.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070011#include "GrClip.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070012#include "GrColor.h"
13#include "GrContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContext.h"
15#include "GrRenderTargetContextPriv.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070016#include "GrDrawingManager.h"
17#include "GrFixedClip.h"
18#include "GrPathRenderer.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070019#include "GrStencilSettings.h"
Chris Daltonbbfd5162017-11-07 13:35:22 -070020#include "GrStencilClip.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 Daltona32a3c32017-12-05 10:05:21 -070037 const GrShaderCaps* caps, int maxWindowRectangles, int maxAnalyticFPs,
38 GrCoverageCountingPathRenderer* ccpr)
39 : fCaps(caps)
40 , fMaxWindowRectangles(maxWindowRectangles)
41 , fMaxAnalyticFPs(maxAnalyticFPs)
42 , fCCPR(fMaxAnalyticFPs ? ccpr : nullptr) {
csmartdaltoncbecb082016-07-22 08:59:08 -070043 SkASSERT(!queryBounds.isEmpty());
Chris Dalton584a79a2017-11-15 13:14:01 -070044 SkASSERT(fMaxWindowRectangles <= GrWindowRectangles::kMaxWindows);
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;
91 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
Chris Dalton584a79a2017-11-15 13:14:01 -0700115 if (SK_InvalidGenID != fAAClipRectGenID && // Is there an AA clip rect?
Chris Dalton3b51df12017-11-27 14:33:06 -0700116 ClipResult::kNotClipped == this->addAnalyticFP(fAAClipRect, Invert::kNo, GrAA::kYes)) {
Chris Dalton79471932017-10-27 01:50:57 -0600117 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;
121 } else {
122 fMaskElements.addToTail(fAAClipRect, SkMatrix::I(), kIntersect_SkClipOp, true /*doAA*/);
123 }
124 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) {
157 const Element* element = iter.prev();
158 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) {
Chris Dalton79471932017-10-27 01:50:57 -0600364 if (fMaskElements.isEmpty()) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700365 // This will be the last element. Record the stricter genID.
Chris Dalton79471932017-10-27 01:50:57 -0600366 fMaskGenID = element->getGenID();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700367 }
368
369 // 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());
Chris Dalton79471932017-10-27 01:50:57 -0600373 fMaskElements.addToHead(SkRect::Make(fScissor), SkMatrix::I(),
374 kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700375 } else {
Chris Dalton79471932017-10-27 01:50:57 -0600376 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 {
Chris Dalton79471932017-10-27 01:50:57 -0600402 Element* element = fMaskElements.headIter().get();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700403 while (element) {
404 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 }
460 if (!skippable) {
461 break;
462 } else {
463 if (element->isAA()) {
464 --numAAElements;
465 }
Chris Dalton79471932017-10-27 01:50:57 -0600466 fMaskElements.popHead();
467 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
Chris Dalton79471932017-10-27 01:50:57 -0600477GrReducedClip::ClipResult GrReducedClip::clipInsideElement(const Element* element) {
478 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()) {
491 case Element::DeviceSpaceType::kEmpty:
492 return ClipResult::kMadeEmpty;
493
494 case Element::DeviceSpaceType::kRect:
495 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()) {
498 if (SK_InvalidGenID == fAAClipRectGenID) { // No AA clip rect yet?
499 fAAClipRect = element->getDeviceSpaceRect();
500 // 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);
506 } else if (!fAAClipRect.intersect(element->getDeviceSpaceRect())) {
507 this->makeEmpty();
508 return ClipResult::kMadeEmpty;
509 }
510 }
511 return ClipResult::kClipped;
512
513 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
Chris Dalton79471932017-10-27 01:50:57 -0600518 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
Chris Dalton584a79a2017-11-15 13:14:01 -0700527GrReducedClip::ClipResult GrReducedClip::clipOutsideElement(const Element* element) {
Chris Dalton79471932017-10-27 01:50:57 -0600528 switch (element->getDeviceSpaceType()) {
529 case Element::DeviceSpaceType::kEmpty:
530 return ClipResult::kMadeEmpty;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700531
Chris Dalton79471932017-10-27 01:50:57 -0600532 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
545 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
585 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
614GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkRect& deviceSpaceRect,
615 Invert invert, GrAA aa) {
616 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700617 return ClipResult::kNotClipped;
618 }
619
Chris Daltona32a3c32017-12-05 10:05:21 -0700620 fAnalyticFPs.push_back(GrAARectEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRect));
621 SkASSERT(fAnalyticFPs.back());
622
623 return ClipResult::kClipped;
624}
625
626GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkRRect& deviceSpaceRRect,
627 Invert invert, GrAA aa) {
628 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
629 return ClipResult::kNotClipped;
Chris Dalton584a79a2017-11-15 13:14:01 -0700630 }
631
Chris Daltona32a3c32017-12-05 10:05:21 -0700632 if (auto fp = GrRRectEffect::Make(GetClipEdgeType(invert, aa), deviceSpaceRRect, *fCaps)) {
Chris Dalton584a79a2017-11-15 13:14:01 -0700633 fAnalyticFPs.push_back(std::move(fp));
634 return ClipResult::kClipped;
635 }
636
Chris Daltona32a3c32017-12-05 10:05:21 -0700637 SkPath deviceSpacePath;
638 deviceSpacePath.setIsVolatile(true);
639 deviceSpacePath.addRRect(deviceSpaceRRect);
640 return this->addAnalyticFP(deviceSpacePath, invert, aa);
641}
642
643GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkPath& deviceSpacePath,
644 Invert invert, GrAA aa) {
645 if (this->numAnalyticFPs() >= fMaxAnalyticFPs) {
646 return ClipResult::kNotClipped;
647 }
648
649 if (auto fp = GrConvexPolyEffect::Make(GetClipEdgeType(invert, aa), deviceSpacePath)) {
650 fAnalyticFPs.push_back(std::move(fp));
651 return ClipResult::kClipped;
652 }
653
654 if (fCCPR && GrAA::kYes == aa && fCCPR->canMakeClipProcessor(deviceSpacePath)) {
655 // Set aside CCPR paths for later. We will create their clip FPs once we know the ID of the
656 // opList they will operate in.
657 SkPath& ccprClipPath = fCCPRClipPaths.push_back(deviceSpacePath);
658 if (Invert::kYes == invert) {
659 ccprClipPath.toggleInverseFillType();
660 }
661 return ClipResult::kClipped;
662 }
663
Chris Dalton584a79a2017-11-15 13:14:01 -0700664 return ClipResult::kNotClipped;
665}
666
Chris Dalton79471932017-10-27 01:50:57 -0600667void GrReducedClip::makeEmpty() {
668 fHasScissor = false;
669 fAAClipRectGenID = SK_InvalidGenID;
670 fWindowRects.reset();
671 fMaskElements.reset();
672 fInitialState = InitialState::kAllOut;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000673}
csmartdaltonbde96c62016-08-31 12:54:46 -0700674
675////////////////////////////////////////////////////////////////////////////////
676// Create a 8-bit clip mask in alpha
677
Brian Osman11052242016-10-27 14:47:55 -0400678static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700679 const GrFixedClip& clip,
680 const GrUserStencilSettings* ss,
681 const SkMatrix& viewMatrix,
682 const SkClipStack::Element* element) {
Chris Dalton3b51df12017-11-27 14:33:06 -0700683 GrAA aa = GrAA(element->isAA());
Brian Salomonf3b46e52017-08-30 11:37:57 -0400684 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600685 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700686 SkDEBUGFAIL("Should never get here with an empty element.");
687 break;
Chris Dalton79471932017-10-27 01:50:57 -0600688 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400689 return rtc->priv().drawAndStencilRect(clip, ss, (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500690 element->isInverseFilled(), aa, viewMatrix,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400691 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700692 break;
693 default: {
694 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400695 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700696 if (path.isInverseFillType()) {
697 path.toggleInverseFillType();
698 }
699
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500700 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
701 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700702 break;
703 }
704 }
705
706 return false;
707}
708
Brian Osman11052242016-10-27 14:47:55 -0400709static void draw_element(GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500710 const GrClip& clip, // TODO: can this just always be WideOpen?
711 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500712 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700713 const SkMatrix& viewMatrix,
714 const SkClipStack::Element* element) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700715 // TODO: Draw rrects directly here.
Brian Salomonf3b46e52017-08-30 11:37:57 -0400716 switch (element->getDeviceSpaceType()) {
Chris Dalton79471932017-10-27 01:50:57 -0600717 case SkClipStack::Element::DeviceSpaceType::kEmpty:
csmartdaltonbde96c62016-08-31 12:54:46 -0700718 SkDEBUGFAIL("Should never get here with an empty element.");
719 break;
Chris Dalton79471932017-10-27 01:50:57 -0600720 case SkClipStack::Element::DeviceSpaceType::kRect:
Brian Salomonf3b46e52017-08-30 11:37:57 -0400721 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700722 break;
723 default: {
724 SkPath path;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400725 element->asDeviceSpacePath(&path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700726 if (path.isInverseFillType()) {
727 path.toggleInverseFillType();
728 }
729
Brian Salomon82f44312017-01-11 13:42:54 -0500730 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700731 break;
732 }
733 }
734}
735
Brian Osman11052242016-10-27 14:47:55 -0400736bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700737 // The texture may be larger than necessary, this rect represents the part of the texture
738 // we populate with a rasterization of the clip.
Chris Dalton79471932017-10-27 01:50:57 -0600739 GrFixedClip clip(SkIRect::MakeWH(fScissor.width(), fScissor.height()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700740
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700741 if (!fWindowRects.empty()) {
Chris Dalton79471932017-10-27 01:50:57 -0600742 clip.setWindowRectangles(fWindowRects.makeOffset(-fScissor.left(), -fScissor.top()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700743 GrWindowRectsState::Mode::kExclusive);
744 }
745
csmartdaltonbde96c62016-08-31 12:54:46 -0700746 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
747 // clear the part that we care about.
748 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Chris Dalton344e9032017-12-11 15:42:09 -0700749 rtc->priv().clear(clip, initialCoverage, GrRenderTargetContext::CanClearFullscreen::kYes);
csmartdaltonbde96c62016-08-31 12:54:46 -0700750
751 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
752 SkMatrix translate;
Chris Dalton79471932017-10-27 01:50:57 -0600753 translate.setTranslate(SkIntToScalar(-fScissor.left()), SkIntToScalar(-fScissor.top()));
csmartdaltonbde96c62016-08-31 12:54:46 -0700754
755 // walk through each clip element and perform its set op
Chris Dalton79471932017-10-27 01:50:57 -0600756 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700757 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700758 SkRegion::Op op = (SkRegion::Op)element->getOp();
Chris Dalton3b51df12017-11-27 14:33:06 -0700759 GrAA aa = GrAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700760 bool invert = element->isInverseFilled();
761 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
762 // draw directly into the result with the stencil set to make the pixels affected
763 // by the clip shape be non-zero.
764 static constexpr GrUserStencilSettings kStencilInElement(
765 GrUserStencilSettings::StaticInit<
766 0xffff,
767 GrUserStencilTest::kAlways,
768 0xffff,
769 GrUserStencilOp::kReplace,
770 GrUserStencilOp::kReplace,
771 0xffff>()
772 );
Brian Osman11052242016-10-27 14:47:55 -0400773 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700774 return false;
775 }
776
777 // Draw to the exterior pixels (those with a zero stencil value).
778 static constexpr GrUserStencilSettings kDrawOutsideElement(
779 GrUserStencilSettings::StaticInit<
780 0x0000,
781 GrUserStencilTest::kEqual,
782 0xffff,
783 GrUserStencilOp::kZero,
784 GrUserStencilOp::kZero,
785 0xffff>()
786 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500787 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Chris Dalton79471932017-10-27 01:50:57 -0600788 translate, SkRect::Make(fScissor))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700789 return false;
790 }
791 } else {
792 // all the remaining ops can just be directly draw into the accumulation buffer
793 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700794 paint.setCoverageSetOpXPFactory(op, false);
795
Brian Salomon82f44312017-01-11 13:42:54 -0500796 draw_element(rtc, clip, std::move(paint), aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700797 }
798 }
799
800 return true;
801}
802
803////////////////////////////////////////////////////////////////////////////////
804// Create a 1-bit clip mask in the stencil buffer.
805
csmartdaltonbde96c62016-08-31 12:54:46 -0700806bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400807 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700808 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Chris Daltonbbfd5162017-11-07 13:35:22 -0700809 GrStencilClip stencilClip(fScissor, this->maskGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700810
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700811 if (!fWindowRects.empty()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700812 stencilClip.fixedClip().setWindowRectangles(fWindowRects,
813 GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700814 }
815
csmartdaltonbde96c62016-08-31 12:54:46 -0700816 bool initialState = InitialState::kAllIn == this->initialState();
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000817 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700818
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500819 // walk through each clip element and perform its set op with the existing clip.
Chris Dalton79471932017-10-27 01:50:57 -0600820 for (ElementList::Iter iter(fMaskElements); iter.get(); iter.next()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700821 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500822 GrAAType aaType = GrAAType::kNone;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400823 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500824 aaType = GrAAType::kMSAA;
825 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700826
827 bool fillInverted = false;
828
829 // This will be used to determine whether the clip shape can be rendered into the
830 // stencil with arbitrary stencil settings.
831 GrPathRenderer::StencilSupport stencilSupport;
832
reed73603f32016-09-20 08:42:38 -0700833 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700834
835 GrPathRenderer* pr = nullptr;
836 SkPath clipPath;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400837 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700838 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
839 fillInverted = false;
840 } else {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400841 element->asDeviceSpacePath(&clipPath);
csmartdaltonbde96c62016-08-31 12:54:46 -0700842 fillInverted = clipPath.isInverseFillType();
843 if (fillInverted) {
844 clipPath.toggleInverseFillType();
845 }
846
847 GrShape shape(clipPath, GrStyle::SimpleFill());
848 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700849 canDrawArgs.fCaps = context->caps();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600850 canDrawArgs.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400851 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700852 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500853 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700854 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700855
856 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500857 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700858 &stencilSupport);
859 if (!pr) {
860 return false;
861 }
862 }
863
864 bool canRenderDirectToStencil =
865 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
866 bool drawDirectToClip; // Given the renderer, the element,
867 // fill rule, and set operation should
868 // we render the element directly to
869 // stencil bit used for clipping.
870 GrUserStencilSettings const* const* stencilPasses =
871 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
872 &drawDirectToClip);
873
874 // draw the element to the client stencil bits if necessary
875 if (!drawDirectToClip) {
876 static constexpr GrUserStencilSettings kDrawToStencil(
877 GrUserStencilSettings::StaticInit<
878 0x0000,
879 GrUserStencilTest::kAlways,
880 0xffff,
881 GrUserStencilOp::kIncMaybeClamp,
882 GrUserStencilOp::kIncMaybeClamp,
883 0xffff>()
884 );
Brian Salomonf3b46e52017-08-30 11:37:57 -0400885 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400886 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400887 aaType, SkMatrix::I(),
888 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700889 } else {
890 if (!clipPath.isEmpty()) {
891 GrShape shape(clipPath, GrStyle::SimpleFill());
892 if (canRenderDirectToStencil) {
893 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500894 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700895
Robert Phillips256c37b2017-03-01 14:32:46 -0500896 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500897 std::move(paint),
898 &kDrawToStencil,
899 renderTargetContext,
900 &stencilClip.fixedClip(),
Chris Daltondb91c6e2017-09-08 16:25:08 -0600901 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400902 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500903 &shape,
904 aaType,
905 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700906 pr->drawPath(args);
907 } else {
908 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500909 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400910 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700911 args.fClip = &stencilClip.fixedClip();
Chris Daltondb91c6e2017-09-08 16:25:08 -0600912 args.fClipConservativeBounds = &stencilClip.fixedClip().scissorRect();
Brian Salomon9a767722017-03-13 17:57:28 -0400913 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500914 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700915 args.fShape = &shape;
916 pr->stencilPath(args);
917 }
918 }
919 }
920 }
921
922 // now we modify the clip bit by rendering either the clip
923 // element directly or a bounding rect of the entire clip.
924 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
925 if (drawDirectToClip) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400926 if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400927 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
Brian Salomonf3b46e52017-08-30 11:37:57 -0400928 SkMatrix::I(),
929 element->getDeviceSpaceRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700930 } else {
931 GrShape shape(clipPath, GrStyle::SimpleFill());
932 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500933 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500934 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500935 std::move(paint),
936 *pass,
937 renderTargetContext,
938 &stencilClip,
Chris Daltondb91c6e2017-09-08 16:25:08 -0600939 &stencilClip.fixedClip().scissorRect(),
Brian Salomon9a767722017-03-13 17:57:28 -0400940 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500941 &shape,
942 aaType,
943 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700944 pr->drawPath(args);
945 }
946 } else {
947 // The view matrix is setup to do clip space -> stencil space translation, so
948 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400949 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Chris Dalton79471932017-10-27 01:50:57 -0600950 SkRect::Make(fScissor));
csmartdaltonbde96c62016-08-31 12:54:46 -0700951 }
952 }
953 }
954 return true;
955}
Chris Daltona32a3c32017-12-05 10:05:21 -0700956
Robert Phillips777707b2018-01-17 11:40:14 -0500957std::unique_ptr<GrFragmentProcessor> GrReducedClip::finishAndDetachAnalyticFPs(
958 GrProxyProvider* proxyProvider, uint32_t opListID,
959 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) {
966 SkASSERT(fHasScissor);
Robert Phillips777707b2018-01-17 11:40:14 -0500967 auto fp = fCCPR->makeClipProcessor(proxyProvider, opListID, ccprClipPath, fScissor,
968 rtWidth, rtHeight);
Chris Daltona32a3c32017-12-05 10:05:21 -0700969 fAnalyticFPs.push_back(std::move(fp));
970 }
971 fCCPRClipPaths.reset();
972 }
973
974 return GrFragmentProcessor::RunInSeries(fAnalyticFPs.begin(), fAnalyticFPs.count());
975}