blob: 1e7af74c2a9274de5a17eb16d088e57deacecf6a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkMatrix.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkPaint.h"
13#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkScalar.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTileMode.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/effects/SkGradientShader.h"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050022#include "include/private/base/SkTArray.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050023#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/ToolUtils.h"
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000025
Herb Derbyec96c212023-03-06 10:31:22 -050026using namespace skia_private;
27
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000028namespace skiagm {
29
30class OvalGM : public GM {
31public:
32 OvalGM() {
33 this->setBGColor(0xFF000000);
34 this->makePaints();
35 this->makeMatrices();
36 }
37
38protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000039 SkString getName() const override { return SkString("ovals"); }
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000040
Leandro Lovisolo8f023882023-08-15 21:13:52 +000041 SkISize getISize() override { return SkISize::Make(1200, 900); }
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000042
43 void makePaints() {
44 {
45 // no AA
46 SkPaint p;
47 fPaints.push_back(p);
48 }
49
50 {
51 // AA
52 SkPaint p;
53 p.setAntiAlias(true);
54 fPaints.push_back(p);
55 }
56
57 {
58 // AA with stroke style
59 SkPaint p;
60 p.setAntiAlias(true);
61 p.setStyle(SkPaint::kStroke_Style);
62 p.setStrokeWidth(SkIntToScalar(5));
63 fPaints.push_back(p);
64 }
65
66 {
67 // AA with stroke style, width = 0
68 SkPaint p;
69 p.setAntiAlias(true);
70 p.setStyle(SkPaint::kStroke_Style);
71 fPaints.push_back(p);
72 }
73
74 {
75 // AA with stroke and fill style
76 SkPaint p;
77 p.setAntiAlias(true);
78 p.setStyle(SkPaint::kStrokeAndFill_Style);
79 p.setStrokeWidth(SkIntToScalar(3));
80 fPaints.push_back(p);
81 }
82 }
83
84 void makeMatrices() {
85 {
86 SkMatrix m;
87 m.setIdentity();
88 fMatrices.push_back(m);
89 }
90
91 {
92 SkMatrix m;
93 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
94 fMatrices.push_back(m);
95 }
96
97 {
98 SkMatrix m;
99 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
100 fMatrices.push_back(m);
101 }
102
103 {
104 SkMatrix m;
105 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
106 fMatrices.push_back(m);
107 }
108
109 {
110 SkMatrix m;
111 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
112 fMatrices.push_back(m);
113 }
114
115 {
116 SkMatrix m;
117 m.setRotate(SkIntToScalar(90));
118 fMatrices.push_back(m);
119 }
120
121 {
122 SkMatrix m;
123 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
124 fMatrices.push_back(m);
125 }
126
127 {
128 SkMatrix m;
129 m.setRotate(SkIntToScalar(60));
130 fMatrices.push_back(m);
131 }
132 }
133
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000134 SkColor genColor(SkRandom* rand) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000135 SkScalar hsv[3];
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000136 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
137 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
138 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000139
Mike Kleinea3f0142019-03-20 11:12:10 -0500140 return ToolUtils::color_to_565(SkHSVToColor(hsv));
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000141 }
142
mtklein36352bf2015-03-25 18:17:31 -0700143 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000144 SkRandom rand(1);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000145 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
John Stilesacf71642021-08-12 22:33:57 -0400146 const SkRect kOval = SkRect::MakeLTRB(-20, -30, 20, 30);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000147
148 const SkScalar kXStart = 60.0f;
149 const SkScalar kYStart = 80.0f;
150 const int kXStep = 150;
151 const int kYStep = 160;
Herb Derbyffacce52022-11-09 10:51:34 -0500152 int maxX = fMatrices.size();
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000153
154 SkPaint rectPaint;
155 rectPaint.setAntiAlias(true);
156 rectPaint.setStyle(SkPaint::kStroke_Style);
157 rectPaint.setStrokeWidth(SkIntToScalar(0));
Mike Kleind46dce32018-08-16 10:17:03 -0400158 rectPaint.setColor(SK_ColorLTGRAY);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000159
160 int testCount = 0;
Herb Derbyffacce52022-11-09 10:51:34 -0500161 for (int i = 0; i < fPaints.size(); ++i) {
162 for (int j = 0; j < fMatrices.size(); ++j) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000163 canvas->save();
164 SkMatrix mat = fMatrices[j];
165 // position the oval, and make it at off-integer coords.
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000166 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000167 SK_Scalar1 / 4,
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000168 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000169 3 * SK_Scalar1 / 4);
170 canvas->concat(mat);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000171
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000172 SkColor color = genColor(&rand);
173 fPaints[i].setColor(color);
174
John Stilesacf71642021-08-12 22:33:57 -0400175 canvas->drawRect(kOval, rectPaint);
176 canvas->drawOval(kOval, fPaints[i]);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000177
178 canvas->restore();
179
180 ++testCount;
181 }
182 }
183
184 // special cases
185
186 // non-scaled tall and skinny oval
Herb Derbyffacce52022-11-09 10:51:34 -0500187 for (int i = 0; i < fPaints.size(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000188 SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60);
189 canvas->save();
190 // position the oval, and make it at off-integer coords.
191 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
192 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000193
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000194 SkColor color = genColor(&rand);
195 fPaints[i].setColor(color);
196
197 canvas->drawRect(oval, rectPaint);
198 canvas->drawOval(oval, fPaints[i]);
199 canvas->restore();
200 }
201
202 // non-scaled wide and short oval
Herb Derbyffacce52022-11-09 10:51:34 -0500203 for (int i = 0; i < fPaints.size(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000204 SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30);
205 canvas->save();
206 // position the oval, and make it at off-integer coords.
207 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
208 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
209 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000210
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000211 SkColor color = genColor(&rand);
212 fPaints[i].setColor(color);
213
214 canvas->drawRect(oval, rectPaint);
215 canvas->drawOval(oval, fPaints[i]);
216 canvas->restore();
217 }
218
219 // super skinny oval
Herb Derbyffacce52022-11-09 10:51:34 -0500220 for (int i = 0; i < fPaints.size(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000221 SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60);
222 canvas->save();
223 // position the oval, and make it at off-integer coords.
224 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
225 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000226
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000227 SkColor color = genColor(&rand);
228 fPaints[i].setColor(color);
229
230 canvas->drawOval(oval, fPaints[i]);
231 canvas->restore();
232 }
233
234 // super short oval
Herb Derbyffacce52022-11-09 10:51:34 -0500235 for (int i = 0; i < fPaints.size(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000236 SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0);
237 canvas->save();
238 // position the oval, and make it at off-integer coords.
239 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
240 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
241 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000242
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000243 SkColor color = genColor(&rand);
244 fPaints[i].setColor(color);
245
246 canvas->drawOval(oval, fPaints[i]);
247 canvas->restore();
248 }
249
250 // radial gradient
251 SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
252 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
253 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
Herb Derbyc37b3862022-06-21 09:49:17 -0400254 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400255 SkTileMode::kClamp);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000256
Herb Derbyffacce52022-11-09 10:51:34 -0500257 for (int i = 0; i < fPaints.size(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000258 canvas->save();
259 // position the path, and make it at off-integer coords.
260 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
261 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
262 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000263
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000264 SkColor color = genColor(&rand);
265 fPaints[i].setColor(color);
266 fPaints[i].setShader(shader);
267
John Stilesacf71642021-08-12 22:33:57 -0400268 canvas->drawRect(kOval, rectPaint);
269 canvas->drawOval(kOval, fPaints[i]);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000270
halcanary96fcdcc2015-08-27 07:41:13 -0700271 fPaints[i].setShader(nullptr);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000272
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000273 canvas->restore();
274 }
Jim Van Verthd952a992017-04-20 17:25:26 -0400275
276 // reflected oval
Herb Derbyffacce52022-11-09 10:51:34 -0500277 for (int i = 0; i < fPaints.size(); ++i) {
Jim Van Verthd952a992017-04-20 17:25:26 -0400278 SkRect oval = SkRect::MakeLTRB(-30, -30, 30, 30);
279 canvas->save();
280 // position the oval, and make it at off-integer coords.
281 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
282 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
283 SK_ScalarHalf * kYStep);
284 canvas->rotate(90);
285 canvas->scale(1, -1);
286 canvas->scale(1, 0.66f);
287
288 SkColor color = genColor(&rand);
289 fPaints[i].setColor(color);
290
291 canvas->drawRect(oval, rectPaint);
292 canvas->drawOval(oval, fPaints[i]);
293 canvas->restore();
294 }
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000295 }
296
297private:
Herb Derbyec96c212023-03-06 10:31:22 -0500298 TArray<SkPaint> fPaints;
299 TArray<SkMatrix> fMatrices;
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000300
John Stiles7571f9e2020-09-02 22:42:33 -0400301 using INHERITED = GM;
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000302};
303
304//////////////////////////////////////////////////////////////////////////////
305
Hal Canarye964c182019-01-23 10:22:01 -0500306DEF_GM( return new OvalGM; )
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000307
John Stilesa6841be2020-08-06 14:11:56 -0400308} // namespace skiagm