blob: 9d2eca5ba77c220b8fb2ab4a2bbef127ec1c70b8 [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 */
7#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurDrawLooper.h"
9#include "SkBlurMask.h"
bsalomon@google.com0b26c152012-04-16 14:19:32 +000010#include "SkBlurMaskFilter.h"
mtklein7a1f45f2016-08-04 06:19:33 -070011#include "SkColorFilter.h"
bsalomon@google.com0b26c152012-04-16 14:19:32 +000012#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000013#include "SkMatrix.h"
14#include "SkRandom.h"
15#include "SkTArray.h"
bsalomon@google.com0b26c152012-04-16 14:19:32 +000016
17namespace skiagm {
18
19class CircleGM : public GM {
20public:
21 CircleGM() {
22 this->setBGColor(0xFF000000);
23 this->makePaints();
24 this->makeMatrices();
25 }
26
27protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
bsalomon@google.com0b26c152012-04-16 14:19:32 +000030 return SkString("circles");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070034 return SkISize::Make(1200, 900);
bsalomon@google.com0b26c152012-04-16 14:19:32 +000035 }
36
37 void makePaints() {
38 {
39 // no AA
40 SkPaint p;
41 fPaints.push_back(p);
42 }
43
44 {
45 // AA
46 SkPaint p;
47 p.setAntiAlias(true);
48 fPaints.push_back(p);
49 }
50
51 {
52 // AA with mask filter
53 SkPaint p;
54 p.setAntiAlias(true);
reedefdfd512016-04-04 10:02:58 -070055 p.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000056 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000057 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
reedefdfd512016-04-04 10:02:58 -070058 SkBlurMaskFilter::kHighQuality_BlurFlag));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000059 fPaints.push_back(p);
60 }
61
62 {
63 // AA with radial shader
64 SkPaint p;
65 p.setAntiAlias(true);
66 SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
67 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
68 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed2ad1aa62016-03-09 09:50:50 -080069 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
70 SkShader::kClamp_TileMode));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000071 fPaints.push_back(p);
72 }
73
74 {
75 // AA with blur
76 SkPaint p;
77 p.setAntiAlias(true);
reed7b380d02016-03-21 13:25:16 -070078 p.setLooper(SkBlurDrawLooper::Make(SK_ColorBLUE,
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000079 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040080 SkIntToScalar(5), SkIntToScalar(10)));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000081 fPaints.push_back(p);
82 }
83
84 {
85 // AA with stroke style
86 SkPaint p;
87 p.setAntiAlias(true);
88 p.setStyle(SkPaint::kStroke_Style);
89 p.setStrokeWidth(SkIntToScalar(3));
90 fPaints.push_back(p);
91 }
92
93 {
94 // AA with stroke style, width = 0
95 SkPaint p;
96 p.setAntiAlias(true);
97 p.setStyle(SkPaint::kStroke_Style);
98 fPaints.push_back(p);
99 }
100
101 {
102 // AA with stroke and fill style
103 SkPaint p;
104 p.setAntiAlias(true);
105 p.setStyle(SkPaint::kStrokeAndFill_Style);
106 p.setStrokeWidth(SkIntToScalar(2));
107 fPaints.push_back(p);
108 }
109 }
110
111 void makeMatrices() {
112 {
113 SkMatrix m;
114 m.setScale(SkIntToScalar(2), SkIntToScalar(3));
115 fMatrices.push_back(m);
116 }
117
118 {
119 SkMatrix m;
120 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
121 fMatrices.push_back(m);
122 }
123
124 {
125 SkMatrix m;
126 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
127 fMatrices.push_back(m);
128 }
129
130 {
131 SkMatrix m;
132 m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
133 fMatrices.push_back(m);
134 }
135
136 {
137 SkMatrix m;
138 m.setRotate(SkIntToScalar(30));
139 fMatrices.push_back(m);
140 }
141 }
142
mtklein36352bf2015-03-25 18:17:31 -0700143 void onDraw(SkCanvas* canvas) override {
bsalomonce1c8862014-12-15 07:11:22 -0800144 // Draw a giant AA circle as the background.
145 SkISize size = this->getISize();
146 SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
147 SkIntToScalar(size.fHeight)) / 2.f;
148 SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
149 SkIntToScalar(size.fHeight/2));
150 SkPaint giantPaint;
151 giantPaint.setAntiAlias(true);
152 giantPaint.setColor(0x80808080);
Hal Canary23e474c2017-05-15 13:35:35 -0400153 canvas->drawCircle(giantCenter, giantRadius, giantPaint);
halcanary9d524f22016-03-29 09:03:52 -0700154
scroggof9d61012014-12-15 12:54:51 -0800155 SkRandom rand;
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000156 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000157 int i;
158 for (i = 0; i < fPaints.count(); ++i) {
159 canvas->save();
160 // position the path, and make it at off-integer coords.
161 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
162 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
163 SkColor color = rand.nextU();
164 color |= 0xff000000;
165 fPaints[i].setColor(color);
166
167 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
168 SkIntToScalar(20),
169 fPaints[i]);
170 canvas->restore();
171 }
172
173 for (int j = 0; j < fMatrices.count(); ++j, ++i) {
174 canvas->save();
175
176 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
177 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
178
179 canvas->concat(fMatrices[j]);
180
181 SkPaint paint;
182 paint.setAntiAlias(true);
183
184 SkColor color = rand.nextU();
185 color |= 0xff000000;
186 paint.setColor(color);
187
188 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
189 SkIntToScalar(20),
190 paint);
191
192 canvas->restore();
193 }
194 }
195
196private:
197 typedef GM INHERITED;
198 SkTArray<SkPaint> fPaints;
199 SkTArray<SkMatrix> fMatrices;
200};
201
202//////////////////////////////////////////////////////////////////////////////
203
204static GM* MyFactory(void*) { return new CircleGM; }
205static GMRegistry reg(MyFactory);
206
207}