blob: 514e43acbf14cb2d6dd3e003a4998ae26ded4b21 [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
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#include "SkRRect.h"
17
18namespace skiagm {
19
robertphillips05302f82015-09-29 11:24:07 -070020static SkColor gen_color(SkRandom* rand) {
21 SkScalar hsv[3];
22 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
23 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
24 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
25
26 return sk_tool_utils::color_to_565(SkHSVToColor(hsv));
27}
28
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000029class RoundRectGM : public GM {
30public:
31 RoundRectGM() {
32 this->setBGColor(0xFF000000);
33 this->makePaints();
34 this->makeMatrices();
35 }
36
37protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000038
mtklein36352bf2015-03-25 18:17:31 -070039 SkString onShortName() override {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000040 return SkString("roundrects");
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070044 return SkISize::Make(1200, 900);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +000045 }
46
47 void makePaints() {
48 {
49 // no AA
50 SkPaint p;
51 fPaints.push_back(p);
52 }
53
54 {
55 // AA
56 SkPaint p;
57 p.setAntiAlias(true);
58 fPaints.push_back(p);
59 }
60
61 {
62 // AA with stroke style
63 SkPaint p;
64 p.setAntiAlias(true);
65 p.setStyle(SkPaint::kStroke_Style);
66 p.setStrokeWidth(SkIntToScalar(5));
67 fPaints.push_back(p);
68 }
69
70 {
71 // AA with stroke style, width = 0
72 SkPaint p;
73 p.setAntiAlias(true);
74 p.setStyle(SkPaint::kStroke_Style);
75 fPaints.push_back(p);
76 }
77
78 {
79 // AA with stroke and fill style
80 SkPaint p;
81 p.setAntiAlias(true);
82 p.setStyle(SkPaint::kStrokeAndFill_Style);
83 p.setStrokeWidth(SkIntToScalar(3));
84 fPaints.push_back(p);
85 }
86 }
87
88 void makeMatrices() {
89 {
90 SkMatrix m;
91 m.setIdentity();
92 fMatrices.push_back(m);
93 }
94
95 {
96 SkMatrix m;
97 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
98 fMatrices.push_back(m);
99 }
100
101 {
102 SkMatrix m;
103 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
104 fMatrices.push_back(m);
105 }
106
107 {
108 SkMatrix m;
109 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
110 fMatrices.push_back(m);
111 }
112
113 {
114 SkMatrix m;
115 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
116 fMatrices.push_back(m);
117 }
118
119 {
120 SkMatrix m;
121 m.setRotate(SkIntToScalar(90));
122 fMatrices.push_back(m);
123 }
124
125 {
126 SkMatrix m;
127 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
128 fMatrices.push_back(m);
129 }
130
131 {
132 SkMatrix m;
133 m.setRotate(SkIntToScalar(60));
134 fMatrices.push_back(m);
135 }
136 }
137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000139 SkRandom rand(1);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000140 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
robertphillips05302f82015-09-29 11:24:07 -0700141 const SkRect rect = SkRect::MakeLTRB(-20, -30, 20, 30);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000142 SkRRect circleRect;
143 circleRect.setRectXY(rect, 5, 5);
144
145 const SkScalar kXStart = 60.0f;
146 const SkScalar kYStart = 80.0f;
147 const int kXStep = 150;
148 const int kYStep = 160;
149 int maxX = fMatrices.count();
150
151 SkPaint rectPaint;
152 rectPaint.setAntiAlias(true);
153 rectPaint.setStyle(SkPaint::kStroke_Style);
154 rectPaint.setStrokeWidth(SkIntToScalar(0));
caryclarkf597c422015-07-28 10:37:53 -0700155 rectPaint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000156
157 int testCount = 0;
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000158 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000159 for (int j = 0; j < fMatrices.count(); ++j) {
160 canvas->save();
161 SkMatrix mat = fMatrices[j];
162 // position the roundrect, and make it at off-integer coords.
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000163 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000164 SK_Scalar1 / 4,
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000165 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000166 3 * SK_Scalar1 / 4);
167 canvas->concat(mat);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000168
robertphillips05302f82015-09-29 11:24:07 -0700169 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000170 fPaints[i].setColor(color);
171
172 canvas->drawRect(rect, rectPaint);
173 canvas->drawRRect(circleRect, fPaints[i]);
174
175 canvas->restore();
176
177 ++testCount;
178 }
179 }
180
181 // special cases
182
183 // non-scaled tall and skinny roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000184 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000185 SkRect rect = SkRect::MakeLTRB(-20, -60, 20, 60);
186 SkRRect ellipseRect;
187 ellipseRect.setRectXY(rect, 5, 10);
188
189 canvas->save();
190 // position the roundrect, 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.come36a1682013-04-23 07:01:29 +0000193
robertphillips05302f82015-09-29 11:24:07 -0700194 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000195 fPaints[i].setColor(color);
196
197 canvas->drawRect(rect, rectPaint);
198 canvas->drawRRect(ellipseRect, fPaints[i]);
199 canvas->restore();
200 }
201
202 // non-scaled wide and short roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000203 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000204 SkRect rect = SkRect::MakeLTRB(-80, -30, 80, 30);
205 SkRRect ellipseRect;
206 ellipseRect.setRectXY(rect, 20, 5);
207
208 canvas->save();
209 // position the roundrect, 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.come36a1682013-04-23 07:01:29 +0000213
robertphillips05302f82015-09-29 11:24:07 -0700214 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000215 fPaints[i].setColor(color);
216
217 canvas->drawRect(rect, rectPaint);
218 canvas->drawRRect(ellipseRect, fPaints[i]);
219 canvas->restore();
220 }
221
222 // super skinny roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000223 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000224 SkRect rect = SkRect::MakeLTRB(0, -60, 1, 60);
225 SkRRect circleRect;
226 circleRect.setRectXY(rect, 5, 5);
227
228 canvas->save();
229 // position the roundrect, and make it at off-integer coords.
230 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
231 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000232
robertphillips05302f82015-09-29 11:24:07 -0700233 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000234 fPaints[i].setColor(color);
235
236 canvas->drawRRect(circleRect, fPaints[i]);
237 canvas->restore();
238 }
239
240 // super short roundrect
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000241 for (int i = 0; i < fPaints.count(); ++i) {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000242 SkRect rect = SkRect::MakeLTRB(-80, -1, 80, 0);
243 SkRRect circleRect;
244 circleRect.setRectXY(rect, 5, 5);
245
246 canvas->save();
247 // position the roundrect, and make it at off-integer coords.
248 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
249 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
250 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000251
robertphillips05302f82015-09-29 11:24:07 -0700252 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000253 fPaints[i].setColor(color);
254
255 canvas->drawRRect(circleRect, fPaints[i]);
256 canvas->restore();
257 }
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000258
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000259 // radial gradient
260 SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
261 SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
262 SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
reed1a9b9642016-03-13 14:13:58 -0700263 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
264 SkShader::kClamp_TileMode);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000265
266 for (int i = 0; i < fPaints.count(); ++i) {
267 canvas->save();
268 // position the path, and make it at off-integer coords.
269 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
270 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
271 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000272
robertphillips05302f82015-09-29 11:24:07 -0700273 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000274 fPaints[i].setColor(color);
275 fPaints[i].setShader(shader);
276
277 canvas->drawRect(rect, rectPaint);
278 canvas->drawRRect(circleRect, fPaints[i]);
279
halcanary96fcdcc2015-08-27 07:41:13 -0700280 fPaints[i].setShader(nullptr);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000281
282 canvas->restore();
283 }
284
285 // strokes and radii
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000286 {
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000287 SkScalar radii[][2] = {
288 {10,10},
289 {5,15},
290 {5,15},
291 {5,15}
292 };
293
294 SkScalar strokeWidths[] = {
295 20, 10, 20, 40
296 };
297
298 for (int i = 0; i < 4; ++i) {
299 SkRRect circleRect;
300 circleRect.setRectXY(rect, radii[i][0], radii[i][1]);
301
302 canvas->save();
303 // position the roundrect, and make it at off-integer coords.
304 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
305 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
306 SK_ScalarHalf * kYStep);
skia.committer@gmail.come36a1682013-04-23 07:01:29 +0000307
robertphillips05302f82015-09-29 11:24:07 -0700308 SkColor color = gen_color(&rand);
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000309
310 SkPaint p;
311 p.setAntiAlias(true);
312 p.setStyle(SkPaint::kStroke_Style);
313 p.setStrokeWidth(strokeWidths[i]);
314 p.setColor(color);
315
316 canvas->drawRRect(circleRect, p);
317 canvas->restore();
318 }
319 }
320
halcanary6950de62015-11-07 05:29:00 -0800321 // test old entry point ( https://bug.skia.org/3786 )
robertphillips05302f82015-09-29 11:24:07 -0700322 {
323 canvas->save();
324
325 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
326 kYStart + SK_Scalar1 * kYStep * 4 + SK_Scalar1 / 4 +
327 SK_ScalarHalf * kYStep);
328
329 const SkColor color = gen_color(&rand);
330
331 SkPaint p;
332 p.setColor(color);
333
334 const SkRect oooRect = { 20, 30, -20, -30 }; // intentionally out of order
335 canvas->drawRoundRect(oooRect, 10, 10, p);
336
337 canvas->restore();
338 }
commit-bot@chromium.orga534b842013-04-22 18:05:19 +0000339 }
340
341private:
342 SkTArray<SkPaint> fPaints;
343 SkTArray<SkMatrix> fMatrices;
344
345 typedef GM INHERITED;
346};
347
348//////////////////////////////////////////////////////////////////////////////
349
350static GM* MyFactory(void*) { return new RoundRectGM; }
351static GMRegistry reg(MyFactory);
352
353}