blob: 4c4a3a526335feb720df7fe66587040614dd3f80 [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"
mtklein7a1f45f2016-08-04 06:19:33 -070010#include "SkColorFilter.h"
bsalomon@google.com0b26c152012-04-16 14:19:32 +000011#include "SkGradientShader.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040012#include "SkMaskFilter.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);
Mike Reed1be1f8d2018-03-14 13:01:17 -040055 p.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000056 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -040057 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000058 fPaints.push_back(p);
59 }
60
61 {
62 // AA with radial shader
63 SkPaint p;
64 p.setAntiAlias(true);
65 SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
66 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
67 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed2ad1aa62016-03-09 09:50:50 -080068 p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
69 SkShader::kClamp_TileMode));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000070 fPaints.push_back(p);
71 }
72
73 {
74 // AA with blur
75 SkPaint p;
76 p.setAntiAlias(true);
reed7b380d02016-03-21 13:25:16 -070077 p.setLooper(SkBlurDrawLooper::Make(SK_ColorBLUE,
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000078 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040079 SkIntToScalar(5), SkIntToScalar(10)));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000080 fPaints.push_back(p);
81 }
82
83 {
84 // AA with stroke style
85 SkPaint p;
86 p.setAntiAlias(true);
87 p.setStyle(SkPaint::kStroke_Style);
88 p.setStrokeWidth(SkIntToScalar(3));
89 fPaints.push_back(p);
90 }
91
92 {
93 // AA with stroke style, width = 0
94 SkPaint p;
95 p.setAntiAlias(true);
96 p.setStyle(SkPaint::kStroke_Style);
97 fPaints.push_back(p);
98 }
99
100 {
101 // AA with stroke and fill style
102 SkPaint p;
103 p.setAntiAlias(true);
104 p.setStyle(SkPaint::kStrokeAndFill_Style);
105 p.setStrokeWidth(SkIntToScalar(2));
106 fPaints.push_back(p);
107 }
108 }
109
110 void makeMatrices() {
111 {
112 SkMatrix m;
113 m.setScale(SkIntToScalar(2), SkIntToScalar(3));
114 fMatrices.push_back(m);
115 }
116
117 {
118 SkMatrix m;
119 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
120 fMatrices.push_back(m);
121 }
122
123 {
124 SkMatrix m;
125 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
126 fMatrices.push_back(m);
127 }
128
129 {
130 SkMatrix m;
131 m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
132 fMatrices.push_back(m);
133 }
134
135 {
136 SkMatrix m;
137 m.setRotate(SkIntToScalar(30));
138 fMatrices.push_back(m);
139 }
140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 void onDraw(SkCanvas* canvas) override {
bsalomonce1c8862014-12-15 07:11:22 -0800143 // Draw a giant AA circle as the background.
144 SkISize size = this->getISize();
145 SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
146 SkIntToScalar(size.fHeight)) / 2.f;
147 SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
148 SkIntToScalar(size.fHeight/2));
149 SkPaint giantPaint;
150 giantPaint.setAntiAlias(true);
151 giantPaint.setColor(0x80808080);
Hal Canary23e474c2017-05-15 13:35:35 -0400152 canvas->drawCircle(giantCenter, giantRadius, giantPaint);
halcanary9d524f22016-03-29 09:03:52 -0700153
scroggof9d61012014-12-15 12:54:51 -0800154 SkRandom rand;
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000155 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000156 int i;
157 for (i = 0; i < fPaints.count(); ++i) {
158 canvas->save();
159 // position the path, and make it at off-integer coords.
160 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
161 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
162 SkColor color = rand.nextU();
163 color |= 0xff000000;
164 fPaints[i].setColor(color);
165
166 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
167 SkIntToScalar(20),
168 fPaints[i]);
169 canvas->restore();
170 }
171
172 for (int j = 0; j < fMatrices.count(); ++j, ++i) {
173 canvas->save();
174
175 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
176 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
177
178 canvas->concat(fMatrices[j]);
179
180 SkPaint paint;
181 paint.setAntiAlias(true);
182
183 SkColor color = rand.nextU();
184 color |= 0xff000000;
185 paint.setColor(color);
186
187 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
188 SkIntToScalar(20),
189 paint);
190
191 canvas->restore();
192 }
193 }
194
195private:
196 typedef GM INHERITED;
197 SkTArray<SkPaint> fPaints;
198 SkTArray<SkMatrix> fMatrices;
199};
200
201//////////////////////////////////////////////////////////////////////////////
202
203static GM* MyFactory(void*) { return new CircleGM; }
204static GMRegistry reg(MyFactory);
205
206}