blob: 4074add1bbec475e2070a4aad1b16973e67f49a5 [file] [log] [blame]
reed@google.comb6705252011-09-06 19:23:41 +00001/*
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.h"
reed4c21dc52015-06-25 12:32:03 -07009#include "SkSurface.h"
reed@google.comb6705252011-09-06 19:23:41 +000010
reed4c21dc52015-06-25 12:32:03 -070011static SkSurface* make_surface(SkCanvas* root, int N) {
12 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
13 SkSurface* surface = root->newSurface(info);
14 if (!surface) {
15 surface = SkSurface::NewRaster(info);
16 }
17 return surface;
18}
19
20static SkImage* make_image(SkCanvas* root, SkIRect* center) {
reed@google.comb6705252011-09-06 19:23:41 +000021 const int kFixed = 28;
22 const int kStretchy = 8;
23 const int kSize = 2*kFixed + kStretchy;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000024
reed4c21dc52015-06-25 12:32:03 -070025 SkAutoTUnref<SkSurface> surface(make_surface(root, kSize));
26 SkCanvas* canvas = surface->getCanvas();
rmistry@google.comd6176b02012-08-23 18:14:13 +000027
reed@google.comb6705252011-09-06 19:23:41 +000028 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
29 const SkScalar strokeWidth = SkIntToScalar(6);
30 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
rmistry@google.comd6176b02012-08-23 18:14:13 +000031
reed@google.comb6705252011-09-06 19:23:41 +000032 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
reed@google.comb6705252011-09-06 19:23:41 +000034 SkPaint paint;
35 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
reed@google.comb6705252011-09-06 19:23:41 +000037 paint.setColor(0xFFFF0000);
reed4c21dc52015-06-25 12:32:03 -070038 canvas->drawRoundRect(r, radius, radius, paint);
reed@google.comb6705252011-09-06 19:23:41 +000039 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
40 paint.setColor(0x8800FF00);
reed4c21dc52015-06-25 12:32:03 -070041 canvas->drawRect(r, paint);
reed@google.comb6705252011-09-06 19:23:41 +000042 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
43 paint.setColor(0x880000FF);
reed4c21dc52015-06-25 12:32:03 -070044 canvas->drawRect(r, paint);
45
46 return surface->newImageSnapshot();
reed@google.comb6705252011-09-06 19:23:41 +000047}
48
reed4c21dc52015-06-25 12:32:03 -070049static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
50 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
51 bm->allocPixels(info);
52 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
53}
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed4c21dc52015-06-25 12:32:03 -070055class NinePatchStretchGM : public skiagm::GM {
reed@google.comb6705252011-09-06 19:23:41 +000056public:
reed4c21dc52015-06-25 12:32:03 -070057 SkAutoTUnref<SkImage> fImage;
58 SkBitmap fBitmap;
59 SkIRect fCenter;
reed@google.comb6705252011-09-06 19:23:41 +000060
rmistry@google.comd6176b02012-08-23 18:14:13 +000061 NinePatchStretchGM() {}
reed@google.comb6705252011-09-06 19:23:41 +000062
63protected:
reed4c21dc52015-06-25 12:32:03 -070064 SkString onShortName() override {
reed@google.comb6705252011-09-06 19:23:41 +000065 return SkString("ninepatch-stretch");
66 }
67
reed4c21dc52015-06-25 12:32:03 -070068 SkISize onISize() override {
69 return SkISize::Make(760, 400);
reed@google.comb6705252011-09-06 19:23:41 +000070 }
71
reed4c21dc52015-06-25 12:32:03 -070072 void onDraw(SkCanvas* canvas) override {
halcanary96fcdcc2015-08-27 07:41:13 -070073 if (nullptr == fBitmap.pixelRef()) {
reed4c21dc52015-06-25 12:32:03 -070074 fImage.reset(make_image(canvas, &fCenter));
75 image_to_bitmap(fImage, &fBitmap);
76 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000077
reed@google.comb6705252011-09-06 19:23:41 +000078 // amount of bm that should not be stretched (unless we have to)
reed4c21dc52015-06-25 12:32:03 -070079 const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width());
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
reed@google.comb6705252011-09-06 19:23:41 +000081 const SkTSize<SkScalar> size[] = {
82 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
83 { fixed * 4 / 5, fixed * 4 }, // shrink in X
84 { fixed * 4, fixed * 4 / 5 }, // shrink in Y
85 { fixed * 4, fixed * 4 }
86 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
halcanary96fcdcc2015-08-27 07:41:13 -070088 canvas->drawBitmap(fBitmap, 10, 10, nullptr);
rmistry@google.comd6176b02012-08-23 18:14:13 +000089
reed@google.comb6705252011-09-06 19:23:41 +000090 SkScalar x = SkIntToScalar(100);
91 SkScalar y = SkIntToScalar(100);
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
reed@google.comb6705252011-09-06 19:23:41 +000093 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070094 paint.setFilterQuality(kLow_SkFilterQuality);
rmistry@google.comd6176b02012-08-23 18:14:13 +000095
reed@google.comb6705252011-09-06 19:23:41 +000096 for (int iy = 0; iy < 2; ++iy) {
97 for (int ix = 0; ix < 2; ++ix) {
98 int i = ix * 2 + iy;
99 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
100 size[i].width(), size[i].height());
reed4c21dc52015-06-25 12:32:03 -0700101 canvas->drawBitmapNine(fBitmap, fCenter, r, &paint);
102 canvas->drawImageNine(fImage, fCenter, r.makeOffset(360, 0), &paint);
103
reed@google.comb6705252011-09-06 19:23:41 +0000104 }
105 }
106 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
reed@google.comb6705252011-09-06 19:23:41 +0000108private:
reed4c21dc52015-06-25 12:32:03 -0700109 typedef skiagm::GM INHERITED;
reed@google.comb6705252011-09-06 19:23:41 +0000110};
reed4c21dc52015-06-25 12:32:03 -0700111DEF_GM( return new NinePatchStretchGM; )
reed@google.comb6705252011-09-06 19:23:41 +0000112