| 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/SkTiledImageUtils.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "include/private/base/SkTo.h" |
| 20 |
#include "src/base/SkRandom.h" |
| 21 |
|
| 22 |
int make_bm(SkBitmap* bm, int height) { |
| 23 |
constexpr int kRadius = 22; |
| 24 |
constexpr int kMargin = 8; |
| 25 |
constexpr SkScalar kStartAngle = 0; |
| 26 |
constexpr SkScalar kDAngle = 25; |
| 27 |
constexpr SkScalar kSweep = 320; |
| 28 |
constexpr SkScalar kThickness = 8; |
| 29 |
|
| 30 |
int count = (height / (2 * kRadius + kMargin)); |
| 31 |
height = count * (2 * kRadius + kMargin); |
| 32 |
|
| 33 |
bm->allocN32Pixels(2 * (kRadius + kMargin), height); |
| 34 |
SkRandom random; |
| 35 |
|
| 36 |
SkCanvas wholeCanvas(*bm); |
| 37 |
wholeCanvas.clear(0x00000000); |
| 38 |
|
| 39 |
SkScalar angle = kStartAngle; |
| 40 |
for (int i = 0; i < count; ++i) { |
| 41 |
SkPaint paint; |
| 42 |
// The sw rasterizer disables AA for large canvii. So we make a small canvas for each draw. |
| 43 |
SkBitmap smallBM; |
| 44 |
SkIRect subRect = SkIRect::MakeXYWH(0, i * (kMargin + 2 * kRadius), |
| 45 |
2 * kRadius + kMargin, 2 * kRadius + kMargin); |
| 46 |
bm->extractSubset(&smallBM, subRect); |
| 47 |
SkCanvas canvas(smallBM); |
| 48 |
canvas.translate(kMargin + kRadius, kMargin + kRadius); |
| 49 |
|
| 50 |
paint.setAntiAlias(true); |
| 51 |
paint.setColor(random.nextU() | 0xFF000000); |
| 52 |
paint.setStyle(SkPaint::kStroke_Style); |
| 53 |
paint.setStrokeWidth(kThickness); |
| 54 |
paint.setStrokeCap(SkPaint::kRound_Cap); |
| 55 |
SkScalar radius = kRadius - kThickness / 2; |
| 56 |
SkRect bounds = SkRect::MakeLTRB(-radius, -radius, radius, radius); |
| 57 |
|
| 58 |
canvas.drawArc(bounds, angle, kSweep, false, paint); |
| 59 |
angle += kDAngle; |
| 60 |
} |
| 61 |
bm->setImmutable(); |
| 62 |
return count; |
| 63 |
} |
| 64 |
|
| 65 |
class TallStretchedBitmapsGM : public skiagm::GM { |
| 66 |
public: |
| 67 |
TallStretchedBitmapsGM() {} |
| 68 |
|
| 69 |
protected: |
| 70 |
SkString getName() const override { return SkString("tall_stretched_bitmaps"); } |
| 71 |
|
| 72 |
SkISize getISize() override { return SkISize::Make(730, 690); } |
| 73 |
|
| 74 |
void onOnceBeforeDraw() override { |
| 75 |
for (size_t i = 0; i < std::size(fTallBmps); ++i) { |
| 76 |
int h = SkToInt((4 + i) * 1024); |
| 77 |
|
| 78 |
fTallBmps[i].fItemCnt = make_bm(&fTallBmps[i].fBmp, h); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void onDraw(SkCanvas* canvas) override { |
| 83 |
canvas->scale(1.3f, 1.3f); |
| 84 |
for (size_t i = 0; i < std::size(fTallBmps); ++i) { |
| 85 |
SkASSERT(fTallBmps[i].fItemCnt > 10); |
| 86 |
SkBitmap bmp = fTallBmps[i].fBmp; |
| 87 |
// Draw the last 10 elements of the bitmap. |
| 88 |
int startItem = fTallBmps[i].fItemCnt - 10; |
| 89 |
int itemHeight = bmp.height() / fTallBmps[i].fItemCnt; |
| 90 |
SkIRect subRect = SkIRect::MakeLTRB(0, startItem * itemHeight, |
| 91 |
bmp.width(), bmp.height()); |
| 92 |
SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bmp.width()), 10.f * itemHeight); |
| 93 |
SkTiledImageUtils::DrawImageRect(canvas, bmp.asImage(), |
| 94 |
SkRect::Make(subRect), dstRect, |
| 95 |
SkSamplingOptions(SkFilterMode::kLinear), nullptr, |
| 96 |
SkCanvas::kStrict_SrcRectConstraint); |
| 97 |
canvas->translate(SkIntToScalar(bmp.width() + 10), 0); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
private: |
| 102 |
struct { |
| 103 |
SkBitmap fBmp; |
| 104 |
int fItemCnt; |
| 105 |
} fTallBmps[8]; |
| 106 |
using INHERITED = skiagm::GM; |
| 107 |
}; |
| 108 |
|
| 109 |
////////////////////////////////////////////////////////////////////////////// |
| 110 |
|
| 111 |
DEF_GM(return new TallStretchedBitmapsGM;) |