Flutter Engine
The Flutter Engine
TileBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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 "bench/Benchmark.h"
14
15static void create_gradient(SkBitmap* bm) {
16 SkASSERT(1 == bm->width());
17 const int height = bm->height();
18
19 float deltaB = 255.0f / height;
20 float blue = 255.0f;
21
22 for (int y = 0; y < height; y++) {
23 *bm->getAddr32(0, y) = SkColorSetRGB(0, 0, (U8CPU) blue);
24 blue -= deltaB;
25 }
26}
27
28// Test out the special case of a tiled 1xN texture. Test out opacity,
29// filtering and the different tiling modes
30class ConstXTileBench : public Benchmark {
31public:
33 SkTileMode yTile,
34 SkFilterMode fm,
35 bool doTrans,
36 bool doScale)
37 : fFilterMode{fm}
38 , fXTile{xTile}
39 , fYTile{yTile}
40 , fDoTrans{doTrans}
41 , fDoScale{doScale} {
42 fName.printf("constXTile_");
43
44 static const char* gTileModeStr[kSkTileModeCount] = { "C", "R", "M", "D" };
45 fName.append(gTileModeStr[(unsigned)xTile]);
46 fName.append(gTileModeStr[(unsigned)yTile]);
47
48 if (fm != SkFilterMode::kNearest) {
49 fName.append("_filter");
50 }
51
52 if (doTrans) {
53 fName.append("_trans");
54 }
55
56 if (doScale) {
57 fName.append("_scale");
58 }
59 }
60
61protected:
62 const char* onGetName() override {
63 return fName.c_str();
64 }
65
66 void onDelayedSetup() override {
67 SkBitmap bm;
68
69 bm.allocN32Pixels(kWidth, kHeight, true);
71
72 create_gradient(&bm);
73
74 fPaint.setShader(bm.makeShader(fXTile, fYTile, SkSamplingOptions(fFilterMode)));
75 }
76
77 void onDraw(int loops, SkCanvas* canvas) override {
78 SkPaint paint(fPaint);
79 this->setupPaint(&paint);
80 if (fDoTrans) {
81 paint.setColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
82 }
83
84 SkRect r;
85
86 if (fDoScale) {
87 r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480));
89 } else {
91 }
92
93 SkPaint bgPaint;
94 bgPaint.setColor(SK_ColorWHITE);
95
96 for (int i = 0; i < loops; i++) {
97 if (fDoTrans) {
98 canvas->drawRect(r, bgPaint);
99 }
100
101 canvas->drawRect(r, paint);
102 }
103 }
104
105private:
106 inline static constexpr int kWidth = 1;
107 inline static constexpr int kHeight = 300;
108
109 const SkFilterMode fFilterMode;
110 const SkTileMode fXTile,
111 fYTile;
112 const bool fDoTrans,
113 fDoScale;
114 SkPaint fPaint;
115 SkString fName;
116 using INHERITED = Benchmark;
117};
118
119// Scaled benches are trending towards free. Seems like caching.
120// TODO(mtklein, reed): fix and reenable
121
124
125//DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, false, false, true))
127//DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, false, false, true))
128
129DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, gLI, false, false))
130//DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, true, false, true))
131DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, gLI, false, false))
132
133//DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, false, true, true))
134DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, gNN, true, false))
135//DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, false, true, true))
136
137DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, gLI, true, false))
138//DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, true, true, true))
139DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, gLI, true, false))
#define DEF_BENCH(code)
Definition: Benchmark.h:20
#define SkASSERT(cond)
Definition: SkAssert.h:116
unsigned U8CPU
Definition: SkCPUTypes.h:18
#define SkColorSetRGB(r, g, b)
Definition: SkColor.h:57
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
SkFilterMode
#define SK_ScalarHalf
Definition: SkScalar.h:19
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SkTileMode
Definition: SkTileMode.h:13
static constexpr int kSkTileModeCount
Definition: SkTileMode.h:39
constexpr SkFilterMode gNN
Definition: TileBench.cpp:122
constexpr SkFilterMode gLI
Definition: TileBench.cpp:123
static void create_gradient(SkBitmap *bm)
Definition: TileBench.cpp:15
virtual void setupPaint(SkPaint *paint)
Definition: Benchmark.cpp:55
void onDraw(int loops, SkCanvas *canvas) override
Definition: TileBench.cpp:77
ConstXTileBench(SkTileMode xTile, SkTileMode yTile, SkFilterMode fm, bool doTrans, bool doScale)
Definition: TileBench.cpp:32
const char * onGetName() override
Definition: TileBench.cpp:62
void onDelayedSetup() override
Definition: TileBench.cpp:66
int width() const
Definition: SkBitmap.h:149
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
int height() const
Definition: SkBitmap.h:158
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setShader(sk_sp< SkShader > shader)
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
void append(const char text[])
Definition: SkString.h:203
const char * c_str() const
Definition: SkString.h:133
const Paint & paint
Definition: color_source.cc:38
double y
int32_t height
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609