reed@google.com | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
reed@google.com | 1df888b | 2012-04-24 22:47:21 +0000 | [diff] [blame] | 10 | #include "SkDashPathEffect.h" |
reed@google.com | f272e35 | 2013-08-26 21:27:03 +0000 | [diff] [blame] | 11 | #include "SkSurface.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
reed@google.com | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 13 | |
reed@google.com | f272e35 | 2013-08-26 21:27:03 +0000 | [diff] [blame] | 14 | // test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint) |
| 15 | static void test_big_aa_rect(skiatest::Reporter* reporter) { |
| 16 | SkBitmap output; |
| 17 | SkPMColor pixel[1]; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 18 | output.installPixels(SkImageInfo::MakeN32Premul(1, 1), |
| 19 | pixel, 4, NULL, NULL); |
reed@google.com | f272e35 | 2013-08-26 21:27:03 +0000 | [diff] [blame] | 20 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 21 | SkSurface* surf = SkSurface::NewRasterPMColor(300, 33300); |
reed@google.com | f272e35 | 2013-08-26 21:27:03 +0000 | [diff] [blame] | 22 | SkCanvas* canvas = surf->getCanvas(); |
| 23 | |
| 24 | SkRect r = { 0, 33000, 300, 33300 }; |
| 25 | int x = SkScalarRoundToInt(r.left()); |
| 26 | int y = SkScalarRoundToInt(r.top()); |
| 27 | |
| 28 | // check that the pixel in question starts as transparent (by the surface) |
| 29 | if (canvas->readPixels(&output, x, y)) { |
| 30 | REPORTER_ASSERT(reporter, 0 == pixel[0]); |
| 31 | } else { |
mtklein@google.com | 9f842d3 | 2013-08-27 16:15:37 +0000 | [diff] [blame] | 32 | REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed"); |
reed@google.com | f272e35 | 2013-08-26 21:27:03 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | SkPaint paint; |
| 36 | paint.setAntiAlias(true); |
| 37 | paint.setColor(SK_ColorWHITE); |
| 38 | |
| 39 | canvas->drawRect(r, paint); |
| 40 | |
| 41 | // Now check that it is BLACK |
| 42 | if (canvas->readPixels(&output, x, y)) { |
| 43 | // don't know what swizzling PMColor did, but white should always |
| 44 | // appear the same. |
| 45 | REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]); |
| 46 | } else { |
mtklein@google.com | 9f842d3 | 2013-08-27 16:15:37 +0000 | [diff] [blame] | 47 | REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed"); |
reed@google.com | f272e35 | 2013-08-26 21:27:03 +0000 | [diff] [blame] | 48 | } |
| 49 | surf->unref(); |
| 50 | } |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
reed@google.com | b59ed51 | 2012-06-15 18:26:04 +0000 | [diff] [blame] | 54 | static void moveToH(SkPath* path, const uint32_t raw[]) { |
| 55 | const float* fptr = (const float*)raw; |
| 56 | path->moveTo(fptr[0], fptr[1]); |
| 57 | } |
| 58 | |
| 59 | static void cubicToH(SkPath* path, const uint32_t raw[]) { |
| 60 | const float* fptr = (const float*)raw; |
| 61 | path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]); |
| 62 | } |
| 63 | |
| 64 | // This used to assert, because we performed a cast (int)(pt[0].fX * scale) to |
| 65 | // arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert |
| 66 | // was that the initial line-segment produced by the cubic was not monotonically |
| 67 | // going down (i.e. the initial DY was negative). By rounding the floats, we get |
| 68 | // the more proper result. |
| 69 | // |
| 70 | // http://code.google.com/p/chromium/issues/detail?id=131181 |
| 71 | // |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 72 | |
| 73 | // we're not calling this test anymore; is that for a reason? |
| 74 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 75 | static void test_crbug131181() { |
reed@google.com | b59ed51 | 2012-06-15 18:26:04 +0000 | [diff] [blame] | 76 | /* |
| 77 | fX = 18.8943768, |
| 78 | fY = 129.121277 |
| 79 | }, { |
| 80 | fX = 18.8937435, |
| 81 | fY = 129.121689 |
| 82 | }, { |
| 83 | fX = 18.8950119, |
| 84 | fY = 129.120422 |
| 85 | }, { |
| 86 | fX = 18.5030727, |
| 87 | fY = 129.13121 |
| 88 | */ |
| 89 | uint32_t data[] = { |
| 90 | 0x419727af, 0x43011f0c, 0x41972663, 0x43011f27, |
| 91 | 0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197 |
| 92 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | |
reed@google.com | b59ed51 | 2012-06-15 18:26:04 +0000 | [diff] [blame] | 94 | SkPath path; |
| 95 | moveToH(&path, &data[0]); |
| 96 | cubicToH(&path, &data[2]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 98 | SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(640, 480)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 99 | |
reed@google.com | b59ed51 | 2012-06-15 18:26:04 +0000 | [diff] [blame] | 100 | SkPaint paint; |
| 101 | paint.setAntiAlias(true); |
| 102 | canvas->drawPath(path, paint); |
| 103 | } |
| 104 | |
reed@google.com | e2faf17 | 2012-08-06 19:01:34 +0000 | [diff] [blame] | 105 | // This used to assert in debug builds (and crash writing bad memory in release) |
| 106 | // because we overflowed an intermediate value (B coefficient) setting up our |
| 107 | // stepper for the quadratic. Now we bias that value by 1/2 so we don't overflow |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 108 | static void test_crbug_140803() { |
reed@google.com | e2faf17 | 2012-08-06 19:01:34 +0000 | [diff] [blame] | 109 | SkBitmap bm; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 110 | bm.allocN32Pixels(2700, 30*1024); |
reed@google.com | e2faf17 | 2012-08-06 19:01:34 +0000 | [diff] [blame] | 111 | SkCanvas canvas(bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 112 | |
reed@google.com | e2faf17 | 2012-08-06 19:01:34 +0000 | [diff] [blame] | 113 | SkPath path; |
| 114 | path.moveTo(2762, 20); |
| 115 | path.quadTo(11, 21702, 10, 21706); |
| 116 | SkPaint paint; |
| 117 | paint.setAntiAlias(true); |
| 118 | canvas.drawPath(path, paint); |
| 119 | } |
| 120 | |
reed@google.com | 9d5f76a | 2012-05-01 14:49:28 +0000 | [diff] [blame] | 121 | // Need to exercise drawing an inverse-path whose bounds intersect the clip, |
| 122 | // but whose edges do not (since its a quad which draws only in the bottom half |
| 123 | // of its bounds). |
| 124 | // In the debug build, we used to assert in this case, until it was fixed. |
| 125 | // |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 126 | static void test_inversepathwithclip() { |
reed@google.com | 9d5f76a | 2012-05-01 14:49:28 +0000 | [diff] [blame] | 127 | SkPath path; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | |
reed@google.com | 9d5f76a | 2012-05-01 14:49:28 +0000 | [diff] [blame] | 129 | path.moveTo(0, SkIntToScalar(20)); |
| 130 | path.quadTo(SkIntToScalar(10), SkIntToScalar(10), |
| 131 | SkIntToScalar(20), SkIntToScalar(20)); |
| 132 | path.toggleInverseFillType(); |
| 133 | |
| 134 | SkPaint paint; |
| 135 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 136 | SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(640, 480)); |
reed@google.com | 9d5f76a | 2012-05-01 14:49:28 +0000 | [diff] [blame] | 137 | canvas.get()->save(); |
| 138 | canvas.get()->clipRect(SkRect::MakeWH(SkIntToScalar(19), SkIntToScalar(11))); |
| 139 | |
| 140 | paint.setAntiAlias(false); |
| 141 | canvas.get()->drawPath(path, paint); |
| 142 | paint.setAntiAlias(true); |
| 143 | canvas.get()->drawPath(path, paint); |
| 144 | |
| 145 | canvas.get()->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 146 | |
reed@google.com | 9d5f76a | 2012-05-01 14:49:28 +0000 | [diff] [blame] | 147 | // Now do the test again, with the path flipped, so we only draw in the |
| 148 | // top half of our bounds, and have the clip intersect our bounds at the |
| 149 | // bottom. |
| 150 | path.reset(); // preserves our filltype |
| 151 | path.moveTo(0, SkIntToScalar(10)); |
| 152 | path.quadTo(SkIntToScalar(10), SkIntToScalar(20), |
| 153 | SkIntToScalar(20), SkIntToScalar(10)); |
| 154 | canvas.get()->clipRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(19), |
| 155 | SkIntToScalar(19), SkIntToScalar(11))); |
| 156 | |
| 157 | paint.setAntiAlias(false); |
| 158 | canvas.get()->drawPath(path, paint); |
| 159 | paint.setAntiAlias(true); |
| 160 | canvas.get()->drawPath(path, paint); |
| 161 | } |
| 162 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 163 | static void test_bug533() { |
mike@reedtribe.org | 6093e65 | 2012-04-14 12:55:17 +0000 | [diff] [blame] | 164 | /* |
| 165 | http://code.google.com/p/skia/issues/detail?id=533 |
| 166 | This particular test/bug only applies to the float case, where the |
| 167 | coordinates are very large. |
| 168 | */ |
| 169 | SkPath path; |
| 170 | path.moveTo(64, 3); |
| 171 | path.quadTo(-329936, -100000000, 1153, 330003); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 172 | |
mike@reedtribe.org | 6093e65 | 2012-04-14 12:55:17 +0000 | [diff] [blame] | 173 | SkPaint paint; |
| 174 | paint.setAntiAlias(true); |
| 175 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 176 | SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(640, 480)); |
mike@reedtribe.org | 6093e65 | 2012-04-14 12:55:17 +0000 | [diff] [blame] | 177 | canvas.get()->drawPath(path, paint); |
mike@reedtribe.org | 6093e65 | 2012-04-14 12:55:17 +0000 | [diff] [blame] | 178 | } |
| 179 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 180 | static void test_crbug_140642() { |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 181 | /* |
| 182 | * We used to see this construct, and due to rounding as we accumulated |
| 183 | * our length, the loop where we apply the phase would run off the end of |
| 184 | * the array, since it relied on just -= each interval value, which did not |
| 185 | * behave as "expected". Now the code explicitly checks for walking off the |
| 186 | * end of that array. |
| 187 | |
| 188 | * A different (better) fix might be to rewrite dashing to do all of its |
| 189 | * length/phase/measure math using double, but this may need to be |
| 190 | * coordinated with SkPathMeasure, to be consistent between the two. |
| 191 | |
| 192 | <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247" |
| 193 | stroke-dashoffset="-248.135982067"> |
| 194 | */ |
| 195 | |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 196 | const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 }; |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 197 | SkAutoTUnref<SkDashPathEffect> dontAssert(SkDashPathEffect::Create(vals, 4, -248.135982067f)); |
reed@google.com | d9ee348 | 2012-08-06 14:58:35 +0000 | [diff] [blame] | 198 | } |
| 199 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 200 | static void test_crbug_124652() { |
reed@google.com | 1df888b | 2012-04-24 22:47:21 +0000 | [diff] [blame] | 201 | /* |
| 202 | http://code.google.com/p/chromium/issues/detail?id=124652 |
| 203 | This particular test/bug only applies to the float case, where |
| 204 | large values can "swamp" small ones. |
| 205 | */ |
| 206 | SkScalar intervals[2] = {837099584, 33450}; |
commit-bot@chromium.org | 35c03fb | 2014-03-31 18:52:51 +0000 | [diff] [blame] | 207 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, -10)); |
reed@google.com | 1df888b | 2012-04-24 22:47:21 +0000 | [diff] [blame] | 208 | } |
| 209 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 210 | static void test_bigcubic() { |
reed@google.com | a90aa53 | 2012-04-16 16:27:09 +0000 | [diff] [blame] | 211 | SkPath path; |
| 212 | path.moveTo(64, 3); |
| 213 | path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 214 | |
reed@google.com | a90aa53 | 2012-04-16 16:27:09 +0000 | [diff] [blame] | 215 | SkPaint paint; |
| 216 | paint.setAntiAlias(true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 217 | |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 218 | SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(640, 480)); |
reed@google.com | a90aa53 | 2012-04-16 16:27:09 +0000 | [diff] [blame] | 219 | canvas.get()->drawPath(path, paint); |
reed@google.com | a90aa53 | 2012-04-16 16:27:09 +0000 | [diff] [blame] | 220 | } |
| 221 | |
reed@google.com | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 222 | // we used to assert if the bounds of the device (clip) was larger than 32K |
| 223 | // even when the path itself was smaller. We just draw and hope in the debug |
| 224 | // version to not assert. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 225 | static void test_giantaa() { |
reed@google.com | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 226 | const int W = 400; |
| 227 | const int H = 400; |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 228 | SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(33000, 10)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 229 | |
reed@google.com | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 230 | SkPaint paint; |
| 231 | paint.setAntiAlias(true); |
| 232 | SkPath path; |
| 233 | path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H)); |
mike@reedtribe.org | 6093e65 | 2012-04-14 12:55:17 +0000 | [diff] [blame] | 234 | canvas.get()->drawPath(path, paint); |
reed@google.com | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 235 | } |
| 236 | |
fmalita@google.com | bfa0401 | 2012-12-12 22:13:58 +0000 | [diff] [blame] | 237 | // Extremely large path_length/dash_length ratios may cause infinite looping |
| 238 | // in SkDashPathEffect::filterPath() due to single precision rounding. |
| 239 | // The test is quite expensive, but it should get much faster after the fix |
| 240 | // for http://crbug.com/165432 goes in. |
| 241 | static void test_infinite_dash(skiatest::Reporter* reporter) { |
| 242 | SkPath path; |
| 243 | path.moveTo(0, 0); |
| 244 | path.lineTo(5000000, 0); |
| 245 | |
| 246 | SkScalar intervals[] = { 0.2f, 0.2f }; |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 247 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); |
fmalita@google.com | bfa0401 | 2012-12-12 22:13:58 +0000 | [diff] [blame] | 248 | |
| 249 | SkPath filteredPath; |
| 250 | SkPaint paint; |
| 251 | paint.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 252 | paint.setPathEffect(dash); |
fmalita@google.com | bfa0401 | 2012-12-12 22:13:58 +0000 | [diff] [blame] | 253 | |
| 254 | paint.getFillPath(path, &filteredPath); |
| 255 | // If we reach this, we passed. |
| 256 | REPORTER_ASSERT(reporter, true); |
| 257 | } |
| 258 | |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 259 | // http://crbug.com/165432 |
| 260 | // Limit extreme dash path effects to avoid exhausting the system memory. |
| 261 | static void test_crbug_165432(skiatest::Reporter* reporter) { |
| 262 | SkPath path; |
| 263 | path.moveTo(0, 0); |
| 264 | path.lineTo(10000000, 0); |
| 265 | |
| 266 | SkScalar intervals[] = { 0.5f, 0.5f }; |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 267 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 268 | |
| 269 | SkPaint paint; |
| 270 | paint.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 271 | paint.setPathEffect(dash); |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 272 | |
| 273 | SkPath filteredPath; |
| 274 | SkStrokeRec rec(paint); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 275 | REPORTER_ASSERT(reporter, !dash->filterPath(&filteredPath, path, &rec, NULL)); |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 276 | REPORTER_ASSERT(reporter, filteredPath.isEmpty()); |
| 277 | } |
| 278 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 279 | DEF_TEST(DrawPath, reporter) { |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 280 | test_giantaa(); |
| 281 | test_bug533(); |
| 282 | test_bigcubic(); |
| 283 | test_crbug_124652(); |
| 284 | test_crbug_140642(); |
| 285 | test_crbug_140803(); |
| 286 | test_inversepathwithclip(); |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 287 | // why? |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 288 | if (false) test_crbug131181(); |
fmalita@google.com | bfa0401 | 2012-12-12 22:13:58 +0000 | [diff] [blame] | 289 | test_infinite_dash(reporter); |
fmalita@google.com | 6b18d24 | 2012-12-17 16:27:34 +0000 | [diff] [blame] | 290 | test_crbug_165432(reporter); |
reed@google.com | 1c028bd | 2013-08-28 15:23:19 +0000 | [diff] [blame] | 291 | test_big_aa_rect(reporter); |
reed@google.com | dceecc7 | 2012-02-23 19:20:19 +0000 | [diff] [blame] | 292 | } |