blob: 4466b4dd49dc2352f8d5bac3868de4cb4167d1cb [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlurTypes.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkDrawLooper.h"
13#include "include/core/SkMaskFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkMatrix.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkPaint.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkShader.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTileMode.h"
23#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkBlurDrawLooper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/effects/SkGradientShader.h"
26#include "include/private/SkTArray.h"
27#include "src/core/SkBlurMask.h"
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000028
29namespace skiagm {
30
31class RectsGM : public GM {
32public:
33 RectsGM() {
34 this->setBGColor(0xFF000000);
35 this->makePaints();
36 this->makeMatrices();
37 this->makeRects();
38 }
39
40protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000041
mtklein36352bf2015-03-25 18:17:31 -070042 SkString onShortName() override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000043 return SkString("rects");
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070047 return SkISize::Make(1200, 900);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000048 }
49
50 void makePaints() {
51 {
52 // no AA
53 SkPaint p;
54 p.setColor(SK_ColorWHITE);
55 fPaints.push_back(p);
56 }
57
58 {
59 // AA
60 SkPaint p;
61 p.setColor(SK_ColorWHITE);
62 p.setAntiAlias(true);
63 fPaints.push_back(p);
64 }
65
66 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +000067 // AA with translucent
68 SkPaint p;
69 p.setColor(SK_ColorWHITE);
70 p.setAntiAlias(true);
71 p.setAlpha(0x66);
72 fPaints.push_back(p);
73 }
74
75 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000076 // AA with mask filter
77 SkPaint p;
78 p.setColor(SK_ColorWHITE);
79 p.setAntiAlias(true);
Mike Reed1be1f8d2018-03-14 13:01:17 -040080 p.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000081 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040082 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000083 fPaints.push_back(p);
84 }
85
86 {
87 // AA with radial shader
88 SkPaint p;
89 p.setColor(SK_ColorWHITE);
90 p.setAntiAlias(true);
91 SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
92 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
93 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -070094 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos,
95 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040096 SkTileMode::kClamp));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000097 fPaints.push_back(p);
98 }
99
100 {
101 // AA with blur
102 SkPaint p;
103 p.setColor(SK_ColorWHITE);
104 p.setAntiAlias(true);
reed7b380d02016-03-21 13:25:16 -0700105 p.setLooper(SkBlurDrawLooper::Make(SK_ColorWHITE,
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +0000106 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -0400107 SkIntToScalar(5), SkIntToScalar(10)));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000108 fPaints.push_back(p);
109 }
110
111 {
112 // AA with stroke style
113 SkPaint p;
114 p.setColor(SK_ColorWHITE);
115 p.setAntiAlias(true);
116 p.setStyle(SkPaint::kStroke_Style);
117 p.setStrokeWidth(SkIntToScalar(3));
118 fPaints.push_back(p);
119 }
120
121 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000122 // AA with bevel-stroke style
123 SkPaint p;
124 p.setColor(SK_ColorWHITE);
125 p.setAntiAlias(true);
126 p.setStyle(SkPaint::kStroke_Style);
127 p.setStrokeJoin(SkPaint::kBevel_Join);
128 p.setStrokeWidth(SkIntToScalar(3));
129 fPaints.push_back(p);
130 }
131
132 {
133 // AA with round-stroke style
134 SkPaint p;
135 p.setColor(SK_ColorWHITE);
136 p.setAntiAlias(true);
137 p.setStyle(SkPaint::kStroke_Style);
138 p.setStrokeJoin(SkPaint::kRound_Join);
139 p.setStrokeWidth(SkIntToScalar(3));
140 fPaints.push_back(p);
141 }
142
143 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000144 // AA with stroke style, width = 0
145 SkPaint p;
146 p.setColor(SK_ColorWHITE);
147 p.setAntiAlias(true);
148 p.setStyle(SkPaint::kStroke_Style);
149 fPaints.push_back(p);
150 }
151
152 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000153 // AA with stroke style, width wider than rect width and/or height
154 SkPaint p;
155 p.setColor(SK_ColorWHITE);
156 p.setAntiAlias(true);
157 p.setStyle(SkPaint::kStroke_Style);
158 p.setStrokeWidth(SkIntToScalar(40));
159 fPaints.push_back(p);
160 }
161
162 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000163 // AA with stroke and fill style
164 SkPaint p;
165 p.setColor(SK_ColorWHITE);
166 p.setAntiAlias(true);
167 p.setStyle(SkPaint::kStrokeAndFill_Style);
168 p.setStrokeWidth(SkIntToScalar(2));
169 fPaints.push_back(p);
170 }
171 }
172
173 void makeMatrices() {
174 {
175 // 1x1.5 scale
176 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000177 m.setScale(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000178 fMatrices.push_back(m);
179 }
180
181 {
182 // 1.5x1.5 scale
183 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000184 m.setScale(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000185 fMatrices.push_back(m);
186 }
187
188 {
189 // 1x1.5 skew
190 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000191 m.setSkew(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000192 fMatrices.push_back(m);
193 }
194
195 {
196 // 1.5x1.5 skew
197 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000198 m.setSkew(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000199 fMatrices.push_back(m);
200 }
201
202 {
203 // 30 degree rotation
204 SkMatrix m;
205 m.setRotate(SkIntToScalar(30));
206 fMatrices.push_back(m);
207 }
robertphillips@google.com6f5abdd2013-04-29 17:44:45 +0000208
209 {
210 // 90 degree rotation
211 SkMatrix m;
212 m.setRotate(SkIntToScalar(90));
213 fMatrices.push_back(m);
214 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000215 }
216
217 void makeRects() {
218 {
219 // small square
220 SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
221 fRects.push_back(r);
222 }
223
224 {
225 // thin vertical
226 SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
227 fRects.push_back(r);
228 }
229
230 {
231 // thin horizontal
232 SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
233 fRects.push_back(r);
234 }
235
236 {
237 // very thin
238 SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
239 fRects.push_back(r);
240 }
241
242 {
243 // zaftig
244 SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
245 fRects.push_back(r);
246 }
247 }
248
249 // position the current test on the canvas
250 static void position(SkCanvas* canvas, int testCount) {
251 canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
252 SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
253 }
254
mtklein36352bf2015-03-25 18:17:31 -0700255 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000256 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
257
258 int testCount = 0;
259
260 for (int i = 0; i < fPaints.count(); ++i) {
261 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
262 canvas->save();
263 this->position(canvas, testCount);
264 canvas->drawRect(fRects[j], fPaints[i]);
265 canvas->restore();
266 }
267 }
268
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000269 SkPaint paint;
270 paint.setColor(SK_ColorWHITE);
271 paint.setAntiAlias(true);
272
273 for (int i = 0; i < fMatrices.count(); ++i) {
274 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
275 canvas->save();
276 this->position(canvas, testCount);
277 canvas->concat(fMatrices[i]);
278 canvas->drawRect(fRects[j], paint);
279 canvas->restore();
280 }
281 }
282 }
283
284private:
285 SkTArray<SkPaint> fPaints;
286 SkTArray<SkMatrix> fMatrices;
287 SkTArray<SkRect> fRects;
288
289 typedef GM INHERITED;
290};
291
292//////////////////////////////////////////////////////////////////////////////
293
Hal Canarye964c182019-01-23 10:22:01 -0500294DEF_GM( return new RectsGM; )
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000295
296}