blob: 5a692149b144f09d38407cd91e9ee97ca2f27f0c [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"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040010#include "SkColor.h"
reed@google.com1df888b2012-04-24 22:47:21 +000011#include "SkDashPathEffect.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040012#include "SkImageInfo.h"
13#include "SkMatrix.h"
14#include "SkPaint.h"
15#include "SkPath.h"
16#include "SkPathEffect.h"
17#include "SkPoint.h"
18#include "SkRRect.h"
19#include "SkRect.h"
20#include "SkRefCnt.h"
21#include "SkScalar.h"
halcanary435657f2015-09-15 12:53:07 -070022#include "SkStrokeRec.h"
reed@google.comf272e352013-08-26 21:27:03 +000023#include "SkSurface.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040024#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
reed@google.comdceecc72012-02-23 19:20:19 +000026
reed@google.comf272e352013-08-26 21:27:03 +000027// test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
28static void test_big_aa_rect(skiatest::Reporter* reporter) {
29 SkBitmap output;
30 SkPMColor pixel[1];
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000031 output.installPixels(SkImageInfo::MakeN32Premul(1, 1), pixel, 4);
reed@google.comf272e352013-08-26 21:27:03 +000032
reede8f30622016-03-23 18:59:25 -070033 auto surf = SkSurface::MakeRasterN32Premul(300, 33300);
reed@google.comf272e352013-08-26 21:27:03 +000034 SkCanvas* canvas = surf->getCanvas();
35
36 SkRect r = { 0, 33000, 300, 33300 };
37 int x = SkScalarRoundToInt(r.left());
38 int y = SkScalarRoundToInt(r.top());
39
40 // check that the pixel in question starts as transparent (by the surface)
Mike Reedf1942192017-07-21 14:24:29 -040041 if (surf->readPixels(output, x, y)) {
reed@google.comf272e352013-08-26 21:27:03 +000042 REPORTER_ASSERT(reporter, 0 == pixel[0]);
43 } else {
Brian Salomon1c80e992018-01-29 09:50:47 -050044 REPORTER_ASSERT(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000045 }
46
47 SkPaint paint;
48 paint.setAntiAlias(true);
49 paint.setColor(SK_ColorWHITE);
50
51 canvas->drawRect(r, paint);
52
53 // Now check that it is BLACK
Mike Reedf1942192017-07-21 14:24:29 -040054 if (surf->readPixels(output, x, y)) {
reed@google.comf272e352013-08-26 21:27:03 +000055 // don't know what swizzling PMColor did, but white should always
56 // appear the same.
57 REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
58 } else {
Brian Salomon1c80e992018-01-29 09:50:47 -050059 REPORTER_ASSERT(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000060 }
reed@google.comf272e352013-08-26 21:27:03 +000061}
62
63///////////////////////////////////////////////////////////////////////////////
64
reed@google.comb59ed512012-06-15 18:26:04 +000065static void moveToH(SkPath* path, const uint32_t raw[]) {
66 const float* fptr = (const float*)raw;
67 path->moveTo(fptr[0], fptr[1]);
68}
69
70static void cubicToH(SkPath* path, const uint32_t raw[]) {
71 const float* fptr = (const float*)raw;
72 path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]);
73}
74
75// This used to assert, because we performed a cast (int)(pt[0].fX * scale) to
76// arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert
77// was that the initial line-segment produced by the cubic was not monotonically
78// going down (i.e. the initial DY was negative). By rounding the floats, we get
79// the more proper result.
80//
81// http://code.google.com/p/chromium/issues/detail?id=131181
82//
humper@google.com05af1af2013-01-07 16:47:43 +000083
84// we're not calling this test anymore; is that for a reason?
85
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000086static void test_crbug131181() {
reed@google.comb59ed512012-06-15 18:26:04 +000087 /*
88 fX = 18.8943768,
89 fY = 129.121277
90 }, {
91 fX = 18.8937435,
92 fY = 129.121689
93 }, {
94 fX = 18.8950119,
95 fY = 129.120422
96 }, {
97 fX = 18.5030727,
98 fY = 129.13121
99 */
100 uint32_t data[] = {
101 0x419727af, 0x43011f0c, 0x41972663, 0x43011f27,
102 0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197
103 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104
reed@google.comb59ed512012-06-15 18:26:04 +0000105 SkPath path;
106 moveToH(&path, &data[0]);
107 cubicToH(&path, &data[2]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
reede8f30622016-03-23 18:59:25 -0700109 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110
reed@google.comb59ed512012-06-15 18:26:04 +0000111 SkPaint paint;
112 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800113 surface->getCanvas()->drawPath(path, paint);
reed@google.comb59ed512012-06-15 18:26:04 +0000114}
115
reed@google.come2faf172012-08-06 19:01:34 +0000116// This used to assert in debug builds (and crash writing bad memory in release)
117// because we overflowed an intermediate value (B coefficient) setting up our
118// 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 +0000119static void test_crbug_140803() {
reed@google.come2faf172012-08-06 19:01:34 +0000120 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000121 bm.allocN32Pixels(2700, 30*1024);
reed@google.come2faf172012-08-06 19:01:34 +0000122 SkCanvas canvas(bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123
reed@google.come2faf172012-08-06 19:01:34 +0000124 SkPath path;
125 path.moveTo(2762, 20);
126 path.quadTo(11, 21702, 10, 21706);
127 SkPaint paint;
128 paint.setAntiAlias(true);
129 canvas.drawPath(path, paint);
130}
131
reed@google.com9d5f76a2012-05-01 14:49:28 +0000132// Need to exercise drawing an inverse-path whose bounds intersect the clip,
133// but whose edges do not (since its a quad which draws only in the bottom half
134// of its bounds).
135// In the debug build, we used to assert in this case, until it was fixed.
136//
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000137static void test_inversepathwithclip() {
reed@google.com9d5f76a2012-05-01 14:49:28 +0000138 SkPath path;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139
reed3054be12014-12-10 07:24:28 -0800140 path.moveTo(0, 20);
141 path.quadTo(10, 10, 20, 20);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000142 path.toggleInverseFillType();
143
144 SkPaint paint;
145
reede8f30622016-03-23 18:59:25 -0700146 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed3054be12014-12-10 07:24:28 -0800147 SkCanvas* canvas = surface->getCanvas();
148 canvas->save();
149 canvas->clipRect(SkRect::MakeWH(19, 11));
reed@google.com9d5f76a2012-05-01 14:49:28 +0000150
151 paint.setAntiAlias(false);
reed3054be12014-12-10 07:24:28 -0800152 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000153 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800154 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000155
reed3054be12014-12-10 07:24:28 -0800156 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000157
reed@google.com9d5f76a2012-05-01 14:49:28 +0000158 // Now do the test again, with the path flipped, so we only draw in the
159 // top half of our bounds, and have the clip intersect our bounds at the
160 // bottom.
161 path.reset(); // preserves our filltype
reed3054be12014-12-10 07:24:28 -0800162 path.moveTo(0, 10);
163 path.quadTo(10, 20, 20, 10);
164 canvas->clipRect(SkRect::MakeXYWH(0, 19, 19, 11));
reed@google.com9d5f76a2012-05-01 14:49:28 +0000165
166 paint.setAntiAlias(false);
reed3054be12014-12-10 07:24:28 -0800167 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000168 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800169 canvas->drawPath(path, paint);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000170}
171
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000172static void test_bug533() {
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000173 /*
174 http://code.google.com/p/skia/issues/detail?id=533
175 This particular test/bug only applies to the float case, where the
176 coordinates are very large.
177 */
178 SkPath path;
179 path.moveTo(64, 3);
180 path.quadTo(-329936, -100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000182 SkPaint paint;
183 paint.setAntiAlias(true);
184
reede8f30622016-03-23 18:59:25 -0700185 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed3054be12014-12-10 07:24:28 -0800186 surface->getCanvas()->drawPath(path, paint);
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000187}
188
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000189static void test_crbug_140642() {
reed@google.comd9ee3482012-08-06 14:58:35 +0000190 /*
191 * We used to see this construct, and due to rounding as we accumulated
192 * our length, the loop where we apply the phase would run off the end of
193 * the array, since it relied on just -= each interval value, which did not
194 * behave as "expected". Now the code explicitly checks for walking off the
195 * end of that array.
196
197 * A different (better) fix might be to rewrite dashing to do all of its
198 * length/phase/measure math using double, but this may need to be
199 * coordinated with SkPathMeasure, to be consistent between the two.
200
201 <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
202 stroke-dashoffset="-248.135982067">
203 */
204
reed@google.comd9ee3482012-08-06 14:58:35 +0000205 const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
reeda4393342016-03-18 11:22:57 -0700206 auto dontAssert = SkDashPathEffect::Make(vals, 4, -248.135982067f);
reed@google.comd9ee3482012-08-06 14:58:35 +0000207}
208
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000209static void test_crbug_124652() {
reed@google.com1df888b2012-04-24 22:47:21 +0000210 /*
211 http://code.google.com/p/chromium/issues/detail?id=124652
212 This particular test/bug only applies to the float case, where
213 large values can "swamp" small ones.
214 */
215 SkScalar intervals[2] = {837099584, 33450};
reeda4393342016-03-18 11:22:57 -0700216 auto dontAssert = SkDashPathEffect::Make(intervals, 2, -10);
reed@google.com1df888b2012-04-24 22:47:21 +0000217}
218
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000219static void test_bigcubic() {
reed@google.coma90aa532012-04-16 16:27:09 +0000220 SkPath path;
221 path.moveTo(64, 3);
222 path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000223
reed@google.coma90aa532012-04-16 16:27:09 +0000224 SkPaint paint;
225 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226
reede8f30622016-03-23 18:59:25 -0700227 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed3054be12014-12-10 07:24:28 -0800228 surface->getCanvas()->drawPath(path, paint);
reed@google.coma90aa532012-04-16 16:27:09 +0000229}
230
caryclark6df61152016-01-04 14:17:47 -0800231// asserts if halfway case is not handled
232static void test_halfway() {
233 SkPaint paint;
234 SkPath path;
235 path.moveTo(16365.5f, 1394);
236 path.lineTo(16365.5f, 1387.5f);
237 path.quadTo(16365.5f, 1385.43f, 16367, 1383.96f);
238 path.quadTo(16368.4f, 1382.5f, 16370.5f, 1382.5f);
239 path.lineTo(16465.5f, 1382.5f);
240 path.quadTo(16467.6f, 1382.5f, 16469, 1383.96f);
241 path.quadTo(16470.5f, 1385.43f, 16470.5f, 1387.5f);
242 path.lineTo(16470.5f, 1394);
243 path.quadTo(16470.5f, 1396.07f, 16469, 1397.54f);
244 path.quadTo(16467.6f, 1399, 16465.5f, 1399);
245 path.lineTo(16370.5f, 1399);
246 path.quadTo(16368.4f, 1399, 16367, 1397.54f);
247 path.quadTo(16365.5f, 1396.07f, 16365.5f, 1394);
248 path.close();
249 SkPath p2;
250 SkMatrix m;
251 m.reset();
252 m.postTranslate(0.001f, 0.001f);
253 path.transform(m, &p2);
254
reede8f30622016-03-23 18:59:25 -0700255 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
caryclark6df61152016-01-04 14:17:47 -0800256 SkCanvas* canvas = surface->getCanvas();
257 canvas->translate(-16366, -1383);
258 canvas->drawPath(p2, paint);
259
260 m.reset();
261 m.postTranslate(-0.001f, -0.001f);
262 path.transform(m, &p2);
263 canvas->drawPath(p2, paint);
264
265 m.reset();
266 path.transform(m, &p2);
267 canvas->drawPath(p2, paint);
268}
269
reed@google.comdceecc72012-02-23 19:20:19 +0000270// we used to assert if the bounds of the device (clip) was larger than 32K
271// even when the path itself was smaller. We just draw and hope in the debug
272// version to not assert.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000273static void test_giantaa() {
reed@google.comdceecc72012-02-23 19:20:19 +0000274 const int W = 400;
275 const int H = 400;
reede8f30622016-03-23 18:59:25 -0700276 auto surface(SkSurface::MakeRasterN32Premul(33000, 10));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000277
reed@google.comdceecc72012-02-23 19:20:19 +0000278 SkPaint paint;
279 paint.setAntiAlias(true);
280 SkPath path;
281 path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
reed3054be12014-12-10 07:24:28 -0800282 surface->getCanvas()->drawPath(path, paint);
reed@google.comdceecc72012-02-23 19:20:19 +0000283}
284
fmalita@google.combfa04012012-12-12 22:13:58 +0000285// Extremely large path_length/dash_length ratios may cause infinite looping
286// in SkDashPathEffect::filterPath() due to single precision rounding.
287// The test is quite expensive, but it should get much faster after the fix
288// for http://crbug.com/165432 goes in.
289static void test_infinite_dash(skiatest::Reporter* reporter) {
290 SkPath path;
291 path.moveTo(0, 0);
292 path.lineTo(5000000, 0);
293
294 SkScalar intervals[] = { 0.2f, 0.2f };
reeda4393342016-03-18 11:22:57 -0700295 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
fmalita@google.combfa04012012-12-12 22:13:58 +0000296
297 SkPath filteredPath;
298 SkPaint paint;
299 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000300 paint.setPathEffect(dash);
fmalita@google.combfa04012012-12-12 22:13:58 +0000301
302 paint.getFillPath(path, &filteredPath);
303 // If we reach this, we passed.
304 REPORTER_ASSERT(reporter, true);
305}
306
fmalita@google.com6b18d242012-12-17 16:27:34 +0000307// http://crbug.com/165432
308// Limit extreme dash path effects to avoid exhausting the system memory.
309static void test_crbug_165432(skiatest::Reporter* reporter) {
310 SkPath path;
311 path.moveTo(0, 0);
312 path.lineTo(10000000, 0);
313
314 SkScalar intervals[] = { 0.5f, 0.5f };
reeda4393342016-03-18 11:22:57 -0700315 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000316
317 SkPaint paint;
318 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000319 paint.setPathEffect(dash);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000320
321 SkPath filteredPath;
322 SkStrokeRec rec(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700323 REPORTER_ASSERT(reporter, !dash->filterPath(&filteredPath, path, &rec, nullptr));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000324 REPORTER_ASSERT(reporter, filteredPath.isEmpty());
325}
326
herb7cf12dd2016-01-11 08:08:56 -0800327// http://crbug.com/472147
328// This is a simplified version from the bug. RRect radii not properly scaled.
329static void test_crbug_472147_simple(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700330 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
herb7cf12dd2016-01-11 08:08:56 -0800331 SkCanvas* canvas = surface->getCanvas();
332 SkPaint p;
333 SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
334 SkVector radii[4] = {
335 { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
336 };
337 SkRRect rr;
338 rr.setRectRadii(r, radii);
339 canvas->drawRRect(rr, p);
340}
341
342// http://crbug.com/472147
343// RRect radii not properly scaled.
344static void test_crbug_472147_actual(skiatest::Reporter* reporter) {
reede8f30622016-03-23 18:59:25 -0700345 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
herb7cf12dd2016-01-11 08:08:56 -0800346 SkCanvas* canvas = surface->getCanvas();
347 SkPaint p;
348 SkRect r = SkRect::MakeLTRB(-246.0f, 33.0f, 848.0f, 33554464.0f);
349 SkVector radii[4] = {
350 { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554430.0f }, { 120.0f, 5.0f }
351 };
352 SkRRect rr;
353 rr.setRectRadii(r, radii);
reed73603f32016-09-20 08:42:38 -0700354 canvas->clipRRect(rr);
herb7cf12dd2016-01-11 08:08:56 -0800355
356 SkRect r2 = SkRect::MakeLTRB(0, 33, 1102, 33554464);
357 canvas->drawRect(r2, p);
358}
359
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000360DEF_TEST(DrawPath, reporter) {
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000361 test_giantaa();
362 test_bug533();
363 test_bigcubic();
364 test_crbug_124652();
365 test_crbug_140642();
366 test_crbug_140803();
367 test_inversepathwithclip();
humper@google.com05af1af2013-01-07 16:47:43 +0000368 // why?
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000369 if (false) test_crbug131181();
fmalita@google.combfa04012012-12-12 22:13:58 +0000370 test_infinite_dash(reporter);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000371 test_crbug_165432(reporter);
herb7cf12dd2016-01-11 08:08:56 -0800372 test_crbug_472147_simple(reporter);
373 test_crbug_472147_actual(reporter);
reed@google.com1c028bd2013-08-28 15:23:19 +0000374 test_big_aa_rect(reporter);
caryclark6df61152016-01-04 14:17:47 -0800375 test_halfway();
reed@google.comdceecc72012-02-23 19:20:19 +0000376}