blob: 63dedb587b940305da18357c36613b36632e738b [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"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050023#include "include/private/base/SkTArray.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050024#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/ToolUtils.h"
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000026
Herb Derbyec96c212023-03-06 10:31:22 -050027using namespace skia_private;
28
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000029namespace skiagm {
30
robertphillips05302f82015-09-29 11:24:07 -070031static SkColor gen_color(SkRandom* rand) {
32 SkScalar hsv[3];
33 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
34 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
35 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
36
Mike Kleinea3f0142019-03-20 11:12:10 -050037 return ToolUtils::color_to_565(SkHSVToColor(hsv));
robertphillips05302f82015-09-29 11:24:07 -070038}
39
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000040class RoundRectGM : public GM {
41public:
42 RoundRectGM() {
43 this->setBGColor(0xFF000000);
44 this->makePaints();
45 this->makeMatrices();
46 }
47
48protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000049 SkString getName() const override { return SkString("roundrects"); }
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000050
Leandro Lovisolo8f023882023-08-15 21:13:52 +000051 SkISize getISize() override { return SkISize::Make(1200, 900); }
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000052
53 void makePaints() {
54 {
jvanverth250d00b2016-08-25 05:53:00 -070055 // no AA
56 SkPaint p;
57 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000058 }
59
60 {
jvanverth250d00b2016-08-25 05:53:00 -070061 // AA
62 SkPaint p;
63 p.setAntiAlias(true);
64 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000065 }
66
67 {
jvanverth250d00b2016-08-25 05:53:00 -070068 // AA with stroke style
69 SkPaint p;
70 p.setAntiAlias(true);
71 p.setStyle(SkPaint::kStroke_Style);
72 p.setStrokeWidth(SkIntToScalar(5));
73 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000074 }
75
76 {
jvanverth250d00b2016-08-25 05:53:00 -070077 // AA with stroke style, width = 0
78 SkPaint p;
79 p.setAntiAlias(true);
80 p.setStyle(SkPaint::kStroke_Style);
81 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000082 }
83
84 {
jvanverth250d00b2016-08-25 05:53:00 -070085 // AA with stroke and fill style
86 SkPaint p;
87 p.setAntiAlias(true);
88 p.setStyle(SkPaint::kStrokeAndFill_Style);
89 p.setStrokeWidth(SkIntToScalar(3));
90 fPaints.push_back(p);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000091 }
92 }
93
94 void makeMatrices() {
95 {
jvanverth250d00b2016-08-25 05:53:00 -070096 SkMatrix m;
97 m.setIdentity();
98 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000099 }
100
101 {
jvanverth250d00b2016-08-25 05:53:00 -0700102 SkMatrix m;
103 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
104 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000105 }
106
107 {
jvanverth250d00b2016-08-25 05:53:00 -0700108 SkMatrix m;
109 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
110 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000111 }
112
113 {
jvanverth250d00b2016-08-25 05:53:00 -0700114 SkMatrix m;
115 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
116 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000117 }
118
119 {
jvanverth250d00b2016-08-25 05:53:00 -0700120 SkMatrix m;
121 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
122 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000123 }
124
125 {
jvanverth250d00b2016-08-25 05:53:00 -0700126 SkMatrix m;
127 m.setRotate(SkIntToScalar(90));
128 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000129 }
130
131 {
jvanverth250d00b2016-08-25 05:53:00 -0700132 SkMatrix m;
133 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
134 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000135 }
136
137 {
jvanverth250d00b2016-08-25 05:53:00 -0700138 SkMatrix m;
139 m.setRotate(SkIntToScalar(60));
140 fMatrices.push_back(m);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000141 }
142 }
143
mtklein36352bf2015-03-25 18:17:31 -0700144 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000145 SkRandom rand(1);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000146 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
John Stilesacf71642021-08-12 22:33:57 -0400147 const SkRect kRect = SkRect::MakeLTRB(-20, -30, 20, 30);
148 SkRRect circleRRect;
149 circleRRect.setRectXY(kRect, 5, 5);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000150
151 const SkScalar kXStart = 60.0f;
152 const SkScalar kYStart = 80.0f;
153 const int kXStep = 150;
154 const int kYStep = 160;
Herb Derbyffacce52022-11-09 10:51:34 -0500155 int maxX = fMatrices.size();
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000156
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);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000162
163 int testCount = 0;
Herb Derbyffacce52022-11-09 10:51:34 -0500164 for (int i = 0; i < fPaints.size(); ++i) {
165 for (int j = 0; j < fMatrices.size(); ++j) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000166 canvas->save();
167 SkMatrix mat = fMatrices[j];
168 // position the roundrect, and make it at off-integer coords.
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000169 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000170 SK_Scalar1 / 4,
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000171 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000172 3 * SK_Scalar1 / 4);
173 canvas->concat(mat);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000174
robertphillips05302f82015-09-29 11:24:07 -0700175 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000176 fPaints[i].setColor(color);
177
John Stilesacf71642021-08-12 22:33:57 -0400178 canvas->drawRect(kRect, rectPaint);
179 canvas->drawRRect(circleRRect, fPaints[i]);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000180
181 canvas->restore();
182
183 ++testCount;
184 }
185 }
186
187 // special cases
188
189 // non-scaled tall and skinny roundrect
Herb Derbyffacce52022-11-09 10:51:34 -0500190 for (int i = 0; i < fPaints.size(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000191 SkRect rect = SkRect::MakeLTRB(-20, -60, 20, 60);
192 SkRRect ellipseRect;
193 ellipseRect.setRectXY(rect, 5, 10);
194
195 canvas->save();
196 // position the roundrect, and make it at off-integer coords.
197 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
198 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000199
robertphillips05302f82015-09-29 11:24:07 -0700200 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000201 fPaints[i].setColor(color);
202
203 canvas->drawRect(rect, rectPaint);
204 canvas->drawRRect(ellipseRect, fPaints[i]);
205 canvas->restore();
206 }
207
208 // non-scaled wide and short roundrect
Herb Derbyffacce52022-11-09 10:51:34 -0500209 for (int i = 0; i < fPaints.size(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000210 SkRect rect = SkRect::MakeLTRB(-80, -30, 80, 30);
211 SkRRect ellipseRect;
212 ellipseRect.setRectXY(rect, 20, 5);
213
214 canvas->save();
215 // position the roundrect, and make it at off-integer coords.
216 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
217 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
218 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000219
robertphillips05302f82015-09-29 11:24:07 -0700220 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000221 fPaints[i].setColor(color);
222
223 canvas->drawRect(rect, rectPaint);
224 canvas->drawRRect(ellipseRect, fPaints[i]);
225 canvas->restore();
226 }
227
228 // super skinny roundrect
Herb Derbyffacce52022-11-09 10:51:34 -0500229 for (int i = 0; i < fPaints.size(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000230 SkRect rect = SkRect::MakeLTRB(0, -60, 1, 60);
231 SkRRect circleRect;
232 circleRect.setRectXY(rect, 5, 5);
233
234 canvas->save();
235 // position the roundrect, and make it at off-integer coords.
236 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
237 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000238
robertphillips05302f82015-09-29 11:24:07 -0700239 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000240 fPaints[i].setColor(color);
241
242 canvas->drawRRect(circleRect, fPaints[i]);
243 canvas->restore();
244 }
245
246 // super short roundrect
Herb Derbyffacce52022-11-09 10:51:34 -0500247 for (int i = 0; i < fPaints.size(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000248 SkRect rect = SkRect::MakeLTRB(-80, -1, 80, 0);
249 SkRRect circleRect;
250 circleRect.setRectXY(rect, 5, 5);
251
252 canvas->save();
253 // position the roundrect, and make it at off-integer coords.
254 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
255 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
256 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000257
robertphillips05302f82015-09-29 11:24:07 -0700258 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000259 fPaints[i].setColor(color);
260
261 canvas->drawRRect(circleRect, fPaints[i]);
262 canvas->restore();
263 }
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000264
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000265 // radial gradient
266 SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
267 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
268 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
Herb Derbyc37b3862022-06-21 09:49:17 -0400269 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400270 SkTileMode::kClamp);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000271
Herb Derbyffacce52022-11-09 10:51:34 -0500272 for (int i = 0; i < fPaints.size(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000273 canvas->save();
274 // position the path, and make it at off-integer coords.
275 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
276 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
277 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000278
robertphillips05302f82015-09-29 11:24:07 -0700279 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000280 fPaints[i].setColor(color);
281 fPaints[i].setShader(shader);
282
John Stilesacf71642021-08-12 22:33:57 -0400283 canvas->drawRect(kRect, rectPaint);
284 canvas->drawRRect(circleRRect, fPaints[i]);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000285
halcanary96fcdcc2015-08-27 07:41:13 -0700286 fPaints[i].setShader(nullptr);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000287
288 canvas->restore();
289 }
290
291 // strokes and radii
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000292 {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000293 SkScalar radii[][2] = {
294 {10,10},
295 {5,15},
296 {5,15},
297 {5,15}
298 };
299
300 SkScalar strokeWidths[] = {
301 20, 10, 20, 40
302 };
303
304 for (int i = 0; i < 4; ++i) {
305 SkRRect circleRect;
John Stilesacf71642021-08-12 22:33:57 -0400306 circleRect.setRectXY(kRect, radii[i][0], radii[i][1]);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000307
308 canvas->save();
309 // position the roundrect, and make it at off-integer coords.
310 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
311 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
312 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000313
robertphillips05302f82015-09-29 11:24:07 -0700314 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000315
316 SkPaint p;
317 p.setAntiAlias(true);
318 p.setStyle(SkPaint::kStroke_Style);
319 p.setStrokeWidth(strokeWidths[i]);
320 p.setColor(color);
321
322 canvas->drawRRect(circleRect, p);
323 canvas->restore();
324 }
325 }
326
halcanary6950de62015-11-07 05:29:00 -0800327 // test old entry point ( https://bug.skia.org/3786 )
robertphillips05302f82015-09-29 11:24:07 -0700328 {
329 canvas->save();
330
331 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
332 kYStart + SK_Scalar1 * kYStep * 4 + SK_Scalar1 / 4 +
333 SK_ScalarHalf * kYStep);
334
335 const SkColor color = gen_color(&rand);
336
337 SkPaint p;
338 p.setColor(color);
339
340 const SkRect oooRect = { 20, 30, -20, -30 }; // intentionally out of order
341 canvas->drawRoundRect(oooRect, 10, 10, p);
342
343 canvas->restore();
344 }
jvanverth250d00b2016-08-25 05:53:00 -0700345
346 // rrect with stroke > radius/2
347 {
348 SkRect smallRect = { -30, -20, 30, 20 };
349 SkRRect circleRect;
350 circleRect.setRectXY(smallRect, 5, 5);
351
352 canvas->save();
353 // position the roundrect, and make it at off-integer coords.
354 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
355 kYStart - SK_Scalar1 * kYStep + 73 * SK_Scalar1 / 4 +
356 SK_ScalarHalf * kYStep);
357
358 SkColor color = gen_color(&rand);
359
360 SkPaint p;
361 p.setAntiAlias(true);
362 p.setStyle(SkPaint::kStroke_Style);
363 p.setStrokeWidth(25);
364 p.setColor(color);
365
366 canvas->drawRRect(circleRect, p);
367 canvas->restore();
368 }
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000369 }
370
371private:
Herb Derbyec96c212023-03-06 10:31:22 -0500372 TArray<SkPaint> fPaints;
373 TArray<SkMatrix> fMatrices;
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000374
John Stiles7571f9e2020-09-02 22:42:33 -0400375 using INHERITED = GM;
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000376};
377
378//////////////////////////////////////////////////////////////////////////////
379
Hal Canarye964c182019-01-23 10:22:01 -0500380DEF_GM( return new RoundRectGM; )
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000381
John Stilesa6841be2020-08-06 14:11:56 -0400382} // namespace skiagm