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 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 13 | |
| 14 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 15 | static void test_assign_and_comparison(skiatest::Reporter* reporter) { |
| 16 | SkClipStack s; |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 17 | bool doAA = false; |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 19 | REPORTER_ASSERT(reporter, 0 == s.getSaveCount()); |
| 20 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 21 | // Build up a clip stack with a path, an empty clip, and a rect. |
| 22 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 23 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
| 24 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 25 | SkPath p; |
| 26 | p.moveTo(5, 6); |
| 27 | p.lineTo(7, 8); |
| 28 | p.lineTo(5, 9); |
| 29 | p.close(); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 30 | s.clipDevPath(p, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 31 | |
| 32 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 33 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
| 34 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 35 | SkRect r = SkRect::MakeLTRB(1, 2, 3, 4); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 36 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 37 | r = SkRect::MakeLTRB(10, 11, 12, 13); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 38 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 39 | |
| 40 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 41 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 42 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 43 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 44 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 45 | |
| 46 | // Test that assignment works. |
| 47 | SkClipStack copy = s; |
| 48 | REPORTER_ASSERT(reporter, s == copy); |
| 49 | |
| 50 | // Test that different save levels triggers not equal. |
| 51 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 52 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, s != copy); |
| 54 | |
| 55 | // Test that an equal, but not copied version is equal. |
| 56 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 57 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 58 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 59 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 60 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, s == copy); |
| 62 | |
| 63 | // Test that a different op on one level triggers not equal. |
| 64 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 65 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 66 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 67 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 68 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 69 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 70 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, s != copy); |
| 72 | |
| 73 | // Test that different state (clip type) triggers not equal. |
tomhudson@google.com | 4c43372 | 2012-03-09 16:48:20 +0000 | [diff] [blame] | 74 | // NO LONGER VALID: if a path contains only a rect, we turn |
| 75 | // it into a bare rect for performance reasons (working |
| 76 | // around Chromium/JavaScript bad pattern). |
| 77 | /* |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 78 | s.restore(); |
| 79 | s.save(); |
| 80 | SkPath rp; |
| 81 | rp.addRect(r); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 82 | s.clipDevPath(rp, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, s != copy); |
tomhudson@google.com | 4c43372 | 2012-03-09 16:48:20 +0000 | [diff] [blame] | 84 | */ |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 85 | |
| 86 | // Test that different rects triggers not equal. |
| 87 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 88 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 89 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 90 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 91 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 92 | r = SkRect::MakeLTRB(24, 25, 26, 27); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 93 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, s != copy); |
| 95 | |
| 96 | // Sanity check |
| 97 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 98 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
| 99 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 100 | copy.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 101 | REPORTER_ASSERT(reporter, 2 == copy.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, s == copy); |
| 103 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 104 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 105 | copy.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 106 | REPORTER_ASSERT(reporter, 1 == copy.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, s == copy); |
| 108 | |
| 109 | // Test that different paths triggers not equal. |
| 110 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 111 | REPORTER_ASSERT(reporter, 0 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 112 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 113 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
| 114 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 115 | p.addRect(r); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 116 | s.clipDevPath(p, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, s != copy); |
| 118 | } |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 119 | |
| 120 | static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack, |
| 121 | int count) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 122 | SkClipStack::B2TIter iter(stack); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 123 | int counter = 0; |
| 124 | while (iter.next()) { |
| 125 | counter += 1; |
| 126 | } |
| 127 | REPORTER_ASSERT(reporter, count == counter); |
| 128 | } |
| 129 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 130 | static void test_iterators(skiatest::Reporter* reporter) { |
| 131 | SkClipStack stack; |
| 132 | |
| 133 | static const SkRect gRects[] = { |
| 134 | { 0, 0, 40, 40 }, |
| 135 | { 60, 0, 100, 40 }, |
| 136 | { 0, 60, 40, 100 }, |
| 137 | { 60, 60, 100, 100 } |
| 138 | }; |
| 139 | |
| 140 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) { |
| 141 | // the union op will prevent these from being fused together |
| 142 | stack.clipDevRect(gRects[i], SkRegion::kUnion_Op, false); |
| 143 | } |
| 144 | |
| 145 | assert_count(reporter, stack, 4); |
| 146 | |
| 147 | // bottom to top iteration |
| 148 | { |
| 149 | const SkClipStack::B2TIter::Clip* clip = NULL; |
| 150 | |
| 151 | SkClipStack::B2TIter iter(stack); |
| 152 | int i; |
| 153 | |
| 154 | for (i = 0, clip = iter.next(); clip; ++i, clip = iter.next()) { |
| 155 | REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]); |
| 156 | } |
| 157 | |
| 158 | SkASSERT(i == 4); |
| 159 | } |
| 160 | |
| 161 | // top to bottom iteration |
| 162 | { |
| 163 | const SkClipStack::Iter::Clip* clip = NULL; |
| 164 | |
| 165 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 166 | int i; |
| 167 | |
| 168 | for (i = 3, clip = iter.prev(); clip; --i, clip = iter.prev()) { |
| 169 | REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]); |
| 170 | } |
| 171 | |
| 172 | SkASSERT(i == -1); |
| 173 | } |
| 174 | |
| 175 | // skipToTopmost |
| 176 | { |
| 177 | const SkClipStack::Iter::Clip*clip = NULL; |
| 178 | |
| 179 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 180 | |
| 181 | clip = iter.skipToTopmost(SkRegion::kUnion_Op); |
| 182 | REPORTER_ASSERT(reporter, *clip->fRect == gRects[3]); |
| 183 | } |
| 184 | } |
| 185 | |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 186 | static void TestClipStack(skiatest::Reporter* reporter) { |
| 187 | SkClipStack stack; |
| 188 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 189 | REPORTER_ASSERT(reporter, 0 == stack.getSaveCount()); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 190 | assert_count(reporter, stack, 0); |
| 191 | |
| 192 | static const SkIRect gRects[] = { |
| 193 | { 0, 0, 100, 100 }, |
| 194 | { 25, 25, 125, 125 }, |
| 195 | { 0, 0, 1000, 1000 }, |
| 196 | { 0, 0, 75, 75 } |
| 197 | }; |
| 198 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) { |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 199 | stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // all of the above rects should have been intersected, leaving only 1 rect |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 203 | SkClipStack::B2TIter iter(stack); |
| 204 | const SkClipStack::B2TIter::Clip* clip = iter.next(); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 205 | SkRect answer; |
| 206 | answer.iset(25, 25, 75, 75); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 207 | |
| 208 | REPORTER_ASSERT(reporter, clip); |
| 209 | REPORTER_ASSERT(reporter, clip->fRect); |
| 210 | REPORTER_ASSERT(reporter, !clip->fPath); |
| 211 | REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp); |
| 212 | REPORTER_ASSERT(reporter, *clip->fRect == answer); |
| 213 | // now check that we only had one in our iterator |
| 214 | REPORTER_ASSERT(reporter, !iter.next()); |
| 215 | |
| 216 | stack.reset(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 217 | REPORTER_ASSERT(reporter, 0 == stack.getSaveCount()); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 218 | assert_count(reporter, stack, 0); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 219 | |
| 220 | test_assign_and_comparison(reporter); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame^] | 221 | test_iterators(reporter); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | #include "TestClassDef.h" |
| 225 | DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack) |