blob: dfa41668d4e9bb60817d44b337950e8c79b2544f [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
Chris Dalton348060f2017-06-05 13:15:37 -060048 if (GrClip::IsOutsideClip(stackBounds, queryBounds)) {
csmartdaltoncbecb082016-07-22 08:59:08 -070049 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);
Chris Dalton348060f2017-06-05 13:15:37 -060075 if (fIBounds.isEmpty()) {
76 fInitialState = InitialState::kAllOut;
77 return;
78 }
csmartdaltond211e782016-08-15 11:17:19 -070079 fHasIBounds = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070080
csmartdalton77f2fae2016-08-08 09:55:06 -070081 // Implement the clip with an AA rect element.
Mike Reedc1f77742016-12-09 09:00:50 -050082 fElements.addToHead(stackBounds, kReplace_SkClipOp, true/*doAA*/);
csmartdalton8d3f92a2016-08-17 09:39:38 -070083 fElementsGenID = stack.getTopmostGenID();
csmartdalton77f2fae2016-08-08 09:55:06 -070084 fRequiresAA = true;
85
86 fInitialState = InitialState::kAllOut;
87 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070088 }
89
90 SkRect tighterQuery = queryBounds;
91 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
92 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This new
csmartdalton77f2fae2016-08-08 09:55:06 -070093 // clip will be enforced by the scissor through fIBounds.)
csmartdaltoncbecb082016-07-22 08:59:08 -070094 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
csmartdaltoncbecb082016-07-22 08:59:08 -070095 }
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000096
csmartdaltond211e782016-08-15 11:17:19 -070097 fIBounds = GrClip::GetPixelIBounds(tighterQuery);
Chris Dalton348060f2017-06-05 13:15:37 -060098 if (fIBounds.isEmpty()) {
99 fInitialState = InitialState::kAllOut;
100 return;
101 }
csmartdaltond211e782016-08-15 11:17:19 -0700102 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -0700103
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000104 // Now that we have determined the bounds to use and filtered out the trivial cases, call the
105 // helper that actually walks the stack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700106 this->walkStack(stack, tighterQuery, maxWindowRectangles);
107
108 if (fWindowRects.count() < maxWindowRectangles) {
109 this->addInteriorWindowRectangles(maxWindowRectangles);
110 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700111}
112
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700113void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
114 int maxWindowRectangles) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700115 // walk backwards until we get to:
116 // a) the beginning
117 // b) an operation that is known to make the bounds all inside/outside
118 // c) a replace operation
119
120 enum class InitialTriState {
121 kUnknown = -1,
122 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
123 kAllOut = (int)GrReducedClip::InitialState::kAllOut
124 } initialTriState = InitialTriState::kUnknown;
125
126 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
127 // TODO: track these per saved clip so that we can consider them on the forward pass.
128 bool embiggens = false;
129 bool emsmallens = false;
130
131 // We use a slightly relaxed set of query bounds for element containment tests. This is to
132 // account for floating point rounding error that may have occurred during coord transforms.
133 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
134 GrClip::kBoundsTolerance);
135
136 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
137 int numAAElements = 0;
138 while (InitialTriState::kUnknown == initialTriState) {
139 const Element* element = iter.prev();
140 if (nullptr == element) {
141 initialTriState = InitialTriState::kAllIn;
142 break;
143 }
144 if (SkClipStack::kEmptyGenID == element->getGenID()) {
145 initialTriState = InitialTriState::kAllOut;
146 break;
147 }
148 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
149 initialTriState = InitialTriState::kAllIn;
150 break;
151 }
152
153 bool skippable = false;
154 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
155
156 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500157 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700158 // check if the shape subtracted either contains the entire bounds (and makes
159 // the clip empty) or is outside the bounds and therefore can be skipped.
160 if (element->isInverseFilled()) {
161 if (element->contains(relaxedQueryBounds)) {
162 skippable = true;
163 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
164 initialTriState = InitialTriState::kAllOut;
165 skippable = true;
166 }
167 } else {
168 if (element->contains(relaxedQueryBounds)) {
169 initialTriState = InitialTriState::kAllOut;
170 skippable = true;
171 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
172 skippable = true;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700173 } else if (fWindowRects.count() < maxWindowRectangles && !embiggens &&
174 !element->isAA() && Element::kRect_Type == element->getType()) {
175 this->addWindowRectangle(element->getRect(), false);
176 skippable = true;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700177 }
178 }
179 if (!skippable) {
180 emsmallens = true;
181 }
182 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500183 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700184 // check if the shape intersected contains the entire bounds and therefore can
185 // be skipped or it is outside the entire bounds and therefore makes the clip
186 // empty.
187 if (element->isInverseFilled()) {
188 if (element->contains(relaxedQueryBounds)) {
189 initialTriState = InitialTriState::kAllOut;
190 skippable = true;
191 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
192 skippable = true;
193 }
194 } else {
195 if (element->contains(relaxedQueryBounds)) {
196 skippable = true;
197 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
198 initialTriState = InitialTriState::kAllOut;
199 skippable = true;
200 } else if (!embiggens && !element->isAA() &&
201 Element::kRect_Type == element->getType()) {
202 // fIBounds and queryBounds have already acccounted for this element via
203 // clip stack bounds; here we just apply the non-aa rounding effect.
204 SkIRect nonaaRect;
205 element->getRect().round(&nonaaRect);
206 if (!this->intersectIBounds(nonaaRect)) {
207 return;
208 }
209 skippable = true;
210 }
211 }
212 if (!skippable) {
213 emsmallens = true;
214 }
215 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500216 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700217 // If the union-ed shape contains the entire bounds then after this element
218 // the bounds is entirely inside the clip. If the union-ed shape is outside the
219 // bounds then this op can be skipped.
220 if (element->isInverseFilled()) {
221 if (element->contains(relaxedQueryBounds)) {
222 skippable = true;
223 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
224 initialTriState = InitialTriState::kAllIn;
225 skippable = true;
226 }
227 } else {
228 if (element->contains(relaxedQueryBounds)) {
229 initialTriState = InitialTriState::kAllIn;
230 skippable = true;
231 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
232 skippable = true;
233 }
234 }
235 if (!skippable) {
236 embiggens = true;
237 }
238 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500239 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700240 // If the bounds is entirely inside the shape being xor-ed then the effect is
241 // to flip the inside/outside state of every point in the bounds. We may be
242 // able to take advantage of this in the forward pass. If the xor-ed shape
243 // doesn't intersect the bounds then it can be skipped.
244 if (element->isInverseFilled()) {
245 if (element->contains(relaxedQueryBounds)) {
246 skippable = true;
247 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
248 isFlip = true;
249 }
250 } else {
251 if (element->contains(relaxedQueryBounds)) {
252 isFlip = true;
253 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
254 skippable = true;
255 }
256 }
257 if (!skippable) {
258 emsmallens = embiggens = true;
259 }
260 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500261 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700262 // When the bounds is entirely within the rev-diff shape then this behaves like xor
263 // and reverses every point inside the bounds. If the shape is completely outside
264 // the bounds then we know after this element is applied that the bounds will be
265 // all outside the current clip.B
266 if (element->isInverseFilled()) {
267 if (element->contains(relaxedQueryBounds)) {
268 initialTriState = InitialTriState::kAllOut;
269 skippable = true;
270 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
271 isFlip = true;
272 }
273 } else {
274 if (element->contains(relaxedQueryBounds)) {
275 isFlip = true;
276 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
277 initialTriState = InitialTriState::kAllOut;
278 skippable = true;
279 }
280 }
281 if (!skippable) {
282 emsmallens = embiggens = true;
283 }
284 break;
285
Mike Reedc1f77742016-12-09 09:00:50 -0500286 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700287 // Replace will always terminate our walk. We will either begin the forward walk
288 // at the replace op or detect here than the shape is either completely inside
289 // or completely outside the bounds. In this latter case it can be skipped by
290 // setting the correct value for initialTriState.
291 if (element->isInverseFilled()) {
292 if (element->contains(relaxedQueryBounds)) {
293 initialTriState = InitialTriState::kAllOut;
294 skippable = true;
295 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
296 initialTriState = InitialTriState::kAllIn;
297 skippable = true;
298 }
299 } else {
300 if (element->contains(relaxedQueryBounds)) {
301 initialTriState = InitialTriState::kAllIn;
302 skippable = true;
303 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
304 initialTriState = InitialTriState::kAllOut;
305 skippable = true;
306 } else if (!embiggens && !element->isAA() &&
307 Element::kRect_Type == element->getType()) {
308 // fIBounds and queryBounds have already acccounted for this element via
309 // clip stack bounds; here we just apply the non-aa rounding effect.
310 SkIRect nonaaRect;
311 element->getRect().round(&nonaaRect);
312 if (!this->intersectIBounds(nonaaRect)) {
313 return;
314 }
315 initialTriState = InitialTriState::kAllIn;
316 skippable = true;
317 }
318 }
319 if (!skippable) {
320 initialTriState = InitialTriState::kAllOut;
321 embiggens = emsmallens = true;
322 }
323 break;
324 default:
325 SkDEBUGFAIL("Unexpected op.");
326 break;
327 }
328 if (!skippable) {
329 if (0 == fElements.count()) {
330 // This will be the last element. Record the stricter genID.
331 fElementsGenID = element->getGenID();
332 }
333
334 // if it is a flip, change it to a bounds-filling rect
335 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500336 SkASSERT(kXOR_SkClipOp == element->getOp() ||
337 kReverseDifference_SkClipOp == element->getOp());
338 fElements.addToHead(SkRect::Make(fIBounds), kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700339 } else {
340 Element* newElement = fElements.addToHead(*element);
341 if (newElement->isAA()) {
342 ++numAAElements;
343 }
344 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
345 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
346 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500347 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700348 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500349 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700350 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500351 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700352 if (isReplace) {
353 SkASSERT(InitialTriState::kAllOut == initialTriState);
354 initialTriState = InitialTriState::kAllIn;
355 }
356 }
357 }
358 }
359 }
360
361 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
362 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
363 fElements.reset();
364 numAAElements = 0;
365 } else {
366 Element* element = fElements.headIter().get();
367 while (element) {
368 bool skippable = false;
369 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500370 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700371 // subtracting from the empty set yields the empty set.
372 skippable = InitialTriState::kAllOut == initialTriState;
373 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500374 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700375 // intersecting with the empty set yields the empty set
376 if (InitialTriState::kAllOut == initialTriState) {
377 skippable = true;
378 } else {
379 // We can clear to zero and then simply draw the clip element.
380 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500381 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700382 }
383 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500384 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700385 if (InitialTriState::kAllIn == initialTriState) {
386 // unioning the infinite plane with anything is a no-op.
387 skippable = true;
388 } else {
389 // unioning the empty set with a shape is the shape.
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 kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700394 if (InitialTriState::kAllOut == initialTriState) {
395 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500396 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700397 }
398 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500399 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700400 if (InitialTriState::kAllIn == initialTriState) {
401 // subtracting the whole plane will yield the empty set.
402 skippable = true;
403 initialTriState = InitialTriState::kAllOut;
404 } else {
405 // this picks up flips inserted in the backwards pass.
406 skippable = element->isInverseFilled() ?
407 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
408 element->contains(relaxedQueryBounds);
409 if (skippable) {
410 initialTriState = InitialTriState::kAllIn;
411 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500412 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700413 }
414 }
415 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500416 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700417 skippable = false; // we would have skipped it in the backwards walk if we
418 // could've.
419 break;
420 default:
421 SkDEBUGFAIL("Unexpected op.");
422 break;
423 }
424 if (!skippable) {
425 break;
426 } else {
427 if (element->isAA()) {
428 --numAAElements;
429 }
430 fElements.popHead();
431 element = fElements.headIter().get();
432 }
433 }
434 }
435 fRequiresAA = numAAElements > 0;
436
437 SkASSERT(InitialTriState::kUnknown != initialTriState);
438 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
439}
440
Mike Reedc1f77742016-12-09 09:00:50 -0500441static bool element_is_pure_subtract(SkClipOp op) {
Mike Reedebfce6d2016-12-12 10:02:12 -0500442 SkASSERT(static_cast<int>(op) >= 0);
443 return static_cast<int>(op) <= static_cast<int>(kIntersect_SkClipOp);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700444
Mike Reedebfce6d2016-12-12 10:02:12 -0500445 GR_STATIC_ASSERT(0 == static_cast<int>(kDifference_SkClipOp));
446 GR_STATIC_ASSERT(1 == static_cast<int>(kIntersect_SkClipOp));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700447}
448
449void GrReducedClip::addInteriorWindowRectangles(int maxWindowRectangles) {
450 SkASSERT(fWindowRects.count() < maxWindowRectangles);
451 // Walk backwards through the element list and add window rectangles to the interiors of
452 // "difference" elements. Quit if we encounter an element that may grow the clip.
453 ElementList::Iter iter(fElements, ElementList::Iter::kTail_IterStart);
454 for (; iter.get() && element_is_pure_subtract(iter.get()->getOp()); iter.prev()) {
455 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500456 if (kDifference_SkClipOp != element->getOp()) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700457 continue;
458 }
459
460 if (Element::kRect_Type == element->getType()) {
461 SkASSERT(element->isAA());
462 this->addWindowRectangle(element->getRect(), true);
463 if (fWindowRects.count() >= maxWindowRectangles) {
464 return;
465 }
466 continue;
467 }
468
469 if (Element::kRRect_Type == element->getType()) {
470 // For round rects we add two overlapping windows in the shape of a plus.
471 const SkRRect& clipRRect = element->getRRect();
472 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
473 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
474 if (SkRRect::kComplex_Type == clipRRect.getType()) {
475 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
476 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
477 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
478 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
479 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
480 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
481 }
482 const SkRect& bounds = clipRRect.getBounds();
483 if (insetTL.x() + insetBR.x() >= bounds.width() ||
484 insetTL.y() + insetBR.y() >= bounds.height()) {
485 continue; // The interior "plus" is empty.
486 }
487
488 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
489 bounds.right(), bounds.bottom() - insetBR.y());
490 this->addWindowRectangle(horzRect, element->isAA());
491 if (fWindowRects.count() >= maxWindowRectangles) {
492 return;
493 }
494
495 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
496 bounds.right() - insetBR.x(), bounds.bottom());
497 this->addWindowRectangle(vertRect, element->isAA());
498 if (fWindowRects.count() >= maxWindowRectangles) {
499 return;
500 }
501 continue;
502 }
503 }
504}
505
506inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
507 SkIRect window;
508 if (!elementIsAA) {
509 elementInteriorRect.round(&window);
510 } else {
511 elementInteriorRect.roundIn(&window);
512 }
513 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
514 fWindowRects.addWindow(window);
515 }
516}
517
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700518inline bool GrReducedClip::intersectIBounds(const SkIRect& irect) {
519 SkASSERT(fHasIBounds);
520 if (!fIBounds.intersect(irect)) {
521 fHasIBounds = false;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700522 fWindowRects.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700523 fElements.reset();
524 fRequiresAA = false;
525 fInitialState = InitialState::kAllOut;
526 return false;
527 }
528 return true;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000529}
csmartdaltonbde96c62016-08-31 12:54:46 -0700530
531////////////////////////////////////////////////////////////////////////////////
532// Create a 8-bit clip mask in alpha
533
Brian Osman11052242016-10-27 14:47:55 -0400534static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700535 const GrFixedClip& clip,
536 const GrUserStencilSettings* ss,
537 const SkMatrix& viewMatrix,
538 const SkClipStack::Element* element) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500539 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700540 switch (element->getType()) {
541 case Element::kEmpty_Type:
542 SkDEBUGFAIL("Should never get here with an empty element.");
543 break;
544 case Element::kRect_Type:
Brian Osman693a5402016-10-27 15:13:22 -0400545 return rtc->priv().drawAndStencilRect(clip, ss,
546 (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500547 element->isInverseFilled(), aa, viewMatrix,
Brian Osman693a5402016-10-27 15:13:22 -0400548 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700549 break;
550 default: {
551 SkPath path;
552 element->asPath(&path);
553 if (path.isInverseFillType()) {
554 path.toggleInverseFillType();
555 }
556
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500557 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
558 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700559 break;
560 }
561 }
562
563 return false;
564}
565
Brian Osman11052242016-10-27 14:47:55 -0400566static void draw_element(GrRenderTargetContext* rtc,
Brian Salomon82f44312017-01-11 13:42:54 -0500567 const GrClip& clip, // TODO: can this just always be WideOpen?
568 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500569 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700570 const SkMatrix& viewMatrix,
571 const SkClipStack::Element* element) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700572 // TODO: Draw rrects directly here.
573 switch (element->getType()) {
574 case Element::kEmpty_Type:
575 SkDEBUGFAIL("Should never get here with an empty element.");
576 break;
577 case Element::kRect_Type:
Brian Salomon82f44312017-01-11 13:42:54 -0500578 rtc->drawRect(clip, std::move(paint), aa, viewMatrix, element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700579 break;
580 default: {
581 SkPath path;
582 element->asPath(&path);
583 if (path.isInverseFillType()) {
584 path.toggleInverseFillType();
585 }
586
Brian Salomon82f44312017-01-11 13:42:54 -0500587 rtc->drawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700588 break;
589 }
590 }
591}
592
Brian Osman11052242016-10-27 14:47:55 -0400593bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700594 // The texture may be larger than necessary, this rect represents the part of the texture
595 // we populate with a rasterization of the clip.
596 GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height()));
597
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700598 if (!fWindowRects.empty()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400599 clip.setWindowRectangles(fWindowRects.makeOffset(-fIBounds.left(), -fIBounds.top()),
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700600 GrWindowRectsState::Mode::kExclusive);
601 }
602
csmartdaltonbde96c62016-08-31 12:54:46 -0700603 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
604 // clear the part that we care about.
605 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400606 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700607
608 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
609 SkMatrix translate;
610 translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top()));
611
612 // walk through each clip element and perform its set op
613 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
614 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700615 SkRegion::Op op = (SkRegion::Op)element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500616 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700617 bool invert = element->isInverseFilled();
618 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
619 // draw directly into the result with the stencil set to make the pixels affected
620 // by the clip shape be non-zero.
621 static constexpr GrUserStencilSettings kStencilInElement(
622 GrUserStencilSettings::StaticInit<
623 0xffff,
624 GrUserStencilTest::kAlways,
625 0xffff,
626 GrUserStencilOp::kReplace,
627 GrUserStencilOp::kReplace,
628 0xffff>()
629 );
Brian Osman11052242016-10-27 14:47:55 -0400630 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700631 return false;
632 }
633
634 // Draw to the exterior pixels (those with a zero stencil value).
635 static constexpr GrUserStencilSettings kDrawOutsideElement(
636 GrUserStencilSettings::StaticInit<
637 0x0000,
638 GrUserStencilTest::kEqual,
639 0xffff,
640 GrUserStencilOp::kZero,
641 GrUserStencilOp::kZero,
642 0xffff>()
643 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500644 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Brian Osman693a5402016-10-27 15:13:22 -0400645 translate, SkRect::Make(fIBounds))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700646 return false;
647 }
648 } else {
649 // all the remaining ops can just be directly draw into the accumulation buffer
650 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700651 paint.setCoverageSetOpXPFactory(op, false);
652
Brian Salomon82f44312017-01-11 13:42:54 -0500653 draw_element(rtc, clip, std::move(paint), aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700654 }
655 }
656
657 return true;
658}
659
660////////////////////////////////////////////////////////////////////////////////
661// Create a 1-bit clip mask in the stencil buffer.
662
663class StencilClip final : public GrClip {
664public:
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400665 StencilClip(const SkIRect& scissorRect, int32_t clipStackID)
666 : fFixedClip(scissorRect)
667 , fClipStackID(clipStackID) {
668 }
669
csmartdaltonbde96c62016-08-31 12:54:46 -0700670 const GrFixedClip& fixedClip() const { return fFixedClip; }
671
Brian Salomon9a767722017-03-13 17:57:28 -0400672 void setWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
673 fFixedClip.setWindowRectangles(windows, mode);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700674 }
675
csmartdaltonbde96c62016-08-31 12:54:46 -0700676private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700677 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700678 return false;
679 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700680 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
681 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700682 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500683 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700684 return false;
685 }
Brian Osman11052242016-10-27 14:47:55 -0400686 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
Brian Salomon97180af2017-03-14 13:42:58 -0400687 bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
688 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out,
689 bounds)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700690 return false;
691 }
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400692 out->addStencilClip(fClipStackID);
csmartdaltonbde96c62016-08-31 12:54:46 -0700693 return true;
694 }
695
696 GrFixedClip fFixedClip;
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400697 int32_t fClipStackID;
csmartdaltonbde96c62016-08-31 12:54:46 -0700698
699 typedef GrClip INHERITED;
700};
701
702bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Salomon9a767722017-03-13 17:57:28 -0400703 GrRenderTargetContext* renderTargetContext) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700704 // We set the current clip to the bounds so that our recursive draws are scissored to them.
Robert Phillipsa4f792d2017-06-28 08:40:11 -0400705 StencilClip stencilClip(fIBounds, this->elementsGenID());
csmartdaltonbde96c62016-08-31 12:54:46 -0700706
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700707 if (!fWindowRects.empty()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400708 stencilClip.setWindowRectangles(fWindowRects, GrWindowRectsState::Mode::kExclusive);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700709 }
710
csmartdaltonbde96c62016-08-31 12:54:46 -0700711 bool initialState = InitialState::kAllIn == this->initialState();
Brian Osman693a5402016-10-27 15:13:22 -0400712 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700713
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500714 // walk through each clip element and perform its set op with the existing clip.
csmartdaltonbde96c62016-08-31 12:54:46 -0700715 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
716 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500717 GrAAType aaType = GrAAType::kNone;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400718 if (element->isAA() && GrFSAAType::kNone != renderTargetContext->fsaaType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500719 aaType = GrAAType::kMSAA;
720 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700721
722 bool fillInverted = false;
723
724 // This will be used to determine whether the clip shape can be rendered into the
725 // stencil with arbitrary stencil settings.
726 GrPathRenderer::StencilSupport stencilSupport;
727
reed73603f32016-09-20 08:42:38 -0700728 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700729
730 GrPathRenderer* pr = nullptr;
731 SkPath clipPath;
732 if (Element::kRect_Type == element->getType()) {
733 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
734 fillInverted = false;
735 } else {
736 element->asPath(&clipPath);
737 fillInverted = clipPath.isInverseFillType();
738 if (fillInverted) {
739 clipPath.toggleInverseFillType();
740 }
741
742 GrShape shape(clipPath, GrStyle::SimpleFill());
743 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Eric Karl5c779752017-05-08 12:02:07 -0700744 canDrawArgs.fCaps = context->caps();
Brian Salomon9a767722017-03-13 17:57:28 -0400745 canDrawArgs.fViewMatrix = &SkMatrix::I();
csmartdaltonbde96c62016-08-31 12:54:46 -0700746 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500747 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700748 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700749
750 GrDrawingManager* dm = context->contextPriv().drawingManager();
Brian Salomon82125e92016-12-10 09:35:48 -0500751 pr = dm->getPathRenderer(canDrawArgs, false, GrPathRendererChain::DrawType::kStencil,
csmartdaltonbde96c62016-08-31 12:54:46 -0700752 &stencilSupport);
753 if (!pr) {
754 return false;
755 }
756 }
757
758 bool canRenderDirectToStencil =
759 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
760 bool drawDirectToClip; // Given the renderer, the element,
761 // fill rule, and set operation should
762 // we render the element directly to
763 // stencil bit used for clipping.
764 GrUserStencilSettings const* const* stencilPasses =
765 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
766 &drawDirectToClip);
767
768 // draw the element to the client stencil bits if necessary
769 if (!drawDirectToClip) {
770 static constexpr GrUserStencilSettings kDrawToStencil(
771 GrUserStencilSettings::StaticInit<
772 0x0000,
773 GrUserStencilTest::kAlways,
774 0xffff,
775 GrUserStencilOp::kIncMaybeClamp,
776 GrUserStencilOp::kIncMaybeClamp,
777 0xffff>()
778 );
779 if (Element::kRect_Type == element->getType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400780 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
Brian Salomon9a767722017-03-13 17:57:28 -0400781 aaType, SkMatrix::I(), element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700782 } else {
783 if (!clipPath.isEmpty()) {
784 GrShape shape(clipPath, GrStyle::SimpleFill());
785 if (canRenderDirectToStencil) {
786 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500787 paint.setXPFactory(GrDisableColorXPFactory::Get());
csmartdaltonbde96c62016-08-31 12:54:46 -0700788
Robert Phillips256c37b2017-03-01 14:32:46 -0500789 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500790 std::move(paint),
791 &kDrawToStencil,
792 renderTargetContext,
793 &stencilClip.fixedClip(),
Brian Salomon9a767722017-03-13 17:57:28 -0400794 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500795 &shape,
796 aaType,
797 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700798 pr->drawPath(args);
799 } else {
800 GrPathRenderer::StencilPathArgs args;
Robert Phillips256c37b2017-03-01 14:32:46 -0500801 args.fContext = context;
Brian Osman11052242016-10-27 14:47:55 -0400802 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700803 args.fClip = &stencilClip.fixedClip();
Brian Salomon9a767722017-03-13 17:57:28 -0400804 args.fViewMatrix = &SkMatrix::I();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500805 args.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700806 args.fShape = &shape;
807 pr->stencilPath(args);
808 }
809 }
810 }
811 }
812
813 // now we modify the clip bit by rendering either the clip
814 // element directly or a bounding rect of the entire clip.
815 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
816 if (drawDirectToClip) {
817 if (Element::kRect_Type == element->getType()) {
Brian Salomon9a767722017-03-13 17:57:28 -0400818 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType,
819 SkMatrix::I(), element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700820 } else {
821 GrShape shape(clipPath, GrStyle::SimpleFill());
822 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500823 paint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips256c37b2017-03-01 14:32:46 -0500824 GrPathRenderer::DrawPathArgs args{context,
Brian Salomon82f44312017-01-11 13:42:54 -0500825 std::move(paint),
826 *pass,
827 renderTargetContext,
828 &stencilClip,
Brian Salomon9a767722017-03-13 17:57:28 -0400829 &SkMatrix::I(),
Brian Salomon82f44312017-01-11 13:42:54 -0500830 &shape,
831 aaType,
832 false};
csmartdaltonbde96c62016-08-31 12:54:46 -0700833 pr->drawPath(args);
834 }
835 } else {
836 // The view matrix is setup to do clip space -> stencil space translation, so
837 // draw rect in clip space.
Brian Salomon9a767722017-03-13 17:57:28 -0400838 renderTargetContext->priv().stencilRect(stencilClip, *pass, aaType, SkMatrix::I(),
Brian Osman693a5402016-10-27 15:13:22 -0400839 SkRect::Make(fIBounds));
csmartdaltonbde96c62016-08-31 12:54:46 -0700840 }
841 }
842 }
843 return true;
844}