blob: 74cc87f30597f1126bcec5117c346f6d67886d8b [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
commit-bot@chromium.org78733332014-01-22 14:43:47 +000010static void make_bitmap(SkBitmap* bitmap, SkIRect* center) {
reed@google.comb6705252011-09-06 19:23:41 +000011 const int kFixed = 28;
12 const int kStretchy = 8;
13 const int kSize = 2*kFixed + kStretchy;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000014
reed@google.comeb9a46c2014-01-25 16:46:20 +000015 bitmap->allocN32Pixels(kSize, kSize);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000016 SkCanvas canvas(*bitmap);
junov@google.comdbfac8a2012-12-06 21:47:40 +000017 canvas.clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000018
reed@google.comb6705252011-09-06 19:23:41 +000019 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
20 const SkScalar strokeWidth = SkIntToScalar(6);
21 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
rmistry@google.comd6176b02012-08-23 18:14:13 +000022
reed@google.comb6705252011-09-06 19:23:41 +000023 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.comb6705252011-09-06 19:23:41 +000025 SkPaint paint;
26 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000027
reed@google.comb6705252011-09-06 19:23:41 +000028 paint.setColor(0xFFFF0000);
29 canvas.drawRoundRect(r, radius, radius, paint);
30 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
31 paint.setColor(0x8800FF00);
32 canvas.drawRect(r, paint);
33 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
34 paint.setColor(0x880000FF);
35 canvas.drawRect(r, paint);
36}
37
38namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +000039
reed@google.comb6705252011-09-06 19:23:41 +000040class NinePatchStretchGM : public GM {
41public:
42 SkBitmap fBM;
43
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 NinePatchStretchGM() {}
reed@google.comb6705252011-09-06 19:23:41 +000045
46protected:
47 virtual SkString onShortName() {
48 return SkString("ninepatch-stretch");
49 }
50
rmistry@google.comd6176b02012-08-23 18:14:13 +000051 virtual SkISize onISize() {
reed@google.comb6705252011-09-06 19:23:41 +000052 return make_isize(400, 400);
53 }
54
reed@google.comb6705252011-09-06 19:23:41 +000055 virtual void onDraw(SkCanvas* canvas) {
reed@google.comb6705252011-09-06 19:23:41 +000056 SkBitmap bm;
57 SkIRect center;
commit-bot@chromium.org78733332014-01-22 14:43:47 +000058 make_bitmap(&bm, &center);
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
reed@google.comb6705252011-09-06 19:23:41 +000060 // amount of bm that should not be stretched (unless we have to)
61 const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@google.comb6705252011-09-06 19:23:41 +000063 const SkTSize<SkScalar> size[] = {
64 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
65 { fixed * 4 / 5, fixed * 4 }, // shrink in X
66 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
67 { fixed * 4, fixed * 4 }
68 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000069
reed@google.comb6705252011-09-06 19:23:41 +000070 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
reed@google.comb6705252011-09-06 19:23:41 +000072 SkScalar x = SkIntToScalar(100);
73 SkScalar y = SkIntToScalar(100);
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
reed@google.comb6705252011-09-06 19:23:41 +000075 SkPaint paint;
reed@google.com44699382013-10-31 17:28:30 +000076 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
rmistry@google.comd6176b02012-08-23 18:14:13 +000077
reed@google.comb6705252011-09-06 19:23:41 +000078 for (int iy = 0; iy < 2; ++iy) {
79 for (int ix = 0; ix < 2; ++ix) {
80 int i = ix * 2 + iy;
81 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
82 size[i].width(), size[i].height());
reed@google.com9c9b8d92011-09-07 12:27:43 +000083 canvas->drawBitmapNine(bm, center, r, &paint);
reed@google.comb6705252011-09-06 19:23:41 +000084 }
85 }
86 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
reed@google.comb6705252011-09-06 19:23:41 +000088private:
89 typedef GM INHERITED;
90};
91
92//////////////////////////////////////////////////////////////////////////////
93
94static GM* MyFactory(void*) { return new NinePatchStretchGM; }
95static GMRegistry reg(MyFactory);
96
97}