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/SkTo.h"
19 #include "include/utils/SkRandom.h"
20
make_bm(SkBitmap * bm,int height)21 int make_bm(SkBitmap* bm, int height) {
22 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;
28
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
64 class TallStretchedBitmapsGM : public skiagm::GM {
65 public:
TallStretchedBitmapsGM()66 TallStretchedBitmapsGM() {}
67
68 protected:
onShortName()69 SkString onShortName() override {
70 return SkString("tall_stretched_bitmaps");
71 }
72
onISize()73 SkISize onISize() override {
74 return SkISize::Make(730, 690);
75 }
76
onOnceBeforeDraw()77 void onOnceBeforeDraw() override {
78 for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
79 int h = SkToInt((4 + i) * 1024);
80
81 fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
82 }
83 }
84
onDraw(SkCanvas * canvas)85 void onDraw(SkCanvas* canvas) override {
86 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 canvas->drawImageRect(bmp.asImage(), SkRect::Make(subRect), dstRect,
97 SkSamplingOptions(SkFilterMode::kLinear), nullptr,
98 SkCanvas::kStrict_SrcRectConstraint);
99 canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
100 }
101 }
102
103 private:
104 struct {
105 SkBitmap fBmp;
106 int fItemCnt;
107 } fTallBmps[8];
108 using INHERITED = skiagm::GM;
109 };
110
111 //////////////////////////////////////////////////////////////////////////////
112
113 DEF_GM(return new TallStretchedBitmapsGM;)
114