blob: 07e4085d696bc35304e1d2d2d3b73386ad2a4219 [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.comb6399a72013-01-28 16:57:29 +000012#include "SkDashPathEffect.h"
13static void test_giant_dash(SkCanvas* canvas) {
14 SkPaint paint;
15 const SkScalar intervals[] = { SK_Scalar1, SK_Scalar1 };
16
17 paint.setStrokeWidth(2);
18 paint.setPathEffect(new SkDashPathEffect(intervals, 2, 0))->unref();
19
20 SkScalar big = 500 * 1000;
21
22 canvas->drawLine(10, 10, big, 10, paint);
23 canvas->drawLine(-big, 20, 500, 20, paint);
24 canvas->drawLine(-big, 30, big, 30, paint);
25
26 const SkScalar intervals2[] = { 20, 5, 10, 5 };
27 paint.setPathEffect(new SkDashPathEffect(intervals2, 4, 17))->unref();
28
29 canvas->translate(0, 40);
30 SkScalar x = -500;
31 SkScalar width = 3173;
32 for (int i = 0; i < 40; ++i) {
33 if (i > 10)
34 canvas->drawLine(x, 0, x + width, 0, paint);
35 x += 1;
36 canvas->translate(0, 4);
37 }
38}
39
reed@google.come2f8c7c2012-11-27 21:11:05 +000040// Reproduces bug found here: http://jsfiddle.net/R8Cu5/1/
41//
42#include "SkGradientShader.h"
43static void test_grad(SkCanvas* canvas) {
44 SkPoint pts[] = {
45 { 478.544067f, -84.2041016f },
46 { 602.455933f, 625.204102f },
47 };
48 SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorRED, SK_ColorRED };
49 SkScalar pos[] = { 0, 0.3f, 0.3f, 1.0f };
50 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode);
51 SkPaint p;
52 p.setShader(s)->unref();
53 canvas->drawPaint(p);
54}
55
reed@google.com5aff0ef2012-05-09 15:04:11 +000056static SkCanvas* MakeCanvas(const SkIRect& bounds) {
57 SkBitmap bm;
58 bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
59 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000060 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.com5aff0ef2012-05-09 15:04:11 +000061
62 SkCanvas* canvas = new SkCanvas(bm);
63 canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
64 return canvas;
65}
66
humper@google.com0e515772013-01-07 19:54:40 +000067#ifdef SK_DEBUG
reed@google.com5aff0ef2012-05-09 15:04:11 +000068static void GetBitmap(const SkCanvas* canvas, SkBitmap* bm) {
69 *bm = canvas->getDevice()->accessBitmap(false);
70}
humper@google.com0e515772013-01-07 19:54:40 +000071#endif
reed@google.com5aff0ef2012-05-09 15:04:11 +000072
73static void compare_canvas(const SkCanvas* a, const SkCanvas* b) {
humper@google.com0e515772013-01-07 19:54:40 +000074#ifdef SK_DEBUG
reed@google.com5aff0ef2012-05-09 15:04:11 +000075 SkBitmap bma, bmb;
76 GetBitmap(a, &bma);
77 GetBitmap(b, &bmb);
78
79 SkASSERT(bma.width() == bmb.width());
80 SkASSERT(bma.height() == bmb.height());
81
82 bma.lockPixels();
83 bmb.lockPixels();
84 for (int y = 0; y < bma.height(); ++y) {
85 const SkPMColor* rowa = bma.getAddr32(0, y);
86 const SkPMColor* rowb = bmb.getAddr32(0, y);
87 SkASSERT(!memcmp(rowa, rowb, bma.width() << 2));
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
reed@google.com5aff0ef2012-05-09 15:04:11 +000089 for (int x = 1; x < bma.width() - 1; ++x) {
90 SkASSERT(0xFF000000 == rowa[x]);
91 SkASSERT(0xFF000000 == rowb[x]);
92 }
93 }
humper@google.com0e515772013-01-07 19:54:40 +000094#endif
reed@google.com5aff0ef2012-05-09 15:04:11 +000095}
96
97static void drawRectAsPath(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
98 SkPath path;
99 path.addRect(r);
100 canvas->drawPath(path, p);
101}
102
103static void test_maskFromPath(const SkPath& path) {
104 SkIRect bounds;
105 path.getBounds().roundOut(&bounds);
106
107 SkPaint paint;
108 paint.setAntiAlias(true);
109
110 SkAutoTUnref<SkCanvas> path_canvas(MakeCanvas(bounds));
111 path_canvas->drawPath(path, paint);
112
113 SkAutoTUnref<SkCanvas> rect_canvas(MakeCanvas(bounds));
114 drawRectAsPath(rect_canvas, path.getBounds(), paint);
115
116 compare_canvas(path_canvas, rect_canvas);
117}
118
119static void test_mask() {
120 for (int i = 1; i <= 20; ++i) {
121 const SkScalar dx = SK_Scalar1 / i;
122 const SkRect constr = SkRect::MakeWH(dx, SkIntToScalar(2));
123 for (int n = 2; n < 20; ++n) {
124 SkPath path;
125 path.setFillType(SkPath::kEvenOdd_FillType);
126 SkRect r = constr;
127 while (r.fRight < SkIntToScalar(4)) {
128 path.addRect(r);
129 r.offset(dx, 0);
130 }
131 test_maskFromPath(path);
132 }
133 }
134}
135
tomhudson@google.comef279d32011-12-21 14:27:14 +0000136namespace skiagm {
137
138/** Draw a 2px border around the target, then red behind the target;
139 set the clip to match the target, then draw >> the target in blue.
140*/
141
caryclark@google.com13130862012-06-06 12:10:45 +0000142static void draw (SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000143 SkPaint borderPaint;
144 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
145 borderPaint.setAntiAlias(true);
146 SkPaint backgroundPaint;
147 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
148 backgroundPaint.setAntiAlias(true);
149 SkPaint foregroundPaint;
150 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
151 foregroundPaint.setAntiAlias(true);
152
153 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000154 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
155 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000156 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000157 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000158 canvas->drawRect(target, backgroundPaint);
159 canvas->clipRect(target, SkRegion::kIntersect_Op, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000160 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000161 canvas->drawRect(target, foregroundPaint);
162 canvas->restore();
163}
164
caryclark@google.com13130862012-06-06 12:10:45 +0000165static void draw_square (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000166 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
167 draw(canvas, target, x, y);
168}
169
caryclark@google.com13130862012-06-06 12:10:45 +0000170static void draw_column (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000171 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
172 draw(canvas, target, x, y);
173}
174
caryclark@google.com13130862012-06-06 12:10:45 +0000175static void draw_bar (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000176 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
177 draw(canvas, target, x, y);
178}
179
caryclark@google.com13130862012-06-06 12:10:45 +0000180static void draw_rect_tests (SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000181 draw_square(canvas, 10, 10);
182 draw_column(canvas, 30, 10);
183 draw_bar(canvas, 10, 30);
184}
185
186/**
187 Test a set of clipping problems discovered while writing blitAntiRect,
188 and test all the code paths through the clipping blitters.
189 Each region should show as a blue center surrounded by a 2px green
190 border, with no red.
191*/
192
193class AAClipGM : public GM {
194public:
195 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000196
tomhudson@google.comef279d32011-12-21 14:27:14 +0000197 }
198
199protected:
200 virtual SkString onShortName() {
201 return SkString("aaclip");
202 }
203
204 virtual SkISize onISize() {
205 return make_isize(640, 480);
206 }
207
reed@google.comb6399a72013-01-28 16:57:29 +0000208 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
209 if (false) {
210 test_giant_dash(canvas); return;
211 }
reed@google.come2f8c7c2012-11-27 21:11:05 +0000212 if (false) {
213 test_grad(canvas); return;
214 }
caryclark@google.com13130862012-06-06 12:10:45 +0000215 if (false) { // avoid bit rot, suppress warning
216 test_mask();
217 }
reed@google.com5aff0ef2012-05-09 15:04:11 +0000218
tomhudson@google.comef279d32011-12-21 14:27:14 +0000219 // Initial pixel-boundary-aligned draw
220 draw_rect_tests(canvas);
221
222 // Repeat 4x with .2, .4, .6, .8 px offsets
223 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000224 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000225 draw_rect_tests(canvas);
226
227 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000228 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000229 draw_rect_tests(canvas);
230
231 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000232 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000233 draw_rect_tests(canvas);
234
235 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000236 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000237 draw_rect_tests(canvas);
238 }
239
borenet@google.comb4e70432012-07-20 18:45:10 +0000240 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
241
tomhudson@google.comef279d32011-12-21 14:27:14 +0000242private:
243 typedef GM INHERITED;
244};
245
246//////////////////////////////////////////////////////////////////////////////
247
248static GM* MyFactory(void*) { return new AAClipGM; }
249static GMRegistry reg(MyFactory);
250
251}