1 /*
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/gm.h"
9 #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"
21 #include "include/core/SkSurface.h"
22 #include "include/gpu/ganesh/GrDirectContext.h"
23 #include "include/private/base/SkMalloc.h"
24 #include "tools/ToolUtils.h"
25
make_surface(SkCanvas * root,int N,int padLeft,int padTop,int padRight,int padBottom)26 static 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);
29 return ToolUtils::makeSurface(root, info);
30 }
31
make_image(SkCanvas * root,int * xDivs,int * yDivs,int padLeft,int padTop,int padRight,int padBottom)32 static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop,
33 int padRight, int padBottom) {
34 const int kCap = 28;
35 const int kMid = 8;
36 const int kSize = 2*kCap + 3*kMid;
37
38 auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom));
39 SkCanvas* canvas = surface->getCanvas();
40 canvas->translate((float) padLeft, (float) padTop);
41
42 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
43 const SkScalar strokeWidth = SkIntToScalar(6);
44 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2;
45
46 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;
54
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
image_to_bitmap(GrDirectContext * dContext,const SkImage * image,SkBitmap * bm)84 static void image_to_bitmap(GrDirectContext* dContext, const SkImage* image, SkBitmap* bm) {
85 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
86 bm->allocPixels(info);
87 image->readPixels(dContext, info, bm->getPixels(), bm->rowBytes(), 0, 0);
88 }
89
90 /**
91 * This is similar to NinePatchStretchGM, but it also tests "ninepatch" images with more
92 * than nine patches.
93 */
94 class LatticeGM : public skiagm::GM {
95 public:
LatticeGM()96 LatticeGM() {}
97
98 protected:
getName() const99 SkString getName() const override { return SkString("lattice"); }
100
getISize()101 SkISize getISize() override { return SkISize::Make(800, 800); }
102
onDrawHelper(GrDirectContext * dContext,SkCanvas * canvas,int padLeft,int padTop,int padRight,int padBottom)103 void onDrawHelper(GrDirectContext* dContext, SkCanvas* canvas, int padLeft, int padTop,
104 int padRight, int padBottom) {
105 canvas->save();
106
107 int xDivs[5];
108 int yDivs[5];
109 xDivs[0] = padLeft;
110 yDivs[0] = padTop;
111
112 SkBitmap bitmap;
113 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
114 padRight, padBottom);
115 image_to_bitmap(dContext, image.get(), &bitmap);
116
117 const SkSize size[] = {
118 { 50, 50, }, // shrink in both axes
119 { 50, 200, }, // shrink in X
120 { 200, 50, }, // shrink in Y
121 { 200, 200, },
122 };
123
124 canvas->drawImage(image, 10, 10);
125
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;
134 lattice.fRectTypes = nullptr;
135 lattice.fColors = nullptr;
136
137 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
142 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());
147 canvas->drawImageLattice(image.get(), lattice, r);
148 }
149 }
150
151 // 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];
161 image->readPixels(dContext, info, &fixedColor[rectNum], 4, srcX, srcY);
162 }
163
164 // 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
171 // Let's skip a few rects.
172 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;
192
193 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 }
202
203 canvas->restore();
204 }
205
onDraw(SkCanvas * canvas,SkString * errorMsg)206 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);
214 canvas->translate(0.0f, 400.0f);
215 this->onDrawHelper(dContext, canvas, 3, 7, 4, 11);
216 return DrawResult::kOk;
217 }
218
219 private:
220 using INHERITED = skiagm::GM;
221 };
222 DEF_GM( return new LatticeGM; )
223
224
225 // LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles.
226 class LatticeGM2 : public skiagm::GM {
227 public:
LatticeGM2()228 LatticeGM2() {}
getName() const229 SkString getName() const override { return SkString("lattice2"); }
230
getISize()231 SkISize getISize() override { return SkISize::Make(800, 800); }
232
makeImage(SkCanvas * root,int padLeft,int padTop,int padRight,int padBottom)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));
236 SkCanvas* canvas = surface->getCanvas();
237 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
onDrawHelper(SkCanvas * canvas,int padLeft,int padTop,int padRight,int padBottom,SkPaint & paint)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
294 canvas->drawImage(image, 10, 10);
295
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,
320 SkRect::MakeXYWH(100, 100, 200, 200),
321 SkFilterMode::kNearest, &paint);
322
323 //draw the same content with alpha
324 canvas->translate(400, 0);
325 paint.setColor(0x80000FFF);
326 canvas->drawImageLattice(image.get(), lattice,
327 SkRect::MakeXYWH(100, 100, 200, 200),
328 SkFilterMode::kNearest, &paint);
329
330 canvas->restore();
331 }
332
onDraw(SkCanvas * canvas)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
351 private:
352 using INHERITED = skiagm::GM;
353 };
354 DEF_GM( return new LatticeGM2; )
355
356 // Code paths that incorporate the paint color when drawing the lattice (using an alpha image)
357 DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) {
358 auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100));
359 surface->getCanvas()->clear(0);
360 surface->getCanvas()->drawCircle(50, 50, 50, SkPaint());
361 auto image = surface->makeImageSnapshot();
362
363 int divs[] = { 20, 40, 60, 80 };
364
365 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);
376 canvas->drawImageLattice(image.get(), lattice, SkRect::MakeWH(120, 120),
377 SkFilterMode::kNearest, &paint);
378 }
379
make_symmetry_test_image()380 static sk_sp<SkImage> make_symmetry_test_image() {
381 auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(8, 8));
382 auto canvas = surface->getCanvas();
383 canvas->drawColor(SK_ColorBLUE);
384 SkPaint p;
385 p.setColor(SK_ColorGREEN);
386 canvas->drawRect(SkRect::MakeXYWH(2, 2, 4, 4), p);
387 return surface->makeImageSnapshot();
388 }
389
390 // b/349428795 : A nine-patch should be able to have zero-sized regions on either end. Before
391 // fixing the bug, it only worked on the left/top, leading to non-symmetric results in this GM.
392 // Correct rendering is for each row to be symmetric.
393 DEF_SIMPLE_GM(ninepatch_edge_case_349428795, canvas, 500, 150) {
394 auto nine = make_symmetry_test_image();
395
396 for (int i = -1; i < 6; i++) {
397 canvas->drawImageNine(nine.get(),
398 SkIRect::MakeXYWH(i, 2, 4, 4),
399 SkRect::MakeXYWH(i * 70 + 80, 10, 64, 64),
400 SkFilterMode::kLinear);
401 canvas->drawImageNine(nine.get(),
402 SkIRect::MakeXYWH(2, i, 4, 4),
403 SkRect::MakeXYWH(i * 70 + 80, 80, 64, 64),
404 SkFilterMode::kLinear);
405 }
406 }
407