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