ninepatchstretch.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "gm/gm.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkFilterQuality.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkSurface.h"
  20. #include "tools/ToolUtils.h"
  21. static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) {
  22. SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
  23. return ToolUtils::makeSurface(root, info);
  24. }
  25. static sk_sp<SkImage> make_image(SkCanvas* root, SkIRect* center) {
  26. const int kFixed = 28;
  27. const int kStretchy = 8;
  28. const int kSize = 2*kFixed + kStretchy;
  29. auto surface(make_surface(root, kSize));
  30. SkCanvas* canvas = surface->getCanvas();
  31. SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
  32. const SkScalar strokeWidth = SkIntToScalar(6);
  33. const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
  34. center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
  35. SkPaint paint;
  36. paint.setAntiAlias(true);
  37. paint.setColor(0xFFFF0000);
  38. canvas->drawRoundRect(r, radius, radius, paint);
  39. r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
  40. paint.setColor(0x8800FF00);
  41. canvas->drawRect(r, paint);
  42. r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
  43. paint.setColor(0x880000FF);
  44. canvas->drawRect(r, paint);
  45. return surface->makeImageSnapshot();
  46. }
  47. static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
  48. SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
  49. bm->allocPixels(info);
  50. image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
  51. }
  52. class NinePatchStretchGM : public skiagm::GM {
  53. public:
  54. sk_sp<SkImage> fImage;
  55. SkBitmap fBitmap;
  56. SkIRect fCenter;
  57. NinePatchStretchGM() {}
  58. protected:
  59. SkString onShortName() override {
  60. return SkString("ninepatch-stretch");
  61. }
  62. SkISize onISize() override {
  63. return SkISize::Make(760, 800);
  64. }
  65. void onDraw(SkCanvas* canvas) override {
  66. if (nullptr == fBitmap.pixelRef() || !fImage->isValid(canvas->getGrContext())) {
  67. fImage = make_image(canvas, &fCenter);
  68. image_to_bitmap(fImage.get(), &fBitmap);
  69. }
  70. // amount of bm that should not be stretched (unless we have to)
  71. const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width());
  72. const SkSize size[] = {
  73. { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
  74. { fixed * 4 / 5, fixed * 4 }, // shrink in X
  75. { fixed * 4, fixed * 4 / 5 }, // shrink in Y
  76. { fixed * 4, fixed * 4 }
  77. };
  78. canvas->drawBitmap(fBitmap, 10, 10, nullptr);
  79. SkScalar x = SkIntToScalar(100);
  80. SkScalar y = SkIntToScalar(100);
  81. SkPaint paint;
  82. for (int filter = 0; filter < 2; filter++) {
  83. paint.setFilterQuality(filter == 0 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
  84. canvas->translate(0, filter * SkIntToScalar(400));
  85. for (int iy = 0; iy < 2; ++iy) {
  86. for (int ix = 0; ix < 2; ++ix) {
  87. int i = ix * 2 + iy;
  88. SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
  89. size[i].width(), size[i].height());
  90. canvas->drawBitmapNine(fBitmap, fCenter, r, &paint);
  91. canvas->drawImageNine(fImage.get(), fCenter, r.makeOffset(360, 0), &paint);
  92. }
  93. }
  94. }
  95. }
  96. private:
  97. typedef skiagm::GM INHERITED;
  98. };
  99. DEF_GM( return new NinePatchStretchGM; )