blob: 21d3d8b69d48a0642c9b2f18f846e19e3d9bf3d6 [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/private/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:
99 SkString onShortName() override {
100 return SkString("lattice");
101 }
102
103 SkISize onISize() override {
msarett71df2d72016-09-30 12:41:42 -0700104 return SkISize::Make(800, 800);
msarettc573a402016-08-02 08:05:56 -0700105 }
106
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400107 void onDrawHelper(GrDirectContext* dContext, SkCanvas* canvas, int padLeft, int padTop,
108 int padRight, int padBottom) {
msarett71df2d72016-09-30 12:41:42 -0700109 canvas->save();
110
msarettc573a402016-08-02 08:05:56 -0700111 int xDivs[5];
112 int yDivs[5];
msarett71df2d72016-09-30 12:41:42 -0700113 xDivs[0] = padLeft;
114 yDivs[0] = padTop;
msarettc573a402016-08-02 08:05:56 -0700115
116 SkBitmap bitmap;
msarett71df2d72016-09-30 12:41:42 -0700117 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
118 padRight, padBottom);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400119 image_to_bitmap(dContext, image.get(), &bitmap);
msarettc573a402016-08-02 08:05:56 -0700120
Hal Canaryfafe1352017-04-11 12:12:02 -0400121 const SkSize size[] = {
msarettc573a402016-08-02 08:05:56 -0700122 { 50, 50, }, // shrink in both axes
123 { 50, 200, }, // shrink in X
124 { 200, 50, }, // shrink in Y
125 { 200, 200, },
126 };
127
128 canvas->drawImage(image, 10, 10, nullptr);
129
130 SkScalar x = SkIntToScalar(100);
131 SkScalar y = SkIntToScalar(100);
132
133 SkCanvas::Lattice lattice;
134 lattice.fXCount = 4;
135 lattice.fXDivs = xDivs + 1;
136 lattice.fYCount = 4;
137 lattice.fYDivs = yDivs + 1;
Stan Ilievca8c0952017-12-11 13:01:58 -0500138 lattice.fRectTypes = nullptr;
139 lattice.fColors = nullptr;
msarettc573a402016-08-02 08:05:56 -0700140
msarett71df2d72016-09-30 12:41:42 -0700141 SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop,
142 image->width() - padRight, image->height() - padBottom);
143 lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ?
144 nullptr : &bounds;
145
msarettc573a402016-08-02 08:05:56 -0700146 for (int iy = 0; iy < 2; ++iy) {
147 for (int ix = 0; ix < 2; ++ix) {
148 int i = ix * 2 + iy;
149 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
150 size[i].width(), size[i].height());
Mike Reedfdf94042020-03-10 21:45:34 -0400151 canvas->drawImageLattice(image.get(), lattice, r);
msarettc573a402016-08-02 08:05:56 -0700152 }
153 }
154
Stan Ilievca8c0952017-12-11 13:01:58 -0500155 // Provide hints about 3 solid color rects. These colors match
156 // what was already in the bitmap.
157 int fixedColorX[3] = {2, 4, 1};
158 int fixedColorY[3] = {1, 1, 2};
159 SkColor fixedColor[3] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
160 const SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType,
161 kUnpremul_SkAlphaType);
162 for (int rectNum = 0; rectNum < 3; rectNum++) {
163 int srcX = xDivs[fixedColorX[rectNum]-1];
164 int srcY = yDivs[fixedColorY[rectNum]-1];
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400165 image->readPixels(dContext, info, &fixedColor[rectNum], 4, srcX, srcY);
Stan Ilievca8c0952017-12-11 13:01:58 -0500166 }
167
msarettc573a402016-08-02 08:05:56 -0700168 // Include the degenerate first div. While normally the first patch is "scalable",
169 // this will mean that the first non-degenerate patch is "fixed".
170 lattice.fXCount = 5;
171 lattice.fXDivs = xDivs;
172 lattice.fYCount = 5;
173 lattice.fYDivs = yDivs;
174
msarett0764efe2016-09-02 11:24:30 -0700175 // Let's skip a few rects.
Stan Ilievca8c0952017-12-11 13:01:58 -0500176 SkCanvas::Lattice::RectType flags[36];
177 sk_bzero(flags, 36 * sizeof(SkCanvas::Lattice::RectType));
178 flags[4] = SkCanvas::Lattice::kTransparent;
179 flags[9] = SkCanvas::Lattice::kTransparent;
180 flags[12] = SkCanvas::Lattice::kTransparent;
181 flags[19] = SkCanvas::Lattice::kTransparent;
182 for (int rectNum = 0; rectNum < 3; rectNum++) {
183 flags[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
184 = SkCanvas::Lattice::kFixedColor;
185 }
186 lattice.fRectTypes = flags;
187
188 SkColor colors[36];
189 sk_bzero(colors, 36 * sizeof(SkColor));
190 for (int rectNum = 0; rectNum < 3; rectNum++) {
191 colors[fixedColorY[rectNum]*6 + fixedColorX[rectNum]]
192 = fixedColor[rectNum];
193 }
194
195 lattice.fColors = colors;
msarett0764efe2016-09-02 11:24:30 -0700196
msarettc573a402016-08-02 08:05:56 -0700197 canvas->translate(400, 0);
198 for (int iy = 0; iy < 2; ++iy) {
199 for (int ix = 0; ix < 2; ++ix) {
200 int i = ix * 2 + iy;
201 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60,
202 size[i].width(), size[i].height());
203 canvas->drawImageLattice(image.get(), lattice, r);
204 }
205 }
msarett71df2d72016-09-30 12:41:42 -0700206
207 canvas->restore();
208 }
209
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400210 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
211 auto rContext = canvas->recordingContext();
212 auto dContext = GrAsDirectContext(rContext);
213 if (rContext && !dContext) {
214 *errorMsg = "not supported in ddl";
215 return DrawResult::kSkip;
216 }
217 this->onDrawHelper(dContext, canvas, 0, 0, 0, 0);
msarett71df2d72016-09-30 12:41:42 -0700218 canvas->translate(0.0f, 400.0f);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400219 this->onDrawHelper(dContext, canvas, 3, 7, 4, 11);
220 return DrawResult::kOk;
msarettc573a402016-08-02 08:05:56 -0700221 }
222
223private:
John Stiles7571f9e2020-09-02 22:42:33 -0400224 using INHERITED = skiagm::GM;
msarettc573a402016-08-02 08:05:56 -0700225};
226DEF_GM( return new LatticeGM; )
Stan Ilievca8c0952017-12-11 13:01:58 -0500227
228
229// LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles.
230class LatticeGM2 : public skiagm::GM {
231public:
232 LatticeGM2() {}
233 SkString onShortName() override {
234 return SkString("lattice2");
235 }
236
237 SkISize onISize() override {
238 return SkISize::Make(800, 800);
239 }
240
241 sk_sp<SkImage> makeImage(SkCanvas* root, int padLeft, int padTop, int padRight, int padBottom) {
242 const int kSize = 80;
243 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
Brian Salomon23356442018-11-30 15:33:19 -0500244 SkCanvas* canvas = surface->getCanvas();
Stan Ilievca8c0952017-12-11 13:01:58 -0500245 SkPaint paint;
246 paint.setAntiAlias(false);
247 SkRect r;
248
249 //first line
250 r.setXYWH(0, 0, 4, 1); //4x1 green rect
251 paint.setColor(0xFF00FF00);
252 canvas->drawRect(r, paint);
253
254 r.setXYWH(4, 0, 1, 1); //1x1 blue pixel -> draws as rectangle
255 paint.setColor(0xFF0000FF);
256 canvas->drawRect(r, paint);
257
258 r.setXYWH(5, 0, kSize-5, 1); //the rest of the line is red
259 paint.setColor(0xFFFF0000);
260 canvas->drawRect(r, paint);
261
262
263 //second line -> draws as fixed color rectangles
264 r.setXYWH(0, 1, 4, 1); //4x1 red rect
265 paint.setColor(0xFFFF0000);
266 canvas->drawRect(r, paint);
267
268 r.setXYWH(4, 1, 1, 1); //1x1 blue pixel with alpha
269 paint.setColor(0x880000FF);
270 canvas->drawRect(r, paint);
271
272 r.setXYWH(5, 1, kSize-5, 1); //the rest of the line is green
273 paint.setColor(0xFF00FF00);
274 canvas->drawRect(r, paint);
275
276
277 //third line - does not draw, because it is transparent
278 r.setXYWH(0, 2, 4, kSize-2); //4x78 green rect
279 paint.setColor(0xFF00FF00);
280 canvas->drawRect(r, paint);
281
282 r.setXYWH(4, 2, 1, kSize-2); //1x78 red pixel with alpha
283 paint.setColor(0x88FF0000);
284 canvas->drawRect(r, paint);
285
286 r.setXYWH(5, 2, kSize-5, kSize-2); //the rest of the image is blue
287 paint.setColor(0xFF0000FF);
288 canvas->drawRect(r, paint);
289
290 return surface->makeImageSnapshot();
291 }
292
293 void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom,
294 SkPaint& paint) {
295 int xDivs[2] = {4, 5};
296 int yDivs[2] = {1, 2};
297
298 canvas->save();
299
300 sk_sp<SkImage> image = makeImage(canvas, padLeft, padTop, padRight, padBottom);
301
302 canvas->drawImage(image, 10, 10, nullptr);
303
304 SkCanvas::Lattice lattice;
305 lattice.fXCount = 2;
306 lattice.fXDivs = xDivs;
307 lattice.fYCount = 2;
308 lattice.fYDivs = yDivs;
309 lattice.fBounds = nullptr;
310
311 SkCanvas::Lattice::RectType flags[9];
312 sk_bzero(flags, 9 * sizeof(SkCanvas::Lattice::RectType));
313 flags[3] = SkCanvas::Lattice::kFixedColor;
314 flags[4] = SkCanvas::Lattice::kFixedColor;
315 flags[5] = SkCanvas::Lattice::kFixedColor;
316
317 flags[6] = SkCanvas::Lattice::kTransparent;
318 flags[7] = SkCanvas::Lattice::kTransparent;
319 flags[8] = SkCanvas::Lattice::kTransparent;
320 lattice.fRectTypes = flags;
321
322 SkColor colors[9] = {SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK,
323 0xFFFF0000, 0x880000FF, 0xFF00FF00,
324 SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK};
325 lattice.fColors = colors;
326 paint.setColor(0xFFFFFFFF);
327 canvas->drawImageLattice(image.get(), lattice,
328 SkRect::MakeXYWH(100, 100, 200, 200), &paint);
329
330 //draw the same content with alpha
331 canvas->translate(400, 0);
332 paint.setColor(0x80000FFF);
333 canvas->drawImageLattice(image.get(), lattice,
334 SkRect::MakeXYWH(100, 100, 200, 200), &paint);
335
336 canvas->restore();
337 }
338
339 void onDraw(SkCanvas* canvas) override {
340
341 //draw a rectangle in the background with transparent pixels
342 SkPaint paint;
343 paint.setColor(0x7F123456);
344 paint.setBlendMode(SkBlendMode::kSrc);
345 canvas->drawRect( SkRect::MakeXYWH(300, 0, 300, 800), paint);
346
347 //draw image lattice with kSrcOver blending
348 paint.setBlendMode(SkBlendMode::kSrcOver);
349 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
350
351 //draw image lattice with kSrcATop blending
352 canvas->translate(0.0f, 400.0f);
353 paint.setBlendMode(SkBlendMode::kSrcATop);
354 this->onDrawHelper(canvas, 0, 0, 0, 0, paint);
355 }
356
357private:
John Stiles7571f9e2020-09-02 22:42:33 -0400358 using INHERITED = skiagm::GM;
Stan Ilievca8c0952017-12-11 13:01:58 -0500359};
360DEF_GM( return new LatticeGM2; )
361
Brian Osman0b537032018-12-26 12:16:44 -0500362// Code paths that incorporate the paint color when drawing the lattice (using an alpha image)
363DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500364 auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100));
Brian Osman0b537032018-12-26 12:16:44 -0500365 surface->getCanvas()->clear(0);
366 surface->getCanvas()->drawCircle(50, 50, 50, SkPaint());
367 auto image = surface->makeImageSnapshot();
Stan Ilievca8c0952017-12-11 13:01:58 -0500368
Brian Osman0b537032018-12-26 12:16:44 -0500369 int divs[] = { 20, 40, 60, 80 };
Stan Ilievca8c0952017-12-11 13:01:58 -0500370
Brian Osman0b537032018-12-26 12:16:44 -0500371 SkCanvas::Lattice lattice;
372 lattice.fXCount = 4;
373 lattice.fXDivs = divs;
374 lattice.fYCount = 4;
375 lattice.fYDivs = divs;
376 lattice.fRectTypes = nullptr;
377 lattice.fColors = nullptr;
378 lattice.fBounds = nullptr;
379
380 SkPaint paint;
381 paint.setColor(SK_ColorMAGENTA);
382 canvas->drawImageLattice(image.get(), lattice, SkRect::MakeWH(120, 120), &paint);
383}