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