blob: f57e57c400775a0b6796c1ee169475d7171f2987 [file] [log] [blame]
tomhudson@google.comef279d32011-12-21 14:27:14 +00001/*
2 * Copyright 2011 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 "gm.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11
reed@google.com439df282013-08-15 20:57:42 +000012static void test_quadstroke(SkCanvas* canvas) {
13 SkPath path;
14 path.moveTo(6, 0);
15 path.quadTo(150, 150, 0, 6);
16
17 SkPaint paint;
skia.committer@gmail.comc9917c02013-08-16 07:01:49 +000018
reed@google.com439df282013-08-15 20:57:42 +000019 paint.setAntiAlias(true);
20 paint.setStyle(SkPaint::kStroke_Style);
21 canvas->translate(20, 20);
22
23#if 1
24 canvas->drawPath(path, paint);
25 canvas->translate(100, 0);
26#endif
skia.committer@gmail.comc9917c02013-08-16 07:01:49 +000027
reed@google.com439df282013-08-15 20:57:42 +000028 paint.setStrokeWidth(1.01f);
29 canvas->drawPath(path, paint);
30}
31
reed@google.com4e05fd22013-06-10 18:58:11 +000032static void draw_conic(SkCanvas* canvas, SkScalar weight, const SkPaint& paint) {
33 SkPath path;
34 path.moveTo(100, 100);
35 path.conicTo(300, 100, 300, 300, weight);
36 canvas->drawPath(path, paint);
37}
38
39static void test_conic(SkCanvas* canvas) {
40 SkPaint paint;
41 paint.setAntiAlias(true);
42 paint.setStyle(SkPaint::kStroke_Style);
43
44 static const struct {
45 SkScalar fWeight;
46 SkColor fColor;
47 } gRec[] = {
48 { 2 , SK_ColorRED },
49 { 1 , SK_ColorGREEN },
50 { 0.5f, SK_ColorBLUE },
51 };
skia.committer@gmail.com7fb53732013-06-11 07:01:17 +000052
reed@google.com7de1e3f2013-06-12 17:46:41 +000053 for (SkScalar width = 0; width <= 20; width += 20) {
54 canvas->save();
55 paint.setStrokeWidth(width);
56 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
57 paint.setColor(gRec[i].fColor);
58 draw_conic(canvas, gRec[i].fWeight, paint);
59 canvas->translate(-30, 30);
60 }
61 canvas->restore();
62 canvas->translate(300, 0);
reed@google.com4e05fd22013-06-10 18:58:11 +000063 }
64}
65
reed@google.com83f7c652013-02-04 16:56:15 +000066#include "SkGradientShader.h"
67static void test_shallow_gradient(SkCanvas* canvas, SkScalar width, SkScalar height) {
68 SkColor colors[] = { 0xFF7F7F7F, 0xFF7F7F7F, 0xFF000000 };
69 SkScalar pos[] = { 0, 0.35f, SK_Scalar1 };
70 SkPoint pts[] = { { 0, 0 }, { width, height } };
71 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos,
72 SK_ARRAY_COUNT(colors),
73 SkShader::kClamp_TileMode);
74 SkPaint paint;
75 paint.setShader(s)->unref();
76 canvas->drawPaint(paint);
77}
78
reed@google.comb6399a72013-01-28 16:57:29 +000079#include "SkDashPathEffect.h"
80static void test_giant_dash(SkCanvas* canvas) {
81 SkPaint paint;
82 const SkScalar intervals[] = { SK_Scalar1, SK_Scalar1 };
83
84 paint.setStrokeWidth(2);
85 paint.setPathEffect(new SkDashPathEffect(intervals, 2, 0))->unref();
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +000086
reed@google.comb6399a72013-01-28 16:57:29 +000087 SkScalar big = 500 * 1000;
88
89 canvas->drawLine(10, 10, big, 10, paint);
90 canvas->drawLine(-big, 20, 500, 20, paint);
91 canvas->drawLine(-big, 30, big, 30, paint);
92
93 const SkScalar intervals2[] = { 20, 5, 10, 5 };
94 paint.setPathEffect(new SkDashPathEffect(intervals2, 4, 17))->unref();
95
96 canvas->translate(0, 40);
97 SkScalar x = -500;
98 SkScalar width = 3173;
99 for (int i = 0; i < 40; ++i) {
100 if (i > 10)
101 canvas->drawLine(x, 0, x + width, 0, paint);
102 x += 1;
103 canvas->translate(0, 4);
104 }
105}
106
reed@google.com83f7c652013-02-04 16:56:15 +0000107
108
reed@google.come2f8c7c2012-11-27 21:11:05 +0000109// Reproduces bug found here: http://jsfiddle.net/R8Cu5/1/
110//
111#include "SkGradientShader.h"
112static void test_grad(SkCanvas* canvas) {
113 SkPoint pts[] = {
114 { 478.544067f, -84.2041016f },
115 { 602.455933f, 625.204102f },
116 };
117 SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorRED, SK_ColorRED };
118 SkScalar pos[] = { 0, 0.3f, 0.3f, 1.0f };
119 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode);
120 SkPaint p;
121 p.setShader(s)->unref();
122 canvas->drawPaint(p);
123}
124
reed@google.com5aff0ef2012-05-09 15:04:11 +0000125static SkCanvas* MakeCanvas(const SkIRect& bounds) {
126 SkBitmap bm;
127 bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
128 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000129 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.com5aff0ef2012-05-09 15:04:11 +0000130
131 SkCanvas* canvas = new SkCanvas(bm);
132 canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
133 return canvas;
134}
135
humper@google.com0e515772013-01-07 19:54:40 +0000136#ifdef SK_DEBUG
reed@google.com5aff0ef2012-05-09 15:04:11 +0000137static void GetBitmap(const SkCanvas* canvas, SkBitmap* bm) {
138 *bm = canvas->getDevice()->accessBitmap(false);
139}
humper@google.com0e515772013-01-07 19:54:40 +0000140#endif
reed@google.com5aff0ef2012-05-09 15:04:11 +0000141
142static void compare_canvas(const SkCanvas* a, const SkCanvas* b) {
humper@google.com0e515772013-01-07 19:54:40 +0000143#ifdef SK_DEBUG
reed@google.com5aff0ef2012-05-09 15:04:11 +0000144 SkBitmap bma, bmb;
145 GetBitmap(a, &bma);
146 GetBitmap(b, &bmb);
147
148 SkASSERT(bma.width() == bmb.width());
149 SkASSERT(bma.height() == bmb.height());
150
151 bma.lockPixels();
152 bmb.lockPixels();
153 for (int y = 0; y < bma.height(); ++y) {
154 const SkPMColor* rowa = bma.getAddr32(0, y);
155 const SkPMColor* rowb = bmb.getAddr32(0, y);
156 SkASSERT(!memcmp(rowa, rowb, bma.width() << 2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000157
reed@google.com5aff0ef2012-05-09 15:04:11 +0000158 for (int x = 1; x < bma.width() - 1; ++x) {
159 SkASSERT(0xFF000000 == rowa[x]);
160 SkASSERT(0xFF000000 == rowb[x]);
161 }
162 }
humper@google.com0e515772013-01-07 19:54:40 +0000163#endif
reed@google.com5aff0ef2012-05-09 15:04:11 +0000164}
165
166static void drawRectAsPath(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
167 SkPath path;
168 path.addRect(r);
169 canvas->drawPath(path, p);
170}
171
172static void test_maskFromPath(const SkPath& path) {
173 SkIRect bounds;
174 path.getBounds().roundOut(&bounds);
175
176 SkPaint paint;
177 paint.setAntiAlias(true);
178
179 SkAutoTUnref<SkCanvas> path_canvas(MakeCanvas(bounds));
180 path_canvas->drawPath(path, paint);
181
182 SkAutoTUnref<SkCanvas> rect_canvas(MakeCanvas(bounds));
183 drawRectAsPath(rect_canvas, path.getBounds(), paint);
184
185 compare_canvas(path_canvas, rect_canvas);
186}
187
188static void test_mask() {
189 for (int i = 1; i <= 20; ++i) {
190 const SkScalar dx = SK_Scalar1 / i;
191 const SkRect constr = SkRect::MakeWH(dx, SkIntToScalar(2));
192 for (int n = 2; n < 20; ++n) {
193 SkPath path;
194 path.setFillType(SkPath::kEvenOdd_FillType);
195 SkRect r = constr;
196 while (r.fRight < SkIntToScalar(4)) {
197 path.addRect(r);
198 r.offset(dx, 0);
199 }
200 test_maskFromPath(path);
201 }
202 }
203}
204
tomhudson@google.comef279d32011-12-21 14:27:14 +0000205/** Draw a 2px border around the target, then red behind the target;
206 set the clip to match the target, then draw >> the target in blue.
207*/
208
caryclark@google.com13130862012-06-06 12:10:45 +0000209static void draw (SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000210 SkPaint borderPaint;
211 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
212 borderPaint.setAntiAlias(true);
213 SkPaint backgroundPaint;
214 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
215 backgroundPaint.setAntiAlias(true);
216 SkPaint foregroundPaint;
217 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
218 foregroundPaint.setAntiAlias(true);
219
220 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000221 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
222 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000223 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000224 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000225 canvas->drawRect(target, backgroundPaint);
226 canvas->clipRect(target, SkRegion::kIntersect_Op, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000227 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000228 canvas->drawRect(target, foregroundPaint);
229 canvas->restore();
230}
231
caryclark@google.com13130862012-06-06 12:10:45 +0000232static void draw_square (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000233 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
234 draw(canvas, target, x, y);
235}
236
caryclark@google.com13130862012-06-06 12:10:45 +0000237static void draw_column (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000238 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
239 draw(canvas, target, x, y);
240}
241
caryclark@google.com13130862012-06-06 12:10:45 +0000242static void draw_bar (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000243 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
244 draw(canvas, target, x, y);
245}
246
caryclark@google.com13130862012-06-06 12:10:45 +0000247static void draw_rect_tests (SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000248 draw_square(canvas, 10, 10);
249 draw_column(canvas, 30, 10);
250 draw_bar(canvas, 10, 30);
251}
252
253/**
254 Test a set of clipping problems discovered while writing blitAntiRect,
255 and test all the code paths through the clipping blitters.
256 Each region should show as a blue center surrounded by a 2px green
257 border, with no red.
258*/
259
reed@google.comff26b7e2013-05-23 17:04:19 +0000260class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000261public:
262 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000263
tomhudson@google.comef279d32011-12-21 14:27:14 +0000264 }
265
266protected:
reed@google.comff26b7e2013-05-23 17:04:19 +0000267 virtual SkString onShortName() SK_OVERRIDE {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000268 return SkString("aaclip");
269 }
270
reed@google.comff26b7e2013-05-23 17:04:19 +0000271 virtual SkISize onISize() SK_OVERRIDE {
272 return SkISize::Make(640, 480);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000273 }
274
reed@google.comb6399a72013-01-28 16:57:29 +0000275 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com439df282013-08-15 20:57:42 +0000276 if (false) { test_quadstroke(canvas); return; }
reed@google.com4e05fd22013-06-10 18:58:11 +0000277 if (false) { test_conic(canvas); return; }
reed@google.comb6399a72013-01-28 16:57:29 +0000278 if (false) {
reed@google.com83f7c652013-02-04 16:56:15 +0000279 SkRect bounds;
280 canvas->getClipBounds(&bounds);
281 test_shallow_gradient(canvas, bounds.width(), bounds.height()); return;
282 }
283 if (false) {
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000284 test_giant_dash(canvas); return;
reed@google.comb6399a72013-01-28 16:57:29 +0000285 }
reed@google.come2f8c7c2012-11-27 21:11:05 +0000286 if (false) {
287 test_grad(canvas); return;
288 }
caryclark@google.com13130862012-06-06 12:10:45 +0000289 if (false) { // avoid bit rot, suppress warning
290 test_mask();
291 }
reed@google.com5aff0ef2012-05-09 15:04:11 +0000292
tomhudson@google.comef279d32011-12-21 14:27:14 +0000293 // Initial pixel-boundary-aligned draw
294 draw_rect_tests(canvas);
295
296 // Repeat 4x with .2, .4, .6, .8 px offsets
297 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000298 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000299 draw_rect_tests(canvas);
300
301 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000302 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000303 draw_rect_tests(canvas);
304
305 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000306 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000307 draw_rect_tests(canvas);
308
309 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000310 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000311 draw_rect_tests(canvas);
312 }
313
borenet@google.comb4e70432012-07-20 18:45:10 +0000314 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
315
tomhudson@google.comef279d32011-12-21 14:27:14 +0000316private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000317 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000318};
319
reed@google.comff26b7e2013-05-23 17:04:19 +0000320DEF_GM( return SkNEW(AAClipGM); )