blob: 275afb4458a0334596ebb1f1b86327bca67db409 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "tools/ToolUtils.h"
mike@reedtribe.org37071642012-12-17 02:10:42 +000020
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000021typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*);
22
23static void inset0(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
24 SkRect r = src.rect();
25
26 r.inset(dx, dy);
27 if (r.isEmpty()) {
28 dst->setEmpty();
29 return;
30 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000031
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000032 SkVector radii[4];
33 for (int i = 0; i < 4; ++i) {
34 radii[i] = src.radii((SkRRect::Corner)i);
35 }
36 for (int i = 0; i < 4; ++i) {
37 radii[i].fX -= dx;
38 radii[i].fY -= dy;
39 }
40 dst->setRectRadii(r, radii);
41}
42
43static void inset1(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
44 SkRect r = src.rect();
45
46 r.inset(dx, dy);
47 if (r.isEmpty()) {
48 dst->setEmpty();
49 return;
50 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000051
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +000052 SkVector radii[4];
53 for (int i = 0; i < 4; ++i) {
54 radii[i] = src.radii((SkRRect::Corner)i);
55 }
56 dst->setRectRadii(r, radii);
57}
58
reed@google.comec93bf92012-12-17 13:39:25 +000059static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
60 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000061
reed@google.comec93bf92012-12-17 13:39:25 +000062 r.inset(dx, dy);
63 if (r.isEmpty()) {
64 dst->setEmpty();
65 return;
66 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000067
reed@google.comec93bf92012-12-17 13:39:25 +000068 SkVector radii[4];
69 for (int i = 0; i < 4; ++i) {
70 radii[i] = src.radii((SkRRect::Corner)i);
71 }
72 for (int i = 0; i < 4; ++i) {
73 if (radii[i].fX) {
74 radii[i].fX -= dx;
75 }
76 if (radii[i].fY) {
77 radii[i].fY -= dy;
78 }
79 }
80 dst->setRectRadii(r, radii);
81}
82
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000083static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) {
reed@google.comec93bf92012-12-17 13:39:25 +000084 return newSize * radius / oldSize;
85}
86
87static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) {
88 SkRect r = src.rect();
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000089
reed@google.comec93bf92012-12-17 13:39:25 +000090 r.inset(dx, dy);
91 if (r.isEmpty()) {
92 dst->setEmpty();
93 return;
94 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +000095
reed@google.comec93bf92012-12-17 13:39:25 +000096 SkVector radii[4];
97 for (int i = 0; i < 4; ++i) {
98 radii[i] = src.radii((SkRRect::Corner)i);
99 }
100 for (int i = 0; i < 4; ++i) {
sugoi@google.com0f0d9b72013-02-27 15:41:12 +0000101 radii[i].fX = prop(radii[i].fX, r.width(), src.rect().width());
102 radii[i].fY = prop(radii[i].fY, r.height(), src.rect().height());
reed@google.comec93bf92012-12-17 13:39:25 +0000103 }
104 dst->setRectRadii(r, radii);
105}
106
mike@reedtribe.org37071642012-12-17 02:10:42 +0000107static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) {
108 SkPaint paint;
109 paint.setAntiAlias(true);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000110 paint.setStyle(SkPaint::kStroke_Style);
111
mike@reedtribe.org37071642012-12-17 02:10:42 +0000112 if (rrect.isRect()) {
113 paint.setColor(SK_ColorRED);
114 } else if (rrect.isOval()) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500115 paint.setColor(ToolUtils::color_to_565(0xFF008800));
mike@reedtribe.org37071642012-12-17 02:10:42 +0000116 } else if (rrect.isSimple()) {
117 paint.setColor(SK_ColorBLUE);
118 } else {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000119 paint.setColor(SK_ColorBLACK);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000120 }
121 canvas->drawRRect(rrect, paint);
122}
123
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000124static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) {
125 SkRRect rr;
mike@reedtribe.org51386382012-12-17 03:48:12 +0000126 for (SkScalar d = -30; d <= 30; d += 5) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000127 proc(rrect, d, d, &rr);
128 draw_rrect_color(canvas, rr);
129 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000130}
131
132class RRectGM : public skiagm::GM {
133public:
134 RRectGM() {}
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000135
mike@reedtribe.org37071642012-12-17 02:10:42 +0000136protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000137
mtklein36352bf2015-03-25 18:17:31 -0700138 SkString onShortName() override {
mike@reedtribe.org37071642012-12-17 02:10:42 +0000139 return SkString("rrect");
140 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000141
mtklein36352bf2015-03-25 18:17:31 -0700142 SkISize onISize() override {
reed@google.comec93bf92012-12-17 13:39:25 +0000143 return SkISize::Make(820, 710);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000144 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000145
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700147 constexpr InsetProc insetProcs[] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000148 inset0, inset1, inset2, inset3
149 };
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000150
mike@reedtribe.org37071642012-12-17 02:10:42 +0000151 SkRRect rrect[4];
reed@google.comec93bf92012-12-17 13:39:25 +0000152 SkRect r = { 0, 0, 120, 100 };
mike@reedtribe.org37071642012-12-17 02:10:42 +0000153 SkVector radii[4] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000154 { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000155 };
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000156
mike@reedtribe.org37071642012-12-17 02:10:42 +0000157 rrect[0].setRect(r);
158 rrect[1].setOval(r);
159 rrect[2].setRectXY(r, 20, 20);
160 rrect[3].setRectRadii(r, radii);
161
reed@google.comec93bf92012-12-17 13:39:25 +0000162 canvas->translate(50.5f, 50.5f);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000163 for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) {
164 canvas->save();
165 for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
166 drawrr(canvas, rrect[i], insetProcs[j]);
reed@google.comec93bf92012-12-17 13:39:25 +0000167 canvas->translate(200, 0);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000168 }
169 canvas->restore();
reed@google.comec93bf92012-12-17 13:39:25 +0000170 canvas->translate(0, 170);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000171 }
172 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000173
mike@reedtribe.org37071642012-12-17 02:10:42 +0000174private:
175 typedef GM INHERITED;
176};
177
178DEF_GM( return new RRectGM; )