blob: 7dcbbce6138c81a05176e687f7683ba8035b4279 [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"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkImageDecoder.h"
12#include "SkNinePatch.h"
13#include "SkPaint.h"
14#include "SkUnPreMultiply.h"
reed@google.com40a37722011-09-06 18:54:54 +000015#include "SkGpuDevice.h"
16
17static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
18 SkDevice* dev;
19 SkCanvas canvas;
20
21 const int kFixed = 28;
22 const int kStretchy = 8;
23 const int kSize = 2*kFixed + kStretchy;
24
25 if (ctx) {
26 dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
27 *bitmap = dev->accessBitmap(false);
28 } else {
29 bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
30 bitmap->allocPixels();
31 dev = new SkDevice(*bitmap);
32 }
33
34 canvas.setDevice(dev)->unref();
35 canvas.clear(0);
36
37 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
38 const SkScalar strokeWidth = SkIntToScalar(6);
39 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
40
41 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
42
43 SkPaint paint;
44 paint.setAntiAlias(true);
45
46#if 0
47 r.inset(strokeWidth/2, strokeWidth/2);
48
49 paint.setColor(SK_ColorBLUE);
50 canvas.drawRoundRect(r, radius, radius, paint);
51 paint.setColor(SK_ColorRED);
52 paint.setStyle(SkPaint::kStroke_Style);
53 paint.setStrokeWidth(strokeWidth);
54 canvas.drawRoundRect(r, radius, radius, paint);
55#else
56 paint.setColor(0xFFFF0000);
57 canvas.drawRoundRect(r, radius, radius, paint);
58 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
59 paint.setColor(0x8800FF00);
60 canvas.drawRect(r, paint);
61 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
62 paint.setColor(0x880000FF);
63 canvas.drawRect(r, paint);
64#endif
65}
66
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
reed@google.com81e3d7f2011-06-01 12:42:36 +000068class NinePatchView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069public:
reed@google.com40a37722011-09-06 18:54:54 +000070 NinePatchView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000071
72protected:
73 // overrides from SkEventSink
74 virtual bool onQuery(SkEvent* evt) {
75 if (SampleCode::TitleQ(*evt)) {
76 SampleCode::TitleR(evt, "NinePatch");
77 return true;
78 }
79 return this->INHERITED::onQuery(evt);
80 }
reed@android.com7d970c72010-04-22 16:07:49 +000081
reed@google.com40a37722011-09-06 18:54:54 +000082 static void drawNine(SkCanvas* canvas, const SkRect& dst, const SkBitmap& bm,
83 const SkIRect& center, const SkPaint* paint) {
84 SkIRect margin;
85 margin.set(center.fLeft, center.fTop, bm.width() - center.fRight,
86 bm.height() - center.fBottom);
87 SkNinePatch::DrawNine(canvas, dst, bm, margin, paint);
reed@android.com7d970c72010-04-22 16:07:49 +000088 }
89
reed@google.com81e3d7f2011-06-01 12:42:36 +000090 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com40a37722011-09-06 18:54:54 +000091 SkBitmap bm;
92 SkIRect center;
93 make_bitmap(&bm, SampleCode::GetGr(), &center);
reed@android.com7d970c72010-04-22 16:07:49 +000094
reed@google.com40a37722011-09-06 18:54:54 +000095 // amount of bm that should not be stretched (unless we have to)
96 const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
reed@android.com7d970c72010-04-22 16:07:49 +000097
reed@google.com40a37722011-09-06 18:54:54 +000098 const SkTSize<SkScalar> size[] = {
99 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
100 { fixed * 4 / 5, fixed * 4 }, // shrink in X
101 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
102 { fixed * 4, fixed * 4 }
103 };
104
105 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
106
107 SkScalar x = SkIntToScalar(100);
108 SkScalar y = SkIntToScalar(100);
109
reed@android.com7d970c72010-04-22 16:07:49 +0000110 SkPaint paint;
reed@google.com40a37722011-09-06 18:54:54 +0000111 paint.setFilterBitmap(true);
112
113 for (int iy = 0; iy < 2; ++iy) {
114 for (int ix = 0; ix < 2; ++ix) {
115 int i = ix * 2 + iy;
116 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
117 size[i].width(), size[i].height());
118 drawNine(canvas, r, bm, center, &paint);
119 }
120 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 }
122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123private:
reed@android.comc4cae852009-09-23 15:06:10 +0000124 SkScalar fX, fY;
reed@google.com81e3d7f2011-06-01 12:42:36 +0000125 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126};
127
128//////////////////////////////////////////////////////////////////////////////
129
130static SkView* MyFactory() { return new NinePatchView; }
131static SkViewRegister reg(MyFactory);
132