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