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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2015-06-25 19:32:03 +00:00
|
|
|
#include "SkSurface.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);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface = root->makeSurface(info);
|
2015-06-25 19:32:03 +00:00
|
|
|
if (!surface) {
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRaster(info);
|
2015-06-25 19:32:03 +00:00
|
|
|
}
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
|
|
|
|
bm->allocPixels(info);
|
|
|
|
image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
|
|
|
|
}
|
2012-08-23 18:14:13 +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;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
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 {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fBitmap.pixelRef()) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = make_image(canvas, &fCenter);
|
|
|
|
image_to_bitmap(fImage.get(), &fBitmap);
|
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)
|
2015-06-25 19:32:03 +00:00
|
|
|
const SkScalar fixed = SkIntToScalar(fBitmap.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
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 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());
|
|
|
|
canvas->drawBitmapNine(fBitmap, fCenter, r, &paint);
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImageNine(fImage.get(), fCenter, r.makeOffset(360, 0), &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:
|
2015-06-25 19:32:03 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2011-09-06 19:23:41 +00:00
|
|
|
};
|
2015-06-25 19:32:03 +00:00
|
|
|
DEF_GM( return new NinePatchStretchGM; )
|