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