blob: d8c2fd9a42602caf9b0aeef8b57552061b2850e4 [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"
mtklein7a1f45f2016-08-04 06:19:33 -070012#include "SkColorFilter.h"
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000013#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000014#include "SkMatrix.h"
15#include "SkTArray.h"
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000016
17namespace skiagm {
18
19class RectsGM : public GM {
20public:
21 RectsGM() {
22 this->setBGColor(0xFF000000);
23 this->makePaints();
24 this->makeMatrices();
25 this->makeRects();
26 }
27
28protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000029
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000031 return SkString("rects");
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070035 return SkISize::Make(1200, 900);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000036 }
37
38 void makePaints() {
39 {
40 // no AA
41 SkPaint p;
42 p.setColor(SK_ColorWHITE);
43 fPaints.push_back(p);
44 }
45
46 {
47 // AA
48 SkPaint p;
49 p.setColor(SK_ColorWHITE);
50 p.setAntiAlias(true);
51 fPaints.push_back(p);
52 }
53
54 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +000055 // AA with translucent
56 SkPaint p;
57 p.setColor(SK_ColorWHITE);
58 p.setAntiAlias(true);
59 p.setAlpha(0x66);
60 fPaints.push_back(p);
61 }
62
63 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000064 // AA with mask filter
65 SkPaint p;
66 p.setColor(SK_ColorWHITE);
67 p.setAntiAlias(true);
reedefdfd512016-04-04 10:02:58 -070068 p.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000069 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000070 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
reedefdfd512016-04-04 10:02:58 -070071 SkBlurMaskFilter::kHighQuality_BlurFlag));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000072 fPaints.push_back(p);
73 }
74
75 {
76 // AA with radial shader
77 SkPaint p;
78 p.setColor(SK_ColorWHITE);
79 p.setAntiAlias(true);
80 SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
81 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
82 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -070083 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos,
84 SK_ARRAY_COUNT(colors),
85 SkShader::kClamp_TileMode));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000086 fPaints.push_back(p);
87 }
88
89 {
90 // AA with blur
91 SkPaint p;
92 p.setColor(SK_ColorWHITE);
93 p.setAntiAlias(true);
reed7b380d02016-03-21 13:25:16 -070094 p.setLooper(SkBlurDrawLooper::Make(SK_ColorWHITE,
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000095 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040096 SkIntToScalar(5), SkIntToScalar(10)));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000097 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 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000111 // AA with bevel-stroke style
112 SkPaint p;
113 p.setColor(SK_ColorWHITE);
114 p.setAntiAlias(true);
115 p.setStyle(SkPaint::kStroke_Style);
116 p.setStrokeJoin(SkPaint::kBevel_Join);
117 p.setStrokeWidth(SkIntToScalar(3));
118 fPaints.push_back(p);
119 }
120
121 {
122 // AA with round-stroke style
123 SkPaint p;
124 p.setColor(SK_ColorWHITE);
125 p.setAntiAlias(true);
126 p.setStyle(SkPaint::kStroke_Style);
127 p.setStrokeJoin(SkPaint::kRound_Join);
128 p.setStrokeWidth(SkIntToScalar(3));
129 fPaints.push_back(p);
130 }
131
132 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000133 // AA with stroke style, width = 0
134 SkPaint p;
135 p.setColor(SK_ColorWHITE);
136 p.setAntiAlias(true);
137 p.setStyle(SkPaint::kStroke_Style);
138 fPaints.push_back(p);
139 }
140
141 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000142 // AA with stroke style, width wider than rect width and/or height
143 SkPaint p;
144 p.setColor(SK_ColorWHITE);
145 p.setAntiAlias(true);
146 p.setStyle(SkPaint::kStroke_Style);
147 p.setStrokeWidth(SkIntToScalar(40));
148 fPaints.push_back(p);
149 }
150
151 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000152 // AA with stroke and fill style
153 SkPaint p;
154 p.setColor(SK_ColorWHITE);
155 p.setAntiAlias(true);
156 p.setStyle(SkPaint::kStrokeAndFill_Style);
157 p.setStrokeWidth(SkIntToScalar(2));
158 fPaints.push_back(p);
159 }
160 }
161
162 void makeMatrices() {
163 {
164 // 1x1.5 scale
165 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000166 m.setScale(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000167 fMatrices.push_back(m);
168 }
169
170 {
171 // 1.5x1.5 scale
172 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000173 m.setScale(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000174 fMatrices.push_back(m);
175 }
176
177 {
178 // 1x1.5 skew
179 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000180 m.setSkew(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000181 fMatrices.push_back(m);
182 }
183
184 {
185 // 1.5x1.5 skew
186 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000187 m.setSkew(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000188 fMatrices.push_back(m);
189 }
190
191 {
192 // 30 degree rotation
193 SkMatrix m;
194 m.setRotate(SkIntToScalar(30));
195 fMatrices.push_back(m);
196 }
robertphillips@google.com6f5abdd2013-04-29 17:44:45 +0000197
198 {
199 // 90 degree rotation
200 SkMatrix m;
201 m.setRotate(SkIntToScalar(90));
202 fMatrices.push_back(m);
203 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000204 }
205
206 void makeRects() {
207 {
208 // small square
209 SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
210 fRects.push_back(r);
211 }
212
213 {
214 // thin vertical
215 SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
216 fRects.push_back(r);
217 }
218
219 {
220 // thin horizontal
221 SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
222 fRects.push_back(r);
223 }
224
225 {
226 // very thin
227 SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
228 fRects.push_back(r);
229 }
230
231 {
232 // zaftig
233 SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
234 fRects.push_back(r);
235 }
236 }
237
238 // position the current test on the canvas
239 static void position(SkCanvas* canvas, int testCount) {
240 canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
241 SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
242 }
243
mtklein36352bf2015-03-25 18:17:31 -0700244 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000245 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
246
247 int testCount = 0;
248
249 for (int i = 0; i < fPaints.count(); ++i) {
250 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
251 canvas->save();
252 this->position(canvas, testCount);
253 canvas->drawRect(fRects[j], fPaints[i]);
254 canvas->restore();
255 }
256 }
257
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000258 SkPaint paint;
259 paint.setColor(SK_ColorWHITE);
260 paint.setAntiAlias(true);
261
262 for (int i = 0; i < fMatrices.count(); ++i) {
263 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
264 canvas->save();
265 this->position(canvas, testCount);
266 canvas->concat(fMatrices[i]);
267 canvas->drawRect(fRects[j], paint);
268 canvas->restore();
269 }
270 }
271 }
272
273private:
274 SkTArray<SkPaint> fPaints;
275 SkTArray<SkMatrix> fMatrices;
276 SkTArray<SkRect> fRects;
277
278 typedef GM INHERITED;
279};
280
281//////////////////////////////////////////////////////////////////////////////
282
283static GM* MyFactory(void*) { return new RectsGM; }
284static GMRegistry reg(MyFactory);
285
286}