blob: 660e72a64732e1d950ba90b0d2df1b6a3d691d1e [file] [log] [blame]
mike@reedtribe.org37071642012-12-17 02:10:42 +00001/*
2 * Copyright 2012 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 "SkCanvas.h"
10#include "SkRRect.h"
11#include "SkPath.h"
12
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000013typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*);
14
15static void inset0(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
16 SkRect r = src.rect();
17
18 r.inset(dx, dy);
19 if (r.isEmpty()) {
20 dst->setEmpty();
21 return;
22 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000023
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000024 SkVector radii[4];
25 for (int i = 0; i < 4; ++i) {
26 radii[i] = src.radii((SkRRect::Corner)i);
27 }
28 for (int i = 0; i < 4; ++i) {
29 radii[i].fX -= dx;
30 radii[i].fY -= dy;
31 }
32 dst->setRectRadii(r, radii);
33}
34
35static void inset1(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
36 SkRect r = src.rect();
37
38 r.inset(dx, dy);
39 if (r.isEmpty()) {
40 dst->setEmpty();
41 return;
42 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000043
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000044 SkVector radii[4];
45 for (int i = 0; i < 4; ++i) {
46 radii[i] = src.radii((SkRRect::Corner)i);
47 }
48 dst->setRectRadii(r, radii);
49}
50
reed@google.comec93bf92012-12-17 13:39:25 +000051static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
52 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000053
reed@google.comec93bf92012-12-17 13:39:25 +000054 r.inset(dx, dy);
55 if (r.isEmpty()) {
56 dst->setEmpty();
57 return;
58 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000059
reed@google.comec93bf92012-12-17 13:39:25 +000060 SkVector radii[4];
61 for (int i = 0; i < 4; ++i) {
62 radii[i] = src.radii((SkRRect::Corner)i);
63 }
64 for (int i = 0; i < 4; ++i) {
65 if (radii[i].fX) {
66 radii[i].fX -= dx;
67 }
68 if (radii[i].fY) {
69 radii[i].fY -= dy;
70 }
71 }
72 dst->setRectRadii(r, radii);
73}
74
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000075static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) {
reed@google.comec93bf92012-12-17 13:39:25 +000076 return newSize * radius / oldSize;
77}
78
79static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
80 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000081
reed@google.comec93bf92012-12-17 13:39:25 +000082 r.inset(dx, dy);
83 if (r.isEmpty()) {
84 dst->setEmpty();
85 return;
86 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000087
reed@google.comec93bf92012-12-17 13:39:25 +000088 SkVector radii[4];
89 for (int i = 0; i < 4; ++i) {
90 radii[i] = src.radii((SkRRect::Corner)i);
91 }
92 for (int i = 0; i < 4; ++i) {
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000093 radii[i].fX = prop(radii[i].fX, r.width(), src.rect().width());
94 radii[i].fY = prop(radii[i].fY, r.height(), src.rect().height());
reed@google.comec93bf92012-12-17 13:39:25 +000095 }
96 dst->setRectRadii(r, radii);
97}
98
mike@reedtribe.org37071642012-12-17 02:10:42 +000099static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
100 SkPaint paint;
101 paint.setAntiAlias(true);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000102 paint.setStyle(SkPaint::kStroke_Style);
103
mike@reedtribe.org37071642012-12-17 02:10:42 +0000104 if (rrect.isRect()) {
105 paint.setColor(SK_ColorRED);
106 } else if (rrect.isOval()) {
caryclark12596012015-07-29 05:27:47 -0700107 paint.setColor(sk_tool_utils::color_to_565(0xFF008800));
mike@reedtribe.org37071642012-12-17 02:10:42 +0000108 } else if (rrect.isSimple()) {
109 paint.setColor(SK_ColorBLUE);
110 } else {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000111 paint.setColor(SK_ColorBLACK);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000112 }
113 canvas->drawRRect(rrect, paint);
114}
115
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000116static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
117 SkRRect rr;
mike@reedtribe.org51386382012-12-17 03:48:12 +0000118 for (SkScalar d = -30; d <= 30; d += 5) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000119 proc(rrect, d, d, &rr);
120 draw_rrect_color(canvas, rr);
121 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000122}
123
124class RRectGM : public skiagm::GM {
125public:
126 RRectGM() {}
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000127
mike@reedtribe.org37071642012-12-17 02:10:42 +0000128protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000129
mtklein36352bf2015-03-25 18:17:31 -0700130 SkString onShortName() override {
mike@reedtribe.org37071642012-12-17 02:10:42 +0000131 return SkString("rrect");
132 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000133
mtklein36352bf2015-03-25 18:17:31 -0700134 SkISize onISize() override {
reed@google.comec93bf92012-12-17 13:39:25 +0000135 return SkISize::Make(820, 710);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000136 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700139 constexpr InsetProc insetProcs[] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000140 inset0, inset1, inset2, inset3
141 };
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000142
mike@reedtribe.org37071642012-12-17 02:10:42 +0000143 SkRRect rrect[4];
reed@google.comec93bf92012-12-17 13:39:25 +0000144 SkRect r = { 0, 0, 120, 100 };
mike@reedtribe.org37071642012-12-17 02:10:42 +0000145 SkVector radii[4] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000146 { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000147 };
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000148
mike@reedtribe.org37071642012-12-17 02:10:42 +0000149 rrect[0].setRect(r);
150 rrect[1].setOval(r);
151 rrect[2].setRectXY(r, 20, 20);
152 rrect[3].setRectRadii(r, radii);
153
reed@google.comec93bf92012-12-17 13:39:25 +0000154 canvas->translate(50.5f, 50.5f);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000155 for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) {
156 canvas->save();
157 for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
158 drawrr(canvas, rrect[i], insetProcs[j]);
reed@google.comec93bf92012-12-17 13:39:25 +0000159 canvas->translate(200, 0);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000160 }
161 canvas->restore();
reed@google.comec93bf92012-12-17 13:39:25 +0000162 canvas->translate(0, 170);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000163 }
164 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000165
mike@reedtribe.org37071642012-12-17 02:10:42 +0000166private:
167 typedef GM INHERITED;
168};
169
170DEF_GM( return new RRectGM; )