| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkSurface.h" |
| 9 | 10 |
| 10 static void make_bitmap(SkBitmap* bitmap, SkIRect* center) { | 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) { |
| 11 const int kFixed = 28; | 21 const int kFixed = 28; |
| 12 const int kStretchy = 8; | 22 const int kStretchy = 8; |
| 13 const int kSize = 2*kFixed + kStretchy; | 23 const int kSize = 2*kFixed + kStretchy; |
| 14 | 24 |
| 15 bitmap->allocN32Pixels(kSize, kSize); | 25 SkAutoTUnref<SkSurface> surface(make_surface(root, kSize)); |
| 16 SkCanvas canvas(*bitmap); | 26 SkCanvas* canvas = surface->getCanvas(); |
| 17 canvas.clear(SK_ColorTRANSPARENT); | |
| 18 | 27 |
| 19 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); | 28 SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); |
| 20 const SkScalar strokeWidth = SkIntToScalar(6); | 29 const SkScalar strokeWidth = SkIntToScalar(6); |
| 21 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; | 30 const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2; |
| 22 | 31 |
| 23 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); | 32 center->setXYWH(kFixed, kFixed, kStretchy, kStretchy); |
| 24 | 33 |
| 25 SkPaint paint; | 34 SkPaint paint; |
| 26 paint.setAntiAlias(true); | 35 paint.setAntiAlias(true); |
| 27 | 36 |
| 28 paint.setColor(0xFFFF0000); | 37 paint.setColor(0xFFFF0000); |
| 29 canvas.drawRoundRect(r, radius, radius, paint); | 38 canvas->drawRoundRect(r, radius, radius, paint); |
| 30 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(
kSize)); | 39 r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(
kSize)); |
| 31 paint.setColor(0x8800FF00); | 40 paint.setColor(0x8800FF00); |
| 32 canvas.drawRect(r, paint); | 41 canvas->drawRect(r, paint); |
| 33 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStr
etchy)); | 42 r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStr
etchy)); |
| 34 paint.setColor(0x880000FF); | 43 paint.setColor(0x880000FF); |
| 35 canvas.drawRect(r, paint); | 44 canvas->drawRect(r, paint); |
| 45 |
| 46 return surface->newImageSnapshot(); |
| 36 } | 47 } |
| 37 | 48 |
| 38 namespace skiagm { | 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 } |
| 39 | 54 |
| 40 class NinePatchStretchGM : public GM { | 55 class NinePatchStretchGM : public skiagm::GM { |
| 41 public: | 56 public: |
| 42 SkBitmap fBM; | 57 SkAutoTUnref<SkImage> fImage; |
| 58 SkBitmap fBitmap; |
| 59 SkIRect fCenter; |
| 43 | 60 |
| 44 NinePatchStretchGM() {} | 61 NinePatchStretchGM() {} |
| 45 | 62 |
| 46 protected: | 63 protected: |
| 47 virtual SkString onShortName() { | 64 SkString onShortName() override { |
| 48 return SkString("ninepatch-stretch"); | 65 return SkString("ninepatch-stretch"); |
| 49 } | 66 } |
| 50 | 67 |
| 51 virtual SkISize onISize() { | 68 SkISize onISize() override { |
| 52 return SkISize::Make(400, 400); | 69 return SkISize::Make(760, 400); |
| 53 } | 70 } |
| 54 | 71 |
| 55 virtual void onDraw(SkCanvas* canvas) { | 72 void onDraw(SkCanvas* canvas) override { |
| 56 SkBitmap bm; | 73 if (NULL == fBitmap.pixelRef()) { |
| 57 SkIRect center; | 74 fImage.reset(make_image(canvas, &fCenter)); |
| 58 make_bitmap(&bm, ¢er); | 75 image_to_bitmap(fImage, &fBitmap); |
| 76 } |
| 59 | 77 |
| 60 // amount of bm that should not be stretched (unless we have to) | 78 // amount of bm that should not be stretched (unless we have to) |
| 61 const SkScalar fixed = SkIntToScalar(bm.width() - center.width()); | 79 const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width()); |
| 62 | 80 |
| 63 const SkTSize<SkScalar> size[] = { | 81 const SkTSize<SkScalar> size[] = { |
| 64 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes | 82 { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes |
| 65 { fixed * 4 / 5, fixed * 4 }, // shrink in X | 83 { fixed * 4 / 5, fixed * 4 }, // shrink in X |
| 66 { fixed * 4, fixed * 4 / 5 }, // shrink in Y | 84 { fixed * 4, fixed * 4 / 5 }, // shrink in Y |
| 67 { fixed * 4, fixed * 4 } | 85 { fixed * 4, fixed * 4 } |
| 68 }; | 86 }; |
| 69 | 87 |
| 70 canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL); | 88 canvas->drawBitmap(fBitmap, 10, 10, NULL); |
| 71 | 89 |
| 72 SkScalar x = SkIntToScalar(100); | 90 SkScalar x = SkIntToScalar(100); |
| 73 SkScalar y = SkIntToScalar(100); | 91 SkScalar y = SkIntToScalar(100); |
| 74 | 92 |
| 75 SkPaint paint; | 93 SkPaint paint; |
| 76 paint.setFilterQuality(kLow_SkFilterQuality); | 94 paint.setFilterQuality(kLow_SkFilterQuality); |
| 77 | 95 |
| 78 for (int iy = 0; iy < 2; ++iy) { | 96 for (int iy = 0; iy < 2; ++iy) { |
| 79 for (int ix = 0; ix < 2; ++ix) { | 97 for (int ix = 0; ix < 2; ++ix) { |
| 80 int i = ix * 2 + iy; | 98 int i = ix * 2 + iy; |
| 81 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, | 99 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, |
| 82 size[i].width(), size[i].height()); | 100 size[i].width(), size[i].height()); |
| 83 canvas->drawBitmapNine(bm, center, r, &paint); | 101 canvas->drawBitmapNine(fBitmap, fCenter, r, &paint); |
| 102 canvas->drawImageNine(fImage, fCenter, r.makeOffset(360, 0), &pa
int); |
| 103 |
| 84 } | 104 } |
| 85 } | 105 } |
| 86 } | 106 } |
| 87 | 107 |
| 88 private: | 108 private: |
| 89 typedef GM INHERITED; | 109 typedef skiagm::GM INHERITED; |
| 90 }; | 110 }; |
| 111 DEF_GM( return new NinePatchStretchGM; ) |
| 91 | 112 |
| 92 ////////////////////////////////////////////////////////////////////////////// | |
| 93 | |
| 94 static GM* MyFactory(void*) { return new NinePatchStretchGM; } | |
| 95 static GMRegistry reg(MyFactory); | |
| 96 | |
| 97 } | |
| OLD | NEW |