reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | |
commit-bot@chromium.org | 7873333 | 2014-01-22 14:43:47 +0000 | [diff] [blame] | 10 | static void make_bitmap(SkBitmap* bitmap, SkIRect* center) { |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 11 | const int kFixed = 28; |
| 12 | const int kStretchy = 8; |
| 13 | const int kSize = 2*kFixed + kStretchy; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 14 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 15 | bitmap->allocN32Pixels(kSize, kSize); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 16 | SkCanvas canvas(*bitmap); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 17 | canvas.clear(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 18 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 19 | SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 20 | const SkScalar strokeWidth = SkIntToScalar(6); |
| 21 | const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 22 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 23 | center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 25 | SkPaint paint; |
| 26 | paint.setAntiAlias(true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 27 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 28 | paint.setColor(0xFFFF0000); |
| 29 | canvas.drawRoundRect(r, radius, radius, paint); |
| 30 | r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize)); |
| 31 | paint.setColor(0x8800FF00); |
| 32 | canvas.drawRect(r, paint); |
| 33 | r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy)); |
| 34 | paint.setColor(0x880000FF); |
| 35 | canvas.drawRect(r, paint); |
| 36 | } |
| 37 | |
| 38 | namespace skiagm { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 39 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 40 | class NinePatchStretchGM : public GM { |
| 41 | public: |
| 42 | SkBitmap fBM; |
| 43 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | NinePatchStretchGM() {} |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 45 | |
| 46 | protected: |
| 47 | virtual SkString onShortName() { |
| 48 | return SkString("ninepatch-stretch"); |
| 49 | } |
| 50 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 51 | virtual SkISize onISize() { |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 52 | return make_isize(400, 400); |
| 53 | } |
| 54 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 55 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 56 | SkBitmap bm; |
| 57 | SkIRect center; |
commit-bot@chromium.org | 7873333 | 2014-01-22 14:43:47 +0000 | [diff] [blame] | 58 | make_bitmap(&bm, ¢er); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 59 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 60 | // amount of bm that should not be stretched (unless we have to) |
| 61 | const SkScalar fixed = SkIntToScalar(bm.width() - center.width()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 62 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 63 | const SkTSize<SkScalar> size[] = { |
| 64 | { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes |
| 65 | { fixed * 4 / 5, fixed * 4 }, // shrink in X |
| 66 | { fixed * 4, fixed * 4 / 5 }, // shrink in Y |
| 67 | { fixed * 4, fixed * 4 } |
| 68 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 69 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 70 | canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 72 | SkScalar x = SkIntToScalar(100); |
| 73 | SkScalar y = SkIntToScalar(100); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 75 | SkPaint paint; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 76 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 77 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 78 | for (int iy = 0; iy < 2; ++iy) { |
| 79 | for (int ix = 0; ix < 2; ++ix) { |
| 80 | int i = ix * 2 + iy; |
| 81 | SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, |
| 82 | size[i].width(), size[i].height()); |
reed@google.com | 9c9b8d9 | 2011-09-07 12:27:43 +0000 | [diff] [blame] | 83 | canvas->drawBitmapNine(bm, center, r, &paint); |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 87 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 88 | private: |
| 89 | typedef GM INHERITED; |
| 90 | }; |
| 91 | |
| 92 | ////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | static GM* MyFactory(void*) { return new NinePatchStretchGM; } |
| 95 | static GMRegistry reg(MyFactory); |
| 96 | |
| 97 | } |