blob: a874981e86d65cab93c0bd6814e46af66859ae0c [file] [log] [blame]
bsalomon17168df2014-12-09 09:00:49 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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 Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkTo.h"
19#include "include/utils/SkRandom.h"
bsalomon17168df2014-12-09 09:00:49 -080020
21int make_bm(SkBitmap* bm, int height) {
mtkleindbfd7ab2016-09-01 11:24:54 -070022 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;
bsalomon17168df2014-12-09 09:00:49 -080028
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
64class TallStretchedBitmapsGM : public skiagm::GM {
65public:
66 TallStretchedBitmapsGM() {}
67
68protected:
mtklein36352bf2015-03-25 18:17:31 -070069 SkString onShortName() override {
bsalomon17168df2014-12-09 09:00:49 -080070 return SkString("tall_stretched_bitmaps");
71 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 SkISize onISize() override {
reed6dc14aa2016-04-11 07:46:38 -070074 return SkISize::Make(730, 690);
bsalomon17168df2014-12-09 09:00:49 -080075 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 void onOnceBeforeDraw() override {
bsalomon17168df2014-12-09 09:00:49 -080078 for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
bsalomonef3fcd82014-12-12 08:51:38 -080079 int h = SkToInt((4 + i) * 1024);
bsalomon17168df2014-12-09 09:00:49 -080080
81 fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
82 }
83 }
84
mtklein36352bf2015-03-25 18:17:31 -070085 void onDraw(SkCanvas* canvas) override {
bsalomon17168df2014-12-09 09:00:49 -080086 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;
reed93a12152015-03-16 10:08:34 -070097 paint.setFilterQuality(kLow_SkFilterQuality);
reed84984ef2015-07-17 07:09:43 -070098 canvas->drawBitmapRect(bmp, subRect, dstRect, &paint);
bsalomon17168df2014-12-09 09:00:49 -080099 canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
100 }
bsalomon17168df2014-12-09 09:00:49 -0800101 }
102
103private:
104 struct {
105 SkBitmap fBmp;
106 int fItemCnt;
107 } fTallBmps[8];
108 typedef skiagm::GM INHERITED;
109};
110
111//////////////////////////////////////////////////////////////////////////////
112
halcanary385fe4d2015-08-26 13:07:48 -0700113DEF_GM(return new TallStretchedBitmapsGM;)