blob: 551f020625175447a23bf3bbc248a749ca3db20f [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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010#if SK_SUPPORT_GPU
reed@google.comb6705252011-09-06 19:23:41 +000011#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012#else
13class GrContext;
14#endif
reed@google.comb6705252011-09-06 19:23:41 +000015
16static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000017 SkBaseDevice* dev;
rmistry@google.comd6176b02012-08-23 18:14:13 +000018
reed@google.comb6705252011-09-06 19:23:41 +000019 const int kFixed = 28;
20 const int kStretchy = 8;
21 const int kSize = 2*kFixed + kStretchy;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000022
23#if SK_SUPPORT_GPU
reed@google.comb6705252011-09-06 19:23:41 +000024 if (ctx) {
25 dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
26 *bitmap = dev->accessBitmap(false);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000027 } else
28#endif
29 {
reed@google.comb6705252011-09-06 19:23:41 +000030 bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
31 bitmap->allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000032 dev = new SkBitmapDevice(*bitmap);
reed@google.comb6705252011-09-06 19:23:41 +000033 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000034
reed@google.com44a42ea2012-10-01 17:54:05 +000035 SkCanvas canvas(dev);
36 dev->unref();
junov@google.comdbfac8a2012-12-06 21:47:40 +000037 canvas.clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000038
reed@google.comb6705252011-09-06 19:23:41 +000039 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
40 const SkScalar strokeWidth = SkIntToScalar(6);
41 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
reed@google.comb6705252011-09-06 19:23:41 +000043 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
reed@google.comb6705252011-09-06 19:23:41 +000045 SkPaint paint;
46 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000047
reed@google.comb6705252011-09-06 19:23:41 +000048 paint.setColor(0xFFFF0000);
49 canvas.drawRoundRect(r, radius, radius, paint);
50 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
51 paint.setColor(0x8800FF00);
52 canvas.drawRect(r, paint);
53 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
54 paint.setColor(0x880000FF);
55 canvas.drawRect(r, paint);
56}
57
58namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
reed@google.comb6705252011-09-06 19:23:41 +000060class NinePatchStretchGM : public GM {
61public:
62 SkBitmap fBM;
63
rmistry@google.comd6176b02012-08-23 18:14:13 +000064 NinePatchStretchGM() {}
reed@google.comb6705252011-09-06 19:23:41 +000065
66protected:
67 virtual SkString onShortName() {
68 return SkString("ninepatch-stretch");
69 }
70
rmistry@google.comd6176b02012-08-23 18:14:13 +000071 virtual SkISize onISize() {
reed@google.comb6705252011-09-06 19:23:41 +000072 return make_isize(400, 400);
73 }
74
reed@google.comb6705252011-09-06 19:23:41 +000075 virtual void onDraw(SkCanvas* canvas) {
reed@google.comb6705252011-09-06 19:23:41 +000076 SkBitmap bm;
77 SkIRect center;
78 make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, &center);
rmistry@google.comd6176b02012-08-23 18:14:13 +000079
reed@google.comb6705252011-09-06 19:23:41 +000080 // amount of bm that should not be stretched (unless we have to)
81 const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
rmistry@google.comd6176b02012-08-23 18:14:13 +000082
reed@google.comb6705252011-09-06 19:23:41 +000083 const SkTSize<SkScalar> size[] = {
84 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
85 { fixed * 4 / 5, fixed * 4 }, // shrink in X
86 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
87 { fixed * 4, fixed * 4 }
88 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000089
reed@google.comb6705252011-09-06 19:23:41 +000090 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +000091
reed@google.comb6705252011-09-06 19:23:41 +000092 SkScalar x = SkIntToScalar(100);
93 SkScalar y = SkIntToScalar(100);
rmistry@google.comd6176b02012-08-23 18:14:13 +000094
reed@google.comb6705252011-09-06 19:23:41 +000095 SkPaint paint;
96 paint.setFilterBitmap(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
reed@google.comb6705252011-09-06 19:23:41 +000098 for (int iy = 0; iy < 2; ++iy) {
99 for (int ix = 0; ix < 2; ++ix) {
100 int i = ix * 2 + iy;
101 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
102 size[i].width(), size[i].height());
reed@google.com9c9b8d92011-09-07 12:27:43 +0000103 canvas->drawBitmapNine(bm, center, r, &paint);
reed@google.comb6705252011-09-06 19:23:41 +0000104 }
105 }
106 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
reed@google.comb6705252011-09-06 19:23:41 +0000108private:
109 typedef GM INHERITED;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114static GM* MyFactory(void*) { return new NinePatchStretchGM; }
115static GMRegistry reg(MyFactory);
116
117}