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