1 /*
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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkSurface.h"
20 #include "tools/ToolUtils.h"
21
make_surface(SkCanvas * root,int N)22 static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
23 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
24 return ToolUtils::makeSurface(root, info);
25 }
26
make_image(SkCanvas * root,SkIRect * center)27 static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) {
28 const int kFixed = 28;
29 const int kStretchy = 8;
30 const int kSize = 2*kFixed + kStretchy;
31
32 auto surface(make_surface(root, kSize));
33 SkCanvas* canvas = surface->getCanvas();
34
35 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
36 const SkScalar strokeWidth = SkIntToScalar(6);
37 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
38
39 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
40
41 SkPaint paint;
42 paint.setAntiAlias(true);
43
44 paint.setColor(0xFFFF0000);
45 canvas->drawRoundRect(r, radius, radius, paint);
46 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
47 paint.setColor(0x8800FF00);
48 canvas->drawRect(r, paint);
49 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
50 paint.setColor(0x880000FF);
51 canvas->drawRect(r, paint);
52
53 return surface->makeImageSnapshot();
54 }
55
56 class NinePatchStretchGM : public skiagm::GM {
57 public:
58 sk_sp<SkImage> fImage;
59 SkIRect fCenter;
60
NinePatchStretchGM()61 NinePatchStretchGM() {}
62
63 protected:
onShortName()64 SkString onShortName() override {
65 return SkString("ninepatch-stretch");
66 }
67
onISize()68 SkISize onISize() override {
69 return SkISize::Make(760, 800);
70 }
71
onDraw(SkCanvas * canvas)72 void onDraw(SkCanvas* canvas) override {
73 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
74 fImage = make_image(canvas, &fCenter);
75 }
76
77 // amount of bm that should not be stretched (unless we have to)
78 const SkScalar fixed = SkIntToScalar(fImage->width() - fCenter.width());
79
80 const SkSize size[] = {
81 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
82 { fixed * 4 / 5, fixed * 4 }, // shrink in X
83 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
84 { fixed * 4, fixed * 4 }
85 };
86
87 canvas->drawImage(fImage, 10, 10);
88
89 SkScalar x = SkIntToScalar(100);
90 SkScalar y = SkIntToScalar(100);
91
92 SkPaint paint;
93 for (auto fm : {SkFilterMode::kLinear, SkFilterMode::kNearest}) {
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->drawImageNine(fImage.get(), fCenter, r, fm);
100 }
101 }
102 canvas->translate(0, 400);
103 }
104 }
105
106 private:
107 using INHERITED = skiagm::GM;
108 };
109 DEF_GM( return new NinePatchStretchGM; )
110