blob: a16f54ac9624d2ad71420bbb473ede6227371c4d [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
8#include "gm.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
bsalomon17168df2014-12-09 09:00:49 -080010#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkPath.h"
13#include "SkRandom.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040014#include "SkTo.h"
bsalomon17168df2014-12-09 09:00:49 -080015
16int make_bm(SkBitmap* bm, int height) {
mtkleindbfd7ab2016-09-01 11:24:54 -070017 constexpr int kRadius = 22;
18 constexpr int kMargin = 8;
19 constexpr SkScalar kStartAngle = 0;
20 constexpr SkScalar kDAngle = 25;
21 constexpr SkScalar kSweep = 320;
22 constexpr SkScalar kThickness = 8;
bsalomon17168df2014-12-09 09:00:49 -080023
24 int count = (height / (2 * kRadius + kMargin));
25 height = count * (2 * kRadius + kMargin);
26
27 bm->allocN32Pixels(2 * (kRadius + kMargin), height);
28 SkRandom random;
29
30 SkCanvas wholeCanvas(*bm);
31 wholeCanvas.clear(0x00000000);
32
33 SkScalar angle = kStartAngle;
34 for (int i = 0; i < count; ++i) {
35 SkPaint paint;
36 // The sw rasterizer disables AA for large canvii. So we make a small canvas for each draw.
37 SkBitmap smallBM;
38 SkIRect subRect = SkIRect::MakeXYWH(0, i * (kMargin + 2 * kRadius),
39 2 * kRadius + kMargin, 2 * kRadius + kMargin);
40 bm->extractSubset(&smallBM, subRect);
41 SkCanvas canvas(smallBM);
42 canvas.translate(kMargin + kRadius, kMargin + kRadius);
43
44 paint.setAntiAlias(true);
45 paint.setColor(random.nextU() | 0xFF000000);
46 paint.setStyle(SkPaint::kStroke_Style);
47 paint.setStrokeWidth(kThickness);
48 paint.setStrokeCap(SkPaint::kRound_Cap);
49 SkScalar radius = kRadius - kThickness / 2;
50 SkRect bounds = SkRect::MakeLTRB(-radius, -radius, radius, radius);
51
52 canvas.drawArc(bounds, angle, kSweep, false, paint);
53 angle += kDAngle;
54 }
55 bm->setImmutable();
56 return count;
57}
58
59class TallStretchedBitmapsGM : public skiagm::GM {
60public:
61 TallStretchedBitmapsGM() {}
62
63protected:
mtklein36352bf2015-03-25 18:17:31 -070064 SkString onShortName() override {
bsalomon17168df2014-12-09 09:00:49 -080065 return SkString("tall_stretched_bitmaps");
66 }
67
mtklein36352bf2015-03-25 18:17:31 -070068 SkISize onISize() override {
reed6dc14aa2016-04-11 07:46:38 -070069 return SkISize::Make(730, 690);
bsalomon17168df2014-12-09 09:00:49 -080070 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 void onOnceBeforeDraw() override {
bsalomon17168df2014-12-09 09:00:49 -080073 for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
bsalomonef3fcd82014-12-12 08:51:38 -080074 int h = SkToInt((4 + i) * 1024);
bsalomon17168df2014-12-09 09:00:49 -080075
76 fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
77 }
78 }
79
mtklein36352bf2015-03-25 18:17:31 -070080 void onDraw(SkCanvas* canvas) override {
bsalomon17168df2014-12-09 09:00:49 -080081 canvas->scale(1.3f, 1.3f);
82 for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
83 SkASSERT(fTallBmps[i].fItemCnt > 10);
84 SkBitmap bmp = fTallBmps[i].fBmp;
85 // Draw the last 10 elements of the bitmap.
86 int startItem = fTallBmps[i].fItemCnt - 10;
87 int itemHeight = bmp.height() / fTallBmps[i].fItemCnt;
88 SkIRect subRect = SkIRect::MakeLTRB(0, startItem * itemHeight,
89 bmp.width(), bmp.height());
90 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bmp.width()), 10.f * itemHeight);
91 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070092 paint.setFilterQuality(kLow_SkFilterQuality);
reed84984ef2015-07-17 07:09:43 -070093 canvas->drawBitmapRect(bmp, subRect, dstRect, &paint);
bsalomon17168df2014-12-09 09:00:49 -080094 canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
95 }
bsalomon17168df2014-12-09 09:00:49 -080096 }
97
98private:
99 struct {
100 SkBitmap fBmp;
101 int fItemCnt;
102 } fTallBmps[8];
103 typedef skiagm::GM INHERITED;
104};
105
106//////////////////////////////////////////////////////////////////////////////
107
halcanary385fe4d2015-08-26 13:07:48 -0700108DEF_GM(return new TallStretchedBitmapsGM;)