tallstretchedbitmaps.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2014 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/SkPaint.h"
  12. #include "include/core/SkRect.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkSize.h"
  15. #include "include/core/SkString.h"
  16. #include "include/core/SkTypes.h"
  17. #include "include/private/SkTo.h"
  18. #include "include/utils/SkRandom.h"
  19. int make_bm(SkBitmap* bm, int height) {
  20. constexpr int kRadius = 22;
  21. constexpr int kMargin = 8;
  22. constexpr SkScalar kStartAngle = 0;
  23. constexpr SkScalar kDAngle = 25;
  24. constexpr SkScalar kSweep = 320;
  25. constexpr SkScalar kThickness = 8;
  26. int count = (height / (2 * kRadius + kMargin));
  27. height = count * (2 * kRadius + kMargin);
  28. bm->allocN32Pixels(2 * (kRadius + kMargin), height);
  29. SkRandom random;
  30. SkCanvas wholeCanvas(*bm);
  31. wholeCanvas.clear(0x00000000);
  32. SkScalar angle = kStartAngle;
  33. for (int i = 0; i < count; ++i) {
  34. SkPaint paint;
  35. // The sw rasterizer disables AA for large canvii. So we make a small canvas for each draw.
  36. SkBitmap smallBM;
  37. SkIRect subRect = SkIRect::MakeXYWH(0, i * (kMargin + 2 * kRadius),
  38. 2 * kRadius + kMargin, 2 * kRadius + kMargin);
  39. bm->extractSubset(&smallBM, subRect);
  40. SkCanvas canvas(smallBM);
  41. canvas.translate(kMargin + kRadius, kMargin + kRadius);
  42. paint.setAntiAlias(true);
  43. paint.setColor(random.nextU() | 0xFF000000);
  44. paint.setStyle(SkPaint::kStroke_Style);
  45. paint.setStrokeWidth(kThickness);
  46. paint.setStrokeCap(SkPaint::kRound_Cap);
  47. SkScalar radius = kRadius - kThickness / 2;
  48. SkRect bounds = SkRect::MakeLTRB(-radius, -radius, radius, radius);
  49. canvas.drawArc(bounds, angle, kSweep, false, paint);
  50. angle += kDAngle;
  51. }
  52. bm->setImmutable();
  53. return count;
  54. }
  55. class TallStretchedBitmapsGM : public skiagm::GM {
  56. public:
  57. TallStretchedBitmapsGM() {}
  58. protected:
  59. SkString onShortName() override {
  60. return SkString("tall_stretched_bitmaps");
  61. }
  62. SkISize onISize() override {
  63. return SkISize::Make(730, 690);
  64. }
  65. void onOnceBeforeDraw() override {
  66. for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
  67. int h = SkToInt((4 + i) * 1024);
  68. fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
  69. }
  70. }
  71. void onDraw(SkCanvas* canvas) override {
  72. canvas->scale(1.3f, 1.3f);
  73. for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
  74. SkASSERT(fTallBmps[i].fItemCnt > 10);
  75. SkBitmap bmp = fTallBmps[i].fBmp;
  76. // Draw the last 10 elements of the bitmap.
  77. int startItem = fTallBmps[i].fItemCnt - 10;
  78. int itemHeight = bmp.height() / fTallBmps[i].fItemCnt;
  79. SkIRect subRect = SkIRect::MakeLTRB(0, startItem * itemHeight,
  80. bmp.width(), bmp.height());
  81. SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bmp.width()), 10.f * itemHeight);
  82. SkPaint paint;
  83. paint.setFilterQuality(kLow_SkFilterQuality);
  84. canvas->drawBitmapRect(bmp, subRect, dstRect, &paint);
  85. canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
  86. }
  87. }
  88. private:
  89. struct {
  90. SkBitmap fBmp;
  91. int fItemCnt;
  92. } fTallBmps[8];
  93. typedef skiagm::GM INHERITED;
  94. };
  95. //////////////////////////////////////////////////////////////////////////////
  96. DEF_GM(return new TallStretchedBitmapsGM;)