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