blob: 3893981c0053c5436d121a0c915d96c3f71be6d0 [file] [log] [blame]
reed@google.comdceecc72012-02-23 19:20:19 +00001/*
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.comdceecc72012-02-23 19:20:19 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
reed@google.com1df888b2012-04-24 22:47:21 +000010#include "SkDashPathEffect.h"
halcanary435657f2015-09-15 12:53:07 -070011#include "SkStrokeRec.h"
reed@google.comf272e352013-08-26 21:27:03 +000012#include "SkSurface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
reed@google.comdceecc72012-02-23 19:20:19 +000014
reed@google.comf272e352013-08-26 21:27:03 +000015// test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
16static void test_big_aa_rect(skiatest::Reporter* reporter) {
17 SkBitmap output;
18 SkPMColor pixel[1];
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000019 output.installPixels(SkImageInfo::MakeN32Premul(1, 1), pixel, 4);
reed@google.comf272e352013-08-26 21:27:03 +000020
reede8f30622016-03-23 18:59:25 -070021 auto surf = SkSurface::MakeRasterN32Premul(300, 33300);
reed@google.comf272e352013-08-26 21:27:03 +000022 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)
Mike Reedf1942192017-07-21 14:24:29 -040029 if (surf->readPixels(output, x, y)) {
reed@google.comf272e352013-08-26 21:27:03 +000030 REPORTER_ASSERT(reporter, 0 == pixel[0]);
31 } else {
mtklein@google.com9f842d32013-08-27 16:15:37 +000032 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000033 }
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
Mike Reedf1942192017-07-21 14:24:29 -040042 if (surf->readPixels(output, x, y)) {
reed@google.comf272e352013-08-26 21:27:03 +000043 // 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.com9f842d32013-08-27 16:15:37 +000047 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000048 }
reed@google.comf272e352013-08-26 21:27:03 +000049}
50
51///////////////////////////////////////////////////////////////////////////////
52
reed@google.comb59ed512012-06-15 18:26:04 +000053static void moveToH(SkPath* path, const uint32_t raw[]) {
54 const float* fptr = (const float*)raw;
55 path->moveTo(fptr[0], fptr[1]);
56}
57
58static void cubicToH(SkPath* path, const uint32_t raw[]) {
59 const float* fptr = (const float*)raw;
60 path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]);
61}
62
63// This used to assert, because we performed a cast (int)(pt[0].fX * scale) to
64// arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert
65// was that the initial line-segment produced by the cubic was not monotonically
66// going down (i.e. the initial DY was negative). By rounding the floats, we get
67// the more proper result.
68//
69// http://code.google.com/p/chromium/issues/detail?id=131181
70//
humper@google.com05af1af2013-01-07 16:47:43 +000071
72// we're not calling this test anymore; is that for a reason?
73
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000074static void test_crbug131181() {
reed@google.comb59ed512012-06-15 18:26:04 +000075 /*
76 fX = 18.8943768,
77 fY = 129.121277
78 }, {
79 fX = 18.8937435,
80 fY = 129.121689
81 }, {
82 fX = 18.8950119,
83 fY = 129.120422
84 }, {
85 fX = 18.5030727,
86 fY = 129.13121
87 */
88 uint32_t data[] = {
89 0x419727af, 0x43011f0c, 0x41972663, 0x43011f27,
90 0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197
91 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
reed@google.comb59ed512012-06-15 18:26:04 +000093 SkPath path;
94 moveToH(&path, &data[0]);
95 cubicToH(&path, &data[2]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000096
reede8f30622016-03-23 18:59:25 -070097 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
reed@google.comb59ed512012-06-15 18:26:04 +000099 SkPaint paint;
100 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800101 surface->getCanvas()->drawPath(path, paint);
reed@google.comb59ed512012-06-15 18:26:04 +0000102}
103
reed@google.come2faf172012-08-06 19:01:34 +0000104// This used to assert in debug builds (and crash writing bad memory in release)
105// because we overflowed an intermediate value (B coefficient) setting up our
106// stepper for the quadratic. Now we bias that value by 1/2 so we don't overflow
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000107static void test_crbug_140803() {
reed@google.come2faf172012-08-06 19:01:34 +0000108 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000109 bm.allocN32Pixels(2700, 30*1024);
reed@google.come2faf172012-08-06 19:01:34 +0000110 SkCanvas canvas(bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111
reed@google.come2faf172012-08-06 19:01:34 +0000112 SkPath path;
113 path.moveTo(2762, 20);
114 path.quadTo(11, 21702, 10, 21706);
115 SkPaint paint;
116 paint.setAntiAlias(true);
117 canvas.drawPath(path, paint);
118}
119
reed@google.com9d5f76a2012-05-01 14:49:28 +0000120// Need to exercise drawing an inverse-path whose bounds intersect the clip,
121// but whose edges do not (since its a quad which draws only in the bottom half
122// of its bounds).
123// In the debug build, we used to assert in this case, until it was fixed.
124//
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000125static void test_inversepathwithclip() {
reed@google.com9d5f76a2012-05-01 14:49:28 +0000126 SkPath path;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
reed3054be12014-12-10 07:24:28 -0800128 path.moveTo(0, 20);
129 path.quadTo(10, 10, 20, 20);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000130 path.toggleInverseFillType();
131
132 SkPaint paint;
133
reede8f30622016-03-23 18:59:25 -0700134 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed3054be12014-12-10 07:24:28 -0800135 SkCanvas* canvas = surface->getCanvas();
136 canvas->save();
137 canvas->clipRect(SkRect::MakeWH(19, 11));
reed@google.com9d5f76a2012-05-01 14:49:28 +0000138
139 paint.setAntiAlias(false);
reed3054be12014-12-10 07:24:28 -0800140 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000141 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800142 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000143
reed3054be12014-12-10 07:24:28 -0800144 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145
reed@google.com9d5f76a2012-05-01 14:49:28 +0000146 // Now do the test again, with the path flipped, so we only draw in the
147 // top half of our bounds, and have the clip intersect our bounds at the
148 // bottom.
149 path.reset(); // preserves our filltype
reed3054be12014-12-10 07:24:28 -0800150 path.moveTo(0, 10);
151 path.quadTo(10, 20, 20, 10);
152 canvas->clipRect(SkRect::MakeXYWH(0, 19, 19, 11));
reed@google.com9d5f76a2012-05-01 14:49:28 +0000153
154 paint.setAntiAlias(false);
reed3054be12014-12-10 07:24:28 -0800155 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000156 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800157 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000158}
159
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000160static void test_bug533() {
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000161 /*
162 http://code.google.com/p/skia/issues/detail?id=533
163 This particular test/bug only applies to the float case, where the
164 coordinates are very large.
165 */
166 SkPath path;
167 path.moveTo(64, 3);
168 path.quadTo(-329936, -100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000169
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000170 SkPaint paint;
171 paint.setAntiAlias(true);
172
reede8f30622016-03-23 18:59:25 -0700173 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed3054be12014-12-10 07:24:28 -0800174 surface->getCanvas()->drawPath(path, paint);
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000175}
176
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000177static void test_crbug_140642() {
reed@google.comd9ee3482012-08-06 14:58:35 +0000178 /*
179 * We used to see this construct, and due to rounding as we accumulated
180 * our length, the loop where we apply the phase would run off the end of
181 * the array, since it relied on just -= each interval value, which did not
182 * behave as "expected". Now the code explicitly checks for walking off the
183 * end of that array.
184
185 * A different (better) fix might be to rewrite dashing to do all of its
186 * length/phase/measure math using double, but this may need to be
187 * coordinated with SkPathMeasure, to be consistent between the two.
188
189 <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
190 stroke-dashoffset="-248.135982067">
191 */
192
reed@google.comd9ee3482012-08-06 14:58:35 +0000193 const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
reeda4393342016-03-18 11:22:57 -0700194 auto dontAssert = SkDashPathEffect::Make(vals, 4, -248.135982067f);
reed@google.comd9ee3482012-08-06 14:58:35 +0000195}
196
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000197static void test_crbug_124652() {
reed@google.com1df888b2012-04-24 22:47:21 +0000198 /*
199 http://code.google.com/p/chromium/issues/detail?id=124652
200 This particular test/bug only applies to the float case, where
201 large values can "swamp" small ones.
202 */
203 SkScalar intervals[2] = {837099584, 33450};
reeda4393342016-03-18 11:22:57 -0700204 auto dontAssert = SkDashPathEffect::Make(intervals, 2, -10);
reed@google.com1df888b2012-04-24 22:47:21 +0000205}
206
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000207static void test_bigcubic() {
reed@google.coma90aa532012-04-16 16:27:09 +0000208 SkPath path;
209 path.moveTo(64, 3);
210 path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000211
reed@google.coma90aa532012-04-16 16:27:09 +0000212 SkPaint paint;
213 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000214
reede8f30622016-03-23 18:59:25 -0700215 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed3054be12014-12-10 07:24:28 -0800216 surface->getCanvas()->drawPath(path, paint);
reed@google.coma90aa532012-04-16 16:27:09 +0000217}
218
caryclark6df61152016-01-04 14:17:47 -0800219// asserts if halfway case is not handled
220static void test_halfway() {
221 SkPaint paint;
222 SkPath path;
223 path.moveTo(16365.5f, 1394);
224 path.lineTo(16365.5f, 1387.5f);
225 path.quadTo(16365.5f, 1385.43f, 16367, 1383.96f);
226 path.quadTo(16368.4f, 1382.5f, 16370.5f, 1382.5f);
227 path.lineTo(16465.5f, 1382.5f);
228 path.quadTo(16467.6f, 1382.5f, 16469, 1383.96f);
229 path.quadTo(16470.5f, 1385.43f, 16470.5f, 1387.5f);
230 path.lineTo(16470.5f, 1394);
231 path.quadTo(16470.5f, 1396.07f, 16469, 1397.54f);
232 path.quadTo(16467.6f, 1399, 16465.5f, 1399);
233 path.lineTo(16370.5f, 1399);
234 path.quadTo(16368.4f, 1399, 16367, 1397.54f);
235 path.quadTo(16365.5f, 1396.07f, 16365.5f, 1394);
236 path.close();
237 SkPath p2;
238 SkMatrix m;
239 m.reset();
240 m.postTranslate(0.001f, 0.001f);
241 path.transform(m, &p2);
242
reede8f30622016-03-23 18:59:25 -0700243 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
caryclark6df61152016-01-04 14:17:47 -0800244 SkCanvas* canvas = surface->getCanvas();
245 canvas->translate(-16366, -1383);
246 canvas->drawPath(p2, paint);
247
248 m.reset();
249 m.postTranslate(-0.001f, -0.001f);
250 path.transform(m, &p2);
251 canvas->drawPath(p2, paint);
252
253 m.reset();
254 path.transform(m, &p2);
255 canvas->drawPath(p2, paint);
256}
257
reed@google.comdceecc72012-02-23 19:20:19 +0000258// we used to assert if the bounds of the device (clip) was larger than 32K
259// even when the path itself was smaller. We just draw and hope in the debug
260// version to not assert.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000261static void test_giantaa() {
reed@google.comdceecc72012-02-23 19:20:19 +0000262 const int W = 400;
263 const int H = 400;
reede8f30622016-03-23 18:59:25 -0700264 auto surface(SkSurface::MakeRasterN32Premul(33000, 10));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000265
reed@google.comdceecc72012-02-23 19:20:19 +0000266 SkPaint paint;
267 paint.setAntiAlias(true);
268 SkPath path;
269 path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
reed3054be12014-12-10 07:24:28 -0800270 surface->getCanvas()->drawPath(path, paint);
reed@google.comdceecc72012-02-23 19:20:19 +0000271}
272
fmalita@google.combfa04012012-12-12 22:13:58 +0000273// Extremely large path_length/dash_length ratios may cause infinite looping
274// in SkDashPathEffect::filterPath() due to single precision rounding.
275// The test is quite expensive, but it should get much faster after the fix
276// for http://crbug.com/165432 goes in.
277static void test_infinite_dash(skiatest::Reporter* reporter) {
278 SkPath path;
279 path.moveTo(0, 0);
280 path.lineTo(5000000, 0);
281
282 SkScalar intervals[] = { 0.2f, 0.2f };
reeda4393342016-03-18 11:22:57 -0700283 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
fmalita@google.combfa04012012-12-12 22:13:58 +0000284
285 SkPath filteredPath;
286 SkPaint paint;
287 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000288 paint.setPathEffect(dash);
fmalita@google.combfa04012012-12-12 22:13:58 +0000289
290 paint.getFillPath(path, &filteredPath);
291 // If we reach this, we passed.
292 REPORTER_ASSERT(reporter, true);
293}
294
fmalita@google.com6b18d242012-12-17 16:27:34 +0000295// http://crbug.com/165432
296// Limit extreme dash path effects to avoid exhausting the system memory.
297static void test_crbug_165432(skiatest::Reporter* reporter) {
298 SkPath path;
299 path.moveTo(0, 0);
300 path.lineTo(10000000, 0);
301
302 SkScalar intervals[] = { 0.5f, 0.5f };
reeda4393342016-03-18 11:22:57 -0700303 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000304
305 SkPaint paint;
306 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000307 paint.setPathEffect(dash);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000308
309 SkPath filteredPath;
310 SkStrokeRec rec(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700311 REPORTER_ASSERT(reporter, !dash->filterPath(&filteredPath, path, &rec, nullptr));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000312 REPORTER_ASSERT(reporter, filteredPath.isEmpty());
313}
314
herb7cf12dd2016-01-11 08:08:56 -0800315// http://crbug.com/472147
316// This is a simplified version from the bug. RRect radii not properly scaled.
317static void test_crbug_472147_simple(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700318 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
herb7cf12dd2016-01-11 08:08:56 -0800319 SkCanvas* canvas = surface->getCanvas();
320 SkPaint p;
321 SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
322 SkVector radii[4] = {
323 { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
324 };
325 SkRRect rr;
326 rr.setRectRadii(r, radii);
327 canvas->drawRRect(rr, p);
328}
329
330// http://crbug.com/472147
331// RRect radii not properly scaled.
332static void test_crbug_472147_actual(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700333 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
herb7cf12dd2016-01-11 08:08:56 -0800334 SkCanvas* canvas = surface->getCanvas();
335 SkPaint p;
336 SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
337 SkVector radii[4] = {
338 { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
339 };
340 SkRRect rr;
341 rr.setRectRadii(r, radii);
reed73603f32016-09-20 08:42:38 -0700342 canvas->clipRRect(rr);
herb7cf12dd2016-01-11 08:08:56 -0800343
344 SkRect r2 = SkRect::MakeLTRB(0, 33, 1102, 33554464);
345 canvas->drawRect(r2, p);
346}
347
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000348DEF_TEST(DrawPath, reporter) {
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000349 test_giantaa();
350 test_bug533();
351 test_bigcubic();
352 test_crbug_124652();
353 test_crbug_140642();
354 test_crbug_140803();
355 test_inversepathwithclip();
humper@google.com05af1af2013-01-07 16:47:43 +0000356 // why?
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000357 if (false) test_crbug131181();
fmalita@google.combfa04012012-12-12 22:13:58 +0000358 test_infinite_dash(reporter);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000359 test_crbug_165432(reporter);
herb7cf12dd2016-01-11 08:08:56 -0800360 test_crbug_472147_simple(reporter);
361 test_crbug_472147_actual(reporter);
reed@google.com1c028bd2013-08-28 15:23:19 +0000362 test_big_aa_rect(reporter);
caryclark6df61152016-01-04 14:17:47 -0800363 test_halfway();
reed@google.comdceecc72012-02-23 19:20:19 +0000364}