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