blob: bab7b232cc141f7d462ec316345fc066e2c43bbe [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkPaint.h"
reed@google.com40a37722011-09-06 18:54:54 +000011#include "SkGpuDevice.h"
12
13static 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
reed@google.com40a37722011-09-06 18:54:54 +000042 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);
reed@google.com40a37722011-09-06 18:54:54 +000050}
51
reed@android.com8a1c16f2008-12-17 15:59:43 +000052
reed@google.com81e3d7f2011-06-01 12:42:36 +000053class NinePatchView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054public:
reed@google.com40a37722011-09-06 18:54:54 +000055 NinePatchView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000056
57protected:
58 // overrides from SkEventSink
59 virtual bool onQuery(SkEvent* evt) {
60 if (SampleCode::TitleQ(*evt)) {
61 SampleCode::TitleR(evt, "NinePatch");
62 return true;
63 }
64 return this->INHERITED::onQuery(evt);
65 }
reed@android.com7d970c72010-04-22 16:07:49 +000066
reed@google.com81e3d7f2011-06-01 12:42:36 +000067 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com40a37722011-09-06 18:54:54 +000068 SkBitmap bm;
69 SkIRect center;
70 make_bitmap(&bm, SampleCode::GetGr(), &center);
reed@android.com7d970c72010-04-22 16:07:49 +000071
reed@google.com40a37722011-09-06 18:54:54 +000072 // amount of bm that should not be stretched (unless we have to)
73 const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
reed@android.com7d970c72010-04-22 16:07:49 +000074
reed@google.com40a37722011-09-06 18:54:54 +000075 const SkTSize<SkScalar> size[] = {
76 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
77 { fixed * 4 / 5, fixed * 4 }, // shrink in X
78 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
79 { fixed * 4, fixed * 4 }
80 };
81
82 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
83
84 SkScalar x = SkIntToScalar(100);
85 SkScalar y = SkIntToScalar(100);
86
reed@android.com7d970c72010-04-22 16:07:49 +000087 SkPaint paint;
reed@google.com40a37722011-09-06 18:54:54 +000088 paint.setFilterBitmap(true);
89
90 for (int iy = 0; iy < 2; ++iy) {
91 for (int ix = 0; ix < 2; ++ix) {
92 int i = ix * 2 + iy;
93 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
94 size[i].width(), size[i].height());
reed@google.com9c9b8d92011-09-07 12:27:43 +000095 canvas->drawBitmapNine(bm, center, r, &paint);
reed@google.com40a37722011-09-06 18:54:54 +000096 }
97 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 }
99
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100private:
reed@android.comc4cae852009-09-23 15:06:10 +0000101 SkScalar fX, fY;
reed@google.com81e3d7f2011-06-01 12:42:36 +0000102 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103};
104
105//////////////////////////////////////////////////////////////////////////////
106
107static SkView* MyFactory() { return new NinePatchView; }
108static SkViewRegister reg(MyFactory);
109