blob: fb70dd76b48a219672220447f9e5f0a2ecdf8672 [file] [log] [blame]
robertphillips@google.com14c3fc62013-03-19 14:11:15 +00001/*
2 * Copyright 2013 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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurDrawLooper.h"
10#include "SkBlurMask.h"
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000011#include "SkBlurMaskFilter.h"
12#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000013#include "SkMatrix.h"
14#include "SkTArray.h"
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000015
16namespace skiagm {
17
18class RectsGM : public GM {
19public:
20 RectsGM() {
21 this->setBGColor(0xFF000000);
22 this->makePaints();
23 this->makeMatrices();
24 this->makeRects();
25 }
26
27protected:
28 virtual SkString onShortName() SK_OVERRIDE {
29 return SkString("rects");
30 }
31
32 virtual SkISize onISize() SK_OVERRIDE {
33 return make_isize(1200, 900);
34 }
35
36 void makePaints() {
37 {
38 // no AA
39 SkPaint p;
40 p.setColor(SK_ColorWHITE);
41 fPaints.push_back(p);
42 }
43
44 {
45 // AA
46 SkPaint p;
47 p.setColor(SK_ColorWHITE);
48 p.setAntiAlias(true);
49 fPaints.push_back(p);
50 }
51
52 {
53 // AA with mask filter
54 SkPaint p;
55 p.setColor(SK_ColorWHITE);
56 p.setAntiAlias(true);
robertphillips@google.comb7061172013-09-06 14:16:12 +000057 SkMaskFilter* mf = SkBlurMaskFilter::Create(
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000058 SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000059 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000060 SkBlurMaskFilter::kHighQuality_BlurFlag);
61 p.setMaskFilter(mf)->unref();
62 fPaints.push_back(p);
63 }
64
65 {
66 // AA with radial shader
67 SkPaint p;
68 p.setColor(SK_ColorWHITE);
69 p.setAntiAlias(true);
70 SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
71 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
72 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
73 SkShader* s = SkGradientShader::CreateRadial(center,
74 SkIntToScalar(20),
75 colors,
76 pos,
77 SK_ARRAY_COUNT(colors),
78 SkShader::kClamp_TileMode);
79 p.setShader(s)->unref();
80 fPaints.push_back(p);
81 }
82
83 {
84 // AA with blur
85 SkPaint p;
86 p.setColor(SK_ColorWHITE);
87 p.setAntiAlias(true);
88 SkBlurDrawLooper* shadowLooper =
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000089 new SkBlurDrawLooper (SK_ColorWHITE,
90 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
robertphillips@google.comb7061172013-09-06 14:16:12 +000091 SkIntToScalar(5), SkIntToScalar(10),
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000092 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
93 SkBlurDrawLooper::kOverrideColor_BlurFlag |
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000094 SkBlurDrawLooper::kHighQuality_BlurFlag);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000095 SkAutoUnref aurL0(shadowLooper);
96 p.setLooper(shadowLooper);
97 fPaints.push_back(p);
98 }
99
100 {
101 // AA with stroke style
102 SkPaint p;
103 p.setColor(SK_ColorWHITE);
104 p.setAntiAlias(true);
105 p.setStyle(SkPaint::kStroke_Style);
106 p.setStrokeWidth(SkIntToScalar(3));
107 fPaints.push_back(p);
108 }
109
110 {
111 // AA with stroke style, width = 0
112 SkPaint p;
113 p.setColor(SK_ColorWHITE);
114 p.setAntiAlias(true);
115 p.setStyle(SkPaint::kStroke_Style);
116 fPaints.push_back(p);
117 }
118
119 {
120 // AA with stroke and fill style
121 SkPaint p;
122 p.setColor(SK_ColorWHITE);
123 p.setAntiAlias(true);
124 p.setStyle(SkPaint::kStrokeAndFill_Style);
125 p.setStrokeWidth(SkIntToScalar(2));
126 fPaints.push_back(p);
127 }
128 }
129
130 void makeMatrices() {
131 {
132 // 1x1.5 scale
133 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000134 m.setScale(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000135 fMatrices.push_back(m);
136 }
137
138 {
139 // 1.5x1.5 scale
140 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000141 m.setScale(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000142 fMatrices.push_back(m);
143 }
144
145 {
146 // 1x1.5 skew
147 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000148 m.setSkew(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000149 fMatrices.push_back(m);
150 }
151
152 {
153 // 1.5x1.5 skew
154 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000155 m.setSkew(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000156 fMatrices.push_back(m);
157 }
158
159 {
160 // 30 degree rotation
161 SkMatrix m;
162 m.setRotate(SkIntToScalar(30));
163 fMatrices.push_back(m);
164 }
robertphillips@google.com6f5abdd2013-04-29 17:44:45 +0000165
166 {
167 // 90 degree rotation
168 SkMatrix m;
169 m.setRotate(SkIntToScalar(90));
170 fMatrices.push_back(m);
171 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000172 }
173
174 void makeRects() {
175 {
176 // small square
177 SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
178 fRects.push_back(r);
179 }
180
181 {
182 // thin vertical
183 SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
184 fRects.push_back(r);
185 }
186
187 {
188 // thin horizontal
189 SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
190 fRects.push_back(r);
191 }
192
193 {
194 // very thin
195 SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
196 fRects.push_back(r);
197 }
198
199 {
200 // zaftig
201 SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
202 fRects.push_back(r);
203 }
204 }
205
206 // position the current test on the canvas
207 static void position(SkCanvas* canvas, int testCount) {
208 canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
209 SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
210 }
211
212 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000213 SkAutoCommentBlock acb(canvas, "onDraw");
214
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000215 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
216
217 int testCount = 0;
218
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000219 canvas->addComment("Test", "Various Paints");
220
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000221 for (int i = 0; i < fPaints.count(); ++i) {
222 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
223 canvas->save();
224 this->position(canvas, testCount);
225 canvas->drawRect(fRects[j], fPaints[i]);
226 canvas->restore();
227 }
228 }
229
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000230 canvas->addComment("Test", "Matrices");
231
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000232 SkPaint paint;
233 paint.setColor(SK_ColorWHITE);
234 paint.setAntiAlias(true);
235
236 for (int i = 0; i < fMatrices.count(); ++i) {
237 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
238 canvas->save();
239 this->position(canvas, testCount);
240 canvas->concat(fMatrices[i]);
241 canvas->drawRect(fRects[j], paint);
242 canvas->restore();
243 }
244 }
245 }
246
247private:
248 SkTArray<SkPaint> fPaints;
249 SkTArray<SkMatrix> fMatrices;
250 SkTArray<SkRect> fRects;
251
252 typedef GM INHERITED;
253};
254
255//////////////////////////////////////////////////////////////////////////////
256
257static GM* MyFactory(void*) { return new RectsGM; }
258static GMRegistry reg(MyFactory);
259
260}