mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkPoint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkRRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "tools/ToolUtils.h" |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 20 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 21 | typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*); |
| 22 | |
| 23 | static 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.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 31 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 32 | 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 | |
| 43 | static 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.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 51 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 52 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 59 | static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 60 | SkRect r = src.rect(); |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 61 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 62 | r.inset(dx, dy); |
| 63 | if (r.isEmpty()) { |
| 64 | dst->setEmpty(); |
| 65 | return; |
| 66 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 67 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 68 | 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.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 83 | static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 84 | return newSize * radius / oldSize; |
| 85 | } |
| 86 | |
| 87 | static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 88 | SkRect r = src.rect(); |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 89 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 90 | r.inset(dx, dy); |
| 91 | if (r.isEmpty()) { |
| 92 | dst->setEmpty(); |
| 93 | return; |
| 94 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 95 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 96 | 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.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 101 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 103 | } |
| 104 | dst->setRectRadii(r, radii); |
| 105 | } |
| 106 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 107 | static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) { |
| 108 | SkPaint paint; |
| 109 | paint.setAntiAlias(true); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 110 | paint.setStyle(SkPaint::kStroke_Style); |
| 111 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 112 | if (rrect.isRect()) { |
| 113 | paint.setColor(SK_ColorRED); |
| 114 | } else if (rrect.isOval()) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 115 | paint.setColor(ToolUtils::color_to_565(0xFF008800)); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 116 | } else if (rrect.isSimple()) { |
| 117 | paint.setColor(SK_ColorBLUE); |
| 118 | } else { |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 119 | paint.setColor(SK_ColorBLACK); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 120 | } |
| 121 | canvas->drawRRect(rrect, paint); |
| 122 | } |
| 123 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 124 | static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) { |
| 125 | SkRRect rr; |
mike@reedtribe.org | 5138638 | 2012-12-17 03:48:12 +0000 | [diff] [blame] | 126 | for (SkScalar d = -30; d <= 30; d += 5) { |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 127 | proc(rrect, d, d, &rr); |
| 128 | draw_rrect_color(canvas, rr); |
| 129 | } |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | class RRectGM : public skiagm::GM { |
| 133 | public: |
| 134 | RRectGM() {} |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 135 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 136 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 137 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 138 | SkString onShortName() override { |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 139 | return SkString("rrect"); |
| 140 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 142 | SkISize onISize() override { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 143 | return SkISize::Make(820, 710); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 144 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 145 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 146 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 147 | constexpr InsetProc insetProcs[] = { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 148 | inset0, inset1, inset2, inset3 |
| 149 | }; |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 150 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 151 | SkRRect rrect[4]; |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 152 | SkRect r = { 0, 0, 120, 100 }; |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 153 | SkVector radii[4] = { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 154 | { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 } |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 155 | }; |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 156 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 157 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 162 | canvas->translate(50.5f, 50.5f); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 163 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 167 | canvas->translate(200, 0); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 168 | } |
| 169 | canvas->restore(); |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 170 | canvas->translate(0, 170); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 173 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 174 | private: |
| 175 | typedef GM INHERITED; |
| 176 | }; |
| 177 | |
| 178 | DEF_GM( return new RRectGM; ) |