Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ninepatchstretch.cpp
Go to the documentation of this file.
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"
14#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
20#include "tools/ToolUtils.h"
21
26
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
37 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
38
39 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
40
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
57public:
60
62
63protected:
64 SkString getName() const override { return SkString("ninepatch-stretch"); }
65
66 SkISize getISize() override { return SkISize::Make(760, 800); }
67
68 void onDraw(SkCanvas* canvas) override {
69 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
70 fImage = make_image(canvas, &fCenter);
71 }
72
73 // amount of bm that should not be stretched (unless we have to)
74 const SkScalar fixed = SkIntToScalar(fImage->width() - fCenter.width());
75
76 const SkSize size[] = {
77 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
78 { fixed * 4 / 5, fixed * 4 }, // shrink in X
79 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
80 { fixed * 4, fixed * 4 }
81 };
82
83 canvas->drawImage(fImage, 10, 10);
84
87
90 for (int iy = 0; iy < 2; ++iy) {
91 for (int ix = 0; ix < 2; ++ix) {
92 int i = ix * 2 + iy;
93 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
94 size[i].width(), size[i].height());
95 canvas->drawImageNine(fImage.get(), fCenter, r, fm);
96 }
97 }
98 canvas->translate(0, 400);
99 }
100 }
101
102private:
103 using INHERITED = skiagm::GM;
104};
105DEF_GM( return new NinePatchStretchGM; )
static const int strokeWidth
Definition BlurTest.cpp:60
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define SkIntToScalar(x)
Definition SkScalar.h:57
static SkScalar center(float pos0, float pos1)
#define N
Definition beziers.cpp:19
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
sk_sp< SkImage > fImage
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void drawImageNine(const SkImage *image, const SkIRect &center, const SkRect &dst, SkFilterMode filter, const SkPaint *paint=nullptr)
virtual GrRecordingContext * recordingContext() const
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
int width() const
Definition SkImage.h:285
virtual bool isValid(GrRecordingContext *context) const =0
T * get() const
Definition SkRefCnt.h:303
SkScalar width()
Definition gm.h:159
SkScalar height()
Definition gm.h:162
static constexpr int kSize
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
static sk_sp< SkImage > make_image()
Definition mipmap.cpp:21
double y
double x
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
static sk_sp< SkSurface > make_surface(SkCanvas *root, int N)
constexpr int32_t width() const
Definition SkRect.h:158
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
void setXYWH(float x, float y, float width, float height)
Definition SkRect.h:931
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609