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