blob: 4c6e7d274209f41bd700db7079da91d340d28a0f [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"
reed4c21dc52015-06-25 12:32:03 -07009#include "SkSurface.h"
reed@google.comb6705252011-09-06 19:23:41 +000010
reede8f30622016-03-23 18:59:25 -070011static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
reed4c21dc52015-06-25 12:32:03 -070012 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
reede8f30622016-03-23 18:59:25 -070013 auto surface = root->makeSurface(info);
reed4c21dc52015-06-25 12:32:03 -070014 if (!surface) {
reede8f30622016-03-23 18:59:25 -070015 surface = SkSurface::MakeRaster(info);
reed4c21dc52015-06-25 12:32:03 -070016 }
17 return surface;
18}
19
reed9ce9d672016-03-17 10:51:11 -070020static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) {
reed@google.comb6705252011-09-06 19:23:41 +000021 const int kFixed = 28;
22 const int kStretchy = 8;
23 const int kSize = 2*kFixed + kStretchy;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000024
reede8f30622016-03-23 18:59:25 -070025 auto surface(make_surface(root, kSize));
reed4c21dc52015-06-25 12:32:03 -070026 SkCanvas* canvas = surface->getCanvas();
rmistry@google.comd6176b02012-08-23 18:14:13 +000027
reed@google.comb6705252011-09-06 19:23:41 +000028 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
29 const SkScalar strokeWidth = SkIntToScalar(6);
30 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
rmistry@google.comd6176b02012-08-23 18:14:13 +000031
reed@google.comb6705252011-09-06 19:23:41 +000032 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
reed@google.comb6705252011-09-06 19:23:41 +000034 SkPaint paint;
35 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
reed@google.comb6705252011-09-06 19:23:41 +000037 paint.setColor(0xFFFF0000);
reed4c21dc52015-06-25 12:32:03 -070038 canvas->drawRoundRect(r, radius, radius, paint);
reed@google.comb6705252011-09-06 19:23:41 +000039 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
40 paint.setColor(0x8800FF00);
reed4c21dc52015-06-25 12:32:03 -070041 canvas->drawRect(r, paint);
reed@google.comb6705252011-09-06 19:23:41 +000042 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
43 paint.setColor(0x880000FF);
reed4c21dc52015-06-25 12:32:03 -070044 canvas->drawRect(r, paint);
45
reed9ce9d672016-03-17 10:51:11 -070046 return surface->makeImageSnapshot();
reed@google.comb6705252011-09-06 19:23:41 +000047}
48
reed4c21dc52015-06-25 12:32:03 -070049static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
50 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
51 bm->allocPixels(info);
52 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
53}
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed4c21dc52015-06-25 12:32:03 -070055class NinePatchStretchGM : public skiagm::GM {
reed@google.comb6705252011-09-06 19:23:41 +000056public:
reed9ce9d672016-03-17 10:51:11 -070057 sk_sp<SkImage> fImage;
58 SkBitmap fBitmap;
59 SkIRect fCenter;
reed@google.comb6705252011-09-06 19:23:41 +000060
rmistry@google.comd6176b02012-08-23 18:14:13 +000061 NinePatchStretchGM() {}
reed@google.comb6705252011-09-06 19:23:41 +000062
63protected:
reed4c21dc52015-06-25 12:32:03 -070064 SkString onShortName() override {
reed@google.comb6705252011-09-06 19:23:41 +000065 return SkString("ninepatch-stretch");
66 }
67
reed4c21dc52015-06-25 12:32:03 -070068 SkISize onISize() override {
joshualitt33a5fce2015-11-18 13:28:51 -080069 return SkISize::Make(760, 800);
reed@google.comb6705252011-09-06 19:23:41 +000070 }
71
reed4c21dc52015-06-25 12:32:03 -070072 void onDraw(SkCanvas* canvas) override {
halcanary96fcdcc2015-08-27 07:41:13 -070073 if (nullptr == fBitmap.pixelRef()) {
reed9ce9d672016-03-17 10:51:11 -070074 fImage = make_image(canvas, &fCenter);
75 image_to_bitmap(fImage.get(), &fBitmap);
reed4c21dc52015-06-25 12:32:03 -070076 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000077
reed@google.comb6705252011-09-06 19:23:41 +000078 // amount of bm that should not be stretched (unless we have to)
reed4c21dc52015-06-25 12:32:03 -070079 const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width());
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
reed@google.comb6705252011-09-06 19:23:41 +000081 const SkTSize<SkScalar> size[] = {
82 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
83 { fixed * 4 / 5, fixed * 4 }, // shrink in X
84 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
85 { fixed * 4, fixed * 4 }
86 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
halcanary96fcdcc2015-08-27 07:41:13 -070088 canvas->drawBitmap(fBitmap, 10, 10, nullptr);
rmistry@google.comd6176b02012-08-23 18:14:13 +000089
reed@google.comb6705252011-09-06 19:23:41 +000090 SkScalar x = SkIntToScalar(100);
91 SkScalar y = SkIntToScalar(100);
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
reed@google.comb6705252011-09-06 19:23:41 +000093 SkPaint paint;
joshualitt33a5fce2015-11-18 13:28:51 -080094 for (int filter = 0; filter < 2; filter++) {
95 paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
96 canvas->translate(0, filter * SkIntToScalar(400));
97 for (int iy = 0; iy < 2; ++iy) {
98 for (int ix = 0; ix < 2; ++ix) {
99 int i = ix * 2 + iy;
100 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
101 size[i].width(), size[i].height());
102 canvas->drawBitmapNine(fBitmap, fCenter, r, &paint);
reed9ce9d672016-03-17 10:51:11 -0700103 canvas->drawImageNine(fImage.get(), fCenter, r.makeOffset(360, 0), &paint);
joshualitt33a5fce2015-11-18 13:28:51 -0800104 }
reed@google.comb6705252011-09-06 19:23:41 +0000105 }
106 }
107 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
reed@google.comb6705252011-09-06 19:23:41 +0000109private:
reed4c21dc52015-06-25 12:32:03 -0700110 typedef skiagm::GM INHERITED;
reed@google.comb6705252011-09-06 19:23:41 +0000111};
reed4c21dc52015-06-25 12:32:03 -0700112DEF_GM( return new NinePatchStretchGM; )