2011-09-06 19:23:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFilterQuality.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2011-09-06 19:23:41 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
|
2015-06-25 19:32:03 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
|
2019-03-20 16:12:10 +00:00
|
|
|
return ToolUtils::makeSurface(root, info);
|
2015-06-25 19:32:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) {
|
2011-09-06 19:23:41 +00:00
|
|
|
const int kFixed = 28;
|
|
|
|
const int kStretchy = 8;
|
|
|
|
const int kSize = 2*kFixed + kStretchy;
|
2012-08-02 14:03:32 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(make_surface(root, kSize));
|
2015-06-25 19:32:03 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
|
|
|
|
const SkScalar strokeWidth = SkIntToScalar(6);
|
|
|
|
const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
paint.setColor(0xFFFF0000);
|
2015-06-25 19:32:03 +00:00
|
|
|
canvas->drawRoundRect(r, radius, radius, paint);
|
2011-09-06 19:23:41 +00:00
|
|
|
r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
|
|
|
|
paint.setColor(0x8800FF00);
|
2015-06-25 19:32:03 +00:00
|
|
|
canvas->drawRect(r, paint);
|
2011-09-06 19:23:41 +00:00
|
|
|
r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
|
|
|
|
paint.setColor(0x880000FF);
|
2015-06-25 19:32:03 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2011-09-06 19:23:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 19:32:03 +00:00
|
|
|
class NinePatchStretchGM : public skiagm::GM {
|
2011-09-06 19:23:41 +00:00
|
|
|
public:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
|
|
|
SkIRect fCenter;
|
2011-09-06 19:23:41 +00:00
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
NinePatchStretchGM() {}
|
2011-09-06 19:23:41 +00:00
|
|
|
|
|
|
|
protected:
|
2015-06-25 19:32:03 +00:00
|
|
|
SkString onShortName() override {
|
2011-09-06 19:23:41 +00:00
|
|
|
return SkString("ninepatch-stretch");
|
|
|
|
}
|
|
|
|
|
2015-06-25 19:32:03 +00:00
|
|
|
SkISize onISize() override {
|
2015-11-18 21:28:51 +00:00
|
|
|
return SkISize::Make(760, 800);
|
2011-09-06 19:23:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 19:32:03 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2020-07-10 15:27:43 +00:00
|
|
|
if (!fImage || !fImage->isValid(canvas->recordingContext())) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = make_image(canvas, &fCenter);
|
2015-06-25 19:32:03 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
// amount of bm that should not be stretched (unless we have to)
|
2020-03-11 01:45:34 +00:00
|
|
|
const SkScalar fixed = SkIntToScalar(fImage->width() - fCenter.width());
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2017-04-11 16:12:02 +00:00
|
|
|
const SkSize size[] = {
|
2011-09-06 19:23:41 +00:00
|
|
|
{ fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
|
|
|
|
{ fixed * 4 / 5, fixed * 4 }, // shrink in X
|
|
|
|
{ fixed * 4, fixed * 4 / 5 }, // shrink in Y
|
|
|
|
{ fixed * 4, fixed * 4 }
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2020-03-11 01:45:34 +00:00
|
|
|
canvas->drawImage(fImage, 10, 10, nullptr);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkScalar x = SkIntToScalar(100);
|
|
|
|
SkScalar y = SkIntToScalar(100);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkPaint paint;
|
2015-11-18 21:28:51 +00:00
|
|
|
for (int filter = 0; filter < 2; filter++) {
|
|
|
|
paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
|
|
|
|
canvas->translate(0, filter * SkIntToScalar(400));
|
|
|
|
for (int iy = 0; iy < 2; ++iy) {
|
|
|
|
for (int ix = 0; ix < 2; ++ix) {
|
|
|
|
int i = ix * 2 + iy;
|
|
|
|
SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
|
|
|
|
size[i].width(), size[i].height());
|
2020-03-11 01:45:34 +00:00
|
|
|
canvas->drawImageNine(fImage.get(), fCenter, r, &paint);
|
2015-11-18 21:28:51 +00:00
|
|
|
}
|
2011-09-06 19:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2011-09-06 19:23:41 +00:00
|
|
|
};
|
2015-06-25 19:32:03 +00:00
|
|
|
DEF_GM( return new NinePatchStretchGM; )
|