1/*
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 "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkDashPathEffect.h"
11#include "SkSurface.h"
12#include "Test.h"
13
14// 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];
18    output.installPixels(SkImageInfo::MakeN32Premul(1, 1), pixel, 4);
19
20    SkSurface* surf = SkSurface::NewRasterN32Premul(300, 33300);
21    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 {
31        REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
32    }
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 {
46        REPORTER_ASSERT_MESSAGE(reporter, false, "readPixels failed");
47    }
48    surf->unref();
49}
50
51///////////////////////////////////////////////////////////////////////////////
52
53static 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//
71
72// we're not calling this test anymore; is that for a reason?
73
74static void test_crbug131181() {
75    /*
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    };
92
93    SkPath path;
94    moveToH(&path, &data[0]);
95    cubicToH(&path, &data[2]);
96
97    SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(640, 480));
98
99    SkPaint paint;
100    paint.setAntiAlias(true);
101    surface->getCanvas()->drawPath(path, paint);
102}
103
104// 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
107static void test_crbug_140803() {
108    SkBitmap bm;
109    bm.allocN32Pixels(2700, 30*1024);
110    SkCanvas canvas(bm);
111
112    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
120// 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//
125static void test_inversepathwithclip() {
126    SkPath path;
127
128    path.moveTo(0, 20);
129    path.quadTo(10, 10, 20, 20);
130    path.toggleInverseFillType();
131
132    SkPaint paint;
133
134    SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(640, 480));
135    SkCanvas* canvas = surface->getCanvas();
136    canvas->save();
137    canvas->clipRect(SkRect::MakeWH(19, 11));
138
139    paint.setAntiAlias(false);
140    canvas->drawPath(path, paint);
141    paint.setAntiAlias(true);
142    canvas->drawPath(path, paint);
143
144    canvas->restore();
145
146    // 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, 10);
151    path.quadTo(10, 20, 20, 10);
152    canvas->clipRect(SkRect::MakeXYWH(0, 19, 19, 11));
153
154    paint.setAntiAlias(false);
155    canvas->drawPath(path, paint);
156    paint.setAntiAlias(true);
157    canvas->drawPath(path, paint);
158}
159
160static void test_bug533() {
161    /*
162        http://code.google.com/p/skia/issues/detail?id=533
163        This particular test/bug only applies to the float case, where the
164        coordinates are very large.
165     */
166    SkPath path;
167    path.moveTo(64, 3);
168    path.quadTo(-329936, -100000000, 1153, 330003);
169
170    SkPaint paint;
171    paint.setAntiAlias(true);
172
173    SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(640, 480));
174    surface->getCanvas()->drawPath(path, paint);
175}
176
177static void test_crbug_140642() {
178    /*
179     *  We used to see this construct, and due to rounding as we accumulated
180     *  our length, the loop where we apply the phase would run off the end of
181     *  the array, since it relied on just -= each interval value, which did not
182     *  behave as "expected". Now the code explicitly checks for walking off the
183     *  end of that array.
184
185     *  A different (better) fix might be to rewrite dashing to do all of its
186     *  length/phase/measure math using double, but this may need to be
187     *  coordinated with SkPathMeasure, to be consistent between the two.
188
189     <path stroke="mintcream" stroke-dasharray="27734 35660 2157846850 247"
190           stroke-dashoffset="-248.135982067">
191     */
192
193    const SkScalar vals[] = { 27734, 35660, 2157846850.0f, 247 };
194    SkAutoTUnref<SkDashPathEffect> dontAssert(SkDashPathEffect::Create(vals, 4, -248.135982067f));
195}
196
197static void test_crbug_124652() {
198    /*
199        http://code.google.com/p/chromium/issues/detail?id=124652
200        This particular test/bug only applies to the float case, where
201        large values can "swamp" small ones.
202     */
203    SkScalar intervals[2] = {837099584, 33450};
204    SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, -10));
205}
206
207static void test_bigcubic() {
208    SkPath path;
209    path.moveTo(64, 3);
210    path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
211
212    SkPaint paint;
213    paint.setAntiAlias(true);
214
215    SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(640, 480));
216    surface->getCanvas()->drawPath(path, paint);
217}
218
219// we used to assert if the bounds of the device (clip) was larger than 32K
220// even when the path itself was smaller. We just draw and hope in the debug
221// version to not assert.
222static void test_giantaa() {
223    const int W = 400;
224    const int H = 400;
225    SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(33000, 10));
226
227    SkPaint paint;
228    paint.setAntiAlias(true);
229    SkPath path;
230    path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
231    surface->getCanvas()->drawPath(path, paint);
232}
233
234// Extremely large path_length/dash_length ratios may cause infinite looping
235// in SkDashPathEffect::filterPath() due to single precision rounding.
236// The test is quite expensive, but it should get much faster after the fix
237// for http://crbug.com/165432 goes in.
238static void test_infinite_dash(skiatest::Reporter* reporter) {
239    SkPath path;
240    path.moveTo(0, 0);
241    path.lineTo(5000000, 0);
242
243    SkScalar intervals[] = { 0.2f, 0.2f };
244    SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
245
246    SkPath filteredPath;
247    SkPaint paint;
248    paint.setStyle(SkPaint::kStroke_Style);
249    paint.setPathEffect(dash);
250
251    paint.getFillPath(path, &filteredPath);
252    // If we reach this, we passed.
253    REPORTER_ASSERT(reporter, true);
254}
255
256// http://crbug.com/165432
257// Limit extreme dash path effects to avoid exhausting the system memory.
258static void test_crbug_165432(skiatest::Reporter* reporter) {
259    SkPath path;
260    path.moveTo(0, 0);
261    path.lineTo(10000000, 0);
262
263    SkScalar intervals[] = { 0.5f, 0.5f };
264    SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0));
265
266    SkPaint paint;
267    paint.setStyle(SkPaint::kStroke_Style);
268    paint.setPathEffect(dash);
269
270    SkPath filteredPath;
271    SkStrokeRec rec(paint);
272    REPORTER_ASSERT(reporter, !dash->filterPath(&filteredPath, path, &rec, NULL));
273    REPORTER_ASSERT(reporter, filteredPath.isEmpty());
274}
275
276DEF_TEST(DrawPath, reporter) {
277    test_giantaa();
278    test_bug533();
279    test_bigcubic();
280    test_crbug_124652();
281    test_crbug_140642();
282    test_crbug_140803();
283    test_inversepathwithclip();
284    // why?
285    if (false) test_crbug131181();
286    test_infinite_dash(reporter);
287    test_crbug_165432(reporter);
288    test_big_aa_rect(reporter);
289}
290