blob: 4f4a8cb847c759599aa0e0bd58c75d36879f13e2 [file] [log] [blame]
commit-bot@chromium.orga534b842013-04-22 18:05:19 +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/SkRRect.h"
15#include "include/core/SkRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkScalar.h"
17#include "include/core/SkShader.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTileMode.h"
21#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkGradientShader.h"
23#include "include/private/SkTArray.h"
24#include "include/utils/SkRandom.h"
25#include "tools/ToolUtils.h"
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000026
27namespace skiagm {
28
robertphillips05302f82015-09-29 11:24:07 -070029static SkColor gen_color(SkRandom* rand) {
30 SkScalar hsv[3];
31 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
32 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
33 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
34
Mike Kleinea3f0142019-03-20 11:12:10 -050035 return ToolUtils::color_to_565(SkHSVToColor(hsv));
robertphillips05302f82015-09-29 11:24:07 -070036}
37
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000038class RoundRectGM : public GM {
39public:
40 RoundRectGM() {
41 this->setBGColor(0xFF000000);
42 this->makePaints();
43 this->makeMatrices();
44 }
45
46protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000047
mtklein36352bf2015-03-25 18:17:31 -070048 SkString onShortName() override {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000049 return SkString("roundrects");
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070053 return SkISize::Make(1200, 900);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000054 }
55
56 void makePaints() {
57 {
jvanverth250d00b2016-08-25 05:53:00 -070058 // no AA
59 SkPaint p;
60 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000061 }
62
63 {
jvanverth250d00b2016-08-25 05:53:00 -070064 // AA
65 SkPaint p;
66 p.setAntiAlias(true);
67 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000068 }
69
70 {
jvanverth250d00b2016-08-25 05:53:00 -070071 // AA with stroke style
72 SkPaint p;
73 p.setAntiAlias(true);
74 p.setStyle(SkPaint::kStroke_Style);
75 p.setStrokeWidth(SkIntToScalar(5));
76 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000077 }
78
79 {
jvanverth250d00b2016-08-25 05:53:00 -070080 // AA with stroke style, width = 0
81 SkPaint p;
82 p.setAntiAlias(true);
83 p.setStyle(SkPaint::kStroke_Style);
84 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000085 }
86
87 {
jvanverth250d00b2016-08-25 05:53:00 -070088 // AA with stroke and fill style
89 SkPaint p;
90 p.setAntiAlias(true);
91 p.setStyle(SkPaint::kStrokeAndFill_Style);
92 p.setStrokeWidth(SkIntToScalar(3));
93 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000094 }
95 }
96
97 void makeMatrices() {
98 {
jvanverth250d00b2016-08-25 05:53:00 -070099 SkMatrix m;
100 m.setIdentity();
101 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000102 }
103
104 {
jvanverth250d00b2016-08-25 05:53:00 -0700105 SkMatrix m;
106 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
107 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000108 }
109
110 {
jvanverth250d00b2016-08-25 05:53:00 -0700111 SkMatrix m;
112 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
113 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000114 }
115
116 {
jvanverth250d00b2016-08-25 05:53:00 -0700117 SkMatrix m;
118 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
119 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000120 }
121
122 {
jvanverth250d00b2016-08-25 05:53:00 -0700123 SkMatrix m;
124 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
125 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000126 }
127
128 {
jvanverth250d00b2016-08-25 05:53:00 -0700129 SkMatrix m;
130 m.setRotate(SkIntToScalar(90));
131 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000132 }
133
134 {
jvanverth250d00b2016-08-25 05:53:00 -0700135 SkMatrix m;
136 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
137 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000138 }
139
140 {
jvanverth250d00b2016-08-25 05:53:00 -0700141 SkMatrix m;
142 m.setRotate(SkIntToScalar(60));
143 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000144 }
145 }
146
mtklein36352bf2015-03-25 18:17:31 -0700147 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000148 SkRandom rand(1);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000149 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
robertphillips05302f82015-09-29 11:24:07 -0700150 const SkRect rect = SkRect::MakeLTRB(-20, -30, 20, 30);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000151 SkRRect circleRect;
152 circleRect.setRectXY(rect, 5, 5);
153
154 const SkScalar kXStart = 60.0f;
155 const SkScalar kYStart = 80.0f;
156 const int kXStep = 150;
157 const int kYStep = 160;
158 int maxX = fMatrices.count();
159
160 SkPaint rectPaint;
161 rectPaint.setAntiAlias(true);
162 rectPaint.setStyle(SkPaint::kStroke_Style);
163 rectPaint.setStrokeWidth(SkIntToScalar(0));
Mike Kleind46dce32018-08-16 10:17:03 -0400164 rectPaint.setColor(SK_ColorLTGRAY);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000165
166 int testCount = 0;
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000167 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000168 for (int j = 0; j < fMatrices.count(); ++j) {
169 canvas->save();
170 SkMatrix mat = fMatrices[j];
171 // position the roundrect, and make it at off-integer coords.
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000172 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000173 SK_Scalar1 / 4,
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000174 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000175 3 * SK_Scalar1 / 4);
176 canvas->concat(mat);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000177
robertphillips05302f82015-09-29 11:24:07 -0700178 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000179 fPaints[i].setColor(color);
180
181 canvas->drawRect(rect, rectPaint);
182 canvas->drawRRect(circleRect, fPaints[i]);
183
184 canvas->restore();
185
186 ++testCount;
187 }
188 }
189
190 // special cases
191
192 // non-scaled tall and skinny roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000193 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000194 SkRect rect = SkRect::MakeLTRB(-20, -60, 20, 60);
195 SkRRect ellipseRect;
196 ellipseRect.setRectXY(rect, 5, 10);
197
198 canvas->save();
199 // position the roundrect, and make it at off-integer coords.
200 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
201 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000202
robertphillips05302f82015-09-29 11:24:07 -0700203 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000204 fPaints[i].setColor(color);
205
206 canvas->drawRect(rect, rectPaint);
207 canvas->drawRRect(ellipseRect, fPaints[i]);
208 canvas->restore();
209 }
210
211 // non-scaled wide and short roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000212 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000213 SkRect rect = SkRect::MakeLTRB(-80, -30, 80, 30);
214 SkRRect ellipseRect;
215 ellipseRect.setRectXY(rect, 20, 5);
216
217 canvas->save();
218 // position the roundrect, and make it at off-integer coords.
219 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
220 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
221 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000222
robertphillips05302f82015-09-29 11:24:07 -0700223 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000224 fPaints[i].setColor(color);
225
226 canvas->drawRect(rect, rectPaint);
227 canvas->drawRRect(ellipseRect, fPaints[i]);
228 canvas->restore();
229 }
230
231 // super skinny roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000232 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000233 SkRect rect = SkRect::MakeLTRB(0, -60, 1, 60);
234 SkRRect circleRect;
235 circleRect.setRectXY(rect, 5, 5);
236
237 canvas->save();
238 // position the roundrect, and make it at off-integer coords.
239 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
240 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000241
robertphillips05302f82015-09-29 11:24:07 -0700242 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000243 fPaints[i].setColor(color);
244
245 canvas->drawRRect(circleRect, fPaints[i]);
246 canvas->restore();
247 }
248
249 // super short roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000250 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000251 SkRect rect = SkRect::MakeLTRB(-80, -1, 80, 0);
252 SkRRect circleRect;
253 circleRect.setRectXY(rect, 5, 5);
254
255 canvas->save();
256 // position the roundrect, and make it at off-integer coords.
257 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
258 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
259 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000260
robertphillips05302f82015-09-29 11:24:07 -0700261 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000262 fPaints[i].setColor(color);
263
264 canvas->drawRRect(circleRect, fPaints[i]);
265 canvas->restore();
266 }
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000267
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000268 // radial gradient
269 SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
270 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
271 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700272 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400273 SkTileMode::kClamp);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000274
275 for (int i = 0; i < fPaints.count(); ++i) {
276 canvas->save();
277 // position the path, and make it at off-integer coords.
278 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
279 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
280 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000281
robertphillips05302f82015-09-29 11:24:07 -0700282 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000283 fPaints[i].setColor(color);
284 fPaints[i].setShader(shader);
285
286 canvas->drawRect(rect, rectPaint);
287 canvas->drawRRect(circleRect, fPaints[i]);
288
halcanary96fcdcc2015-08-27 07:41:13 -0700289 fPaints[i].setShader(nullptr);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000290
291 canvas->restore();
292 }
293
294 // strokes and radii
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000295 {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000296 SkScalar radii[][2] = {
297 {10,10},
298 {5,15},
299 {5,15},
300 {5,15}
301 };
302
303 SkScalar strokeWidths[] = {
304 20, 10, 20, 40
305 };
306
307 for (int i = 0; i < 4; ++i) {
308 SkRRect circleRect;
309 circleRect.setRectXY(rect, radii[i][0], radii[i][1]);
310
311 canvas->save();
312 // position the roundrect, and make it at off-integer coords.
313 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
314 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
315 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000316
robertphillips05302f82015-09-29 11:24:07 -0700317 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000318
319 SkPaint p;
320 p.setAntiAlias(true);
321 p.setStyle(SkPaint::kStroke_Style);
322 p.setStrokeWidth(strokeWidths[i]);
323 p.setColor(color);
324
325 canvas->drawRRect(circleRect, p);
326 canvas->restore();
327 }
328 }
329
halcanary6950de62015-11-07 05:29:00 -0800330 // test old entry point ( https://bug.skia.org/3786 )
robertphillips05302f82015-09-29 11:24:07 -0700331 {
332 canvas->save();
333
334 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
335 kYStart + SK_Scalar1 * kYStep * 4 + SK_Scalar1 / 4 +
336 SK_ScalarHalf * kYStep);
337
338 const SkColor color = gen_color(&rand);
339
340 SkPaint p;
341 p.setColor(color);
342
343 const SkRect oooRect = { 20, 30, -20, -30 }; // intentionally out of order
344 canvas->drawRoundRect(oooRect, 10, 10, p);
345
346 canvas->restore();
347 }
jvanverth250d00b2016-08-25 05:53:00 -0700348
349 // rrect with stroke > radius/2
350 {
351 SkRect smallRect = { -30, -20, 30, 20 };
352 SkRRect circleRect;
353 circleRect.setRectXY(smallRect, 5, 5);
354
355 canvas->save();
356 // position the roundrect, and make it at off-integer coords.
357 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
358 kYStart - SK_Scalar1 * kYStep + 73 * SK_Scalar1 / 4 +
359 SK_ScalarHalf * kYStep);
360
361 SkColor color = gen_color(&rand);
362
363 SkPaint p;
364 p.setAntiAlias(true);
365 p.setStyle(SkPaint::kStroke_Style);
366 p.setStrokeWidth(25);
367 p.setColor(color);
368
369 canvas->drawRRect(circleRect, p);
370 canvas->restore();
371 }
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000372 }
373
374private:
375 SkTArray<SkPaint> fPaints;
376 SkTArray<SkMatrix> fMatrices;
377
378 typedef GM INHERITED;
379};
380
381//////////////////////////////////////////////////////////////////////////////
382
Hal Canarye964c182019-01-23 10:22:01 -0500383DEF_GM( return new RoundRectGM; )
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000384
John Stilesa6841be2020-08-06 14:11:56 -0400385} // namespace skiagm