blob: 2af4224c9c4cfaa51c0d885a319dc96e64027b12 [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"
reed@google.comf272e352013-08-26 21:27:03 +000011#include "SkSurface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
reed@google.comdceecc72012-02-23 19:20:19 +000013
reed@google.comf272e352013-08-26 21:27:03 +000014// test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
15static void test_big_aa_rect(skiatest::Reporter* reporter) {
16 SkBitmap output;
17 SkPMColor pixel[1];
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000018 output.installPixels(SkImageInfo::MakeN32Premul(1, 1), pixel, 4);
reed@google.comf272e352013-08-26 21:27:03 +000019
reed@google.comd28ba802013-09-20 19:33:52 +000020 SkSurface* surf = SkSurface::NewRasterPMColor(300, 33300);
reed@google.comf272e352013-08-26 21:27:03 +000021 SkCanvas* canvas = surf->getCanvas();
22
23 SkRect r = { 0, 33000, 300, 33300 };
24 int x = SkScalarRoundToInt(r.left());
25 int y = SkScalarRoundToInt(r.top());
26
27 // check that the pixel in question starts as transparent (by the surface)
28 if (canvas->readPixels(&output, x, y)) {
29 REPORTER_ASSERT(reporter, 0 == pixel[0]);
30 } else {
mtklein@google.com9f842d32013-08-27 16:15:37 +000031 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000032 }
33
34 SkPaint paint;
35 paint.setAntiAlias(true);
36 paint.setColor(SK_ColorWHITE);
37
38 canvas->drawRect(r, paint);
39
40 // Now check that it is BLACK
41 if (canvas->readPixels(&output, x, y)) {
42 // don't know what swizzling PMColor did, but white should always
43 // appear the same.
44 REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
45 } else {
mtklein@google.com9f842d32013-08-27 16:15:37 +000046 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000047 }
48 surf->unref();
49}
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
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +000097 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(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);
101 canvas->drawPath(path, paint);
102}
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
reed@google.com9d5f76a2012-05-01 14:49:28 +0000128 path.moveTo(0, SkIntToScalar(20));
129 path.quadTo(SkIntToScalar(10), SkIntToScalar(10),
130 SkIntToScalar(20), SkIntToScalar(20));
131 path.toggleInverseFillType();
132
133 SkPaint paint;
134
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000135 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(640, 480));
reed@google.com9d5f76a2012-05-01 14:49:28 +0000136 canvas.get()->save();
137 canvas.get()->clipRect(SkRect::MakeWH(SkIntToScalar(19), SkIntToScalar(11)));
138
139 paint.setAntiAlias(false);
140 canvas.get()->drawPath(path, paint);
141 paint.setAntiAlias(true);
142 canvas.get()->drawPath(path, paint);
143
144 canvas.get()->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
150 path.moveTo(0, SkIntToScalar(10));
151 path.quadTo(SkIntToScalar(10), SkIntToScalar(20),
152 SkIntToScalar(20), SkIntToScalar(10));
153 canvas.get()->clipRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(19),
154 SkIntToScalar(19), SkIntToScalar(11)));
155
156 paint.setAntiAlias(false);
157 canvas.get()->drawPath(path, paint);
158 paint.setAntiAlias(true);
159 canvas.get()->drawPath(path, paint);
160}
161
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000162static void test_bug533() {
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000163 /*
164 http://code.google.com/p/skia/issues/detail?id=533
165 This particular test/bug only applies to the float case, where the
166 coordinates are very large.
167 */
168 SkPath path;
169 path.moveTo(64, 3);
170 path.quadTo(-329936, -100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000171
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000172 SkPaint paint;
173 paint.setAntiAlias(true);
174
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000175 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(640, 480));
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000176 canvas.get()->drawPath(path, paint);
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000177}
178
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000179static void test_crbug_140642() {
reed@google.comd9ee3482012-08-06 14:58:35 +0000180 /*
181 * We used to see this construct, and due to rounding as we accumulated
182 * our length, the loop where we apply the phase would run off the end of
183 * the array, since it relied on just -= each interval value, which did not
184 * behave as "expected". Now the code explicitly checks for walking off the
185 * end of that array.
186
187 * A different (better) fix might be to rewrite dashing to do all of its
188 * length/phase/measure math using double, but this may need to be
189 * coordinated with SkPathMeasure, to be consistent between the two.
190
191 <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
192 stroke-dashoffset="-248.135982067">
193 */
194
reed@google.comd9ee3482012-08-06 14:58:35 +0000195 const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000196 SkAutoTUnref<SkDashPathEffect> dontAssert(SkDashPathEffect::Create(vals, 4, -248.135982067f));
reed@google.comd9ee3482012-08-06 14:58:35 +0000197}
198
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000199static void test_crbug_124652() {
reed@google.com1df888b2012-04-24 22:47:21 +0000200 /*
201 http://code.google.com/p/chromium/issues/detail?id=124652
202 This particular test/bug only applies to the float case, where
203 large values can "swamp" small ones.
204 */
205 SkScalar intervals[2] = {837099584, 33450};
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000206 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, -10));
reed@google.com1df888b2012-04-24 22:47:21 +0000207}
208
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000209static void test_bigcubic() {
reed@google.coma90aa532012-04-16 16:27:09 +0000210 SkPath path;
211 path.moveTo(64, 3);
212 path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000213
reed@google.coma90aa532012-04-16 16:27:09 +0000214 SkPaint paint;
215 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000216
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000217 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(640, 480));
reed@google.coma90aa532012-04-16 16:27:09 +0000218 canvas.get()->drawPath(path, paint);
reed@google.coma90aa532012-04-16 16:27:09 +0000219}
220
reed@google.comdceecc72012-02-23 19:20:19 +0000221// we used to assert if the bounds of the device (clip) was larger than 32K
222// even when the path itself was smaller. We just draw and hope in the debug
223// version to not assert.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000224static void test_giantaa() {
reed@google.comdceecc72012-02-23 19:20:19 +0000225 const int W = 400;
226 const int H = 400;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000227 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterN32(33000, 10));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228
reed@google.comdceecc72012-02-23 19:20:19 +0000229 SkPaint paint;
230 paint.setAntiAlias(true);
231 SkPath path;
232 path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000233 canvas.get()->drawPath(path, paint);
reed@google.comdceecc72012-02-23 19:20:19 +0000234}
235
fmalita@google.combfa04012012-12-12 22:13:58 +0000236// Extremely large path_length/dash_length ratios may cause infinite looping
237// in SkDashPathEffect::filterPath() due to single precision rounding.
238// The test is quite expensive, but it should get much faster after the fix
239// for http://crbug.com/165432 goes in.
240static void test_infinite_dash(skiatest::Reporter* reporter) {
241 SkPath path;
242 path.moveTo(0, 0);
243 path.lineTo(5000000, 0);
244
245 SkScalar intervals[] = { 0.2f, 0.2f };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000246 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
fmalita@google.combfa04012012-12-12 22:13:58 +0000247
248 SkPath filteredPath;
249 SkPaint paint;
250 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000251 paint.setPathEffect(dash);
fmalita@google.combfa04012012-12-12 22:13:58 +0000252
253 paint.getFillPath(path, &filteredPath);
254 // If we reach this, we passed.
255 REPORTER_ASSERT(reporter, true);
256}
257
fmalita@google.com6b18d242012-12-17 16:27:34 +0000258// http://crbug.com/165432
259// Limit extreme dash path effects to avoid exhausting the system memory.
260static void test_crbug_165432(skiatest::Reporter* reporter) {
261 SkPath path;
262 path.moveTo(0, 0);
263 path.lineTo(10000000, 0);
264
265 SkScalar intervals[] = { 0.5f, 0.5f };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000266 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000267
268 SkPaint paint;
269 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000270 paint.setPathEffect(dash);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000271
272 SkPath filteredPath;
273 SkStrokeRec rec(paint);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000274 REPORTER_ASSERT(reporter, !dash->filterPath(&filteredPath, path, &rec, NULL));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000275 REPORTER_ASSERT(reporter, filteredPath.isEmpty());
276}
277
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000278DEF_TEST(DrawPath, reporter) {
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000279 test_giantaa();
280 test_bug533();
281 test_bigcubic();
282 test_crbug_124652();
283 test_crbug_140642();
284 test_crbug_140803();
285 test_inversepathwithclip();
humper@google.com05af1af2013-01-07 16:47:43 +0000286 // why?
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000287 if (false) test_crbug131181();
fmalita@google.combfa04012012-12-12 22:13:58 +0000288 test_infinite_dash(reporter);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000289 test_crbug_165432(reporter);
reed@google.com1c028bd2013-08-28 15:23:19 +0000290 test_big_aa_rect(reporter);
reed@google.comdceecc72012-02-23 19:20:19 +0000291}