blob: b513ccba7b19e9ca834fd90d6cd5673f09be6d00 [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:
mtklein72c9faa2015-01-09 10:06:39 -080028 uint32_t onGetFlags() const SK_OVERRIDE {
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000029 return kSkipTiled_Flag;
30 }
31
mtklein72c9faa2015-01-09 10:06:39 -080032 SkString onShortName() SK_OVERRIDE {
bsalomon@google.com0b26c152012-04-16 14:19:32 +000033 return SkString("circles");
34 }
35
mtklein72c9faa2015-01-09 10:06:39 -080036 SkISize onISize() SK_OVERRIDE {
tfarinaf5393182014-06-09 23:59:03 -070037 return SkISize::Make(1200, 900);
bsalomon@google.com0b26c152012-04-16 14:19:32 +000038 }
39
40 void makePaints() {
41 {
42 // no AA
43 SkPaint p;
44 fPaints.push_back(p);
45 }
46
47 {
48 // AA
49 SkPaint p;
50 p.setAntiAlias(true);
51 fPaints.push_back(p);
52 }
53
54 {
55 // AA with mask filter
56 SkPaint p;
57 p.setAntiAlias(true);
robertphillips@google.comb7061172013-09-06 14:16:12 +000058 SkMaskFilter* mf = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000059 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000060 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
bsalomon@google.com0b26c152012-04-16 14:19:32 +000061 SkBlurMaskFilter::kHighQuality_BlurFlag);
62 p.setMaskFilter(mf)->unref();
63 fPaints.push_back(p);
64 }
65
66 {
67 // AA with radial shader
68 SkPaint p;
69 p.setAntiAlias(true);
70 SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
71 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
72 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
73 SkShader* s = SkGradientShader::CreateRadial(center,
74 SkIntToScalar(20),
75 colors,
76 pos,
77 SK_ARRAY_COUNT(colors),
78 SkShader::kClamp_TileMode);
79 p.setShader(s)->unref();
80 fPaints.push_back(p);
81 }
82
83 {
84 // AA with blur
85 SkPaint p;
86 p.setAntiAlias(true);
87 SkBlurDrawLooper* shadowLooper =
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000088 SkBlurDrawLooper::Create(SK_ColorBLUE,
89 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
90 SkIntToScalar(5), SkIntToScalar(10),
91 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
92 SkBlurDrawLooper::kOverrideColor_BlurFlag |
93 SkBlurDrawLooper::kHighQuality_BlurFlag);
bsalomon@google.com0b26c152012-04-16 14:19:32 +000094 SkAutoUnref aurL0(shadowLooper);
95 p.setLooper(shadowLooper);
96 fPaints.push_back(p);
97 }
98
99 {
100 // AA with stroke style
101 SkPaint p;
102 p.setAntiAlias(true);
103 p.setStyle(SkPaint::kStroke_Style);
104 p.setStrokeWidth(SkIntToScalar(3));
105 fPaints.push_back(p);
106 }
107
108 {
109 // AA with stroke style, width = 0
110 SkPaint p;
111 p.setAntiAlias(true);
112 p.setStyle(SkPaint::kStroke_Style);
113 fPaints.push_back(p);
114 }
115
116 {
117 // AA with stroke and fill style
118 SkPaint p;
119 p.setAntiAlias(true);
120 p.setStyle(SkPaint::kStrokeAndFill_Style);
121 p.setStrokeWidth(SkIntToScalar(2));
122 fPaints.push_back(p);
123 }
124 }
125
126 void makeMatrices() {
127 {
128 SkMatrix m;
129 m.setScale(SkIntToScalar(2), SkIntToScalar(3));
130 fMatrices.push_back(m);
131 }
132
133 {
134 SkMatrix m;
135 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
136 fMatrices.push_back(m);
137 }
138
139 {
140 SkMatrix m;
141 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
142 fMatrices.push_back(m);
143 }
144
145 {
146 SkMatrix m;
147 m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
148 fMatrices.push_back(m);
149 }
150
151 {
152 SkMatrix m;
153 m.setRotate(SkIntToScalar(30));
154 fMatrices.push_back(m);
155 }
156 }
157
mtklein72c9faa2015-01-09 10:06:39 -0800158 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
bsalomonce1c8862014-12-15 07:11:22 -0800159 // Draw a giant AA circle as the background.
160 SkISize size = this->getISize();
161 SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
162 SkIntToScalar(size.fHeight)) / 2.f;
163 SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
164 SkIntToScalar(size.fHeight/2));
165 SkPaint giantPaint;
166 giantPaint.setAntiAlias(true);
167 giantPaint.setColor(0x80808080);
168 canvas->drawCircle(giantCenter.fX, giantCenter.fY, giantRadius, giantPaint);
169
scroggof9d61012014-12-15 12:54:51 -0800170 SkRandom rand;
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000171 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000172 int i;
173 for (i = 0; i < fPaints.count(); ++i) {
174 canvas->save();
175 // position the path, and make it at off-integer coords.
176 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
177 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
178 SkColor color = rand.nextU();
179 color |= 0xff000000;
180 fPaints[i].setColor(color);
181
182 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
183 SkIntToScalar(20),
184 fPaints[i]);
185 canvas->restore();
186 }
187
188 for (int j = 0; j < fMatrices.count(); ++j, ++i) {
189 canvas->save();
190
191 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
192 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
193
194 canvas->concat(fMatrices[j]);
195
196 SkPaint paint;
197 paint.setAntiAlias(true);
198
199 SkColor color = rand.nextU();
200 color |= 0xff000000;
201 paint.setColor(color);
202
203 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
204 SkIntToScalar(20),
205 paint);
206
207 canvas->restore();
208 }
209 }
210
211private:
212 typedef GM INHERITED;
213 SkTArray<SkPaint> fPaints;
214 SkTArray<SkMatrix> fMatrices;
215};
216
217//////////////////////////////////////////////////////////////////////////////
218
219static GM* MyFactory(void*) { return new CircleGM; }
220static GMRegistry reg(MyFactory);
221
222}