2016-08-02 15:05:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 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"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
2016-09-30 19:41:42 +00:00
|
|
|
static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int padTop,
|
|
|
|
int padRight, int padBottom) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + padTop + padBottom);
|
2016-08-02 15:05:56 +00:00
|
|
|
auto surface = root->makeSurface(info);
|
|
|
|
if (!surface) {
|
|
|
|
surface = SkSurface::MakeRaster(info);
|
|
|
|
}
|
2016-09-30 19:41:42 +00:00
|
|
|
|
2016-08-02 15:05:56 +00:00
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2016-09-30 19:41:42 +00:00
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop,
|
|
|
|
int padRight, int padBottom) {
|
2016-08-02 15:05:56 +00:00
|
|
|
const int kCap = 28;
|
|
|
|
const int kMid = 8;
|
|
|
|
const int kSize = 2*kCap + 3*kMid;
|
|
|
|
|
2016-09-30 19:41:42 +00:00
|
|
|
auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
|
2016-08-02 15:05:56 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2016-09-30 19:41:42 +00:00
|
|
|
canvas->translate((float) padLeft, (float) padTop);
|
2016-08-02 15:05:56 +00:00
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
|
|
|
|
const SkScalar strokeWidth = SkIntToScalar(6);
|
|
|
|
const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
|
|
|
|
|
2016-09-30 19:41:42 +00:00
|
|
|
xDivs[0] = kCap + padLeft;
|
|
|
|
yDivs[0] = kCap + padTop;
|
|
|
|
xDivs[1] = kCap + kMid + padLeft;
|
|
|
|
yDivs[1] = kCap + kMid + padTop;
|
|
|
|
xDivs[2] = kCap + 2 * kMid + padLeft;
|
|
|
|
yDivs[2] = kCap + 2 * kMid + padTop;
|
|
|
|
xDivs[3] = kCap + 3 * kMid + padLeft;
|
|
|
|
yDivs[3] = kCap + 3 * kMid + padTop;
|
2016-08-02 15:05:56 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
paint.setColor(0xFFFFFF00);
|
|
|
|
canvas->drawRoundRect(r, radius, radius, paint);
|
|
|
|
|
|
|
|
r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
|
|
|
|
paint.setColor(0x8800FF00);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
|
|
|
|
paint.setColor(0x880000FF);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
|
|
|
|
paint.setColor(0x88FF00FF);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid));
|
|
|
|
paint.setColor(0x8800FF00);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
|
|
|
|
paint.setColor(0x880000FF);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
|
|
|
|
paint.setColor(0x88FF00FF);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
return surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
|
|
|
|
* than nine patches.
|
|
|
|
*/
|
|
|
|
class LatticeGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
LatticeGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("lattice");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2016-09-30 19:41:42 +00:00
|
|
|
return SkISize::Make(800, 800);
|
2016-08-02 15:05:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 19:41:42 +00:00
|
|
|
void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom) {
|
|
|
|
canvas->save();
|
|
|
|
|
2016-08-02 15:05:56 +00:00
|
|
|
int xDivs[5];
|
|
|
|
int yDivs[5];
|
2016-09-30 19:41:42 +00:00
|
|
|
xDivs[0] = padLeft;
|
|
|
|
yDivs[0] = padTop;
|
2016-08-02 15:05:56 +00:00
|
|
|
|
|
|
|
SkBitmap bitmap;
|
2016-09-30 19:41:42 +00:00
|
|
|
sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
|
|
|
|
padRight, padBottom);
|
2016-08-02 15:05:56 +00:00
|
|
|
image_to_bitmap(image.get(), &bitmap);
|
|
|
|
|
2017-04-11 16:12:02 +00:00
|
|
|
const SkSize size[] = {
|
2016-08-02 15:05:56 +00:00
|
|
|
{ 50, 50, }, // shrink in both axes
|
|
|
|
{ 50, 200, }, // shrink in X
|
|
|
|
{ 200, 50, }, // shrink in Y
|
|
|
|
{ 200, 200, },
|
|
|
|
};
|
|
|
|
|
|
|
|
canvas->drawImage(image, 10, 10, nullptr);
|
|
|
|
|
|
|
|
SkScalar x = SkIntToScalar(100);
|
|
|
|
SkScalar y = SkIntToScalar(100);
|
|
|
|
|
|
|
|
SkCanvas::Lattice lattice;
|
|
|
|
lattice.fXCount = 4;
|
|
|
|
lattice.fXDivs = xDivs + 1;
|
|
|
|
lattice.fYCount = 4;
|
|
|
|
lattice.fYDivs = yDivs + 1;
|
2016-09-02 18:24:30 +00:00
|
|
|
lattice.fFlags = nullptr;
|
2016-08-02 15:05:56 +00:00
|
|
|
|
2016-09-30 19:41:42 +00:00
|
|
|
SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop,
|
|
|
|
image->width() - padRight, image->height() - padBottom);
|
|
|
|
lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ?
|
|
|
|
nullptr : &bounds;
|
|
|
|
|
2016-08-02 15:05:56 +00:00
|
|
|
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 * 60, y + iy * 60,
|
|
|
|
size[i].width(), size[i].height());
|
|
|
|
canvas->drawBitmapLattice(bitmap, lattice, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include the degenerate first div. While normally the first patch is "scalable",
|
|
|
|
// this will mean that the first non-degenerate patch is "fixed".
|
|
|
|
lattice.fXCount = 5;
|
|
|
|
lattice.fXDivs = xDivs;
|
|
|
|
lattice.fYCount = 5;
|
|
|
|
lattice.fYDivs = yDivs;
|
|
|
|
|
2016-09-02 18:24:30 +00:00
|
|
|
// Let's skip a few rects.
|
|
|
|
SkCanvas::Lattice::Flags flags[36];
|
|
|
|
sk_bzero(flags, 36 * sizeof(SkCanvas::Lattice::Flags));
|
|
|
|
flags[4] = SkCanvas::Lattice::kTransparent_Flags;
|
|
|
|
flags[9] = SkCanvas::Lattice::kTransparent_Flags;
|
|
|
|
flags[12] = SkCanvas::Lattice::kTransparent_Flags;
|
|
|
|
flags[19] = SkCanvas::Lattice::kTransparent_Flags;
|
|
|
|
lattice.fFlags = flags;
|
|
|
|
|
2016-08-02 15:05:56 +00:00
|
|
|
canvas->translate(400, 0);
|
|
|
|
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 * 60, y + iy * 60,
|
|
|
|
size[i].width(), size[i].height());
|
|
|
|
canvas->drawImageLattice(image.get(), lattice, r);
|
|
|
|
}
|
|
|
|
}
|
2016-09-30 19:41:42 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
this->onDrawHelper(canvas, 0, 0, 0, 0);
|
|
|
|
canvas->translate(0.0f, 400.0f);
|
|
|
|
this->onDrawHelper(canvas, 3, 7, 4, 11);
|
2016-08-02 15:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new LatticeGM; )
|