blob: f9cecf4e420a21ae8ae060b278687b9255dc399c [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.com83f7c652013-02-04 16:56:15 +000012#include "SkGradientShader.h"
13static void test_shallow_gradient(SkCanvas* canvas, SkScalar width, SkScalar height) {
14 SkColor colors[] = { 0xFF7F7F7F, 0xFF7F7F7F, 0xFF000000 };
15 SkScalar pos[] = { 0, 0.35f, SK_Scalar1 };
16 SkPoint pts[] = { { 0, 0 }, { width, height } };
17 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos,
18 SK_ARRAY_COUNT(colors),
19 SkShader::kClamp_TileMode);
20 SkPaint paint;
21 paint.setShader(s)->unref();
22 canvas->drawPaint(paint);
23}
24
reed@google.comb6399a72013-01-28 16:57:29 +000025#include "SkDashPathEffect.h"
26static void test_giant_dash(SkCanvas* canvas) {
27 SkPaint paint;
28 const SkScalar intervals[] = { SK_Scalar1, SK_Scalar1 };
29
30 paint.setStrokeWidth(2);
31 paint.setPathEffect(new SkDashPathEffect(intervals, 2, 0))->unref();
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +000032
reed@google.comb6399a72013-01-28 16:57:29 +000033 SkScalar big = 500 * 1000;
34
35 canvas->drawLine(10, 10, big, 10, paint);
36 canvas->drawLine(-big, 20, 500, 20, paint);
37 canvas->drawLine(-big, 30, big, 30, paint);
38
39 const SkScalar intervals2[] = { 20, 5, 10, 5 };
40 paint.setPathEffect(new SkDashPathEffect(intervals2, 4, 17))->unref();
41
42 canvas->translate(0, 40);
43 SkScalar x = -500;
44 SkScalar width = 3173;
45 for (int i = 0; i < 40; ++i) {
46 if (i > 10)
47 canvas->drawLine(x, 0, x + width, 0, paint);
48 x += 1;
49 canvas->translate(0, 4);
50 }
51}
52
reed@google.com83f7c652013-02-04 16:56:15 +000053
54
reed@google.come2f8c7c2012-11-27 21:11:05 +000055// Reproduces bug found here: http://jsfiddle.net/R8Cu5/1/
56//
57#include "SkGradientShader.h"
58static void test_grad(SkCanvas* canvas) {
59 SkPoint pts[] = {
60 { 478.544067f, -84.2041016f },
61 { 602.455933f, 625.204102f },
62 };
63 SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorRED, SK_ColorRED };
64 SkScalar pos[] = { 0, 0.3f, 0.3f, 1.0f };
65 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode);
66 SkPaint p;
67 p.setShader(s)->unref();
68 canvas->drawPaint(p);
69}
70
reed@google.com5aff0ef2012-05-09 15:04:11 +000071static SkCanvas* MakeCanvas(const SkIRect& bounds) {
72 SkBitmap bm;
73 bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
74 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000075 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.com5aff0ef2012-05-09 15:04:11 +000076
77 SkCanvas* canvas = new SkCanvas(bm);
78 canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
79 return canvas;
80}
81
humper@google.com0e515772013-01-07 19:54:40 +000082#ifdef SK_DEBUG
reed@google.com5aff0ef2012-05-09 15:04:11 +000083static void GetBitmap(const SkCanvas* canvas, SkBitmap* bm) {
84 *bm = canvas->getDevice()->accessBitmap(false);
85}
humper@google.com0e515772013-01-07 19:54:40 +000086#endif
reed@google.com5aff0ef2012-05-09 15:04:11 +000087
88static void compare_canvas(const SkCanvas* a, const SkCanvas* b) {
humper@google.com0e515772013-01-07 19:54:40 +000089#ifdef SK_DEBUG
reed@google.com5aff0ef2012-05-09 15:04:11 +000090 SkBitmap bma, bmb;
91 GetBitmap(a, &bma);
92 GetBitmap(b, &bmb);
93
94 SkASSERT(bma.width() == bmb.width());
95 SkASSERT(bma.height() == bmb.height());
96
97 bma.lockPixels();
98 bmb.lockPixels();
99 for (int y = 0; y < bma.height(); ++y) {
100 const SkPMColor* rowa = bma.getAddr32(0, y);
101 const SkPMColor* rowb = bmb.getAddr32(0, y);
102 SkASSERT(!memcmp(rowa, rowb, bma.width() << 2));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
reed@google.com5aff0ef2012-05-09 15:04:11 +0000104 for (int x = 1; x < bma.width() - 1; ++x) {
105 SkASSERT(0xFF000000 == rowa[x]);
106 SkASSERT(0xFF000000 == rowb[x]);
107 }
108 }
humper@google.com0e515772013-01-07 19:54:40 +0000109#endif
reed@google.com5aff0ef2012-05-09 15:04:11 +0000110}
111
112static void drawRectAsPath(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
113 SkPath path;
114 path.addRect(r);
115 canvas->drawPath(path, p);
116}
117
118static void test_maskFromPath(const SkPath& path) {
119 SkIRect bounds;
120 path.getBounds().roundOut(&bounds);
121
122 SkPaint paint;
123 paint.setAntiAlias(true);
124
125 SkAutoTUnref<SkCanvas> path_canvas(MakeCanvas(bounds));
126 path_canvas->drawPath(path, paint);
127
128 SkAutoTUnref<SkCanvas> rect_canvas(MakeCanvas(bounds));
129 drawRectAsPath(rect_canvas, path.getBounds(), paint);
130
131 compare_canvas(path_canvas, rect_canvas);
132}
133
134static void test_mask() {
135 for (int i = 1; i <= 20; ++i) {
136 const SkScalar dx = SK_Scalar1 / i;
137 const SkRect constr = SkRect::MakeWH(dx, SkIntToScalar(2));
138 for (int n = 2; n < 20; ++n) {
139 SkPath path;
140 path.setFillType(SkPath::kEvenOdd_FillType);
141 SkRect r = constr;
142 while (r.fRight < SkIntToScalar(4)) {
143 path.addRect(r);
144 r.offset(dx, 0);
145 }
146 test_maskFromPath(path);
147 }
148 }
149}
150
tomhudson@google.comef279d32011-12-21 14:27:14 +0000151namespace skiagm {
152
153/** Draw a 2px border around the target, then red behind the target;
154 set the clip to match the target, then draw >> the target in blue.
155*/
156
caryclark@google.com13130862012-06-06 12:10:45 +0000157static void draw (SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000158 SkPaint borderPaint;
159 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
160 borderPaint.setAntiAlias(true);
161 SkPaint backgroundPaint;
162 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
163 backgroundPaint.setAntiAlias(true);
164 SkPaint foregroundPaint;
165 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
166 foregroundPaint.setAntiAlias(true);
167
168 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000169 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
170 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000171 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000172 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000173 canvas->drawRect(target, backgroundPaint);
174 canvas->clipRect(target, SkRegion::kIntersect_Op, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000175 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000176 canvas->drawRect(target, foregroundPaint);
177 canvas->restore();
178}
179
caryclark@google.com13130862012-06-06 12:10:45 +0000180static void draw_square (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000181 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
182 draw(canvas, target, x, y);
183}
184
caryclark@google.com13130862012-06-06 12:10:45 +0000185static void draw_column (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000186 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
187 draw(canvas, target, x, y);
188}
189
caryclark@google.com13130862012-06-06 12:10:45 +0000190static void draw_bar (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000191 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
192 draw(canvas, target, x, y);
193}
194
caryclark@google.com13130862012-06-06 12:10:45 +0000195static void draw_rect_tests (SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000196 draw_square(canvas, 10, 10);
197 draw_column(canvas, 30, 10);
198 draw_bar(canvas, 10, 30);
199}
200
201/**
202 Test a set of clipping problems discovered while writing blitAntiRect,
203 and test all the code paths through the clipping blitters.
204 Each region should show as a blue center surrounded by a 2px green
205 border, with no red.
206*/
207
208class AAClipGM : public GM {
209public:
210 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000211
tomhudson@google.comef279d32011-12-21 14:27:14 +0000212 }
213
214protected:
215 virtual SkString onShortName() {
216 return SkString("aaclip");
217 }
218
219 virtual SkISize onISize() {
220 return make_isize(640, 480);
221 }
222
reed@google.comb6399a72013-01-28 16:57:29 +0000223 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
224 if (false) {
reed@google.com83f7c652013-02-04 16:56:15 +0000225 SkRect bounds;
226 canvas->getClipBounds(&bounds);
227 test_shallow_gradient(canvas, bounds.width(), bounds.height()); return;
228 }
229 if (false) {
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000230 test_giant_dash(canvas); return;
reed@google.comb6399a72013-01-28 16:57:29 +0000231 }
reed@google.come2f8c7c2012-11-27 21:11:05 +0000232 if (false) {
233 test_grad(canvas); return;
234 }
caryclark@google.com13130862012-06-06 12:10:45 +0000235 if (false) { // avoid bit rot, suppress warning
236 test_mask();
237 }
reed@google.com5aff0ef2012-05-09 15:04:11 +0000238
tomhudson@google.comef279d32011-12-21 14:27:14 +0000239 // Initial pixel-boundary-aligned draw
240 draw_rect_tests(canvas);
241
242 // Repeat 4x with .2, .4, .6, .8 px offsets
243 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000244 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000245 draw_rect_tests(canvas);
246
247 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000248 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000249 draw_rect_tests(canvas);
250
251 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000252 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000253 draw_rect_tests(canvas);
254
255 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000256 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000257 draw_rect_tests(canvas);
258 }
259
borenet@google.comb4e70432012-07-20 18:45:10 +0000260 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
261
tomhudson@google.comef279d32011-12-21 14:27:14 +0000262private:
263 typedef GM INHERITED;
264};
265
266//////////////////////////////////////////////////////////////////////////////
267
268static GM* MyFactory(void*) { return new AAClipGM; }
269static GMRegistry reg(MyFactory);
270
271}