bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 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 | |
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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkFilterQuality.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/core/SkScalar.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
| 17 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/private/SkTo.h" |
| 19 | #include "include/utils/SkRandom.h" |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 20 | |
| 21 | int make_bm(SkBitmap* bm, int height) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 22 | constexpr int kRadius = 22; |
| 23 | constexpr int kMargin = 8; |
| 24 | constexpr SkScalar kStartAngle = 0; |
| 25 | constexpr SkScalar kDAngle = 25; |
| 26 | constexpr SkScalar kSweep = 320; |
| 27 | constexpr SkScalar kThickness = 8; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 28 | |
| 29 | int count = (height / (2 * kRadius + kMargin)); |
| 30 | height = count * (2 * kRadius + kMargin); |
| 31 | |
| 32 | bm->allocN32Pixels(2 * (kRadius + kMargin), height); |
| 33 | SkRandom random; |
| 34 | |
| 35 | SkCanvas wholeCanvas(*bm); |
| 36 | wholeCanvas.clear(0x00000000); |
| 37 | |
| 38 | SkScalar angle = kStartAngle; |
| 39 | for (int i = 0; i < count; ++i) { |
| 40 | SkPaint paint; |
| 41 | // The sw rasterizer disables AA for large canvii. So we make a small canvas for each draw. |
| 42 | SkBitmap smallBM; |
| 43 | SkIRect subRect = SkIRect::MakeXYWH(0, i * (kMargin + 2 * kRadius), |
| 44 | 2 * kRadius + kMargin, 2 * kRadius + kMargin); |
| 45 | bm->extractSubset(&smallBM, subRect); |
| 46 | SkCanvas canvas(smallBM); |
| 47 | canvas.translate(kMargin + kRadius, kMargin + kRadius); |
| 48 | |
| 49 | paint.setAntiAlias(true); |
| 50 | paint.setColor(random.nextU() | 0xFF000000); |
| 51 | paint.setStyle(SkPaint::kStroke_Style); |
| 52 | paint.setStrokeWidth(kThickness); |
| 53 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 54 | SkScalar radius = kRadius - kThickness / 2; |
| 55 | SkRect bounds = SkRect::MakeLTRB(-radius, -radius, radius, radius); |
| 56 | |
| 57 | canvas.drawArc(bounds, angle, kSweep, false, paint); |
| 58 | angle += kDAngle; |
| 59 | } |
| 60 | bm->setImmutable(); |
| 61 | return count; |
| 62 | } |
| 63 | |
| 64 | class TallStretchedBitmapsGM : public skiagm::GM { |
| 65 | public: |
| 66 | TallStretchedBitmapsGM() {} |
| 67 | |
| 68 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | SkString onShortName() override { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 70 | return SkString("tall_stretched_bitmaps"); |
| 71 | } |
| 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | SkISize onISize() override { |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 74 | return SkISize::Make(730, 690); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 75 | } |
| 76 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 77 | void onOnceBeforeDraw() override { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 78 | for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) { |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 79 | int h = SkToInt((4 + i) * 1024); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 80 | |
| 81 | fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h); |
| 82 | } |
| 83 | } |
| 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | void onDraw(SkCanvas* canvas) override { |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 86 | canvas->scale(1.3f, 1.3f); |
| 87 | for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) { |
| 88 | SkASSERT(fTallBmps[i].fItemCnt > 10); |
| 89 | SkBitmap bmp = fTallBmps[i].fBmp; |
| 90 | // Draw the last 10 elements of the bitmap. |
| 91 | int startItem = fTallBmps[i].fItemCnt - 10; |
| 92 | int itemHeight = bmp.height() / fTallBmps[i].fItemCnt; |
| 93 | SkIRect subRect = SkIRect::MakeLTRB(0, startItem * itemHeight, |
| 94 | bmp.width(), bmp.height()); |
| 95 | SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bmp.width()), 10.f * itemHeight); |
| 96 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 97 | paint.setFilterQuality(kLow_SkFilterQuality); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 98 | canvas->drawBitmapRect(bmp, subRect, dstRect, &paint); |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 99 | canvas->translate(SkIntToScalar(bmp.width() + 10), 0); |
| 100 | } |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | struct { |
| 105 | SkBitmap fBmp; |
| 106 | int fItemCnt; |
| 107 | } fTallBmps[8]; |
| 108 | typedef skiagm::GM INHERITED; |
| 109 | }; |
| 110 | |
| 111 | ////////////////////////////////////////////////////////////////////////////// |
| 112 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 113 | DEF_GM(return new TallStretchedBitmapsGM;) |