OLD | NEW |
(Empty) | |
| 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.h" |
| 9 #include "SkSurface.h" |
| 10 |
| 11 static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) { |
| 12 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); |
| 13 auto surface = root->makeSurface(info); |
| 14 if (!surface) { |
| 15 surface = SkSurface::MakeRaster(info); |
| 16 } |
| 17 return surface; |
| 18 } |
| 19 |
| 20 static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs) { |
| 21 const int kCap = 28; |
| 22 const int kMid = 8; |
| 23 const int kSize = 2*kCap + 3*kMid; |
| 24 |
| 25 auto surface(make_surface(root, kSize)); |
| 26 SkCanvas* canvas = surface->getCanvas(); |
| 27 |
| 28 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 29 const SkScalar strokeWidth = SkIntToScalar(6); |
| 30 const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2; |
| 31 |
| 32 xDivs[0] = yDivs[0] = kCap; |
| 33 xDivs[1] = yDivs[1] = kCap + kMid; |
| 34 xDivs[2] = yDivs[2] = kCap + 2 * kMid; |
| 35 xDivs[3] = yDivs[3] = kCap + 3 * kMid; |
| 36 |
| 37 SkPaint paint; |
| 38 paint.setAntiAlias(true); |
| 39 |
| 40 paint.setColor(0xFFFFFF00); |
| 41 canvas->drawRoundRect(r, radius, radius, paint); |
| 42 |
| 43 r.setXYWH(SkIntToScalar(kCap), 0, SkIntToScalar(kMid), SkIntToScalar(kSize))
; |
| 44 paint.setColor(0x8800FF00); |
| 45 canvas->drawRect(r, paint); |
| 46 r.setXYWH(SkIntToScalar(kCap + kMid), 0, SkIntToScalar(kMid), SkIntToScalar(
kSize)); |
| 47 paint.setColor(0x880000FF); |
| 48 canvas->drawRect(r, paint); |
| 49 r.setXYWH(SkIntToScalar(kCap + 2*kMid), 0, SkIntToScalar(kMid), SkIntToScala
r(kSize)); |
| 50 paint.setColor(0x88FF00FF); |
| 51 canvas->drawRect(r, paint); |
| 52 |
| 53 r.setXYWH(0, SkIntToScalar(kCap), SkIntToScalar(kSize), SkIntToScalar(kMid))
; |
| 54 paint.setColor(0x8800FF00); |
| 55 canvas->drawRect(r, paint); |
| 56 r.setXYWH(0, SkIntToScalar(kCap + kMid), SkIntToScalar(kSize), SkIntToScalar
(kMid)); |
| 57 paint.setColor(0x880000FF); |
| 58 canvas->drawRect(r, paint); |
| 59 r.setXYWH(0, SkIntToScalar(kCap + 2*kMid), SkIntToScalar(kSize), SkIntToScal
ar(kMid)); |
| 60 paint.setColor(0x88FF00FF); |
| 61 canvas->drawRect(r, paint); |
| 62 |
| 63 return surface->makeImageSnapshot(); |
| 64 } |
| 65 |
| 66 static void image_to_bitmap(const SkImage* image, SkBitmap* bm) { |
| 67 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height(
)); |
| 68 bm->allocPixels(info); |
| 69 image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0); |
| 70 } |
| 71 |
| 72 /** |
| 73 * This is similar to NinePatchStretchGM, but it also tests "ninepatch" images
with more |
| 74 * than nine patches. |
| 75 */ |
| 76 class LatticeGM : public skiagm::GM { |
| 77 public: |
| 78 LatticeGM() {} |
| 79 |
| 80 protected: |
| 81 SkString onShortName() override { |
| 82 return SkString("lattice"); |
| 83 } |
| 84 |
| 85 SkISize onISize() override { |
| 86 return SkISize::Make(800, 400); |
| 87 } |
| 88 |
| 89 void onDraw(SkCanvas* canvas) override { |
| 90 int xDivs[5]; |
| 91 int yDivs[5]; |
| 92 xDivs[0] = 0; |
| 93 yDivs[0] = 0; |
| 94 |
| 95 SkBitmap bitmap; |
| 96 sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1); |
| 97 image_to_bitmap(image.get(), &bitmap); |
| 98 |
| 99 const SkTSize<SkScalar> size[] = { |
| 100 { 50, 50, }, // shrink in both axes |
| 101 { 50, 200, }, // shrink in X |
| 102 { 200, 50, }, // shrink in Y |
| 103 { 200, 200, }, |
| 104 }; |
| 105 |
| 106 canvas->drawImage(image, 10, 10, nullptr); |
| 107 |
| 108 SkScalar x = SkIntToScalar(100); |
| 109 SkScalar y = SkIntToScalar(100); |
| 110 |
| 111 SkCanvas::Lattice lattice; |
| 112 lattice.fXCount = 4; |
| 113 lattice.fXDivs = xDivs + 1; |
| 114 lattice.fYCount = 4; |
| 115 lattice.fYDivs = yDivs + 1; |
| 116 |
| 117 for (int iy = 0; iy < 2; ++iy) { |
| 118 for (int ix = 0; ix < 2; ++ix) { |
| 119 int i = ix * 2 + iy; |
| 120 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60, |
| 121 size[i].width(), size[i].height()); |
| 122 canvas->drawBitmapLattice(bitmap, lattice, r); |
| 123 } |
| 124 } |
| 125 |
| 126 // Include the degenerate first div. While normally the first patch is
"scalable", |
| 127 // this will mean that the first non-degenerate patch is "fixed". |
| 128 lattice.fXCount = 5; |
| 129 lattice.fXDivs = xDivs; |
| 130 lattice.fYCount = 5; |
| 131 lattice.fYDivs = yDivs; |
| 132 |
| 133 canvas->translate(400, 0); |
| 134 for (int iy = 0; iy < 2; ++iy) { |
| 135 for (int ix = 0; ix < 2; ++ix) { |
| 136 int i = ix * 2 + iy; |
| 137 SkRect r = SkRect::MakeXYWH(x + ix * 60, y + iy * 60, |
| 138 size[i].width(), size[i].height()); |
| 139 canvas->drawImageLattice(image.get(), lattice, r); |
| 140 } |
| 141 } |
| 142 } |
| 143 |
| 144 private: |
| 145 typedef skiagm::GM INHERITED; |
| 146 }; |
| 147 DEF_GM( return new LatticeGM; ) |
OLD | NEW |