blob: c16b6a9892cfd62d202e3f6edc9a912a016b38b2 [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 -070057class NinePatchStretchGM : public skiagm::GM {
reed@google.comb6705252011-09-06 19:23:41 +000058public:
reed9ce9d672016-03-17 10:51:11 -070059 sk_sp<SkImage> fImage;
reed9ce9d672016-03-17 10:51:11 -070060 SkIRect fCenter;
reed@google.comb6705252011-09-06 19:23:41 +000061
rmistry@google.comd6176b02012-08-23 18:14:13 +000062 NinePatchStretchGM() {}
reed@google.comb6705252011-09-06 19:23:41 +000063
64protected:
reed4c21dc52015-06-25 12:32:03 -070065 SkString onShortName() override {
reed@google.comb6705252011-09-06 19:23:41 +000066 return SkString("ninepatch-stretch");
67 }
68
reed4c21dc52015-06-25 12:32:03 -070069 SkISize onISize() override {
joshualitt33a5fce2015-11-18 13:28:51 -080070 return SkISize::Make(760, 800);
reed@google.comb6705252011-09-06 19:23:41 +000071 }
72
reed4c21dc52015-06-25 12:32:03 -070073 void onDraw(SkCanvas* canvas) override {
Mike Reedfdf94042020-03-10 21:45:34 -040074 if (!fImage || !fImage->isValid(canvas->getGrContext())) {
reed9ce9d672016-03-17 10:51:11 -070075 fImage = make_image(canvas, &fCenter);
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)
Mike Reedfdf94042020-03-10 21:45:34 -040079 const SkScalar fixed = SkIntToScalar(fImage->width() - fCenter.width());
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
Hal Canaryfafe1352017-04-11 12:12:02 -040081 const SkSize size[] = {
reed@google.comb6705252011-09-06 19:23:41 +000082 { 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
Mike Reedfdf94042020-03-10 21:45:34 -040088 canvas->drawImage(fImage, 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());
Mike Reedfdf94042020-03-10 21:45:34 -0400102 canvas->drawImageNine(fImage.get(), fCenter, r, &paint);
joshualitt33a5fce2015-11-18 13:28:51 -0800103 }
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:
reed4c21dc52015-06-25 12:32:03 -0700109 typedef skiagm::GM INHERITED;
reed@google.comb6705252011-09-06 19:23:41 +0000110};
reed4c21dc52015-06-25 12:32:03 -0700111DEF_GM( return new NinePatchStretchGM; )