epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 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" |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 12 | #include "SkRect.h" |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 13 | #include "SkRegion.h" |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 14 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 15 | #if SK_SUPPORT_GPU |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 16 | #include "GrClipStackClip.h" |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 17 | #include "GrReducedClip.h" |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 18 | #include "GrResourceCache.h" |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 19 | typedef GrReducedClip::ElementList ElementList; |
| 20 | typedef GrReducedClip::InitialState InitialState; |
| 21 | #endif |
| 22 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 23 | static void test_assign_and_comparison(skiatest::Reporter* reporter) { |
| 24 | SkClipStack s; |
reed@google.com | d9f2dea | 2011-10-12 14:43:27 +0000 | [diff] [blame] | 25 | bool doAA = false; |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 26 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, 0 == s.getSaveCount()); |
| 28 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 29 | // Build up a clip stack with a path, an empty clip, and a rect. |
| 30 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
| 32 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 33 | SkPath p; |
| 34 | p.moveTo(5, 6); |
| 35 | p.lineTo(7, 8); |
| 36 | p.lineTo(5, 9); |
| 37 | p.close(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 38 | s.clipPath(p, SkMatrix::I(), kIntersect_SkClipOp, 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, 2 == s.getSaveCount()); |
| 42 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 43 | SkRect r = SkRect::MakeLTRB(1, 2, 3, 4); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 44 | s.clipRect(r, SkMatrix::I(), kIntersect_SkClipOp, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 45 | r = SkRect::MakeLTRB(10, 11, 12, 13); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 46 | s.clipRect(r, SkMatrix::I(), kIntersect_SkClipOp, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 47 | |
| 48 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 49 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 50 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 51 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 52 | s.clipRect(r, SkMatrix::I(), kUnion_SkClipOp, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 53 | |
| 54 | // Test that assignment works. |
| 55 | SkClipStack copy = s; |
| 56 | REPORTER_ASSERT(reporter, s == copy); |
| 57 | |
| 58 | // Test that different save levels triggers not equal. |
| 59 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, s != copy); |
| 62 | |
| 63 | // Test that an equal, but not copied version is equal. |
| 64 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 66 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 67 | s.clipRect(r, SkMatrix::I(), kUnion_SkClipOp, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, s == copy); |
| 69 | |
| 70 | // Test that a different op on one level triggers not equal. |
| 71 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 73 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 75 | r = SkRect::MakeLTRB(14, 15, 16, 17); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 76 | s.clipRect(r, SkMatrix::I(), kIntersect_SkClipOp, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, s != copy); |
| 78 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 79 | // Test that version constructed with rect-path rather than a rect is still considered equal. |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 80 | s.restore(); |
| 81 | s.save(); |
| 82 | SkPath rp; |
| 83 | rp.addRect(r); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 84 | s.clipPath(rp, SkMatrix::I(), kUnion_SkClipOp, doAA); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, s == copy); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 86 | |
| 87 | // Test that different rects triggers not equal. |
| 88 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 89 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 90 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, 3 == s.getSaveCount()); |
| 92 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 93 | r = SkRect::MakeLTRB(24, 25, 26, 27); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 94 | s.clipRect(r, SkMatrix::I(), kUnion_SkClipOp, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, s != copy); |
| 96 | |
| 97 | // Sanity check |
| 98 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 99 | REPORTER_ASSERT(reporter, 2 == s.getSaveCount()); |
| 100 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 101 | copy.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, 2 == copy.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, s == copy); |
| 104 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 106 | copy.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, 1 == copy.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, s == copy); |
| 109 | |
| 110 | // Test that different paths triggers not equal. |
| 111 | s.restore(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 112 | REPORTER_ASSERT(reporter, 0 == s.getSaveCount()); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 113 | s.save(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 114 | REPORTER_ASSERT(reporter, 1 == s.getSaveCount()); |
| 115 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 116 | p.addRect(r); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 117 | s.clipPath(p, SkMatrix::I(), kIntersect_SkClipOp, doAA); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, s != copy); |
| 119 | } |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 120 | |
| 121 | static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack, |
| 122 | int count) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 123 | SkClipStack::B2TIter iter(stack); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 124 | int counter = 0; |
| 125 | while (iter.next()) { |
| 126 | counter += 1; |
| 127 | } |
| 128 | REPORTER_ASSERT(reporter, count == counter); |
| 129 | } |
| 130 | |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 131 | // Exercise the SkClipStack's bottom to top and bidirectional iterators |
| 132 | // (including the skipToTopmost functionality) |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 133 | static void test_iterators(skiatest::Reporter* reporter) { |
| 134 | SkClipStack stack; |
| 135 | |
| 136 | static const SkRect gRects[] = { |
| 137 | { 0, 0, 40, 40 }, |
| 138 | { 60, 0, 100, 40 }, |
| 139 | { 0, 60, 40, 100 }, |
| 140 | { 60, 60, 100, 100 } |
| 141 | }; |
| 142 | |
| 143 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) { |
| 144 | // the union op will prevent these from being fused together |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 145 | stack.clipRect(gRects[i], SkMatrix::I(), kUnion_SkClipOp, false); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | assert_count(reporter, stack, 4); |
| 149 | |
| 150 | // bottom to top iteration |
| 151 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | const SkClipStack::Element* element = nullptr; |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 153 | |
| 154 | SkClipStack::B2TIter iter(stack); |
| 155 | int i; |
| 156 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 157 | for (i = 0, element = iter.next(); element; ++i, element = iter.next()) { |
| 158 | REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType()); |
| 159 | REPORTER_ASSERT(reporter, element->getRect() == gRects[i]); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | SkASSERT(i == 4); |
| 163 | } |
| 164 | |
| 165 | // top to bottom iteration |
| 166 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | const SkClipStack::Element* element = nullptr; |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 168 | |
| 169 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 170 | int i; |
| 171 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 172 | for (i = 3, element = iter.prev(); element; --i, element = iter.prev()) { |
| 173 | REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType()); |
| 174 | REPORTER_ASSERT(reporter, element->getRect() == gRects[i]); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | SkASSERT(i == -1); |
| 178 | } |
| 179 | |
| 180 | // skipToTopmost |
| 181 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 182 | const SkClipStack::Element* element = nullptr; |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 183 | |
| 184 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 185 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 186 | element = iter.skipToTopmost(kUnion_SkClipOp); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 187 | REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType()); |
| 188 | REPORTER_ASSERT(reporter, element->getRect() == gRects[3]); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 192 | // Exercise the SkClipStack's getConservativeBounds computation |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 193 | static void test_bounds(skiatest::Reporter* reporter, SkClipStack::Element::Type primType) { |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 194 | static const int gNumCases = 20; |
| 195 | static const SkRect gAnswerRectsBW[gNumCases] = { |
| 196 | // A op B |
| 197 | { 40, 40, 50, 50 }, |
| 198 | { 10, 10, 50, 50 }, |
| 199 | { 10, 10, 80, 80 }, |
| 200 | { 10, 10, 80, 80 }, |
| 201 | { 40, 40, 80, 80 }, |
| 202 | |
| 203 | // invA op B |
| 204 | { 40, 40, 80, 80 }, |
| 205 | { 0, 0, 100, 100 }, |
| 206 | { 0, 0, 100, 100 }, |
| 207 | { 0, 0, 100, 100 }, |
| 208 | { 40, 40, 50, 50 }, |
| 209 | |
| 210 | // A op invB |
| 211 | { 10, 10, 50, 50 }, |
| 212 | { 40, 40, 50, 50 }, |
| 213 | { 0, 0, 100, 100 }, |
| 214 | { 0, 0, 100, 100 }, |
| 215 | { 0, 0, 100, 100 }, |
| 216 | |
| 217 | // invA op invB |
| 218 | { 0, 0, 100, 100 }, |
| 219 | { 40, 40, 80, 80 }, |
| 220 | { 0, 0, 100, 100 }, |
| 221 | { 10, 10, 80, 80 }, |
| 222 | { 10, 10, 50, 50 }, |
| 223 | }; |
| 224 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 225 | static const SkClipOp gOps[] = { |
| 226 | kIntersect_SkClipOp, |
| 227 | kDifference_SkClipOp, |
| 228 | kUnion_SkClipOp, |
| 229 | kXOR_SkClipOp, |
| 230 | kReverseDifference_SkClipOp |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | SkRect rectA, rectB; |
| 234 | |
| 235 | rectA.iset(10, 10, 50, 50); |
| 236 | rectB.iset(40, 40, 80, 80); |
| 237 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 238 | SkRRect rrectA, rrectB; |
| 239 | rrectA.setOval(rectA); |
| 240 | rrectB.setRectXY(rectB, SkIntToScalar(1), SkIntToScalar(2)); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 241 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 242 | SkPath pathA, pathB; |
| 243 | |
| 244 | pathA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5)); |
| 245 | pathB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5)); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 246 | |
| 247 | SkClipStack stack; |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 248 | SkRect devClipBound; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 249 | bool isIntersectionOfRects = false; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 250 | |
| 251 | int testCase = 0; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 252 | int numBitTests = SkClipStack::Element::kPath_Type == primType ? 4 : 1; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 253 | for (int invBits = 0; invBits < numBitTests; ++invBits) { |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 254 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
| 255 | |
| 256 | stack.save(); |
| 257 | bool doInvA = SkToBool(invBits & 1); |
| 258 | bool doInvB = SkToBool(invBits & 2); |
| 259 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 260 | pathA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType : |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 261 | SkPath::kEvenOdd_FillType); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 262 | pathB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType : |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 263 | SkPath::kEvenOdd_FillType); |
| 264 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 265 | switch (primType) { |
| 266 | case SkClipStack::Element::kEmpty_Type: |
| 267 | SkDEBUGFAIL("Don't call this with kEmpty."); |
| 268 | break; |
| 269 | case SkClipStack::Element::kRect_Type: |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 270 | stack.clipRect(rectA, SkMatrix::I(), kIntersect_SkClipOp, false); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 271 | stack.clipRect(rectB, SkMatrix::I(), gOps[op], false); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 272 | break; |
| 273 | case SkClipStack::Element::kRRect_Type: |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 274 | stack.clipRRect(rrectA, SkMatrix::I(), kIntersect_SkClipOp, false); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 275 | stack.clipRRect(rrectB, SkMatrix::I(), gOps[op], false); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 276 | break; |
| 277 | case SkClipStack::Element::kPath_Type: |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 278 | stack.clipPath(pathA, SkMatrix::I(), kIntersect_SkClipOp, false); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 279 | stack.clipPath(pathB, SkMatrix::I(), gOps[op], false); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 280 | break; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 281 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 282 | |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 283 | REPORTER_ASSERT(reporter, !stack.isWideOpen()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID != stack.getTopmostGenID()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 285 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 286 | stack.getConservativeBounds(0, 0, 100, 100, &devClipBound, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 287 | &isIntersectionOfRects); |
| 288 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 289 | if (SkClipStack::Element::kRect_Type == primType) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 290 | REPORTER_ASSERT(reporter, isIntersectionOfRects == |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 291 | (gOps[op] == kIntersect_SkClipOp)); |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 292 | } else { |
| 293 | REPORTER_ASSERT(reporter, !isIntersectionOfRects); |
| 294 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 295 | |
| 296 | SkASSERT(testCase < gNumCases); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 297 | REPORTER_ASSERT(reporter, devClipBound == gAnswerRectsBW[testCase]); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 298 | ++testCase; |
| 299 | |
| 300 | stack.restore(); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 305 | // Test out 'isWideOpen' entry point |
| 306 | static void test_isWideOpen(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 307 | { |
| 308 | // Empty stack is wide open. Wide open stack means that gen id is wide open. |
| 309 | SkClipStack stack; |
| 310 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
| 311 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID()); |
| 312 | } |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 313 | |
| 314 | SkRect rectA, rectB; |
| 315 | |
| 316 | rectA.iset(10, 10, 40, 40); |
| 317 | rectB.iset(50, 50, 80, 80); |
| 318 | |
| 319 | // Stack should initially be wide open |
| 320 | { |
| 321 | SkClipStack stack; |
| 322 | |
| 323 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 324 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Test out case where the user specifies a union that includes everything |
| 328 | { |
| 329 | SkClipStack stack; |
| 330 | |
| 331 | SkPath clipA, clipB; |
| 332 | |
| 333 | clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5)); |
| 334 | clipA.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 335 | |
| 336 | clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5)); |
| 337 | clipB.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 338 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 339 | stack.clipPath(clipA, SkMatrix::I(), kReplace_SkClipOp, false); |
| 340 | stack.clipPath(clipB, SkMatrix::I(), kUnion_SkClipOp, false); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 341 | |
| 342 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 343 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Test out union w/ a wide open clip |
| 347 | { |
| 348 | SkClipStack stack; |
| 349 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 350 | stack.clipRect(rectA, SkMatrix::I(), kUnion_SkClipOp, false); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 351 | |
| 352 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 353 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // Test out empty difference from a wide open clip |
| 357 | { |
| 358 | SkClipStack stack; |
| 359 | |
| 360 | SkRect emptyRect; |
| 361 | emptyRect.setEmpty(); |
| 362 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 363 | stack.clipRect(emptyRect, SkMatrix::I(), kDifference_SkClipOp, false); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 364 | |
| 365 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 366 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Test out return to wide open |
| 370 | { |
| 371 | SkClipStack stack; |
| 372 | |
| 373 | stack.save(); |
| 374 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 375 | stack.clipRect(rectA, SkMatrix::I(), kReplace_SkClipOp, false); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 376 | |
| 377 | REPORTER_ASSERT(reporter, !stack.isWideOpen()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 378 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID != stack.getTopmostGenID()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 379 | |
| 380 | stack.restore(); |
| 381 | |
| 382 | REPORTER_ASSERT(reporter, stack.isWideOpen()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 383 | REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID()); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
bsalomon@google.com | 100abf4 | 2012-09-05 17:40:04 +0000 | [diff] [blame] | 387 | static int count(const SkClipStack& stack) { |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 388 | |
| 389 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart); |
| 390 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 391 | const SkClipStack::Element* element = nullptr; |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 392 | int count = 0; |
| 393 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 394 | for (element = iter.prev(); element; element = iter.prev(), ++count) { |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 395 | ; |
| 396 | } |
| 397 | |
| 398 | return count; |
| 399 | } |
| 400 | |
junov@chromium.org | edf32d5 | 2012-12-10 14:57:54 +0000 | [diff] [blame] | 401 | static void test_rect_inverse_fill(skiatest::Reporter* reporter) { |
| 402 | // non-intersecting rectangles |
| 403 | SkRect rect = SkRect::MakeLTRB(0, 0, 10, 10); |
| 404 | |
| 405 | SkPath path; |
| 406 | path.addRect(rect); |
| 407 | path.toggleInverseFillType(); |
| 408 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 409 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | edf32d5 | 2012-12-10 14:57:54 +0000 | [diff] [blame] | 410 | |
| 411 | SkRect bounds; |
| 412 | SkClipStack::BoundsType boundsType; |
| 413 | stack.getBounds(&bounds, &boundsType); |
| 414 | REPORTER_ASSERT(reporter, SkClipStack::kInsideOut_BoundsType == boundsType); |
| 415 | REPORTER_ASSERT(reporter, bounds == rect); |
| 416 | } |
| 417 | |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 418 | static void test_rect_replace(skiatest::Reporter* reporter) { |
| 419 | SkRect rect = SkRect::MakeWH(100, 100); |
| 420 | SkRect rect2 = SkRect::MakeXYWH(50, 50, 100, 100); |
| 421 | |
| 422 | SkRect bound; |
| 423 | SkClipStack::BoundsType type; |
| 424 | bool isIntersectionOfRects; |
| 425 | |
| 426 | // Adding a new rect with the replace operator should not increase |
| 427 | // the stack depth. BW replacing BW. |
| 428 | { |
| 429 | SkClipStack stack; |
| 430 | REPORTER_ASSERT(reporter, 0 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 431 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 432 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 433 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 434 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 435 | } |
| 436 | |
| 437 | // Adding a new rect with the replace operator should not increase |
| 438 | // the stack depth. AA replacing AA. |
| 439 | { |
| 440 | SkClipStack stack; |
| 441 | REPORTER_ASSERT(reporter, 0 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 442 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 443 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 444 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 445 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 446 | } |
| 447 | |
| 448 | // Adding a new rect with the replace operator should not increase |
| 449 | // the stack depth. BW replacing AA replacing BW. |
| 450 | { |
| 451 | SkClipStack stack; |
| 452 | REPORTER_ASSERT(reporter, 0 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 453 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 454 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 455 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 456 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 457 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 458 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 459 | } |
| 460 | |
| 461 | // Make sure replace clip rects don't collapse too much. |
| 462 | { |
| 463 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 464 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
| 465 | stack.clipRect(rect2, SkMatrix::I(), kIntersect_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 466 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 467 | |
| 468 | stack.save(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 469 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 470 | REPORTER_ASSERT(reporter, 2 == count(stack)); |
| 471 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 472 | REPORTER_ASSERT(reporter, bound == rect); |
| 473 | stack.restore(); |
| 474 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 475 | |
| 476 | stack.save(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 477 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
| 478 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 479 | REPORTER_ASSERT(reporter, 2 == count(stack)); |
| 480 | stack.restore(); |
| 481 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 482 | |
| 483 | stack.save(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 484 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
| 485 | stack.clipRect(rect2, SkMatrix::I(), kIntersect_SkClipOp, false); |
| 486 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 487 | REPORTER_ASSERT(reporter, 2 == count(stack)); |
| 488 | stack.restore(); |
| 489 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Simplified path-based version of test_rect_replace. |
| 494 | static void test_path_replace(skiatest::Reporter* reporter) { |
| 495 | SkRect rect = SkRect::MakeWH(100, 100); |
| 496 | SkPath path; |
| 497 | path.addCircle(50, 50, 50); |
| 498 | |
| 499 | // Replace operation doesn't grow the stack. |
| 500 | { |
| 501 | SkClipStack stack; |
| 502 | REPORTER_ASSERT(reporter, 0 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 503 | stack.clipPath(path, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 504 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 505 | stack.clipPath(path, SkMatrix::I(), kReplace_SkClipOp, false); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 506 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 507 | } |
| 508 | |
| 509 | // Replacing rect with path. |
| 510 | { |
| 511 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 512 | stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 513 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 514 | stack.clipPath(path, SkMatrix::I(), kReplace_SkClipOp, true); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 515 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 516 | } |
| 517 | } |
| 518 | |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 519 | // Test out SkClipStack's merging of rect clips. In particular exercise |
| 520 | // merging of aa vs. bw rects. |
| 521 | static void test_rect_merging(skiatest::Reporter* reporter) { |
| 522 | |
| 523 | SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50); |
| 524 | SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80); |
| 525 | |
| 526 | SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90); |
| 527 | SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60); |
| 528 | |
| 529 | SkRect bound; |
| 530 | SkClipStack::BoundsType type; |
| 531 | bool isIntersectionOfRects; |
| 532 | |
| 533 | // all bw overlapping - should merge |
| 534 | { |
| 535 | SkClipStack stack; |
| 536 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 537 | stack.clipRect(overlapLeft, SkMatrix::I(), kReplace_SkClipOp, false); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 538 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 539 | stack.clipRect(overlapRight, SkMatrix::I(), kIntersect_SkClipOp, false); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 540 | |
| 541 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 542 | |
| 543 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 544 | |
| 545 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 546 | } |
| 547 | |
| 548 | // all aa overlapping - should merge |
| 549 | { |
| 550 | SkClipStack stack; |
| 551 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 552 | stack.clipRect(overlapLeft, SkMatrix::I(), kReplace_SkClipOp, true); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 553 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 554 | stack.clipRect(overlapRight, SkMatrix::I(), kIntersect_SkClipOp, true); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 555 | |
| 556 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 557 | |
| 558 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 559 | |
| 560 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 561 | } |
| 562 | |
| 563 | // mixed overlapping - should _not_ merge |
| 564 | { |
| 565 | SkClipStack stack; |
| 566 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 567 | stack.clipRect(overlapLeft, SkMatrix::I(), kReplace_SkClipOp, true); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 568 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 569 | stack.clipRect(overlapRight, SkMatrix::I(), kIntersect_SkClipOp, false); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 570 | |
| 571 | REPORTER_ASSERT(reporter, 2 == count(stack)); |
| 572 | |
| 573 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 574 | |
| 575 | REPORTER_ASSERT(reporter, !isIntersectionOfRects); |
| 576 | } |
| 577 | |
| 578 | // mixed nested (bw inside aa) - should merge |
| 579 | { |
| 580 | SkClipStack stack; |
| 581 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 582 | stack.clipRect(nestedParent, SkMatrix::I(), kReplace_SkClipOp, true); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 583 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 584 | stack.clipRect(nestedChild, SkMatrix::I(), kIntersect_SkClipOp, false); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 585 | |
| 586 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 587 | |
| 588 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 589 | |
| 590 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 591 | } |
| 592 | |
| 593 | // mixed nested (aa inside bw) - should merge |
| 594 | { |
| 595 | SkClipStack stack; |
| 596 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 597 | stack.clipRect(nestedParent, SkMatrix::I(), kReplace_SkClipOp, false); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 598 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 599 | stack.clipRect(nestedChild, SkMatrix::I(), kIntersect_SkClipOp, true); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 600 | |
| 601 | REPORTER_ASSERT(reporter, 1 == count(stack)); |
| 602 | |
| 603 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 604 | |
| 605 | REPORTER_ASSERT(reporter, isIntersectionOfRects); |
| 606 | } |
| 607 | |
| 608 | // reverse nested (aa inside bw) - should _not_ merge |
| 609 | { |
| 610 | SkClipStack stack; |
| 611 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 612 | stack.clipRect(nestedChild, SkMatrix::I(), kReplace_SkClipOp, false); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 613 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 614 | stack.clipRect(nestedParent, SkMatrix::I(), kIntersect_SkClipOp, true); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 615 | |
| 616 | REPORTER_ASSERT(reporter, 2 == count(stack)); |
| 617 | |
| 618 | stack.getBounds(&bound, &type, &isIntersectionOfRects); |
| 619 | |
| 620 | REPORTER_ASSERT(reporter, !isIntersectionOfRects); |
| 621 | } |
| 622 | } |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 623 | |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 624 | static void test_quickContains(skiatest::Reporter* reporter) { |
| 625 | SkRect testRect = SkRect::MakeLTRB(10, 10, 40, 40); |
| 626 | SkRect insideRect = SkRect::MakeLTRB(20, 20, 30, 30); |
| 627 | SkRect intersectingRect = SkRect::MakeLTRB(25, 25, 50, 50); |
| 628 | SkRect outsideRect = SkRect::MakeLTRB(0, 0, 50, 50); |
| 629 | SkRect nonIntersectingRect = SkRect::MakeLTRB(100, 100, 110, 110); |
| 630 | |
| 631 | SkPath insideCircle; |
| 632 | insideCircle.addCircle(25, 25, 5); |
| 633 | SkPath intersectingCircle; |
| 634 | intersectingCircle.addCircle(25, 40, 10); |
| 635 | SkPath outsideCircle; |
| 636 | outsideCircle.addCircle(25, 25, 50); |
| 637 | SkPath nonIntersectingCircle; |
| 638 | nonIntersectingCircle.addCircle(100, 100, 5); |
| 639 | |
| 640 | { |
| 641 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 642 | stack.clipRect(outsideRect, SkMatrix::I(), kDifference_SkClipOp, false); |
| 643 | // return false because quickContains currently does not care for kDifference_SkClipOp |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 644 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 645 | } |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 646 | |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 647 | // Replace Op tests |
| 648 | { |
| 649 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 650 | stack.clipRect(outsideRect, SkMatrix::I(), kReplace_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 651 | REPORTER_ASSERT(reporter, true == stack.quickContains(testRect)); |
| 652 | } |
| 653 | |
| 654 | { |
| 655 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 656 | stack.clipRect(insideRect, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 657 | stack.save(); // To prevent in-place substitution by replace OP |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 658 | stack.clipRect(outsideRect, SkMatrix::I(), kReplace_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 659 | REPORTER_ASSERT(reporter, true == stack.quickContains(testRect)); |
| 660 | stack.restore(); |
| 661 | } |
| 662 | |
| 663 | { |
| 664 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 665 | stack.clipRect(outsideRect, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 666 | stack.save(); // To prevent in-place substitution by replace OP |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 667 | stack.clipRect(insideRect, SkMatrix::I(), kReplace_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 668 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 669 | stack.restore(); |
| 670 | } |
| 671 | |
| 672 | // Verify proper traversal of multi-element clip |
| 673 | { |
| 674 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 675 | stack.clipRect(insideRect, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 676 | // Use a path for second clip to prevent in-place intersection |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 677 | stack.clipPath(outsideCircle, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 678 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 679 | } |
| 680 | |
| 681 | // Intersect Op tests with rectangles |
| 682 | { |
| 683 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 684 | stack.clipRect(outsideRect, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 685 | REPORTER_ASSERT(reporter, true == stack.quickContains(testRect)); |
| 686 | } |
| 687 | |
| 688 | { |
| 689 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 690 | stack.clipRect(insideRect, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 691 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 692 | } |
| 693 | |
| 694 | { |
| 695 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 696 | stack.clipRect(intersectingRect, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 697 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 698 | } |
| 699 | |
| 700 | { |
| 701 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 702 | stack.clipRect(nonIntersectingRect, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 703 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 704 | } |
| 705 | |
| 706 | // Intersect Op tests with circle paths |
| 707 | { |
| 708 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 709 | stack.clipPath(outsideCircle, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 710 | REPORTER_ASSERT(reporter, true == stack.quickContains(testRect)); |
| 711 | } |
| 712 | |
| 713 | { |
| 714 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 715 | stack.clipPath(insideCircle, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 716 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 717 | } |
| 718 | |
| 719 | { |
| 720 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 721 | stack.clipPath(intersectingCircle, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 722 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 723 | } |
| 724 | |
| 725 | { |
| 726 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 727 | stack.clipPath(nonIntersectingCircle, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 728 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 729 | } |
| 730 | |
| 731 | // Intersect Op tests with inverse filled rectangles |
| 732 | { |
| 733 | SkClipStack stack; |
| 734 | SkPath path; |
| 735 | path.addRect(outsideRect); |
| 736 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 737 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 738 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 739 | } |
| 740 | |
| 741 | { |
| 742 | SkClipStack stack; |
| 743 | SkPath path; |
| 744 | path.addRect(insideRect); |
| 745 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 746 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 747 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 748 | } |
| 749 | |
| 750 | { |
| 751 | SkClipStack stack; |
| 752 | SkPath path; |
| 753 | path.addRect(intersectingRect); |
| 754 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 755 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 756 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 757 | } |
| 758 | |
| 759 | { |
| 760 | SkClipStack stack; |
| 761 | SkPath path; |
| 762 | path.addRect(nonIntersectingRect); |
| 763 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 764 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 765 | REPORTER_ASSERT(reporter, true == stack.quickContains(testRect)); |
| 766 | } |
| 767 | |
| 768 | // Intersect Op tests with inverse filled circles |
| 769 | { |
| 770 | SkClipStack stack; |
| 771 | SkPath path = outsideCircle; |
| 772 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 773 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 774 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 775 | } |
| 776 | |
| 777 | { |
| 778 | SkClipStack stack; |
| 779 | SkPath path = insideCircle; |
| 780 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 781 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 782 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 783 | } |
| 784 | |
| 785 | { |
| 786 | SkClipStack stack; |
| 787 | SkPath path = intersectingCircle; |
| 788 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 789 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 790 | REPORTER_ASSERT(reporter, false == stack.quickContains(testRect)); |
| 791 | } |
| 792 | |
| 793 | { |
| 794 | SkClipStack stack; |
| 795 | SkPath path = nonIntersectingCircle; |
| 796 | path.toggleInverseFillType(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 797 | stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 798 | REPORTER_ASSERT(reporter, true == stack.quickContains(testRect)); |
| 799 | } |
| 800 | } |
| 801 | |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 802 | static void set_region_to_stack(const SkClipStack& stack, const SkIRect& bounds, SkRegion* region) { |
| 803 | region->setRect(bounds); |
| 804 | SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 805 | while (const SkClipStack::Element *element = iter.next()) { |
| 806 | SkRegion elemRegion; |
| 807 | SkRegion boundsRgn(bounds); |
| 808 | SkPath path; |
| 809 | |
| 810 | switch (element->getType()) { |
| 811 | case SkClipStack::Element::kEmpty_Type: |
| 812 | elemRegion.setEmpty(); |
| 813 | break; |
| 814 | default: |
| 815 | element->asPath(&path); |
| 816 | elemRegion.setPath(path, boundsRgn); |
| 817 | break; |
| 818 | } |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 819 | region->op(elemRegion, (SkRegion::Op)element->getOp()); |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
| 823 | static void test_invfill_diff_bug(skiatest::Reporter* reporter) { |
| 824 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 825 | stack.clipRect({10, 10, 20, 20}, SkMatrix::I(), kIntersect_SkClipOp, false); |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 826 | |
| 827 | SkPath path; |
| 828 | path.addRect({30, 10, 40, 20}); |
| 829 | path.setFillType(SkPath::kInverseWinding_FillType); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 830 | stack.clipPath(path, SkMatrix::I(), kDifference_SkClipOp, false); |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 831 | |
| 832 | REPORTER_ASSERT(reporter, SkClipStack::kEmptyGenID == stack.getTopmostGenID()); |
| 833 | |
| 834 | SkRect stackBounds; |
| 835 | SkClipStack::BoundsType stackBoundsType; |
| 836 | stack.getBounds(&stackBounds, &stackBoundsType); |
| 837 | |
| 838 | REPORTER_ASSERT(reporter, stackBounds.isEmpty()); |
| 839 | REPORTER_ASSERT(reporter, SkClipStack::kNormal_BoundsType == stackBoundsType); |
| 840 | |
| 841 | SkRegion region; |
| 842 | set_region_to_stack(stack, {0, 0, 50, 30}, ®ion); |
| 843 | |
| 844 | REPORTER_ASSERT(reporter, region.isEmpty()); |
| 845 | } |
| 846 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 847 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 848 | |
bsalomon@google.com | a4e13c8 | 2012-11-26 21:38:37 +0000 | [diff] [blame] | 849 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 850 | // Functions that add a shape to the clip stack. The shape is computed from a rectangle. |
| 851 | // AA is always disabled since the clip stack reducer can cause changes in aa rasterization of the |
| 852 | // stack. A fractional edge repeated in different elements may be rasterized fewer times using the |
| 853 | // reduced stack. |
| 854 | typedef void (*AddElementFunc) (const SkRect& rect, |
| 855 | bool invert, |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 856 | SkClipOp op, |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 857 | SkClipStack* stack, |
| 858 | bool doAA); |
bsalomon@google.com | a4e13c8 | 2012-11-26 21:38:37 +0000 | [diff] [blame] | 859 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 860 | static void add_round_rect(const SkRect& rect, bool invert, SkClipOp op, SkClipStack* stack, |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 861 | bool doAA) { |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 862 | SkScalar rx = rect.width() / 10; |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 863 | SkScalar ry = rect.height() / 20; |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 864 | if (invert) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 865 | SkPath path; |
| 866 | path.addRoundRect(rect, rx, ry); |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 867 | path.setFillType(SkPath::kInverseWinding_FillType); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 868 | stack->clipPath(path, SkMatrix::I(), op, doAA); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 869 | } else { |
| 870 | SkRRect rrect; |
| 871 | rrect.setRectXY(rect, rx, ry); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 872 | stack->clipRRect(rrect, SkMatrix::I(), op, doAA); |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 873 | } |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 874 | }; |
| 875 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 876 | static void add_rect(const SkRect& rect, bool invert, SkClipOp op, SkClipStack* stack, |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 877 | bool doAA) { |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 878 | if (invert) { |
| 879 | SkPath path; |
| 880 | path.addRect(rect); |
| 881 | path.setFillType(SkPath::kInverseWinding_FillType); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 882 | stack->clipPath(path, SkMatrix::I(), op, doAA); |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 883 | } else { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 884 | stack->clipRect(rect, SkMatrix::I(), op, doAA); |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 885 | } |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 886 | }; |
| 887 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 888 | static void add_oval(const SkRect& rect, bool invert, SkClipOp op, SkClipStack* stack, |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 889 | bool doAA) { |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 890 | SkPath path; |
| 891 | path.addOval(rect); |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 892 | if (invert) { |
| 893 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 894 | } |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 895 | stack->clipPath(path, SkMatrix::I(), op, doAA); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 896 | }; |
| 897 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 898 | static void add_elem_to_stack(const SkClipStack::Element& element, SkClipStack* stack) { |
| 899 | switch (element.getType()) { |
| 900 | case SkClipStack::Element::kRect_Type: |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 901 | stack->clipRect(element.getRect(), SkMatrix::I(), element.getOp(), element.isAA()); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 902 | break; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 903 | case SkClipStack::Element::kRRect_Type: |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 904 | stack->clipRRect(element.getRRect(), SkMatrix::I(), element.getOp(), element.isAA()); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 905 | break; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 906 | case SkClipStack::Element::kPath_Type: |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 907 | stack->clipPath(element.getPath(), SkMatrix::I(), element.getOp(), element.isAA()); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 908 | break; |
| 909 | case SkClipStack::Element::kEmpty_Type: |
| 910 | SkDEBUGFAIL("Why did the reducer produce an explicit empty."); |
| 911 | stack->clipEmpty(); |
| 912 | break; |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 916 | static void test_reduced_clip_stack(skiatest::Reporter* reporter) { |
| 917 | // We construct random clip stacks, reduce them, and then rasterize both versions to verify that |
skia.committer@gmail.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 918 | // they are equal. |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 919 | |
| 920 | // All the clip elements will be contained within these bounds. |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 921 | static const SkIRect kIBounds = SkIRect::MakeWH(100, 100); |
| 922 | static const SkRect kBounds = SkRect::Make(kIBounds); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 923 | |
| 924 | enum { |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 925 | kNumTests = 250, |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 926 | kMinElemsPerTest = 1, |
| 927 | kMaxElemsPerTest = 50, |
| 928 | }; |
| 929 | |
| 930 | // min/max size of a clip element as a fraction of kBounds. |
| 931 | static const SkScalar kMinElemSizeFrac = SK_Scalar1 / 5; |
| 932 | static const SkScalar kMaxElemSizeFrac = SK_Scalar1; |
| 933 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 934 | static const SkClipOp kOps[] = { |
| 935 | kDifference_SkClipOp, |
| 936 | kIntersect_SkClipOp, |
| 937 | kUnion_SkClipOp, |
| 938 | kXOR_SkClipOp, |
| 939 | kReverseDifference_SkClipOp, |
| 940 | kReplace_SkClipOp, |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 941 | }; |
| 942 | |
| 943 | // Replace operations short-circuit the optimizer. We want to make sure that we test this code |
| 944 | // path a little bit but we don't want it to prevent us from testing many longer traversals in |
| 945 | // the optimizer. |
| 946 | static const int kReplaceDiv = 4 * kMaxElemsPerTest; |
| 947 | |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 948 | // We want to test inverse fills. However, they are quite rare in practice so don't over do it. |
| 949 | static const SkScalar kFractionInverted = SK_Scalar1 / kMaxElemsPerTest; |
| 950 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 951 | static const SkScalar kFractionAntialiased = 0.25; |
| 952 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 953 | static const AddElementFunc kElementFuncs[] = { |
| 954 | add_rect, |
| 955 | add_round_rect, |
| 956 | add_oval, |
| 957 | }; |
| 958 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 959 | SkRandom r; |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 960 | |
| 961 | for (int i = 0; i < kNumTests; ++i) { |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 962 | SkString testCase; |
| 963 | testCase.printf("Iteration %d", i); |
| 964 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 965 | // Randomly generate a clip stack. |
| 966 | SkClipStack stack; |
| 967 | int numElems = r.nextRangeU(kMinElemsPerTest, kMaxElemsPerTest); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 968 | bool doAA = r.nextBiasedBool(kFractionAntialiased); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 969 | for (int e = 0; e < numElems; ++e) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 970 | SkClipOp op = kOps[r.nextULessThan(SK_ARRAY_COUNT(kOps))]; |
| 971 | if (op == kReplace_SkClipOp) { |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 972 | if (r.nextU() % kReplaceDiv) { |
| 973 | --e; |
| 974 | continue; |
| 975 | } |
| 976 | } |
skia.committer@gmail.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 977 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 978 | // saves can change the clip stack behavior when an element is added. |
| 979 | bool doSave = r.nextBool(); |
skia.committer@gmail.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 980 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 981 | SkSize size = SkSize::Make( |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 982 | kBounds.width() * r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac), |
| 983 | kBounds.height() * r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac)); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 984 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 985 | SkPoint xy = {r.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth), |
| 986 | r.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight)}; |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 987 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 988 | SkRect rect; |
| 989 | if (doAA) { |
| 990 | rect.setXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight); |
| 991 | if (GrClip::IsPixelAligned(rect)) { |
| 992 | // Don't create an element that may accidentally become not antialiased. |
| 993 | rect.outset(0.5f, 0.5f); |
| 994 | } |
| 995 | SkASSERT(!GrClip::IsPixelAligned(rect)); |
| 996 | } else { |
| 997 | rect.setXYWH(SkScalarFloorToScalar(xy.fX), |
| 998 | SkScalarFloorToScalar(xy.fY), |
| 999 | SkScalarCeilToScalar(size.fWidth), |
| 1000 | SkScalarCeilToScalar(size.fHeight)); |
| 1001 | } |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1002 | |
bsalomon@google.com | 705e840 | 2012-11-27 15:43:57 +0000 | [diff] [blame] | 1003 | bool invert = r.nextBiasedBool(kFractionInverted); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 1004 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1005 | kElementFuncs[r.nextULessThan(SK_ARRAY_COUNT(kElementFuncs))](rect, invert, op, &stack, |
| 1006 | doAA); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1007 | if (doSave) { |
| 1008 | stack.save(); |
| 1009 | } |
| 1010 | } |
| 1011 | |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1012 | // Zero the memory we will new the GrReducedClip into. This ensures the elements gen ID |
| 1013 | // will be kInvalidGenID if left uninitialized. |
| 1014 | SkAlignedSTStorage<1, GrReducedClip> storage; |
| 1015 | memset(storage.get(), 0, sizeof(GrReducedClip)); |
| 1016 | GR_STATIC_ASSERT(0 == SkClipStack::kInvalidGenID); |
| 1017 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1018 | // Get the reduced version of the stack. |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1019 | SkRect queryBounds = kBounds; |
| 1020 | queryBounds.outset(kBounds.width() / 2, kBounds.height() / 2); |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1021 | const GrReducedClip* reduced = new (storage.get()) GrReducedClip(stack, queryBounds); |
bsalomon@google.com | a444430 | 2012-12-04 15:22:12 +0000 | [diff] [blame] | 1022 | |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1023 | REPORTER_ASSERT_MESSAGE(reporter, |
| 1024 | reduced->elements().isEmpty() || |
| 1025 | SkClipStack::kInvalidGenID != reduced->elementsGenID(), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1026 | testCase.c_str()); |
| 1027 | |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1028 | if (!reduced->elements().isEmpty()) { |
| 1029 | REPORTER_ASSERT_MESSAGE(reporter, reduced->hasIBounds(), testCase.c_str()); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1030 | SkRect stackBounds; |
| 1031 | SkClipStack::BoundsType stackBoundsType; |
| 1032 | stack.getBounds(&stackBounds, &stackBoundsType); |
| 1033 | if (SkClipStack::kNormal_BoundsType == stackBoundsType) { |
| 1034 | // Unless GrReducedClip starts doing some heroic tightening of the clip bounds, this |
| 1035 | // will be true since the stack bounds are completely contained inside the query. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1036 | REPORTER_ASSERT_MESSAGE(reporter, |
| 1037 | GrClip::IsInsideClip(reduced->ibounds(), stackBounds), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1038 | testCase.c_str()); |
| 1039 | } |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1040 | REPORTER_ASSERT_MESSAGE(reporter, reduced->requiresAA() == doAA, testCase.c_str()); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1041 | } |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1042 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1043 | // Build a new clip stack based on the reduced clip elements |
| 1044 | SkClipStack reducedStack; |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1045 | if (GrReducedClip::InitialState::kAllOut == reduced->initialState()) { |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1046 | // whether the result is bounded or not, the whole plane should start outside the clip. |
| 1047 | reducedStack.clipEmpty(); |
| 1048 | } |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1049 | for (ElementList::Iter iter(reduced->elements()); iter.get(); iter.next()) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1050 | add_elem_to_stack(*iter.get(), &reducedStack); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1051 | } |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1052 | |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1053 | SkIRect ibounds = reduced->hasIBounds() ? reduced->ibounds() : kIBounds; |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 1054 | |
bsalomon@google.com | 34cd70a | 2012-12-06 14:23:20 +0000 | [diff] [blame] | 1055 | // GrReducedClipStack assumes that the final result is clipped to the returned bounds |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1056 | reducedStack.clipDevRect(ibounds, kIntersect_SkClipOp); |
| 1057 | stack.clipDevRect(ibounds, kIntersect_SkClipOp); |
bsalomon@google.com | 34cd70a | 2012-12-06 14:23:20 +0000 | [diff] [blame] | 1058 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1059 | // convert both the original stack and reduced stack to SkRegions and see if they're equal |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1060 | SkRegion region; |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 1061 | set_region_to_stack(stack, ibounds, ®ion); |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 1062 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1063 | SkRegion reducedRegion; |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 1064 | set_region_to_stack(reducedStack, ibounds, &reducedRegion); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1065 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 1066 | REPORTER_ASSERT_MESSAGE(reporter, region == reducedRegion, testCase.c_str()); |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1067 | |
| 1068 | reduced->~GrReducedClip(); |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 1072 | #ifdef SK_BUILD_FOR_WIN |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1073 | #define SUPPRESS_VISIBILITY_WARNING |
| 1074 | #else |
| 1075 | #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden"))) |
| 1076 | #endif |
| 1077 | |
| 1078 | static void test_reduced_clip_stack_genid(skiatest::Reporter* reporter) { |
| 1079 | { |
| 1080 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1081 | stack.clipRect(SkRect::MakeXYWH(0, 0, 100, 100), SkMatrix::I(), kReplace_SkClipOp, |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1082 | true); |
| 1083 | stack.clipRect(SkRect::MakeXYWH(0, 0, SkScalar(50.3), SkScalar(50.3)), SkMatrix::I(), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1084 | kReplace_SkClipOp, true); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1085 | SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1086 | |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1087 | SkAlignedSTStorage<1, GrReducedClip> storage; |
| 1088 | memset(storage.get(), 0, sizeof(GrReducedClip)); |
| 1089 | GR_STATIC_ASSERT(0 == SkClipStack::kInvalidGenID); |
| 1090 | const GrReducedClip* reduced = new (storage.get()) GrReducedClip(stack, bounds); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1091 | |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1092 | REPORTER_ASSERT(reporter, reduced->elements().count() == 1); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1093 | // Clips will be cached based on the generation id. Make sure the gen id is valid. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1094 | REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID != reduced->elementsGenID()); |
| 1095 | |
| 1096 | reduced->~GrReducedClip(); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1097 | } |
| 1098 | { |
| 1099 | SkClipStack stack; |
| 1100 | |
| 1101 | // Create a clip with following 25.3, 25.3 boxes which are 25 apart: |
| 1102 | // A B |
| 1103 | // C D |
| 1104 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1105 | stack.clipRect(SkRect::MakeXYWH(0, 0, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1106 | kReplace_SkClipOp, true); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1107 | int32_t genIDA = stack.getTopmostGenID(); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1108 | stack.clipRect(SkRect::MakeXYWH(50, 0, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1109 | kUnion_SkClipOp, true); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1110 | int32_t genIDB = stack.getTopmostGenID(); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1111 | stack.clipRect(SkRect::MakeXYWH(0, 50, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1112 | kUnion_SkClipOp, true); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1113 | int32_t genIDC = stack.getTopmostGenID(); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1114 | stack.clipRect(SkRect::MakeXYWH(50, 50, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(), |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1115 | kUnion_SkClipOp, true); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1116 | int32_t genIDD = stack.getTopmostGenID(); |
| 1117 | |
| 1118 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1119 | #define IXYWH SkIRect::MakeXYWH |
| 1120 | #define XYWH SkRect::MakeXYWH |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1121 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1122 | SkIRect stackBounds = IXYWH(0, 0, 76, 76); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1123 | |
| 1124 | // The base test is to test each rect in two ways: |
| 1125 | // 1) The box dimensions. (Should reduce to "all in", no elements). |
| 1126 | // 2) A bit over the box dimensions. |
| 1127 | // In the case 2, test that the generation id is what is expected. |
| 1128 | // The rects are of fractional size so that case 2 never gets optimized to an empty element |
| 1129 | // list. |
| 1130 | |
| 1131 | // Not passing in tighter bounds is tested for consistency. |
| 1132 | static const struct SUPPRESS_VISIBILITY_WARNING { |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1133 | SkRect testBounds; |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1134 | int reducedClipCount; |
| 1135 | int32_t reducedGenID; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1136 | InitialState initialState; |
| 1137 | SkIRect clipIRect; |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1138 | // parameter. |
| 1139 | } testCases[] = { |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1140 | |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1141 | // Rect A. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1142 | { XYWH(0, 0, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 0, 25, 25) }, |
| 1143 | { XYWH(0.1f, 0.1f, 25.1f, 25.1f), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 0, 26, 26) }, |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1144 | { XYWH(0, 0, 27, 27), 1, genIDA, GrReducedClip::InitialState::kAllOut, IXYWH(0, 0, 27, 27)}, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1145 | |
| 1146 | // Rect B. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1147 | { XYWH(50, 0, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 0, 25, 25) }, |
| 1148 | { XYWH(50, 0, 25.3f, 25.3f), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 0, 26, 26) }, |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1149 | { XYWH(50, 0, 27, 27), 1, genIDB, GrReducedClip::InitialState::kAllOut, IXYWH(50, 0, 26, 27) }, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Rect C. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1152 | { XYWH(0, 50, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 50, 25, 25) }, |
| 1153 | { XYWH(0.2f, 50.1f, 25.1f, 25.2f), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 50, 26, 26) }, |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1154 | { XYWH(0, 50, 27, 27), 1, genIDC, GrReducedClip::InitialState::kAllOut, IXYWH(0, 50, 27, 26) }, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1155 | |
| 1156 | // Rect D. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1157 | { XYWH(50, 50, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 50, 25, 25)}, |
| 1158 | { XYWH(50.3f, 50.3f, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 50, 26, 26)}, |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1159 | { XYWH(50, 50, 27, 27), 1, genIDD, GrReducedClip::InitialState::kAllOut, IXYWH(50, 50, 26, 26)}, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Other tests: |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1162 | { XYWH(0, 0, 100, 100), 4, genIDD, GrReducedClip::InitialState::kAllOut, stackBounds }, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Rect in the middle, touches none. |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1165 | { XYWH(26, 26, 24, 24), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllOut, IXYWH(26, 26, 24, 24) }, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1166 | |
| 1167 | // Rect in the middle, touches all the rects. GenID is the last rect. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1168 | { XYWH(24, 24, 27, 27), 4, genIDD, GrReducedClip::InitialState::kAllOut, IXYWH(24, 24, 27, 27) }, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1169 | }; |
| 1170 | |
| 1171 | #undef XYWH |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1172 | #undef IXYWH |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1173 | |
| 1174 | for (size_t i = 0; i < SK_ARRAY_COUNT(testCases); ++i) { |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1175 | const GrReducedClip reduced(stack, testCases[i].testBounds); |
| 1176 | REPORTER_ASSERT(reporter, reduced.elements().count() == testCases[i].reducedClipCount); |
| 1177 | SkASSERT(reduced.elements().count() == testCases[i].reducedClipCount); |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 1178 | if (reduced.elements().count()) { |
| 1179 | REPORTER_ASSERT(reporter, reduced.elementsGenID() == testCases[i].reducedGenID); |
| 1180 | SkASSERT(reduced.elementsGenID() == testCases[i].reducedGenID); |
| 1181 | } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1182 | REPORTER_ASSERT(reporter, reduced.initialState() == testCases[i].initialState); |
| 1183 | SkASSERT(reduced.initialState() == testCases[i].initialState); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 1184 | REPORTER_ASSERT(reporter, reduced.hasIBounds()); |
| 1185 | SkASSERT(reduced.hasIBounds()); |
| 1186 | REPORTER_ASSERT(reporter, reduced.ibounds() == testCases[i].clipIRect); |
| 1187 | SkASSERT(reduced.ibounds() == testCases[i].clipIRect); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | static void test_reduced_clip_stack_no_aa_crash(skiatest::Reporter* reporter) { |
| 1193 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1194 | stack.clipDevRect(SkIRect::MakeXYWH(0, 0, 100, 100), kReplace_SkClipOp); |
| 1195 | stack.clipDevRect(SkIRect::MakeXYWH(0, 0, 50, 50), kReplace_SkClipOp); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1196 | SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1197 | |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1198 | // At the time, this would crash. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1199 | const GrReducedClip reduced(stack, bounds); |
| 1200 | REPORTER_ASSERT(reporter, reduced.elements().isEmpty()); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1203 | enum class ClipMethod { |
| 1204 | kSkipDraw, |
| 1205 | kIgnoreClip, |
| 1206 | kScissor, |
| 1207 | kAAElements |
| 1208 | }; |
| 1209 | |
| 1210 | static void test_aa_query(skiatest::Reporter* reporter, const SkString& testName, |
| 1211 | const SkClipStack& stack, const SkMatrix& queryXform, |
| 1212 | const SkRect& preXformQuery, ClipMethod expectedMethod, |
| 1213 | int numExpectedElems = 0) { |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1214 | SkRect queryBounds; |
| 1215 | queryXform.mapRect(&queryBounds, preXformQuery); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1216 | const GrReducedClip reduced(stack, queryBounds); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1217 | |
| 1218 | SkClipStack::BoundsType stackBoundsType; |
| 1219 | SkRect stackBounds; |
| 1220 | stack.getBounds(&stackBounds, &stackBoundsType); |
| 1221 | |
| 1222 | switch (expectedMethod) { |
| 1223 | case ClipMethod::kSkipDraw: |
| 1224 | SkASSERT(0 == numExpectedElems); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1225 | REPORTER_ASSERT_MESSAGE(reporter, reduced.elements().isEmpty(), testName.c_str()); |
| 1226 | REPORTER_ASSERT_MESSAGE(reporter, |
| 1227 | GrReducedClip::InitialState::kAllOut == reduced.initialState(), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1228 | testName.c_str()); |
| 1229 | return; |
| 1230 | case ClipMethod::kIgnoreClip: |
| 1231 | SkASSERT(0 == numExpectedElems); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 1232 | REPORTER_ASSERT_MESSAGE(reporter, |
| 1233 | !reduced.hasIBounds() || |
| 1234 | GrClip::IsInsideClip(reduced.ibounds(), queryBounds), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1235 | testName.c_str()); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1236 | REPORTER_ASSERT_MESSAGE(reporter, reduced.elements().isEmpty(), testName.c_str()); |
| 1237 | REPORTER_ASSERT_MESSAGE(reporter, |
| 1238 | GrReducedClip::InitialState::kAllIn == reduced.initialState(), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1239 | testName.c_str()); |
| 1240 | return; |
| 1241 | case ClipMethod::kScissor: { |
| 1242 | SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType); |
| 1243 | SkASSERT(0 == numExpectedElems); |
| 1244 | SkIRect expectedScissor; |
| 1245 | stackBounds.round(&expectedScissor); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1246 | REPORTER_ASSERT_MESSAGE(reporter, reduced.elements().isEmpty(), testName.c_str()); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 1247 | REPORTER_ASSERT_MESSAGE(reporter, reduced.hasIBounds(), testName.c_str()); |
| 1248 | REPORTER_ASSERT_MESSAGE(reporter, expectedScissor == reduced.ibounds(), |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1249 | testName.c_str()); |
| 1250 | REPORTER_ASSERT_MESSAGE(reporter, |
| 1251 | GrReducedClip::InitialState::kAllIn == reduced.initialState(), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1252 | testName.c_str()); |
| 1253 | return; |
| 1254 | } |
| 1255 | case ClipMethod::kAAElements: { |
| 1256 | SkIRect expectedClipIBounds = GrClip::GetPixelIBounds(queryBounds); |
| 1257 | if (SkClipStack::kNormal_BoundsType == stackBoundsType) { |
| 1258 | SkAssertResult(expectedClipIBounds.intersect(GrClip::GetPixelIBounds(stackBounds))); |
| 1259 | } |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1260 | REPORTER_ASSERT_MESSAGE(reporter, numExpectedElems == reduced.elements().count(), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1261 | testName.c_str()); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 1262 | REPORTER_ASSERT_MESSAGE(reporter, reduced.hasIBounds(), testName.c_str()); |
| 1263 | REPORTER_ASSERT_MESSAGE(reporter, expectedClipIBounds == reduced.ibounds(), |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1264 | testName.c_str()); |
| 1265 | REPORTER_ASSERT_MESSAGE(reporter, reduced.requiresAA() == !reduced.elements().isEmpty(), |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1266 | testName.c_str()); |
| 1267 | break; |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | static void test_reduced_clip_stack_aa(skiatest::Reporter* reporter) { |
| 1273 | constexpr SkScalar IL = 2, IT = 1, IR = 6, IB = 7; // Pixel aligned rect. |
| 1274 | constexpr SkScalar L = 2.2f, T = 1.7f, R = 5.8f, B = 7.3f; // Generic rect. |
| 1275 | constexpr SkScalar l = 3.3f, t = 2.8f, r = 4.7f, b = 6.2f; // Small rect contained in R. |
| 1276 | |
| 1277 | SkRect alignedRect = {IL, IT, IR, IB}; |
| 1278 | SkRect rect = {L, T, R, B}; |
| 1279 | SkRect innerRect = {l, t, r, b}; |
| 1280 | |
| 1281 | SkMatrix m; |
| 1282 | m.setIdentity(); |
| 1283 | |
| 1284 | constexpr SkScalar kMinScale = 2.0001f; |
| 1285 | constexpr SkScalar kMaxScale = 3; |
| 1286 | constexpr int kNumIters = 8; |
| 1287 | |
| 1288 | SkString name; |
| 1289 | SkRandom rand; |
| 1290 | |
| 1291 | for (int i = 0; i < kNumIters; ++i) { |
| 1292 | // Pixel-aligned rect (iior=true). |
| 1293 | name.printf("Pixel-aligned rect test, iter %i", i); |
| 1294 | SkClipStack stack; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1295 | stack.clipRect(alignedRect, SkMatrix::I(), kIntersect_SkClipOp, true); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1296 | test_aa_query(reporter, name, stack, m, {IL, IT, IR, IB}, ClipMethod::kIgnoreClip); |
| 1297 | test_aa_query(reporter, name, stack, m, {IL, IT-1, IR, IT}, ClipMethod::kSkipDraw); |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 1298 | test_aa_query(reporter, name, stack, m, {IL, IT-2, IR, IB}, ClipMethod::kScissor); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1299 | |
| 1300 | // Rect (iior=true). |
| 1301 | name.printf("Rect test, iter %i", i); |
| 1302 | stack.reset(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1303 | stack.clipRect(rect, SkMatrix::I(), kIntersect_SkClipOp, true); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1304 | test_aa_query(reporter, name, stack, m, {L, T, R, B}, ClipMethod::kIgnoreClip); |
| 1305 | test_aa_query(reporter, name, stack, m, {L-.1f, T, L, B}, ClipMethod::kSkipDraw); |
| 1306 | test_aa_query(reporter, name, stack, m, {L-.1f, T, L+.1f, B}, ClipMethod::kAAElements, 1); |
| 1307 | |
| 1308 | // Difference rect (iior=false, inside-out bounds). |
| 1309 | name.printf("Difference rect test, iter %i", i); |
| 1310 | stack.reset(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1311 | stack.clipRect(rect, SkMatrix::I(), kDifference_SkClipOp, true); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1312 | test_aa_query(reporter, name, stack, m, {L, T, R, B}, ClipMethod::kSkipDraw); |
| 1313 | test_aa_query(reporter, name, stack, m, {L, T-.1f, R, T}, ClipMethod::kIgnoreClip); |
| 1314 | test_aa_query(reporter, name, stack, m, {L, T-.1f, R, T+.1f}, ClipMethod::kAAElements, 1); |
| 1315 | |
| 1316 | // Complex clip (iior=false, normal bounds). |
| 1317 | name.printf("Complex clip test, iter %i", i); |
| 1318 | stack.reset(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1319 | stack.clipRect(rect, SkMatrix::I(), kIntersect_SkClipOp, true); |
| 1320 | stack.clipRect(innerRect, SkMatrix::I(), kXOR_SkClipOp, true); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1321 | test_aa_query(reporter, name, stack, m, {l, t, r, b}, ClipMethod::kSkipDraw); |
| 1322 | test_aa_query(reporter, name, stack, m, {r-.1f, t, R, b}, ClipMethod::kAAElements, 1); |
| 1323 | test_aa_query(reporter, name, stack, m, {r-.1f, t, R+.1f, b}, ClipMethod::kAAElements, 2); |
| 1324 | test_aa_query(reporter, name, stack, m, {r, t, R+.1f, b}, ClipMethod::kAAElements, 1); |
| 1325 | test_aa_query(reporter, name, stack, m, {r, t, R, b}, ClipMethod::kIgnoreClip); |
| 1326 | test_aa_query(reporter, name, stack, m, {R, T, R+.1f, B}, ClipMethod::kSkipDraw); |
| 1327 | |
| 1328 | // Complex clip where outer rect is pixel aligned (iior=false, normal bounds). |
| 1329 | name.printf("Aligned Complex clip test, iter %i", i); |
| 1330 | stack.reset(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1331 | stack.clipRect(alignedRect, SkMatrix::I(), kIntersect_SkClipOp, true); |
| 1332 | stack.clipRect(innerRect, SkMatrix::I(), kXOR_SkClipOp, true); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1333 | test_aa_query(reporter, name, stack, m, {l, t, r, b}, ClipMethod::kSkipDraw); |
| 1334 | test_aa_query(reporter, name, stack, m, {l, b-.1f, r, IB}, ClipMethod::kAAElements, 1); |
| 1335 | test_aa_query(reporter, name, stack, m, {l, b-.1f, r, IB+.1f}, ClipMethod::kAAElements, 1); |
| 1336 | test_aa_query(reporter, name, stack, m, {l, b, r, IB+.1f}, ClipMethod::kAAElements, 0); |
| 1337 | test_aa_query(reporter, name, stack, m, {l, b, r, IB}, ClipMethod::kIgnoreClip); |
| 1338 | test_aa_query(reporter, name, stack, m, {IL, IB, IR, IB+.1f}, ClipMethod::kSkipDraw); |
| 1339 | |
| 1340 | // Apply random transforms and try again. This ensures the clip stack reduction is hardened |
| 1341 | // against FP rounding error. |
| 1342 | SkScalar sx = rand.nextRangeScalar(kMinScale, kMaxScale); |
| 1343 | sx = SkScalarFloorToScalar(sx * alignedRect.width()) / alignedRect.width(); |
| 1344 | SkScalar sy = rand.nextRangeScalar(kMinScale, kMaxScale); |
| 1345 | sy = SkScalarFloorToScalar(sy * alignedRect.height()) / alignedRect.height(); |
| 1346 | SkScalar tx = SkScalarRoundToScalar(sx * alignedRect.x()) - sx * alignedRect.x(); |
| 1347 | SkScalar ty = SkScalarRoundToScalar(sy * alignedRect.y()) - sy * alignedRect.y(); |
| 1348 | |
| 1349 | SkMatrix xform = SkMatrix::MakeScale(sx, sy); |
| 1350 | xform.postTranslate(tx, ty); |
| 1351 | xform.mapRect(&alignedRect); |
| 1352 | xform.mapRect(&rect); |
| 1353 | xform.mapRect(&innerRect); |
| 1354 | m.postConcat(xform); |
| 1355 | } |
| 1356 | } |
| 1357 | |
bsalomon@google.com | a4e13c8 | 2012-11-26 21:38:37 +0000 | [diff] [blame] | 1358 | #endif |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 1359 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1360 | DEF_TEST(ClipStack, reporter) { |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 1361 | SkClipStack stack; |
| 1362 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1363 | REPORTER_ASSERT(reporter, 0 == stack.getSaveCount()); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 1364 | assert_count(reporter, stack, 0); |
| 1365 | |
| 1366 | static const SkIRect gRects[] = { |
| 1367 | { 0, 0, 100, 100 }, |
| 1368 | { 25, 25, 125, 125 }, |
| 1369 | { 0, 0, 1000, 1000 }, |
| 1370 | { 0, 0, 75, 75 } |
| 1371 | }; |
| 1372 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1373 | stack.clipDevRect(gRects[i], kIntersect_SkClipOp); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | // 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] | 1377 | SkClipStack::B2TIter iter(stack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1378 | const SkClipStack::Element* element = iter.next(); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame] | 1379 | SkRect answer; |
| 1380 | answer.iset(25, 25, 75, 75); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 1381 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1382 | REPORTER_ASSERT(reporter, element); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1383 | REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType()); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1384 | REPORTER_ASSERT(reporter, kIntersect_SkClipOp == element->getOp()); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1385 | REPORTER_ASSERT(reporter, element->getRect() == answer); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 1386 | // now check that we only had one in our iterator |
| 1387 | REPORTER_ASSERT(reporter, !iter.next()); |
| 1388 | |
| 1389 | stack.reset(); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1390 | REPORTER_ASSERT(reporter, 0 == stack.getSaveCount()); |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 1391 | assert_count(reporter, stack, 0); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 1392 | |
| 1393 | test_assign_and_comparison(reporter); |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1394 | test_iterators(reporter); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 1395 | test_bounds(reporter, SkClipStack::Element::kRect_Type); |
| 1396 | test_bounds(reporter, SkClipStack::Element::kRRect_Type); |
| 1397 | test_bounds(reporter, SkClipStack::Element::kPath_Type); |
robertphillips@google.com | cc6493b | 2012-07-26 18:39:13 +0000 | [diff] [blame] | 1398 | test_isWideOpen(reporter); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 1399 | test_rect_merging(reporter); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 1400 | test_rect_replace(reporter); |
junov@chromium.org | edf32d5 | 2012-12-10 14:57:54 +0000 | [diff] [blame] | 1401 | test_rect_inverse_fill(reporter); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 1402 | test_path_replace(reporter); |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 1403 | test_quickContains(reporter); |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 1404 | test_invfill_diff_bug(reporter); |
bsalomon@google.com | e7b3d29 | 2012-11-26 21:42:32 +0000 | [diff] [blame] | 1405 | #if SK_SUPPORT_GPU |
bsalomon@google.com | edb26fd | 2012-11-28 14:42:41 +0000 | [diff] [blame] | 1406 | test_reduced_clip_stack(reporter); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1407 | test_reduced_clip_stack_genid(reporter); |
| 1408 | test_reduced_clip_stack_no_aa_crash(reporter); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 1409 | test_reduced_clip_stack_aa(reporter); |
bsalomon@google.com | e7b3d29 | 2012-11-26 21:42:32 +0000 | [diff] [blame] | 1410 | #endif |
reed@google.com | bdee9fc | 2011-02-22 20:17:43 +0000 | [diff] [blame] | 1411 | } |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 1412 | |
| 1413 | ////////////////////////////////////////////////////////////////////////////// |
| 1414 | |
| 1415 | #if SK_SUPPORT_GPU |
Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame^] | 1416 | sk_sp<GrTextureProxy> GrClipStackClip::testingOnly_createClipMask(GrContext* context) const { |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 1417 | const GrReducedClip reducedClip(*fStack, SkRect::MakeWH(512, 512), 0); |
| 1418 | return this->createSoftwareClipMask(context, reducedClip); |
| 1419 | } |
| 1420 | |
| 1421 | // Verify that clip masks are freed up when the clip state that generated them goes away. |
| 1422 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ClipMaskCache, reporter, ctxInfo) { |
| 1423 | // This test uses resource key tags which only function in debug builds. |
| 1424 | #ifdef SK_DEBUG |
| 1425 | GrContext* context = ctxInfo.grContext(); |
| 1426 | SkClipStack stack; |
| 1427 | |
| 1428 | SkPath path; |
| 1429 | path.addCircle(10, 10, 8); |
| 1430 | path.addCircle(15, 15, 8); |
| 1431 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1432 | |
| 1433 | static const char* kTag = GrClipStackClip::kMaskTestTag; |
| 1434 | GrResourceCache* cache = context->getResourceCache(); |
| 1435 | |
| 1436 | static constexpr int kN = 5; |
| 1437 | |
| 1438 | for (int i = 0; i < kN; ++i) { |
| 1439 | SkMatrix m; |
| 1440 | m.setTranslate(0.5, 0.5); |
| 1441 | stack.save(); |
| 1442 | stack.clipPath(path, m, SkClipOp::kIntersect, true); |
Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame^] | 1443 | sk_sp<GrTextureProxy> mask = GrClipStackClip(&stack).testingOnly_createClipMask(context); |
| 1444 | GrTexture* tex = mask->instantiate(context->textureProvider()); |
| 1445 | REPORTER_ASSERT(reporter, 0 == strcmp(tex->getUniqueKey().tag(), kTag)); |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 1446 | // Make sure mask isn't pinned in cache. |
| 1447 | mask.reset(nullptr); |
| 1448 | context->flush(); |
| 1449 | REPORTER_ASSERT(reporter, i + 1 == cache->countUniqueKeysWithTag(kTag)); |
| 1450 | } |
| 1451 | |
| 1452 | for (int i = 0; i < kN; ++i) { |
| 1453 | stack.restore(); |
| 1454 | cache->purgeAsNeeded(); |
| 1455 | REPORTER_ASSERT(reporter, kN - (i + 1) == cache->countUniqueKeysWithTag(kTag)); |
| 1456 | } |
| 1457 | #endif |
| 1458 | } |
| 1459 | |
Mike Reed | 3726a4a | 2017-01-19 11:36:41 -0500 | [diff] [blame] | 1460 | #include "SkSurface.h" |
| 1461 | DEF_GPUTEST_FOR_ALL_CONTEXTS(canvas_private_clipRgn, reporter, ctxInfo) { |
| 1462 | GrContext* context = ctxInfo.grContext(); |
| 1463 | |
| 1464 | const int w = 10; |
| 1465 | const int h = 10; |
| 1466 | SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
| 1467 | sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
| 1468 | SkCanvas* canvas = surf->getCanvas(); |
| 1469 | SkRegion rgn; |
| 1470 | |
| 1471 | canvas->temporary_internal_getRgnClip(&rgn); |
| 1472 | REPORTER_ASSERT(reporter, rgn.isRect()); |
| 1473 | REPORTER_ASSERT(reporter, rgn.getBounds() == SkIRect::MakeWH(w, h)); |
| 1474 | |
| 1475 | canvas->save(); |
| 1476 | canvas->clipRect(SkRect::MakeWH(5, 5), kDifference_SkClipOp); |
| 1477 | canvas->temporary_internal_getRgnClip(&rgn); |
| 1478 | REPORTER_ASSERT(reporter, rgn.isComplex()); |
| 1479 | REPORTER_ASSERT(reporter, rgn.getBounds() == SkIRect::MakeWH(w, h)); |
| 1480 | canvas->restore(); |
| 1481 | |
| 1482 | canvas->save(); |
| 1483 | canvas->clipRRect(SkRRect::MakeOval(SkRect::MakeLTRB(3, 3, 7, 7))); |
| 1484 | canvas->temporary_internal_getRgnClip(&rgn); |
| 1485 | REPORTER_ASSERT(reporter, rgn.isComplex()); |
| 1486 | REPORTER_ASSERT(reporter, rgn.getBounds() == SkIRect::MakeLTRB(3, 3, 7, 7)); |
| 1487 | canvas->restore(); |
| 1488 | } |
Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 1489 | #endif |