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