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" |
bsalomon@google.com | a4e13c8 | 2012-11-26 21:38:37 +0000 | [diff] [blame^] | 9 | #if SK_SUPPORT_GPU |
| 10 | #include "GrClipMaskManager.h" |
| 11 | #endif |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 12 | #include "SkClipStack.h" |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 16 | #include "SkRegion.h" |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 17 | |
| 18 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 19 | static void test_assign_and_comparison(skiatest::Reporter* reporter) { |
| 20 | SkClipStack s; |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 21 | bool doAA = false; |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 22 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 23 | REPORTER_ASSERT(reporter, 0 == s.getSaveCount()); |
| 24 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 25 | // Build up a clip stack with a path, an empty clip, and a rect. |
| 26 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
| 28 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 29 | SkPath p; |
| 30 | p.moveTo(5, 6); |
| 31 | p.lineTo(7, 8); |
| 32 | p.lineTo(5, 9); |
| 33 | p.close(); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 34 | s.clipDevPath(p, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 35 | |
| 36 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
| 38 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 39 | SkRect r = SkRect::MakeLTRB(1, 2, 3, 4); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 40 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 41 | r = SkRect::MakeLTRB(10, 11, 12, 13); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 42 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 43 | |
| 44 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 46 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 47 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 48 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 49 | |
| 50 | // Test that assignment works. |
| 51 | SkClipStack copy = s; |
| 52 | REPORTER_ASSERT(reporter, s == copy); |
| 53 | |
| 54 | // Test that different save levels triggers not equal. |
| 55 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 56 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, s != copy); |
| 58 | |
| 59 | // Test that an equal, but not copied version is equal. |
| 60 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 62 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 63 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 64 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, s == copy); |
| 66 | |
| 67 | // Test that a different op on one level triggers not equal. |
| 68 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 70 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 72 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 73 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 74 | s.clipDevRect(r, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, s != copy); |
| 76 | |
| 77 | // Test that different state (clip type) triggers not equal. |
tomhudson@google.com | 4c43372 | 2012-03-09 16:48:20 +0000 | [diff] [blame] | 78 | // NO LONGER VALID: if a path contains only a rect, we turn |
| 79 | // it into a bare rect for performance reasons (working |
| 80 | // around Chromium/JavaScript bad pattern). |
| 81 | /* |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 82 | s.restore(); |
| 83 | s.save(); |
| 84 | SkPath rp; |
| 85 | rp.addRect(r); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 86 | s.clipDevPath(rp, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, s != copy); |
tomhudson@google.com | 4c43372 | 2012-03-09 16:48:20 +0000 | [diff] [blame] | 88 | */ |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 89 | |
| 90 | // Test that different rects triggers not equal. |
| 91 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 93 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 95 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 96 | r = SkRect::MakeLTRB(24, 25, 26, 27); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 97 | s.clipDevRect(r, SkRegion::kUnion_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, s != copy); |
| 99 | |
| 100 | // Sanity check |
| 101 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
| 103 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 104 | copy.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, 2 == copy.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, s == copy); |
| 107 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 109 | copy.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, 1 == copy.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, s == copy); |
| 112 | |
| 113 | // Test that different paths triggers not equal. |
| 114 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 115 | REPORTER_ASSERT(reporter, 0 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 116 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
| 118 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 119 | p.addRect(r); |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 120 | s.clipDevPath(p, SkRegion::kIntersect_Op, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, s != copy); |
| 122 | } |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 123 | |
| 124 | static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack, |
| 125 | int count) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 126 | SkClipStack::B2TIter iter(stack); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 127 | int counter = 0; |
| 128 | while (iter.next()) { |
| 129 | counter += 1; |
| 130 | } |
| 131 | REPORTER_ASSERT(reporter, count == counter); |
| 132 | } |
| 133 | |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 134 | // Exercise the SkClipStack's bottom to top and bidirectional iterators |
| 135 | // (including the skipToTopmost functionality) |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 136 | static void test_iterators(skiatest::Reporter* reporter) { |
| 137 | SkClipStack stack; |
| 138 | |
| 139 | static const SkRect gRects[] = { |
| 140 | { 0, 0, 40, 40 }, |
| 141 | { 60, 0, 100, 40 }, |
| 142 | { 0, 60, 40, 100 }, |
| 143 | { 60, 60, 100, 100 } |
| 144 | }; |
| 145 | |
| 146 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) { |
| 147 | // the union op will prevent these from being fused together |
| 148 | stack.clipDevRect(gRects[i], SkRegion::kUnion_Op, false); |
| 149 | } |
| 150 | |
| 151 | assert_count(reporter, stack, 4); |
| 152 | |
| 153 | // bottom to top iteration |
| 154 | { |
| 155 | const SkClipStack::B2TIter::Clip* clip = NULL; |
| 156 | |
| 157 | SkClipStack::B2TIter iter(stack); |
| 158 | int i; |
| 159 | |
| 160 | for (i = 0, clip = iter.next(); clip; ++i, clip = iter.next()) { |
| 161 | REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]); |
| 162 | } |
| 163 | |
| 164 | SkASSERT(i == 4); |
| 165 | } |
| 166 | |
| 167 | // top to bottom iteration |
| 168 | { |
| 169 | const SkClipStack::Iter::Clip* clip = NULL; |
| 170 | |
| 171 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 172 | int i; |
| 173 | |
| 174 | for (i = 3, clip = iter.prev(); clip; --i, clip = iter.prev()) { |
| 175 | REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]); |
| 176 | } |
| 177 | |
| 178 | SkASSERT(i == -1); |
| 179 | } |
| 180 | |
| 181 | // skipToTopmost |
| 182 | { |
| 183 | const SkClipStack::Iter::Clip*clip = NULL; |
| 184 | |
| 185 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 186 | |
| 187 | clip = iter.skipToTopmost(SkRegion::kUnion_Op); |
| 188 | REPORTER_ASSERT(reporter, *clip->fRect == gRects[3]); |
| 189 | } |
| 190 | } |
| 191 | |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 192 | // Exercise the SkClipStack's getConservativeBounds computation |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 193 | static void test_bounds(skiatest::Reporter* reporter, bool useRects) { |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 194 | |
| 195 | static const int gNumCases = 20; |
| 196 | static const SkRect gAnswerRectsBW[gNumCases] = { |
| 197 | // A op B |
| 198 | { 40, 40, 50, 50 }, |
| 199 | { 10, 10, 50, 50 }, |
| 200 | { 10, 10, 80, 80 }, |
| 201 | { 10, 10, 80, 80 }, |
| 202 | { 40, 40, 80, 80 }, |
| 203 | |
| 204 | // invA op B |
| 205 | { 40, 40, 80, 80 }, |
| 206 | { 0, 0, 100, 100 }, |
| 207 | { 0, 0, 100, 100 }, |
| 208 | { 0, 0, 100, 100 }, |
| 209 | { 40, 40, 50, 50 }, |
| 210 | |
| 211 | // A op invB |
| 212 | { 10, 10, 50, 50 }, |
| 213 | { 40, 40, 50, 50 }, |
| 214 | { 0, 0, 100, 100 }, |
| 215 | { 0, 0, 100, 100 }, |
| 216 | { 0, 0, 100, 100 }, |
| 217 | |
| 218 | // invA op invB |
| 219 | { 0, 0, 100, 100 }, |
| 220 | { 40, 40, 80, 80 }, |
| 221 | { 0, 0, 100, 100 }, |
| 222 | { 10, 10, 80, 80 }, |
| 223 | { 10, 10, 50, 50 }, |
| 224 | }; |
| 225 | |
| 226 | static const SkRegion::Op gOps[] = { |
| 227 | SkRegion::kIntersect_Op, |
| 228 | SkRegion::kDifference_Op, |
| 229 | SkRegion::kUnion_Op, |
| 230 | SkRegion::kXOR_Op, |
| 231 | SkRegion::kReverseDifference_Op |
| 232 | }; |
| 233 | |
| 234 | SkRect rectA, rectB; |
| 235 | |
| 236 | rectA.iset(10, 10, 50, 50); |
| 237 | rectB.iset(40, 40, 80, 80); |
| 238 | |
| 239 | SkPath clipA, clipB; |
| 240 | |
| 241 | clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5)); |
| 242 | clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5)); |
| 243 | |
| 244 | SkClipStack stack; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 245 | SkRect devClipBound; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 246 | bool isIntersectionOfRects = false; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 247 | |
| 248 | int testCase = 0; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 249 | int numBitTests = useRects ? 1 : 4; |
| 250 | for (int invBits = 0; invBits < numBitTests; ++invBits) { |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 251 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
| 252 | |
| 253 | stack.save(); |
| 254 | bool doInvA = SkToBool(invBits & 1); |
| 255 | bool doInvB = SkToBool(invBits & 2); |
| 256 | |
| 257 | clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType : |
| 258 | SkPath::kEvenOdd_FillType); |
| 259 | clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType : |
| 260 | SkPath::kEvenOdd_FillType); |
| 261 | |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 262 | if (useRects) { |
| 263 | stack.clipDevRect(rectA, SkRegion::kIntersect_Op, false); |
| 264 | stack.clipDevRect(rectB, gOps[op], false); |
| 265 | } else { |
| 266 | stack.clipDevPath(clipA, SkRegion::kIntersect_Op, false); |
| 267 | stack.clipDevPath(clipB, gOps[op], false); |
| 268 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 269 | |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 270 | REPORTER_ASSERT(reporter, !stack.isWideOpen()); |
| 271 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 272 | stack.getConservativeBounds(0, 0, 100, 100, &devClipBound, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 273 | &isIntersectionOfRects); |
| 274 | |
| 275 | if (useRects) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 276 | REPORTER_ASSERT(reporter, isIntersectionOfRects == |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 277 | (gOps[op] == SkRegion::kIntersect_Op)); |
| 278 | } else { |
| 279 | REPORTER_ASSERT(reporter, !isIntersectionOfRects); |
| 280 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 281 | |
| 282 | SkASSERT(testCase < gNumCases); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 283 | REPORTER_ASSERT(reporter, devClipBound == gAnswerRectsBW[testCase]); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 284 | ++testCase; |
| 285 | |
| 286 | stack.restore(); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 291 | // Test out 'isWideOpen' entry point |
| 292 | static void test_isWideOpen(skiatest::Reporter* reporter) { |
| 293 | |
| 294 | SkRect rectA, rectB; |
| 295 | |
| 296 | rectA.iset(10, 10, 40, 40); |
| 297 | rectB.iset(50, 50, 80, 80); |
| 298 | |
| 299 | // Stack should initially be wide open |
| 300 | { |
| 301 | SkClipStack stack; |
| 302 | |
| 303 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
| 304 | } |
| 305 | |
| 306 | // Test out case where the user specifies a union that includes everything |
| 307 | { |
| 308 | SkClipStack stack; |
| 309 | |
| 310 | SkPath clipA, clipB; |
| 311 | |
| 312 | clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5)); |
| 313 | clipA.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 314 | |
| 315 | clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5)); |
| 316 | clipB.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 317 | |
| 318 | stack.clipDevPath(clipA, SkRegion::kReplace_Op, false); |
| 319 | stack.clipDevPath(clipB, SkRegion::kUnion_Op, false); |
| 320 | |
| 321 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
| 322 | } |
| 323 | |
| 324 | // Test out union w/ a wide open clip |
| 325 | { |
| 326 | SkClipStack stack; |
| 327 | |
| 328 | stack.clipDevRect(rectA, SkRegion::kUnion_Op, false); |
| 329 | |
| 330 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
| 331 | } |
| 332 | |
| 333 | // Test out empty difference from a wide open clip |
| 334 | { |
| 335 | SkClipStack stack; |
| 336 | |
| 337 | SkRect emptyRect; |
| 338 | emptyRect.setEmpty(); |
| 339 | |
| 340 | stack.clipDevRect(emptyRect, SkRegion::kDifference_Op, false); |
| 341 | |
| 342 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
| 343 | } |
| 344 | |
| 345 | // Test out return to wide open |
| 346 | { |
| 347 | SkClipStack stack; |
| 348 | |
| 349 | stack.save(); |
| 350 | |
| 351 | stack.clipDevRect(rectA, SkRegion::kReplace_Op, false); |
| 352 | |
| 353 | REPORTER_ASSERT(reporter, !stack.isWideOpen()); |
| 354 | |
| 355 | stack.restore(); |
| 356 | |
| 357 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
| 358 | } |
| 359 | } |
| 360 | |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 361 | static int count(const SkClipStack& stack) { |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 362 | |
| 363 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 364 | |
| 365 | const SkClipStack::Iter::Clip* clip = NULL; |
| 366 | int count = 0; |
| 367 | |
| 368 | for (clip = iter.prev(); clip; clip = iter.prev(), ++count) { |
| 369 | ; |
| 370 | } |
| 371 | |
| 372 | return count; |
| 373 | } |
| 374 | |
| 375 | // Test out SkClipStack's merging of rect clips. In particular exercise |
| 376 | // merging of aa vs. bw rects. |
| 377 | static void test_rect_merging(skiatest::Reporter* reporter) { |
| 378 | |
| 379 | SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50); |
| 380 | SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80); |
| 381 | |
| 382 | SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90); |
| 383 | SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60); |
| 384 | |
| 385 | SkRect bound; |
| 386 | SkClipStack::BoundsType type; |
| 387 | bool isIntersectionOfRects; |
| 388 | |
| 389 | // all bw overlapping - should merge |
| 390 | { |
| 391 | SkClipStack stack; |
| 392 | |
| 393 | stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, false); |
| 394 | |
| 395 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); |
| 396 | |
| 397 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 398 | |
| 399 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 400 | |
| 401 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 402 | } |
| 403 | |
| 404 | // all aa overlapping - should merge |
| 405 | { |
| 406 | SkClipStack stack; |
| 407 | |
| 408 | stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true); |
| 409 | |
| 410 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true); |
| 411 | |
| 412 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 413 | |
| 414 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 415 | |
| 416 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 417 | } |
| 418 | |
| 419 | // mixed overlapping - should _not_ merge |
| 420 | { |
| 421 | SkClipStack stack; |
| 422 | |
| 423 | stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true); |
| 424 | |
| 425 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); |
| 426 | |
| 427 | REPORTER_ASSERT(reporter, 2 == count(stack)); |
| 428 | |
| 429 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 430 | |
| 431 | REPORTER_ASSERT(reporter, !isIntersectionOfRects); |
| 432 | } |
| 433 | |
| 434 | // mixed nested (bw inside aa) - should merge |
| 435 | { |
| 436 | SkClipStack stack; |
| 437 | |
| 438 | stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, true); |
| 439 | |
| 440 | stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, false); |
| 441 | |
| 442 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 443 | |
| 444 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 445 | |
| 446 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 447 | } |
| 448 | |
| 449 | // mixed nested (aa inside bw) - should merge |
| 450 | { |
| 451 | SkClipStack stack; |
| 452 | |
| 453 | stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, false); |
| 454 | |
| 455 | stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, true); |
| 456 | |
| 457 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 458 | |
| 459 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 460 | |
| 461 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 462 | } |
| 463 | |
| 464 | // reverse nested (aa inside bw) - should _not_ merge |
| 465 | { |
| 466 | SkClipStack stack; |
| 467 | |
| 468 | stack.clipDevRect(nestedChild, SkRegion::kReplace_Op, false); |
| 469 | |
| 470 | stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, true); |
| 471 | |
| 472 | REPORTER_ASSERT(reporter, 2 == count(stack)); |
| 473 | |
| 474 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 475 | |
| 476 | REPORTER_ASSERT(reporter, !isIntersectionOfRects); |
| 477 | } |
| 478 | } |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 479 | |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame] | 480 | |
| 481 | // This is similar to the above test but tests the iterator's ability to merge rects in the |
| 482 | // middle of a clip stack's sequence using nextCombined(). There is a save after every clip |
| 483 | // element to prevent the clip stack from merging the rectangles as they are added. |
| 484 | static void test_iter_rect_merging(skiatest::Reporter* reporter) { |
| 485 | |
| 486 | SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50); |
| 487 | SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80); |
| 488 | |
| 489 | SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90); |
| 490 | SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60); |
| 491 | |
| 492 | SkRect farAway = SkRect::MakeLTRB(1000, 1000, 1010, 1010); |
| 493 | |
| 494 | SkRect overlapIntersect; |
| 495 | overlapIntersect.intersect(overlapLeft, overlapRight); |
| 496 | |
| 497 | SkPath path1, path2; |
| 498 | path1.addCircle(SkIntToScalar(30), SkIntToScalar(30), SkIntToScalar(1000)); |
| 499 | path2.addOval(SkRect::MakeWH(500, 600)); |
| 500 | |
| 501 | const SkClipStack::Iter::Clip* clip; |
| 502 | |
| 503 | // call nextCombined with an empty clip stack |
| 504 | { |
| 505 | SkClipStack stack; |
| 506 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 507 | REPORTER_ASSERT(reporter, NULL == iter.nextCombined()); |
| 508 | } |
| 509 | |
| 510 | // two bw overlapping - should merge, bracketed by paths |
| 511 | { |
| 512 | SkClipStack stack; |
| 513 | stack.clipDevPath(path1, SkRegion::kIntersect_Op, false); stack.save(); |
| 514 | |
| 515 | stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, false); stack.save(); |
| 516 | |
| 517 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); stack.save(); |
| 518 | |
| 519 | stack.clipDevPath(path2, SkRegion::kIntersect_Op, false); stack.save(); |
| 520 | |
| 521 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 522 | |
| 523 | clip = iter.nextCombined(); |
| 524 | REPORTER_ASSERT(reporter, *clip->fPath == path1 && !clip->fDoAA); |
| 525 | |
| 526 | clip = iter.nextCombined(); |
| 527 | REPORTER_ASSERT(reporter, !clip->fDoAA && *clip->fRect == overlapIntersect); |
| 528 | |
| 529 | clip = iter.nextCombined(); |
| 530 | REPORTER_ASSERT(reporter, *clip->fPath == path2 && !clip->fDoAA); |
| 531 | |
| 532 | clip = iter.nextCombined(); |
| 533 | REPORTER_ASSERT(reporter, NULL == clip); |
| 534 | } |
| 535 | |
| 536 | // same as above but rects are aa and no final path. |
| 537 | { |
| 538 | SkClipStack stack; |
| 539 | stack.clipDevPath(path1, SkRegion::kIntersect_Op, false); stack.save(); |
| 540 | |
| 541 | stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, true); stack.save(); |
| 542 | |
| 543 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true); stack.save(); |
| 544 | |
| 545 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 546 | |
| 547 | clip = iter.nextCombined(); |
| 548 | REPORTER_ASSERT(reporter, *clip->fPath == path1 && !clip->fDoAA); |
| 549 | |
| 550 | clip = iter.nextCombined(); |
| 551 | REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == overlapIntersect); |
| 552 | |
| 553 | clip = iter.nextCombined(); |
| 554 | REPORTER_ASSERT(reporter, NULL == clip); |
| 555 | } |
| 556 | |
| 557 | // mixed overlapping - no paths - should _not_ merge |
| 558 | { |
| 559 | SkClipStack stack; |
| 560 | |
| 561 | stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, true); stack.save(); |
| 562 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); stack.save(); |
| 563 | |
| 564 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 565 | |
| 566 | clip = iter.nextCombined(); |
| 567 | REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == overlapLeft); |
| 568 | |
| 569 | clip = iter.nextCombined(); |
| 570 | REPORTER_ASSERT(reporter, !clip->fDoAA && *clip->fRect == overlapRight); |
| 571 | |
| 572 | clip = iter.nextCombined(); |
| 573 | REPORTER_ASSERT(reporter, NULL == clip); |
| 574 | } |
| 575 | |
| 576 | // three rects in a row where the third rect uses a non-intersect op. |
| 577 | { |
| 578 | SkClipStack stack; |
| 579 | |
| 580 | stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, true); stack.save(); |
| 581 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true); stack.save(); |
| 582 | stack.clipDevRect(nestedParent, SkRegion::kXOR_Op, true); stack.save(); |
| 583 | |
| 584 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 585 | |
| 586 | clip = iter.nextCombined(); |
| 587 | REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == overlapIntersect); |
| 588 | clip = iter.nextCombined(); |
| 589 | REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == nestedParent); |
| 590 | clip = iter.nextCombined(); |
| 591 | REPORTER_ASSERT(reporter, NULL == clip); |
| 592 | } |
| 593 | |
| 594 | // mixed nested (bw inside aa) - should merge |
| 595 | { |
| 596 | SkClipStack stack; |
| 597 | stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, false); stack.save(); |
| 598 | |
| 599 | stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, true); stack.save(); |
| 600 | |
| 601 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
skia.committer@gmail.com | 72b2e6f | 2012-11-08 02:03:56 +0000 | [diff] [blame] | 602 | |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame] | 603 | clip = iter.nextCombined(); |
| 604 | REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == nestedChild); |
| 605 | |
| 606 | clip = iter.nextCombined(); |
| 607 | REPORTER_ASSERT(reporter, NULL == clip); |
| 608 | } |
| 609 | |
| 610 | // mixed nested (aa inside bw) - should merge |
| 611 | { |
| 612 | SkClipStack stack; |
| 613 | stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, false); stack.save(); |
| 614 | |
| 615 | stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, true); stack.save(); |
| 616 | |
| 617 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
skia.committer@gmail.com | 72b2e6f | 2012-11-08 02:03:56 +0000 | [diff] [blame] | 618 | |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame] | 619 | clip = iter.nextCombined(); |
| 620 | REPORTER_ASSERT(reporter, !clip->fDoAA && *clip->fRect == nestedChild); |
| 621 | |
| 622 | clip = iter.nextCombined(); |
| 623 | REPORTER_ASSERT(reporter, NULL == clip); |
| 624 | } |
| 625 | |
| 626 | // three rect intersects in a row where result is empty after the second. |
| 627 | { |
| 628 | SkClipStack stack; |
| 629 | |
| 630 | stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, false); stack.save(); |
| 631 | stack.clipDevRect(farAway, SkRegion::kIntersect_Op, false); stack.save(); |
| 632 | stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); stack.save(); |
| 633 | |
| 634 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 635 | |
| 636 | clip = iter.nextCombined(); |
| 637 | REPORTER_ASSERT(reporter, clip->fRect->isEmpty()); |
| 638 | |
| 639 | clip = iter.nextCombined(); |
| 640 | REPORTER_ASSERT(reporter, *clip->fRect == overlapRight); |
| 641 | |
| 642 | clip = iter.nextCombined(); |
| 643 | REPORTER_ASSERT(reporter, NULL == clip); |
| 644 | } |
| 645 | } |
| 646 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 647 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 648 | |
bsalomon@google.com | a4e13c8 | 2012-11-26 21:38:37 +0000 | [diff] [blame^] | 649 | #if SK_SUPPORT_GPU |
| 650 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 651 | typedef void (*AddElementFunc) (const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack); |
| 652 | |
| 653 | static void add_round_rect(const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack) { |
| 654 | SkPath path; |
| 655 | SkScalar rx = rect.width() / 10; |
| 656 | SkScalar ry = rect.width() / 20; |
| 657 | path.addRoundRect(rect, rx, ry); |
| 658 | stack->clipDevPath(path, op, aa); |
| 659 | }; |
| 660 | |
| 661 | static void add_rect(const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack) { |
| 662 | stack->clipDevRect(rect, op, aa); |
| 663 | }; |
| 664 | |
| 665 | static void add_oval(const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack) { |
| 666 | SkPath path; |
| 667 | path.addOval(rect); |
| 668 | stack->clipDevPath(path, op, aa); |
| 669 | }; |
| 670 | |
| 671 | static void add_elem_to_stack(const SkClipStack::Iter::Clip& clip, SkClipStack* stack) { |
| 672 | if (NULL != clip.fPath) { |
| 673 | stack->clipDevPath(*clip.fPath, clip.fOp, clip.fDoAA); |
| 674 | } else if (NULL != clip.fRect) { |
| 675 | stack->clipDevRect(*clip.fRect, clip.fOp, clip.fDoAA); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static void add_elem_to_region(const SkClipStack::Iter::Clip& clip, |
| 680 | const SkIRect& bounds, |
| 681 | SkRegion* region) { |
| 682 | SkRegion elemRegion; |
| 683 | SkRegion boundsRgn(bounds); |
| 684 | |
| 685 | if (NULL != clip.fPath) { |
| 686 | elemRegion.setPath(*clip.fPath, boundsRgn); |
| 687 | } else if (NULL != clip.fRect) { |
| 688 | SkPath path; |
| 689 | path.addRect(*clip.fRect); |
| 690 | elemRegion.setPath(path, boundsRgn); |
| 691 | } else { |
| 692 | // TODO: Figure out why we sometimes get here in the reduced clip stack. |
| 693 | region->setEmpty(); |
| 694 | return; |
| 695 | } |
| 696 | region->op(elemRegion, clip.fOp); |
| 697 | } |
| 698 | |
| 699 | // This can assist with debugging the clip stack reduction code when the test below fails. |
| 700 | static void print_clip(const SkClipStack::Iter::Clip& clip) { |
| 701 | static const char* kOpStrs[] = { |
| 702 | "DF", |
| 703 | "IS", |
| 704 | "UN", |
| 705 | "XR", |
| 706 | "RD", |
| 707 | "RP", |
| 708 | }; |
| 709 | if (clip.fRect || clip.fPath) { |
| 710 | const SkRect& bounds = clip.getBounds(); |
| 711 | SkDebugf("%s %s [%f %f] x [%f %f]\n", |
| 712 | kOpStrs[clip.fOp], |
| 713 | (clip.fRect ? "R" : "P"), |
| 714 | bounds.fLeft, bounds.fRight, bounds.fTop, bounds.fBottom); |
| 715 | } else { |
| 716 | SkDebugf("EM\n"); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | static void test_reduced_clip_stack(skiatest::Reporter* reporter) { |
| 721 | // We construct random clip stacks, reduce them, and then rasterize both versions to verify that |
| 722 | // they are equal. |
| 723 | |
| 724 | // All the clip elements will be contained within these bounds. |
| 725 | static const SkRect kBounds = SkRect::MakeWH(100, 100); |
| 726 | |
| 727 | enum { |
| 728 | kNumTests = 200, |
| 729 | kMinElemsPerTest = 1, |
| 730 | kMaxElemsPerTest = 50, |
| 731 | }; |
| 732 | |
| 733 | // min/max size of a clip element as a fraction of kBounds. |
| 734 | static const SkScalar kMinElemSizeFrac = SK_Scalar1 / 5; |
| 735 | static const SkScalar kMaxElemSizeFrac = SK_Scalar1; |
| 736 | |
| 737 | static const SkRegion::Op kOps[] = { |
| 738 | SkRegion::kDifference_Op, |
| 739 | SkRegion::kIntersect_Op, |
| 740 | SkRegion::kUnion_Op, |
| 741 | SkRegion::kXOR_Op, |
| 742 | SkRegion::kReverseDifference_Op, |
| 743 | SkRegion::kReplace_Op, |
| 744 | }; |
| 745 | |
| 746 | // Replace operations short-circuit the optimizer. We want to make sure that we test this code |
| 747 | // path a little bit but we don't want it to prevent us from testing many longer traversals in |
| 748 | // the optimizer. |
| 749 | static const int kReplaceDiv = 4 * kMaxElemsPerTest; |
| 750 | |
| 751 | static const AddElementFunc kElementFuncs[] = { |
| 752 | add_rect, |
| 753 | add_round_rect, |
| 754 | add_oval, |
| 755 | }; |
| 756 | |
| 757 | SkRandom r; |
| 758 | |
| 759 | for (int i = 0; i < kNumTests; ++i) { |
| 760 | // Randomly generate a clip stack. |
| 761 | SkClipStack stack; |
| 762 | int numElems = r.nextRangeU(kMinElemsPerTest, kMaxElemsPerTest); |
| 763 | for (int e = 0; e < numElems; ++e) { |
| 764 | SkRegion::Op op = kOps[r.nextULessThan(SK_ARRAY_COUNT(kOps))]; |
| 765 | if (op == SkRegion::kReplace_Op) { |
| 766 | if (r.nextU() % kReplaceDiv) { |
| 767 | --e; |
| 768 | continue; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // saves can change the clip stack behavior when an element is added. |
| 773 | bool doSave = r.nextBool(); |
| 774 | |
| 775 | SkSize size = SkSize::Make( |
| 776 | SkScalarFloorToScalar(SkScalarMul(kBounds.width(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))), |
| 777 | SkScalarFloorToScalar(SkScalarMul(kBounds.height(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac)))); |
| 778 | |
| 779 | SkPoint xy = {SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth)), |
| 780 | SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight))}; |
| 781 | |
| 782 | SkRect rect = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight); |
| 783 | |
| 784 | // AA is always disabled. The optimizer can cause changes in aa rasterization of the |
| 785 | // clip stack. A fractional edge repeated in different elements may be rasterized fewer |
| 786 | // times using the reduced stack. |
| 787 | kElementFuncs[r.nextULessThan(SK_ARRAY_COUNT(kElementFuncs))](rect, false, op, &stack); |
| 788 | if (doSave) { |
| 789 | stack.save(); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | // Get the reduced version of the stack. |
| 794 | SkTDArray<SkClipStack::Iter::Clip> reducedClips; |
| 795 | SkRect resultBounds; |
| 796 | bool bounded; |
| 797 | GrReducedClip::InitialState initial; |
| 798 | GrReducedClip::GrReduceClipStack(stack, &reducedClips, &resultBounds, &bounded, &initial); |
| 799 | |
| 800 | // Build a new clip stack based on the reduced clip elements |
| 801 | SkClipStack reducedStack; |
| 802 | if (GrReducedClip::kAllOut_InitialState == initial) { |
| 803 | // whether the result is bounded or not, the whole plane should start outside the clip. |
| 804 | reducedStack.clipEmpty(); |
| 805 | } |
| 806 | for (int c = 0; c < reducedClips.count(); ++c) { |
| 807 | add_elem_to_stack(reducedClips[c], &reducedStack); |
| 808 | } |
| 809 | if (bounded) { |
| 810 | // GrReduceClipStack() assumes that there is an implicit clip to the bounds |
| 811 | reducedStack.clipDevRect(resultBounds, SkRegion::kIntersect_Op, true); |
| 812 | } |
| 813 | |
| 814 | // convert both the original stack and reduced stack to SkRegions and see if they're equal |
| 815 | SkRect inflatedBounds = kBounds; |
| 816 | inflatedBounds.outset(kBounds.width() / 2, kBounds.height() / 2); |
| 817 | SkIRect inflatedIBounds; |
| 818 | inflatedBounds.roundOut(&inflatedIBounds); |
| 819 | |
| 820 | SkRegion region; |
| 821 | SkRegion reducedRegion; |
| 822 | |
| 823 | region.setRect(inflatedIBounds); |
| 824 | const SkClipStack::Iter::Clip* clip; |
| 825 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 826 | while ((clip = iter.next())) { |
| 827 | add_elem_to_region(*clip, inflatedIBounds, ®ion); |
| 828 | } |
| 829 | |
| 830 | reducedRegion.setRect(inflatedIBounds); |
| 831 | iter.reset(reducedStack, SkClipStack::Iter::kBottom_IterStart); |
| 832 | while ((clip = iter.next())) { |
| 833 | add_elem_to_region(*clip, inflatedIBounds, &reducedRegion); |
| 834 | } |
| 835 | |
| 836 | REPORTER_ASSERT(reporter, region == reducedRegion); |
| 837 | } |
| 838 | } |
| 839 | |
bsalomon@google.com | a4e13c8 | 2012-11-26 21:38:37 +0000 | [diff] [blame^] | 840 | #endif |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 841 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 842 | |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 843 | static void TestClipStack(skiatest::Reporter* reporter) { |
| 844 | SkClipStack stack; |
| 845 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 846 | REPORTER_ASSERT(reporter, 0 == stack.getSaveCount()); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 847 | assert_count(reporter, stack, 0); |
| 848 | |
| 849 | static const SkIRect gRects[] = { |
| 850 | { 0, 0, 100, 100 }, |
| 851 | { 25, 25, 125, 125 }, |
| 852 | { 0, 0, 1000, 1000 }, |
| 853 | { 0, 0, 75, 75 } |
| 854 | }; |
| 855 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) { |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 856 | stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | // 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] | 860 | SkClipStack::B2TIter iter(stack); |
| 861 | const SkClipStack::B2TIter::Clip* clip = iter.next(); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 862 | SkRect answer; |
| 863 | answer.iset(25, 25, 75, 75); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 864 | |
| 865 | REPORTER_ASSERT(reporter, clip); |
| 866 | REPORTER_ASSERT(reporter, clip->fRect); |
| 867 | REPORTER_ASSERT(reporter, !clip->fPath); |
| 868 | REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp); |
| 869 | REPORTER_ASSERT(reporter, *clip->fRect == answer); |
| 870 | // now check that we only had one in our iterator |
| 871 | REPORTER_ASSERT(reporter, !iter.next()); |
| 872 | |
| 873 | stack.reset(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 874 | REPORTER_ASSERT(reporter, 0 == stack.getSaveCount()); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 875 | assert_count(reporter, stack, 0); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 876 | |
| 877 | test_assign_and_comparison(reporter); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 878 | test_iterators(reporter); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 879 | test_bounds(reporter, true); // once with rects |
| 880 | test_bounds(reporter, false); // once with paths |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 881 | test_isWideOpen(reporter); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 882 | test_rect_merging(reporter); |
bsalomon@google.com | e8ca6c6 | 2012-11-07 21:19:10 +0000 | [diff] [blame] | 883 | test_iter_rect_merging(reporter); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 884 | test_reduced_clip_stack(reporter); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | #include "TestClassDef.h" |
| 888 | DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack) |