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