blob: c905cee80381a1cc80fbf7365a877e9e42973ed6 [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"
Mike Reed46596ae2018-01-02 15:40:29 -050010#include "sk_tool_utils.h"
reed@google.comb6705252011-09-06 19:23:41 +000011
reede8f30622016-03-23 18:59:25 -070012static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
reed4c21dc52015-06-25 12:32:03 -070013 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
Mike Reed46596ae2018-01-02 15:40:29 -050014 return sk_tool_utils::makeSurface(root, info);
reed4c21dc52015-06-25 12:32:03 -070015}
16
reed9ce9d672016-03-17 10:51:11 -070017static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) {
reed@google.comb6705252011-09-06 19:23:41 +000018 const int kFixed = 28;
19 const int kStretchy = 8;
20 const int kSize = 2*kFixed + kStretchy;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000021
reede8f30622016-03-23 18:59:25 -070022 auto surface(make_surface(root, kSize));
reed4c21dc52015-06-25 12:32:03 -070023 SkCanvas* canvas = surface->getCanvas();
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.comb6705252011-09-06 19:23:41 +000025 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
26 const SkScalar strokeWidth = SkIntToScalar(6);
27 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
reed@google.comb6705252011-09-06 19:23:41 +000029 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
reed@google.comb6705252011-09-06 19:23:41 +000031 SkPaint paint;
32 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
reed@google.comb6705252011-09-06 19:23:41 +000034 paint.setColor(0xFFFF0000);
reed4c21dc52015-06-25 12:32:03 -070035 canvas->drawRoundRect(r, radius, radius, paint);
reed@google.comb6705252011-09-06 19:23:41 +000036 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
37 paint.setColor(0x8800FF00);
reed4c21dc52015-06-25 12:32:03 -070038 canvas->drawRect(r, paint);
reed@google.comb6705252011-09-06 19:23:41 +000039 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
40 paint.setColor(0x880000FF);
reed4c21dc52015-06-25 12:32:03 -070041 canvas->drawRect(r, paint);
42
reed9ce9d672016-03-17 10:51:11 -070043 return surface->makeImageSnapshot();
reed@google.comb6705252011-09-06 19:23:41 +000044}
45
reed4c21dc52015-06-25 12:32:03 -070046static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
47 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
48 bm->allocPixels(info);
49 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
50}
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
reed4c21dc52015-06-25 12:32:03 -070052class NinePatchStretchGM : public skiagm::GM {
reed@google.comb6705252011-09-06 19:23:41 +000053public:
reed9ce9d672016-03-17 10:51:11 -070054 sk_sp<SkImage> fImage;
55 SkBitmap fBitmap;
56 SkIRect fCenter;
reed@google.comb6705252011-09-06 19:23:41 +000057
rmistry@google.comd6176b02012-08-23 18:14:13 +000058 NinePatchStretchGM() {}
reed@google.comb6705252011-09-06 19:23:41 +000059
60protected:
reed4c21dc52015-06-25 12:32:03 -070061 SkString onShortName() override {
reed@google.comb6705252011-09-06 19:23:41 +000062 return SkString("ninepatch-stretch");
63 }
64
reed4c21dc52015-06-25 12:32:03 -070065 SkISize onISize() override {
joshualitt33a5fce2015-11-18 13:28:51 -080066 return SkISize::Make(760, 800);
reed@google.comb6705252011-09-06 19:23:41 +000067 }
68
reed4c21dc52015-06-25 12:32:03 -070069 void onDraw(SkCanvas* canvas) override {
halcanary96fcdcc2015-08-27 07:41:13 -070070 if (nullptr == fBitmap.pixelRef()) {
reed9ce9d672016-03-17 10:51:11 -070071 fImage = make_image(canvas, &fCenter);
72 image_to_bitmap(fImage.get(), &fBitmap);
reed4c21dc52015-06-25 12:32:03 -070073 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
reed@google.comb6705252011-09-06 19:23:41 +000075 // amount of bm that should not be stretched (unless we have to)
reed4c21dc52015-06-25 12:32:03 -070076 const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width());
rmistry@google.comd6176b02012-08-23 18:14:13 +000077
Hal Canaryfafe1352017-04-11 12:12:02 -040078 const SkSize size[] = {
reed@google.comb6705252011-09-06 19:23:41 +000079 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
80 { fixed * 4 / 5, fixed * 4 }, // shrink in X
81 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
82 { fixed * 4, fixed * 4 }
83 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
halcanary96fcdcc2015-08-27 07:41:13 -070085 canvas->drawBitmap(fBitmap, 10, 10, nullptr);
rmistry@google.comd6176b02012-08-23 18:14:13 +000086
reed@google.comb6705252011-09-06 19:23:41 +000087 SkScalar x = SkIntToScalar(100);
88 SkScalar y = SkIntToScalar(100);
rmistry@google.comd6176b02012-08-23 18:14:13 +000089
reed@google.comb6705252011-09-06 19:23:41 +000090 SkPaint paint;
joshualitt33a5fce2015-11-18 13:28:51 -080091 for (int filter = 0; filter < 2; filter++) {
92 paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
93 canvas->translate(0, filter * SkIntToScalar(400));
94 for (int iy = 0; iy < 2; ++iy) {
95 for (int ix = 0; ix < 2; ++ix) {
96 int i = ix * 2 + iy;
97 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
98 size[i].width(), size[i].height());
99 canvas->drawBitmapNine(fBitmap, fCenter, r, &paint);
reed9ce9d672016-03-17 10:51:11 -0700100 canvas->drawImageNine(fImage.get(), fCenter, r.makeOffset(360, 0), &paint);
joshualitt33a5fce2015-11-18 13:28:51 -0800101 }
reed@google.comb6705252011-09-06 19:23:41 +0000102 }
103 }
104 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105
reed@google.comb6705252011-09-06 19:23:41 +0000106private:
reed4c21dc52015-06-25 12:32:03 -0700107 typedef skiagm::GM INHERITED;
reed@google.comb6705252011-09-06 19:23:41 +0000108};
reed4c21dc52015-06-25 12:32:03 -0700109DEF_GM( return new NinePatchStretchGM; )