blob: 9802f877557ecd73ab2220b6851c532f24ff0c59 [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"
csmartdaltonbde96c62016-08-31 12:54:46 -070020#include "GrStyle.h"
21#include "GrUserStencilSettings.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050022#include "SkClipOpPriv.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070023
bsalomon@google.com170bd792012-12-05 22:26:11 +000024typedef SkClipStack::Element Element;
bsalomon@google.com170bd792012-12-05 22:26:11 +000025
csmartdalton5ecbbbe2016-08-23 13:26:40 -070026/**
27 * There are plenty of optimizations that could be added here. Maybe flips could be folded into
28 * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
29 * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
30 * based on later intersect operations, and perhaps remove intersect-rects. We could optionally
31 * take a rect in case the caller knows a bound on what is to be drawn through this clip.
32 */
csmartdaltonbf4a8f92016-09-06 10:01:06 -070033GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
34 int maxWindowRectangles) {
csmartdaltoncbecb082016-07-22 08:59:08 -070035 SkASSERT(!queryBounds.isEmpty());
csmartdaltond211e782016-08-15 11:17:19 -070036 fHasIBounds = false;
csmartdaltoncbecb082016-07-22 08:59:08 -070037
bsalomon@google.com170bd792012-12-05 22:26:11 +000038 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070039 fInitialState = InitialState::kAllIn;
40 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000041 }
42
43 SkClipStack::BoundsType stackBoundsType;
44 SkRect stackBounds;
45 bool iior;
46 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
47
csmartdaltoncbecb082016-07-22 08:59:08 -070048 if (stackBounds.isEmpty() || GrClip::IsOutsideClip(stackBounds, queryBounds)) {
49 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070050 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
51 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000052 }
53
csmartdaltoncbecb082016-07-22 08:59:08 -070054 if (iior) {
55 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
56 // This should only be true if aa/non-aa status matches among all elements.
57 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
58 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
59 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
60 // The clip is a non-aa rect. This is the one spot where we can actually implement the
csmartdalton77f2fae2016-08-08 09:55:06 -070061 // clip (using fIBounds) rather than just telling the caller what it should be.
62 stackBounds.round(&fIBounds);
csmartdaltond211e782016-08-15 11:17:19 -070063 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070064 fInitialState = fIBounds.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
65 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070066 }
67 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070068 fInitialState = InitialState::kAllIn;
69 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070070 }
71
csmartdaltond211e782016-08-15 11:17:19 -070072 SkRect tightBounds;
73 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
74 fIBounds = GrClip::GetPixelIBounds(tightBounds);
csmartdalton77f2fae2016-08-08 09:55:06 -070075 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070076 fHasIBounds = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070077
csmartdalton77f2fae2016-08-08 09:55:06 -070078 // Implement the clip with an AA rect element.
Mike Reedc1f77742016-12-09 09:00:50 -050079 fElements.addToHead(stackBounds, kReplace_SkClipOp, true/*doAA*/);
csmartdalton8d3f92a2016-08-17 09:39:38 -070080 fElementsGenID = stack.getTopmostGenID();
csmartdalton77f2fae2016-08-08 09:55:06 -070081 fRequiresAA = true;
82
83 fInitialState = InitialState::kAllOut;
84 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070085 }
86
87 SkRect tighterQuery = queryBounds;
88 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
89 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This new
csmartdalton77f2fae2016-08-08 09:55:06 -070090 // clip will be enforced by the scissor through fIBounds.)
csmartdaltoncbecb082016-07-22 08:59:08 -070091 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
csmartdaltoncbecb082016-07-22 08:59:08 -070092 }
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000093
csmartdaltond211e782016-08-15 11:17:19 -070094 fIBounds = GrClip::GetPixelIBounds(tighterQuery);
csmartdalton77f2fae2016-08-08 09:55:06 -070095 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070096 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070097
bsalomon@google.com34cd70a2012-12-06 14:23:20 +000098 // Now that we have determined the bounds to use and filtered out the trivial cases, call the
99 // helper that actually walks the stack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700100 this->walkStack(stack, tighterQuery, maxWindowRectangles);
101
102 if (fWindowRects.count() < maxWindowRectangles) {
103 this->addInteriorWindowRectangles(maxWindowRectangles);
104 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700105}
106
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700107void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
108 int maxWindowRectangles) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700109 // walk backwards until we get to:
110 // a) the beginning
111 // b) an operation that is known to make the bounds all inside/outside
112 // c) a replace operation
113
114 enum class InitialTriState {
115 kUnknown = -1,
116 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
117 kAllOut = (int)GrReducedClip::InitialState::kAllOut
118 } initialTriState = InitialTriState::kUnknown;
119
120 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
121 // TODO: track these per saved clip so that we can consider them on the forward pass.
122 bool embiggens = false;
123 bool emsmallens = false;
124
125 // We use a slightly relaxed set of query bounds for element containment tests. This is to
126 // account for floating point rounding error that may have occurred during coord transforms.
127 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
128 GrClip::kBoundsTolerance);
129
130 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
131 int numAAElements = 0;
132 while (InitialTriState::kUnknown == initialTriState) {
133 const Element* element = iter.prev();
134 if (nullptr == element) {
135 initialTriState = InitialTriState::kAllIn;
136 break;
137 }
138 if (SkClipStack::kEmptyGenID == element->getGenID()) {
139 initialTriState = InitialTriState::kAllOut;
140 break;
141 }
142 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
143 initialTriState = InitialTriState::kAllIn;
144 break;
145 }
146
147 bool skippable = false;
148 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
149
150 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500151 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700152 // check if the shape subtracted either contains the entire bounds (and makes
153 // the clip empty) or is outside the bounds and therefore can be skipped.
154 if (element->isInverseFilled()) {
155 if (element->contains(relaxedQueryBounds)) {
156 skippable = true;
157 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
158 initialTriState = InitialTriState::kAllOut;
159 skippable = true;
160 }
161 } else {
162 if (element->contains(relaxedQueryBounds)) {
163 initialTriState = InitialTriState::kAllOut;
164 skippable = true;
165 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
166 skippable = true;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700167 } else if (fWindowRects.count() < maxWindowRectangles && !embiggens &&
168 !element->isAA() && Element::kRect_Type == element->getType()) {
169 this->addWindowRectangle(element->getRect(), false);
170 skippable = true;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700171 }
172 }
173 if (!skippable) {
174 emsmallens = true;
175 }
176 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500177 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700178 // check if the shape intersected contains the entire bounds and therefore can
179 // be skipped or it is outside the entire bounds and therefore makes the clip
180 // empty.
181 if (element->isInverseFilled()) {
182 if (element->contains(relaxedQueryBounds)) {
183 initialTriState = InitialTriState::kAllOut;
184 skippable = true;
185 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
186 skippable = true;
187 }
188 } else {
189 if (element->contains(relaxedQueryBounds)) {
190 skippable = true;
191 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
192 initialTriState = InitialTriState::kAllOut;
193 skippable = true;
194 } else if (!embiggens && !element->isAA() &&
195 Element::kRect_Type == element->getType()) {
196 // fIBounds and queryBounds have already acccounted for this element via
197 // clip stack bounds; here we just apply the non-aa rounding effect.
198 SkIRect nonaaRect;
199 element->getRect().round(&nonaaRect);
200 if (!this->intersectIBounds(nonaaRect)) {
201 return;
202 }
203 skippable = true;
204 }
205 }
206 if (!skippable) {
207 emsmallens = true;
208 }
209 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500210 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700211 // If the union-ed shape contains the entire bounds then after this element
212 // the bounds is entirely inside the clip. If the union-ed shape is outside the
213 // bounds then this op can be skipped.
214 if (element->isInverseFilled()) {
215 if (element->contains(relaxedQueryBounds)) {
216 skippable = true;
217 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
218 initialTriState = InitialTriState::kAllIn;
219 skippable = true;
220 }
221 } else {
222 if (element->contains(relaxedQueryBounds)) {
223 initialTriState = InitialTriState::kAllIn;
224 skippable = true;
225 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
226 skippable = true;
227 }
228 }
229 if (!skippable) {
230 embiggens = true;
231 }
232 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500233 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700234 // If the bounds is entirely inside the shape being xor-ed then the effect is
235 // to flip the inside/outside state of every point in the bounds. We may be
236 // able to take advantage of this in the forward pass. If the xor-ed shape
237 // doesn't intersect the bounds then it can be skipped.
238 if (element->isInverseFilled()) {
239 if (element->contains(relaxedQueryBounds)) {
240 skippable = true;
241 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
242 isFlip = true;
243 }
244 } else {
245 if (element->contains(relaxedQueryBounds)) {
246 isFlip = true;
247 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
248 skippable = true;
249 }
250 }
251 if (!skippable) {
252 emsmallens = embiggens = true;
253 }
254 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500255 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700256 // When the bounds is entirely within the rev-diff shape then this behaves like xor
257 // and reverses every point inside the bounds. If the shape is completely outside
258 // the bounds then we know after this element is applied that the bounds will be
259 // all outside the current clip.B
260 if (element->isInverseFilled()) {
261 if (element->contains(relaxedQueryBounds)) {
262 initialTriState = InitialTriState::kAllOut;
263 skippable = true;
264 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
265 isFlip = true;
266 }
267 } else {
268 if (element->contains(relaxedQueryBounds)) {
269 isFlip = true;
270 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
271 initialTriState = InitialTriState::kAllOut;
272 skippable = true;
273 }
274 }
275 if (!skippable) {
276 emsmallens = embiggens = true;
277 }
278 break;
279
Mike Reedc1f77742016-12-09 09:00:50 -0500280 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700281 // Replace will always terminate our walk. We will either begin the forward walk
282 // at the replace op or detect here than the shape is either completely inside
283 // or completely outside the bounds. In this latter case it can be skipped by
284 // setting the correct value for initialTriState.
285 if (element->isInverseFilled()) {
286 if (element->contains(relaxedQueryBounds)) {
287 initialTriState = InitialTriState::kAllOut;
288 skippable = true;
289 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
290 initialTriState = InitialTriState::kAllIn;
291 skippable = true;
292 }
293 } else {
294 if (element->contains(relaxedQueryBounds)) {
295 initialTriState = InitialTriState::kAllIn;
296 skippable = true;
297 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
298 initialTriState = InitialTriState::kAllOut;
299 skippable = true;
300 } else if (!embiggens && !element->isAA() &&
301 Element::kRect_Type == element->getType()) {
302 // fIBounds and queryBounds have already acccounted for this element via
303 // clip stack bounds; here we just apply the non-aa rounding effect.
304 SkIRect nonaaRect;
305 element->getRect().round(&nonaaRect);
306 if (!this->intersectIBounds(nonaaRect)) {
307 return;
308 }
309 initialTriState = InitialTriState::kAllIn;
310 skippable = true;
311 }
312 }
313 if (!skippable) {
314 initialTriState = InitialTriState::kAllOut;
315 embiggens = emsmallens = true;
316 }
317 break;
318 default:
319 SkDEBUGFAIL("Unexpected op.");
320 break;
321 }
322 if (!skippable) {
323 if (0 == fElements.count()) {
324 // This will be the last element. Record the stricter genID.
325 fElementsGenID = element->getGenID();
326 }
327
328 // if it is a flip, change it to a bounds-filling rect
329 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500330 SkASSERT(kXOR_SkClipOp == element->getOp() ||
331 kReverseDifference_SkClipOp == element->getOp());
332 fElements.addToHead(SkRect::Make(fIBounds), kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700333 } else {
334 Element* newElement = fElements.addToHead(*element);
335 if (newElement->isAA()) {
336 ++numAAElements;
337 }
338 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
339 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
340 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500341 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700342 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500343 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700344 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500345 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700346 if (isReplace) {
347 SkASSERT(InitialTriState::kAllOut == initialTriState);
348 initialTriState = InitialTriState::kAllIn;
349 }
350 }
351 }
352 }
353 }
354
355 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
356 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
357 fElements.reset();
358 numAAElements = 0;
359 } else {
360 Element* element = fElements.headIter().get();
361 while (element) {
362 bool skippable = false;
363 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500364 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700365 // subtracting from the empty set yields the empty set.
366 skippable = InitialTriState::kAllOut == initialTriState;
367 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500368 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700369 // intersecting with the empty set yields the empty set
370 if (InitialTriState::kAllOut == initialTriState) {
371 skippable = true;
372 } else {
373 // We can clear to zero and then simply draw the clip element.
374 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500375 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700376 }
377 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500378 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700379 if (InitialTriState::kAllIn == initialTriState) {
380 // unioning the infinite plane with anything is a no-op.
381 skippable = true;
382 } else {
383 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500384 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700385 }
386 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500387 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700388 if (InitialTriState::kAllOut == initialTriState) {
389 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500390 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700391 }
392 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500393 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700394 if (InitialTriState::kAllIn == initialTriState) {
395 // subtracting the whole plane will yield the empty set.
396 skippable = true;
397 initialTriState = InitialTriState::kAllOut;
398 } else {
399 // this picks up flips inserted in the backwards pass.
400 skippable = element->isInverseFilled() ?
401 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
402 element->contains(relaxedQueryBounds);
403 if (skippable) {
404 initialTriState = InitialTriState::kAllIn;
405 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500406 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700407 }
408 }
409 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500410 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700411 skippable = false; // we would have skipped it in the backwards walk if we
412 // could've.
413 break;
414 default:
415 SkDEBUGFAIL("Unexpected op.");
416 break;
417 }
418 if (!skippable) {
419 break;
420 } else {
421 if (element->isAA()) {
422 --numAAElements;
423 }
424 fElements.popHead();
425 element = fElements.headIter().get();
426 }
427 }
428 }
429 fRequiresAA = numAAElements > 0;
430
431 SkASSERT(InitialTriState::kUnknown != initialTriState);
432 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
433}
434
Mike Reedc1f77742016-12-09 09:00:50 -0500435static bool element_is_pure_subtract(SkClipOp op) {
Mike Reedebfce6d2016-12-12 10:02:12 -0500436 SkASSERT(static_cast<int>(op) >= 0);
437 return static_cast<int>(op) <= static_cast<int>(kIntersect_SkClipOp);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700438
Mike Reedebfce6d2016-12-12 10:02:12 -0500439 GR_STATIC_ASSERT(0 == static_cast<int>(kDifference_SkClipOp));
440 GR_STATIC_ASSERT(1 == static_cast<int>(kIntersect_SkClipOp));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700441}
442
443void GrReducedClip::addInteriorWindowRectangles(int maxWindowRectangles) {
444 SkASSERT(fWindowRects.count() < maxWindowRectangles);
445 // Walk backwards through the element list and add window rectangles to the interiors of
446 // "difference" elements. Quit if we encounter an element that may grow the clip.
447 ElementList::Iter iter(fElements, ElementList::Iter::kTail_IterStart);
448 for (; iter.get() && element_is_pure_subtract(iter.get()->getOp()); iter.prev()) {
449 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500450 if (kDifference_SkClipOp != element->getOp()) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700451 continue;
452 }
453
454 if (Element::kRect_Type == element->getType()) {
455 SkASSERT(element->isAA());
456 this->addWindowRectangle(element->getRect(), true);
457 if (fWindowRects.count() >= maxWindowRectangles) {
458 return;
459 }
460 continue;
461 }
462
463 if (Element::kRRect_Type == element->getType()) {
464 // For round rects we add two overlapping windows in the shape of a plus.
465 const SkRRect& clipRRect = element->getRRect();
466 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
467 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
468 if (SkRRect::kComplex_Type == clipRRect.getType()) {
469 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
470 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
471 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
472 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
473 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
474 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
475 }
476 const SkRect& bounds = clipRRect.getBounds();
477 if (insetTL.x() + insetBR.x() >= bounds.width() ||
478 insetTL.y() + insetBR.y() >= bounds.height()) {
479 continue; // The interior "plus" is empty.
480 }
481
482 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
483 bounds.right(), bounds.bottom() - insetBR.y());
484 this->addWindowRectangle(horzRect, element->isAA());
485 if (fWindowRects.count() >= maxWindowRectangles) {
486 return;
487 }
488
489 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
490 bounds.right() - insetBR.x(), bounds.bottom());
491 this->addWindowRectangle(vertRect, element->isAA());
492 if (fWindowRects.count() >= maxWindowRectangles) {
493 return;
494 }
495 continue;
496 }
497 }
498}
499
500inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
501 SkIRect window;
502 if (!elementIsAA) {
503 elementInteriorRect.round(&window);
504 } else {
505 elementInteriorRect.roundIn(&window);
506 }
507 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
508 fWindowRects.addWindow(window);
509 }
510}
511
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700512inline bool GrReducedClip::intersectIBounds(const SkIRect& irect) {
513 SkASSERT(fHasIBounds);
514 if (!fIBounds.intersect(irect)) {
515 fHasIBounds = false;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700516 fWindowRects.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700517 fElements.reset();
518 fRequiresAA = false;
519 fInitialState = InitialState::kAllOut;
520 return false;
521 }
522 return true;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000523}
csmartdaltonbde96c62016-08-31 12:54:46 -0700524
525////////////////////////////////////////////////////////////////////////////////
526// Create a 8-bit clip mask in alpha
527
Brian Osman11052242016-10-27 14:47:55 -0400528static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700529 const GrFixedClip& clip,
530 const GrUserStencilSettings* ss,
531 const SkMatrix& viewMatrix,
532 const SkClipStack::Element* element) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500533 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700534 switch (element->getType()) {
535 case Element::kEmpty_Type:
536 SkDEBUGFAIL("Should never get here with an empty element.");
537 break;
538 case Element::kRect_Type:
Brian Osman693a5402016-10-27 15:13:22 -0400539 return rtc->priv().drawAndStencilRect(clip, ss,
540 (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500541 element->isInverseFilled(), aa, viewMatrix,
Brian Osman693a5402016-10-27 15:13:22 -0400542 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700543 break;
544 default: {
545 SkPath path;
546 element->asPath(&path);
547 if (path.isInverseFillType()) {
548 path.toggleInverseFillType();
549 }
550
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500551 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
552 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700553 break;
554 }
555 }
556
557 return false;
558}
559
Brian Osman11052242016-10-27 14:47:55 -0400560static void draw_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700561 const GrClip& clip, // TODO: can this just always be WideOpen?
562 const GrPaint &paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500563 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700564 const SkMatrix& viewMatrix,
565 const SkClipStack::Element* element) {
566
567 // TODO: Draw rrects directly here.
568 switch (element->getType()) {
569 case Element::kEmpty_Type:
570 SkDEBUGFAIL("Should never get here with an empty element.");
571 break;
572 case Element::kRect_Type:
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500573 rtc->drawRect(clip, paint, aa, viewMatrix, element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700574 break;
575 default: {
576 SkPath path;
577 element->asPath(&path);
578 if (path.isInverseFillType()) {
579 path.toggleInverseFillType();
580 }
581
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500582 rtc->drawPath(clip, paint, aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700583 break;
584 }
585 }
586}
587
Brian Osman11052242016-10-27 14:47:55 -0400588bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700589 // The texture may be larger than necessary, this rect represents the part of the texture
590 // we populate with a rasterization of the clip.
591 GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height()));
592
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700593 if (!fWindowRects.empty()) {
594 clip.setWindowRectangles(fWindowRects, {fIBounds.left(), fIBounds.top()},
595 GrWindowRectsState::Mode::kExclusive);
596 }
597
csmartdaltonbde96c62016-08-31 12:54:46 -0700598 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
599 // clear the part that we care about.
600 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400601 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700602
603 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
604 SkMatrix translate;
605 translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top()));
606
607 // walk through each clip element and perform its set op
608 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
609 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700610 SkRegion::Op op = (SkRegion::Op)element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500611 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700612 bool invert = element->isInverseFilled();
613 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
614 // draw directly into the result with the stencil set to make the pixels affected
615 // by the clip shape be non-zero.
616 static constexpr GrUserStencilSettings kStencilInElement(
617 GrUserStencilSettings::StaticInit<
618 0xffff,
619 GrUserStencilTest::kAlways,
620 0xffff,
621 GrUserStencilOp::kReplace,
622 GrUserStencilOp::kReplace,
623 0xffff>()
624 );
Brian Osman11052242016-10-27 14:47:55 -0400625 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700626 return false;
627 }
628
629 // Draw to the exterior pixels (those with a zero stencil value).
630 static constexpr GrUserStencilSettings kDrawOutsideElement(
631 GrUserStencilSettings::StaticInit<
632 0x0000,
633 GrUserStencilTest::kEqual,
634 0xffff,
635 GrUserStencilOp::kZero,
636 GrUserStencilOp::kZero,
637 0xffff>()
638 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500639 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Brian Osman693a5402016-10-27 15:13:22 -0400640 translate, SkRect::Make(fIBounds))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700641 return false;
642 }
643 } else {
644 // all the remaining ops can just be directly draw into the accumulation buffer
645 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700646 paint.setCoverageSetOpXPFactory(op, false);
647
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500648 draw_element(rtc, clip, paint, aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700649 }
650 }
651
652 return true;
653}
654
655////////////////////////////////////////////////////////////////////////////////
656// Create a 1-bit clip mask in the stencil buffer.
657
658class StencilClip final : public GrClip {
659public:
660 StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {}
661 const GrFixedClip& fixedClip() const { return fFixedClip; }
662
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700663 void setWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin,
664 GrWindowRectsState::Mode mode) {
665 fFixedClip.setWindowRectangles(windows, origin, mode);
666 }
667
csmartdaltonbde96c62016-08-31 12:54:46 -0700668private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700669 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700670 return false;
671 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700672 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
673 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700674 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500675 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700676 return false;
677 }
Brian Osman11052242016-10-27 14:47:55 -0400678 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700679 bool hasUserStencilSettings, GrAppliedClip* out) const override {
Brian Osman11052242016-10-27 14:47:55 -0400680 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700681 return false;
682 }
683 out->addStencilClip();
684 return true;
685 }
686
687 GrFixedClip fFixedClip;
688
689 typedef GrClip INHERITED;
690};
691
692bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400693 GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700694 const SkIPoint& clipOrigin) const {
695 // We set the current clip to the bounds so that our recursive draws are scissored to them.
696 StencilClip stencilClip(fIBounds.makeOffset(-clipOrigin.x(), -clipOrigin.y()));
697
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700698 if (!fWindowRects.empty()) {
699 stencilClip.setWindowRectangles(fWindowRects, clipOrigin,
700 GrWindowRectsState::Mode::kExclusive);
701 }
702
csmartdaltonbde96c62016-08-31 12:54:46 -0700703 bool initialState = InitialState::kAllIn == this->initialState();
Brian Osman693a5402016-10-27 15:13:22 -0400704 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700705
706 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
707 SkMatrix viewMatrix;
708 viewMatrix.setTranslate(SkIntToScalar(-clipOrigin.x()), SkIntToScalar(-clipOrigin.y()));
709
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500710 // walk through each clip element and perform its set op with the existing clip.
csmartdaltonbde96c62016-08-31 12:54:46 -0700711 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
712 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500713 GrAAType aaType = GrAAType::kNone;
714 if (element->isAA() && renderTargetContext->isStencilBufferMultisampled()) {
715 aaType = GrAAType::kMSAA;
716 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700717
718 bool fillInverted = false;
719
720 // This will be used to determine whether the clip shape can be rendered into the
721 // stencil with arbitrary stencil settings.
722 GrPathRenderer::StencilSupport stencilSupport;
723
reed73603f32016-09-20 08:42:38 -0700724 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700725
726 GrPathRenderer* pr = nullptr;
727 SkPath clipPath;
728 if (Element::kRect_Type == element->getType()) {
729 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
730 fillInverted = false;
731 } else {
732 element->asPath(&clipPath);
733 fillInverted = clipPath.isInverseFillType();
734 if (fillInverted) {
735 clipPath.toggleInverseFillType();
736 }
737
738 GrShape shape(clipPath, GrStyle::SimpleFill());
739 GrPathRenderer::CanDrawPathArgs canDrawArgs;
740 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
741 canDrawArgs.fViewMatrix = &viewMatrix;
742 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500743 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700744 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700745
746 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500747 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700748 &stencilSupport);
749 if (!pr) {
750 return false;
751 }
752 }
753
754 bool canRenderDirectToStencil =
755 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
756 bool drawDirectToClip; // Given the renderer, the element,
757 // fill rule, and set operation should
758 // we render the element directly to
759 // stencil bit used for clipping.
760 GrUserStencilSettings const* const* stencilPasses =
761 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
762 &drawDirectToClip);
763
764 // draw the element to the client stencil bits if necessary
765 if (!drawDirectToClip) {
766 static constexpr GrUserStencilSettings kDrawToStencil(
767 GrUserStencilSettings::StaticInit<
768 0x0000,
769 GrUserStencilTest::kAlways,
770 0xffff,
771 GrUserStencilOp::kIncMaybeClamp,
772 GrUserStencilOp::kIncMaybeClamp,
773 0xffff>()
774 );
775 if (Element::kRect_Type == element->getType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400776 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500777 aaType, viewMatrix, element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700778 } else {
779 if (!clipPath.isEmpty()) {
780 GrShape shape(clipPath, GrStyle::SimpleFill());
781 if (canRenderDirectToStencil) {
782 GrPaint paint;
Brian Salomon003312a2017-01-09 16:00:33 +0000783 paint.setXPFactory(GrDisableColorXPFactory::Make());
csmartdaltonbde96c62016-08-31 12:54:46 -0700784
785 GrPathRenderer::DrawPathArgs args;
786 args.fResourceProvider = context->resourceProvider();
787 args.fPaint = &paint;
788 args.fUserStencilSettings = &kDrawToStencil;
Brian Osman11052242016-10-27 14:47:55 -0400789 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700790 args.fClip = &stencilClip.fixedClip();
791 args.fViewMatrix = &viewMatrix;
792 args.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500793 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700794 args.fGammaCorrect = false;
795 pr->drawPath(args);
796 } else {
797 GrPathRenderer::StencilPathArgs args;
798 args.fResourceProvider = context->resourceProvider();
Brian Osman11052242016-10-27 14:47:55 -0400799 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700800 args.fClip = &stencilClip.fixedClip();
801 args.fViewMatrix = &viewMatrix;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500802 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700803 args.fShape = &shape;
804 pr->stencilPath(args);
805 }
806 }
807 }
808 }
809
810 // now we modify the clip bit by rendering either the clip
811 // element directly or a bounding rect of the entire clip.
812 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
813 if (drawDirectToClip) {
814 if (Element::kRect_Type == element->getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500815 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, viewMatrix,
Brian Osman693a5402016-10-27 15:13:22 -0400816 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700817 } else {
818 GrShape shape(clipPath, GrStyle::SimpleFill());
819 GrPaint paint;
Brian Salomon003312a2017-01-09 16:00:33 +0000820 paint.setXPFactory(GrDisableColorXPFactory::Make());
csmartdaltonbde96c62016-08-31 12:54:46 -0700821 GrPathRenderer::DrawPathArgs args;
822 args.fResourceProvider = context->resourceProvider();
823 args.fPaint = &paint;
824 args.fUserStencilSettings = *pass;
Brian Osman11052242016-10-27 14:47:55 -0400825 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700826 args.fClip = &stencilClip;
827 args.fViewMatrix = &viewMatrix;
828 args.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500829 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700830 args.fGammaCorrect = false;
831 pr->drawPath(args);
832 }
833 } else {
834 // The view matrix is setup to do clip space -> stencil space translation, so
835 // draw rect in clip space.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500836 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, viewMatrix,
Brian Osman693a5402016-10-27 15:13:22 -0400837 SkRect::Make(fIBounds));
csmartdaltonbde96c62016-08-31 12:54:46 -0700838 }
839 }
840 }
841 return true;
842}