1
2 /*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9 #include "Benchmark.h"
10 #include "SkBitmap.h"
11 #include "SkCanvas.h"
12 #include "SkShader.h"
13 #include "SkString.h"
14
create_gradient(SkBitmap * bm)15 static 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 SkAutoLockPixels lock(*bm);
23 for (int y = 0; y < height; y++) {
24 *bm->getAddr32(0, y) = SkColorSetRGB(0, 0, (U8CPU) blue);
25 blue -= deltaB;
26 }
27 }
28
29 // Test out the special case of a tiled 1xN texture. Test out opacity,
30 // filtering and the different tiling modes
31 class ConstXTileBench : public Benchmark {
32 SkPaint fPaint;
33 SkString fName;
34 bool fDoFilter;
35 bool fDoTrans;
36 bool fDoScale;
37 static const int kWidth = 1;
38 static const int kHeight = 300;
39
40 public:
ConstXTileBench(SkShader::TileMode xTile,SkShader::TileMode yTile,bool doFilter,bool doTrans,bool doScale)41 ConstXTileBench(SkShader::TileMode xTile,
42 SkShader::TileMode yTile,
43 bool doFilter,
44 bool doTrans,
45 bool doScale)
46 : fDoFilter(doFilter)
47 , fDoTrans(doTrans)
48 , fDoScale(doScale) {
49 SkBitmap bm;
50
51 bm.allocN32Pixels(kWidth, kHeight, true);
52 bm.eraseColor(SK_ColorWHITE);
53
54 create_gradient(&bm);
55
56 SkShader* s = SkShader::CreateBitmapShader(bm, xTile, yTile);
57 fPaint.setShader(s)->unref();
58
59 fName.printf("constXTile_");
60
61 static const char* gTileModeStr[SkShader::kTileModeCount] = { "C", "R", "M" };
62 fName.append(gTileModeStr[xTile]);
63 fName.append(gTileModeStr[yTile]);
64
65 if (doFilter) {
66 fName.append("_filter");
67 }
68
69 if (doTrans) {
70 fName.append("_trans");
71 }
72
73 if (doScale) {
74 fName.append("_scale");
75 }
76 }
77
78 protected:
onGetName()79 virtual const char* onGetName() {
80 return fName.c_str();
81 }
82
onDraw(const int loops,SkCanvas * canvas)83 virtual void onDraw(const int loops, SkCanvas* canvas) {
84 SkPaint paint(fPaint);
85 this->setupPaint(&paint);
86 paint.setFilterLevel(fDoFilter ? SkPaint::kLow_FilterLevel
87 : SkPaint::kNone_FilterLevel);
88 if (fDoTrans) {
89 paint.setColor(SkColorSetARGBMacro(0x80, 0xFF, 0xFF, 0xFF));
90 }
91
92 SkRect r;
93
94 if (fDoScale) {
95 r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480));
96 canvas->scale(SK_ScalarHalf, SK_ScalarHalf);
97 } else {
98 r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480));
99 }
100
101 SkPaint bgPaint;
102 bgPaint.setColor(SK_ColorWHITE);
103
104 for (int i = 0; i < loops; i++) {
105 if (fDoTrans) {
106 canvas->drawRect(r, bgPaint);
107 }
108
109 canvas->drawRect(r, paint);
110 }
111 }
112
113 private:
114 typedef Benchmark INHERITED;
115 };
116
117 DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, false, true))
118 DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false))
119 DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, false, true))
120
121 DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false))
122 DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true))
123 DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, false, false))
124
125 DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true))
126 DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false))
127 DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, true, true))
128
129 DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false))
130 DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true))
131 DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false))
132