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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkImage.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"
18 #include "include/private/base/SkTo.h"
19 #include "src/base/SkRandom.h"
20 #include "tools/GpuToolUtils.h"
21 #include "tools/ToolUtils.h"
22
make_bm(SkBitmap * bm,int height)23 int make_bm(SkBitmap* bm, int height) {
24 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;
30
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
66 class TallStretchedBitmapsGM : public skiagm::GM {
67 public:
TallStretchedBitmapsGM()68 TallStretchedBitmapsGM() {}
69
70 protected:
getName() const71 SkString getName() const override { return SkString("tall_stretched_bitmaps"); }
72
getISize()73 SkISize getISize() override { return SkISize::Make(730, 690); }
74
onOnceBeforeDraw()75 void onOnceBeforeDraw() override {
76 for (size_t i = 0; i < std::size(fTallBmps); ++i) {
77 int h = SkToInt((4 + i) * 1024);
78
79 fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
80 }
81 }
82
onDraw(SkCanvas * canvas)83 void onDraw(SkCanvas* canvas) override {
84 canvas->scale(1.3f, 1.3f);
85 for (size_t i = 0; i < std::size(fTallBmps); ++i) {
86 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);
94 canvas->drawImageRect(ToolUtils::MakeTextureImage(canvas, bmp.asImage()),
95 SkRect::Make(subRect), dstRect,
96 SkSamplingOptions(SkFilterMode::kLinear), nullptr,
97 SkCanvas::kStrict_SrcRectConstraint);
98 canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
99 }
100 }
101
102 private:
103 struct {
104 SkBitmap fBmp;
105 int fItemCnt;
106 } fTallBmps[8];
107 using INHERITED = skiagm::GM;
108 };
109
110 //////////////////////////////////////////////////////////////////////////////
111
112 DEF_GM(return new TallStretchedBitmapsGM;)
113