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