blob: 7b526ee04596e7211f9f1350039c2fce3d0487c8 [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"
19#include "GrStyle.h"
20#include "GrUserStencilSettings.h"
csmartdaltoncbecb082016-07-22 08:59:08 -070021
bsalomon@google.com170bd792012-12-05 22:26:11 +000022typedef SkClipStack::Element Element;
bsalomon@google.com170bd792012-12-05 22:26:11 +000023
csmartdalton5ecbbbe2016-08-23 13:26:40 -070024/**
25 * There are plenty of optimizations that could be added here. Maybe flips could be folded into
26 * earlier operations. Or would inserting flips and reversing earlier ops ever be a win? Perhaps
27 * for the case where the bounds are kInsideOut_BoundsType. We could restrict earlier operations
28 * based on later intersect operations, and perhaps remove intersect-rects. We could optionally
29 * take a rect in case the caller knows a bound on what is to be drawn through this clip.
30 */
csmartdaltonbf4a8f92016-09-06 10:01:06 -070031GrReducedClip::GrReducedClip(const SkClipStack& stack, const SkRect& queryBounds,
32 int maxWindowRectangles) {
csmartdaltoncbecb082016-07-22 08:59:08 -070033 SkASSERT(!queryBounds.isEmpty());
csmartdaltond211e782016-08-15 11:17:19 -070034 fHasIBounds = false;
csmartdaltoncbecb082016-07-22 08:59:08 -070035
bsalomon@google.com170bd792012-12-05 22:26:11 +000036 if (stack.isWideOpen()) {
csmartdalton77f2fae2016-08-08 09:55:06 -070037 fInitialState = InitialState::kAllIn;
38 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000039 }
40
41 SkClipStack::BoundsType stackBoundsType;
42 SkRect stackBounds;
43 bool iior;
44 stack.getBounds(&stackBounds, &stackBoundsType, &iior);
45
csmartdaltoncbecb082016-07-22 08:59:08 -070046 if (stackBounds.isEmpty() || GrClip::IsOutsideClip(stackBounds, queryBounds)) {
47 bool insideOut = SkClipStack::kInsideOut_BoundsType == stackBoundsType;
csmartdalton77f2fae2016-08-08 09:55:06 -070048 fInitialState = insideOut ? InitialState::kAllIn : InitialState::kAllOut;
49 return;
bsalomon@google.com170bd792012-12-05 22:26:11 +000050 }
51
csmartdaltoncbecb082016-07-22 08:59:08 -070052 if (iior) {
53 // "Is intersection of rects" means the clip is a single rect indicated by the stack bounds.
54 // This should only be true if aa/non-aa status matches among all elements.
55 SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
56 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
57 if (!iter.prev()->isAA() || GrClip::IsPixelAligned(stackBounds)) {
58 // The clip is a non-aa rect. This is the one spot where we can actually implement the
csmartdalton77f2fae2016-08-08 09:55:06 -070059 // clip (using fIBounds) rather than just telling the caller what it should be.
60 stackBounds.round(&fIBounds);
csmartdaltond211e782016-08-15 11:17:19 -070061 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070062 fInitialState = fIBounds.isEmpty() ? InitialState::kAllOut : InitialState::kAllIn;
63 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070064 }
65 if (GrClip::IsInsideClip(stackBounds, queryBounds)) {
csmartdalton77f2fae2016-08-08 09:55:06 -070066 fInitialState = InitialState::kAllIn;
67 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070068 }
69
csmartdaltond211e782016-08-15 11:17:19 -070070 SkRect tightBounds;
71 SkAssertResult(tightBounds.intersect(stackBounds, queryBounds));
72 fIBounds = GrClip::GetPixelIBounds(tightBounds);
csmartdalton77f2fae2016-08-08 09:55:06 -070073 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070074 fHasIBounds = true;
csmartdaltoncbecb082016-07-22 08:59:08 -070075
csmartdalton77f2fae2016-08-08 09:55:06 -070076 // Implement the clip with an AA rect element.
reed73603f32016-09-20 08:42:38 -070077 fElements.addToHead(stackBounds, SkCanvas::kReplace_Op, true/*doAA*/);
csmartdalton8d3f92a2016-08-17 09:39:38 -070078 fElementsGenID = stack.getTopmostGenID();
csmartdalton77f2fae2016-08-08 09:55:06 -070079 fRequiresAA = true;
80
81 fInitialState = InitialState::kAllOut;
82 return;
csmartdaltoncbecb082016-07-22 08:59:08 -070083 }
84
85 SkRect tighterQuery = queryBounds;
86 if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
87 // Tighten the query by introducing a new clip at the stack's pixel boundaries. (This new
csmartdalton77f2fae2016-08-08 09:55:06 -070088 // clip will be enforced by the scissor through fIBounds.)
csmartdaltoncbecb082016-07-22 08:59:08 -070089 SkAssertResult(tighterQuery.intersect(GrClip::GetPixelBounds(stackBounds)));
csmartdaltoncbecb082016-07-22 08:59:08 -070090 }
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +000091
csmartdaltond211e782016-08-15 11:17:19 -070092 fIBounds = GrClip::GetPixelIBounds(tighterQuery);
csmartdalton77f2fae2016-08-08 09:55:06 -070093 SkASSERT(!fIBounds.isEmpty()); // Empty should have been blocked by IsOutsideClip above.
csmartdaltond211e782016-08-15 11:17:19 -070094 fHasIBounds = true;
csmartdalton77f2fae2016-08-08 09:55:06 -070095
bsalomon@google.com34cd70a2012-12-06 14:23:20 +000096 // Now that we have determined the bounds to use and filtered out the trivial cases, call the
97 // helper that actually walks the stack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -070098 this->walkStack(stack, tighterQuery, maxWindowRectangles);
99
100 if (fWindowRects.count() < maxWindowRectangles) {
101 this->addInteriorWindowRectangles(maxWindowRectangles);
102 }
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700103}
104
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700105void GrReducedClip::walkStack(const SkClipStack& stack, const SkRect& queryBounds,
106 int maxWindowRectangles) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700107 // walk backwards until we get to:
108 // a) the beginning
109 // b) an operation that is known to make the bounds all inside/outside
110 // c) a replace operation
111
112 enum class InitialTriState {
113 kUnknown = -1,
114 kAllIn = (int)GrReducedClip::InitialState::kAllIn,
115 kAllOut = (int)GrReducedClip::InitialState::kAllOut
116 } initialTriState = InitialTriState::kUnknown;
117
118 // During our backwards walk, track whether we've seen ops that either grow or shrink the clip.
119 // TODO: track these per saved clip so that we can consider them on the forward pass.
120 bool embiggens = false;
121 bool emsmallens = false;
122
123 // We use a slightly relaxed set of query bounds for element containment tests. This is to
124 // account for floating point rounding error that may have occurred during coord transforms.
125 SkRect relaxedQueryBounds = queryBounds.makeInset(GrClip::kBoundsTolerance,
126 GrClip::kBoundsTolerance);
127
128 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
129 int numAAElements = 0;
130 while (InitialTriState::kUnknown == initialTriState) {
131 const Element* element = iter.prev();
132 if (nullptr == element) {
133 initialTriState = InitialTriState::kAllIn;
134 break;
135 }
136 if (SkClipStack::kEmptyGenID == element->getGenID()) {
137 initialTriState = InitialTriState::kAllOut;
138 break;
139 }
140 if (SkClipStack::kWideOpenGenID == element->getGenID()) {
141 initialTriState = InitialTriState::kAllIn;
142 break;
143 }
144
145 bool skippable = false;
146 bool isFlip = false; // does this op just flip the in/out state of every point in the bounds
147
148 switch (element->getOp()) {
reed73603f32016-09-20 08:42:38 -0700149 case SkCanvas::kDifference_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700150 // check if the shape subtracted either contains the entire bounds (and makes
151 // the clip empty) or is outside the bounds and therefore can be skipped.
152 if (element->isInverseFilled()) {
153 if (element->contains(relaxedQueryBounds)) {
154 skippable = true;
155 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
156 initialTriState = InitialTriState::kAllOut;
157 skippable = true;
158 }
159 } else {
160 if (element->contains(relaxedQueryBounds)) {
161 initialTriState = InitialTriState::kAllOut;
162 skippable = true;
163 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
164 skippable = true;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700165 } else if (fWindowRects.count() < maxWindowRectangles && !embiggens &&
166 !element->isAA() && Element::kRect_Type == element->getType()) {
167 this->addWindowRectangle(element->getRect(), false);
168 skippable = true;
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700169 }
170 }
171 if (!skippable) {
172 emsmallens = true;
173 }
174 break;
reed73603f32016-09-20 08:42:38 -0700175 case SkCanvas::kIntersect_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700176 // check if the shape intersected contains the entire bounds and therefore can
177 // be skipped or it is outside the entire bounds and therefore makes the clip
178 // empty.
179 if (element->isInverseFilled()) {
180 if (element->contains(relaxedQueryBounds)) {
181 initialTriState = InitialTriState::kAllOut;
182 skippable = true;
183 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
184 skippable = true;
185 }
186 } else {
187 if (element->contains(relaxedQueryBounds)) {
188 skippable = true;
189 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
190 initialTriState = InitialTriState::kAllOut;
191 skippable = true;
192 } else if (!embiggens && !element->isAA() &&
193 Element::kRect_Type == element->getType()) {
194 // fIBounds and queryBounds have already acccounted for this element via
195 // clip stack bounds; here we just apply the non-aa rounding effect.
196 SkIRect nonaaRect;
197 element->getRect().round(&nonaaRect);
198 if (!this->intersectIBounds(nonaaRect)) {
199 return;
200 }
201 skippable = true;
202 }
203 }
204 if (!skippable) {
205 emsmallens = true;
206 }
207 break;
reed73603f32016-09-20 08:42:38 -0700208 case SkCanvas::kUnion_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700209 // If the union-ed shape contains the entire bounds then after this element
210 // the bounds is entirely inside the clip. If the union-ed shape is outside the
211 // bounds then this op can be skipped.
212 if (element->isInverseFilled()) {
213 if (element->contains(relaxedQueryBounds)) {
214 skippable = true;
215 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
216 initialTriState = InitialTriState::kAllIn;
217 skippable = true;
218 }
219 } else {
220 if (element->contains(relaxedQueryBounds)) {
221 initialTriState = InitialTriState::kAllIn;
222 skippable = true;
223 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
224 skippable = true;
225 }
226 }
227 if (!skippable) {
228 embiggens = true;
229 }
230 break;
reed73603f32016-09-20 08:42:38 -0700231 case SkCanvas::kXOR_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700232 // If the bounds is entirely inside the shape being xor-ed then the effect is
233 // to flip the inside/outside state of every point in the bounds. We may be
234 // able to take advantage of this in the forward pass. If the xor-ed shape
235 // doesn't intersect the bounds then it can be skipped.
236 if (element->isInverseFilled()) {
237 if (element->contains(relaxedQueryBounds)) {
238 skippable = true;
239 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
240 isFlip = true;
241 }
242 } else {
243 if (element->contains(relaxedQueryBounds)) {
244 isFlip = true;
245 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
246 skippable = true;
247 }
248 }
249 if (!skippable) {
250 emsmallens = embiggens = true;
251 }
252 break;
reed73603f32016-09-20 08:42:38 -0700253 case SkCanvas::kReverseDifference_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700254 // When the bounds is entirely within the rev-diff shape then this behaves like xor
255 // and reverses every point inside the bounds. If the shape is completely outside
256 // the bounds then we know after this element is applied that the bounds will be
257 // all outside the current clip.B
258 if (element->isInverseFilled()) {
259 if (element->contains(relaxedQueryBounds)) {
260 initialTriState = InitialTriState::kAllOut;
261 skippable = true;
262 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
263 isFlip = true;
264 }
265 } else {
266 if (element->contains(relaxedQueryBounds)) {
267 isFlip = true;
268 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
269 initialTriState = InitialTriState::kAllOut;
270 skippable = true;
271 }
272 }
273 if (!skippable) {
274 emsmallens = embiggens = true;
275 }
276 break;
277
reed73603f32016-09-20 08:42:38 -0700278 case SkCanvas::kReplace_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700279 // Replace will always terminate our walk. We will either begin the forward walk
280 // at the replace op or detect here than the shape is either completely inside
281 // or completely outside the bounds. In this latter case it can be skipped by
282 // setting the correct value for initialTriState.
283 if (element->isInverseFilled()) {
284 if (element->contains(relaxedQueryBounds)) {
285 initialTriState = InitialTriState::kAllOut;
286 skippable = true;
287 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
288 initialTriState = InitialTriState::kAllIn;
289 skippable = true;
290 }
291 } else {
292 if (element->contains(relaxedQueryBounds)) {
293 initialTriState = InitialTriState::kAllIn;
294 skippable = true;
295 } else if (GrClip::IsOutsideClip(element->getBounds(), queryBounds)) {
296 initialTriState = InitialTriState::kAllOut;
297 skippable = true;
298 } else if (!embiggens && !element->isAA() &&
299 Element::kRect_Type == element->getType()) {
300 // fIBounds and queryBounds have already acccounted for this element via
301 // clip stack bounds; here we just apply the non-aa rounding effect.
302 SkIRect nonaaRect;
303 element->getRect().round(&nonaaRect);
304 if (!this->intersectIBounds(nonaaRect)) {
305 return;
306 }
307 initialTriState = InitialTriState::kAllIn;
308 skippable = true;
309 }
310 }
311 if (!skippable) {
312 initialTriState = InitialTriState::kAllOut;
313 embiggens = emsmallens = true;
314 }
315 break;
316 default:
317 SkDEBUGFAIL("Unexpected op.");
318 break;
319 }
320 if (!skippable) {
321 if (0 == fElements.count()) {
322 // This will be the last element. Record the stricter genID.
323 fElementsGenID = element->getGenID();
324 }
325
326 // if it is a flip, change it to a bounds-filling rect
327 if (isFlip) {
reed73603f32016-09-20 08:42:38 -0700328 SkASSERT(SkCanvas::kXOR_Op == element->getOp() ||
329 SkCanvas::kReverseDifference_Op == element->getOp());
330 fElements.addToHead(SkRect::Make(fIBounds), SkCanvas::kReverseDifference_Op, false);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700331 } else {
332 Element* newElement = fElements.addToHead(*element);
333 if (newElement->isAA()) {
334 ++numAAElements;
335 }
336 // Intersecting an inverse shape is the same as differencing the non-inverse shape.
337 // Replacing with an inverse shape is the same as setting initialState=kAllIn and
338 // differencing the non-inverse shape.
reed73603f32016-09-20 08:42:38 -0700339 bool isReplace = SkCanvas::kReplace_Op == newElement->getOp();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700340 if (newElement->isInverseFilled() &&
reed73603f32016-09-20 08:42:38 -0700341 (SkCanvas::kIntersect_Op == newElement->getOp() || isReplace)) {
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700342 newElement->invertShapeFillType();
reed73603f32016-09-20 08:42:38 -0700343 newElement->setOp(SkCanvas::kDifference_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700344 if (isReplace) {
345 SkASSERT(InitialTriState::kAllOut == initialTriState);
346 initialTriState = InitialTriState::kAllIn;
347 }
348 }
349 }
350 }
351 }
352
353 if ((InitialTriState::kAllOut == initialTriState && !embiggens) ||
354 (InitialTriState::kAllIn == initialTriState && !emsmallens)) {
355 fElements.reset();
356 numAAElements = 0;
357 } else {
358 Element* element = fElements.headIter().get();
359 while (element) {
360 bool skippable = false;
361 switch (element->getOp()) {
reed73603f32016-09-20 08:42:38 -0700362 case SkCanvas::kDifference_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700363 // subtracting from the empty set yields the empty set.
364 skippable = InitialTriState::kAllOut == initialTriState;
365 break;
reed73603f32016-09-20 08:42:38 -0700366 case SkCanvas::kIntersect_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700367 // intersecting with the empty set yields the empty set
368 if (InitialTriState::kAllOut == initialTriState) {
369 skippable = true;
370 } else {
371 // We can clear to zero and then simply draw the clip element.
372 initialTriState = InitialTriState::kAllOut;
reed73603f32016-09-20 08:42:38 -0700373 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700374 }
375 break;
reed73603f32016-09-20 08:42:38 -0700376 case SkCanvas::kUnion_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700377 if (InitialTriState::kAllIn == initialTriState) {
378 // unioning the infinite plane with anything is a no-op.
379 skippable = true;
380 } else {
381 // unioning the empty set with a shape is the shape.
reed73603f32016-09-20 08:42:38 -0700382 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700383 }
384 break;
reed73603f32016-09-20 08:42:38 -0700385 case SkCanvas::kXOR_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700386 if (InitialTriState::kAllOut == initialTriState) {
387 // xor could be changed to diff in the kAllIn case, not sure it's a win.
reed73603f32016-09-20 08:42:38 -0700388 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700389 }
390 break;
reed73603f32016-09-20 08:42:38 -0700391 case SkCanvas::kReverseDifference_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700392 if (InitialTriState::kAllIn == initialTriState) {
393 // subtracting the whole plane will yield the empty set.
394 skippable = true;
395 initialTriState = InitialTriState::kAllOut;
396 } else {
397 // this picks up flips inserted in the backwards pass.
398 skippable = element->isInverseFilled() ?
399 GrClip::IsOutsideClip(element->getBounds(), queryBounds) :
400 element->contains(relaxedQueryBounds);
401 if (skippable) {
402 initialTriState = InitialTriState::kAllIn;
403 } else {
reed73603f32016-09-20 08:42:38 -0700404 element->setOp(SkCanvas::kReplace_Op);
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700405 }
406 }
407 break;
reed73603f32016-09-20 08:42:38 -0700408 case SkCanvas::kReplace_Op:
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700409 skippable = false; // we would have skipped it in the backwards walk if we
410 // could've.
411 break;
412 default:
413 SkDEBUGFAIL("Unexpected op.");
414 break;
415 }
416 if (!skippable) {
417 break;
418 } else {
419 if (element->isAA()) {
420 --numAAElements;
421 }
422 fElements.popHead();
423 element = fElements.headIter().get();
424 }
425 }
426 }
427 fRequiresAA = numAAElements > 0;
428
429 SkASSERT(InitialTriState::kUnknown != initialTriState);
430 fInitialState = static_cast<GrReducedClip::InitialState>(initialTriState);
431}
432
reed73603f32016-09-20 08:42:38 -0700433static bool element_is_pure_subtract(SkCanvas::ClipOp op) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700434 SkASSERT(op >= 0);
reed73603f32016-09-20 08:42:38 -0700435 return op <= SkCanvas::kIntersect_Op;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700436
reed73603f32016-09-20 08:42:38 -0700437 GR_STATIC_ASSERT(0 == SkCanvas::kDifference_Op);
438 GR_STATIC_ASSERT(1 == SkCanvas::kIntersect_Op);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700439}
440
441void GrReducedClip::addInteriorWindowRectangles(int maxWindowRectangles) {
442 SkASSERT(fWindowRects.count() < maxWindowRectangles);
443 // Walk backwards through the element list and add window rectangles to the interiors of
444 // "difference" elements. Quit if we encounter an element that may grow the clip.
445 ElementList::Iter iter(fElements, ElementList::Iter::kTail_IterStart);
446 for (; iter.get() && element_is_pure_subtract(iter.get()->getOp()); iter.prev()) {
447 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700448 if (SkCanvas::kDifference_Op != element->getOp()) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700449 continue;
450 }
451
452 if (Element::kRect_Type == element->getType()) {
453 SkASSERT(element->isAA());
454 this->addWindowRectangle(element->getRect(), true);
455 if (fWindowRects.count() >= maxWindowRectangles) {
456 return;
457 }
458 continue;
459 }
460
461 if (Element::kRRect_Type == element->getType()) {
462 // For round rects we add two overlapping windows in the shape of a plus.
463 const SkRRect& clipRRect = element->getRRect();
464 SkVector insetTL = clipRRect.radii(SkRRect::kUpperLeft_Corner);
465 SkVector insetBR = clipRRect.radii(SkRRect::kLowerRight_Corner);
466 if (SkRRect::kComplex_Type == clipRRect.getType()) {
467 const SkVector& insetTR = clipRRect.radii(SkRRect::kUpperRight_Corner);
468 const SkVector& insetBL = clipRRect.radii(SkRRect::kLowerLeft_Corner);
469 insetTL.fX = SkTMax(insetTL.x(), insetBL.x());
470 insetTL.fY = SkTMax(insetTL.y(), insetTR.y());
471 insetBR.fX = SkTMax(insetBR.x(), insetTR.x());
472 insetBR.fY = SkTMax(insetBR.y(), insetBL.y());
473 }
474 const SkRect& bounds = clipRRect.getBounds();
475 if (insetTL.x() + insetBR.x() >= bounds.width() ||
476 insetTL.y() + insetBR.y() >= bounds.height()) {
477 continue; // The interior "plus" is empty.
478 }
479
480 SkRect horzRect = SkRect::MakeLTRB(bounds.left(), bounds.top() + insetTL.y(),
481 bounds.right(), bounds.bottom() - insetBR.y());
482 this->addWindowRectangle(horzRect, element->isAA());
483 if (fWindowRects.count() >= maxWindowRectangles) {
484 return;
485 }
486
487 SkRect vertRect = SkRect::MakeLTRB(bounds.left() + insetTL.x(), bounds.top(),
488 bounds.right() - insetBR.x(), bounds.bottom());
489 this->addWindowRectangle(vertRect, element->isAA());
490 if (fWindowRects.count() >= maxWindowRectangles) {
491 return;
492 }
493 continue;
494 }
495 }
496}
497
498inline void GrReducedClip::addWindowRectangle(const SkRect& elementInteriorRect, bool elementIsAA) {
499 SkIRect window;
500 if (!elementIsAA) {
501 elementInteriorRect.round(&window);
502 } else {
503 elementInteriorRect.roundIn(&window);
504 }
505 if (!window.isEmpty()) { // Skip very thin windows that round to zero or negative dimensions.
506 fWindowRects.addWindow(window);
507 }
508}
509
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700510inline bool GrReducedClip::intersectIBounds(const SkIRect& irect) {
511 SkASSERT(fHasIBounds);
512 if (!fIBounds.intersect(irect)) {
513 fHasIBounds = false;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700514 fWindowRects.reset();
csmartdalton5ecbbbe2016-08-23 13:26:40 -0700515 fElements.reset();
516 fRequiresAA = false;
517 fInitialState = InitialState::kAllOut;
518 return false;
519 }
520 return true;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000521}
csmartdaltonbde96c62016-08-31 12:54:46 -0700522
523////////////////////////////////////////////////////////////////////////////////
524// Create a 8-bit clip mask in alpha
525
Brian Osman11052242016-10-27 14:47:55 -0400526static bool stencil_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700527 const GrFixedClip& clip,
528 const GrUserStencilSettings* ss,
529 const SkMatrix& viewMatrix,
530 const SkClipStack::Element* element) {
531
532 // TODO: Draw rrects directly here.
533 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(),
540 element->isInverseFilled(),
541 element->isAA(),
542 viewMatrix,
543 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700544 break;
545 default: {
546 SkPath path;
547 element->asPath(&path);
548 if (path.isInverseFillType()) {
549 path.toggleInverseFillType();
550 }
551
Brian Osman693a5402016-10-27 15:13:22 -0400552 return rtc->priv().drawAndStencilPath(clip, ss,
553 (SkRegion::Op)element->getOp(),
554 element->isInverseFilled(),
555 element->isAA(), viewMatrix,
556 path);
csmartdaltonbde96c62016-08-31 12:54:46 -0700557 break;
558 }
559 }
560
561 return false;
562}
563
Brian Osman11052242016-10-27 14:47:55 -0400564static void draw_element(GrRenderTargetContext* rtc,
csmartdaltonbde96c62016-08-31 12:54:46 -0700565 const GrClip& clip, // TODO: can this just always be WideOpen?
566 const GrPaint &paint,
567 const SkMatrix& viewMatrix,
568 const SkClipStack::Element* element) {
569
570 // TODO: Draw rrects directly here.
571 switch (element->getType()) {
572 case Element::kEmpty_Type:
573 SkDEBUGFAIL("Should never get here with an empty element.");
574 break;
575 case Element::kRect_Type:
Brian Osman11052242016-10-27 14:47:55 -0400576 rtc->drawRect(clip, paint, viewMatrix, element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700577 break;
578 default: {
579 SkPath path;
580 element->asPath(&path);
581 if (path.isInverseFillType()) {
582 path.toggleInverseFillType();
583 }
584
Brian Osman11052242016-10-27 14:47:55 -0400585 rtc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
csmartdaltonbde96c62016-08-31 12:54:46 -0700586 break;
587 }
588 }
589}
590
Brian Osman11052242016-10-27 14:47:55 -0400591bool GrReducedClip::drawAlphaClipMask(GrRenderTargetContext* rtc) const {
csmartdaltonbde96c62016-08-31 12:54:46 -0700592 // The texture may be larger than necessary, this rect represents the part of the texture
593 // we populate with a rasterization of the clip.
594 GrFixedClip clip(SkIRect::MakeWH(fIBounds.width(), fIBounds.height()));
595
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700596 if (!fWindowRects.empty()) {
597 clip.setWindowRectangles(fWindowRects, {fIBounds.left(), fIBounds.top()},
598 GrWindowRectsState::Mode::kExclusive);
599 }
600
csmartdaltonbde96c62016-08-31 12:54:46 -0700601 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
602 // clear the part that we care about.
603 GrColor initialCoverage = InitialState::kAllIn == this->initialState() ? -1 : 0;
Brian Osman693a5402016-10-27 15:13:22 -0400604 rtc->priv().clear(clip, initialCoverage, true);
csmartdaltonbde96c62016-08-31 12:54:46 -0700605
606 // Set the matrix so that rendered clip elements are transformed to mask space from clip space.
607 SkMatrix translate;
608 translate.setTranslate(SkIntToScalar(-fIBounds.left()), SkIntToScalar(-fIBounds.top()));
609
610 // walk through each clip element and perform its set op
611 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
612 const Element* element = iter.get();
reed73603f32016-09-20 08:42:38 -0700613 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700614 bool invert = element->isInverseFilled();
615 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
616 // draw directly into the result with the stencil set to make the pixels affected
617 // by the clip shape be non-zero.
618 static constexpr GrUserStencilSettings kStencilInElement(
619 GrUserStencilSettings::StaticInit<
620 0xffff,
621 GrUserStencilTest::kAlways,
622 0xffff,
623 GrUserStencilOp::kReplace,
624 GrUserStencilOp::kReplace,
625 0xffff>()
626 );
Brian Osman11052242016-10-27 14:47:55 -0400627 if (!stencil_element(rtc, clip, &kStencilInElement, translate, element)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700628 return false;
629 }
630
631 // Draw to the exterior pixels (those with a zero stencil value).
632 static constexpr GrUserStencilSettings kDrawOutsideElement(
633 GrUserStencilSettings::StaticInit<
634 0x0000,
635 GrUserStencilTest::kEqual,
636 0xffff,
637 GrUserStencilOp::kZero,
638 GrUserStencilOp::kZero,
639 0xffff>()
640 );
Brian Osman693a5402016-10-27 15:13:22 -0400641 if (!rtc->priv().drawAndStencilRect(clip, &kDrawOutsideElement, op, !invert, false,
642 translate, SkRect::Make(fIBounds))) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700643 return false;
644 }
645 } else {
646 // all the remaining ops can just be directly draw into the accumulation buffer
647 GrPaint paint;
648 paint.setAntiAlias(element->isAA());
649 paint.setCoverageSetOpXPFactory(op, false);
650
Brian Osman11052242016-10-27 14:47:55 -0400651 draw_element(rtc, clip, paint, translate, element);
csmartdaltonbde96c62016-08-31 12:54:46 -0700652 }
653 }
654
655 return true;
656}
657
658////////////////////////////////////////////////////////////////////////////////
659// Create a 1-bit clip mask in the stencil buffer.
660
661class StencilClip final : public GrClip {
662public:
663 StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {}
664 const GrFixedClip& fixedClip() const { return fFixedClip; }
665
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700666 void setWindowRectangles(const GrWindowRectangles& windows, const SkIPoint& origin,
667 GrWindowRectsState::Mode mode) {
668 fFixedClip.setWindowRectangles(windows, origin, mode);
669 }
670
csmartdaltonbde96c62016-08-31 12:54:46 -0700671private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700672 bool quickContains(const SkRect&) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700673 return false;
674 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700675 void getConservativeBounds(int width, int height, SkIRect* bounds, bool* iior) const override {
676 fFixedClip.getConservativeBounds(width, height, bounds, iior);
csmartdaltonbde96c62016-08-31 12:54:46 -0700677 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700678 bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const override {
csmartdaltonbde96c62016-08-31 12:54:46 -0700679 return false;
680 }
Brian Osman11052242016-10-27 14:47:55 -0400681 bool apply(GrContext* context, GrRenderTargetContext* renderTargetContext, bool useHWAA,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700682 bool hasUserStencilSettings, GrAppliedClip* out) const override {
Brian Osman11052242016-10-27 14:47:55 -0400683 if (!fFixedClip.apply(context, renderTargetContext, useHWAA, hasUserStencilSettings, out)) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700684 return false;
685 }
686 out->addStencilClip();
687 return true;
688 }
689
690 GrFixedClip fFixedClip;
691
692 typedef GrClip INHERITED;
693};
694
695bool GrReducedClip::drawStencilClipMask(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400696 GrRenderTargetContext* renderTargetContext,
csmartdaltonbde96c62016-08-31 12:54:46 -0700697 const SkIPoint& clipOrigin) const {
698 // We set the current clip to the bounds so that our recursive draws are scissored to them.
699 StencilClip stencilClip(fIBounds.makeOffset(-clipOrigin.x(), -clipOrigin.y()));
700
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700701 if (!fWindowRects.empty()) {
702 stencilClip.setWindowRectangles(fWindowRects, clipOrigin,
703 GrWindowRectsState::Mode::kExclusive);
704 }
705
csmartdaltonbde96c62016-08-31 12:54:46 -0700706 bool initialState = InitialState::kAllIn == this->initialState();
Brian Osman693a5402016-10-27 15:13:22 -0400707 renderTargetContext->priv().clearStencilClip(stencilClip.fixedClip(), initialState);
csmartdaltonbde96c62016-08-31 12:54:46 -0700708
709 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
710 SkMatrix viewMatrix;
711 viewMatrix.setTranslate(SkIntToScalar(-clipOrigin.x()), SkIntToScalar(-clipOrigin.y()));
712
713 // walk through each clip element and perform its set op
714 // with the existing clip.
715 for (ElementList::Iter iter(fElements); iter.get(); iter.next()) {
716 const Element* element = iter.get();
Brian Osman11052242016-10-27 14:47:55 -0400717 bool useHWAA = element->isAA() && renderTargetContext->isStencilBufferMultisampled();
csmartdaltonbde96c62016-08-31 12:54:46 -0700718
719 bool fillInverted = false;
720
721 // This will be used to determine whether the clip shape can be rendered into the
722 // stencil with arbitrary stencil settings.
723 GrPathRenderer::StencilSupport stencilSupport;
724
reed73603f32016-09-20 08:42:38 -0700725 SkRegion::Op op = (SkRegion::Op)element->getOp();
csmartdaltonbde96c62016-08-31 12:54:46 -0700726
727 GrPathRenderer* pr = nullptr;
728 SkPath clipPath;
729 if (Element::kRect_Type == element->getType()) {
730 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
731 fillInverted = false;
732 } else {
733 element->asPath(&clipPath);
734 fillInverted = clipPath.isInverseFillType();
735 if (fillInverted) {
736 clipPath.toggleInverseFillType();
737 }
738
739 GrShape shape(clipPath, GrStyle::SimpleFill());
740 GrPathRenderer::CanDrawPathArgs canDrawArgs;
741 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
742 canDrawArgs.fViewMatrix = &viewMatrix;
743 canDrawArgs.fShape = &shape;
744 canDrawArgs.fAntiAlias = false;
745 canDrawArgs.fHasUserStencilSettings = false;
Brian Osman11052242016-10-27 14:47:55 -0400746 canDrawArgs.fIsStencilBufferMSAA = renderTargetContext->isStencilBufferMultisampled();
csmartdaltonbde96c62016-08-31 12:54:46 -0700747
748 GrDrawingManager* dm = context->contextPriv().drawingManager();
749 pr = dm->getPathRenderer(canDrawArgs, false,
750 GrPathRendererChain::kStencilOnly_DrawType,
751 &stencilSupport);
752 if (!pr) {
753 return false;
754 }
755 }
756
757 bool canRenderDirectToStencil =
758 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
759 bool drawDirectToClip; // Given the renderer, the element,
760 // fill rule, and set operation should
761 // we render the element directly to
762 // stencil bit used for clipping.
763 GrUserStencilSettings const* const* stencilPasses =
764 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
765 &drawDirectToClip);
766
767 // draw the element to the client stencil bits if necessary
768 if (!drawDirectToClip) {
769 static constexpr GrUserStencilSettings kDrawToStencil(
770 GrUserStencilSettings::StaticInit<
771 0x0000,
772 GrUserStencilTest::kAlways,
773 0xffff,
774 GrUserStencilOp::kIncMaybeClamp,
775 GrUserStencilOp::kIncMaybeClamp,
776 0xffff>()
777 );
778 if (Element::kRect_Type == element->getType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400779 renderTargetContext->priv().stencilRect(stencilClip.fixedClip(), &kDrawToStencil,
780 useHWAA, viewMatrix, element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700781 } else {
782 if (!clipPath.isEmpty()) {
783 GrShape shape(clipPath, GrStyle::SimpleFill());
784 if (canRenderDirectToStencil) {
785 GrPaint paint;
786 paint.setXPFactory(GrDisableColorXPFactory::Make());
787 paint.setAntiAlias(element->isAA());
788
789 GrPathRenderer::DrawPathArgs args;
790 args.fResourceProvider = context->resourceProvider();
791 args.fPaint = &paint;
792 args.fUserStencilSettings = &kDrawToStencil;
Brian Osman11052242016-10-27 14:47:55 -0400793 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700794 args.fClip = &stencilClip.fixedClip();
795 args.fViewMatrix = &viewMatrix;
796 args.fShape = &shape;
797 args.fAntiAlias = false;
798 args.fGammaCorrect = false;
799 pr->drawPath(args);
800 } else {
801 GrPathRenderer::StencilPathArgs args;
802 args.fResourceProvider = context->resourceProvider();
Brian Osman11052242016-10-27 14:47:55 -0400803 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700804 args.fClip = &stencilClip.fixedClip();
805 args.fViewMatrix = &viewMatrix;
806 args.fIsAA = element->isAA();
807 args.fShape = &shape;
808 pr->stencilPath(args);
809 }
810 }
811 }
812 }
813
814 // now we modify the clip bit by rendering either the clip
815 // element directly or a bounding rect of the entire clip.
816 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
817 if (drawDirectToClip) {
818 if (Element::kRect_Type == element->getType()) {
Brian Osman693a5402016-10-27 15:13:22 -0400819 renderTargetContext->priv().stencilRect(stencilClip, *pass, useHWAA, viewMatrix,
820 element->getRect());
csmartdaltonbde96c62016-08-31 12:54:46 -0700821 } else {
822 GrShape shape(clipPath, GrStyle::SimpleFill());
823 GrPaint paint;
824 paint.setXPFactory(GrDisableColorXPFactory::Make());
825 paint.setAntiAlias(element->isAA());
826 GrPathRenderer::DrawPathArgs args;
827 args.fResourceProvider = context->resourceProvider();
828 args.fPaint = &paint;
829 args.fUserStencilSettings = *pass;
Brian Osman11052242016-10-27 14:47:55 -0400830 args.fRenderTargetContext = renderTargetContext;
csmartdaltonbde96c62016-08-31 12:54:46 -0700831 args.fClip = &stencilClip;
832 args.fViewMatrix = &viewMatrix;
833 args.fShape = &shape;
834 args.fAntiAlias = false;
835 args.fGammaCorrect = false;
836 pr->drawPath(args);
837 }
838 } else {
839 // The view matrix is setup to do clip space -> stencil space translation, so
840 // draw rect in clip space.
Brian Osman693a5402016-10-27 15:13:22 -0400841 renderTargetContext->priv().stencilRect(stencilClip, *pass, false, viewMatrix,
842 SkRect::Make(fIBounds));
csmartdaltonbde96c62016-08-31 12:54:46 -0700843 }
844 }
845 }
846 return true;
847}