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