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