blob: 919fb3588e7dde16ec073b487ec0a5e66abcc8ee [file] [log] [blame]
msarettc573a402016-08-02 08:05:56 -07001/*
2 * Copyright 2016 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"
9#include "SkSurface.h"
10
11static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
12 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
13 auto surface = root->makeSurface(info);
14 if (!surface) {
15 surface = SkSurface::MakeRaster(info);
16 }
17 return surface;
18}
19
20static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs) {
21 const int kCap = 28;
22 const int kMid = 8;
23 const int kSize = 2*kCap + 3*kMid;
24
25 auto surface(make_surface(root, kSize));
26 SkCanvas* canvas = surface->getCanvas();
27
28 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
29 const SkScalar strokeWidth = SkIntToScalar(6);
30 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
31
32 xDivs[0] = yDivs[0] = kCap;
33 xDivs[1] = yDivs[1] = kCap + kMid;
34 xDivs[2] = yDivs[2] = kCap + 2 * kMid;
35 xDivs[3] = yDivs[3] = kCap + 3 * kMid;
36
37 SkPaint paint;
38 paint.setAntiAlias(true);
39
40 paint.setColor(0xFFFFFF00);
41 canvas->drawRoundRect(r, radius, radius, paint);
42
43 r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
44 paint.setColor(0x8800FF00);
45 canvas->drawRect(r, paint);
46 r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
47 paint.setColor(0x880000FF);
48 canvas->drawRect(r, paint);
49 r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
50 paint.setColor(0x88FF00FF);
51 canvas->drawRect(r, paint);
52
53 r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid));
54 paint.setColor(0x8800FF00);
55 canvas->drawRect(r, paint);
56 r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
57 paint.setColor(0x880000FF);
58 canvas->drawRect(r, paint);
59 r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
60 paint.setColor(0x88FF00FF);
61 canvas->drawRect(r, paint);
62
63 return surface->makeImageSnapshot();
64}
65
66static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
67 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
68 bm->allocPixels(info);
69 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
70}
71
72/**
73 * This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
74 * than nine patches.
75 */
76class LatticeGM : public skiagm::GM {
77public:
78 LatticeGM() {}
79
80protected:
81 SkString onShortName() override {
82 return SkString("lattice");
83 }
84
85 SkISize onISize() override {
86 return SkISize::Make(800, 400);
87 }
88
89 void onDraw(SkCanvas* canvas) override {
90 int xDivs[5];
91 int yDivs[5];
92 xDivs[0] = 0;
93 yDivs[0] = 0;
94
95 SkBitmap bitmap;
96 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1);
97 image_to_bitmap(image.get(), &bitmap);
98
99 const SkTSize<SkScalar> size[] = {
100 { 50, 50, }, // shrink in both axes
101 { 50, 200, }, // shrink in X
102 { 200, 50, }, // shrink in Y
103 { 200, 200, },
104 };
105
106 canvas->drawImage(image, 10, 10, nullptr);
107
108 SkScalar x = SkIntToScalar(100);
109 SkScalar y = SkIntToScalar(100);
110
111 SkCanvas::Lattice lattice;
112 lattice.fXCount = 4;
113 lattice.fXDivs = xDivs + 1;
114 lattice.fYCount = 4;
115 lattice.fYDivs = yDivs + 1;
msarett0764efe2016-09-02 11:24:30 -0700116 lattice.fFlags = nullptr;
msarettc573a402016-08-02 08:05:56 -0700117
118 for (int iy = 0; iy < 2; ++iy) {
119 for (int ix = 0; ix < 2; ++ix) {
120 int i = ix * 2 + iy;
121 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
122 size[i].width(), size[i].height());
123 canvas->drawBitmapLattice(bitmap, lattice, r);
124 }
125 }
126
127 // Include the degenerate first div. While normally the first patch is "scalable",
128 // this will mean that the first non-degenerate patch is "fixed".
129 lattice.fXCount = 5;
130 lattice.fXDivs = xDivs;
131 lattice.fYCount = 5;
132 lattice.fYDivs = yDivs;
133
msarett0764efe2016-09-02 11:24:30 -0700134 // Let's skip a few rects.
135 SkCanvas::Lattice::Flags flags[36];
136 sk_bzero(flags, 36 * sizeof(SkCanvas::Lattice::Flags));
137 flags[4] = SkCanvas::Lattice::kTransparent_Flags;
138 flags[9] = SkCanvas::Lattice::kTransparent_Flags;
139 flags[12] = SkCanvas::Lattice::kTransparent_Flags;
140 flags[19] = SkCanvas::Lattice::kTransparent_Flags;
141 lattice.fFlags = flags;
142
msarettc573a402016-08-02 08:05:56 -0700143 canvas->translate(400, 0);
144 for (int iy = 0; iy < 2; ++iy) {
145 for (int ix = 0; ix < 2; ++ix) {
146 int i = ix * 2 + iy;
147 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
148 size[i].width(), size[i].height());
149 canvas->drawImageLattice(image.get(), lattice, r);
150 }
151 }
152 }
153
154private:
155 typedef skiagm::GM INHERITED;
156};
157DEF_GM( return new LatticeGM; )