blob: 8b9090f4279b191ff16e5501e1ec265c588cc814 [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"
csmartdaltoncbecb082016-07-22 08:59:08 -070022
bsalomon@google.com170bd792012-12-05 22:26:11 +000023typedef SkClipStack::Element Element;
bsalomon@google.com170bd792012-12-05 22:26:11 +000024
csmartdalton5ecbbbe2016-08-23 13:26:40 -070025/**
26 * There are plenty of optimizations that could be added here. Maybe flips could be folded into
27 * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
28 * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
29 * based on later intersect operations, and perhaps remove intersect-rects. We could optionally
30 * take a rect in case the caller knows a bound on what is to be drawn through this clip.
31 */
csmartdaltonbf4a8f92016-09-06 10:01:06 -070032GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
33 int maxWindowRectangles) {
csmartdaltoncbecb082016-07-22 08:59:08 -070034 SkASSERT(!queryBounds.isEmpty());
csmartdaltond211e782016-08-15 11:17:19 -070035 fHasIBounds = false;
csmartdaltoncbecb082016-07-22 08:59:08 -070036
bsalomon@google.com170bd792012-12-05 22:26:11 +000037 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070038 fInitialState = InitialState::kAllIn;
39 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000040 }
41
42 SkClipStack::BoundsType stackBoundsType;
43 SkRect stackBounds;
44 bool iior;
45 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
46
csmartdaltoncbecb082016-07-22 08:59:08 -070047 if (stackBounds.isEmpty() || GrClip::IsOutsideClip(stackBounds, queryBounds)) {
48 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070049 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
50 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000051 }
52
csmartdaltoncbecb082016-07-22 08:59:08 -070053 if (iior) {
54 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
55 // This should only be true if aa/non-aa status matches among all elements.
56 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
57 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
58 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
59 // The clip is a non-aa rect. This is the one spot where we can actually implement the
csmartdalton77f2fae2016-08-08 09:55:06 -070060 // clip (using fIBounds) rather than just telling the caller what it should be.
61 stackBounds.round(&fIBounds);
csmartdaltond211e782016-08-15 11:17:19 -070062 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070063 fInitialState = fIBounds.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
64 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070065 }
66 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070067 fInitialState = InitialState::kAllIn;
68 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070069 }
70
csmartdaltond211e782016-08-15 11:17:19 -070071 SkRect tightBounds;
72 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
73 fIBounds = GrClip::GetPixelIBounds(tightBounds);
csmartdalton77f2fae2016-08-08 09:55:06 -070074 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070075 fHasIBounds = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070076
csmartdalton77f2fae2016-08-08 09:55:06 -070077 // Implement the clip with an AA rect element.
Mike Reedc1f77742016-12-09 09:00:50 -050078 fElements.addToHead(stackBounds, kReplace_SkClipOp, true/*doAA*/);
csmartdalton8d3f92a2016-08-17 09:39:38 -070079 fElementsGenID = stack.getTopmostGenID();
csmartdalton77f2fae2016-08-08 09:55:06 -070080 fRequiresAA = true;
81
82 fInitialState = InitialState::kAllOut;
83 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070084 }
85
86 SkRect tighterQuery = queryBounds;
87 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
88 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This new
csmartdalton77f2fae2016-08-08 09:55:06 -070089 // clip will be enforced by the scissor through fIBounds.)
csmartdaltoncbecb082016-07-22 08:59:08 -070090 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
csmartdaltoncbecb082016-07-22 08:59:08 -070091 }
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000092
csmartdaltond211e782016-08-15 11:17:19 -070093 fIBounds = GrClip::GetPixelIBounds(tighterQuery);
csmartdalton77f2fae2016-08-08 09:55:06 -070094 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070095 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070096
bsalomon@google.com34cd70a2012-12-06 14:23:20 +000097 // Now that we have determined the bounds to use and filtered out the trivial cases, call the
98 // helper that actually walks the stack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -070099 this->walkStack(stack, tighterQuery, maxWindowRectangles);
100
101 if (fWindowRects.count() < maxWindowRectangles) {
102 this->addInteriorWindowRectangles(maxWindowRectangles);
103 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700104}
105
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700106void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
107 int maxWindowRectangles) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700108 // walk backwards until we get to:
109 // a) the beginning
110 // b) an operation that is known to make the bounds all inside/outside
111 // c) a replace operation
112
113 enum class InitialTriState {
114 kUnknown = -1,
115 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
116 kAllOut = (int)GrReducedClip::InitialState::kAllOut
117 } initialTriState = InitialTriState::kUnknown;
118
119 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
120 // TODO: track these per saved clip so that we can consider them on the forward pass.
121 bool embiggens = false;
122 bool emsmallens = false;
123
124 // We use a slightly relaxed set of query bounds for element containment tests. This is to
125 // account for floating point rounding error that may have occurred during coord transforms.
126 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
127 GrClip::kBoundsTolerance);
128
129 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
130 int numAAElements = 0;
131 while (InitialTriState::kUnknown == initialTriState) {
132 const Element* element = iter.prev();
133 if (nullptr == element) {
134 initialTriState = InitialTriState::kAllIn;
135 break;
136 }
137 if (SkClipStack::kEmptyGenID == element->getGenID()) {
138 initialTriState = InitialTriState::kAllOut;
139 break;
140 }
141 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
142 initialTriState = InitialTriState::kAllIn;
143 break;
144 }
145
146 bool skippable = false;
147 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
148
149 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500150 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700151 // check if the shape subtracted either contains the entire bounds (and makes
152 // the clip empty) or is outside the bounds and therefore can be skipped.
153 if (element->isInverseFilled()) {
154 if (element->contains(relaxedQueryBounds)) {
155 skippable = true;
156 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
157 initialTriState = InitialTriState::kAllOut;
158 skippable = true;
159 }
160 } else {
161 if (element->contains(relaxedQueryBounds)) {
162 initialTriState = InitialTriState::kAllOut;
163 skippable = true;
164 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
165 skippable = true;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700166 } else if (fWindowRects.count() < maxWindowRectangles && !embiggens &&
167 !element->isAA() && Element::kRect_Type == element->getType()) {
168 this->addWindowRectangle(element->getRect(), false);
169 skippable = true;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700170 }
171 }
172 if (!skippable) {
173 emsmallens = true;
174 }
175 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500176 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700177 // check if the shape intersected contains the entire bounds and therefore can
178 // be skipped or it is outside the entire bounds and therefore makes the clip
179 // empty.
180 if (element->isInverseFilled()) {
181 if (element->contains(relaxedQueryBounds)) {
182 initialTriState = InitialTriState::kAllOut;
183 skippable = true;
184 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
185 skippable = true;
186 }
187 } else {
188 if (element->contains(relaxedQueryBounds)) {
189 skippable = true;
190 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
191 initialTriState = InitialTriState::kAllOut;
192 skippable = true;
193 } else if (!embiggens && !element->isAA() &&
194 Element::kRect_Type == element->getType()) {
195 // fIBounds and queryBounds have already acccounted for this element via
196 // clip stack bounds; here we just apply the non-aa rounding effect.
197 SkIRect nonaaRect;
198 element->getRect().round(&nonaaRect);
199 if (!this->intersectIBounds(nonaaRect)) {
200 return;
201 }
202 skippable = true;
203 }
204 }
205 if (!skippable) {
206 emsmallens = true;
207 }
208 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500209 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700210 // If the union-ed shape contains the entire bounds then after this element
211 // the bounds is entirely inside the clip. If the union-ed shape is outside the
212 // bounds then this op can be skipped.
213 if (element->isInverseFilled()) {
214 if (element->contains(relaxedQueryBounds)) {
215 skippable = true;
216 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
217 initialTriState = InitialTriState::kAllIn;
218 skippable = true;
219 }
220 } else {
221 if (element->contains(relaxedQueryBounds)) {
222 initialTriState = InitialTriState::kAllIn;
223 skippable = true;
224 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
225 skippable = true;
226 }
227 }
228 if (!skippable) {
229 embiggens = true;
230 }
231 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500232 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700233 // If the bounds is entirely inside the shape being xor-ed then the effect is
234 // to flip the inside/outside state of every point in the bounds. We may be
235 // able to take advantage of this in the forward pass. If the xor-ed shape
236 // doesn't intersect the bounds then it can be skipped.
237 if (element->isInverseFilled()) {
238 if (element->contains(relaxedQueryBounds)) {
239 skippable = true;
240 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
241 isFlip = true;
242 }
243 } else {
244 if (element->contains(relaxedQueryBounds)) {
245 isFlip = true;
246 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
247 skippable = true;
248 }
249 }
250 if (!skippable) {
251 emsmallens = embiggens = true;
252 }
253 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500254 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700255 // When the bounds is entirely within the rev-diff shape then this behaves like xor
256 // and reverses every point inside the bounds. If the shape is completely outside
257 // the bounds then we know after this element is applied that the bounds will be
258 // all outside the current clip.B
259 if (element->isInverseFilled()) {
260 if (element->contains(relaxedQueryBounds)) {
261 initialTriState = InitialTriState::kAllOut;
262 skippable = true;
263 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
264 isFlip = true;
265 }
266 } else {
267 if (element->contains(relaxedQueryBounds)) {
268 isFlip = true;
269 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
270 initialTriState = InitialTriState::kAllOut;
271 skippable = true;
272 }
273 }
274 if (!skippable) {
275 emsmallens = embiggens = true;
276 }
277 break;
278
Mike Reedc1f77742016-12-09 09:00:50 -0500279 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700280 // Replace will always terminate our walk. We will either begin the forward walk
281 // at the replace op or detect here than the shape is either completely inside
282 // or completely outside the bounds. In this latter case it can be skipped by
283 // setting the correct value for initialTriState.
284 if (element->isInverseFilled()) {
285 if (element->contains(relaxedQueryBounds)) {
286 initialTriState = InitialTriState::kAllOut;
287 skippable = true;
288 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
289 initialTriState = InitialTriState::kAllIn;
290 skippable = true;
291 }
292 } else {
293 if (element->contains(relaxedQueryBounds)) {
294 initialTriState = InitialTriState::kAllIn;
295 skippable = true;
296 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
297 initialTriState = InitialTriState::kAllOut;
298 skippable = true;
299 } else if (!embiggens && !element->isAA() &&
300 Element::kRect_Type == element->getType()) {
301 // fIBounds and queryBounds have already acccounted for this element via
302 // clip stack bounds; here we just apply the non-aa rounding effect.
303 SkIRect nonaaRect;
304 element->getRect().round(&nonaaRect);
305 if (!this->intersectIBounds(nonaaRect)) {
306 return;
307 }
308 initialTriState = InitialTriState::kAllIn;
309 skippable = true;
310 }
311 }
312 if (!skippable) {
313 initialTriState = InitialTriState::kAllOut;
314 embiggens = emsmallens = true;
315 }
316 break;
317 default:
318 SkDEBUGFAIL("Unexpected op.");
319 break;
320 }
321 if (!skippable) {
322 if (0 == fElements.count()) {
323 // This will be the last element. Record the stricter genID.
324 fElementsGenID = element->getGenID();
325 }
326
327 // if it is a flip, change it to a bounds-filling rect
328 if (isFlip) {
Mike Reedc1f77742016-12-09 09:00:50 -0500329 SkASSERT(kXOR_SkClipOp == element->getOp() ||
330 kReverseDifference_SkClipOp == element->getOp());
331 fElements.addToHead(SkRect::Make(fIBounds), kReverseDifference_SkClipOp, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700332 } else {
333 Element* newElement = fElements.addToHead(*element);
334 if (newElement->isAA()) {
335 ++numAAElements;
336 }
337 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
338 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
339 // differencing the non-inverse shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500340 bool isReplace = kReplace_SkClipOp == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700341 if (newElement->isInverseFilled() &&
Mike Reedc1f77742016-12-09 09:00:50 -0500342 (kIntersect_SkClipOp == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700343 newElement->invertShapeFillType();
Mike Reedc1f77742016-12-09 09:00:50 -0500344 newElement->setOp(kDifference_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700345 if (isReplace) {
346 SkASSERT(InitialTriState::kAllOut == initialTriState);
347 initialTriState = InitialTriState::kAllIn;
348 }
349 }
350 }
351 }
352 }
353
354 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
355 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
356 fElements.reset();
357 numAAElements = 0;
358 } else {
359 Element* element = fElements.headIter().get();
360 while (element) {
361 bool skippable = false;
362 switch (element->getOp()) {
Mike Reedc1f77742016-12-09 09:00:50 -0500363 case kDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700364 // subtracting from the empty set yields the empty set.
365 skippable = InitialTriState::kAllOut == initialTriState;
366 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500367 case kIntersect_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700368 // intersecting with the empty set yields the empty set
369 if (InitialTriState::kAllOut == initialTriState) {
370 skippable = true;
371 } else {
372 // We can clear to zero and then simply draw the clip element.
373 initialTriState = InitialTriState::kAllOut;
Mike Reedc1f77742016-12-09 09:00:50 -0500374 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700375 }
376 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500377 case kUnion_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700378 if (InitialTriState::kAllIn == initialTriState) {
379 // unioning the infinite plane with anything is a no-op.
380 skippable = true;
381 } else {
382 // unioning the empty set with a shape is the shape.
Mike Reedc1f77742016-12-09 09:00:50 -0500383 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700384 }
385 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500386 case kXOR_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700387 if (InitialTriState::kAllOut == initialTriState) {
388 // xor could be changed to diff in the kAllIn case, not sure it's a win.
Mike Reedc1f77742016-12-09 09:00:50 -0500389 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700390 }
391 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500392 case kReverseDifference_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700393 if (InitialTriState::kAllIn == initialTriState) {
394 // subtracting the whole plane will yield the empty set.
395 skippable = true;
396 initialTriState = InitialTriState::kAllOut;
397 } else {
398 // this picks up flips inserted in the backwards pass.
399 skippable = element->isInverseFilled() ?
400 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
401 element->contains(relaxedQueryBounds);
402 if (skippable) {
403 initialTriState = InitialTriState::kAllIn;
404 } else {
Mike Reedc1f77742016-12-09 09:00:50 -0500405 element->setOp(kReplace_SkClipOp);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700406 }
407 }
408 break;
Mike Reedc1f77742016-12-09 09:00:50 -0500409 case kReplace_SkClipOp:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700410 skippable = false; // we would have skipped it in the backwards walk if we
411 // could've.
412 break;
413 default:
414 SkDEBUGFAIL("Unexpected op.");
415 break;
416 }
417 if (!skippable) {
418 break;
419 } else {
420 if (element->isAA()) {
421 --numAAElements;
422 }
423 fElements.popHead();
424 element = fElements.headIter().get();
425 }
426 }
427 }
428 fRequiresAA = numAAElements > 0;
429
430 SkASSERT(InitialTriState::kUnknown != initialTriState);
431 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
432}
433
Mike Reedc1f77742016-12-09 09:00:50 -0500434static bool element_is_pure_subtract(SkClipOp op) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700435 SkASSERT(op >= 0);
Mike Reedc1f77742016-12-09 09:00:50 -0500436 return op <= kIntersect_SkClipOp;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700437
Mike Reedc1f77742016-12-09 09:00:50 -0500438 GR_STATIC_ASSERT(0 == kDifference_SkClipOp);
439 GR_STATIC_ASSERT(1 == kIntersect_SkClipOp);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700440}
441
442void GrReducedClip::addInteriorWindowRectangles(int maxWindowRectangles) {
443 SkASSERT(fWindowRects.count() < maxWindowRectangles);
444 // Walk backwards through the element list and add window rectangles to the interiors of
445 // "difference" elements. Quit if we encounter an element that may grow the clip.
446 ElementList::Iter iter(fElements, ElementList::Iter::kTail_IterStart);
447 for (; iter.get() && element_is_pure_subtract(iter.get()->getOp()); iter.prev()) {
448 const Element* element = iter.get();
Mike Reedc1f77742016-12-09 09:00:50 -0500449 if (kDifference_SkClipOp != element->getOp()) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700450 continue;
451 }
452
453 if (Element::kRect_Type == element->getType()) {
454 SkASSERT(element->isAA());
455 this->addWindowRectangle(element->getRect(), true);
456 if (fWindowRects.count() >= maxWindowRectangles) {
457 return;
458 }
459 continue;
460 }
461
462 if (Element::kRRect_Type == element->getType()) {
463 // For round rects we add two overlapping windows in the shape of a plus.
464 const SkRRect& clipRRect = element->getRRect();
465 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
466 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
467 if (SkRRect::kComplex_Type == clipRRect.getType()) {
468 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
469 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
470 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
471 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
472 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
473 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
474 }
475 const SkRect& bounds = clipRRect.getBounds();
476 if (insetTL.x() + insetBR.x() >= bounds.width() ||
477 insetTL.y() + insetBR.y() >= bounds.height()) {
478 continue; // The interior "plus" is empty.
479 }
480
481 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
482 bounds.right(), bounds.bottom() - insetBR.y());
483 this->addWindowRectangle(horzRect, element->isAA());
484 if (fWindowRects.count() >= maxWindowRectangles) {
485 return;
486 }
487
488 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
489 bounds.right() - insetBR.x(), bounds.bottom());
490 this->addWindowRectangle(vertRect, element->isAA());
491 if (fWindowRects.count() >= maxWindowRectangles) {
492 return;
493 }
494 continue;
495 }
496 }
497}
498
499inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
500 SkIRect window;
501 if (!elementIsAA) {
502 elementInteriorRect.round(&window);
503 } else {
504 elementInteriorRect.roundIn(&window);
505 }
506 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
507 fWindowRects.addWindow(window);
508 }
509}
510
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700511inline bool GrReducedClip::intersectIBounds(const SkIRect& irect) {
512 SkASSERT(fHasIBounds);
513 if (!fIBounds.intersect(irect)) {
514 fHasIBounds = false;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700515 fWindowRects.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700516 fElements.reset();
517 fRequiresAA = false;
518 fInitialState = InitialState::kAllOut;
519 return false;
520 }
521 return true;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000522}
csmartdaltonbde96c62016-08-31 12:54:46 -0700523
524////////////////////////////////////////////////////////////////////////////////
525// Create a 8-bit clip mask in alpha
526
Brian Osman11052242016-10-27 14:47:55 -0400527static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700528 const GrFixedClip& clip,
529 const GrUserStencilSettings* ss,
530 const SkMatrix& viewMatrix,
531 const SkClipStack::Element* element) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500532 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700533 switch (element->getType()) {
534 case Element::kEmpty_Type:
535 SkDEBUGFAIL("Should never get here with an empty element.");
536 break;
537 case Element::kRect_Type:
Brian Osman693a5402016-10-27 15:13:22 -0400538 return rtc->priv().drawAndStencilRect(clip, ss,
539 (SkRegion::Op)element->getOp(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500540 element->isInverseFilled(), aa, viewMatrix,
Brian Osman693a5402016-10-27 15:13:22 -0400541 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700542 break;
543 default: {
544 SkPath path;
545 element->asPath(&path);
546 if (path.isInverseFillType()) {
547 path.toggleInverseFillType();
548 }
549
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500550 return rtc->priv().drawAndStencilPath(clip, ss, (SkRegion::Op)element->getOp(),
551 element->isInverseFilled(), aa, viewMatrix, path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700552 break;
553 }
554 }
555
556 return false;
557}
558
Brian Osman11052242016-10-27 14:47:55 -0400559static void draw_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700560 const GrClip& clip, // TODO: can this just always be WideOpen?
561 const GrPaint &paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500562 GrAA aa,
csmartdaltonbde96c62016-08-31 12:54:46 -0700563 const SkMatrix& viewMatrix,
564 const SkClipStack::Element* element) {
565
566 // TODO: Draw rrects directly here.
567 switch (element->getType()) {
568 case Element::kEmpty_Type:
569 SkDEBUGFAIL("Should never get here with an empty element.");
570 break;
571 case Element::kRect_Type:
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500572 rtc->drawRect(clip, paint, aa, viewMatrix, element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700573 break;
574 default: {
575 SkPath path;
576 element->asPath(&path);
577 if (path.isInverseFillType()) {
578 path.toggleInverseFillType();
579 }
580
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500581 rtc->drawPath(clip, paint, aa, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700582 break;
583 }
584 }
585}
586
Brian Osman11052242016-10-27 14:47:55 -0400587bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700588 // The texture may be larger than necessary, this rect represents the part of the texture
589 // we populate with a rasterization of the clip.
590 GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height()));
591
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700592 if (!fWindowRects.empty()) {
593 clip.setWindowRectangles(fWindowRects, {fIBounds.left(), fIBounds.top()},
594 GrWindowRectsState::Mode::kExclusive);
595 }
596
csmartdaltonbde96c62016-08-31 12:54:46 -0700597 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
598 // clear the part that we care about.
599 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400600 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700601
602 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
603 SkMatrix translate;
604 translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top()));
605
606 // walk through each clip element and perform its set op
607 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
608 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700609 SkRegion::Op op = (SkRegion::Op)element->getOp();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500610 GrAA aa = GrBoolToAA(element->isAA());
csmartdaltonbde96c62016-08-31 12:54:46 -0700611 bool invert = element->isInverseFilled();
612 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
613 // draw directly into the result with the stencil set to make the pixels affected
614 // by the clip shape be non-zero.
615 static constexpr GrUserStencilSettings kStencilInElement(
616 GrUserStencilSettings::StaticInit<
617 0xffff,
618 GrUserStencilTest::kAlways,
619 0xffff,
620 GrUserStencilOp::kReplace,
621 GrUserStencilOp::kReplace,
622 0xffff>()
623 );
Brian Osman11052242016-10-27 14:47:55 -0400624 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700625 return false;
626 }
627
628 // Draw to the exterior pixels (those with a zero stencil value).
629 static constexpr GrUserStencilSettings kDrawOutsideElement(
630 GrUserStencilSettings::StaticInit<
631 0x0000,
632 GrUserStencilTest::kEqual,
633 0xffff,
634 GrUserStencilOp::kZero,
635 GrUserStencilOp::kZero,
636 0xffff>()
637 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500638 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, GrAA::kNo,
Brian Osman693a5402016-10-27 15:13:22 -0400639 translate, SkRect::Make(fIBounds))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700640 return false;
641 }
642 } else {
643 // all the remaining ops can just be directly draw into the accumulation buffer
644 GrPaint paint;
csmartdaltonbde96c62016-08-31 12:54:46 -0700645 paint.setCoverageSetOpXPFactory(op, false);
646
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500647 draw_element(rtc, clip, paint, aa, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700648 }
649 }
650
651 return true;
652}
653
654////////////////////////////////////////////////////////////////////////////////
655// Create a 1-bit clip mask in the stencil buffer.
656
657class StencilClip final : public GrClip {
658public:
659 StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {}
660 const GrFixedClip& fixedClip() const { return fFixedClip; }
661
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700662 void setWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin,
663 GrWindowRectsState::Mode mode) {
664 fFixedClip.setWindowRectangles(windows, origin, mode);
665 }
666
csmartdaltonbde96c62016-08-31 12:54:46 -0700667private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700668 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700669 return false;
670 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700671 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
672 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700673 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500674 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700675 return false;
676 }
Brian Osman11052242016-10-27 14:47:55 -0400677 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700678 bool hasUserStencilSettings, GrAppliedClip* out) const override {
Brian Osman11052242016-10-27 14:47:55 -0400679 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700680 return false;
681 }
682 out->addStencilClip();
683 return true;
684 }
685
686 GrFixedClip fFixedClip;
687
688 typedef GrClip INHERITED;
689};
690
691bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400692 GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700693 const SkIPoint& clipOrigin) const {
694 // We set the current clip to the bounds so that our recursive draws are scissored to them.
695 StencilClip stencilClip(fIBounds.makeOffset(-clipOrigin.x(), -clipOrigin.y()));
696
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700697 if (!fWindowRects.empty()) {
698 stencilClip.setWindowRectangles(fWindowRects, clipOrigin,
699 GrWindowRectsState::Mode::kExclusive);
700 }
701
csmartdaltonbde96c62016-08-31 12:54:46 -0700702 bool initialState = InitialState::kAllIn == this->initialState();
Brian Osman693a5402016-10-27 15:13:22 -0400703 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700704
705 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
706 SkMatrix viewMatrix;
707 viewMatrix.setTranslate(SkIntToScalar(-clipOrigin.x()), SkIntToScalar(-clipOrigin.y()));
708
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500709 // walk through each clip element and perform its set op with the existing clip.
csmartdaltonbde96c62016-08-31 12:54:46 -0700710 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
711 const Element* element = iter.get();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500712 GrAAType aaType = GrAAType::kNone;
713 if (element->isAA() && renderTargetContext->isStencilBufferMultisampled()) {
714 aaType = GrAAType::kMSAA;
715 }
csmartdaltonbde96c62016-08-31 12:54:46 -0700716
717 bool fillInverted = false;
718
719 // This will be used to determine whether the clip shape can be rendered into the
720 // stencil with arbitrary stencil settings.
721 GrPathRenderer::StencilSupport stencilSupport;
722
reed73603f32016-09-20 08:42:38 -0700723 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700724
725 GrPathRenderer* pr = nullptr;
726 SkPath clipPath;
727 if (Element::kRect_Type == element->getType()) {
728 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
729 fillInverted = false;
730 } else {
731 element->asPath(&clipPath);
732 fillInverted = clipPath.isInverseFillType();
733 if (fillInverted) {
734 clipPath.toggleInverseFillType();
735 }
736
737 GrShape shape(clipPath, GrStyle::SimpleFill());
738 GrPathRenderer::CanDrawPathArgs canDrawArgs;
739 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
740 canDrawArgs.fViewMatrix = &viewMatrix;
741 canDrawArgs.fShape = &shape;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500742 canDrawArgs.fAAType = aaType;
csmartdaltonbde96c62016-08-31 12:54:46 -0700743 canDrawArgs.fHasUserStencilSettings = false;
csmartdaltonbde96c62016-08-31 12:54:46 -0700744
745 GrDrawingManager* dm = context->contextPriv().drawingManager();
746 pr = dm->getPathRenderer(canDrawArgs, false,
747 GrPathRendererChain::kStencilOnly_DrawType,
748 &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;
783 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;
820 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}