blob: 06f33559229ddf81ac94533feaca3cbd3404bff8 [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"
Adlai Hollerbcfc5542020-08-27 12:44:07 -040022#include "include/gpu/GrDirectContext.h"
Kevin Lubick19936eb2023-01-05 09:00:37 -050023#include "include/private/base/SkMalloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/ToolUtils.h"
msarettc573a402016-08-02 08:05:56 -070025
msarett71df2d72016-09-30 12:41:42 -070026static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int padTop,
27 int padRight, int padBottom) {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + padTop + padBottom);
Mike Kleinea3f0142019-03-20 11:12:10 -050029 return ToolUtils::makeSurface(root, info);
msarettc573a402016-08-02 08:05:56 -070030}
31
msarett71df2d72016-09-30 12:41:42 -070032static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop,
33 int padRight, int padBottom) {
msarettc573a402016-08-02 08:05:56 -070034 const int kCap = 28;
35 const int kMid = 8;
36 const int kSize = 2*kCap + 3*kMid;
37
msarett71df2d72016-09-30 12:41:42 -070038 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
msarettc573a402016-08-02 08:05:56 -070039 SkCanvas* canvas = surface->getCanvas();
msarett71df2d72016-09-30 12:41:42 -070040 canvas->translate((float) padLeft, (float) padTop);
msarettc573a402016-08-02 08:05:56 -070041
42 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
43 const SkScalar strokeWidth = SkIntToScalar(6);
44 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
45
msarett71df2d72016-09-30 12:41:42 -070046 xDivs[0] = kCap + padLeft;
47 yDivs[0] = kCap + padTop;
48 xDivs[1] = kCap + kMid + padLeft;
49 yDivs[1] = kCap + kMid + padTop;
50 xDivs[2] = kCap + 2 * kMid + padLeft;
51 yDivs[2] = kCap + 2 * kMid + padTop;
52 xDivs[3] = kCap + 3 * kMid + padLeft;
53 yDivs[3] = kCap + 3 * kMid + padTop;
msarettc573a402016-08-02 08:05:56 -070054
55 SkPaint paint;
56 paint.setAntiAlias(true);
57
58 paint.setColor(0xFFFFFF00);
59 canvas->drawRoundRect(r, radius, radius, paint);
60
61 r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
62 paint.setColor(0x8800FF00);
63 canvas->drawRect(r, paint);
64 r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
65 paint.setColor(0x880000FF);
66 canvas->drawRect(r, paint);
67 r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScalar(kSize));
68 paint.setColor(0x88FF00FF);
69 canvas->drawRect(r, paint);
70
71 r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid));
72 paint.setColor(0x8800FF00);
73 canvas->drawRect(r, paint);
74 r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
75 paint.setColor(0x880000FF);
76 canvas->drawRect(r, paint);
77 r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScalar(kMid));
78 paint.setColor(0x88FF00FF);
79 canvas->drawRect(r, paint);
80
81 return surface->makeImageSnapshot();
82}
83
Adlai Hollerbcfc5542020-08-27 12:44:07 -040084static void image_to_bitmap(GrDirectContext* dContext, const SkImage* image, SkBitmap* bm) {
msarettc573a402016-08-02 08:05:56 -070085 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
86 bm->allocPixels(info);
Adlai Hollerbcfc5542020-08-27 12:44:07 -040087 image->readPixels(dContext, info, bm->getPixels(), bm->rowBytes(), 0, 0);
msarettc573a402016-08-02 08:05:56 -070088}
89
90/**
91 * This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
92 * than nine patches.
93 */
94class LatticeGM : public skiagm::GM {
95public:
96 LatticeGM() {}
97
98protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000099 SkString getName() const override { return SkString("lattice"); }
msarettc573a402016-08-02 08:05:56 -0700100
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000101 SkISize getISize() override { return SkISize::Make(800, 800); }
msarettc573a402016-08-02 08:05:56 -0700102
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400103 void onDrawHelper(GrDirectContext* dContext, SkCanvas* canvas, int padLeft, int padTop,
104 int padRight, int padBottom) {
msarett71df2d72016-09-30 12:41:42 -0700105 canvas->save();
106
msarettc573a402016-08-02 08:05:56 -0700107 int xDivs[5];
108 int yDivs[5];
msarett71df2d72016-09-30 12:41:42 -0700109 xDivs[0] = padLeft;
110 yDivs[0] = padTop;
msarettc573a402016-08-02 08:05:56 -0700111
112 SkBitmap bitmap;
msarett71df2d72016-09-30 12:41:42 -0700113 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
114 padRight, padBottom);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400115 image_to_bitmap(dContext, image.get(), &bitmap);
msarettc573a402016-08-02 08:05:56 -0700116
Hal Canaryfafe1352017-04-11 12:12:02 -0400117 const SkSize size[] = {
msarettc573a402016-08-02 08:05:56 -0700118 { 50, 50, }, // shrink in both axes
119 { 50, 200, }, // shrink in X
120 { 200, 50, }, // shrink in Y
121 { 200, 200, },
122 };
123
Mike Reed8d29ab62021-01-23 18:10:39 -0500124 canvas->drawImage(image, 10, 10);
msarettc573a402016-08-02 08:05:56 -0700125
126 SkScalar x = SkIntToScalar(100);
127 SkScalar y = SkIntToScalar(100);
128
129 SkCanvas::Lattice lattice;
130 lattice.fXCount = 4;
131 lattice.fXDivs = xDivs + 1;
132 lattice.fYCount = 4;
133 lattice.fYDivs = yDivs + 1;
Stan Ilievca8c0952017-12-11 13:01:58 -0500134 lattice.fRectTypes = nullptr;
135 lattice.fColors = nullptr;
msarettc573a402016-08-02 08:05:56 -0700136
msarett71df2d72016-09-30 12:41:42 -0700137 SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop,
138 image->width() - padRight, image->height() - padBottom);
139 lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ?
140 nullptr : &bounds;
141
msarettc573a402016-08-02 08:05:56 -0700142 for (int iy = 0; iy < 2; ++iy) {
143 for (int ix = 0; ix < 2; ++ix) {
144 int i = ix * 2 + iy;
145 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
146 size[i].width(), size[i].height());
Mike Reedfdf94042020-03-10 21:45:34 -0400147 canvas->drawImageLattice(image.get(), lattice, r);
msarettc573a402016-08-02 08:05:56 -0700148 }
149 }
150
Stan Ilievca8c0952017-12-11 13:01:58 -0500151 // Provide hints about 3 solid color rects. These colors match
152 // what was already in the bitmap.
153 int fixedColorX[3] = {2, 4, 1};
154 int fixedColorY[3] = {1, 1, 2};
155 SkColor fixedColor[3] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
156 const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType,
157 kUnpremul_SkAlphaType);
158 for (int rectNum = 0; rectNum < 3; rectNum++) {
159 int srcX = xDivs[fixedColorX[rectNum]-1];
160 int srcY = yDivs[fixedColorY[rectNum]-1];
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400161 image->readPixels(dContext, info, &fixedColor[rectNum], 4, srcX, srcY);
Stan Ilievca8c0952017-12-11 13:01:58 -0500162 }
163
msarettc573a402016-08-02 08:05:56 -0700164 // Include the degenerate first div. While normally the first patch is "scalable",
165 // this will mean that the first non-degenerate patch is "fixed".
166 lattice.fXCount = 5;
167 lattice.fXDivs = xDivs;
168 lattice.fYCount = 5;
169 lattice.fYDivs = yDivs;
170
msarett0764efe2016-09-02 11:24:30 -0700171 // Let's skip a few rects.
Stan Ilievca8c0952017-12-11 13:01:58 -0500172 SkCanvas::Lattice::RectType flags[36];
173 sk_bzero(flags, 36 * sizeof(SkCanvas::Lattice::RectType));
174 flags[4] = SkCanvas::Lattice::kTransparent;
175 flags[9] = SkCanvas::Lattice::kTransparent;
176 flags[12] = SkCanvas::Lattice::kTransparent;
177 flags[19] = SkCanvas::Lattice::kTransparent;
178 for (int rectNum = 0; rectNum < 3; rectNum++) {
179 flags[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
180 = SkCanvas::Lattice::kFixedColor;
181 }
182 lattice.fRectTypes = flags;
183
184 SkColor colors[36];
185 sk_bzero(colors, 36 * sizeof(SkColor));
186 for (int rectNum = 0; rectNum < 3; rectNum++) {
187 colors[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
188 = fixedColor[rectNum];
189 }
190
191 lattice.fColors = colors;
msarett0764efe2016-09-02 11:24:30 -0700192
msarettc573a402016-08-02 08:05:56 -0700193 canvas->translate(400, 0);
194 for (int iy = 0; iy < 2; ++iy) {
195 for (int ix = 0; ix < 2; ++ix) {
196 int i = ix * 2 + iy;
197 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
198 size[i].width(), size[i].height());
199 canvas->drawImageLattice(image.get(), lattice, r);
200 }
201 }
msarett71df2d72016-09-30 12:41:42 -0700202
203 canvas->restore();
204 }
205
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400206 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
207 auto rContext = canvas->recordingContext();
208 auto dContext = GrAsDirectContext(rContext);
209 if (rContext && !dContext) {
210 *errorMsg = "not supported in ddl";
211 return DrawResult::kSkip;
212 }
213 this->onDrawHelper(dContext, canvas, 0, 0, 0, 0);
msarett71df2d72016-09-30 12:41:42 -0700214 canvas->translate(0.0f, 400.0f);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400215 this->onDrawHelper(dContext, canvas, 3, 7, 4, 11);
216 return DrawResult::kOk;
msarettc573a402016-08-02 08:05:56 -0700217 }
218
219private:
John Stiles7571f9e2020-09-02 22:42:33 -0400220 using INHERITED = skiagm::GM;
msarettc573a402016-08-02 08:05:56 -0700221};
222DEF_GM( return new LatticeGM; )
Stan Ilievca8c0952017-12-11 13:01:58 -0500223
224
225// LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles.
226class LatticeGM2 : public skiagm::GM {
227public:
228 LatticeGM2() {}
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000229 SkString getName() const override { return SkString("lattice2"); }
Stan Ilievca8c0952017-12-11 13:01:58 -0500230
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000231 SkISize getISize() override { return SkISize::Make(800, 800); }
Stan Ilievca8c0952017-12-11 13:01:58 -0500232
233 sk_sp<SkImage> makeImage(SkCanvas* root, int padLeft, int padTop, int padRight, int padBottom) {
234 const int kSize = 80;
235 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
Brian Salomon23356442018-11-30 15:33:19 -0500236 SkCanvas* canvas = surface->getCanvas();
Stan Ilievca8c0952017-12-11 13:01:58 -0500237 SkPaint paint;
238 paint.setAntiAlias(false);
239 SkRect r;
240
241 //first line
242 r.setXYWH(0, 0, 4, 1); //4x1 green rect
243 paint.setColor(0xFF00FF00);
244 canvas->drawRect(r, paint);
245
246 r.setXYWH(4, 0, 1, 1); //1x1 blue pixel -> draws as rectangle
247 paint.setColor(0xFF0000FF);
248 canvas->drawRect(r, paint);
249
250 r.setXYWH(5, 0, kSize-5, 1); //the rest of the line is red
251 paint.setColor(0xFFFF0000);
252 canvas->drawRect(r, paint);
253
254
255 //second line -> draws as fixed color rectangles
256 r.setXYWH(0, 1, 4, 1); //4x1 red rect
257 paint.setColor(0xFFFF0000);
258 canvas->drawRect(r, paint);
259
260 r.setXYWH(4, 1, 1, 1); //1x1 blue pixel with alpha
261 paint.setColor(0x880000FF);
262 canvas->drawRect(r, paint);
263
264 r.setXYWH(5, 1, kSize-5, 1); //the rest of the line is green
265 paint.setColor(0xFF00FF00);
266 canvas->drawRect(r, paint);
267
268
269 //third line - does not draw, because it is transparent
270 r.setXYWH(0, 2, 4, kSize-2); //4x78 green rect
271 paint.setColor(0xFF00FF00);
272 canvas->drawRect(r, paint);
273
274 r.setXYWH(4, 2, 1, kSize-2); //1x78 red pixel with alpha
275 paint.setColor(0x88FF0000);
276 canvas->drawRect(r, paint);
277
278 r.setXYWH(5, 2, kSize-5, kSize-2); //the rest of the image is blue
279 paint.setColor(0xFF0000FF);
280 canvas->drawRect(r, paint);
281
282 return surface->makeImageSnapshot();
283 }
284
285 void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom,
286 SkPaint& paint) {
287 int xDivs[2] = {4, 5};
288 int yDivs[2] = {1, 2};
289
290 canvas->save();
291
292 sk_sp<SkImage> image = makeImage(canvas, padLeft, padTop, padRight, padBottom);
293
Mike Reed8d29ab62021-01-23 18:10:39 -0500294 canvas->drawImage(image, 10, 10);
Stan Ilievca8c0952017-12-11 13:01:58 -0500295
296 SkCanvas::Lattice lattice;
297 lattice.fXCount = 2;
298 lattice.fXDivs = xDivs;
299 lattice.fYCount = 2;
300 lattice.fYDivs = yDivs;
301 lattice.fBounds = nullptr;
302
303 SkCanvas::Lattice::RectType flags[9];
304 sk_bzero(flags, 9 * sizeof(SkCanvas::Lattice::RectType));
305 flags[3] = SkCanvas::Lattice::kFixedColor;
306 flags[4] = SkCanvas::Lattice::kFixedColor;
307 flags[5] = SkCanvas::Lattice::kFixedColor;
308
309 flags[6] = SkCanvas::Lattice::kTransparent;
310 flags[7] = SkCanvas::Lattice::kTransparent;
311 flags[8] = SkCanvas::Lattice::kTransparent;
312 lattice.fRectTypes = flags;
313
314 SkColor colors[9] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK,
315 0xFFFF0000, 0x880000FF, 0xFF00FF00,
316 SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
317 lattice.fColors = colors;
318 paint.setColor(0xFFFFFFFF);
319 canvas->drawImageLattice(image.get(), lattice,
Mike Reed99116302021-01-25 11:37:10 -0500320 SkRect::MakeXYWH(100, 100, 200, 200),
321 SkFilterMode::kNearest, &paint);
Stan Ilievca8c0952017-12-11 13:01:58 -0500322
323 //draw the same content with alpha
324 canvas->translate(400, 0);
325 paint.setColor(0x80000FFF);
326 canvas->drawImageLattice(image.get(), lattice,
Mike Reed99116302021-01-25 11:37:10 -0500327 SkRect::MakeXYWH(100, 100, 200, 200),
328 SkFilterMode::kNearest, &paint);
Stan Ilievca8c0952017-12-11 13:01:58 -0500329
330 canvas->restore();
331 }
332
333 void onDraw(SkCanvas* canvas) override {
334
335 //draw a rectangle in the background with transparent pixels
336 SkPaint paint;
337 paint.setColor(0x7F123456);
338 paint.setBlendMode(SkBlendMode::kSrc);
339 canvas->drawRect( SkRect::MakeXYWH(300, 0, 300, 800), paint);
340
341 //draw image lattice with kSrcOver blending
342 paint.setBlendMode(SkBlendMode::kSrcOver);
343 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
344
345 //draw image lattice with kSrcATop blending
346 canvas->translate(0.0f, 400.0f);
347 paint.setBlendMode(SkBlendMode::kSrcATop);
348 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
349 }
350
351private:
John Stiles7571f9e2020-09-02 22:42:33 -0400352 using INHERITED = skiagm::GM;
Stan Ilievca8c0952017-12-11 13:01:58 -0500353};
354DEF_GM( return new LatticeGM2; )
355
Brian Osman0b537032018-12-26 12:16:44 -0500356// Code paths that incorporate the paint color when drawing the lattice (using an alpha image)
357DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500358 auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100));
Brian Osman0b537032018-12-26 12:16:44 -0500359 surface->getCanvas()->clear(0);
360 surface->getCanvas()->drawCircle(50, 50, 50, SkPaint());
361 auto image = surface->makeImageSnapshot();
Stan Ilievca8c0952017-12-11 13:01:58 -0500362
Brian Osman0b537032018-12-26 12:16:44 -0500363 int divs[] = { 20, 40, 60, 80 };
Stan Ilievca8c0952017-12-11 13:01:58 -0500364
Brian Osman0b537032018-12-26 12:16:44 -0500365 SkCanvas::Lattice lattice;
366 lattice.fXCount = 4;
367 lattice.fXDivs = divs;
368 lattice.fYCount = 4;
369 lattice.fYDivs = divs;
370 lattice.fRectTypes = nullptr;
371 lattice.fColors = nullptr;
372 lattice.fBounds = nullptr;
373
374 SkPaint paint;
375 paint.setColor(SK_ColorMAGENTA);
Mike Reed99116302021-01-25 11:37:10 -0500376 canvas->drawImageLattice(image.get(), lattice, SkRect::MakeWH(120, 120),
377 SkFilterMode::kNearest, &paint);
Brian Osman0b537032018-12-26 12:16:44 -0500378}