| 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/SkImage.h" |
| 12 |
#include "include/core/SkImageInfo.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkSurface.h" |
| 20 |
#include "tools/ToolUtils.h" |
| 21 |
|
| 22 |
static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) { |
| 23 |
SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); |
| 24 |
return ToolUtils::makeSurface(root, info); |
| 25 |
} |
| 26 |
|
| 27 |
static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) { |
| 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 |
|
| 35 |
SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 36 |
const SkScalar strokeWidth = SkIntToScalar(6); |
| 37 |
const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; |
| 38 |
|
| 39 |
center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); |
| 40 |
|
| 41 |
SkPaint paint; |
| 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 |
|
| 56 |
class NinePatchStretchGM : public skiagm::GM { |
| 57 |
public: |
| 58 |
sk_sp<SkImage> fImage; |
| 59 |
SkIRect fCenter; |
| 60 |
|
| 61 |
NinePatchStretchGM() {} |
| 62 |
|
| 63 |
protected: |
| 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->baseRecorder())) { |
| 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 |
|
| 85 |
SkScalar x = SkIntToScalar(100); |
| 86 |
SkScalar y = SkIntToScalar(100); |
| 87 |
|
| 88 |
SkPaint paint; |
| 89 |
for (auto fm : {SkFilterMode::kLinear, SkFilterMode::kNearest}) { |
| 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 |
|
| 102 |
private: |
| 103 |
using INHERITED = skiagm::GM; |
| 104 |
}; |
| 105 |
DEF_GM( return new NinePatchStretchGM; ) |