blob: 8c77ffe03106eafd886a2ebc8f8afb9099119290 [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
8#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.comdceecc72012-02-23 19:20:19 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
reed@google.com1df888b2012-04-24 22:47:21 +000012#include "SkDashPathEffect.h"
reed@google.comf272e352013-08-26 21:27:03 +000013#include "SkSurface.h"
reed@google.comdceecc72012-02-23 19:20:19 +000014
15static SkCanvas* create(SkBitmap::Config config, int w, int h, int rb,
16 void* addr = NULL) {
17 SkBitmap bm;
18 bm.setConfig(config, w, h, rb);
19 if (addr) {
20 bm.setPixels(addr);
21 } else {
22 bm.allocPixels();
23 }
24 return new SkCanvas(bm);
25}
26
mike@reedtribe.org6093e652012-04-14 12:55:17 +000027static SkCanvas* new_canvas(int w, int h) {
28 return create(SkBitmap::kARGB_8888_Config, w, h, 0, NULL);
29}
30
reed@google.coma90aa532012-04-16 16:27:09 +000031///////////////////////////////////////////////////////////////////////////////
32
reed@google.comf272e352013-08-26 21:27:03 +000033// test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
34static void test_big_aa_rect(skiatest::Reporter* reporter) {
35 SkBitmap output;
36 SkPMColor pixel[1];
37 output.setConfig(SkBitmap::kARGB_8888_Config, 1, 1, 4);
38 output.setPixels(pixel);
39
reed@google.comd28ba802013-09-20 19:33:52 +000040 SkSurface* surf = SkSurface::NewRasterPMColor(300, 33300);
reed@google.comf272e352013-08-26 21:27:03 +000041 SkCanvas* canvas = surf->getCanvas();
42
43 SkRect r = { 0, 33000, 300, 33300 };
44 int x = SkScalarRoundToInt(r.left());
45 int y = SkScalarRoundToInt(r.top());
46
47 // check that the pixel in question starts as transparent (by the surface)
48 if (canvas->readPixels(&output, x, y)) {
49 REPORTER_ASSERT(reporter, 0 == pixel[0]);
50 } else {
mtklein@google.com9f842d32013-08-27 16:15:37 +000051 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000052 }
53
54 SkPaint paint;
55 paint.setAntiAlias(true);
56 paint.setColor(SK_ColorWHITE);
57
58 canvas->drawRect(r, paint);
59
60 // Now check that it is BLACK
61 if (canvas->readPixels(&output, x, y)) {
62 // don't know what swizzling PMColor did, but white should always
63 // appear the same.
64 REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
65 } else {
mtklein@google.com9f842d32013-08-27 16:15:37 +000066 REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
reed@google.comf272e352013-08-26 21:27:03 +000067 }
68 surf->unref();
69}
70
71///////////////////////////////////////////////////////////////////////////////
72
reed@google.comb59ed512012-06-15 18:26:04 +000073static void moveToH(SkPath* path, const uint32_t raw[]) {
74 const float* fptr = (const float*)raw;
75 path->moveTo(fptr[0], fptr[1]);
76}
77
78static void cubicToH(SkPath* path, const uint32_t raw[]) {
79 const float* fptr = (const float*)raw;
80 path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]);
81}
82
83// This used to assert, because we performed a cast (int)(pt[0].fX * scale) to
84// arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert
85// was that the initial line-segment produced by the cubic was not monotonically
86// going down (i.e. the initial DY was negative). By rounding the floats, we get
87// the more proper result.
88//
89// http://code.google.com/p/chromium/issues/detail?id=131181
90//
humper@google.com05af1af2013-01-07 16:47:43 +000091
92// we're not calling this test anymore; is that for a reason?
93
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000094static void test_crbug131181() {
reed@google.comb59ed512012-06-15 18:26:04 +000095 /*
96 fX = 18.8943768,
97 fY = 129.121277
98 }, {
99 fX = 18.8937435,
100 fY = 129.121689
101 }, {
102 fX = 18.8950119,
103 fY = 129.120422
104 }, {
105 fX = 18.5030727,
106 fY = 129.13121
107 */
108 uint32_t data[] = {
109 0x419727af, 0x43011f0c, 0x41972663, 0x43011f27,
110 0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197
111 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112
reed@google.comb59ed512012-06-15 18:26:04 +0000113 SkPath path;
114 moveToH(&path, &data[0]);
115 cubicToH(&path, &data[2]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
reed@google.comb59ed512012-06-15 18:26:04 +0000117 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118
reed@google.comb59ed512012-06-15 18:26:04 +0000119 SkPaint paint;
120 paint.setAntiAlias(true);
121 canvas->drawPath(path, paint);
122}
123
reed@google.come2faf172012-08-06 19:01:34 +0000124// This used to assert in debug builds (and crash writing bad memory in release)
125// because we overflowed an intermediate value (B coefficient) setting up our
126// 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 +0000127static void test_crbug_140803() {
reed@google.come2faf172012-08-06 19:01:34 +0000128 SkBitmap bm;
129 bm.setConfig(SkBitmap::kARGB_8888_Config, 2700, 30*1024);
130 bm.allocPixels();
131 SkCanvas canvas(bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@google.come2faf172012-08-06 19:01:34 +0000133 SkPath path;
134 path.moveTo(2762, 20);
135 path.quadTo(11, 21702, 10, 21706);
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 canvas.drawPath(path, paint);
139}
140
reed@google.com9d5f76a2012-05-01 14:49:28 +0000141// Need to exercise drawing an inverse-path whose bounds intersect the clip,
142// but whose edges do not (since its a quad which draws only in the bottom half
143// of its bounds).
144// In the debug build, we used to assert in this case, until it was fixed.
145//
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000146static void test_inversepathwithclip() {
reed@google.com9d5f76a2012-05-01 14:49:28 +0000147 SkPath path;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000148
reed@google.com9d5f76a2012-05-01 14:49:28 +0000149 path.moveTo(0, SkIntToScalar(20));
150 path.quadTo(SkIntToScalar(10), SkIntToScalar(10),
151 SkIntToScalar(20), SkIntToScalar(20));
152 path.toggleInverseFillType();
153
154 SkPaint paint;
155
156 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
157 canvas.get()->save();
158 canvas.get()->clipRect(SkRect::MakeWH(SkIntToScalar(19), SkIntToScalar(11)));
159
160 paint.setAntiAlias(false);
161 canvas.get()->drawPath(path, paint);
162 paint.setAntiAlias(true);
163 canvas.get()->drawPath(path, paint);
164
165 canvas.get()->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166
reed@google.com9d5f76a2012-05-01 14:49:28 +0000167 // Now do the test again, with the path flipped, so we only draw in the
168 // top half of our bounds, and have the clip intersect our bounds at the
169 // bottom.
170 path.reset(); // preserves our filltype
171 path.moveTo(0, SkIntToScalar(10));
172 path.quadTo(SkIntToScalar(10), SkIntToScalar(20),
173 SkIntToScalar(20), SkIntToScalar(10));
174 canvas.get()->clipRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(19),
175 SkIntToScalar(19), SkIntToScalar(11)));
176
177 paint.setAntiAlias(false);
178 canvas.get()->drawPath(path, paint);
179 paint.setAntiAlias(true);
180 canvas.get()->drawPath(path, paint);
181}
182
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000183static void test_bug533() {
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000184 /*
185 http://code.google.com/p/skia/issues/detail?id=533
186 This particular test/bug only applies to the float case, where the
187 coordinates are very large.
188 */
189 SkPath path;
190 path.moveTo(64, 3);
191 path.quadTo(-329936, -100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000193 SkPaint paint;
194 paint.setAntiAlias(true);
195
196 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
197 canvas.get()->drawPath(path, paint);
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000198}
199
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000200static void test_crbug_140642() {
reed@google.comd9ee3482012-08-06 14:58:35 +0000201 /*
202 * We used to see this construct, and due to rounding as we accumulated
203 * our length, the loop where we apply the phase would run off the end of
204 * the array, since it relied on just -= each interval value, which did not
205 * behave as "expected". Now the code explicitly checks for walking off the
206 * end of that array.
207
208 * A different (better) fix might be to rewrite dashing to do all of its
209 * length/phase/measure math using double, but this may need to be
210 * coordinated with SkPathMeasure, to be consistent between the two.
211
212 <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
213 stroke-dashoffset="-248.135982067">
214 */
215
reed@google.comd9ee3482012-08-06 14:58:35 +0000216 const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
217 SkDashPathEffect dontAssert(vals, 4, -248.135982067f);
reed@google.comd9ee3482012-08-06 14:58:35 +0000218}
219
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000220static void test_crbug_124652() {
reed@google.com1df888b2012-04-24 22:47:21 +0000221 /*
222 http://code.google.com/p/chromium/issues/detail?id=124652
223 This particular test/bug only applies to the float case, where
224 large values can "swamp" small ones.
225 */
226 SkScalar intervals[2] = {837099584, 33450};
227 SkAutoTUnref<SkDashPathEffect> dash(
228 new SkDashPathEffect(intervals, 2, -10, false));
reed@google.com1df888b2012-04-24 22:47:21 +0000229}
230
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000231static void test_bigcubic() {
reed@google.coma90aa532012-04-16 16:27:09 +0000232 SkPath path;
233 path.moveTo(64, 3);
234 path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235
reed@google.coma90aa532012-04-16 16:27:09 +0000236 SkPaint paint;
237 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000238
reed@google.coma90aa532012-04-16 16:27:09 +0000239 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
240 canvas.get()->drawPath(path, paint);
reed@google.coma90aa532012-04-16 16:27:09 +0000241}
242
reed@google.comdceecc72012-02-23 19:20:19 +0000243// we used to assert if the bounds of the device (clip) was larger than 32K
244// even when the path itself was smaller. We just draw and hope in the debug
245// version to not assert.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000246static void test_giantaa() {
reed@google.comdceecc72012-02-23 19:20:19 +0000247 const int W = 400;
248 const int H = 400;
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000249 SkAutoTUnref<SkCanvas> canvas(new_canvas(33000, 10));
junov@google.comdbfac8a2012-12-06 21:47:40 +0000250 canvas.get()->clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000251
reed@google.comdceecc72012-02-23 19:20:19 +0000252 SkPaint paint;
253 paint.setAntiAlias(true);
254 SkPath path;
255 path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000256 canvas.get()->drawPath(path, paint);
reed@google.comdceecc72012-02-23 19:20:19 +0000257}
258
fmalita@google.combfa04012012-12-12 22:13:58 +0000259// Extremely large path_length/dash_length ratios may cause infinite looping
260// in SkDashPathEffect::filterPath() due to single precision rounding.
261// The test is quite expensive, but it should get much faster after the fix
262// for http://crbug.com/165432 goes in.
263static void test_infinite_dash(skiatest::Reporter* reporter) {
264 SkPath path;
265 path.moveTo(0, 0);
266 path.lineTo(5000000, 0);
267
268 SkScalar intervals[] = { 0.2f, 0.2f };
269 SkDashPathEffect dash(intervals, 2, 0);
270
271 SkPath filteredPath;
272 SkPaint paint;
273 paint.setStyle(SkPaint::kStroke_Style);
274 paint.setPathEffect(&dash);
275
276 paint.getFillPath(path, &filteredPath);
277 // If we reach this, we passed.
278 REPORTER_ASSERT(reporter, true);
279}
280
fmalita@google.com6b18d242012-12-17 16:27:34 +0000281// http://crbug.com/165432
282// Limit extreme dash path effects to avoid exhausting the system memory.
283static void test_crbug_165432(skiatest::Reporter* reporter) {
284 SkPath path;
285 path.moveTo(0, 0);
286 path.lineTo(10000000, 0);
287
288 SkScalar intervals[] = { 0.5f, 0.5f };
289 SkDashPathEffect dash(intervals, 2, 0);
290
291 SkPaint paint;
292 paint.setStyle(SkPaint::kStroke_Style);
293 paint.setPathEffect(&dash);
294
295 SkPath filteredPath;
296 SkStrokeRec rec(paint);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000297 REPORTER_ASSERT(reporter, !dash.filterPath(&filteredPath, path, &rec, NULL));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000298 REPORTER_ASSERT(reporter, filteredPath.isEmpty());
299}
300
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000301DEF_TEST(DrawPath, reporter) {
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000302 test_giantaa();
303 test_bug533();
304 test_bigcubic();
305 test_crbug_124652();
306 test_crbug_140642();
307 test_crbug_140803();
308 test_inversepathwithclip();
humper@google.com05af1af2013-01-07 16:47:43 +0000309 // why?
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000310 if (false) test_crbug131181();
fmalita@google.combfa04012012-12-12 22:13:58 +0000311 test_infinite_dash(reporter);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000312 test_crbug_165432(reporter);
reed@google.com1c028bd2013-08-28 15:23:19 +0000313 test_big_aa_rect(reporter);
reed@google.comdceecc72012-02-23 19:20:19 +0000314}