blob: 197f6938ef65ee0061e5ff90f98e6dd9c7f43116 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkImage.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/private/SkMalloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/ToolUtils.h"
msarettc573a402016-08-02 08:05:56 -070024
msarett71df2d72016-09-30 12:41:42 -070025static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int padTop,
26 int padRight, int padBottom) {
27 SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + padTop + padBottom);
Mike Kleinea3f0142019-03-20 11:12:10 -050028 return ToolUtils::makeSurface(root, info);
msarettc573a402016-08-02 08:05:56 -070029}
30
msarett71df2d72016-09-30 12:41:42 -070031static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop,
32 int padRight, int padBottom) {
msarettc573a402016-08-02 08:05:56 -070033 const int kCap = 28;
34 const int kMid = 8;
35 const int kSize = 2*kCap + 3*kMid;
36
msarett71df2d72016-09-30 12:41:42 -070037 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
msarettc573a402016-08-02 08:05:56 -070038 SkCanvas* canvas = surface->getCanvas();
msarett71df2d72016-09-30 12:41:42 -070039 canvas->translate((float) padLeft, (float) padTop);
msarettc573a402016-08-02 08:05:56 -070040
41 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
42 const SkScalar strokeWidth = SkIntToScalar(6);
43 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
44
msarett71df2d72016-09-30 12:41:42 -070045 xDivs[0] = kCap + padLeft;
46 yDivs[0] = kCap + padTop;
47 xDivs[1] = kCap + kMid + padLeft;
48 yDivs[1] = kCap + kMid + padTop;
49 xDivs[2] = kCap + 2 * kMid + padLeft;
50 yDivs[2] = kCap + 2 * kMid + padTop;
51 xDivs[3] = kCap + 3 * kMid + padLeft;
52 yDivs[3] = kCap + 3 * kMid + padTop;
msarettc573a402016-08-02 08:05:56 -070053
54 SkPaint paint;
55 paint.setAntiAlias(true);
56
57 paint.setColor(0xFFFFFF00);
58 canvas->drawRoundRect(r, radius, radius, paint);
59
60 r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
61 paint.setColor(0x8800FF00);
62 canvas->drawRect(r, paint);
63 r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
64 paint.setColor(0x880000FF);
65 canvas->drawRect(r, paint);
66 r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
67 paint.setColor(0x88FF00FF);
68 canvas->drawRect(r, paint);
69
70 r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid));
71 paint.setColor(0x8800FF00);
72 canvas->drawRect(r, paint);
73 r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
74 paint.setColor(0x880000FF);
75 canvas->drawRect(r, paint);
76 r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
77 paint.setColor(0x88FF00FF);
78 canvas->drawRect(r, paint);
79
80 return surface->makeImageSnapshot();
81}
82
83static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
84 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
85 bm->allocPixels(info);
86 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
87}
88
89/**
90 * This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
91 * than nine patches.
92 */
93class LatticeGM : public skiagm::GM {
94public:
95 LatticeGM() {}
96
97protected:
98 SkString onShortName() override {
99 return SkString("lattice");
100 }
101
102 SkISize onISize() override {
msarett71df2d72016-09-30 12:41:42 -0700103 return SkISize::Make(800, 800);
msarettc573a402016-08-02 08:05:56 -0700104 }
105
msarett71df2d72016-09-30 12:41:42 -0700106 void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom) {
107 canvas->save();
108
msarettc573a402016-08-02 08:05:56 -0700109 int xDivs[5];
110 int yDivs[5];
msarett71df2d72016-09-30 12:41:42 -0700111 xDivs[0] = padLeft;
112 yDivs[0] = padTop;
msarettc573a402016-08-02 08:05:56 -0700113
114 SkBitmap bitmap;
msarett71df2d72016-09-30 12:41:42 -0700115 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
116 padRight, padBottom);
msarettc573a402016-08-02 08:05:56 -0700117 image_to_bitmap(image.get(), &bitmap);
118
Hal Canaryfafe1352017-04-11 12:12:02 -0400119 const SkSize size[] = {
msarettc573a402016-08-02 08:05:56 -0700120 { 50, 50, }, // shrink in both axes
121 { 50, 200, }, // shrink in X
122 { 200, 50, }, // shrink in Y
123 { 200, 200, },
124 };
125
126 canvas->drawImage(image, 10, 10, nullptr);
127
128 SkScalar x = SkIntToScalar(100);
129 SkScalar y = SkIntToScalar(100);
130
131 SkCanvas::Lattice lattice;
132 lattice.fXCount = 4;
133 lattice.fXDivs = xDivs + 1;
134 lattice.fYCount = 4;
135 lattice.fYDivs = yDivs + 1;
Stan Ilievca8c0952017-12-11 13:01:58 -0500136 lattice.fRectTypes = nullptr;
137 lattice.fColors = nullptr;
msarettc573a402016-08-02 08:05:56 -0700138
msarett71df2d72016-09-30 12:41:42 -0700139 SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop,
140 image->width() - padRight, image->height() - padBottom);
141 lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ?
142 nullptr : &bounds;
143
msarettc573a402016-08-02 08:05:56 -0700144 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->drawBitmapLattice(bitmap, lattice, r);
150 }
151 }
152
Stan Ilievca8c0952017-12-11 13:01:58 -0500153 // Provide hints about 3 solid color rects. These colors match
154 // what was already in the bitmap.
155 int fixedColorX[3] = {2, 4, 1};
156 int fixedColorY[3] = {1, 1, 2};
157 SkColor fixedColor[3] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
158 const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType,
159 kUnpremul_SkAlphaType);
160 for (int rectNum = 0; rectNum < 3; rectNum++) {
161 int srcX = xDivs[fixedColorX[rectNum]-1];
162 int srcY = yDivs[fixedColorY[rectNum]-1];
163 image->readPixels(info, &fixedColor[rectNum], 4, srcX, srcY);
164 }
165
msarettc573a402016-08-02 08:05:56 -0700166 // Include the degenerate first div. While normally the first patch is "scalable",
167 // this will mean that the first non-degenerate patch is "fixed".
168 lattice.fXCount = 5;
169 lattice.fXDivs = xDivs;
170 lattice.fYCount = 5;
171 lattice.fYDivs = yDivs;
172
msarett0764efe2016-09-02 11:24:30 -0700173 // Let's skip a few rects.
Stan Ilievca8c0952017-12-11 13:01:58 -0500174 SkCanvas::Lattice::RectType flags[36];
175 sk_bzero(flags, 36 * sizeof(SkCanvas::Lattice::RectType));
176 flags[4] = SkCanvas::Lattice::kTransparent;
177 flags[9] = SkCanvas::Lattice::kTransparent;
178 flags[12] = SkCanvas::Lattice::kTransparent;
179 flags[19] = SkCanvas::Lattice::kTransparent;
180 for (int rectNum = 0; rectNum < 3; rectNum++) {
181 flags[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
182 = SkCanvas::Lattice::kFixedColor;
183 }
184 lattice.fRectTypes = flags;
185
186 SkColor colors[36];
187 sk_bzero(colors, 36 * sizeof(SkColor));
188 for (int rectNum = 0; rectNum < 3; rectNum++) {
189 colors[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
190 = fixedColor[rectNum];
191 }
192
193 lattice.fColors = colors;
msarett0764efe2016-09-02 11:24:30 -0700194
msarettc573a402016-08-02 08:05:56 -0700195 canvas->translate(400, 0);
196 for (int iy = 0; iy < 2; ++iy) {
197 for (int ix = 0; ix < 2; ++ix) {
198 int i = ix * 2 + iy;
199 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
200 size[i].width(), size[i].height());
201 canvas->drawImageLattice(image.get(), lattice, r);
202 }
203 }
msarett71df2d72016-09-30 12:41:42 -0700204
205 canvas->restore();
206 }
207
208 void onDraw(SkCanvas* canvas) override {
209 this->onDrawHelper(canvas, 0, 0, 0, 0);
210 canvas->translate(0.0f, 400.0f);
211 this->onDrawHelper(canvas, 3, 7, 4, 11);
msarettc573a402016-08-02 08:05:56 -0700212 }
213
214private:
215 typedef skiagm::GM INHERITED;
216};
217DEF_GM( return new LatticeGM; )
Stan Ilievca8c0952017-12-11 13:01:58 -0500218
219
220// LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles.
221class LatticeGM2 : public skiagm::GM {
222public:
223 LatticeGM2() {}
224 SkString onShortName() override {
225 return SkString("lattice2");
226 }
227
228 SkISize onISize() override {
229 return SkISize::Make(800, 800);
230 }
231
232 sk_sp<SkImage> makeImage(SkCanvas* root, int padLeft, int padTop, int padRight, int padBottom) {
233 const int kSize = 80;
234 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
Brian Salomon23356442018-11-30 15:33:19 -0500235 SkCanvas* canvas = surface->getCanvas();
Stan Ilievca8c0952017-12-11 13:01:58 -0500236 SkPaint paint;
237 paint.setAntiAlias(false);
238 SkRect r;
239
240 //first line
241 r.setXYWH(0, 0, 4, 1); //4x1 green rect
242 paint.setColor(0xFF00FF00);
243 canvas->drawRect(r, paint);
244
245 r.setXYWH(4, 0, 1, 1); //1x1 blue pixel -> draws as rectangle
246 paint.setColor(0xFF0000FF);
247 canvas->drawRect(r, paint);
248
249 r.setXYWH(5, 0, kSize-5, 1); //the rest of the line is red
250 paint.setColor(0xFFFF0000);
251 canvas->drawRect(r, paint);
252
253
254 //second line -> draws as fixed color rectangles
255 r.setXYWH(0, 1, 4, 1); //4x1 red rect
256 paint.setColor(0xFFFF0000);
257 canvas->drawRect(r, paint);
258
259 r.setXYWH(4, 1, 1, 1); //1x1 blue pixel with alpha
260 paint.setColor(0x880000FF);
261 canvas->drawRect(r, paint);
262
263 r.setXYWH(5, 1, kSize-5, 1); //the rest of the line is green
264 paint.setColor(0xFF00FF00);
265 canvas->drawRect(r, paint);
266
267
268 //third line - does not draw, because it is transparent
269 r.setXYWH(0, 2, 4, kSize-2); //4x78 green rect
270 paint.setColor(0xFF00FF00);
271 canvas->drawRect(r, paint);
272
273 r.setXYWH(4, 2, 1, kSize-2); //1x78 red pixel with alpha
274 paint.setColor(0x88FF0000);
275 canvas->drawRect(r, paint);
276
277 r.setXYWH(5, 2, kSize-5, kSize-2); //the rest of the image is blue
278 paint.setColor(0xFF0000FF);
279 canvas->drawRect(r, paint);
280
281 return surface->makeImageSnapshot();
282 }
283
284 void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom,
285 SkPaint& paint) {
286 int xDivs[2] = {4, 5};
287 int yDivs[2] = {1, 2};
288
289 canvas->save();
290
291 sk_sp<SkImage> image = makeImage(canvas, padLeft, padTop, padRight, padBottom);
292
293 canvas->drawImage(image, 10, 10, nullptr);
294
295 SkCanvas::Lattice lattice;
296 lattice.fXCount = 2;
297 lattice.fXDivs = xDivs;
298 lattice.fYCount = 2;
299 lattice.fYDivs = yDivs;
300 lattice.fBounds = nullptr;
301
302 SkCanvas::Lattice::RectType flags[9];
303 sk_bzero(flags, 9 * sizeof(SkCanvas::Lattice::RectType));
304 flags[3] = SkCanvas::Lattice::kFixedColor;
305 flags[4] = SkCanvas::Lattice::kFixedColor;
306 flags[5] = SkCanvas::Lattice::kFixedColor;
307
308 flags[6] = SkCanvas::Lattice::kTransparent;
309 flags[7] = SkCanvas::Lattice::kTransparent;
310 flags[8] = SkCanvas::Lattice::kTransparent;
311 lattice.fRectTypes = flags;
312
313 SkColor colors[9] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK,
314 0xFFFF0000, 0x880000FF, 0xFF00FF00,
315 SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
316 lattice.fColors = colors;
317 paint.setColor(0xFFFFFFFF);
318 canvas->drawImageLattice(image.get(), lattice,
319 SkRect::MakeXYWH(100, 100, 200, 200), &paint);
320
321 //draw the same content with alpha
322 canvas->translate(400, 0);
323 paint.setColor(0x80000FFF);
324 canvas->drawImageLattice(image.get(), lattice,
325 SkRect::MakeXYWH(100, 100, 200, 200), &paint);
326
327 canvas->restore();
328 }
329
330 void onDraw(SkCanvas* canvas) override {
331
332 //draw a rectangle in the background with transparent pixels
333 SkPaint paint;
334 paint.setColor(0x7F123456);
335 paint.setBlendMode(SkBlendMode::kSrc);
336 canvas->drawRect( SkRect::MakeXYWH(300, 0, 300, 800), paint);
337
338 //draw image lattice with kSrcOver blending
339 paint.setBlendMode(SkBlendMode::kSrcOver);
340 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
341
342 //draw image lattice with kSrcATop blending
343 canvas->translate(0.0f, 400.0f);
344 paint.setBlendMode(SkBlendMode::kSrcATop);
345 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
346 }
347
348private:
349 typedef skiagm::GM INHERITED;
350};
351DEF_GM( return new LatticeGM2; )
352
Brian Osman0b537032018-12-26 12:16:44 -0500353// Code paths that incorporate the paint color when drawing the lattice (using an alpha image)
354DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500355 auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100));
Brian Osman0b537032018-12-26 12:16:44 -0500356 surface->getCanvas()->clear(0);
357 surface->getCanvas()->drawCircle(50, 50, 50, SkPaint());
358 auto image = surface->makeImageSnapshot();
Stan Ilievca8c0952017-12-11 13:01:58 -0500359
Brian Osman0b537032018-12-26 12:16:44 -0500360 int divs[] = { 20, 40, 60, 80 };
Stan Ilievca8c0952017-12-11 13:01:58 -0500361
Brian Osman0b537032018-12-26 12:16:44 -0500362 SkCanvas::Lattice lattice;
363 lattice.fXCount = 4;
364 lattice.fXDivs = divs;
365 lattice.fYCount = 4;
366 lattice.fYDivs = divs;
367 lattice.fRectTypes = nullptr;
368 lattice.fColors = nullptr;
369 lattice.fBounds = nullptr;
370
371 SkPaint paint;
372 paint.setColor(SK_ColorMAGENTA);
373 canvas->drawImageLattice(image.get(), lattice, SkRect::MakeWH(120, 120), &paint);
374}