blob: 02011914f03c737b86c54ea3008d8c9579178327 [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"
Mike Reed607a3822021-01-24 19:49:21 -050011#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#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"
Kevin Lubick19936eb2023-01-05 09:00:37 -050018#include "include/private/base/SkTo.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050019#include "src/base/SkRandom.h"
Kevin Lubick9b028372023-10-05 15:04:54 -040020#include "tools/GpuToolUtils.h"
Robert Phillips0fb10ab2022-04-20 14:57:03 -040021#include "tools/ToolUtils.h"
bsalomon17168df2014-12-09 09:00:49 -080022
23int make_bm(SkBitmap* bm, int height) {
mtkleindbfd7ab2016-09-01 11:24:54 -070024 constexpr int kRadius = 22;
25 constexpr int kMargin = 8;
26 constexpr SkScalar kStartAngle = 0;
27 constexpr SkScalar kDAngle = 25;
28 constexpr SkScalar kSweep = 320;
29 constexpr SkScalar kThickness = 8;
bsalomon17168df2014-12-09 09:00:49 -080030
31 int count = (height / (2 * kRadius + kMargin));
32 height = count * (2 * kRadius + kMargin);
33
34 bm->allocN32Pixels(2 * (kRadius + kMargin), height);
35 SkRandom random;
36
37 SkCanvas wholeCanvas(*bm);
38 wholeCanvas.clear(0x00000000);
39
40 SkScalar angle = kStartAngle;
41 for (int i = 0; i < count; ++i) {
42 SkPaint paint;
43 // The sw rasterizer disables AA for large canvii. So we make a small canvas for each draw.
44 SkBitmap smallBM;
45 SkIRect subRect = SkIRect::MakeXYWH(0, i * (kMargin + 2 * kRadius),
46 2 * kRadius + kMargin, 2 * kRadius + kMargin);
47 bm->extractSubset(&smallBM, subRect);
48 SkCanvas canvas(smallBM);
49 canvas.translate(kMargin + kRadius, kMargin + kRadius);
50
51 paint.setAntiAlias(true);
52 paint.setColor(random.nextU() | 0xFF000000);
53 paint.setStyle(SkPaint::kStroke_Style);
54 paint.setStrokeWidth(kThickness);
55 paint.setStrokeCap(SkPaint::kRound_Cap);
56 SkScalar radius = kRadius - kThickness / 2;
57 SkRect bounds = SkRect::MakeLTRB(-radius, -radius, radius, radius);
58
59 canvas.drawArc(bounds, angle, kSweep, false, paint);
60 angle += kDAngle;
61 }
62 bm->setImmutable();
63 return count;
64}
65
66class TallStretchedBitmapsGM : public skiagm::GM {
67public:
68 TallStretchedBitmapsGM() {}
69
70protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000071 SkString getName() const override { return SkString("tall_stretched_bitmaps"); }
bsalomon17168df2014-12-09 09:00:49 -080072
Leandro Lovisolo8f023882023-08-15 21:13:52 +000073 SkISize getISize() override { return SkISize::Make(730, 690); }
bsalomon17168df2014-12-09 09:00:49 -080074
mtklein36352bf2015-03-25 18:17:31 -070075 void onOnceBeforeDraw() override {
Herb Derbyc37b3862022-06-21 09:49:17 -040076 for (size_t i = 0; i < std::size(fTallBmps); ++i) {
bsalomonef3fcd82014-12-12 08:51:38 -080077 int h = SkToInt((4 + i) * 1024);
bsalomon17168df2014-12-09 09:00:49 -080078
79 fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
80 }
81 }
82
mtklein36352bf2015-03-25 18:17:31 -070083 void onDraw(SkCanvas* canvas) override {
bsalomon17168df2014-12-09 09:00:49 -080084 canvas->scale(1.3f, 1.3f);
Herb Derbyc37b3862022-06-21 09:49:17 -040085 for (size_t i = 0; i < std::size(fTallBmps); ++i) {
bsalomon17168df2014-12-09 09:00:49 -080086 SkASSERT(fTallBmps[i].fItemCnt > 10);
87 SkBitmap bmp = fTallBmps[i].fBmp;
88 // Draw the last 10 elements of the bitmap.
89 int startItem = fTallBmps[i].fItemCnt - 10;
90 int itemHeight = bmp.height() / fTallBmps[i].fItemCnt;
91 SkIRect subRect = SkIRect::MakeLTRB(0, startItem * itemHeight,
92 bmp.width(), bmp.height());
93 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bmp.width()), 10.f * itemHeight);
Robert Phillips0fb10ab2022-04-20 14:57:03 -040094 canvas->drawImageRect(ToolUtils::MakeTextureImage(canvas, bmp.asImage()),
95 SkRect::Make(subRect), dstRect,
Mike Reed607a3822021-01-24 19:49:21 -050096 SkSamplingOptions(SkFilterMode::kLinear), nullptr,
97 SkCanvas::kStrict_SrcRectConstraint);
bsalomon17168df2014-12-09 09:00:49 -080098 canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
99 }
bsalomon17168df2014-12-09 09:00:49 -0800100 }
101
102private:
103 struct {
104 SkBitmap fBmp;
105 int fItemCnt;
106 } fTallBmps[8];
John Stiles7571f9e2020-09-02 22:42:33 -0400107 using INHERITED = skiagm::GM;
bsalomon17168df2014-12-09 09:00:49 -0800108};
109
110//////////////////////////////////////////////////////////////////////////////
111
halcanary385fe4d2015-08-26 13:07:48 -0700112DEF_GM(return new TallStretchedBitmapsGM;)