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 | */ |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 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/SkPaint.h" |
| 12 | #include "include/core/SkPoint.h" |
| 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/core/SkScalar.h" |
| 15 | #include "include/core/SkTypes.h" |
| 16 | #include "src/core/SkEdgeClipper.h" |
| 17 | #include "src/core/SkLineClipper.h" |
| 18 | #include "tests/Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 19 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 20 | #include <cstring> |
| 21 | |
reed@google.com | 0a07265 | 2012-03-12 21:11:18 +0000 | [diff] [blame] | 22 | static void test_hairclipping(skiatest::Reporter* reporter) { |
| 23 | SkBitmap bm; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 24 | bm.allocN32Pixels(4, 4); |
reed@google.com | 0a07265 | 2012-03-12 21:11:18 +0000 | [diff] [blame] | 25 | bm.eraseColor(SK_ColorWHITE); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 26 | |
reed@google.com | 0a07265 | 2012-03-12 21:11:18 +0000 | [diff] [blame] | 27 | SkPaint paint; |
| 28 | paint.setAntiAlias(true); |
| 29 | |
| 30 | SkCanvas canvas(bm); |
reed@google.com | 470f07f | 2012-03-12 21:31:00 +0000 | [diff] [blame] | 31 | canvas.clipRect(SkRect::MakeWH(SkIntToScalar(4), SkIntToScalar(2))); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 32 | canvas.drawLine(1.5f, 1.5f, |
| 33 | 3.5f, 3.5f, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 34 | |
reed@google.com | 0a07265 | 2012-03-12 21:11:18 +0000 | [diff] [blame] | 35 | /** |
| 36 | * We had a bug where we misinterpreted the bottom of the clip, and |
| 37 | * would draw another pixel (to the right in this case) on the same |
| 38 | * last scanline. i.e. we would draw to [2,1], even though this hairline |
| 39 | * should just draw to [1,1], [2,2], [3,3] modulo the clip. |
| 40 | * |
| 41 | * The result of this entire draw should be that we only draw to [1,1] |
| 42 | * |
| 43 | * Fixed in rev. 3366 |
| 44 | */ |
| 45 | for (int y = 0; y < 4; ++y) { |
| 46 | for (int x = 0; x < 4; ++x) { |
| 47 | bool nonWhite = (1 == y) && (1 == x); |
| 48 | SkPMColor c = *bm.getAddr32(x, y); |
| 49 | if (nonWhite) { |
| 50 | REPORTER_ASSERT(reporter, 0xFFFFFFFF != c); |
| 51 | } else { |
| 52 | REPORTER_ASSERT(reporter, 0xFFFFFFFF == c); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 58 | static void test_edgeclipper() { |
reed | 31223e0 | 2015-02-09 08:33:07 -0800 | [diff] [blame] | 59 | SkEdgeClipper clipper(false); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 60 | |
reed@google.com | 6da3d17 | 2012-01-11 16:41:26 +0000 | [diff] [blame] | 61 | const SkPoint pts[] = { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 62 | { 3.0995476e+010f, 42.929779f }, |
| 63 | { -3.0995163e+010f, 51.050385f }, |
| 64 | { -3.0995157e+010f, 51.050392f }, |
| 65 | { -3.0995134e+010f, 51.050400f }, |
reed@google.com | 6da3d17 | 2012-01-11 16:41:26 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
epoger@google.com | dc7a506 | 2012-01-11 20:43:29 +0000 | [diff] [blame] | 68 | const SkRect clip = { 0, 0, SkIntToScalar(300), SkIntToScalar(200) }; |
reed@google.com | 6da3d17 | 2012-01-11 16:41:26 +0000 | [diff] [blame] | 69 | |
| 70 | // this should not assert, even though our choppers do a poor numerical |
| 71 | // job when computing their t values. |
| 72 | // http://code.google.com/p/skia/issues/detail?id=444 |
| 73 | clipper.clipCubic(pts, clip); |
| 74 | } |
| 75 | |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 76 | static void test_intersectline(skiatest::Reporter* reporter) { |
| 77 | static const SkScalar L = 0; |
| 78 | static const SkScalar T = 0; |
| 79 | static const SkScalar R = SkIntToScalar(100); |
| 80 | static const SkScalar B = SkIntToScalar(100); |
| 81 | static const SkScalar CX = SkScalarHalf(L + R); |
| 82 | static const SkScalar CY = SkScalarHalf(T + B); |
| 83 | static const SkRect gR = { L, T, R, B }; |
| 84 | |
| 85 | size_t i; |
| 86 | SkPoint dst[2]; |
| 87 | |
| 88 | static const SkPoint gEmpty[] = { |
| 89 | // sides |
| 90 | { L, CY }, { L - 10, CY }, |
| 91 | { R, CY }, { R + 10, CY }, |
| 92 | { CX, T }, { CX, T - 10 }, |
| 93 | { CX, B }, { CX, B + 10 }, |
| 94 | // corners |
| 95 | { L, T }, { L - 10, T - 10 }, |
| 96 | { L, B }, { L - 10, B + 10 }, |
| 97 | { R, T }, { R + 10, T - 10 }, |
| 98 | { R, B }, { R + 10, B + 10 }, |
| 99 | }; |
| 100 | for (i = 0; i < SK_ARRAY_COUNT(gEmpty); i += 2) { |
| 101 | bool valid = SkLineClipper::IntersectLine(&gEmpty[i], gR, dst); |
| 102 | if (valid) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 103 | SkDebugf("----- [%zu] %g %g -> %g %g\n", |
| 104 | i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY); |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 105 | } |
| 106 | REPORTER_ASSERT(reporter, !valid); |
| 107 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 108 | |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 109 | static const SkPoint gFull[] = { |
| 110 | // diagonals, chords |
| 111 | { L, T }, { R, B }, |
| 112 | { L, B }, { R, T }, |
| 113 | { CX, T }, { CX, B }, |
| 114 | { L, CY }, { R, CY }, |
| 115 | { CX, T }, { R, CY }, |
| 116 | { CX, T }, { L, CY }, |
| 117 | { L, CY }, { CX, B }, |
| 118 | { R, CY }, { CX, B }, |
| 119 | // edges |
| 120 | { L, T }, { L, B }, |
| 121 | { R, T }, { R, B }, |
| 122 | { L, T }, { R, T }, |
| 123 | { L, B }, { R, B }, |
| 124 | }; |
| 125 | for (i = 0; i < SK_ARRAY_COUNT(gFull); i += 2) { |
| 126 | bool valid = SkLineClipper::IntersectLine(&gFull[i], gR, dst); |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 127 | if (!valid || 0 != memcmp(&gFull[i], dst, sizeof(dst))) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 128 | SkDebugf("++++ [%zu] %g %g -> %g %g\n", |
| 129 | i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY); |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 130 | } |
| 131 | REPORTER_ASSERT(reporter, valid && !memcmp(&gFull[i], dst, sizeof(dst))); |
| 132 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 133 | |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 134 | static const SkPoint gPartial[] = { |
| 135 | { L - 10, CY }, { CX, CY }, { L, CY }, { CX, CY }, |
| 136 | { CX, T - 10 }, { CX, CY }, { CX, T }, { CX, CY }, |
| 137 | { R + 10, CY }, { CX, CY }, { R, CY }, { CX, CY }, |
| 138 | { CX, B + 10 }, { CX, CY }, { CX, B }, { CX, CY }, |
| 139 | // extended edges |
| 140 | { L, T - 10 }, { L, B + 10 }, { L, T }, { L, B }, |
| 141 | { R, T - 10 }, { R, B + 10 }, { R, T }, { R, B }, |
| 142 | { L - 10, T }, { R + 10, T }, { L, T }, { R, T }, |
| 143 | { L - 10, B }, { R + 10, B }, { L, B }, { R, B }, |
| 144 | }; |
| 145 | for (i = 0; i < SK_ARRAY_COUNT(gPartial); i += 4) { |
| 146 | bool valid = SkLineClipper::IntersectLine(&gPartial[i], gR, dst); |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 147 | if (!valid || 0 != memcmp(&gPartial[i+2], dst, sizeof(dst))) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 148 | SkDebugf("++++ [%zu] %g %g -> %g %g\n", |
| 149 | i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY); |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 150 | } |
| 151 | REPORTER_ASSERT(reporter, valid && |
| 152 | !memcmp(&gPartial[i+2], dst, sizeof(dst))); |
| 153 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 154 | |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 155 | } |
| 156 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 157 | DEF_TEST(Clipper, reporter) { |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 158 | test_intersectline(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 159 | test_edgeclipper(); |
reed@google.com | 0a07265 | 2012-03-12 21:11:18 +0000 | [diff] [blame] | 160 | test_hairclipping(reporter); |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 161 | } |
Mike Reed | 3054989 | 2018-08-24 17:11:28 -0400 | [diff] [blame] | 162 | |
Mike Reed | 3054989 | 2018-08-24 17:11:28 -0400 | [diff] [blame] | 163 | DEF_TEST(LineClipper_skbug_7981, r) { |
| 164 | SkPoint src[] = {{ -5.77698802E+17f, -1.81758057E+23f}, {38127, 2}}; |
| 165 | SkPoint dst[2]; |
| 166 | SkRect clip = { -32767, -32767, 32767, 32767 }; |
| 167 | |
| 168 | SkLineClipper::IntersectLine(src, clip, dst); |
| 169 | } |
| 170 | |