blob: 6c7f0b5ffce213b1c242d98244df1e04c668121a [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.comdceecc72012-02-23 19:20:19 +000012
13static SkCanvas* create(SkBitmap::Config config, int w, int h, int rb,
14 void* addr = NULL) {
15 SkBitmap bm;
16 bm.setConfig(config, w, h, rb);
17 if (addr) {
18 bm.setPixels(addr);
19 } else {
20 bm.allocPixels();
21 }
22 return new SkCanvas(bm);
23}
24
mike@reedtribe.org6093e652012-04-14 12:55:17 +000025static SkCanvas* new_canvas(int w, int h) {
26 return create(SkBitmap::kARGB_8888_Config, w, h, 0, NULL);
27}
28
reed@google.coma90aa532012-04-16 16:27:09 +000029///////////////////////////////////////////////////////////////////////////////
30
reed@google.com9d5f76a2012-05-01 14:49:28 +000031// Need to exercise drawing an inverse-path whose bounds intersect the clip,
32// but whose edges do not (since its a quad which draws only in the bottom half
33// of its bounds).
34// In the debug build, we used to assert in this case, until it was fixed.
35//
36static void test_inversepathwithclip(skiatest::Reporter* reporter) {
37 SkPath path;
38
39 path.moveTo(0, SkIntToScalar(20));
40 path.quadTo(SkIntToScalar(10), SkIntToScalar(10),
41 SkIntToScalar(20), SkIntToScalar(20));
42 path.toggleInverseFillType();
43
44 SkPaint paint;
45
46 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
47 canvas.get()->save();
48 canvas.get()->clipRect(SkRect::MakeWH(SkIntToScalar(19), SkIntToScalar(11)));
49
50 paint.setAntiAlias(false);
51 canvas.get()->drawPath(path, paint);
52 paint.setAntiAlias(true);
53 canvas.get()->drawPath(path, paint);
54
55 canvas.get()->restore();
56
57 // Now do the test again, with the path flipped, so we only draw in the
58 // top half of our bounds, and have the clip intersect our bounds at the
59 // bottom.
60 path.reset(); // preserves our filltype
61 path.moveTo(0, SkIntToScalar(10));
62 path.quadTo(SkIntToScalar(10), SkIntToScalar(20),
63 SkIntToScalar(20), SkIntToScalar(10));
64 canvas.get()->clipRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(19),
65 SkIntToScalar(19), SkIntToScalar(11)));
66
67 paint.setAntiAlias(false);
68 canvas.get()->drawPath(path, paint);
69 paint.setAntiAlias(true);
70 canvas.get()->drawPath(path, paint);
71}
72
mike@reedtribe.org6093e652012-04-14 12:55:17 +000073static void test_bug533(skiatest::Reporter* reporter) {
74#ifdef SK_SCALAR_IS_FLOAT
75 /*
76 http://code.google.com/p/skia/issues/detail?id=533
77 This particular test/bug only applies to the float case, where the
78 coordinates are very large.
79 */
80 SkPath path;
81 path.moveTo(64, 3);
82 path.quadTo(-329936, -100000000, 1153, 330003);
83
84 SkPaint paint;
85 paint.setAntiAlias(true);
86
87 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
88 canvas.get()->drawPath(path, paint);
89#endif
90}
91
reed@google.com1df888b2012-04-24 22:47:21 +000092static void test_crbug_124652(skiatest::Reporter* reporter) {
93#ifdef SK_SCALAR_IS_FLOAT
94 /*
95 http://code.google.com/p/chromium/issues/detail?id=124652
96 This particular test/bug only applies to the float case, where
97 large values can "swamp" small ones.
98 */
99 SkScalar intervals[2] = {837099584, 33450};
100 SkAutoTUnref<SkDashPathEffect> dash(
101 new SkDashPathEffect(intervals, 2, -10, false));
102#endif
103}
104
reed@google.coma90aa532012-04-16 16:27:09 +0000105static void test_bigcubic(skiatest::Reporter* reporter) {
106#ifdef SK_SCALAR_IS_FLOAT
107 SkPath path;
108 path.moveTo(64, 3);
109 path.cubicTo(-329936, -100000000, -329936, 100000000, 1153, 330003);
110
111 SkPaint paint;
112 paint.setAntiAlias(true);
113
114 SkAutoTUnref<SkCanvas> canvas(new_canvas(640, 480));
115 canvas.get()->drawPath(path, paint);
116#endif
117}
118
reed@google.comdceecc72012-02-23 19:20:19 +0000119// we used to assert if the bounds of the device (clip) was larger than 32K
120// even when the path itself was smaller. We just draw and hope in the debug
121// version to not assert.
122static void test_giantaa(skiatest::Reporter* reporter) {
123 const int W = 400;
124 const int H = 400;
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000125 SkAutoTUnref<SkCanvas> canvas(new_canvas(33000, 10));
126 canvas.get()->clear(0);
reed@google.comdceecc72012-02-23 19:20:19 +0000127
128 SkPaint paint;
129 paint.setAntiAlias(true);
130 SkPath path;
131 path.addOval(SkRect::MakeXYWH(-10, -10, 20 + W, 20 + H));
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000132 canvas.get()->drawPath(path, paint);
reed@google.comdceecc72012-02-23 19:20:19 +0000133}
134
135static void TestDrawPath(skiatest::Reporter* reporter) {
136 test_giantaa(reporter);
mike@reedtribe.org6093e652012-04-14 12:55:17 +0000137 test_bug533(reporter);
reed@google.coma90aa532012-04-16 16:27:09 +0000138 test_bigcubic(reporter);
reed@google.com1df888b2012-04-24 22:47:21 +0000139 test_crbug_124652(reporter);
reed@google.com9d5f76a2012-05-01 14:49:28 +0000140 test_inversepathwithclip(reporter);
reed@google.comdceecc72012-02-23 19:20:19 +0000141}
142
143#include "TestClassDef.h"
144DEFINE_TESTCLASS("DrawPath", TestDrawPathClass, TestDrawPath)