blob: aea07c10cde492c11627a2e6af51633ee406c2bb [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"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
reed@google.com1df888b2012-04-24 22:47:21 +000011#include "SkDashPathEffect.h"
reed@google.comf272e352013-08-26 21:27:03 +000012#include "SkSurface.h"
reed@google.comdceecc72012-02-23 19:20:19 +000013
14static SkCanvas* create(SkBitmap::Config config, int w, int h, int rb,
15 void* addr = NULL) {
16 SkBitmap bm;
17 bm.setConfig(config, w, h, rb);
18 if (addr) {
19 bm.setPixels(addr);
20 } else {
21 bm.allocPixels();
22 }
23 return new SkCanvas(bm);
24}
25
mike@reedtribe.org6093e652012-04-14 12:55:17 +000026static SkCanvas* new_canvas(int w, int h) {
27 return create(SkBitmap::kARGB_8888_Config, w, h, 0, NULL);
28}
29
reed@google.coma90aa532012-04-16 16:27:09 +000030///////////////////////////////////////////////////////////////////////////////
31
reed@google.comf272e352013-08-26 21:27:03 +000032// test that we can draw an aa-rect at coordinates > 32K (bigger than fixedpoint)
33static void test_big_aa_rect(skiatest::Reporter* reporter) {
34 SkBitmap output;
35 SkPMColor pixel[1];
36 output.setConfig(SkBitmap::kARGB_8888_Config, 1, 1, 4);
37 output.setPixels(pixel);
38
39 SkImage::Info info = {
40 300, 33300, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType
41 };
42 SkSurface* surf = SkSurface::NewRaster(info);
43 SkCanvas* canvas = surf->getCanvas();
44
45 SkRect r = { 0, 33000, 300, 33300 };
46 int x = SkScalarRoundToInt(r.left());
47 int y = SkScalarRoundToInt(r.top());
48
49 // check that the pixel in question starts as transparent (by the surface)
50 if (canvas->readPixels(&output, x, y)) {
51 REPORTER_ASSERT(reporter, 0 == pixel[0]);
52 } else {
53 REPORTER_ASSERT(reporter, !"readPixels failed");
54 }
55
56 SkPaint paint;
57 paint.setAntiAlias(true);
58 paint.setColor(SK_ColorWHITE);
59
60 canvas->drawRect(r, paint);
61
62 // Now check that it is BLACK
63 if (canvas->readPixels(&output, x, y)) {
64 // don't know what swizzling PMColor did, but white should always
65 // appear the same.
66 REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
67 } else {
68 REPORTER_ASSERT(reporter, !"readPixels failed");
69 }
70 surf->unref();
71}
72
73///////////////////////////////////////////////////////////////////////////////
74
reed@google.comb59ed512012-06-15 18:26:04 +000075static void moveToH(SkPath* path, const uint32_t raw[]) {
76 const float* fptr = (const float*)raw;
77 path->moveTo(fptr[0], fptr[1]);
78}
79
80static void cubicToH(SkPath* path, const uint32_t raw[]) {
81 const float* fptr = (const float*)raw;
82 path->cubicTo(fptr[0], fptr[1], fptr[2], fptr[3], fptr[4], fptr[5]);
83}
84
85// This used to assert, because we performed a cast (int)(pt[0].fX * scale) to
86// arrive at an int (SkFDot6) rather than calling sk_float_round2int. The assert
87// was that the initial line-segment produced by the cubic was not monotonically
88// going down (i.e. the initial DY was negative). By rounding the floats, we get
89// the more proper result.
90//
91// http://code.google.com/p/chromium/issues/detail?id=131181
92//
humper@google.com05af1af2013-01-07 16:47:43 +000093
94// we're not calling this test anymore; is that for a reason?
95
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000096static void test_crbug131181() {
reed@google.comb59ed512012-06-15 18:26:04 +000097 /*
98 fX = 18.8943768,
99 fY = 129.121277
100 }, {
101 fX = 18.8937435,
102 fY = 129.121689
103 }, {
104 fX = 18.8950119,
105 fY = 129.120422
106 }, {
107 fX = 18.5030727,
108 fY = 129.13121
109 */
110 uint32_t data[] = {
111 0x419727af, 0x43011f0c, 0x41972663, 0x43011f27,
112 0x419728fc, 0x43011ed4, 0x4194064b, 0x43012197
113 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
reed@google.comb59ed512012-06-15 18:26:04 +0000115 SkPath path;
116 moveToH(&path, &data[0]);
117 cubicToH(&path, &data[2]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118
reed@google.comb59ed512012-06-15 18:26:04 +0000119 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
reed@google.comb59ed512012-06-15 18:26:04 +0000121 SkPaint paint;
122 paint.setAntiAlias(true);
123 canvas->drawPath(path, paint);
124}
125
reed@google.come2faf172012-08-06 19:01:34 +0000126// This used to assert in debug builds (and crash writing bad memory in release)
127// because we overflowed an intermediate value (B coefficient) setting up our
128// 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 +0000129static void test_crbug_140803() {
reed@google.come2faf172012-08-06 19:01:34 +0000130 SkBitmap bm;
131 bm.setConfig(SkBitmap::kARGB_8888_Config, 2700, 30*1024);
132 bm.allocPixels();
133 SkCanvas canvas(bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134
reed@google.come2faf172012-08-06 19:01:34 +0000135 SkPath path;
136 path.moveTo(2762, 20);
137 path.quadTo(11, 21702, 10, 21706);
138 SkPaint paint;
139 paint.setAntiAlias(true);
140 canvas.drawPath(path, paint);
141}
142
reed@google.com9d5f76a2012-05-01 14:49:28 +0000143// Need to exercise drawing an inverse-path whose bounds intersect the clip,
144// but whose edges do not (since its a quad which draws only in the bottom half
145// of its bounds).
146// In the debug build, we used to assert in this case, until it was fixed.
147//
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000148static void test_inversepathwithclip() {
reed@google.com9d5f76a2012-05-01 14:49:28 +0000149 SkPath path;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150
reed@google.com9d5f76a2012-05-01 14:49:28 +0000151 path.moveTo(0, SkIntToScalar(20));
152 path.quadTo(SkIntToScalar(10), SkIntToScalar(10),
153 SkIntToScalar(20), SkIntToScalar(20));
154 path.toggleInverseFillType();
155
156 SkPaint paint;
157
158 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
159 canvas.get()->save();
160 canvas.get()->clipRect(SkRect::MakeWH(SkIntToScalar(19), SkIntToScalar(11)));
161
162 paint.setAntiAlias(false);
163 canvas.get()->drawPath(path, paint);
164 paint.setAntiAlias(true);
165 canvas.get()->drawPath(path, paint);
166
167 canvas.get()->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000168
reed@google.com9d5f76a2012-05-01 14:49:28 +0000169 // Now do the test again, with the path flipped, so we only draw in the
170 // top half of our bounds, and have the clip intersect our bounds at the
171 // bottom.
172 path.reset(); // preserves our filltype
173 path.moveTo(0, SkIntToScalar(10));
174 path.quadTo(SkIntToScalar(10), SkIntToScalar(20),
175 SkIntToScalar(20), SkIntToScalar(10));
176 canvas.get()->clipRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(19),
177 SkIntToScalar(19), SkIntToScalar(11)));
178
179 paint.setAntiAlias(false);
180 canvas.get()->drawPath(path, paint);
181 paint.setAntiAlias(true);
182 canvas.get()->drawPath(path, paint);
183}
184
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000185static void test_bug533() {
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000186#ifdef SK_SCALAR_IS_FLOAT
187 /*
188 http://code.google.com/p/skia/issues/detail?id=533
189 This particular test/bug only applies to the float case, where the
190 coordinates are very large.
191 */
192 SkPath path;
193 path.moveTo(64, 3);
194 path.quadTo(-329936, -100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000195
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000196 SkPaint paint;
197 paint.setAntiAlias(true);
198
199 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
200 canvas.get()->drawPath(path, paint);
201#endif
202}
203
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000204static void test_crbug_140642() {
reed@google.comd9ee3482012-08-06 14:58:35 +0000205 /*
206 * We used to see this construct, and due to rounding as we accumulated
207 * our length, the loop where we apply the phase would run off the end of
208 * the array, since it relied on just -= each interval value, which did not
209 * behave as "expected". Now the code explicitly checks for walking off the
210 * end of that array.
211
212 * A different (better) fix might be to rewrite dashing to do all of its
213 * length/phase/measure math using double, but this may need to be
214 * coordinated with SkPathMeasure, to be consistent between the two.
215
216 <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
217 stroke-dashoffset="-248.135982067">
218 */
219
220#ifdef SK_SCALAR_IS_FLOAT
221 const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
222 SkDashPathEffect dontAssert(vals, 4, -248.135982067f);
223#endif
224}
225
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000226static void test_crbug_124652() {
reed@google.com1df888b2012-04-24 22:47:21 +0000227#ifdef SK_SCALAR_IS_FLOAT
228 /*
229 http://code.google.com/p/chromium/issues/detail?id=124652
230 This particular test/bug only applies to the float case, where
231 large values can "swamp" small ones.
232 */
233 SkScalar intervals[2] = {837099584, 33450};
234 SkAutoTUnref<SkDashPathEffect> dash(
235 new SkDashPathEffect(intervals, 2, -10, false));
236#endif
237}
238
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000239static void test_bigcubic() {
reed@google.coma90aa532012-04-16 16:27:09 +0000240#ifdef SK_SCALAR_IS_FLOAT
241 SkPath path;
242 path.moveTo(64, 3);
243 path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000244
reed@google.coma90aa532012-04-16 16:27:09 +0000245 SkPaint paint;
246 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000247
reed@google.coma90aa532012-04-16 16:27:09 +0000248 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
249 canvas.get()->drawPath(path, paint);
250#endif
251}
252
reed@google.comdceecc72012-02-23 19:20:19 +0000253// we used to assert if the bounds of the device (clip) was larger than 32K
254// even when the path itself was smaller. We just draw and hope in the debug
255// version to not assert.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000256static void test_giantaa() {
reed@google.comdceecc72012-02-23 19:20:19 +0000257 const int W = 400;
258 const int H = 400;
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000259 SkAutoTUnref<SkCanvas> canvas(new_canvas(33000, 10));
junov@google.comdbfac8a2012-12-06 21:47:40 +0000260 canvas.get()->clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000261
reed@google.comdceecc72012-02-23 19:20:19 +0000262 SkPaint paint;
263 paint.setAntiAlias(true);
264 SkPath path;
265 path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000266 canvas.get()->drawPath(path, paint);
reed@google.comdceecc72012-02-23 19:20:19 +0000267}
268
fmalita@google.combfa04012012-12-12 22:13:58 +0000269// Extremely large path_length/dash_length ratios may cause infinite looping
270// in SkDashPathEffect::filterPath() due to single precision rounding.
271// The test is quite expensive, but it should get much faster after the fix
272// for http://crbug.com/165432 goes in.
273static void test_infinite_dash(skiatest::Reporter* reporter) {
274 SkPath path;
275 path.moveTo(0, 0);
276 path.lineTo(5000000, 0);
277
278 SkScalar intervals[] = { 0.2f, 0.2f };
279 SkDashPathEffect dash(intervals, 2, 0);
280
281 SkPath filteredPath;
282 SkPaint paint;
283 paint.setStyle(SkPaint::kStroke_Style);
284 paint.setPathEffect(&dash);
285
286 paint.getFillPath(path, &filteredPath);
287 // If we reach this, we passed.
288 REPORTER_ASSERT(reporter, true);
289}
290
fmalita@google.com6b18d242012-12-17 16:27:34 +0000291// http://crbug.com/165432
292// Limit extreme dash path effects to avoid exhausting the system memory.
293static void test_crbug_165432(skiatest::Reporter* reporter) {
294 SkPath path;
295 path.moveTo(0, 0);
296 path.lineTo(10000000, 0);
297
298 SkScalar intervals[] = { 0.5f, 0.5f };
299 SkDashPathEffect dash(intervals, 2, 0);
300
301 SkPaint paint;
302 paint.setStyle(SkPaint::kStroke_Style);
303 paint.setPathEffect(&dash);
304
305 SkPath filteredPath;
306 SkStrokeRec rec(paint);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000307 REPORTER_ASSERT(reporter, !dash.filterPath(&filteredPath, path, &rec, NULL));
fmalita@google.com6b18d242012-12-17 16:27:34 +0000308 REPORTER_ASSERT(reporter, filteredPath.isEmpty());
309}
310
reed@google.comdceecc72012-02-23 19:20:19 +0000311static void TestDrawPath(skiatest::Reporter* reporter) {
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000312 test_giantaa();
313 test_bug533();
314 test_bigcubic();
315 test_crbug_124652();
316 test_crbug_140642();
317 test_crbug_140803();
318 test_inversepathwithclip();
humper@google.com05af1af2013-01-07 16:47:43 +0000319 // why?
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000320 if (false) test_crbug131181();
fmalita@google.combfa04012012-12-12 22:13:58 +0000321 test_infinite_dash(reporter);
fmalita@google.com6b18d242012-12-17 16:27:34 +0000322 test_crbug_165432(reporter);
reed@google.comf272e352013-08-26 21:27:03 +0000323
324 if (false) { // working on a fix
325 test_big_aa_rect(reporter);
326 }
reed@google.comdceecc72012-02-23 19:20:19 +0000327}
328
329#include "TestClassDef.h"
330DEFINE_TESTCLASS("DrawPath", TestDrawPathClass, TestDrawPath)