blob: d6afdcbba776d248ad4ecf30c20c8ecb001a9585 [file] [log] [blame]
jvanverth@google.com8e2962f2013-04-18 13:59:04 +00001/*
2 * Copyright 2013 Google 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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000010#include "SkTArray.h"
11#include "SkRandom.h"
12#include "SkMatrix.h"
13#include "SkBlurMaskFilter.h"
mtklein7a1f45f2016-08-04 06:19:33 -070014#include "SkColorFilter.h"
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000015#include "SkGradientShader.h"
16#include "SkBlurDrawLooper.h"
17#include "SkRect.h"
18
19namespace skiagm {
20
21class OvalGM : public GM {
22public:
23 OvalGM() {
24 this->setBGColor(0xFF000000);
25 this->makePaints();
26 this->makeMatrices();
27 }
28
29protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000030
mtklein36352bf2015-03-25 18:17:31 -070031 SkString onShortName() override {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000032 return SkString("ovals");
33 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070036 return SkISize::Make(1200, 900);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000037 }
38
39 void makePaints() {
40 {
41 // no AA
42 SkPaint p;
43 fPaints.push_back(p);
44 }
45
46 {
47 // AA
48 SkPaint p;
49 p.setAntiAlias(true);
50 fPaints.push_back(p);
51 }
52
53 {
54 // AA with stroke style
55 SkPaint p;
56 p.setAntiAlias(true);
57 p.setStyle(SkPaint::kStroke_Style);
58 p.setStrokeWidth(SkIntToScalar(5));
59 fPaints.push_back(p);
60 }
61
62 {
63 // AA with stroke style, width = 0
64 SkPaint p;
65 p.setAntiAlias(true);
66 p.setStyle(SkPaint::kStroke_Style);
67 fPaints.push_back(p);
68 }
69
70 {
71 // AA with stroke and fill style
72 SkPaint p;
73 p.setAntiAlias(true);
74 p.setStyle(SkPaint::kStrokeAndFill_Style);
75 p.setStrokeWidth(SkIntToScalar(3));
76 fPaints.push_back(p);
77 }
78 }
79
80 void makeMatrices() {
81 {
82 SkMatrix m;
83 m.setIdentity();
84 fMatrices.push_back(m);
85 }
86
87 {
88 SkMatrix m;
89 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
90 fMatrices.push_back(m);
91 }
92
93 {
94 SkMatrix m;
95 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
96 fMatrices.push_back(m);
97 }
98
99 {
100 SkMatrix m;
101 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
102 fMatrices.push_back(m);
103 }
104
105 {
106 SkMatrix m;
107 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
108 fMatrices.push_back(m);
109 }
110
111 {
112 SkMatrix m;
113 m.setRotate(SkIntToScalar(90));
114 fMatrices.push_back(m);
115 }
116
117 {
118 SkMatrix m;
119 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
120 fMatrices.push_back(m);
121 }
122
123 {
124 SkMatrix m;
125 m.setRotate(SkIntToScalar(60));
126 fMatrices.push_back(m);
127 }
128 }
129
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000130 SkColor genColor(SkRandom* rand) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000131 SkScalar hsv[3];
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000132 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
133 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
134 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000135
caryclarkf597c422015-07-28 10:37:53 -0700136 return sk_tool_utils::color_to_565(SkHSVToColor(hsv));
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000137 }
138
mtklein36352bf2015-03-25 18:17:31 -0700139 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000140 SkRandom rand(1);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000141 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
142 SkRect oval = SkRect::MakeLTRB(-20, -30, 20, 30);
143
144 const SkScalar kXStart = 60.0f;
145 const SkScalar kYStart = 80.0f;
146 const int kXStep = 150;
147 const int kYStep = 160;
148 int maxX = fMatrices.count();
149
150 SkPaint rectPaint;
151 rectPaint.setAntiAlias(true);
152 rectPaint.setStyle(SkPaint::kStroke_Style);
153 rectPaint.setStrokeWidth(SkIntToScalar(0));
caryclarkf597c422015-07-28 10:37:53 -0700154 rectPaint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000155
156 int testCount = 0;
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000157 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000158 for (int j = 0; j < fMatrices.count(); ++j) {
159 canvas->save();
160 SkMatrix mat = fMatrices[j];
161 // position the oval, and make it at off-integer coords.
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000162 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000163 SK_Scalar1 / 4,
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000164 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000165 3 * SK_Scalar1 / 4);
166 canvas->concat(mat);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000167
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000168 SkColor color = genColor(&rand);
169 fPaints[i].setColor(color);
170
171 canvas->drawRect(oval, rectPaint);
172 canvas->drawOval(oval, fPaints[i]);
173
174 canvas->restore();
175
176 ++testCount;
177 }
178 }
179
180 // special cases
181
182 // non-scaled tall and skinny oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000183 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000184 SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60);
185 canvas->save();
186 // position the oval, and make it at off-integer coords.
187 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
188 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000189
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000190 SkColor color = genColor(&rand);
191 fPaints[i].setColor(color);
192
193 canvas->drawRect(oval, rectPaint);
194 canvas->drawOval(oval, fPaints[i]);
195 canvas->restore();
196 }
197
198 // non-scaled wide and short oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000199 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000200 SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30);
201 canvas->save();
202 // position the oval, and make it at off-integer coords.
203 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
204 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
205 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000206
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000207 SkColor color = genColor(&rand);
208 fPaints[i].setColor(color);
209
210 canvas->drawRect(oval, rectPaint);
211 canvas->drawOval(oval, fPaints[i]);
212 canvas->restore();
213 }
214
215 // super skinny oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000216 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000217 SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60);
218 canvas->save();
219 // position the oval, and make it at off-integer coords.
220 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
221 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000222
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000223 SkColor color = genColor(&rand);
224 fPaints[i].setColor(color);
225
226 canvas->drawOval(oval, fPaints[i]);
227 canvas->restore();
228 }
229
230 // super short oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000231 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000232 SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0);
233 canvas->save();
234 // position the oval, and make it at off-integer coords.
235 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
236 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
237 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000238
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000239 SkColor color = genColor(&rand);
240 fPaints[i].setColor(color);
241
242 canvas->drawOval(oval, fPaints[i]);
243 canvas->restore();
244 }
245
246 // radial gradient
247 SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
248 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
249 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700250 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
251 SkShader::kClamp_TileMode);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000252
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000253 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000254 canvas->save();
255 // position the path, and make it at off-integer coords.
256 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
257 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
258 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000259
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000260 SkColor color = genColor(&rand);
261 fPaints[i].setColor(color);
262 fPaints[i].setShader(shader);
263
264 canvas->drawRect(oval, rectPaint);
265 canvas->drawOval(oval, fPaints[i]);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000266
halcanary96fcdcc2015-08-27 07:41:13 -0700267 fPaints[i].setShader(nullptr);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000268
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000269 canvas->restore();
270 }
Jim Van Verthd952a992017-04-20 17:25:26 -0400271
272 // reflected oval
273 for (int i = 0; i < fPaints.count(); ++i) {
274 SkRect oval = SkRect::MakeLTRB(-30, -30, 30, 30);
275 canvas->save();
276 // position the oval, and make it at off-integer coords.
277 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
278 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
279 SK_ScalarHalf * kYStep);
280 canvas->rotate(90);
281 canvas->scale(1, -1);
282 canvas->scale(1, 0.66f);
283
284 SkColor color = genColor(&rand);
285 fPaints[i].setColor(color);
286
287 canvas->drawRect(oval, rectPaint);
288 canvas->drawOval(oval, fPaints[i]);
289 canvas->restore();
290 }
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000291 }
292
293private:
294 SkTArray<SkPaint> fPaints;
295 SkTArray<SkMatrix> fMatrices;
296
297 typedef GM INHERITED;
298};
299
300//////////////////////////////////////////////////////////////////////////////
301
302static GM* MyFactory(void*) { return new OvalGM; }
303static GMRegistry reg(MyFactory);
304
305}