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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkRRect.h" |
| 11 | #include "SkPath.h" |
| 12 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 13 | typedef void (*InsetProc)(const SkRRect&, SkScalar dx, SkScalar dy, SkRRect*); |
| 14 | |
| 15 | static 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.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 23 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 24 | 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 | |
| 35 | static 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.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 43 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 44 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 51 | static void inset2(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 52 | SkRect r = src.rect(); |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 53 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 54 | r.inset(dx, dy); |
| 55 | if (r.isEmpty()) { |
| 56 | dst->setEmpty(); |
| 57 | return; |
| 58 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 59 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 60 | 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.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 75 | static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize) { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 76 | return newSize * radius / oldSize; |
| 77 | } |
| 78 | |
| 79 | static void inset3(const SkRRect& src, SkScalar dx, SkScalar dy, SkRRect* dst) { |
| 80 | SkRect r = src.rect(); |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 81 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 82 | r.inset(dx, dy); |
| 83 | if (r.isEmpty()) { |
| 84 | dst->setEmpty(); |
| 85 | return; |
| 86 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 87 | |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 88 | 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.com | 0f0d9b7 | 2013-02-27 15:41:12 +0000 | [diff] [blame] | 93 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 95 | } |
| 96 | dst->setRectRadii(r, radii); |
| 97 | } |
| 98 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 99 | static void draw_rrect_color(SkCanvas* canvas, const SkRRect& rrect) { |
| 100 | SkPaint paint; |
| 101 | paint.setAntiAlias(true); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 102 | paint.setStyle(SkPaint::kStroke_Style); |
| 103 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 104 | if (rrect.isRect()) { |
| 105 | paint.setColor(SK_ColorRED); |
| 106 | } else if (rrect.isOval()) { |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 107 | paint.setColor(0xFF008800); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 108 | } else if (rrect.isSimple()) { |
| 109 | paint.setColor(SK_ColorBLUE); |
| 110 | } else { |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 111 | paint.setColor(SK_ColorBLACK); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 112 | } |
| 113 | canvas->drawRRect(rrect, paint); |
| 114 | } |
| 115 | |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 116 | static void drawrr(SkCanvas* canvas, const SkRRect& rrect, InsetProc proc) { |
| 117 | SkRRect rr; |
mike@reedtribe.org | 5138638 | 2012-12-17 03:48:12 +0000 | [diff] [blame] | 118 | for (SkScalar d = -30; d <= 30; d += 5) { |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 119 | proc(rrect, d, d, &rr); |
| 120 | draw_rrect_color(canvas, rr); |
| 121 | } |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | class RRectGM : public skiagm::GM { |
| 125 | public: |
| 126 | RRectGM() {} |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 127 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 128 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 129 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 130 | return kSkipTiled_Flag; |
| 131 | } |
| 132 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 133 | virtual SkString onShortName() { |
| 134 | return SkString("rrect"); |
| 135 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 136 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 137 | virtual SkISize onISize() { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 138 | return SkISize::Make(820, 710); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 139 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 140 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 141 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 142 | static const InsetProc insetProcs[] = { |
| 143 | inset0, inset1, inset2, inset3 |
| 144 | }; |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 145 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 146 | SkRRect rrect[4]; |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 147 | SkRect r = { 0, 0, 120, 100 }; |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 148 | SkVector radii[4] = { |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 149 | { 0, 0 }, { 30, 1 }, { 10, 40 }, { 40, 40 } |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 150 | }; |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 151 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 152 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 157 | canvas->translate(50.5f, 50.5f); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 158 | 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.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 162 | canvas->translate(200, 0); |
mike@reedtribe.org | c3442d5 | 2012-12-17 02:34:28 +0000 | [diff] [blame] | 163 | } |
| 164 | canvas->restore(); |
reed@google.com | ec93bf9 | 2012-12-17 13:39:25 +0000 | [diff] [blame] | 165 | canvas->translate(0, 170); |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 168 | |
mike@reedtribe.org | 3707164 | 2012-12-17 02:10:42 +0000 | [diff] [blame] | 169 | private: |
| 170 | typedef GM INHERITED; |
| 171 | }; |
| 172 | |
| 173 | DEF_GM( return new RRectGM; ) |