blob: 8e26b1b897ba51489bb3f34922b9d2a0e8be4846 [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()) {
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000107 paint.setColor(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 virtual uint32_t onGetFlags() const SK_OVERRIDE {
130 return kSkipTiled_Flag;
131 }
132
mike@reedtribe.org37071642012-12-17 02:10:42 +0000133 virtual SkString onShortName() {
134 return SkString("rrect");
135 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000136
mike@reedtribe.org37071642012-12-17 02:10:42 +0000137 virtual SkISize onISize() {
reed@google.comec93bf92012-12-17 13:39:25 +0000138 return SkISize::Make(820, 710);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000139 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000140
mike@reedtribe.org37071642012-12-17 02:10:42 +0000141 virtual void onDraw(SkCanvas* canvas) {
reed@google.comec93bf92012-12-17 13:39:25 +0000142 static const InsetProc insetProcs[] = {
143 inset0, inset1, inset2, inset3
144 };
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000145
mike@reedtribe.org37071642012-12-17 02:10:42 +0000146 SkRRect rrect[4];
reed@google.comec93bf92012-12-17 13:39:25 +0000147 SkRect r = { 0, 0, 120, 100 };
mike@reedtribe.org37071642012-12-17 02:10:42 +0000148 SkVector radii[4] = {
reed@google.comec93bf92012-12-17 13:39:25 +0000149 { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 }
mike@reedtribe.org37071642012-12-17 02:10:42 +0000150 };
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000151
mike@reedtribe.org37071642012-12-17 02:10:42 +0000152 rrect[0].setRect(r);
153 rrect[1].setOval(r);
154 rrect[2].setRectXY(r, 20, 20);
155 rrect[3].setRectRadii(r, radii);
156
reed@google.comec93bf92012-12-17 13:39:25 +0000157 canvas->translate(50.5f, 50.5f);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000158 for (size_t j = 0; j < SK_ARRAY_COUNT(insetProcs); ++j) {
159 canvas->save();
160 for (size_t i = 0; i < SK_ARRAY_COUNT(rrect); ++i) {
161 drawrr(canvas, rrect[i], insetProcs[j]);
reed@google.comec93bf92012-12-17 13:39:25 +0000162 canvas->translate(200, 0);
mike@reedtribe.orgc3442d52012-12-17 02:34:28 +0000163 }
164 canvas->restore();
reed@google.comec93bf92012-12-17 13:39:25 +0000165 canvas->translate(0, 170);
mike@reedtribe.org37071642012-12-17 02:10:42 +0000166 }
167 }
skia.committer@gmail.com7a03d862012-12-18 02:03:03 +0000168
mike@reedtribe.org37071642012-12-17 02:10:42 +0000169private:
170 typedef GM INHERITED;
171};
172
173DEF_GM( return new RRectGM; )