blob: b7cb6b55019a46ea3bebb8033c8902f34e6f9160 [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"
22#include "include/private/SkTArray.h"
23#include "include/utils/SkRandom.h"
24#include "tools/ToolUtils.h"
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000025
26namespace skiagm {
27
28class OvalGM : public GM {
29public:
30 OvalGM() {
31 this->setBGColor(0xFF000000);
32 this->makePaints();
33 this->makeMatrices();
34 }
35
36protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000037
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000039 return SkString("ovals");
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070043 return SkISize::Make(1200, 900);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +000044 }
45
46 void makePaints() {
47 {
48 // no AA
49 SkPaint p;
50 fPaints.push_back(p);
51 }
52
53 {
54 // AA
55 SkPaint p;
56 p.setAntiAlias(true);
57 fPaints.push_back(p);
58 }
59
60 {
61 // AA with stroke style
62 SkPaint p;
63 p.setAntiAlias(true);
64 p.setStyle(SkPaint::kStroke_Style);
65 p.setStrokeWidth(SkIntToScalar(5));
66 fPaints.push_back(p);
67 }
68
69 {
70 // AA with stroke style, width = 0
71 SkPaint p;
72 p.setAntiAlias(true);
73 p.setStyle(SkPaint::kStroke_Style);
74 fPaints.push_back(p);
75 }
76
77 {
78 // AA with stroke and fill style
79 SkPaint p;
80 p.setAntiAlias(true);
81 p.setStyle(SkPaint::kStrokeAndFill_Style);
82 p.setStrokeWidth(SkIntToScalar(3));
83 fPaints.push_back(p);
84 }
85 }
86
87 void makeMatrices() {
88 {
89 SkMatrix m;
90 m.setIdentity();
91 fMatrices.push_back(m);
92 }
93
94 {
95 SkMatrix m;
96 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
97 fMatrices.push_back(m);
98 }
99
100 {
101 SkMatrix m;
102 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
103 fMatrices.push_back(m);
104 }
105
106 {
107 SkMatrix m;
108 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
109 fMatrices.push_back(m);
110 }
111
112 {
113 SkMatrix m;
114 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
115 fMatrices.push_back(m);
116 }
117
118 {
119 SkMatrix m;
120 m.setRotate(SkIntToScalar(90));
121 fMatrices.push_back(m);
122 }
123
124 {
125 SkMatrix m;
126 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
127 fMatrices.push_back(m);
128 }
129
130 {
131 SkMatrix m;
132 m.setRotate(SkIntToScalar(60));
133 fMatrices.push_back(m);
134 }
135 }
136
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000137 SkColor genColor(SkRandom* rand) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000138 SkScalar hsv[3];
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000139 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
140 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
141 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000142
Mike Kleinea3f0142019-03-20 11:12:10 -0500143 return ToolUtils::color_to_565(SkHSVToColor(hsv));
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000144 }
145
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000147 SkRandom rand(1);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000148 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
149 SkRect oval = SkRect::MakeLTRB(-20, -30, 20, 30);
150
151 const SkScalar kXStart = 60.0f;
152 const SkScalar kYStart = 80.0f;
153 const int kXStep = 150;
154 const int kYStep = 160;
155 int maxX = fMatrices.count();
156
157 SkPaint rectPaint;
158 rectPaint.setAntiAlias(true);
159 rectPaint.setStyle(SkPaint::kStroke_Style);
160 rectPaint.setStrokeWidth(SkIntToScalar(0));
Mike Kleind46dce32018-08-16 10:17:03 -0400161 rectPaint.setColor(SK_ColorLTGRAY);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000162
163 int testCount = 0;
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000164 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000165 for (int j = 0; j < fMatrices.count(); ++j) {
166 canvas->save();
167 SkMatrix mat = fMatrices[j];
168 // position the oval, and make it at off-integer coords.
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000169 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000170 SK_Scalar1 / 4,
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000171 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000172 3 * SK_Scalar1 / 4);
173 canvas->concat(mat);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000174
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000175 SkColor color = genColor(&rand);
176 fPaints[i].setColor(color);
177
178 canvas->drawRect(oval, rectPaint);
179 canvas->drawOval(oval, fPaints[i]);
180
181 canvas->restore();
182
183 ++testCount;
184 }
185 }
186
187 // special cases
188
189 // non-scaled tall and skinny oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000190 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000191 SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60);
192 canvas->save();
193 // position the oval, and make it at off-integer coords.
194 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
195 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000196
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000197 SkColor color = genColor(&rand);
198 fPaints[i].setColor(color);
199
200 canvas->drawRect(oval, rectPaint);
201 canvas->drawOval(oval, fPaints[i]);
202 canvas->restore();
203 }
204
205 // non-scaled wide and short oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000206 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000207 SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30);
208 canvas->save();
209 // position the oval, and make it at off-integer coords.
210 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
211 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
212 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000213
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000214 SkColor color = genColor(&rand);
215 fPaints[i].setColor(color);
216
217 canvas->drawRect(oval, rectPaint);
218 canvas->drawOval(oval, fPaints[i]);
219 canvas->restore();
220 }
221
222 // super skinny oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000223 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000224 SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60);
225 canvas->save();
226 // position the oval, and make it at off-integer coords.
227 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
228 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000229
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000230 SkColor color = genColor(&rand);
231 fPaints[i].setColor(color);
232
233 canvas->drawOval(oval, fPaints[i]);
234 canvas->restore();
235 }
236
237 // super short oval
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000238 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000239 SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0);
240 canvas->save();
241 // position the oval, and make it at off-integer coords.
242 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
243 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
244 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000245
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000246 SkColor color = genColor(&rand);
247 fPaints[i].setColor(color);
248
249 canvas->drawOval(oval, fPaints[i]);
250 canvas->restore();
251 }
252
253 // radial gradient
254 SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
255 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
256 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700257 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400258 SkTileMode::kClamp);
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000259
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000260 for (int i = 0; i < fPaints.count(); ++i) {
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000261 canvas->save();
262 // position the path, and make it at off-integer coords.
263 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
264 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
265 SK_ScalarHalf * kYStep);
skia.committer@gmail.comcb6dc752013-04-19 07:01:00 +0000266
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000267 SkColor color = genColor(&rand);
268 fPaints[i].setColor(color);
269 fPaints[i].setShader(shader);
270
271 canvas->drawRect(oval, rectPaint);
272 canvas->drawOval(oval, fPaints[i]);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000273
halcanary96fcdcc2015-08-27 07:41:13 -0700274 fPaints[i].setShader(nullptr);
jvanverth@google.comcabd0ed2013-04-18 14:48:35 +0000275
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000276 canvas->restore();
277 }
Jim Van Verthd952a992017-04-20 17:25:26 -0400278
279 // reflected oval
280 for (int i = 0; i < fPaints.count(); ++i) {
281 SkRect oval = SkRect::MakeLTRB(-30, -30, 30, 30);
282 canvas->save();
283 // position the oval, and make it at off-integer coords.
284 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
285 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
286 SK_ScalarHalf * kYStep);
287 canvas->rotate(90);
288 canvas->scale(1, -1);
289 canvas->scale(1, 0.66f);
290
291 SkColor color = genColor(&rand);
292 fPaints[i].setColor(color);
293
294 canvas->drawRect(oval, rectPaint);
295 canvas->drawOval(oval, fPaints[i]);
296 canvas->restore();
297 }
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000298 }
299
300private:
301 SkTArray<SkPaint> fPaints;
302 SkTArray<SkMatrix> fMatrices;
303
304 typedef GM INHERITED;
305};
306
307//////////////////////////////////////////////////////////////////////////////
308
Hal Canarye964c182019-01-23 10:22:01 -0500309DEF_GM( return new OvalGM; )
jvanverth@google.com8e2962f2013-04-18 13:59:04 +0000310
311}