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