blob: 1374b7d35d2483cb5a8a51eecd80a35bd543a1ed [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"
9#include "SkTArray.h"
10#include "SkRandom.h"
11#include "SkMatrix.h"
12#include "SkBlurMaskFilter.h"
13#include "SkGradientShader.h"
14#include "SkBlurDrawLooper.h"
15#include "SkRect.h"
16
17namespace skiagm {
18
19class OvalGM : public GM {
20public:
21 OvalGM() {
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 {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000030 return SkString("ovals");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070034 return SkISize::Make(1200, 900);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +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 stroke style
53 SkPaint p;
54 p.setAntiAlias(true);
55 p.setStyle(SkPaint::kStroke_Style);
56 p.setStrokeWidth(SkIntToScalar(5));
57 fPaints.push_back(p);
58 }
59
60 {
61 // AA with stroke style, width = 0
62 SkPaint p;
63 p.setAntiAlias(true);
64 p.setStyle(SkPaint::kStroke_Style);
65 fPaints.push_back(p);
66 }
67
68 {
69 // AA with stroke and fill style
70 SkPaint p;
71 p.setAntiAlias(true);
72 p.setStyle(SkPaint::kStrokeAndFill_Style);
73 p.setStrokeWidth(SkIntToScalar(3));
74 fPaints.push_back(p);
75 }
76 }
77
78 void makeMatrices() {
79 {
80 SkMatrix m;
81 m.setIdentity();
82 fMatrices.push_back(m);
83 }
84
85 {
86 SkMatrix m;
87 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
88 fMatrices.push_back(m);
89 }
90
91 {
92 SkMatrix m;
93 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
94 fMatrices.push_back(m);
95 }
96
97 {
98 SkMatrix m;
99 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
100 fMatrices.push_back(m);
101 }
102
103 {
104 SkMatrix m;
105 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
106 fMatrices.push_back(m);
107 }
108
109 {
110 SkMatrix m;
111 m.setRotate(SkIntToScalar(90));
112 fMatrices.push_back(m);
113 }
114
115 {
116 SkMatrix m;
117 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
118 fMatrices.push_back(m);
119 }
120
121 {
122 SkMatrix m;
123 m.setRotate(SkIntToScalar(60));
124 fMatrices.push_back(m);
125 }
126 }
127
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000128 SkColor genColor(SkRandom* rand) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000129 SkScalar hsv[3];
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000130 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
131 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
132 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000133
caryclarkf597c422015-07-28 10:37:53 -0700134 return sk_tool_utils::color_to_565(SkHSVToColor(hsv));
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000135 }
136
mtklein36352bf2015-03-25 18:17:31 -0700137 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000138 SkRandom rand(1);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000139 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
140 SkRect oval = SkRect::MakeLTRB(-20, -30, 20, 30);
141
142 const SkScalar kXStart = 60.0f;
143 const SkScalar kYStart = 80.0f;
144 const int kXStep = 150;
145 const int kYStep = 160;
146 int maxX = fMatrices.count();
147
148 SkPaint rectPaint;
149 rectPaint.setAntiAlias(true);
150 rectPaint.setStyle(SkPaint::kStroke_Style);
151 rectPaint.setStrokeWidth(SkIntToScalar(0));
caryclarkf597c422015-07-28 10:37:53 -0700152 rectPaint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000153
154 int testCount = 0;
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000155 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000156 for (int j = 0; j < fMatrices.count(); ++j) {
157 canvas->save();
158 SkMatrix mat = fMatrices[j];
159 // position the oval, and make it at off-integer coords.
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000160 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000161 SK_Scalar1 / 4,
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000162 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000163 3 * SK_Scalar1 / 4);
164 canvas->concat(mat);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000165
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000166 SkColor color = genColor(&rand);
167 fPaints[i].setColor(color);
168
169 canvas->drawRect(oval, rectPaint);
170 canvas->drawOval(oval, fPaints[i]);
171
172 canvas->restore();
173
174 ++testCount;
175 }
176 }
177
178 // special cases
179
180 // non-scaled tall and skinny oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000181 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000182 SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60);
183 canvas->save();
184 // position the oval, and make it at off-integer coords.
185 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
186 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000187
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000188 SkColor color = genColor(&rand);
189 fPaints[i].setColor(color);
190
191 canvas->drawRect(oval, rectPaint);
192 canvas->drawOval(oval, fPaints[i]);
193 canvas->restore();
194 }
195
196 // non-scaled wide and short oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000197 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000198 SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30);
199 canvas->save();
200 // position the oval, and make it at off-integer coords.
201 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
202 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
203 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000204
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000205 SkColor color = genColor(&rand);
206 fPaints[i].setColor(color);
207
208 canvas->drawRect(oval, rectPaint);
209 canvas->drawOval(oval, fPaints[i]);
210 canvas->restore();
211 }
212
213 // super skinny oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000214 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000215 SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60);
216 canvas->save();
217 // position the oval, and make it at off-integer coords.
218 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
219 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000220
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000221 SkColor color = genColor(&rand);
222 fPaints[i].setColor(color);
223
224 canvas->drawOval(oval, fPaints[i]);
225 canvas->restore();
226 }
227
228 // super short oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000229 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000230 SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0);
231 canvas->save();
232 // position the oval, and make it at off-integer coords.
233 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
234 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
235 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000236
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000237 SkColor color = genColor(&rand);
238 fPaints[i].setColor(color);
239
240 canvas->drawOval(oval, fPaints[i]);
241 canvas->restore();
242 }
243
244 // radial gradient
245 SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
246 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
247 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700248 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
249 SkShader::kClamp_TileMode);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000250
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000251 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000252 canvas->save();
253 // position the path, and make it at off-integer coords.
254 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
255 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
256 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000257
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000258 SkColor color = genColor(&rand);
259 fPaints[i].setColor(color);
260 fPaints[i].setShader(shader);
261
262 canvas->drawRect(oval, rectPaint);
263 canvas->drawOval(oval, fPaints[i]);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000264
halcanary96fcdcc2015-08-27 07:41:13 -0700265 fPaints[i].setShader(nullptr);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000266
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000267 canvas->restore();
268 }
269 }
270
271private:
272 SkTArray<SkPaint> fPaints;
273 SkTArray<SkMatrix> fMatrices;
274
275 typedef GM INHERITED;
276};
277
278//////////////////////////////////////////////////////////////////////////////
279
280static GM* MyFactory(void*) { return new OvalGM; }
281static GMRegistry reg(MyFactory);
282
283}