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