blob: e7f70057b9a8be01aa2b48afdd5bc0e615996d16 [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
mike@reedtribe.org6093e652012-04-14 12:55:17 +000014static SkCanvas* new_canvas(int w, int h) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000015 SkBitmap bm;
16 bm.allocN32Pixels(w, h);
17 return new SkCanvas(bm);
mike@reedtribe.org6093e652012-04-14 12:55:17 +000018}
19
reed@google.coma90aa532012-04-16 16:27:09 +000020///////////////////////////////////////////////////////////////////////////////
21
reed@google.comf272e352013-08-26 21:27:03 +000022// test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
23static void test_big_aa_rect(skiatest::Reporter* reporter) {
24 SkBitmap output;
25 SkPMColor pixel[1];
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000026 output.installPixels(SkImageInfo::MakeN32Premul(1, 1),
27 pixel, 4, NULL, NULL);
reed@google.comf272e352013-08-26 21:27:03 +000028
reed@google.comd28ba802013-09-20 19:33:52 +000029 SkSurface* surf = SkSurface::NewRasterPMColor(300, 33300);
reed@google.comf272e352013-08-26 21:27:03 +000030 SkCanvas* canvas = surf->getCanvas();
31
32 SkRect r = { 0, 33000, 300, 33300 };
33 int x = SkScalarRoundToInt(r.left());
34 int y = SkScalarRoundToInt(r.top());
35
36 // check that the pixel in question starts as transparent (by the surface)
37 if (canvas->readPixels(&output, x, y)) {
38 REPORTER_ASSERT(reporter, 0 == pixel[0]);
39 } else {
mtklein@google.com9f842d32013-08-27 16:15:37 +000040 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000041 }
42
43 SkPaint paint;
44 paint.setAntiAlias(true);
45 paint.setColor(SK_ColorWHITE);
46
47 canvas->drawRect(r, paint);
48
49 // Now check that it is BLACK
50 if (canvas->readPixels(&output, x, y)) {
51 // don't know what swizzling PMColor did, but white should always
52 // appear the same.
53 REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
54 } else {
mtklein@google.com9f842d32013-08-27 16:15:37 +000055 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000056 }
57 surf->unref();
58}
59
60///////////////////////////////////////////////////////////////////////////////
61
reed@google.comb59ed512012-06-15 18:26:04 +000062static void moveToH(SkPath* path, const uint32_t raw[]) {
63 const float* fptr = (const float*)raw;
64 path->moveTo(fptr[0], fptr[1]);
65}
66
67static void cubicToH(SkPath* path, const uint32_t raw[]) {
68 const float* fptr = (const float*)raw;
69 path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]);
70}
71
72// This used to assert, because we performed a cast (int)(pt[0].fX * scale) to
73// arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert
74// was that the initial line-segment produced by the cubic was not monotonically
75// going down (i.e. the initial DY was negative). By rounding the floats, we get
76// the more proper result.
77//
78// http://code.google.com/p/chromium/issues/detail?id=131181
79//
humper@google.com05af1af2013-01-07 16:47:43 +000080
81// we're not calling this test anymore; is that for a reason?
82
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000083static void test_crbug131181() {
reed@google.comb59ed512012-06-15 18:26:04 +000084 /*
85 fX = 18.8943768,
86 fY = 129.121277
87 }, {
88 fX = 18.8937435,
89 fY = 129.121689
90 }, {
91 fX = 18.8950119,
92 fY = 129.120422
93 }, {
94 fX = 18.5030727,
95 fY = 129.13121
96 */
97 uint32_t data[] = {
98 0x419727af, 0x43011f0c, 0x41972663, 0x43011f27,
99 0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197
100 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
reed@google.comb59ed512012-06-15 18:26:04 +0000102 SkPath path;
103 moveToH(&path, &data[0]);
104 cubicToH(&path, &data[2]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105
reed@google.comb59ed512012-06-15 18:26:04 +0000106 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
reed@google.comb59ed512012-06-15 18:26:04 +0000108 SkPaint paint;
109 paint.setAntiAlias(true);
110 canvas->drawPath(path, paint);
111}
112
reed@google.come2faf172012-08-06 19:01:34 +0000113// This used to assert in debug builds (and crash writing bad memory in release)
114// because we overflowed an intermediate value (B coefficient) setting up our
115// 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 +0000116static void test_crbug_140803() {
reed@google.come2faf172012-08-06 19:01:34 +0000117 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000118 bm.allocN32Pixels(2700, 30*1024);
reed@google.come2faf172012-08-06 19:01:34 +0000119 SkCanvas canvas(bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
reed@google.come2faf172012-08-06 19:01:34 +0000121 SkPath path;
122 path.moveTo(2762, 20);
123 path.quadTo(11, 21702, 10, 21706);
124 SkPaint paint;
125 paint.setAntiAlias(true);
126 canvas.drawPath(path, paint);
127}
128
reed@google.com9d5f76a2012-05-01 14:49:28 +0000129// Need to exercise drawing an inverse-path whose bounds intersect the clip,
130// but whose edges do not (since its a quad which draws only in the bottom half
131// of its bounds).
132// In the debug build, we used to assert in this case, until it was fixed.
133//
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000134static void test_inversepathwithclip() {
reed@google.com9d5f76a2012-05-01 14:49:28 +0000135 SkPath path;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000136
reed@google.com9d5f76a2012-05-01 14:49:28 +0000137 path.moveTo(0, SkIntToScalar(20));
138 path.quadTo(SkIntToScalar(10), SkIntToScalar(10),
139 SkIntToScalar(20), SkIntToScalar(20));
140 path.toggleInverseFillType();
141
142 SkPaint paint;
143
144 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
145 canvas.get()->save();
146 canvas.get()->clipRect(SkRect::MakeWH(SkIntToScalar(19), SkIntToScalar(11)));
147
148 paint.setAntiAlias(false);
149 canvas.get()->drawPath(path, paint);
150 paint.setAntiAlias(true);
151 canvas.get()->drawPath(path, paint);
152
153 canvas.get()->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154
reed@google.com9d5f76a2012-05-01 14:49:28 +0000155 // Now do the test again, with the path flipped, so we only draw in the
156 // top half of our bounds, and have the clip intersect our bounds at the
157 // bottom.
158 path.reset(); // preserves our filltype
159 path.moveTo(0, SkIntToScalar(10));
160 path.quadTo(SkIntToScalar(10), SkIntToScalar(20),
161 SkIntToScalar(20), SkIntToScalar(10));
162 canvas.get()->clipRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(19),
163 SkIntToScalar(19), SkIntToScalar(11)));
164
165 paint.setAntiAlias(false);
166 canvas.get()->drawPath(path, paint);
167 paint.setAntiAlias(true);
168 canvas.get()->drawPath(path, paint);
169}
170
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000171static void test_bug533() {
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000172 /*
173 http://code.google.com/p/skia/issues/detail?id=533
174 This particular test/bug only applies to the float case, where the
175 coordinates are very large.
176 */
177 SkPath path;
178 path.moveTo(64, 3);
179 path.quadTo(-329936, -100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000180
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000181 SkPaint paint;
182 paint.setAntiAlias(true);
183
184 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
185 canvas.get()->drawPath(path, paint);
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000186}
187
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000188static void test_crbug_140642() {
reed@google.comd9ee3482012-08-06 14:58:35 +0000189 /*
190 * We used to see this construct, and due to rounding as we accumulated
191 * our length, the loop where we apply the phase would run off the end of
192 * the array, since it relied on just -= each interval value, which did not
193 * behave as "expected". Now the code explicitly checks for walking off the
194 * end of that array.
195
196 * A different (better) fix might be to rewrite dashing to do all of its
197 * length/phase/measure math using double, but this may need to be
198 * coordinated with SkPathMeasure, to be consistent between the two.
199
200 <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
201 stroke-dashoffset="-248.135982067">
202 */
203
reed@google.comd9ee3482012-08-06 14:58:35 +0000204 const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
205 SkDashPathEffect dontAssert(vals, 4, -248.135982067f);
reed@google.comd9ee3482012-08-06 14:58:35 +0000206}
207
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000208static void test_crbug_124652() {
reed@google.com1df888b2012-04-24 22:47:21 +0000209 /*
210 http://code.google.com/p/chromium/issues/detail?id=124652
211 This particular test/bug only applies to the float case, where
212 large values can "swamp" small ones.
213 */
214 SkScalar intervals[2] = {837099584, 33450};
215 SkAutoTUnref<SkDashPathEffect> dash(
216 new SkDashPathEffect(intervals, 2, -10, false));
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
reed@google.coma90aa532012-04-16 16:27:09 +0000227 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
228 canvas.get()->drawPath(path, paint);
reed@google.coma90aa532012-04-16 16:27:09 +0000229}
230
reed@google.comdceecc72012-02-23 19:20:19 +0000231// we used to assert if the bounds of the device (clip) was larger than 32K
232// even when the path itself was smaller. We just draw and hope in the debug
233// version to not assert.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000234static void test_giantaa() {
reed@google.comdceecc72012-02-23 19:20:19 +0000235 const int W = 400;
236 const int H = 400;
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000237 SkAutoTUnref<SkCanvas> canvas(new_canvas(33000, 10));
junov@google.comdbfac8a2012-12-06 21:47:40 +0000238 canvas.get()->clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000239
reed@google.comdceecc72012-02-23 19:20:19 +0000240 SkPaint paint;
241 paint.setAntiAlias(true);
242 SkPath path;
243 path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000244 canvas.get()->drawPath(path, paint);
reed@google.comdceecc72012-02-23 19:20:19 +0000245}
246
fmalita@google.combfa04012012-12-12 22:13:58 +0000247// Extremely large path_length/dash_length ratios may cause infinite looping
248// in SkDashPathEffect::filterPath() due to single precision rounding.
249// The test is quite expensive, but it should get much faster after the fix
250// for http://crbug.com/165432 goes in.
251static void test_infinite_dash(skiatest::Reporter* reporter) {
252 SkPath path;
253 path.moveTo(0, 0);
254 path.lineTo(5000000, 0);
255
256 SkScalar intervals[] = { 0.2f, 0.2f };
257 SkDashPathEffect dash(intervals, 2, 0);
258
259 SkPath filteredPath;
260 SkPaint paint;
261 paint.setStyle(SkPaint::kStroke_Style);
262 paint.setPathEffect(&dash);
263
264 paint.getFillPath(path, &filteredPath);
265 // If we reach this, we passed.
266 REPORTER_ASSERT(reporter, true);
267}
268
fmalita@google.com6b18d242012-12-17 16:27:34 +0000269// http://crbug.com/165432
270// Limit extreme dash path effects to avoid exhausting the system memory.
271static void test_crbug_165432(skiatest::Reporter* reporter) {
272 SkPath path;
273 path.moveTo(0, 0);
274 path.lineTo(10000000, 0);
275
276 SkScalar intervals[] = { 0.5f, 0.5f };
277 SkDashPathEffect dash(intervals, 2, 0);
278
279 SkPaint paint;
280 paint.setStyle(SkPaint::kStroke_Style);
281 paint.setPathEffect(&dash);
282
283 SkPath filteredPath;
284 SkStrokeRec rec(paint);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000285 REPORTER_ASSERT(reporter, !dash.filterPath(&filteredPath, path, &rec, NULL));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000286 REPORTER_ASSERT(reporter, filteredPath.isEmpty());
287}
288
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000289DEF_TEST(DrawPath, reporter) {
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000290 test_giantaa();
291 test_bug533();
292 test_bigcubic();
293 test_crbug_124652();
294 test_crbug_140642();
295 test_crbug_140803();
296 test_inversepathwithclip();
humper@google.com05af1af2013-01-07 16:47:43 +0000297 // why?
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000298 if (false) test_crbug131181();
fmalita@google.combfa04012012-12-12 22:13:58 +0000299 test_infinite_dash(reporter);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000300 test_crbug_165432(reporter);
reed@google.com1c028bd2013-08-28 15:23:19 +0000301 test_big_aa_rect(reporter);
reed@google.comdceecc72012-02-23 19:20:19 +0000302}