blob: 28649b21c59b4c2bbce5f48ce9a87731de6f9f04 [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)),
96 SkIntToScalar(5), SkIntToScalar(10),
97 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
98 SkBlurDrawLooper::kOverrideColor_BlurFlag |
reed7b380d02016-03-21 13:25:16 -070099 SkBlurDrawLooper::kHighQuality_BlurFlag));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000100 fPaints.push_back(p);
101 }
102
103 {
104 // AA with stroke style
105 SkPaint p;
106 p.setColor(SK_ColorWHITE);
107 p.setAntiAlias(true);
108 p.setStyle(SkPaint::kStroke_Style);
109 p.setStrokeWidth(SkIntToScalar(3));
110 fPaints.push_back(p);
111 }
112
113 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000114 // AA with bevel-stroke style
115 SkPaint p;
116 p.setColor(SK_ColorWHITE);
117 p.setAntiAlias(true);
118 p.setStyle(SkPaint::kStroke_Style);
119 p.setStrokeJoin(SkPaint::kBevel_Join);
120 p.setStrokeWidth(SkIntToScalar(3));
121 fPaints.push_back(p);
122 }
123
124 {
125 // AA with round-stroke style
126 SkPaint p;
127 p.setColor(SK_ColorWHITE);
128 p.setAntiAlias(true);
129 p.setStyle(SkPaint::kStroke_Style);
130 p.setStrokeJoin(SkPaint::kRound_Join);
131 p.setStrokeWidth(SkIntToScalar(3));
132 fPaints.push_back(p);
133 }
134
135 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000136 // AA with stroke style, width = 0
137 SkPaint p;
138 p.setColor(SK_ColorWHITE);
139 p.setAntiAlias(true);
140 p.setStyle(SkPaint::kStroke_Style);
141 fPaints.push_back(p);
142 }
143
144 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000145 // AA with stroke style, width wider than rect width and/or height
146 SkPaint p;
147 p.setColor(SK_ColorWHITE);
148 p.setAntiAlias(true);
149 p.setStyle(SkPaint::kStroke_Style);
150 p.setStrokeWidth(SkIntToScalar(40));
151 fPaints.push_back(p);
152 }
153
154 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000155 // AA with stroke and fill style
156 SkPaint p;
157 p.setColor(SK_ColorWHITE);
158 p.setAntiAlias(true);
159 p.setStyle(SkPaint::kStrokeAndFill_Style);
160 p.setStrokeWidth(SkIntToScalar(2));
161 fPaints.push_back(p);
162 }
163 }
164
165 void makeMatrices() {
166 {
167 // 1x1.5 scale
168 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000169 m.setScale(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000170 fMatrices.push_back(m);
171 }
172
173 {
174 // 1.5x1.5 scale
175 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000176 m.setScale(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000177 fMatrices.push_back(m);
178 }
179
180 {
181 // 1x1.5 skew
182 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000183 m.setSkew(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000184 fMatrices.push_back(m);
185 }
186
187 {
188 // 1.5x1.5 skew
189 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000190 m.setSkew(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000191 fMatrices.push_back(m);
192 }
193
194 {
195 // 30 degree rotation
196 SkMatrix m;
197 m.setRotate(SkIntToScalar(30));
198 fMatrices.push_back(m);
199 }
robertphillips@google.com6f5abdd2013-04-29 17:44:45 +0000200
201 {
202 // 90 degree rotation
203 SkMatrix m;
204 m.setRotate(SkIntToScalar(90));
205 fMatrices.push_back(m);
206 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000207 }
208
209 void makeRects() {
210 {
211 // small square
212 SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
213 fRects.push_back(r);
214 }
215
216 {
217 // thin vertical
218 SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
219 fRects.push_back(r);
220 }
221
222 {
223 // thin horizontal
224 SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
225 fRects.push_back(r);
226 }
227
228 {
229 // very thin
230 SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
231 fRects.push_back(r);
232 }
233
234 {
235 // zaftig
236 SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
237 fRects.push_back(r);
238 }
239 }
240
241 // position the current test on the canvas
242 static void position(SkCanvas* canvas, int testCount) {
243 canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
244 SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
245 }
246
mtklein36352bf2015-03-25 18:17:31 -0700247 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000248 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
249
250 int testCount = 0;
251
252 for (int i = 0; i < fPaints.count(); ++i) {
253 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
254 canvas->save();
255 this->position(canvas, testCount);
256 canvas->drawRect(fRects[j], fPaints[i]);
257 canvas->restore();
258 }
259 }
260
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000261 SkPaint paint;
262 paint.setColor(SK_ColorWHITE);
263 paint.setAntiAlias(true);
264
265 for (int i = 0; i < fMatrices.count(); ++i) {
266 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
267 canvas->save();
268 this->position(canvas, testCount);
269 canvas->concat(fMatrices[i]);
270 canvas->drawRect(fRects[j], paint);
271 canvas->restore();
272 }
273 }
274 }
275
276private:
277 SkTArray<SkPaint> fPaints;
278 SkTArray<SkMatrix> fMatrices;
279 SkTArray<SkRect> fRects;
280
281 typedef GM INHERITED;
282};
283
284//////////////////////////////////////////////////////////////////////////////
285
286static GM* MyFactory(void*) { return new RectsGM; }
287static GMRegistry reg(MyFactory);
288
289}