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.h"
9
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPath.h"
13 #include "SkRandom.h"
14 #include "SkTo.h"
15
make_bm(SkBitmap * bm,int height)16 int make_bm(SkBitmap* bm, int height) {
17 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;
23
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
59 class TallStretchedBitmapsGM : public skiagm::GM {
60 public:
TallStretchedBitmapsGM()61 TallStretchedBitmapsGM() {}
62
63 protected:
onShortName()64 SkString onShortName() override {
65 return SkString("tall_stretched_bitmaps");
66 }
67
onISize()68 SkISize onISize() override {
69 return SkISize::Make(730, 690);
70 }
71
onOnceBeforeDraw()72 void onOnceBeforeDraw() override {
73 for (size_t i = 0; i < SK_ARRAY_COUNT(fTallBmps); ++i) {
74 int h = SkToInt((4 + i) * 1024);
75
76 fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h);
77 }
78 }
79
onDraw(SkCanvas * canvas)80 void onDraw(SkCanvas* canvas) override {
81 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;
92 paint.setFilterQuality(kLow_SkFilterQuality);
93 canvas->drawBitmapRect(bmp, subRect, dstRect, &paint);
94 canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
95 }
96 }
97
98 private:
99 struct {
100 SkBitmap fBmp;
101 int fItemCnt;
102 } fTallBmps[8];
103 typedef skiagm::GM INHERITED;
104 };
105
106 //////////////////////////////////////////////////////////////////////////////
107
108 DEF_GM(return new TallStretchedBitmapsGM;)
109