blob: 369548cc5996d878aad3568dc818bd805b2a5fa5 [file] [log] [blame]
bsalomon@google.com0b26c152012-04-16 14:19:32 +00001
2/*
3 * Copyright 2012 Intel Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurDrawLooper.h"
10#include "SkBlurMask.h"
bsalomon@google.com0b26c152012-04-16 14:19:32 +000011#include "SkBlurMaskFilter.h"
12#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);
robertphillips@google.comb7061172013-09-06 14:16:12 +000055 SkMaskFilter* mf = SkBlurMaskFilter::Create(
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)),
bsalomon@google.com0b26c152012-04-16 14:19:32 +000058 SkBlurMaskFilter::kHighQuality_BlurFlag);
59 p.setMaskFilter(mf)->unref();
60 fPaints.push_back(p);
61 }
62
63 {
64 // AA with radial shader
65 SkPaint p;
66 p.setAntiAlias(true);
67 SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
68 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
69 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
70 SkShader* s = SkGradientShader::CreateRadial(center,
71 SkIntToScalar(20),
72 colors,
73 pos,
74 SK_ARRAY_COUNT(colors),
75 SkShader::kClamp_TileMode);
76 p.setShader(s)->unref();
77 fPaints.push_back(p);
78 }
79
80 {
81 // AA with blur
82 SkPaint p;
83 p.setAntiAlias(true);
reed5e1ddb12015-12-21 08:52:45 -080084 SkDrawLooper* shadowLooper =
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000085 SkBlurDrawLooper::Create(SK_ColorBLUE,
86 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
87 SkIntToScalar(5), SkIntToScalar(10),
88 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
89 SkBlurDrawLooper::kOverrideColor_BlurFlag |
90 SkBlurDrawLooper::kHighQuality_BlurFlag);
bsalomon@google.com0b26c152012-04-16 14:19:32 +000091 SkAutoUnref aurL0(shadowLooper);
92 p.setLooper(shadowLooper);
93 fPaints.push_back(p);
94 }
95
96 {
97 // AA with stroke style
98 SkPaint p;
99 p.setAntiAlias(true);
100 p.setStyle(SkPaint::kStroke_Style);
101 p.setStrokeWidth(SkIntToScalar(3));
102 fPaints.push_back(p);
103 }
104
105 {
106 // AA with stroke style, width = 0
107 SkPaint p;
108 p.setAntiAlias(true);
109 p.setStyle(SkPaint::kStroke_Style);
110 fPaints.push_back(p);
111 }
112
113 {
114 // AA with stroke and fill style
115 SkPaint p;
116 p.setAntiAlias(true);
117 p.setStyle(SkPaint::kStrokeAndFill_Style);
118 p.setStrokeWidth(SkIntToScalar(2));
119 fPaints.push_back(p);
120 }
121 }
122
123 void makeMatrices() {
124 {
125 SkMatrix m;
126 m.setScale(SkIntToScalar(2), SkIntToScalar(3));
127 fMatrices.push_back(m);
128 }
129
130 {
131 SkMatrix m;
132 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
133 fMatrices.push_back(m);
134 }
135
136 {
137 SkMatrix m;
138 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
139 fMatrices.push_back(m);
140 }
141
142 {
143 SkMatrix m;
144 m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
145 fMatrices.push_back(m);
146 }
147
148 {
149 SkMatrix m;
150 m.setRotate(SkIntToScalar(30));
151 fMatrices.push_back(m);
152 }
153 }
154
mtklein36352bf2015-03-25 18:17:31 -0700155 void onDraw(SkCanvas* canvas) override {
bsalomonce1c8862014-12-15 07:11:22 -0800156 // Draw a giant AA circle as the background.
157 SkISize size = this->getISize();
158 SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
159 SkIntToScalar(size.fHeight)) / 2.f;
160 SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
161 SkIntToScalar(size.fHeight/2));
162 SkPaint giantPaint;
163 giantPaint.setAntiAlias(true);
164 giantPaint.setColor(0x80808080);
165 canvas->drawCircle(giantCenter.fX, giantCenter.fY, giantRadius, giantPaint);
166
scroggof9d61012014-12-15 12:54:51 -0800167 SkRandom rand;
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000168 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000169 int i;
170 for (i = 0; i < fPaints.count(); ++i) {
171 canvas->save();
172 // position the path, and make it at off-integer coords.
173 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
174 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
175 SkColor color = rand.nextU();
176 color |= 0xff000000;
177 fPaints[i].setColor(color);
178
179 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
180 SkIntToScalar(20),
181 fPaints[i]);
182 canvas->restore();
183 }
184
185 for (int j = 0; j < fMatrices.count(); ++j, ++i) {
186 canvas->save();
187
188 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
189 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
190
191 canvas->concat(fMatrices[j]);
192
193 SkPaint paint;
194 paint.setAntiAlias(true);
195
196 SkColor color = rand.nextU();
197 color |= 0xff000000;
198 paint.setColor(color);
199
200 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
201 SkIntToScalar(20),
202 paint);
203
204 canvas->restore();
205 }
206 }
207
208private:
209 typedef GM INHERITED;
210 SkTArray<SkPaint> fPaints;
211 SkTArray<SkMatrix> fMatrices;
212};
213
214//////////////////////////////////////////////////////////////////////////////
215
216static GM* MyFactory(void*) { return new CircleGM; }
217static GMRegistry reg(MyFactory);
218
219}