reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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" |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 9 | #include "SkSurface.h" |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 10 | #include "sk_tool_utils.h" |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 11 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 12 | static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) { |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 13 | SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 14 | return sk_tool_utils::makeSurface(root, info); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 15 | } |
| 16 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 17 | static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) { |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 18 | const int kFixed = 28; |
| 19 | const int kStretchy = 8; |
| 20 | const int kSize = 2*kFixed + kStretchy; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 21 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 22 | auto surface(make_surface(root, kSize)); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 23 | SkCanvas* canvas = surface->getCanvas(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 25 | SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 26 | const SkScalar strokeWidth = SkIntToScalar(6); |
| 27 | const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 29 | center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 30 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 31 | SkPaint paint; |
| 32 | paint.setAntiAlias(true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 33 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 34 | paint.setColor(0xFFFF0000); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 35 | canvas->drawRoundRect(r, radius, radius, paint); |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 36 | r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize)); |
| 37 | paint.setColor(0x8800FF00); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 38 | canvas->drawRect(r, paint); |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 39 | r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy)); |
| 40 | paint.setColor(0x880000FF); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 41 | canvas->drawRect(r, paint); |
| 42 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 43 | return surface->makeImageSnapshot(); |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 44 | } |
| 45 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 46 | static void image_to_bitmap(const SkImage* image, SkBitmap* bm) { |
| 47 | SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height()); |
| 48 | bm->allocPixels(info); |
| 49 | image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0); |
| 50 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 51 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 52 | class NinePatchStretchGM : public skiagm::GM { |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 53 | public: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 54 | sk_sp<SkImage> fImage; |
| 55 | SkBitmap fBitmap; |
| 56 | SkIRect fCenter; |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 57 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | NinePatchStretchGM() {} |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 59 | |
| 60 | protected: |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 61 | SkString onShortName() override { |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 62 | return SkString("ninepatch-stretch"); |
| 63 | } |
| 64 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 65 | SkISize onISize() override { |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 66 | return SkISize::Make(760, 800); |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 67 | } |
| 68 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 69 | void onDraw(SkCanvas* canvas) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | if (nullptr == fBitmap.pixelRef()) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 71 | fImage = make_image(canvas, &fCenter); |
| 72 | image_to_bitmap(fImage.get(), &fBitmap); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 73 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 74 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 75 | // amount of bm that should not be stretched (unless we have to) |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 76 | const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 77 | |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 78 | const SkSize size[] = { |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 79 | { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes |
| 80 | { fixed * 4 / 5, fixed * 4 }, // shrink in X |
| 81 | { fixed * 4, fixed * 4 / 5 }, // shrink in Y |
| 82 | { fixed * 4, fixed * 4 } |
| 83 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 84 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | canvas->drawBitmap(fBitmap, 10, 10, nullptr); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 86 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 87 | SkScalar x = SkIntToScalar(100); |
| 88 | SkScalar y = SkIntToScalar(100); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 90 | SkPaint paint; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 91 | for (int filter = 0; filter < 2; filter++) { |
| 92 | paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
| 93 | canvas->translate(0, filter * SkIntToScalar(400)); |
| 94 | for (int iy = 0; iy < 2; ++iy) { |
| 95 | for (int ix = 0; ix < 2; ++ix) { |
| 96 | int i = ix * 2 + iy; |
| 97 | SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, |
| 98 | size[i].width(), size[i].height()); |
| 99 | canvas->drawBitmapNine(fBitmap, fCenter, r, &paint); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 100 | canvas->drawImageNine(fImage.get(), fCenter, r.makeOffset(360, 0), &paint); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 101 | } |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 105 | |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 106 | private: |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 107 | typedef skiagm::GM INHERITED; |
reed@google.com | b670525 | 2011-09-06 19:23:41 +0000 | [diff] [blame] | 108 | }; |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 109 | DEF_GM( return new NinePatchStretchGM; ) |