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