msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "include/private/SkMalloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/ToolUtils.h" |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 24 | |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 25 | static 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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 28 | return ToolUtils::makeSurface(root, info); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 29 | } |
| 30 | |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 31 | static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop, |
| 32 | int padRight, int padBottom) { |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 33 | const int kCap = 28; |
| 34 | const int kMid = 8; |
| 35 | const int kSize = 2*kCap + 3*kMid; |
| 36 | |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 37 | auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom)); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 38 | SkCanvas* canvas = surface->getCanvas(); |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 39 | canvas->translate((float) padLeft, (float) padTop); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 40 | |
| 41 | SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 42 | const SkScalar strokeWidth = SkIntToScalar(6); |
| 43 | const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2; |
| 44 | |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 45 | 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; |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 53 | |
| 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 | |
| 83 | static 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 | */ |
| 93 | class LatticeGM : public skiagm::GM { |
| 94 | public: |
| 95 | LatticeGM() {} |
| 96 | |
| 97 | protected: |
| 98 | SkString onShortName() override { |
| 99 | return SkString("lattice"); |
| 100 | } |
| 101 | |
| 102 | SkISize onISize() override { |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 103 | return SkISize::Make(800, 800); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 106 | void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom) { |
| 107 | canvas->save(); |
| 108 | |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 109 | int xDivs[5]; |
| 110 | int yDivs[5]; |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 111 | xDivs[0] = padLeft; |
| 112 | yDivs[0] = padTop; |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 113 | |
| 114 | SkBitmap bitmap; |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 115 | sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop, |
| 116 | padRight, padBottom); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 117 | image_to_bitmap(image.get(), &bitmap); |
| 118 | |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 119 | const SkSize size[] = { |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 120 | { 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 Iliev | ca8c095 | 2017-12-11 13:01:58 -0500 | [diff] [blame] | 136 | lattice.fRectTypes = nullptr; |
| 137 | lattice.fColors = nullptr; |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 138 | |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 139 | 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 | |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 144 | 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 Iliev | ca8c095 | 2017-12-11 13:01:58 -0500 | [diff] [blame] | 153 | // 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 | |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 166 | // 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 | |
msarett | 0764efe | 2016-09-02 11:24:30 -0700 | [diff] [blame] | 173 | // Let's skip a few rects. |
Stan Iliev | ca8c095 | 2017-12-11 13:01:58 -0500 | [diff] [blame] | 174 | 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; |
msarett | 0764efe | 2016-09-02 11:24:30 -0700 | [diff] [blame] | 194 | |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 195 | 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 | } |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 204 | |
| 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); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | private: |
| 215 | typedef skiagm::GM INHERITED; |
| 216 | }; |
| 217 | DEF_GM( return new LatticeGM; ) |
Stan Iliev | ca8c095 | 2017-12-11 13:01:58 -0500 | [diff] [blame] | 218 | |
| 219 | |
| 220 | // LatticeGM2 exercises code paths that draw fixed color and 1x1 rectangles. |
| 221 | class LatticeGM2 : public skiagm::GM { |
| 222 | public: |
| 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 Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 235 | SkCanvas* canvas = surface->getCanvas(); |
Stan Iliev | ca8c095 | 2017-12-11 13:01:58 -0500 | [diff] [blame] | 236 | 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 | |
| 348 | private: |
| 349 | typedef skiagm::GM INHERITED; |
| 350 | }; |
| 351 | DEF_GM( return new LatticeGM2; ) |
| 352 | |
Brian Osman | 0b53703 | 2018-12-26 12:16:44 -0500 | [diff] [blame] | 353 | // Code paths that incorporate the paint color when drawing the lattice (using an alpha image) |
| 354 | DEF_SIMPLE_GM_BG(lattice_alpha, canvas, 120, 120, SK_ColorWHITE) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 355 | auto surface = ToolUtils::makeSurface(canvas, SkImageInfo::MakeA8(100, 100)); |
Brian Osman | 0b53703 | 2018-12-26 12:16:44 -0500 | [diff] [blame] | 356 | surface->getCanvas()->clear(0); |
| 357 | surface->getCanvas()->drawCircle(50, 50, 50, SkPaint()); |
| 358 | auto image = surface->makeImageSnapshot(); |
Stan Iliev | ca8c095 | 2017-12-11 13:01:58 -0500 | [diff] [blame] | 359 | |
Brian Osman | 0b53703 | 2018-12-26 12:16:44 -0500 | [diff] [blame] | 360 | int divs[] = { 20, 40, 60, 80 }; |
Stan Iliev | ca8c095 | 2017-12-11 13:01:58 -0500 | [diff] [blame] | 361 | |
Brian Osman | 0b53703 | 2018-12-26 12:16:44 -0500 | [diff] [blame] | 362 | 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 | } |