blob: 0630a1563c603cb82c8d9dcaaafe63d18225eebb [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 {
Mike Reedaed493e2019-07-23 09:49:59 -040032 sk_sp<SkDrawLooper> fLooper;
33 enum {
34 kLooperColorSentinel = 0x01020304
35 };
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000036public:
37 RectsGM() {
38 this->setBGColor(0xFF000000);
39 this->makePaints();
40 this->makeMatrices();
41 this->makeRects();
42 }
43
44protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000045
mtklein36352bf2015-03-25 18:17:31 -070046 SkString onShortName() override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000047 return SkString("rects");
48 }
49
mtklein36352bf2015-03-25 18:17:31 -070050 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070051 return SkISize::Make(1200, 900);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000052 }
53
54 void makePaints() {
55 {
56 // no AA
57 SkPaint p;
58 p.setColor(SK_ColorWHITE);
59 fPaints.push_back(p);
60 }
61
62 {
63 // AA
64 SkPaint p;
65 p.setColor(SK_ColorWHITE);
66 p.setAntiAlias(true);
67 fPaints.push_back(p);
68 }
69
70 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +000071 // AA with translucent
72 SkPaint p;
73 p.setColor(SK_ColorWHITE);
74 p.setAntiAlias(true);
75 p.setAlpha(0x66);
76 fPaints.push_back(p);
77 }
78
79 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000080 // AA with mask filter
81 SkPaint p;
82 p.setColor(SK_ColorWHITE);
83 p.setAntiAlias(true);
Mike Reed1be1f8d2018-03-14 13:01:17 -040084 p.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000085 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040086 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000087 fPaints.push_back(p);
88 }
89
90 {
91 // AA with radial shader
92 SkPaint p;
93 p.setColor(SK_ColorWHITE);
94 p.setAntiAlias(true);
95 SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
96 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
97 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -070098 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos,
99 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400100 SkTileMode::kClamp));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000101 fPaints.push_back(p);
102 }
103
Mike Reedaed493e2019-07-23 09:49:59 -0400104 fLooper = SkBlurDrawLooper::Make(SK_ColorWHITE, SkBlurMask::ConvertRadiusToSigma(10),5,10);
105 {
106 SkPaint p;
107 p.setColor(kLooperColorSentinel);
108 p.setAntiAlias(true);
109 fPaints.push_back(p);
110 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000111 {
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);
Mike Reedaed493e2019-07-23 09:49:59 -0400264 SkPaint p = fPaints[i];
265 if (p.getColor() == kLooperColorSentinel) {
266 p.setColor(SK_ColorWHITE);
267 SkRect r = fRects[j];
268 fLooper->apply(canvas, p, [r](SkCanvas* c, const SkPaint& p) {
269 c->drawRect(r, p);
270 });
271 } else {
272 canvas->drawRect(fRects[j], p);
273 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000274 canvas->restore();
275 }
276 }
277
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000278 SkPaint paint;
279 paint.setColor(SK_ColorWHITE);
280 paint.setAntiAlias(true);
281
282 for (int i = 0; i < fMatrices.count(); ++i) {
283 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
284 canvas->save();
285 this->position(canvas, testCount);
286 canvas->concat(fMatrices[i]);
287 canvas->drawRect(fRects[j], paint);
288 canvas->restore();
289 }
290 }
291 }
292
293private:
294 SkTArray<SkPaint> fPaints;
295 SkTArray<SkMatrix> fMatrices;
296 SkTArray<SkRect> fRects;
297
298 typedef GM INHERITED;
299};
300
301//////////////////////////////////////////////////////////////////////////////
302
Hal Canarye964c182019-01-23 10:22:01 -0500303DEF_GM( return new RectsGM; )
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000304
305}