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