epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkClipStack.h" |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 10 | #include "SkPath.h" |
| 11 | #include "SkRect.h" |
| 12 | |
| 13 | static void test_assign_and_comparison(skiatest::Reporter* reporter) { |
| 14 | SkClipStack s; |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 15 | bool doAA = false; |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 16 | |
| 17 | // Build up a clip stack with a path, an empty clip, and a rect. |
| 18 | s.save(); |
| 19 | SkPath p; |
| 20 | p.moveTo(5, 6); |
| 21 | p.lineTo(7, 8); |
| 22 | p.lineTo(5, 9); |
| 23 | p.close(); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 24 | s.clipDevPath(p, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 25 | |
| 26 | s.save(); |
| 27 | SkRect r = SkRect::MakeLTRB(1, 2, 3, 4); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 28 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 29 | r = SkRect::MakeLTRB(10, 11, 12, 13); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 30 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 31 | |
| 32 | s.save(); |
| 33 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 34 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 35 | |
| 36 | // Test that assignment works. |
| 37 | SkClipStack copy = s; |
| 38 | REPORTER_ASSERT(reporter, s == copy); |
| 39 | |
| 40 | // Test that different save levels triggers not equal. |
| 41 | s.restore(); |
| 42 | REPORTER_ASSERT(reporter, s != copy); |
| 43 | |
| 44 | // Test that an equal, but not copied version is equal. |
| 45 | s.save(); |
| 46 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 47 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, s == copy); |
| 49 | |
| 50 | // Test that a different op on one level triggers not equal. |
| 51 | s.restore(); |
| 52 | s.save(); |
| 53 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 54 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, s != copy); |
| 56 | |
| 57 | // Test that different state (clip type) triggers not equal. |
tomhudson@google.com | 4c43372 | 2012-03-09 16:48:20 +0000 | [diff] [blame] | 58 | // NO LONGER VALID: if a path contains only a rect, we turn |
| 59 | // it into a bare rect for performance reasons (working |
| 60 | // around Chromium/JavaScript bad pattern). |
| 61 | /* |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 62 | s.restore(); |
| 63 | s.save(); |
| 64 | SkPath rp; |
| 65 | rp.addRect(r); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 66 | s.clipDevPath(rp, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, s != copy); |
tomhudson@google.com | 4c43372 | 2012-03-09 16:48:20 +0000 | [diff] [blame] | 68 | */ |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 69 | |
| 70 | // Test that different rects triggers not equal. |
| 71 | s.restore(); |
| 72 | s.save(); |
| 73 | r = SkRect::MakeLTRB(24, 25, 26, 27); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 74 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, s != copy); |
| 76 | |
| 77 | // Sanity check |
| 78 | s.restore(); |
| 79 | copy.restore(); |
| 80 | REPORTER_ASSERT(reporter, s == copy); |
| 81 | s.restore(); |
| 82 | copy.restore(); |
| 83 | REPORTER_ASSERT(reporter, s == copy); |
| 84 | |
| 85 | // Test that different paths triggers not equal. |
| 86 | s.restore(); |
| 87 | s.save(); |
| 88 | p.addRect(r); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 89 | s.clipDevPath(p, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 90 | REPORTER_ASSERT(reporter, s != copy); |
| 91 | } |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 92 | |
| 93 | static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack, |
| 94 | int count) { |
| 95 | REPORTER_ASSERT(reporter, count == stack.getSaveCount()); |
| 96 | |
| 97 | SkClipStack::B2FIter iter(stack); |
| 98 | int counter = 0; |
| 99 | while (iter.next()) { |
| 100 | counter += 1; |
| 101 | } |
| 102 | REPORTER_ASSERT(reporter, count == counter); |
| 103 | } |
| 104 | |
| 105 | static void TestClipStack(skiatest::Reporter* reporter) { |
| 106 | SkClipStack stack; |
| 107 | |
| 108 | assert_count(reporter, stack, 0); |
| 109 | |
| 110 | static const SkIRect gRects[] = { |
| 111 | { 0, 0, 100, 100 }, |
| 112 | { 25, 25, 125, 125 }, |
| 113 | { 0, 0, 1000, 1000 }, |
| 114 | { 0, 0, 75, 75 } |
| 115 | }; |
| 116 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) { |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 117 | stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // all of the above rects should have been intersected, leaving only 1 rect |
| 121 | SkClipStack::B2FIter iter(stack); |
| 122 | const SkClipStack::B2FIter::Clip* clip = iter.next(); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 123 | SkRect answer; |
| 124 | answer.iset(25, 25, 75, 75); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 125 | |
| 126 | REPORTER_ASSERT(reporter, clip); |
| 127 | REPORTER_ASSERT(reporter, clip->fRect); |
| 128 | REPORTER_ASSERT(reporter, !clip->fPath); |
| 129 | REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp); |
| 130 | REPORTER_ASSERT(reporter, *clip->fRect == answer); |
| 131 | // now check that we only had one in our iterator |
| 132 | REPORTER_ASSERT(reporter, !iter.next()); |
| 133 | |
| 134 | stack.reset(); |
| 135 | assert_count(reporter, stack, 0); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 136 | |
| 137 | test_assign_and_comparison(reporter); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | #include "TestClassDef.h" |
| 141 | DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack) |