2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "GrReducedClip.h"
12 #include "SkClipStack.h"
18 static void test_assign_and_comparison(skiatest::Reporter* reporter) {
22 REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
24 // Build up a clip stack with a path, an empty clip, and a rect.
26 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
33 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
36 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
38 SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
39 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
40 r = SkRect::MakeLTRB(10, 11, 12, 13);
41 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
44 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
46 r = SkRect::MakeLTRB(14, 15, 16, 17);
47 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
49 // Test that assignment works.
51 REPORTER_ASSERT(reporter, s == copy);
53 // Test that different save levels triggers not equal.
55 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
56 REPORTER_ASSERT(reporter, s != copy);
58 // Test that an equal, but not copied version is equal.
60 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
61 r = SkRect::MakeLTRB(14, 15, 16, 17);
62 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
63 REPORTER_ASSERT(reporter, s == copy);
65 // Test that a different op on one level triggers not equal.
67 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
69 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
70 r = SkRect::MakeLTRB(14, 15, 16, 17);
71 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
72 REPORTER_ASSERT(reporter, s != copy);
74 // Test that version constructed with rect-path rather than a rect is still considered equal.
79 s.clipDevPath(rp, SkRegion::kUnion_Op, doAA);
80 REPORTER_ASSERT(reporter, s == copy);
82 // Test that different rects triggers not equal.
84 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
86 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
88 r = SkRect::MakeLTRB(24, 25, 26, 27);
89 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
90 REPORTER_ASSERT(reporter, s != copy);
94 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
97 REPORTER_ASSERT(reporter, 2 == copy.getSaveCount());
98 REPORTER_ASSERT(reporter, s == copy);
100 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
102 REPORTER_ASSERT(reporter, 1 == copy.getSaveCount());
103 REPORTER_ASSERT(reporter, s == copy);
105 // Test that different paths triggers not equal.
107 REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
109 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
112 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
113 REPORTER_ASSERT(reporter, s != copy);
116 static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
118 SkClipStack::B2TIter iter(stack);
120 while (iter.next()) {
123 REPORTER_ASSERT(reporter, count == counter);
126 // Exercise the SkClipStack's bottom to top and bidirectional iterators
127 // (including the skipToTopmost functionality)
128 static void test_iterators(skiatest::Reporter* reporter) {
131 static const SkRect gRects[] = {
138 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
139 // the union op will prevent these from being fused together
140 stack.clipDevRect(gRects[i], SkRegion::kUnion_Op, false);
143 assert_count(reporter, stack, 4);
145 // bottom to top iteration
147 const SkClipStack::Element* element = NULL;
149 SkClipStack::B2TIter iter(stack);
152 for (i = 0, element = iter.next(); element; ++i, element = iter.next()) {
153 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
154 REPORTER_ASSERT(reporter, element->getRect() == gRects[i]);
160 // top to bottom iteration
162 const SkClipStack::Element* element = NULL;
164 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
167 for (i = 3, element = iter.prev(); element; --i, element = iter.prev()) {
168 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
169 REPORTER_ASSERT(reporter, element->getRect() == gRects[i]);
177 const SkClipStack::Element* element = NULL;
179 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
181 element = iter.skipToTopmost(SkRegion::kUnion_Op);
182 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
183 REPORTER_ASSERT(reporter, element->getRect() == gRects[3]);
187 // Exercise the SkClipStack's getConservativeBounds computation
188 static void test_bounds(skiatest::Reporter* reporter, SkClipStack::Element::Type primType) {
189 static const int gNumCases = 20;
190 static const SkRect gAnswerRectsBW[gNumCases] = {
220 static const SkRegion::Op gOps[] = {
221 SkRegion::kIntersect_Op,
222 SkRegion::kDifference_Op,
225 SkRegion::kReverseDifference_Op
230 rectA.iset(10, 10, 50, 50);
231 rectB.iset(40, 40, 80, 80);
233 SkRRect rrectA, rrectB;
234 rrectA.setOval(rectA);
235 rrectB.setRectXY(rectB, SkIntToScalar(1), SkIntToScalar(2));
239 pathA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
240 pathB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
244 bool isIntersectionOfRects = false;
247 int numBitTests = SkClipStack::Element::kPath_Type == primType ? 4 : 1;
248 for (int invBits = 0; invBits < numBitTests; ++invBits) {
249 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
252 bool doInvA = SkToBool(invBits & 1);
253 bool doInvB = SkToBool(invBits & 2);
255 pathA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
256 SkPath::kEvenOdd_FillType);
257 pathB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
258 SkPath::kEvenOdd_FillType);
261 case SkClipStack::Element::kEmpty_Type:
262 SkDEBUGFAIL("Don't call this with kEmpty.");
264 case SkClipStack::Element::kRect_Type:
265 stack.clipDevRect(rectA, SkRegion::kIntersect_Op, false);
266 stack.clipDevRect(rectB, gOps[op], false);
268 case SkClipStack::Element::kRRect_Type:
269 stack.clipDevRRect(rrectA, SkRegion::kIntersect_Op, false);
270 stack.clipDevRRect(rrectB, gOps[op], false);
272 case SkClipStack::Element::kPath_Type:
273 stack.clipDevPath(pathA, SkRegion::kIntersect_Op, false);
274 stack.clipDevPath(pathB, gOps[op], false);
278 REPORTER_ASSERT(reporter, !stack.isWideOpen());
279 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID != stack.getTopmostGenID());
281 stack.getConservativeBounds(0, 0, 100, 100, &devClipBound,
282 &isIntersectionOfRects);
284 if (SkClipStack::Element::kRect_Type == primType) {
285 REPORTER_ASSERT(reporter, isIntersectionOfRects ==
286 (gOps[op] == SkRegion::kIntersect_Op));
288 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
291 SkASSERT(testCase < gNumCases);
292 REPORTER_ASSERT(reporter, devClipBound == gAnswerRectsBW[testCase]);
300 // Test out 'isWideOpen' entry point
301 static void test_isWideOpen(skiatest::Reporter* reporter) {
303 // Empty stack is wide open. Wide open stack means that gen id is wide open.
305 REPORTER_ASSERT(reporter, stack.isWideOpen());
306 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
311 rectA.iset(10, 10, 40, 40);
312 rectB.iset(50, 50, 80, 80);
314 // Stack should initially be wide open
318 REPORTER_ASSERT(reporter, stack.isWideOpen());
319 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
322 // Test out case where the user specifies a union that includes everything
328 clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
329 clipA.setFillType(SkPath::kInverseEvenOdd_FillType);
331 clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
332 clipB.setFillType(SkPath::kInverseEvenOdd_FillType);
334 stack.clipDevPath(clipA, SkRegion::kReplace_Op, false);
335 stack.clipDevPath(clipB, SkRegion::kUnion_Op, false);
337 REPORTER_ASSERT(reporter, stack.isWideOpen());
338 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
341 // Test out union w/ a wide open clip
345 stack.clipDevRect(rectA, SkRegion::kUnion_Op, false);
347 REPORTER_ASSERT(reporter, stack.isWideOpen());
348 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
351 // Test out empty difference from a wide open clip
356 emptyRect.setEmpty();
358 stack.clipDevRect(emptyRect, SkRegion::kDifference_Op, false);
360 REPORTER_ASSERT(reporter, stack.isWideOpen());
361 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
364 // Test out return to wide open
370 stack.clipDevRect(rectA, SkRegion::kReplace_Op, false);
372 REPORTER_ASSERT(reporter, !stack.isWideOpen());
373 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID != stack.getTopmostGenID());
377 REPORTER_ASSERT(reporter, stack.isWideOpen());
378 REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
382 static int count(const SkClipStack& stack) {
384 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
386 const SkClipStack::Element* element = NULL;
389 for (element = iter.prev(); element; element = iter.prev(), ++count) {
396 static void test_rect_inverse_fill(skiatest::Reporter* reporter) {
397 // non-intersecting rectangles
398 SkRect rect = SkRect::MakeLTRB(0, 0, 10, 10);
402 path.toggleInverseFillType();
404 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
407 SkClipStack::BoundsType boundsType;
408 stack.getBounds(&bounds, &boundsType);
409 REPORTER_ASSERT(reporter, SkClipStack::kInsideOut_BoundsType == boundsType);
410 REPORTER_ASSERT(reporter, bounds == rect);
413 static void test_rect_replace(skiatest::Reporter* reporter) {
414 SkRect rect = SkRect::MakeWH(100, 100);
415 SkRect rect2 = SkRect::MakeXYWH(50, 50, 100, 100);
418 SkClipStack::BoundsType type;
419 bool isIntersectionOfRects;
421 // Adding a new rect with the replace operator should not increase
422 // the stack depth. BW replacing BW.
425 REPORTER_ASSERT(reporter, 0 == count(stack));
426 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
427 REPORTER_ASSERT(reporter, 1 == count(stack));
428 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
429 REPORTER_ASSERT(reporter, 1 == count(stack));
432 // Adding a new rect with the replace operator should not increase
433 // the stack depth. AA replacing AA.
436 REPORTER_ASSERT(reporter, 0 == count(stack));
437 stack.clipDevRect(rect, SkRegion::kReplace_Op, true);
438 REPORTER_ASSERT(reporter, 1 == count(stack));
439 stack.clipDevRect(rect, SkRegion::kReplace_Op, true);
440 REPORTER_ASSERT(reporter, 1 == count(stack));
443 // Adding a new rect with the replace operator should not increase
444 // the stack depth. BW replacing AA replacing BW.
447 REPORTER_ASSERT(reporter, 0 == count(stack));
448 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
449 REPORTER_ASSERT(reporter, 1 == count(stack));
450 stack.clipDevRect(rect, SkRegion::kReplace_Op, true);
451 REPORTER_ASSERT(reporter, 1 == count(stack));
452 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
453 REPORTER_ASSERT(reporter, 1 == count(stack));
456 // Make sure replace clip rects don't collapse too much.
459 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
460 stack.clipDevRect(rect2, SkRegion::kIntersect_Op, false);
461 REPORTER_ASSERT(reporter, 1 == count(stack));
464 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
465 REPORTER_ASSERT(reporter, 2 == count(stack));
466 stack.getBounds(&bound, &type, &isIntersectionOfRects);
467 REPORTER_ASSERT(reporter, bound == rect);
469 REPORTER_ASSERT(reporter, 1 == count(stack));
472 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
473 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
474 REPORTER_ASSERT(reporter, 2 == count(stack));
476 REPORTER_ASSERT(reporter, 1 == count(stack));
479 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
480 stack.clipDevRect(rect2, SkRegion::kIntersect_Op, false);
481 stack.clipDevRect(rect, SkRegion::kReplace_Op, false);
482 REPORTER_ASSERT(reporter, 2 == count(stack));
484 REPORTER_ASSERT(reporter, 1 == count(stack));
488 // Simplified path-based version of test_rect_replace.
489 static void test_path_replace(skiatest::Reporter* reporter) {
490 SkRect rect = SkRect::MakeWH(100, 100);
492 path.addCircle(50, 50, 50);
494 // Replace operation doesn't grow the stack.
497 REPORTER_ASSERT(reporter, 0 == count(stack));
498 stack.clipDevPath(path, SkRegion::kReplace_Op, false);
499 REPORTER_ASSERT(reporter, 1 == count(stack));
500 stack.clipDevPath(path, SkRegion::kReplace_Op, false);
501 REPORTER_ASSERT(reporter, 1 == count(stack));
504 // Replacing rect with path.
507 stack.clipDevRect(rect, SkRegion::kReplace_Op, true);
508 REPORTER_ASSERT(reporter, 1 == count(stack));
509 stack.clipDevPath(path, SkRegion::kReplace_Op, true);
510 REPORTER_ASSERT(reporter, 1 == count(stack));
514 // Test out SkClipStack's merging of rect clips. In particular exercise
515 // merging of aa vs. bw rects.
516 static void test_rect_merging(skiatest::Reporter* reporter) {
518 SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50);
519 SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80);
521 SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90);
522 SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60);
525 SkClipStack::BoundsType type;
526 bool isIntersectionOfRects;
528 // all bw overlapping - should merge
532 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, false);
534 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
536 REPORTER_ASSERT(reporter, 1 == count(stack));
538 stack.getBounds(&bound, &type, &isIntersectionOfRects);
540 REPORTER_ASSERT(reporter, isIntersectionOfRects);
543 // all aa overlapping - should merge
547 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
549 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true);
551 REPORTER_ASSERT(reporter, 1 == count(stack));
553 stack.getBounds(&bound, &type, &isIntersectionOfRects);
555 REPORTER_ASSERT(reporter, isIntersectionOfRects);
558 // mixed overlapping - should _not_ merge
562 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
564 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
566 REPORTER_ASSERT(reporter, 2 == count(stack));
568 stack.getBounds(&bound, &type, &isIntersectionOfRects);
570 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
573 // mixed nested (bw inside aa) - should merge
577 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, true);
579 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, false);
581 REPORTER_ASSERT(reporter, 1 == count(stack));
583 stack.getBounds(&bound, &type, &isIntersectionOfRects);
585 REPORTER_ASSERT(reporter, isIntersectionOfRects);
588 // mixed nested (aa inside bw) - should merge
592 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, false);
594 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, true);
596 REPORTER_ASSERT(reporter, 1 == count(stack));
598 stack.getBounds(&bound, &type, &isIntersectionOfRects);
600 REPORTER_ASSERT(reporter, isIntersectionOfRects);
603 // reverse nested (aa inside bw) - should _not_ merge
607 stack.clipDevRect(nestedChild, SkRegion::kReplace_Op, false);
609 stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, true);
611 REPORTER_ASSERT(reporter, 2 == count(stack));
613 stack.getBounds(&bound, &type, &isIntersectionOfRects);
615 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
619 static void test_quickContains(skiatest::Reporter* reporter) {
620 SkRect testRect = SkRect::MakeLTRB(10, 10, 40, 40);
621 SkRect insideRect = SkRect::MakeLTRB(20, 20, 30, 30);
622 SkRect intersectingRect = SkRect::MakeLTRB(25, 25, 50, 50);
623 SkRect outsideRect = SkRect::MakeLTRB(0, 0, 50, 50);
624 SkRect nonIntersectingRect = SkRect::MakeLTRB(100, 100, 110, 110);
627 insideCircle.addCircle(25, 25, 5);
628 SkPath intersectingCircle;
629 intersectingCircle.addCircle(25, 40, 10);
630 SkPath outsideCircle;
631 outsideCircle.addCircle(25, 25, 50);
632 SkPath nonIntersectingCircle;
633 nonIntersectingCircle.addCircle(100, 100, 5);
637 stack.clipDevRect(outsideRect, SkRegion::kDifference_Op, false);
638 // return false because quickContains currently does not care for kDifference_Op
639 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
645 stack.clipDevRect(outsideRect, SkRegion::kReplace_Op, false);
646 REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
651 stack.clipDevRect(insideRect, SkRegion::kIntersect_Op, false);
652 stack.save(); // To prevent in-place substitution by replace OP
653 stack.clipDevRect(outsideRect, SkRegion::kReplace_Op, false);
654 REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
660 stack.clipDevRect(outsideRect, SkRegion::kIntersect_Op, false);
661 stack.save(); // To prevent in-place substitution by replace OP
662 stack.clipDevRect(insideRect, SkRegion::kReplace_Op, false);
663 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
667 // Verify proper traversal of multi-element clip
670 stack.clipDevRect(insideRect, SkRegion::kIntersect_Op, false);
671 // Use a path for second clip to prevent in-place intersection
672 stack.clipDevPath(outsideCircle, SkRegion::kIntersect_Op, false);
673 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
676 // Intersect Op tests with rectangles
679 stack.clipDevRect(outsideRect, SkRegion::kIntersect_Op, false);
680 REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
685 stack.clipDevRect(insideRect, SkRegion::kIntersect_Op, false);
686 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
691 stack.clipDevRect(intersectingRect, SkRegion::kIntersect_Op, false);
692 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
697 stack.clipDevRect(nonIntersectingRect, SkRegion::kIntersect_Op, false);
698 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
701 // Intersect Op tests with circle paths
704 stack.clipDevPath(outsideCircle, SkRegion::kIntersect_Op, false);
705 REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
710 stack.clipDevPath(insideCircle, SkRegion::kIntersect_Op, false);
711 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
716 stack.clipDevPath(intersectingCircle, SkRegion::kIntersect_Op, false);
717 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
722 stack.clipDevPath(nonIntersectingCircle, SkRegion::kIntersect_Op, false);
723 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
726 // Intersect Op tests with inverse filled rectangles
730 path.addRect(outsideRect);
731 path.toggleInverseFillType();
732 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
733 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
739 path.addRect(insideRect);
740 path.toggleInverseFillType();
741 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
742 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
748 path.addRect(intersectingRect);
749 path.toggleInverseFillType();
750 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
751 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
757 path.addRect(nonIntersectingRect);
758 path.toggleInverseFillType();
759 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
760 REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
763 // Intersect Op tests with inverse filled circles
766 SkPath path = outsideCircle;
767 path.toggleInverseFillType();
768 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
769 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
774 SkPath path = insideCircle;
775 path.toggleInverseFillType();
776 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
777 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
782 SkPath path = intersectingCircle;
783 path.toggleInverseFillType();
784 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
785 REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
790 SkPath path = nonIntersectingCircle;
791 path.toggleInverseFillType();
792 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
793 REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
797 ///////////////////////////////////////////////////////////////////////////////////////////////////
800 // Functions that add a shape to the clip stack. The shape is computed from a rectangle.
801 // AA is always disabled since the clip stack reducer can cause changes in aa rasterization of the
802 // stack. A fractional edge repeated in different elements may be rasterized fewer times using the
804 typedef void (*AddElementFunc) (const SkRect& rect,
809 static void add_round_rect(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
810 SkScalar rx = rect.width() / 10;
811 SkScalar ry = rect.height() / 20;
814 path.addRoundRect(rect, rx, ry);
815 path.setFillType(SkPath::kInverseWinding_FillType);
816 stack->clipDevPath(path, op, false);
819 rrect.setRectXY(rect, rx, ry);
820 stack->clipDevRRect(rrect, op, false);
824 static void add_rect(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
828 path.setFillType(SkPath::kInverseWinding_FillType);
829 stack->clipDevPath(path, op, false);
831 stack->clipDevRect(rect, op, false);
835 static void add_oval(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
839 path.setFillType(SkPath::kInverseWinding_FillType);
841 stack->clipDevPath(path, op, false);
844 static void add_elem_to_stack(const SkClipStack::Element& element, SkClipStack* stack) {
845 switch (element.getType()) {
846 case SkClipStack::Element::kRect_Type:
847 stack->clipDevRect(element.getRect(), element.getOp(), element.isAA());
849 case SkClipStack::Element::kRRect_Type:
850 stack->clipDevRRect(element.getRRect(), element.getOp(), element.isAA());
852 case SkClipStack::Element::kPath_Type:
853 stack->clipDevPath(element.getPath(), element.getOp(), element.isAA());
855 case SkClipStack::Element::kEmpty_Type:
856 SkDEBUGFAIL("Why did the reducer produce an explicit empty.");
862 static void add_elem_to_region(const SkClipStack::Element& element,
863 const SkIRect& bounds,
866 SkRegion boundsRgn(bounds);
869 switch (element.getType()) {
870 case SkClipStack::Element::kEmpty_Type:
871 elemRegion.setEmpty();
874 element.asPath(&path);
875 elemRegion.setPath(path, boundsRgn);
878 region->op(elemRegion, element.getOp());
881 static void test_reduced_clip_stack(skiatest::Reporter* reporter) {
882 // We construct random clip stacks, reduce them, and then rasterize both versions to verify that
885 // All the clip elements will be contained within these bounds.
886 static const SkRect kBounds = SkRect::MakeWH(100, 100);
890 kMinElemsPerTest = 1,
891 kMaxElemsPerTest = 50,
894 // min/max size of a clip element as a fraction of kBounds.
895 static const SkScalar kMinElemSizeFrac = SK_Scalar1 / 5;
896 static const SkScalar kMaxElemSizeFrac = SK_Scalar1;
898 static const SkRegion::Op kOps[] = {
899 SkRegion::kDifference_Op,
900 SkRegion::kIntersect_Op,
903 SkRegion::kReverseDifference_Op,
904 SkRegion::kReplace_Op,
907 // Replace operations short-circuit the optimizer. We want to make sure that we test this code
908 // path a little bit but we don't want it to prevent us from testing many longer traversals in
910 static const int kReplaceDiv = 4 * kMaxElemsPerTest;
912 // We want to test inverse fills. However, they are quite rare in practice so don't over do it.
913 static const SkScalar kFractionInverted = SK_Scalar1 / kMaxElemsPerTest;
915 static const AddElementFunc kElementFuncs[] = {
923 for (int i = 0; i < kNumTests; ++i) {
924 // Randomly generate a clip stack.
926 int numElems = r.nextRangeU(kMinElemsPerTest, kMaxElemsPerTest);
927 for (int e = 0; e < numElems; ++e) {
928 SkRegion::Op op = kOps[r.nextULessThan(SK_ARRAY_COUNT(kOps))];
929 if (op == SkRegion::kReplace_Op) {
930 if (r.nextU() % kReplaceDiv) {
936 // saves can change the clip stack behavior when an element is added.
937 bool doSave = r.nextBool();
939 SkSize size = SkSize::Make(
940 SkScalarFloorToScalar(SkScalarMul(kBounds.width(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))),
941 SkScalarFloorToScalar(SkScalarMul(kBounds.height(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))));
943 SkPoint xy = {SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth)),
944 SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight))};
946 SkRect rect = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
948 bool invert = r.nextBiasedBool(kFractionInverted);
950 kElementFuncs[r.nextULessThan(SK_ARRAY_COUNT(kElementFuncs))](rect, invert, op, &stack);
956 SkRect inflatedBounds = kBounds;
957 inflatedBounds.outset(kBounds.width() / 2, kBounds.height() / 2);
958 SkIRect inflatedIBounds;
959 inflatedBounds.roundOut(&inflatedIBounds);
961 typedef GrReducedClip::ElementList ElementList;
962 // Get the reduced version of the stack.
963 ElementList reducedClips;
964 int32_t reducedGenID;
965 GrReducedClip::InitialState initial;
966 SkIRect tBounds(inflatedIBounds);
967 SkIRect* tightBounds = r.nextBool() ? &tBounds : NULL;
968 GrReducedClip::ReduceClipStack(stack,
975 REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID != reducedGenID);
977 // Build a new clip stack based on the reduced clip elements
978 SkClipStack reducedStack;
979 if (GrReducedClip::kAllOut_InitialState == initial) {
980 // whether the result is bounded or not, the whole plane should start outside the clip.
981 reducedStack.clipEmpty();
983 for (ElementList::Iter iter = reducedClips.headIter(); iter.get(); iter.next()) {
984 add_elem_to_stack(*iter.get(), &reducedStack);
987 // GrReducedClipStack assumes that the final result is clipped to the returned bounds
989 reducedStack.clipDevRect(*tightBounds, SkRegion::kIntersect_Op);
992 // convert both the original stack and reduced stack to SkRegions and see if they're equal
994 SkRegion reducedRegion;
996 region.setRect(inflatedIBounds);
997 const SkClipStack::Element* element;
998 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
999 while ((element = iter.next())) {
1000 add_elem_to_region(*element, inflatedIBounds, ®ion);
1003 reducedRegion.setRect(inflatedIBounds);
1004 iter.reset(reducedStack, SkClipStack::Iter::kBottom_IterStart);
1005 while ((element = iter.next())) {
1006 add_elem_to_region(*element, inflatedIBounds, &reducedRegion);
1009 testCase.printf("Iteration %d", i);
1010 REPORTER_ASSERT_MESSAGE(reporter, region == reducedRegion, testCase.c_str());
1015 #define SUPPRESS_VISIBILITY_WARNING
1017 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
1020 static void test_reduced_clip_stack_genid(skiatest::Reporter* reporter) {
1023 stack.clipDevRect(SkRect::MakeXYWH(0, 0, 100, 100), SkRegion::kReplace_Op, true);
1024 stack.clipDevRect(SkRect::MakeXYWH(0, 0, SkScalar(50.3), SkScalar(50.3)), SkRegion::kReplace_Op, true);
1025 SkIRect inflatedIBounds = SkIRect::MakeXYWH(0, 0, 100, 100);
1027 GrReducedClip::ElementList reducedClips;
1028 int32_t reducedGenID;
1029 GrReducedClip::InitialState initial;
1030 SkIRect tightBounds;
1032 GrReducedClip::ReduceClipStack(stack,
1039 REPORTER_ASSERT(reporter, reducedClips.count() == 1);
1040 // Clips will be cached based on the generation id. Make sure the gen id is valid.
1041 REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID != reducedGenID);
1046 // Create a clip with following 25.3, 25.3 boxes which are 25 apart:
1050 stack.clipDevRect(SkRect::MakeXYWH(0, 0, SkScalar(25.3), SkScalar(25.3)), SkRegion::kReplace_Op, true);
1051 int32_t genIDA = stack.getTopmostGenID();
1052 stack.clipDevRect(SkRect::MakeXYWH(50, 0, SkScalar(25.3), SkScalar(25.3)), SkRegion::kUnion_Op, true);
1053 int32_t genIDB = stack.getTopmostGenID();
1054 stack.clipDevRect(SkRect::MakeXYWH(0, 50, SkScalar(25.3), SkScalar(25.3)), SkRegion::kUnion_Op, true);
1055 int32_t genIDC = stack.getTopmostGenID();
1056 stack.clipDevRect(SkRect::MakeXYWH(50, 50, SkScalar(25.3), SkScalar(25.3)), SkRegion::kUnion_Op, true);
1057 int32_t genIDD = stack.getTopmostGenID();
1060 #define XYWH SkIRect::MakeXYWH
1064 SkIRect stackBounds = XYWH(0, 0, 76, 76);
1066 // The base test is to test each rect in two ways:
1067 // 1) The box dimensions. (Should reduce to "all in", no elements).
1068 // 2) A bit over the box dimensions.
1069 // In the case 2, test that the generation id is what is expected.
1070 // The rects are of fractional size so that case 2 never gets optimized to an empty element
1073 // Not passing in tighter bounds is tested for consistency.
1074 static const struct SUPPRESS_VISIBILITY_WARNING {
1076 int reducedClipCount;
1077 int32_t reducedGenID;
1078 GrReducedClip::InitialState initialState;
1079 SkIRect tighterBounds; // If this is empty, the query will not pass tighter bounds
1083 { XYWH(0, 0, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, XYWH(0, 0, 25, 25) },
1084 { XYWH(0, 0, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, unused },
1085 { XYWH(0, 0, 27, 27), 1, genIDA, GrReducedClip::kAllOut_InitialState, XYWH(0, 0, 27, 27)},
1086 { XYWH(0, 0, 27, 27), 1, genIDA, GrReducedClip::kAllOut_InitialState, unused },
1089 { XYWH(50, 0, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, XYWH(50, 0, 25, 25) },
1090 { XYWH(50, 0, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, unused },
1091 { XYWH(50, 0, 27, 27), 1, genIDB, GrReducedClip::kAllOut_InitialState, XYWH(50, 0, 26, 27) },
1092 { XYWH(50, 0, 27, 27), 1, genIDB, GrReducedClip::kAllOut_InitialState, unused },
1095 { XYWH(0, 50, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, XYWH(0, 50, 25, 25) },
1096 { XYWH(0, 50, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, unused },
1097 { XYWH(0, 50, 27, 27), 1, genIDC, GrReducedClip::kAllOut_InitialState, XYWH(0, 50, 27, 26) },
1098 { XYWH(0, 50, 27, 27), 1, genIDC, GrReducedClip::kAllOut_InitialState, unused },
1101 { XYWH(50, 50, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, unused },
1102 { XYWH(50, 50, 25, 25), 0, SkClipStack::kWideOpenGenID, GrReducedClip::kAllIn_InitialState, XYWH(50, 50, 25, 25)},
1103 { XYWH(50, 50, 27, 27), 1, genIDD, GrReducedClip::kAllOut_InitialState, unused },
1104 { XYWH(50, 50, 27, 27), 1, genIDD, GrReducedClip::kAllOut_InitialState, XYWH(50, 50, 26, 26)},
1107 { XYWH(0, 0, 100, 100), 4, genIDD, GrReducedClip::kAllOut_InitialState, unused },
1108 { XYWH(0, 0, 100, 100), 4, genIDD, GrReducedClip::kAllOut_InitialState, stackBounds },
1110 // Rect in the middle, touches none.
1111 { XYWH(26, 26, 24, 24), 0, SkClipStack::kEmptyGenID, GrReducedClip::kAllOut_InitialState, unused },
1112 { XYWH(26, 26, 24, 24), 0, SkClipStack::kEmptyGenID, GrReducedClip::kAllOut_InitialState, XYWH(26, 26, 24, 24) },
1114 // Rect in the middle, touches all the rects. GenID is the last rect.
1115 { XYWH(24, 24, 27, 27), 4, genIDD, GrReducedClip::kAllOut_InitialState, unused },
1116 { XYWH(24, 24, 27, 27), 4, genIDD, GrReducedClip::kAllOut_InitialState, XYWH(24, 24, 27, 27) },
1121 for (size_t i = 0; i < SK_ARRAY_COUNT(testCases); ++i) {
1122 GrReducedClip::ElementList reducedClips;
1123 int32_t reducedGenID;
1124 GrReducedClip::InitialState initial;
1125 SkIRect tightBounds;
1127 GrReducedClip::ReduceClipStack(stack,
1128 testCases[i].testBounds,
1132 testCases[i].tighterBounds.isEmpty() ? NULL : &tightBounds);
1134 REPORTER_ASSERT(reporter, reducedClips.count() == testCases[i].reducedClipCount);
1135 SkASSERT(reducedClips.count() == testCases[i].reducedClipCount);
1136 REPORTER_ASSERT(reporter, reducedGenID == testCases[i].reducedGenID);
1137 SkASSERT(reducedGenID == testCases[i].reducedGenID);
1138 REPORTER_ASSERT(reporter, initial == testCases[i].initialState);
1139 SkASSERT(initial == testCases[i].initialState);
1140 if (!testCases[i].tighterBounds.isEmpty()) {
1141 REPORTER_ASSERT(reporter, tightBounds == testCases[i].tighterBounds);
1142 SkASSERT(tightBounds == testCases[i].tighterBounds);
1148 static void test_reduced_clip_stack_no_aa_crash(skiatest::Reporter* reporter) {
1150 stack.clipDevRect(SkIRect::MakeXYWH(0, 0, 100, 100), SkRegion::kReplace_Op);
1151 stack.clipDevRect(SkIRect::MakeXYWH(0, 0, 50, 50), SkRegion::kReplace_Op);
1152 SkIRect inflatedIBounds = SkIRect::MakeXYWH(0, 0, 100, 100);
1154 GrReducedClip::ElementList reducedClips;
1155 int32_t reducedGenID;
1156 GrReducedClip::InitialState initial;
1157 SkIRect tightBounds;
1159 // At the time, this would crash.
1160 GrReducedClip::ReduceClipStack(stack,
1167 REPORTER_ASSERT(reporter, 0 == reducedClips.count());
1172 DEF_TEST(ClipStack, reporter) {
1175 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
1176 assert_count(reporter, stack, 0);
1178 static const SkIRect gRects[] = {
1180 { 25, 25, 125, 125 },
1181 { 0, 0, 1000, 1000 },
1184 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
1185 stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op);
1188 // all of the above rects should have been intersected, leaving only 1 rect
1189 SkClipStack::B2TIter iter(stack);
1190 const SkClipStack::Element* element = iter.next();
1192 answer.iset(25, 25, 75, 75);
1194 REPORTER_ASSERT(reporter, element);
1195 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
1196 REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == element->getOp());
1197 REPORTER_ASSERT(reporter, element->getRect() == answer);
1198 // now check that we only had one in our iterator
1199 REPORTER_ASSERT(reporter, !iter.next());
1202 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
1203 assert_count(reporter, stack, 0);
1205 test_assign_and_comparison(reporter);
1206 test_iterators(reporter);
1207 test_bounds(reporter, SkClipStack::Element::kRect_Type);
1208 test_bounds(reporter, SkClipStack::Element::kRRect_Type);
1209 test_bounds(reporter, SkClipStack::Element::kPath_Type);
1210 test_isWideOpen(reporter);
1211 test_rect_merging(reporter);
1212 test_rect_replace(reporter);
1213 test_rect_inverse_fill(reporter);
1214 test_path_replace(reporter);
1215 test_quickContains(reporter);
1217 test_reduced_clip_stack(reporter);
1218 test_reduced_clip_stack_genid(reporter);
1219 test_reduced_clip_stack_no_aa_crash(reporter);