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/GrDirectContext.h"
23 #include "include/private/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:
onShortName()99     SkString onShortName() override {
100         return SkString("lattice");
101     }
102 
onISize()103     SkISize onISize() override {
104         return SkISize::Make(800, 800);
105     }
106 
onDrawHelper(GrDirectContext * dContext,SkCanvas * canvas,int padLeft,int padTop,int padRight,int padBottom)107     void onDrawHelper(GrDirectContext* dContext, SkCanvas* canvas, int padLeft, int padTop,
108                       int padRight, int padBottom) {
109         canvas->save();
110 
111         int xDivs[5];
112         int yDivs[5];
113         xDivs[0] = padLeft;
114         yDivs[0] = padTop;
115 
116         SkBitmap bitmap;
117         sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop,
118                                           padRight, padBottom);
119         image_to_bitmap(dContext, image.get(), &bitmap);
120 
121         const SkSize size[] = {
122             {  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;
138         lattice.fRectTypes = nullptr;
139         lattice.fColors = nullptr;
140 
141         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 
146         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());
151                 canvas->drawImageLattice(image.get(), lattice, r);
152             }
153         }
154 
155         // 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];
165             image->readPixels(dContext, info, &fixedColor[rectNum], 4, srcX, srcY);
166         }
167 
168         // 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 
175         // Let's skip a few rects.
176         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;
196 
197         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         }
206 
207         canvas->restore();
208     }
209 
onDraw(SkCanvas * canvas,SkString * errorMsg)210     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);
218         canvas->translate(0.0f, 400.0f);
219         this->onDrawHelper(dContext, canvas, 3, 7, 4, 11);
220         return DrawResult::kOk;
221     }
222 
223 private:
224     using INHERITED = skiagm::GM;
225 };
226 DEF_GM( return new LatticeGM; )
227 
228 
229 // LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles.
230 class LatticeGM2 : public skiagm::GM {
231 public:
LatticeGM2()232     LatticeGM2() {}
onShortName()233     SkString onShortName() override {
234         return SkString("lattice2");
235     }
236 
onISize()237     SkISize onISize() override {
238         return SkISize::Make(800, 800);
239     }
240 
makeImage(SkCanvas * root,int padLeft,int padTop,int padRight,int padBottom)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));
244         SkCanvas* canvas = surface->getCanvas();
245         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 
onDrawHelper(SkCanvas * canvas,int padLeft,int padTop,int padRight,int padBottom,SkPaint & paint)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 
onDraw(SkCanvas * canvas)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 
357 private:
358     using INHERITED = skiagm::GM;
359 };
360 DEF_GM( return new LatticeGM2; )
361 
362 // Code paths that incorporate the paint color when drawing the lattice (using an alpha image)
363 DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) {
364     auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100));
365     surface->getCanvas()->clear(0);
366     surface->getCanvas()->drawCircle(50, 50, 50, SkPaint());
367     auto image = surface->makeImageSnapshot();
368 
369     int divs[] = { 20, 40, 60, 80 };
370 
371     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 }
384