reed@google.com | 209c415 | 2011-10-26 15:03:48 +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 | */ |
| 7 | |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 8 | #include "SkAAClip.h" |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkMask.h" |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 13 | #include "SkRasterClip.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 14 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "Test.h" |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 16 | |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 17 | static bool operator==(const SkMask& a, const SkMask& b) { |
| 18 | if (a.fFormat != b.fFormat || a.fBounds != b.fBounds) { |
| 19 | return false; |
| 20 | } |
| 21 | if (!a.fImage && !b.fImage) { |
| 22 | return true; |
| 23 | } |
| 24 | if (!a.fImage || !b.fImage) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | size_t wbytes = a.fBounds.width(); |
| 29 | switch (a.fFormat) { |
| 30 | case SkMask::kBW_Format: |
| 31 | wbytes = (wbytes + 7) >> 3; |
| 32 | break; |
| 33 | case SkMask::kA8_Format: |
| 34 | case SkMask::k3D_Format: |
| 35 | break; |
| 36 | case SkMask::kLCD16_Format: |
| 37 | wbytes <<= 1; |
| 38 | break; |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 39 | case SkMask::kARGB32_Format: |
| 40 | wbytes <<= 2; |
| 41 | break; |
| 42 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 43 | SkDEBUGFAIL("unknown mask format"); |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
| 47 | const int h = a.fBounds.height(); |
| 48 | const char* aptr = (const char*)a.fImage; |
| 49 | const char* bptr = (const char*)b.fImage; |
| 50 | for (int y = 0; y < h; ++y) { |
| 51 | if (memcmp(aptr, bptr, wbytes)) { |
| 52 | return false; |
| 53 | } |
| 54 | aptr += wbytes; |
| 55 | bptr += wbytes; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | static void copyToMask(const SkRegion& rgn, SkMask* mask) { |
reed@google.com | a052aca | 2011-11-23 19:43:46 +0000 | [diff] [blame] | 61 | mask->fFormat = SkMask::kA8_Format; |
| 62 | |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 63 | if (rgn.isEmpty()) { |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 64 | mask->fBounds.setEmpty(); |
| 65 | mask->fRowBytes = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | mask->fImage = nullptr; |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | mask->fBounds = rgn.getBounds(); |
| 71 | mask->fRowBytes = mask->fBounds.width(); |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 72 | mask->fImage = SkMask::AllocImage(mask->computeImageSize()); |
| 73 | sk_bzero(mask->fImage, mask->computeImageSize()); |
| 74 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 75 | SkImageInfo info = SkImageInfo::Make(mask->fBounds.width(), |
| 76 | mask->fBounds.height(), |
| 77 | kAlpha_8_SkColorType, |
| 78 | kPremul_SkAlphaType); |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 79 | SkBitmap bitmap; |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 80 | bitmap.installPixels(info, mask->fImage, mask->fRowBytes); |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 81 | |
| 82 | // canvas expects its coordinate system to always be 0,0 in the top/left |
| 83 | // so we translate the rgn to match that before drawing into the mask. |
| 84 | // |
| 85 | SkRegion tmpRgn(rgn); |
| 86 | tmpRgn.translate(-rgn.getBounds().fLeft, -rgn.getBounds().fTop); |
| 87 | |
| 88 | SkCanvas canvas(bitmap); |
| 89 | canvas.clipRegion(tmpRgn); |
| 90 | canvas.drawColor(SK_ColorBLACK); |
| 91 | } |
| 92 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 93 | static SkIRect rand_rect(SkRandom& rand, int n) { |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 94 | int x = rand.nextS() % n; |
| 95 | int y = rand.nextS() % n; |
| 96 | int w = rand.nextU() % n; |
| 97 | int h = rand.nextU() % n; |
| 98 | return SkIRect::MakeXYWH(x, y, w, h); |
| 99 | } |
| 100 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 101 | static void make_rand_rgn(SkRegion* rgn, SkRandom& rand) { |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 102 | int count = rand.nextU() % 20; |
| 103 | for (int i = 0; i < count; ++i) { |
| 104 | rgn->op(rand_rect(rand, 100), SkRegion::kXOR_Op); |
| 105 | } |
| 106 | } |
| 107 | |
reed@google.com | a052aca | 2011-11-23 19:43:46 +0000 | [diff] [blame] | 108 | static bool operator==(const SkRegion& rgn, const SkAAClip& aaclip) { |
| 109 | SkMask mask0, mask1; |
| 110 | |
| 111 | copyToMask(rgn, &mask0); |
| 112 | aaclip.copyToMask(&mask1); |
| 113 | bool eq = (mask0 == mask1); |
| 114 | |
| 115 | SkMask::FreeImage(mask0.fImage); |
| 116 | SkMask::FreeImage(mask1.fImage); |
| 117 | return eq; |
| 118 | } |
| 119 | |
| 120 | static bool equalsAAClip(const SkRegion& rgn) { |
| 121 | SkAAClip aaclip; |
| 122 | aaclip.setRegion(rgn); |
| 123 | return rgn == aaclip; |
| 124 | } |
| 125 | |
| 126 | static void setRgnToPath(SkRegion* rgn, const SkPath& path) { |
| 127 | SkIRect ir; |
| 128 | path.getBounds().round(&ir); |
| 129 | rgn->setPath(path, SkRegion(ir)); |
| 130 | } |
| 131 | |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 132 | // aaclip.setRegion should create idential masks to the region |
| 133 | static void test_rgn(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 134 | SkRandom rand; |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 135 | for (int i = 0; i < 1000; i++) { |
| 136 | SkRegion rgn; |
| 137 | make_rand_rgn(&rgn, rand); |
reed@google.com | a052aca | 2011-11-23 19:43:46 +0000 | [diff] [blame] | 138 | REPORTER_ASSERT(reporter, equalsAAClip(rgn)); |
| 139 | } |
| 140 | |
| 141 | { |
| 142 | SkRegion rgn; |
| 143 | SkPath path; |
| 144 | path.addCircle(0, 0, SkIntToScalar(30)); |
| 145 | setRgnToPath(&rgn, path); |
| 146 | REPORTER_ASSERT(reporter, equalsAAClip(rgn)); |
| 147 | |
| 148 | path.reset(); |
| 149 | path.moveTo(0, 0); |
| 150 | path.lineTo(SkIntToScalar(100), 0); |
| 151 | path.lineTo(SkIntToScalar(100 - 20), SkIntToScalar(20)); |
| 152 | path.lineTo(SkIntToScalar(20), SkIntToScalar(20)); |
| 153 | setRgnToPath(&rgn, path); |
| 154 | REPORTER_ASSERT(reporter, equalsAAClip(rgn)); |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 158 | static const SkRegion::Op gRgnOps[] = { |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 159 | SkRegion::kDifference_Op, |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 160 | SkRegion::kIntersect_Op, |
| 161 | SkRegion::kUnion_Op, |
| 162 | SkRegion::kXOR_Op, |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 163 | SkRegion::kReverseDifference_Op, |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 164 | SkRegion::kReplace_Op |
| 165 | }; |
| 166 | |
| 167 | static const char* gRgnOpNames[] = { |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 168 | "DIFF", "INTERSECT", "UNION", "XOR", "REVERSE_DIFF", "REPLACE" |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 169 | }; |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 170 | |
reed@google.com | 12e1525 | 2011-10-26 15:19:36 +0000 | [diff] [blame] | 171 | static void imoveTo(SkPath& path, int x, int y) { |
| 172 | path.moveTo(SkIntToScalar(x), SkIntToScalar(y)); |
| 173 | } |
| 174 | |
| 175 | static void icubicTo(SkPath& path, int x0, int y0, int x1, int y1, int x2, int y2) { |
| 176 | path.cubicTo(SkIntToScalar(x0), SkIntToScalar(y0), |
| 177 | SkIntToScalar(x1), SkIntToScalar(y1), |
| 178 | SkIntToScalar(x2), SkIntToScalar(y2)); |
| 179 | } |
| 180 | |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 181 | static void test_path_bounds(skiatest::Reporter* reporter) { |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 182 | SkPath path; |
| 183 | SkAAClip clip; |
| 184 | const int height = 40; |
| 185 | const SkScalar sheight = SkIntToScalar(height); |
| 186 | |
| 187 | path.addOval(SkRect::MakeWH(sheight, sheight)); |
| 188 | REPORTER_ASSERT(reporter, sheight == path.getBounds().height()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 189 | clip.setPath(path, nullptr, true); |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, height == clip.getBounds().height()); |
| 191 | |
| 192 | // this is the trimmed height of this cubic (with aa). The critical thing |
| 193 | // for this test is that it is less than height, which represents just |
| 194 | // the bounds of the path's control-points. |
| 195 | // |
| 196 | // This used to fail until we tracked the MinY in the BuilderBlitter. |
| 197 | // |
| 198 | const int teardrop_height = 12; |
| 199 | path.reset(); |
reed@google.com | 12e1525 | 2011-10-26 15:19:36 +0000 | [diff] [blame] | 200 | imoveTo(path, 0, 20); |
| 201 | icubicTo(path, 40, 40, 40, 0, 0, 20); |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 202 | REPORTER_ASSERT(reporter, sheight == path.getBounds().height()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 203 | clip.setPath(path, nullptr, true); |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 204 | REPORTER_ASSERT(reporter, teardrop_height == clip.getBounds().height()); |
| 205 | } |
| 206 | |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 207 | static void test_empty(skiatest::Reporter* reporter) { |
| 208 | SkAAClip clip0, clip1; |
| 209 | |
| 210 | REPORTER_ASSERT(reporter, clip0.isEmpty()); |
| 211 | REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty()); |
| 212 | REPORTER_ASSERT(reporter, clip1 == clip0); |
| 213 | |
| 214 | clip0.translate(10, 10); // should have no effect on empty |
| 215 | REPORTER_ASSERT(reporter, clip0.isEmpty()); |
| 216 | REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty()); |
| 217 | REPORTER_ASSERT(reporter, clip1 == clip0); |
| 218 | |
| 219 | SkIRect r = { 10, 10, 40, 50 }; |
| 220 | clip0.setRect(r); |
| 221 | REPORTER_ASSERT(reporter, !clip0.isEmpty()); |
| 222 | REPORTER_ASSERT(reporter, !clip0.getBounds().isEmpty()); |
| 223 | REPORTER_ASSERT(reporter, clip0 != clip1); |
| 224 | REPORTER_ASSERT(reporter, clip0.getBounds() == r); |
| 225 | |
| 226 | clip0.setEmpty(); |
| 227 | REPORTER_ASSERT(reporter, clip0.isEmpty()); |
| 228 | REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty()); |
| 229 | REPORTER_ASSERT(reporter, clip1 == clip0); |
| 230 | |
| 231 | SkMask mask; |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 232 | clip0.copyToMask(&mask); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 233 | REPORTER_ASSERT(reporter, nullptr == mask.fImage); |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 234 | REPORTER_ASSERT(reporter, mask.fBounds.isEmpty()); |
| 235 | } |
| 236 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 237 | static void rand_irect(SkIRect* r, int N, SkRandom& rand) { |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 238 | r->setXYWH(0, 0, rand.nextU() % N, rand.nextU() % N); |
| 239 | int dx = rand.nextU() % (2*N); |
| 240 | int dy = rand.nextU() % (2*N); |
| 241 | // use int dx,dy to make the subtract be signed |
| 242 | r->offset(N - dx, N - dy); |
| 243 | } |
| 244 | |
| 245 | static void test_irect(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 246 | SkRandom rand; |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 247 | |
reed@google.com | c904191 | 2011-10-27 16:58:46 +0000 | [diff] [blame] | 248 | for (int i = 0; i < 10000; i++) { |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 249 | SkAAClip clip0, clip1; |
| 250 | SkRegion rgn0, rgn1; |
| 251 | SkIRect r0, r1; |
| 252 | |
| 253 | rand_irect(&r0, 10, rand); |
| 254 | rand_irect(&r1, 10, rand); |
| 255 | clip0.setRect(r0); |
| 256 | clip1.setRect(r1); |
| 257 | rgn0.setRect(r0); |
| 258 | rgn1.setRect(r1); |
| 259 | for (size_t j = 0; j < SK_ARRAY_COUNT(gRgnOps); ++j) { |
| 260 | SkRegion::Op op = gRgnOps[j]; |
| 261 | SkAAClip clip2; |
| 262 | SkRegion rgn2; |
| 263 | bool nonEmptyAA = clip2.op(clip0, clip1, op); |
| 264 | bool nonEmptyBW = rgn2.op(rgn0, rgn1, op); |
| 265 | if (nonEmptyAA != nonEmptyBW || clip2.getBounds() != rgn2.getBounds()) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 266 | ERRORF(reporter, "%s %s " |
| 267 | "[%d %d %d %d] %s [%d %d %d %d] = BW:[%d %d %d %d] AA:[%d %d %d %d]\n", |
| 268 | nonEmptyAA == nonEmptyBW ? "true" : "false", |
| 269 | clip2.getBounds() == rgn2.getBounds() ? "true" : "false", |
| 270 | r0.fLeft, r0.fTop, r0.right(), r0.bottom(), |
| 271 | gRgnOpNames[j], |
| 272 | r1.fLeft, r1.fTop, r1.right(), r1.bottom(), |
| 273 | rgn2.getBounds().fLeft, rgn2.getBounds().fTop, |
| 274 | rgn2.getBounds().right(), rgn2.getBounds().bottom(), |
| 275 | clip2.getBounds().fLeft, clip2.getBounds().fTop, |
| 276 | clip2.getBounds().right(), clip2.getBounds().bottom()); |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 277 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 278 | |
reed@google.com | 80cdb9a | 2012-02-16 18:56:17 +0000 | [diff] [blame] | 279 | SkMask maskBW, maskAA; |
| 280 | copyToMask(rgn2, &maskBW); |
| 281 | clip2.copyToMask(&maskAA); |
tomhudson@google.com | 0435f16 | 2012-03-15 18:16:39 +0000 | [diff] [blame] | 282 | SkAutoMaskFreeImage freeBW(maskBW.fImage); |
| 283 | SkAutoMaskFreeImage freeAA(maskAA.fImage); |
reed@google.com | 80cdb9a | 2012-02-16 18:56:17 +0000 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, maskBW == maskAA); |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
reed@google.com | 80cdb9a | 2012-02-16 18:56:17 +0000 | [diff] [blame] | 289 | static void test_path_with_hole(skiatest::Reporter* reporter) { |
| 290 | static const uint8_t gExpectedImage[] = { |
| 291 | 0xFF, 0xFF, 0xFF, 0xFF, |
| 292 | 0xFF, 0xFF, 0xFF, 0xFF, |
| 293 | 0x00, 0x00, 0x00, 0x00, |
| 294 | 0x00, 0x00, 0x00, 0x00, |
| 295 | 0xFF, 0xFF, 0xFF, 0xFF, |
| 296 | 0xFF, 0xFF, 0xFF, 0xFF, |
| 297 | }; |
| 298 | SkMask expected; |
| 299 | expected.fBounds.set(0, 0, 4, 6); |
| 300 | expected.fRowBytes = 4; |
| 301 | expected.fFormat = SkMask::kA8_Format; |
| 302 | expected.fImage = (uint8_t*)gExpectedImage; |
| 303 | |
| 304 | SkPath path; |
| 305 | path.addRect(SkRect::MakeXYWH(0, 0, |
| 306 | SkIntToScalar(4), SkIntToScalar(2))); |
| 307 | path.addRect(SkRect::MakeXYWH(0, SkIntToScalar(4), |
| 308 | SkIntToScalar(4), SkIntToScalar(2))); |
| 309 | |
| 310 | for (int i = 0; i < 2; ++i) { |
| 311 | SkAAClip clip; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 312 | clip.setPath(path, nullptr, 1 == i); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 313 | |
reed@google.com | 80cdb9a | 2012-02-16 18:56:17 +0000 | [diff] [blame] | 314 | SkMask mask; |
| 315 | clip.copyToMask(&mask); |
tomhudson@google.com | 0435f16 | 2012-03-15 18:16:39 +0000 | [diff] [blame] | 316 | SkAutoMaskFreeImage freeM(mask.fImage); |
| 317 | |
reed@google.com | 80cdb9a | 2012-02-16 18:56:17 +0000 | [diff] [blame] | 318 | REPORTER_ASSERT(reporter, expected == mask); |
| 319 | } |
| 320 | } |
| 321 | |
reed | 202ab2a | 2014-08-07 11:48:10 -0700 | [diff] [blame] | 322 | static void test_really_a_rect(skiatest::Reporter* reporter) { |
| 323 | SkRRect rrect; |
| 324 | rrect.setRectXY(SkRect::MakeWH(100, 100), 5, 5); |
| 325 | |
| 326 | SkPath path; |
| 327 | path.addRRect(rrect); |
| 328 | |
| 329 | SkAAClip clip; |
| 330 | clip.setPath(path); |
| 331 | |
| 332 | REPORTER_ASSERT(reporter, clip.getBounds() == SkIRect::MakeWH(100, 100)); |
| 333 | REPORTER_ASSERT(reporter, !clip.isRect()); |
| 334 | |
| 335 | // This rect should intersect the clip, but slice-out all of the "soft" parts, |
| 336 | // leaving just a rect. |
| 337 | const SkIRect ir = SkIRect::MakeLTRB(10, -10, 50, 90); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 338 | |
reed | 202ab2a | 2014-08-07 11:48:10 -0700 | [diff] [blame] | 339 | clip.op(ir, SkRegion::kIntersect_Op); |
| 340 | |
| 341 | REPORTER_ASSERT(reporter, clip.getBounds() == SkIRect::MakeLTRB(10, 0, 50, 90)); |
| 342 | // the clip recognized that that it is just a rect! |
| 343 | REPORTER_ASSERT(reporter, clip.isRect()); |
| 344 | } |
| 345 | |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 346 | static void did_dx_affect(skiatest::Reporter* reporter, const SkScalar dx[], |
| 347 | size_t count, bool changed) { |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 348 | const SkIRect baseBounds = SkIRect::MakeXYWH(0, 0, 10, 10); |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 349 | SkIRect ir = { 0, 0, 10, 10 }; |
| 350 | |
| 351 | for (size_t i = 0; i < count; ++i) { |
| 352 | SkRect r; |
| 353 | r.set(ir); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 354 | |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 355 | SkRasterClip rc0(ir); |
| 356 | SkRasterClip rc1(ir); |
| 357 | SkRasterClip rc2(ir); |
| 358 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 359 | rc0.op(r, SkMatrix::I(), baseBounds, SkRegion::kIntersect_Op, false); |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 360 | r.offset(dx[i], 0); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 361 | rc1.op(r, SkMatrix::I(), baseBounds, SkRegion::kIntersect_Op, true); |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 362 | r.offset(-2*dx[i], 0); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 363 | rc2.op(r, SkMatrix::I(), baseBounds, SkRegion::kIntersect_Op, true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 364 | |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 365 | REPORTER_ASSERT(reporter, changed != (rc0 == rc1)); |
| 366 | REPORTER_ASSERT(reporter, changed != (rc0 == rc2)); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | static void test_nearly_integral(skiatest::Reporter* reporter) { |
| 371 | // All of these should generate equivalent rasterclips |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 372 | |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 373 | static const SkScalar gSafeX[] = { |
| 374 | 0, SK_Scalar1/1000, SK_Scalar1/100, SK_Scalar1/10, |
| 375 | }; |
| 376 | did_dx_affect(reporter, gSafeX, SK_ARRAY_COUNT(gSafeX), false); |
| 377 | |
| 378 | static const SkScalar gUnsafeX[] = { |
| 379 | SK_Scalar1/4, SK_Scalar1/3, |
| 380 | }; |
| 381 | did_dx_affect(reporter, gUnsafeX, SK_ARRAY_COUNT(gUnsafeX), true); |
| 382 | } |
| 383 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 384 | static void test_regressions() { |
reed@google.com | 9b0da23 | 2012-02-29 13:59:15 +0000 | [diff] [blame] | 385 | // these should not assert in the debug build |
| 386 | // bug was introduced in rev. 3209 |
| 387 | { |
| 388 | SkAAClip clip; |
| 389 | SkRect r; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 390 | r.fLeft = 129.892181f; |
| 391 | r.fTop = 10.3999996f; |
| 392 | r.fRight = 130.892181f; |
| 393 | r.fBottom = 20.3999996f; |
reed@google.com | 9b0da23 | 2012-02-29 13:59:15 +0000 | [diff] [blame] | 394 | clip.setRect(r, true); |
| 395 | } |
| 396 | } |
| 397 | |
reed | 157f36d | 2014-10-15 07:05:09 -0700 | [diff] [blame] | 398 | // Building aaclip meant aa-scan-convert a path into a huge clip. |
| 399 | // the old algorithm sized the supersampler to the size of the clip, which overflowed |
| 400 | // its internal 16bit coordinates. The fix was to intersect the clip+path_bounds before |
| 401 | // sizing the supersampler. |
| 402 | // |
| 403 | // Before the fix, the following code would assert in debug builds. |
| 404 | // |
| 405 | static void test_crbug_422693(skiatest::Reporter* reporter) { |
reed | 157f36d | 2014-10-15 07:05:09 -0700 | [diff] [blame] | 406 | SkRasterClip rc(SkIRect::MakeLTRB(-25000, -25000, 25000, 25000)); |
| 407 | SkPath path; |
| 408 | path.addCircle(50, 50, 50); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 409 | rc.op(path, SkMatrix::I(), rc.getBounds(), SkRegion::kIntersect_Op, true); |
reed | 157f36d | 2014-10-15 07:05:09 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Mike Reed | 9fc5362 | 2018-01-03 15:35:33 -0500 | [diff] [blame] | 412 | static void test_huge(skiatest::Reporter* reporter) { |
| 413 | SkAAClip clip; |
| 414 | int big = 0x70000000; |
| 415 | SkIRect r = { -big, -big, big, big }; |
| 416 | SkASSERT(r.width() < 0 && r.height() < 0); |
| 417 | |
| 418 | clip.setRect(r); |
| 419 | } |
| 420 | |
mtklein@google.com | 014f2c4 | 2013-09-19 20:56:46 +0000 | [diff] [blame] | 421 | DEF_TEST(AAClip, reporter) { |
reed@google.com | d8676d2 | 2011-10-26 18:01:25 +0000 | [diff] [blame] | 422 | test_empty(reporter); |
| 423 | test_path_bounds(reporter); |
| 424 | test_irect(reporter); |
mike@reedtribe.org | 95b85bd | 2011-11-23 03:12:58 +0000 | [diff] [blame] | 425 | test_rgn(reporter); |
reed@google.com | 80cdb9a | 2012-02-16 18:56:17 +0000 | [diff] [blame] | 426 | test_path_with_hole(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 427 | test_regressions(); |
reed@google.com | 18c464b | 2012-05-11 20:57:25 +0000 | [diff] [blame] | 428 | test_nearly_integral(reporter); |
reed | 202ab2a | 2014-08-07 11:48:10 -0700 | [diff] [blame] | 429 | test_really_a_rect(reporter); |
reed | 157f36d | 2014-10-15 07:05:09 -0700 | [diff] [blame] | 430 | test_crbug_422693(reporter); |
Mike Reed | 9fc5362 | 2018-01-03 15:35:33 -0500 | [diff] [blame] | 431 | test_huge(reporter); |
reed@google.com | 209c415 | 2011-10-26 15:03:48 +0000 | [diff] [blame] | 432 | } |