blob: a7dcc3d80872980310d890e7e19fa4241db34d5c [file] [log] [blame]
bsalomon@google.com0b26c152012-04-16 14:19:32 +00001/*
2 * Copyright 2012 Intel 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 */
Ben Wagner7fde8e12019-05-01 17:28:53 -04007
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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkMaskFilter.h"
14#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/SkScalar.h"
18#include "include/core/SkShader.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
21#include "include/core/SkTileMode.h"
22#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/effects/SkBlurDrawLooper.h"
24#include "include/effects/SkGradientShader.h"
25#include "include/private/SkTArray.h"
26#include "include/utils/SkRandom.h"
27#include "src/core/SkBlurMask.h"
bsalomon@google.com0b26c152012-04-16 14:19:32 +000028
29namespace skiagm {
30
31class CircleGM : public GM {
32public:
33 CircleGM() {
34 this->setBGColor(0xFF000000);
35 this->makePaints();
36 this->makeMatrices();
37 }
38
39protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 SkString onShortName() override {
bsalomon@google.com0b26c152012-04-16 14:19:32 +000042 return SkString("circles");
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070046 return SkISize::Make(1200, 900);
bsalomon@google.com0b26c152012-04-16 14:19:32 +000047 }
48
49 void makePaints() {
50 {
51 // no AA
52 SkPaint p;
53 fPaints.push_back(p);
54 }
55
56 {
57 // AA
58 SkPaint p;
59 p.setAntiAlias(true);
60 fPaints.push_back(p);
61 }
62
63 {
64 // AA with mask filter
65 SkPaint p;
66 p.setAntiAlias(true);
Mike Reed1be1f8d2018-03-14 13:01:17 -040067 p.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000068 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040069 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000070 fPaints.push_back(p);
71 }
72
73 {
74 // AA with radial shader
75 SkPaint p;
76 p.setAntiAlias(true);
77 SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
78 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
79 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed2ad1aa62016-03-09 09:50:50 -080080 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040081 SkTileMode::kClamp));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000082 fPaints.push_back(p);
83 }
84
85 {
86 // AA with blur
87 SkPaint p;
88 p.setAntiAlias(true);
reed7b380d02016-03-21 13:25:16 -070089 p.setLooper(SkBlurDrawLooper::Make(SK_ColorBLUE,
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000090 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040091 SkIntToScalar(5), SkIntToScalar(10)));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000092 fPaints.push_back(p);
93 }
94
95 {
96 // AA with stroke style
97 SkPaint p;
98 p.setAntiAlias(true);
99 p.setStyle(SkPaint::kStroke_Style);
100 p.setStrokeWidth(SkIntToScalar(3));
101 fPaints.push_back(p);
102 }
103
104 {
105 // AA with stroke style, width = 0
106 SkPaint p;
107 p.setAntiAlias(true);
108 p.setStyle(SkPaint::kStroke_Style);
109 fPaints.push_back(p);
110 }
111
112 {
113 // AA with stroke and fill style
114 SkPaint p;
115 p.setAntiAlias(true);
116 p.setStyle(SkPaint::kStrokeAndFill_Style);
117 p.setStrokeWidth(SkIntToScalar(2));
118 fPaints.push_back(p);
119 }
120 }
121
122 void makeMatrices() {
123 {
124 SkMatrix m;
125 m.setScale(SkIntToScalar(2), SkIntToScalar(3));
126 fMatrices.push_back(m);
127 }
128
129 {
130 SkMatrix m;
131 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
132 fMatrices.push_back(m);
133 }
134
135 {
136 SkMatrix m;
137 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
138 fMatrices.push_back(m);
139 }
140
141 {
142 SkMatrix m;
143 m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
144 fMatrices.push_back(m);
145 }
146
147 {
148 SkMatrix m;
149 m.setRotate(SkIntToScalar(30));
150 fMatrices.push_back(m);
151 }
152 }
153
mtklein36352bf2015-03-25 18:17:31 -0700154 void onDraw(SkCanvas* canvas) override {
bsalomonce1c8862014-12-15 07:11:22 -0800155 // Draw a giant AA circle as the background.
156 SkISize size = this->getISize();
157 SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
158 SkIntToScalar(size.fHeight)) / 2.f;
159 SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
160 SkIntToScalar(size.fHeight/2));
161 SkPaint giantPaint;
162 giantPaint.setAntiAlias(true);
163 giantPaint.setColor(0x80808080);
Hal Canary23e474c2017-05-15 13:35:35 -0400164 canvas->drawCircle(giantCenter, giantRadius, giantPaint);
halcanary9d524f22016-03-29 09:03:52 -0700165
scroggof9d61012014-12-15 12:54:51 -0800166 SkRandom rand;
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000167 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000168 int i;
169 for (i = 0; i < fPaints.count(); ++i) {
170 canvas->save();
171 // position the path, and make it at off-integer coords.
172 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
173 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
174 SkColor color = rand.nextU();
175 color |= 0xff000000;
176 fPaints[i].setColor(color);
177
178 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
179 SkIntToScalar(20),
180 fPaints[i]);
181 canvas->restore();
182 }
183
184 for (int j = 0; j < fMatrices.count(); ++j, ++i) {
185 canvas->save();
186
187 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
188 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
189
190 canvas->concat(fMatrices[j]);
191
192 SkPaint paint;
193 paint.setAntiAlias(true);
194
195 SkColor color = rand.nextU();
196 color |= 0xff000000;
197 paint.setColor(color);
198
199 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
200 SkIntToScalar(20),
201 paint);
202
203 canvas->restore();
204 }
205 }
206
207private:
208 typedef GM INHERITED;
209 SkTArray<SkPaint> fPaints;
210 SkTArray<SkMatrix> fMatrices;
211};
212
213//////////////////////////////////////////////////////////////////////////////
214
Hal Canarye964c182019-01-23 10:22:01 -0500215DEF_GM( return new CircleGM; )
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000216
217}