blob: e7d3b04dad8427ea597da1b7e05cf31ca8e674bc [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 };
John Stilese6a05a42020-11-05 10:05:19 -050036
37protected:
38 void onOnceBeforeDraw() override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000039 this->setBGColor(0xFF000000);
40 this->makePaints();
41 this->makeMatrices();
42 this->makeRects();
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 SkString onShortName() override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000046 return SkString("rects");
47 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070050 return SkISize::Make(1200, 900);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000051 }
52
53 void makePaints() {
54 {
55 // no AA
56 SkPaint p;
57 p.setColor(SK_ColorWHITE);
58 fPaints.push_back(p);
59 }
60
61 {
62 // AA
63 SkPaint p;
64 p.setColor(SK_ColorWHITE);
65 p.setAntiAlias(true);
66 fPaints.push_back(p);
67 }
68
69 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +000070 // AA with translucent
71 SkPaint p;
72 p.setColor(SK_ColorWHITE);
73 p.setAntiAlias(true);
74 p.setAlpha(0x66);
75 fPaints.push_back(p);
76 }
77
78 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000079 // AA with mask filter
80 SkPaint p;
81 p.setColor(SK_ColorWHITE);
82 p.setAntiAlias(true);
Mike Reed1be1f8d2018-03-14 13:01:17 -040083 p.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000084 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040085 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +000086 fPaints.push_back(p);
87 }
88
89 {
90 // AA with radial shader
91 SkPaint p;
92 p.setColor(SK_ColorWHITE);
93 p.setAntiAlias(true);
94 SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
95 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
96 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -070097 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos,
98 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040099 SkTileMode::kClamp));
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000100 fPaints.push_back(p);
101 }
102
Mike Reedaed493e2019-07-23 09:49:59 -0400103 fLooper = SkBlurDrawLooper::Make(SK_ColorWHITE, SkBlurMask::ConvertRadiusToSigma(10),5,10);
104 {
105 SkPaint p;
106 p.setColor(kLooperColorSentinel);
107 p.setAntiAlias(true);
108 fPaints.push_back(p);
109 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000110 {
111 // AA with stroke style
112 SkPaint p;
113 p.setColor(SK_ColorWHITE);
114 p.setAntiAlias(true);
115 p.setStyle(SkPaint::kStroke_Style);
116 p.setStrokeWidth(SkIntToScalar(3));
117 fPaints.push_back(p);
118 }
119
120 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000121 // AA with bevel-stroke style
122 SkPaint p;
123 p.setColor(SK_ColorWHITE);
124 p.setAntiAlias(true);
125 p.setStyle(SkPaint::kStroke_Style);
126 p.setStrokeJoin(SkPaint::kBevel_Join);
127 p.setStrokeWidth(SkIntToScalar(3));
128 fPaints.push_back(p);
129 }
130
131 {
132 // AA with round-stroke style
133 SkPaint p;
134 p.setColor(SK_ColorWHITE);
135 p.setAntiAlias(true);
136 p.setStyle(SkPaint::kStroke_Style);
137 p.setStrokeJoin(SkPaint::kRound_Join);
138 p.setStrokeWidth(SkIntToScalar(3));
139 fPaints.push_back(p);
140 }
141
142 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000143 // AA with stroke style, width = 0
144 SkPaint p;
145 p.setColor(SK_ColorWHITE);
146 p.setAntiAlias(true);
147 p.setStyle(SkPaint::kStroke_Style);
148 fPaints.push_back(p);
149 }
150
151 {
commit-bot@chromium.orga0fd1b32013-11-01 11:54:29 +0000152 // AA with stroke style, width wider than rect width and/or height
153 SkPaint p;
154 p.setColor(SK_ColorWHITE);
155 p.setAntiAlias(true);
156 p.setStyle(SkPaint::kStroke_Style);
157 p.setStrokeWidth(SkIntToScalar(40));
158 fPaints.push_back(p);
159 }
160
161 {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000162 // AA with stroke and fill style
163 SkPaint p;
164 p.setColor(SK_ColorWHITE);
165 p.setAntiAlias(true);
166 p.setStyle(SkPaint::kStrokeAndFill_Style);
167 p.setStrokeWidth(SkIntToScalar(2));
168 fPaints.push_back(p);
169 }
170 }
171
172 void makeMatrices() {
173 {
174 // 1x1.5 scale
175 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000176 m.setScale(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000177 fMatrices.push_back(m);
178 }
179
180 {
181 // 1.5x1.5 scale
182 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000183 m.setScale(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000184 fMatrices.push_back(m);
185 }
186
187 {
188 // 1x1.5 skew
189 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000190 m.setSkew(1, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000191 fMatrices.push_back(m);
192 }
193
194 {
195 // 1.5x1.5 skew
196 SkMatrix m;
reed@google.com56a2e0d2013-03-19 14:50:41 +0000197 m.setSkew(1.5f, 1.5f);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000198 fMatrices.push_back(m);
199 }
200
201 {
202 // 30 degree rotation
203 SkMatrix m;
204 m.setRotate(SkIntToScalar(30));
205 fMatrices.push_back(m);
206 }
robertphillips@google.com6f5abdd2013-04-29 17:44:45 +0000207
208 {
209 // 90 degree rotation
210 SkMatrix m;
211 m.setRotate(SkIntToScalar(90));
212 fMatrices.push_back(m);
213 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000214 }
215
216 void makeRects() {
217 {
218 // small square
219 SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
220 fRects.push_back(r);
221 }
222
223 {
224 // thin vertical
225 SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
226 fRects.push_back(r);
227 }
228
229 {
230 // thin horizontal
231 SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
232 fRects.push_back(r);
233 }
234
235 {
236 // very thin
237 SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
238 fRects.push_back(r);
239 }
240
241 {
242 // zaftig
243 SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
244 fRects.push_back(r);
245 }
246 }
247
248 // position the current test on the canvas
John Stiles39770882020-08-07 12:20:23 -0400249 static void Position(SkCanvas* canvas, int testCount) {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000250 canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
251 SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
252 }
253
mtklein36352bf2015-03-25 18:17:31 -0700254 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000255 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
256
257 int testCount = 0;
258
259 for (int i = 0; i < fPaints.count(); ++i) {
260 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
261 canvas->save();
John Stiles39770882020-08-07 12:20:23 -0400262 Position(canvas, testCount);
Mike Reedaed493e2019-07-23 09:49:59 -0400263 SkPaint p = fPaints[i];
264 if (p.getColor() == kLooperColorSentinel) {
265 p.setColor(SK_ColorWHITE);
266 SkRect r = fRects[j];
267 fLooper->apply(canvas, p, [r](SkCanvas* c, const SkPaint& p) {
268 c->drawRect(r, p);
269 });
270 } else {
271 canvas->drawRect(fRects[j], p);
272 }
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000273 canvas->restore();
274 }
275 }
276
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000277 SkPaint paint;
278 paint.setColor(SK_ColorWHITE);
279 paint.setAntiAlias(true);
280
281 for (int i = 0; i < fMatrices.count(); ++i) {
282 for (int j = 0; j < fRects.count(); ++j, ++testCount) {
283 canvas->save();
John Stiles39770882020-08-07 12:20:23 -0400284 Position(canvas, testCount);
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000285 canvas->concat(fMatrices[i]);
286 canvas->drawRect(fRects[j], paint);
287 canvas->restore();
288 }
289 }
290 }
291
292private:
293 SkTArray<SkPaint> fPaints;
294 SkTArray<SkMatrix> fMatrices;
295 SkTArray<SkRect> fRects;
296
John Stiles7571f9e2020-09-02 22:42:33 -0400297 using INHERITED = GM;
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000298};
299
300//////////////////////////////////////////////////////////////////////////////
301
Hal Canarye964c182019-01-23 10:22:01 -0500302DEF_GM( return new RectsGM; )
robertphillips@google.com14c3fc62013-03-19 14:11:15 +0000303
John Stilesa6841be2020-08-06 14:11:56 -0400304} // namespace skiagm