blob: 8acd04d1bab1ae52c455ff885502580586292093 [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.come2f8c7c2012-11-27 21:11:05 +000012// Reproduces bug found here: http://jsfiddle.net/R8Cu5/1/
13//
14#include "SkGradientShader.h"
15static void test_grad(SkCanvas* canvas) {
16 SkPoint pts[] = {
17 { 478.544067f, -84.2041016f },
18 { 602.455933f, 625.204102f },
19 };
20 SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorRED, SK_ColorRED };
21 SkScalar pos[] = { 0, 0.3f, 0.3f, 1.0f };
22 SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode);
23 SkPaint p;
24 p.setShader(s)->unref();
25 canvas->drawPaint(p);
26}
27
reed@google.com5aff0ef2012-05-09 15:04:11 +000028static SkCanvas* MakeCanvas(const SkIRect& bounds) {
29 SkBitmap bm;
30 bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
31 bm.allocPixels();
32 bm.eraseColor(0);
33
34 SkCanvas* canvas = new SkCanvas(bm);
35 canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
36 return canvas;
37}
38
39static void GetBitmap(const SkCanvas* canvas, SkBitmap* bm) {
40 *bm = canvas->getDevice()->accessBitmap(false);
41}
42
43static void compare_canvas(const SkCanvas* a, const SkCanvas* b) {
44 SkBitmap bma, bmb;
45 GetBitmap(a, &bma);
46 GetBitmap(b, &bmb);
47
48 SkASSERT(bma.width() == bmb.width());
49 SkASSERT(bma.height() == bmb.height());
50
51 bma.lockPixels();
52 bmb.lockPixels();
53 for (int y = 0; y < bma.height(); ++y) {
54 const SkPMColor* rowa = bma.getAddr32(0, y);
55 const SkPMColor* rowb = bmb.getAddr32(0, y);
56 SkASSERT(!memcmp(rowa, rowb, bma.width() << 2));
rmistry@google.comd6176b02012-08-23 18:14:13 +000057
reed@google.com5aff0ef2012-05-09 15:04:11 +000058 for (int x = 1; x < bma.width() - 1; ++x) {
59 SkASSERT(0xFF000000 == rowa[x]);
60 SkASSERT(0xFF000000 == rowb[x]);
61 }
62 }
63}
64
65static void drawRectAsPath(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
66 SkPath path;
67 path.addRect(r);
68 canvas->drawPath(path, p);
69}
70
71static void test_maskFromPath(const SkPath& path) {
72 SkIRect bounds;
73 path.getBounds().roundOut(&bounds);
74
75 SkPaint paint;
76 paint.setAntiAlias(true);
77
78 SkAutoTUnref<SkCanvas> path_canvas(MakeCanvas(bounds));
79 path_canvas->drawPath(path, paint);
80
81 SkAutoTUnref<SkCanvas> rect_canvas(MakeCanvas(bounds));
82 drawRectAsPath(rect_canvas, path.getBounds(), paint);
83
84 compare_canvas(path_canvas, rect_canvas);
85}
86
87static void test_mask() {
88 for (int i = 1; i <= 20; ++i) {
89 const SkScalar dx = SK_Scalar1 / i;
90 const SkRect constr = SkRect::MakeWH(dx, SkIntToScalar(2));
91 for (int n = 2; n < 20; ++n) {
92 SkPath path;
93 path.setFillType(SkPath::kEvenOdd_FillType);
94 SkRect r = constr;
95 while (r.fRight < SkIntToScalar(4)) {
96 path.addRect(r);
97 r.offset(dx, 0);
98 }
99 test_maskFromPath(path);
100 }
101 }
102}
103
tomhudson@google.comef279d32011-12-21 14:27:14 +0000104namespace skiagm {
105
106/** Draw a 2px border around the target, then red behind the target;
107 set the clip to match the target, then draw >> the target in blue.
108*/
109
caryclark@google.com13130862012-06-06 12:10:45 +0000110static void draw (SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000111 SkPaint borderPaint;
112 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
113 borderPaint.setAntiAlias(true);
114 SkPaint backgroundPaint;
115 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
116 backgroundPaint.setAntiAlias(true);
117 SkPaint foregroundPaint;
118 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
119 foregroundPaint.setAntiAlias(true);
120
121 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000122 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
123 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000124 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000125 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000126 canvas->drawRect(target, backgroundPaint);
127 canvas->clipRect(target, SkRegion::kIntersect_Op, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000128 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +0000129 canvas->drawRect(target, foregroundPaint);
130 canvas->restore();
131}
132
caryclark@google.com13130862012-06-06 12:10:45 +0000133static void draw_square (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000134 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
135 draw(canvas, target, x, y);
136}
137
caryclark@google.com13130862012-06-06 12:10:45 +0000138static void draw_column (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000139 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
140 draw(canvas, target, x, y);
141}
142
caryclark@google.com13130862012-06-06 12:10:45 +0000143static void draw_bar (SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000144 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
145 draw(canvas, target, x, y);
146}
147
caryclark@google.com13130862012-06-06 12:10:45 +0000148static void draw_rect_tests (SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +0000149 draw_square(canvas, 10, 10);
150 draw_column(canvas, 30, 10);
151 draw_bar(canvas, 10, 30);
152}
153
154/**
155 Test a set of clipping problems discovered while writing blitAntiRect,
156 and test all the code paths through the clipping blitters.
157 Each region should show as a blue center surrounded by a 2px green
158 border, with no red.
159*/
160
161class AAClipGM : public GM {
162public:
163 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +0000164
tomhudson@google.comef279d32011-12-21 14:27:14 +0000165 }
166
167protected:
168 virtual SkString onShortName() {
169 return SkString("aaclip");
170 }
171
172 virtual SkISize onISize() {
173 return make_isize(640, 480);
174 }
175
176 virtual void onDraw(SkCanvas* canvas) {
reed@google.come2f8c7c2012-11-27 21:11:05 +0000177 if (false) {
178 test_grad(canvas); return;
179 }
caryclark@google.com13130862012-06-06 12:10:45 +0000180 if (false) { // avoid bit rot, suppress warning
181 test_mask();
182 }
reed@google.com5aff0ef2012-05-09 15:04:11 +0000183
tomhudson@google.comef279d32011-12-21 14:27:14 +0000184 // Initial pixel-boundary-aligned draw
185 draw_rect_tests(canvas);
186
187 // Repeat 4x with .2, .4, .6, .8 px offsets
188 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000189 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000190 draw_rect_tests(canvas);
191
192 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000193 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000194 draw_rect_tests(canvas);
195
196 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000197 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000198 draw_rect_tests(canvas);
199
200 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000201 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000202 draw_rect_tests(canvas);
203 }
204
borenet@google.comb4e70432012-07-20 18:45:10 +0000205 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
206
tomhudson@google.comef279d32011-12-21 14:27:14 +0000207private:
208 typedef GM INHERITED;
209};
210
211//////////////////////////////////////////////////////////////////////////////
212
213static GM* MyFactory(void*) { return new AAClipGM; }
214static GMRegistry reg(MyFactory);
215
216}