blob: 226649dc15829114f465e6ac8ff7369fc82639d2 [file] [log] [blame]
reed@google.comb6705252011-09-06 19:23:41 +00001/*
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.comcf8fb1f2012-08-02 14:03:32 +000010#if SK_SUPPORT_GPU
reed@google.comb6705252011-09-06 19:23:41 +000011#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012#else
13class GrContext;
14#endif
reed@google.comb6705252011-09-06 19:23:41 +000015
16static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
17 SkDevice* dev;
18 SkCanvas canvas;
19
20 const int kFixed = 28;
21 const int kStretchy = 8;
22 const int kSize = 2*kFixed + kStretchy;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000023
24#if SK_SUPPORT_GPU
reed@google.comb6705252011-09-06 19:23:41 +000025 if (ctx) {
26 dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
27 *bitmap = dev->accessBitmap(false);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000028 } else
29#endif
30 {
reed@google.comb6705252011-09-06 19:23:41 +000031 bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
32 bitmap->allocPixels();
33 dev = new SkDevice(*bitmap);
34 }
35
36 canvas.setDevice(dev)->unref();
37 canvas.clear(0);
38
39 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
40 const SkScalar strokeWidth = SkIntToScalar(6);
41 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
42
43 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
44
45 SkPaint paint;
46 paint.setAntiAlias(true);
47
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
58namespace skiagm {
59
60class NinePatchStretchGM : public GM {
61public:
62 SkBitmap fBM;
63
64 NinePatchStretchGM() {}
65
66protected:
67 virtual SkString onShortName() {
68 return SkString("ninepatch-stretch");
69 }
70
71 virtual SkISize onISize() {
72 return make_isize(400, 400);
73 }
74
reed@google.comb6705252011-09-06 19:23:41 +000075 virtual void onDraw(SkCanvas* canvas) {
reed@google.comb6705252011-09-06 19:23:41 +000076 SkBitmap bm;
77 SkIRect center;
78 make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, &center);
79
80 // amount of bm that should not be stretched (unless we have to)
81 const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
82
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 };
89
90 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
91
92 SkScalar x = SkIntToScalar(100);
93 SkScalar y = SkIntToScalar(100);
94
95 SkPaint paint;
96 paint.setFilterBitmap(true);
97
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.com9c9b8d92011-09-07 12:27:43 +0000103 canvas->drawBitmapNine(bm, center, r, &paint);
reed@google.comb6705252011-09-06 19:23:41 +0000104 }
105 }
106 }
107
108private:
109 typedef GM INHERITED;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114static GM* MyFactory(void*) { return new NinePatchStretchGM; }
115static GMRegistry reg(MyFactory);
116
117}
118
119
120