blob: 4286ed3e70d50d4b618f5a1d67dbfe7f92029779 [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 */
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SampleCode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000011#include "SkDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPaint.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000013#if SK_SUPPORT_GPU
reed@google.com40a37722011-09-06 18:54:54 +000014#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000015#else
16class GrContext;
17#endif
reed@google.com40a37722011-09-06 18:54:54 +000018
19static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
20 SkDevice* dev;
21 SkCanvas canvas;
22
23 const int kFixed = 28;
24 const int kStretchy = 8;
25 const int kSize = 2*kFixed + kStretchy;
26
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000027#if SK_SUPPORT_GPU
reed@google.com40a37722011-09-06 18:54:54 +000028 if (ctx) {
29 dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
30 *bitmap = dev->accessBitmap(false);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000031 } else
32#endif
33 {
reed@google.com40a37722011-09-06 18:54:54 +000034 bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
35 bitmap->allocPixels();
36 dev = new SkDevice(*bitmap);
37 }
38
39 canvas.setDevice(dev)->unref();
40 canvas.clear(0);
41
42 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
43 const SkScalar strokeWidth = SkIntToScalar(6);
44 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
45
46 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
47
48 SkPaint paint;
49 paint.setAntiAlias(true);
50
reed@google.com40a37722011-09-06 18:54:54 +000051 paint.setColor(0xFFFF0000);
52 canvas.drawRoundRect(r, radius, radius, paint);
53 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
54 paint.setColor(0x8800FF00);
55 canvas.drawRect(r, paint);
56 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
57 paint.setColor(0x880000FF);
58 canvas.drawRect(r, paint);
reed@google.com40a37722011-09-06 18:54:54 +000059}
60
reed@android.com8a1c16f2008-12-17 15:59:43 +000061
reed@google.com81e3d7f2011-06-01 12:42:36 +000062class NinePatchView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063public:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000064 NinePatchView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
66protected:
67 // overrides from SkEventSink
68 virtual bool onQuery(SkEvent* evt) {
69 if (SampleCode::TitleQ(*evt)) {
70 SampleCode::TitleR(evt, "NinePatch");
71 return true;
72 }
73 return this->INHERITED::onQuery(evt);
74 }
reed@android.com7d970c72010-04-22 16:07:49 +000075
reed@google.com81e3d7f2011-06-01 12:42:36 +000076 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com40a37722011-09-06 18:54:54 +000077 SkBitmap bm;
78 SkIRect center;
79 make_bitmap(&bm, SampleCode::GetGr(), &center);
reed@android.com7d970c72010-04-22 16:07:49 +000080
reed@google.com40a37722011-09-06 18:54:54 +000081 // amount of bm that should not be stretched (unless we have to)
82 const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
reed@android.com7d970c72010-04-22 16:07:49 +000083
reed@google.com40a37722011-09-06 18:54:54 +000084 const SkTSize<SkScalar> size[] = {
85 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
86 { fixed * 4 / 5, fixed * 4 }, // shrink in X
87 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
88 { fixed * 4, fixed * 4 }
89 };
90
91 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
92
93 SkScalar x = SkIntToScalar(100);
94 SkScalar y = SkIntToScalar(100);
95
reed@android.com7d970c72010-04-22 16:07:49 +000096 SkPaint paint;
reed@google.com40a37722011-09-06 18:54:54 +000097 paint.setFilterBitmap(true);
98
99 for (int iy = 0; iy < 2; ++iy) {
100 for (int ix = 0; ix < 2; ++ix) {
101 int i = ix * 2 + iy;
102 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
103 size[i].width(), size[i].height());
reed@google.com9c9b8d92011-09-07 12:27:43 +0000104 canvas->drawBitmapNine(bm, center, r, &paint);
reed@google.com40a37722011-09-06 18:54:54 +0000105 }
106 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 }
108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109private:
reed@android.comc4cae852009-09-23 15:06:10 +0000110 SkScalar fX, fY;
reed@google.com81e3d7f2011-06-01 12:42:36 +0000111 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112};
113
114//////////////////////////////////////////////////////////////////////////////
115
116static SkView* MyFactory() { return new NinePatchView; }
117static SkViewRegister reg(MyFactory);
118