blob: a82048b2f98d026ccd0363a12b862acdad96800d [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.
reed73603f32016-09-20 08:42:38 -070078 fElements.addToHead(stackBounds, SkCanvas::kReplace_Op, 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()) {
reed73603f32016-09-20 08:42:38 -0700150 case SkCanvas::kDifference_Op:
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;
reed73603f32016-09-20 08:42:38 -0700176 case SkCanvas::kIntersect_Op:
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;
reed73603f32016-09-20 08:42:38 -0700209 case SkCanvas::kUnion_Op:
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;
reed73603f32016-09-20 08:42:38 -0700232 case SkCanvas::kXOR_Op:
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;
reed73603f32016-09-20 08:42:38 -0700254 case SkCanvas::kReverseDifference_Op:
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
reed73603f32016-09-20 08:42:38 -0700279 case SkCanvas::kReplace_Op:
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) {
reed73603f32016-09-20 08:42:38 -0700329 SkASSERT(SkCanvas::kXOR_Op == element->getOp() ||
330 SkCanvas::kReverseDifference_Op == element->getOp());
331 fElements.addToHead(SkRect::Make(fIBounds), SkCanvas::kReverseDifference_Op, 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.
reed73603f32016-09-20 08:42:38 -0700340 bool isReplace = SkCanvas::kReplace_Op == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700341 if (newElement->isInverseFilled() &&
reed73603f32016-09-20 08:42:38 -0700342 (SkCanvas::kIntersect_Op == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700343 newElement->invertShapeFillType();
reed73603f32016-09-20 08:42:38 -0700344 newElement->setOp(SkCanvas::kDifference_Op);
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()) {
reed73603f32016-09-20 08:42:38 -0700363 case SkCanvas::kDifference_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700364 // subtracting from the empty set yields the empty set.
365 skippable = InitialTriState::kAllOut == initialTriState;
366 break;
reed73603f32016-09-20 08:42:38 -0700367 case SkCanvas::kIntersect_Op:
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;
reed73603f32016-09-20 08:42:38 -0700374 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700375 }
376 break;
reed73603f32016-09-20 08:42:38 -0700377 case SkCanvas::kUnion_Op:
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.
reed73603f32016-09-20 08:42:38 -0700383 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700384 }
385 break;
reed73603f32016-09-20 08:42:38 -0700386 case SkCanvas::kXOR_Op:
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.
reed73603f32016-09-20 08:42:38 -0700389 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700390 }
391 break;
reed73603f32016-09-20 08:42:38 -0700392 case SkCanvas::kReverseDifference_Op:
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 {
reed73603f32016-09-20 08:42:38 -0700405 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700406 }
407 }
408 break;
reed73603f32016-09-20 08:42:38 -0700409 case SkCanvas::kReplace_Op:
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
reed73603f32016-09-20 08:42:38 -0700434static bool element_is_pure_subtract(SkCanvas::ClipOp op) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700435 SkASSERT(op >= 0);
reed73603f32016-09-20 08:42:38 -0700436 return op <= SkCanvas::kIntersect_Op;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700437
reed73603f32016-09-20 08:42:38 -0700438 GR_STATIC_ASSERT(0 == SkCanvas::kDifference_Op);
439 GR_STATIC_ASSERT(1 == SkCanvas::kIntersect_Op);
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();
reed73603f32016-09-20 08:42:38 -0700449 if (SkCanvas::kDifference_Op != 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) {
532
533 // TODO: Draw rrects directly here.
534 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(),
541 element->isInverseFilled(),
542 element->isAA(),
543 viewMatrix,
544 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700545 break;
546 default: {
547 SkPath path;
548 element->asPath(&path);
549 if (path.isInverseFillType()) {
550 path.toggleInverseFillType();
551 }
552
Brian Osman693a5402016-10-27 15:13:22 -0400553 return rtc->priv().drawAndStencilPath(clip, ss,
554 (SkRegion::Op)element->getOp(),
555 element->isInverseFilled(),
556 element->isAA(), viewMatrix,
557 path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700558 break;
559 }
560 }
561
562 return false;
563}
564
Brian Osman11052242016-10-27 14:47:55 -0400565static void draw_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700566 const GrClip& clip, // TODO: can this just always be WideOpen?
567 const GrPaint &paint,
568 const SkMatrix& viewMatrix,
569 const SkClipStack::Element* element) {
570
571 // TODO: Draw rrects directly here.
572 switch (element->getType()) {
573 case Element::kEmpty_Type:
574 SkDEBUGFAIL("Should never get here with an empty element.");
575 break;
576 case Element::kRect_Type:
Brian Osman11052242016-10-27 14:47:55 -0400577 rtc->drawRect(clip, paint, viewMatrix, element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700578 break;
579 default: {
580 SkPath path;
581 element->asPath(&path);
582 if (path.isInverseFillType()) {
583 path.toggleInverseFillType();
584 }
585
Brian Osman11052242016-10-27 14:47:55 -0400586 rtc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700587 break;
588 }
589 }
590}
591
Brian Osman11052242016-10-27 14:47:55 -0400592bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700593 // The texture may be larger than necessary, this rect represents the part of the texture
594 // we populate with a rasterization of the clip.
595 GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height()));
596
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700597 if (!fWindowRects.empty()) {
598 clip.setWindowRectangles(fWindowRects, {fIBounds.left(), fIBounds.top()},
599 GrWindowRectsState::Mode::kExclusive);
600 }
601
csmartdaltonbde96c62016-08-31 12:54:46 -0700602 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
603 // clear the part that we care about.
604 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400605 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700606
607 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
608 SkMatrix translate;
609 translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top()));
610
611 // walk through each clip element and perform its set op
612 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
613 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700614 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700615 bool invert = element->isInverseFilled();
616 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
617 // draw directly into the result with the stencil set to make the pixels affected
618 // by the clip shape be non-zero.
619 static constexpr GrUserStencilSettings kStencilInElement(
620 GrUserStencilSettings::StaticInit<
621 0xffff,
622 GrUserStencilTest::kAlways,
623 0xffff,
624 GrUserStencilOp::kReplace,
625 GrUserStencilOp::kReplace,
626 0xffff>()
627 );
Brian Osman11052242016-10-27 14:47:55 -0400628 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700629 return false;
630 }
631
632 // Draw to the exterior pixels (those with a zero stencil value).
633 static constexpr GrUserStencilSettings kDrawOutsideElement(
634 GrUserStencilSettings::StaticInit<
635 0x0000,
636 GrUserStencilTest::kEqual,
637 0xffff,
638 GrUserStencilOp::kZero,
639 GrUserStencilOp::kZero,
640 0xffff>()
641 );
Brian Osman693a5402016-10-27 15:13:22 -0400642 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, false,
643 translate, SkRect::Make(fIBounds))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700644 return false;
645 }
646 } else {
647 // all the remaining ops can just be directly draw into the accumulation buffer
648 GrPaint paint;
649 paint.setAntiAlias(element->isAA());
650 paint.setCoverageSetOpXPFactory(op, false);
651
Brian Osman11052242016-10-27 14:47:55 -0400652 draw_element(rtc, clip, paint, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700653 }
654 }
655
656 return true;
657}
658
659////////////////////////////////////////////////////////////////////////////////
660// Create a 1-bit clip mask in the stencil buffer.
661
662class StencilClip final : public GrClip {
663public:
664 StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {}
665 const GrFixedClip& fixedClip() const { return fFixedClip; }
666
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700667 void setWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin,
668 GrWindowRectsState::Mode mode) {
669 fFixedClip.setWindowRectangles(windows, origin, mode);
670 }
671
csmartdaltonbde96c62016-08-31 12:54:46 -0700672private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700673 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700674 return false;
675 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700676 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
677 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700678 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700679 bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700680 return false;
681 }
Brian Osman11052242016-10-27 14:47:55 -0400682 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700683 bool hasUserStencilSettings, GrAppliedClip* out) const override {
Brian Osman11052242016-10-27 14:47:55 -0400684 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700685 return false;
686 }
687 out->addStencilClip();
688 return true;
689 }
690
691 GrFixedClip fFixedClip;
692
693 typedef GrClip INHERITED;
694};
695
696bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400697 GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700698 const SkIPoint& clipOrigin) const {
699 // We set the current clip to the bounds so that our recursive draws are scissored to them.
700 StencilClip stencilClip(fIBounds.makeOffset(-clipOrigin.x(), -clipOrigin.y()));
701
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700702 if (!fWindowRects.empty()) {
703 stencilClip.setWindowRectangles(fWindowRects, clipOrigin,
704 GrWindowRectsState::Mode::kExclusive);
705 }
706
csmartdaltonbde96c62016-08-31 12:54:46 -0700707 bool initialState = InitialState::kAllIn == this->initialState();
Brian Osman693a5402016-10-27 15:13:22 -0400708 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700709
710 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
711 SkMatrix viewMatrix;
712 viewMatrix.setTranslate(SkIntToScalar(-clipOrigin.x()), SkIntToScalar(-clipOrigin.y()));
713
714 // walk through each clip element and perform its set op
715 // with the existing clip.
716 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
717 const Element* element = iter.get();
Brian Osman11052242016-10-27 14:47:55 -0400718 bool useHWAA = element->isAA() && renderTargetContext->isStencilBufferMultisampled();
csmartdaltonbde96c62016-08-31 12:54:46 -0700719
720 bool fillInverted = false;
721
722 // This will be used to determine whether the clip shape can be rendered into the
723 // stencil with arbitrary stencil settings.
724 GrPathRenderer::StencilSupport stencilSupport;
725
reed73603f32016-09-20 08:42:38 -0700726 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700727
728 GrPathRenderer* pr = nullptr;
729 SkPath clipPath;
730 if (Element::kRect_Type == element->getType()) {
731 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
732 fillInverted = false;
733 } else {
734 element->asPath(&clipPath);
735 fillInverted = clipPath.isInverseFillType();
736 if (fillInverted) {
737 clipPath.toggleInverseFillType();
738 }
739
740 GrShape shape(clipPath, GrStyle::SimpleFill());
741 GrPathRenderer::CanDrawPathArgs canDrawArgs;
742 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
743 canDrawArgs.fViewMatrix = &viewMatrix;
744 canDrawArgs.fShape = &shape;
745 canDrawArgs.fAntiAlias = false;
746 canDrawArgs.fHasUserStencilSettings = false;
Brian Osman11052242016-10-27 14:47:55 -0400747 canDrawArgs.fIsStencilBufferMSAA = renderTargetContext->isStencilBufferMultisampled();
csmartdaltonbde96c62016-08-31 12:54:46 -0700748
749 GrDrawingManager* dm = context->contextPriv().drawingManager();
750 pr = dm->getPathRenderer(canDrawArgs, false,
751 GrPathRendererChain::kStencilOnly_DrawType,
752 &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,
781 useHWAA, viewMatrix, 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;
787 paint.setXPFactory(GrDisableColorXPFactory::Make());
788 paint.setAntiAlias(element->isAA());
789
790 GrPathRenderer::DrawPathArgs args;
791 args.fResourceProvider = context->resourceProvider();
792 args.fPaint = &paint;
793 args.fUserStencilSettings = &kDrawToStencil;
Brian Osman11052242016-10-27 14:47:55 -0400794 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700795 args.fClip = &stencilClip.fixedClip();
796 args.fViewMatrix = &viewMatrix;
797 args.fShape = &shape;
798 args.fAntiAlias = false;
799 args.fGammaCorrect = false;
800 pr->drawPath(args);
801 } else {
802 GrPathRenderer::StencilPathArgs args;
803 args.fResourceProvider = context->resourceProvider();
Brian Osman11052242016-10-27 14:47:55 -0400804 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700805 args.fClip = &stencilClip.fixedClip();
806 args.fViewMatrix = &viewMatrix;
807 args.fIsAA = element->isAA();
808 args.fShape = &shape;
809 pr->stencilPath(args);
810 }
811 }
812 }
813 }
814
815 // now we modify the clip bit by rendering either the clip
816 // element directly or a bounding rect of the entire clip.
817 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
818 if (drawDirectToClip) {
819 if (Element::kRect_Type == element->getType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400820 renderTargetContext->priv().stencilRect(stencilClip, *pass, useHWAA, viewMatrix,
821 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700822 } else {
823 GrShape shape(clipPath, GrStyle::SimpleFill());
824 GrPaint paint;
825 paint.setXPFactory(GrDisableColorXPFactory::Make());
826 paint.setAntiAlias(element->isAA());
827 GrPathRenderer::DrawPathArgs args;
828 args.fResourceProvider = context->resourceProvider();
829 args.fPaint = &paint;
830 args.fUserStencilSettings = *pass;
Brian Osman11052242016-10-27 14:47:55 -0400831 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700832 args.fClip = &stencilClip;
833 args.fViewMatrix = &viewMatrix;
834 args.fShape = &shape;
835 args.fAntiAlias = false;
836 args.fGammaCorrect = false;
837 pr->drawPath(args);
838 }
839 } else {
840 // The view matrix is setup to do clip space -> stencil space translation, so
841 // draw rect in clip space.
Brian Osman693a5402016-10-27 15:13:22 -0400842 renderTargetContext->priv().stencilRect(stencilClip, *pass, false, viewMatrix,
843 SkRect::Make(fIBounds));
csmartdaltonbde96c62016-08-31 12:54:46 -0700844 }
845 }
846 }
847 return true;
848}