blob: 53ca5ddb1bb3f204408bc1e6fa93b1b4a9675ffc [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)),
80 SkIntToScalar(5), SkIntToScalar(10),
81 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
82 SkBlurDrawLooper::kOverrideColor_BlurFlag |
reed7b380d02016-03-21 13:25:16 -070083 SkBlurDrawLooper::kHighQuality_BlurFlag));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000084 fPaints.push_back(p);
85 }
86
87 {
88 // AA with stroke style
89 SkPaint p;
90 p.setAntiAlias(true);
91 p.setStyle(SkPaint::kStroke_Style);
92 p.setStrokeWidth(SkIntToScalar(3));
93 fPaints.push_back(p);
94 }
95
96 {
97 // AA with stroke style, width = 0
98 SkPaint p;
99 p.setAntiAlias(true);
100 p.setStyle(SkPaint::kStroke_Style);
101 fPaints.push_back(p);
102 }
103
104 {
105 // AA with stroke and fill style
106 SkPaint p;
107 p.setAntiAlias(true);
108 p.setStyle(SkPaint::kStrokeAndFill_Style);
109 p.setStrokeWidth(SkIntToScalar(2));
110 fPaints.push_back(p);
111 }
112 }
113
114 void makeMatrices() {
115 {
116 SkMatrix m;
117 m.setScale(SkIntToScalar(2), SkIntToScalar(3));
118 fMatrices.push_back(m);
119 }
120
121 {
122 SkMatrix m;
123 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
124 fMatrices.push_back(m);
125 }
126
127 {
128 SkMatrix m;
129 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
130 fMatrices.push_back(m);
131 }
132
133 {
134 SkMatrix m;
135 m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
136 fMatrices.push_back(m);
137 }
138
139 {
140 SkMatrix m;
141 m.setRotate(SkIntToScalar(30));
142 fMatrices.push_back(m);
143 }
144 }
145
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDraw(SkCanvas* canvas) override {
bsalomonce1c8862014-12-15 07:11:22 -0800147 // Draw a giant AA circle as the background.
148 SkISize size = this->getISize();
149 SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
150 SkIntToScalar(size.fHeight)) / 2.f;
151 SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
152 SkIntToScalar(size.fHeight/2));
153 SkPaint giantPaint;
154 giantPaint.setAntiAlias(true);
155 giantPaint.setColor(0x80808080);
156 canvas->drawCircle(giantCenter.fX, giantCenter.fY, giantRadius, giantPaint);
halcanary9d524f22016-03-29 09:03:52 -0700157
scroggof9d61012014-12-15 12:54:51 -0800158 SkRandom rand;
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000159 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000160 int i;
161 for (i = 0; i < fPaints.count(); ++i) {
162 canvas->save();
163 // position the path, and make it at off-integer coords.
164 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
165 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
166 SkColor color = rand.nextU();
167 color |= 0xff000000;
168 fPaints[i].setColor(color);
169
170 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
171 SkIntToScalar(20),
172 fPaints[i]);
173 canvas->restore();
174 }
175
176 for (int j = 0; j < fMatrices.count(); ++j, ++i) {
177 canvas->save();
178
179 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
180 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
181
182 canvas->concat(fMatrices[j]);
183
184 SkPaint paint;
185 paint.setAntiAlias(true);
186
187 SkColor color = rand.nextU();
188 color |= 0xff000000;
189 paint.setColor(color);
190
191 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
192 SkIntToScalar(20),
193 paint);
194
195 canvas->restore();
196 }
197 }
198
199private:
200 typedef GM INHERITED;
201 SkTArray<SkPaint> fPaints;
202 SkTArray<SkMatrix> fMatrices;
203};
204
205//////////////////////////////////////////////////////////////////////////////
206
207static GM* MyFactory(void*) { return new CircleGM; }
208static GMRegistry reg(MyFactory);
209
210}