ninepatchstretch.cpp revision 48dd1a26ec07c5baa04856202e4e7e2a53e4d7e5
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
12static 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
51namespace skiagm {
52
53class NinePatchStretchGM : public GM {
54public:
55    SkBitmap fBM;
56
57	NinePatchStretchGM() {}
58
59protected:
60    virtual SkString onShortName() {
61        return SkString("ninepatch-stretch");
62    }
63
64	virtual SkISize onISize() {
65        return make_isize(400, 400);
66    }
67
68    virtual void onDraw(SkCanvas* canvas) {
69        SkBitmap bm;
70        SkIRect center;
71        make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, &center);
72
73        // amount of bm that should not be stretched (unless we have to)
74        const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
75
76        const SkTSize<SkScalar> size[] = {
77            { fixed * 4 / 5, fixed * 4 / 5 },   // shrink in both axes
78            { fixed * 4 / 5, fixed * 4 },       // shrink in X
79            { fixed * 4,     fixed * 4 / 5 },   // shrink in Y
80            { fixed * 4,     fixed * 4 }
81        };
82
83        canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
84
85        SkScalar x = SkIntToScalar(100);
86        SkScalar y = SkIntToScalar(100);
87
88        SkPaint paint;
89        paint.setFilterBitmap(true);
90
91        for (int iy = 0; iy < 2; ++iy) {
92            for (int ix = 0; ix < 2; ++ix) {
93                int i = ix * 2 + iy;
94                SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
95                                            size[i].width(), size[i].height());
96                canvas->drawBitmapNine(bm, center, r, &paint);
97            }
98        }
99    }
100
101private:
102    typedef GM INHERITED;
103};
104
105//////////////////////////////////////////////////////////////////////////////
106
107static GM* MyFactory(void*) { return new NinePatchStretchGM; }
108static GMRegistry reg(MyFactory);
109
110}
111
112
113
114