blob: 2c2542b9dcdf14aa6605dad760c195029c1fdaf8 [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"
11#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000012#include "SkMatrix.h"
13#include "SkRandom.h"
14#include "SkTArray.h"
bsalomon@google.com0b26c152012-04-16 14:19:32 +000015
16namespace skiagm {
17
18class CircleGM : public GM {
19public:
20 CircleGM() {
21 this->setBGColor(0xFF000000);
22 this->makePaints();
23 this->makeMatrices();
24 }
25
26protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000027
mtklein36352bf2015-03-25 18:17:31 -070028 SkString onShortName() override {
bsalomon@google.com0b26c152012-04-16 14:19:32 +000029 return SkString("circles");
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070033 return SkISize::Make(1200, 900);
bsalomon@google.com0b26c152012-04-16 14:19:32 +000034 }
35
36 void makePaints() {
37 {
38 // no AA
39 SkPaint p;
40 fPaints.push_back(p);
41 }
42
43 {
44 // AA
45 SkPaint p;
46 p.setAntiAlias(true);
47 fPaints.push_back(p);
48 }
49
50 {
51 // AA with mask filter
52 SkPaint p;
53 p.setAntiAlias(true);
reedefdfd512016-04-04 10:02:58 -070054 p.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000055 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000056 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
reedefdfd512016-04-04 10:02:58 -070057 SkBlurMaskFilter::kHighQuality_BlurFlag));
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)),
79 SkIntToScalar(5), SkIntToScalar(10),
80 SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
81 SkBlurDrawLooper::kOverrideColor_BlurFlag |
reed7b380d02016-03-21 13:25:16 -070082 SkBlurDrawLooper::kHighQuality_BlurFlag));
bsalomon@google.com0b26c152012-04-16 14:19:32 +000083 fPaints.push_back(p);
84 }
85
86 {
87 // AA with stroke style
88 SkPaint p;
89 p.setAntiAlias(true);
90 p.setStyle(SkPaint::kStroke_Style);
91 p.setStrokeWidth(SkIntToScalar(3));
92 fPaints.push_back(p);
93 }
94
95 {
96 // AA with stroke style, width = 0
97 SkPaint p;
98 p.setAntiAlias(true);
99 p.setStyle(SkPaint::kStroke_Style);
100 fPaints.push_back(p);
101 }
102
103 {
104 // AA with stroke and fill style
105 SkPaint p;
106 p.setAntiAlias(true);
107 p.setStyle(SkPaint::kStrokeAndFill_Style);
108 p.setStrokeWidth(SkIntToScalar(2));
109 fPaints.push_back(p);
110 }
111 }
112
113 void makeMatrices() {
114 {
115 SkMatrix m;
116 m.setScale(SkIntToScalar(2), SkIntToScalar(3));
117 fMatrices.push_back(m);
118 }
119
120 {
121 SkMatrix m;
122 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
123 fMatrices.push_back(m);
124 }
125
126 {
127 SkMatrix m;
128 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
129 fMatrices.push_back(m);
130 }
131
132 {
133 SkMatrix m;
134 m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
135 fMatrices.push_back(m);
136 }
137
138 {
139 SkMatrix m;
140 m.setRotate(SkIntToScalar(30));
141 fMatrices.push_back(m);
142 }
143 }
144
mtklein36352bf2015-03-25 18:17:31 -0700145 void onDraw(SkCanvas* canvas) override {
bsalomonce1c8862014-12-15 07:11:22 -0800146 // Draw a giant AA circle as the background.
147 SkISize size = this->getISize();
148 SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
149 SkIntToScalar(size.fHeight)) / 2.f;
150 SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
151 SkIntToScalar(size.fHeight/2));
152 SkPaint giantPaint;
153 giantPaint.setAntiAlias(true);
154 giantPaint.setColor(0x80808080);
155 canvas->drawCircle(giantCenter.fX, giantCenter.fY, giantRadius, giantPaint);
halcanary9d524f22016-03-29 09:03:52 -0700156
scroggof9d61012014-12-15 12:54:51 -0800157 SkRandom rand;
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000158 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
bsalomon@google.com0b26c152012-04-16 14:19:32 +0000159 int i;
160 for (i = 0; i < fPaints.count(); ++i) {
161 canvas->save();
162 // position the path, and make it at off-integer coords.
163 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
164 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
165 SkColor color = rand.nextU();
166 color |= 0xff000000;
167 fPaints[i].setColor(color);
168
169 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
170 SkIntToScalar(20),
171 fPaints[i]);
172 canvas->restore();
173 }
174
175 for (int j = 0; j < fMatrices.count(); ++j, ++i) {
176 canvas->save();
177
178 canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
179 SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
180
181 canvas->concat(fMatrices[j]);
182
183 SkPaint paint;
184 paint.setAntiAlias(true);
185
186 SkColor color = rand.nextU();
187 color |= 0xff000000;
188 paint.setColor(color);
189
190 canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
191 SkIntToScalar(20),
192 paint);
193
194 canvas->restore();
195 }
196 }
197
198private:
199 typedef GM INHERITED;
200 SkTArray<SkPaint> fPaints;
201 SkTArray<SkMatrix> fMatrices;
202};
203
204//////////////////////////////////////////////////////////////////////////////
205
206static GM* MyFactory(void*) { return new CircleGM; }
207static GMRegistry reg(MyFactory);
208
209}