Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
tallstretchedbitmaps.cpp
Go to the documentation of this file.
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"
13#include "include/core/SkRect.h"
15#include "include/core/SkSize.h"
19#include "src/base/SkRandom.h"
20#include "tools/GpuToolUtils.h"
21#include "tools/ToolUtils.h"
22
23int 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
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) {
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);
50
51 paint.setAntiAlias(true);
52 paint.setColor(random.nextU() | 0xFF000000);
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
67public:
69
70protected:
71 SkString getName() const override { return SkString("tall_stretched_bitmaps"); }
72
73 SkISize getISize() override { return SkISize::Make(730, 690); }
74
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
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);
95 SkRect::Make(subRect), dstRect,
98 canvas->translate(SkIntToScalar(bmp.width() + 10), 0);
99 }
100 }
101
102private:
103 struct {
106 } fTallBmps[8];
107 using INHERITED = skiagm::GM;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
int count
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkIntToScalar(x)
Definition SkScalar.h:57
constexpr int SkToInt(S x)
Definition SkTo.h:29
@ kMargin
static sk_sp< SkImage > make_bm()
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
void setImmutable()
Definition SkBitmap.cpp:400
bool extractSubset(SkBitmap *dst, const SkIRect &subset) const
Definition SkBitmap.cpp:453
int width() const
Definition SkBitmap.h:149
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
int height() const
Definition SkBitmap.h:158
void translate(SkScalar dx, SkScalar dy)
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition SkCanvas.h:1542
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawArc(const SkRect &oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint &paint)
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
void scale(SkScalar sx, SkScalar sy)
@ kRound_Cap
adds circle
Definition SkPaint.h:335
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
uint32_t nextU()
Definition SkRandom.h:42
SkString getName() const override
void onDraw(SkCanvas *canvas) override
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
sk_sp< SkImage > MakeTextureImage(SkCanvas *canvas, sk_sp< SkImage > orig)
SkScalar h
int32_t height
constexpr int kRadius
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition SkRect.h:91
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646