blob: d7ba99ca261e95210a36104a23695831e54c17db [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkFilterQuality.h"
12#include "include/core/SkImage.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkSurface.h"
21#include "tools/ToolUtils.h"
reed@google.comb6705252011-09-06 19:23:41 +000022
reede8f30622016-03-23 18:59:25 -070023static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
reed4c21dc52015-06-25 12:32:03 -070024 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
Mike Kleinea3f0142019-03-20 11:12:10 -050025 return ToolUtils::makeSurface(root, info);
reed4c21dc52015-06-25 12:32:03 -070026}
27
reed9ce9d672016-03-17 10:51:11 -070028static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) {
reed@google.comb6705252011-09-06 19:23:41 +000029 const int kFixed = 28;
30 const int kStretchy = 8;
31 const int kSize = 2*kFixed + kStretchy;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000032
reede8f30622016-03-23 18:59:25 -070033 auto surface(make_surface(root, kSize));
reed4c21dc52015-06-25 12:32:03 -070034 SkCanvas* canvas = surface->getCanvas();
rmistry@google.comd6176b02012-08-23 18:14:13 +000035
reed@google.comb6705252011-09-06 19:23:41 +000036 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
37 const SkScalar strokeWidth = SkIntToScalar(6);
38 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
rmistry@google.comd6176b02012-08-23 18:14:13 +000039
reed@google.comb6705252011-09-06 19:23:41 +000040 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
rmistry@google.comd6176b02012-08-23 18:14:13 +000041
reed@google.comb6705252011-09-06 19:23:41 +000042 SkPaint paint;
43 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
reed@google.comb6705252011-09-06 19:23:41 +000045 paint.setColor(0xFFFF0000);
reed4c21dc52015-06-25 12:32:03 -070046 canvas->drawRoundRect(r, radius, radius, paint);
reed@google.comb6705252011-09-06 19:23:41 +000047 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
48 paint.setColor(0x8800FF00);
reed4c21dc52015-06-25 12:32:03 -070049 canvas->drawRect(r, paint);
reed@google.comb6705252011-09-06 19:23:41 +000050 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
51 paint.setColor(0x880000FF);
reed4c21dc52015-06-25 12:32:03 -070052 canvas->drawRect(r, paint);
53
reed9ce9d672016-03-17 10:51:11 -070054 return surface->makeImageSnapshot();
reed@google.comb6705252011-09-06 19:23:41 +000055}
56
reed4c21dc52015-06-25 12:32:03 -070057static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
58 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
59 bm->allocPixels(info);
60 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
61}
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed4c21dc52015-06-25 12:32:03 -070063class NinePatchStretchGM : public skiagm::GM {
reed@google.comb6705252011-09-06 19:23:41 +000064public:
reed9ce9d672016-03-17 10:51:11 -070065 sk_sp<SkImage> fImage;
66 SkBitmap fBitmap;
67 SkIRect fCenter;
reed@google.comb6705252011-09-06 19:23:41 +000068
rmistry@google.comd6176b02012-08-23 18:14:13 +000069 NinePatchStretchGM() {}
reed@google.comb6705252011-09-06 19:23:41 +000070
71protected:
reed4c21dc52015-06-25 12:32:03 -070072 SkString onShortName() override {
reed@google.comb6705252011-09-06 19:23:41 +000073 return SkString("ninepatch-stretch");
74 }
75
reed4c21dc52015-06-25 12:32:03 -070076 SkISize onISize() override {
joshualitt33a5fce2015-11-18 13:28:51 -080077 return SkISize::Make(760, 800);
reed@google.comb6705252011-09-06 19:23:41 +000078 }
79
reed4c21dc52015-06-25 12:32:03 -070080 void onDraw(SkCanvas* canvas) override {
Brian Salomon54268502018-05-24 13:22:01 -040081 if (nullptr == fBitmap.pixelRef() || !fImage->isValid(canvas->getGrContext())) {
reed9ce9d672016-03-17 10:51:11 -070082 fImage = make_image(canvas, &fCenter);
83 image_to_bitmap(fImage.get(), &fBitmap);
reed4c21dc52015-06-25 12:32:03 -070084 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000085
reed@google.comb6705252011-09-06 19:23:41 +000086 // amount of bm that should not be stretched (unless we have to)
reed4c21dc52015-06-25 12:32:03 -070087 const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width());
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
Hal Canaryfafe1352017-04-11 12:12:02 -040089 const SkSize size[] = {
reed@google.comb6705252011-09-06 19:23:41 +000090 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
91 { fixed * 4 / 5, fixed * 4 }, // shrink in X
92 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
93 { fixed * 4, fixed * 4 }
94 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000095
halcanary96fcdcc2015-08-27 07:41:13 -070096 canvas->drawBitmap(fBitmap, 10, 10, nullptr);
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
reed@google.comb6705252011-09-06 19:23:41 +000098 SkScalar x = SkIntToScalar(100);
99 SkScalar y = SkIntToScalar(100);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100
reed@google.comb6705252011-09-06 19:23:41 +0000101 SkPaint paint;
joshualitt33a5fce2015-11-18 13:28:51 -0800102 for (int filter = 0; filter < 2; filter++) {
103 paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
104 canvas->translate(0, filter * SkIntToScalar(400));
105 for (int iy = 0; iy < 2; ++iy) {
106 for (int ix = 0; ix < 2; ++ix) {
107 int i = ix * 2 + iy;
108 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
109 size[i].width(), size[i].height());
110 canvas->drawBitmapNine(fBitmap, fCenter, r, &paint);
reed9ce9d672016-03-17 10:51:11 -0700111 canvas->drawImageNine(fImage.get(), fCenter, r.makeOffset(360, 0), &paint);
joshualitt33a5fce2015-11-18 13:28:51 -0800112 }
reed@google.comb6705252011-09-06 19:23:41 +0000113 }
114 }
115 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
reed@google.comb6705252011-09-06 19:23:41 +0000117private:
reed4c21dc52015-06-25 12:32:03 -0700118 typedef skiagm::GM INHERITED;
reed@google.comb6705252011-09-06 19:23:41 +0000119};
reed4c21dc52015-06-25 12:32:03 -0700120DEF_GM( return new NinePatchStretchGM; )