blob: 9d7ac6dbb8512d7ee8a9f22556eae0a9970790b3 [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);
Mike Reed1be1f8d2018-03-14 13:01:17 -040068 p.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000069 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040070 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000071 fPaints.push_back(p);
72 }
73
74 {
75 // AA with radial shader
76 SkPaint p;
77 p.setColor(SK_ColorWHITE);
78 p.setAntiAlias(true);
79 SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
80 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
81 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -070082 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos,
83 SK_ARRAY_COUNT(colors),
84 SkShader::kClamp_TileMode));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000085 fPaints.push_back(p);
86 }
87
88 {
89 // AA with blur
90 SkPaint p;
91 p.setColor(SK_ColorWHITE);
92 p.setAntiAlias(true);
reed7b380d02016-03-21 13:25:16 -070093 p.setLooper(SkBlurDrawLooper::Make(SK_ColorWHITE,
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000094 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040095 SkIntToScalar(5), SkIntToScalar(10)));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000096 fPaints.push_back(p);
97 }
98
99 {
100 // AA with stroke style
101 SkPaint p;
102 p.setColor(SK_ColorWHITE);
103 p.setAntiAlias(true);
104 p.setStyle(SkPaint::kStroke_Style);
105 p.setStrokeWidth(SkIntToScalar(3));
106 fPaints.push_back(p);
107 }
108
109 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000110 // AA with bevel-stroke style
111 SkPaint p;
112 p.setColor(SK_ColorWHITE);
113 p.setAntiAlias(true);
114 p.setStyle(SkPaint::kStroke_Style);
115 p.setStrokeJoin(SkPaint::kBevel_Join);
116 p.setStrokeWidth(SkIntToScalar(3));
117 fPaints.push_back(p);
118 }
119
120 {
121 // AA with round-stroke style
122 SkPaint p;
123 p.setColor(SK_ColorWHITE);
124 p.setAntiAlias(true);
125 p.setStyle(SkPaint::kStroke_Style);
126 p.setStrokeJoin(SkPaint::kRound_Join);
127 p.setStrokeWidth(SkIntToScalar(3));
128 fPaints.push_back(p);
129 }
130
131 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000132 // AA with stroke style, width = 0
133 SkPaint p;
134 p.setColor(SK_ColorWHITE);
135 p.setAntiAlias(true);
136 p.setStyle(SkPaint::kStroke_Style);
137 fPaints.push_back(p);
138 }
139
140 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000141 // AA with stroke style, width wider than rect width and/or height
142 SkPaint p;
143 p.setColor(SK_ColorWHITE);
144 p.setAntiAlias(true);
145 p.setStyle(SkPaint::kStroke_Style);
146 p.setStrokeWidth(SkIntToScalar(40));
147 fPaints.push_back(p);
148 }
149
150 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000151 // AA with stroke and fill style
152 SkPaint p;
153 p.setColor(SK_ColorWHITE);
154 p.setAntiAlias(true);
155 p.setStyle(SkPaint::kStrokeAndFill_Style);
156 p.setStrokeWidth(SkIntToScalar(2));
157 fPaints.push_back(p);
158 }
159 }
160
161 void makeMatrices() {
162 {
163 // 1x1.5 scale
164 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000165 m.setScale(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000166 fMatrices.push_back(m);
167 }
168
169 {
170 // 1.5x1.5 scale
171 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000172 m.setScale(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000173 fMatrices.push_back(m);
174 }
175
176 {
177 // 1x1.5 skew
178 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000179 m.setSkew(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000180 fMatrices.push_back(m);
181 }
182
183 {
184 // 1.5x1.5 skew
185 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000186 m.setSkew(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000187 fMatrices.push_back(m);
188 }
189
190 {
191 // 30 degree rotation
192 SkMatrix m;
193 m.setRotate(SkIntToScalar(30));
194 fMatrices.push_back(m);
195 }
robertphillips@google.com6f5abdd2013-04-29 17:44:45 +0000196
197 {
198 // 90 degree rotation
199 SkMatrix m;
200 m.setRotate(SkIntToScalar(90));
201 fMatrices.push_back(m);
202 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000203 }
204
205 void makeRects() {
206 {
207 // small square
208 SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
209 fRects.push_back(r);
210 }
211
212 {
213 // thin vertical
214 SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
215 fRects.push_back(r);
216 }
217
218 {
219 // thin horizontal
220 SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
221 fRects.push_back(r);
222 }
223
224 {
225 // very thin
226 SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
227 fRects.push_back(r);
228 }
229
230 {
231 // zaftig
232 SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
233 fRects.push_back(r);
234 }
235 }
236
237 // position the current test on the canvas
238 static void position(SkCanvas* canvas, int testCount) {
239 canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
240 SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
241 }
242
mtklein36352bf2015-03-25 18:17:31 -0700243 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000244 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
245
246 int testCount = 0;
247
248 for (int i = 0; i < fPaints.count(); ++i) {
249 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
250 canvas->save();
251 this->position(canvas, testCount);
252 canvas->drawRect(fRects[j], fPaints[i]);
253 canvas->restore();
254 }
255 }
256
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000257 SkPaint paint;
258 paint.setColor(SK_ColorWHITE);
259 paint.setAntiAlias(true);
260
261 for (int i = 0; i < fMatrices.count(); ++i) {
262 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
263 canvas->save();
264 this->position(canvas, testCount);
265 canvas->concat(fMatrices[i]);
266 canvas->drawRect(fRects[j], paint);
267 canvas->restore();
268 }
269 }
270 }
271
272private:
273 SkTArray<SkPaint> fPaints;
274 SkTArray<SkMatrix> fMatrices;
275 SkTArray<SkRect> fRects;
276
277 typedef GM INHERITED;
278};
279
280//////////////////////////////////////////////////////////////////////////////
281
282static GM* MyFactory(void*) { return new RectsGM; }
283static GMRegistry reg(MyFactory);
284
285}