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 | |
| 10 | #include "SkGpuDevice.h" |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 11 | |
| 12 | static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) { |
| 13 | SkDevice* dev; |
| 14 | SkCanvas canvas; |
| 15 | |
| 16 | const int kFixed = 28; |
| 17 | const int kStretchy = 8; |
| 18 | const int kSize = 2*kFixed + kStretchy; |
| 19 | |
| 20 | if (ctx) { |
| 21 | dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize); |
| 22 | *bitmap = dev->accessBitmap(false); |
| 23 | } else { |
| 24 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize); |
| 25 | bitmap->allocPixels(); |
| 26 | dev = new SkDevice(*bitmap); |
| 27 | } |
| 28 | |
| 29 | canvas.setDevice(dev)->unref(); |
| 30 | canvas.clear(0); |
| 31 | |
| 32 | SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 33 | const SkScalar strokeWidth = SkIntToScalar(6); |
| 34 | const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; |
| 35 | |
| 36 | center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); |
| 37 | |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
| 40 | |
| 41 | paint.setColor(0xFFFF0000); |
| 42 | canvas.drawRoundRect(r, radius, radius, paint); |
| 43 | r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize)); |
| 44 | paint.setColor(0x8800FF00); |
| 45 | canvas.drawRect(r, paint); |
| 46 | r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy)); |
| 47 | paint.setColor(0x880000FF); |
| 48 | canvas.drawRect(r, paint); |
| 49 | } |
| 50 | |
| 51 | namespace skiagm { |
| 52 | |
| 53 | class NinePatchStretchGM : public GM { |
| 54 | public: |
| 55 | SkBitmap fBM; |
| 56 | |
| 57 | NinePatchStretchGM() {} |
| 58 | |
| 59 | protected: |
| 60 | virtual SkString onShortName() { |
| 61 | return SkString("ninepatch-stretch"); |
| 62 | } |
| 63 | |
| 64 | virtual SkISize onISize() { |
| 65 | return make_isize(400, 400); |
| 66 | } |
| 67 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 68 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | b636adf | 2011-09-06 19:34:13 +0000 | [diff] [blame] | 69 | canvas->drawColor(SK_ColorWHITE); |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 70 | |
| 71 | SkBitmap bm; |
| 72 | SkIRect center; |
| 73 | make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, ¢er); |
| 74 | |
| 75 | // amount of bm that should not be stretched (unless we have to) |
| 76 | const SkScalar fixed = SkIntToScalar(bm.width() - center.width()); |
| 77 | |
| 78 | const SkTSize<SkScalar> size[] = { |
| 79 | { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes |
| 80 | { fixed * 4 / 5, fixed * 4 }, // shrink in X |
| 81 | { fixed * 4, fixed * 4 / 5 }, // shrink in Y |
| 82 | { fixed * 4, fixed * 4 } |
| 83 | }; |
| 84 | |
| 85 | canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL); |
| 86 | |
| 87 | SkScalar x = SkIntToScalar(100); |
| 88 | SkScalar y = SkIntToScalar(100); |
| 89 | |
| 90 | SkPaint paint; |
| 91 | paint.setFilterBitmap(true); |
| 92 | |
| 93 | for (int iy = 0; iy < 2; ++iy) { |
| 94 | for (int ix = 0; ix < 2; ++ix) { |
| 95 | int i = ix * 2 + iy; |
| 96 | SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, |
| 97 | size[i].width(), size[i].height()); |
reed@google.com | 9c9b8d9 | 2011-09-07 12:27:43 +0000 | [diff] [blame^] | 98 | canvas->drawBitmapNine(bm, center, r, &paint); |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | typedef GM INHERITED; |
| 105 | }; |
| 106 | |
| 107 | ////////////////////////////////////////////////////////////////////////////// |
| 108 | |
| 109 | static GM* MyFactory(void*) { return new NinePatchStretchGM; } |
| 110 | static GMRegistry reg(MyFactory); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |