blob: f18e5fcee53879f419c5bb26204d39b8d2563fd1 [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
10#include "SkGpuDevice.h"
reed@google.comb6705252011-09-06 19:23:41 +000011
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
reed@google.comb6705252011-09-06 19:23:41 +000068 virtual void onDraw(SkCanvas* canvas) {
reed@google.comb636adf2011-09-06 19:34:13 +000069 canvas->drawColor(SK_ColorWHITE);
reed@google.comb6705252011-09-06 19:23:41 +000070
71 SkBitmap bm;
72 SkIRect center;
73 make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, &center);
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.com9c9b8d92011-09-07 12:27:43 +000098 canvas->drawBitmapNine(bm, center, r, &paint);
reed@google.comb6705252011-09-06 19:23:41 +000099 }
100 }
101 }
102
103private:
104 typedef GM INHERITED;
105};
106
107//////////////////////////////////////////////////////////////////////////////
108
109static GM* MyFactory(void*) { return new NinePatchStretchGM; }
110static GMRegistry reg(MyFactory);
111
112}
113
114
115