blob: 7f2e7a8e95ae3e7164fe613921f8622f95955edc [file] [log] [blame]
robertphillips@google.com8e679fe2013-01-17 19:51:41 +00001
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 "SkBenchmark.h"
10#include "SkBitmap.h"
11#include "SkCanvas.h"
12#include "SkShader.h"
13#include "SkString.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 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
31class ConstXTileBench : public SkBenchmark {
32 SkPaint fPaint;
33 SkString fName;
34 bool fDoFilter;
35 bool fDoTrans;
robertphillips@google.com1e305232013-01-22 20:29:16 +000036 bool fDoScale;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000037 enum { N = SkBENCHLOOP(20) };
38 static const int kWidth = 1;
39 static const int kHeight = 300;
40
41public:
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000042 ConstXTileBench(void* param,
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000043 SkShader::TileMode xTile,
44 SkShader::TileMode yTile,
45 bool doFilter,
robertphillips@google.com1e305232013-01-22 20:29:16 +000046 bool doTrans,
47 bool doScale)
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000048 : INHERITED(param)
49 , fDoFilter(doFilter)
robertphillips@google.com1e305232013-01-22 20:29:16 +000050 , fDoTrans(doTrans)
51 , fDoScale(doScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000052 SkBitmap bm;
53
54 bm.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight);
55
56 bm.allocPixels();
57 bm.eraseColor(SK_ColorWHITE);
58 bm.setIsOpaque(true);
59
60 create_gradient(&bm);
61
62 SkShader* s = SkShader::CreateBitmapShader(bm, xTile, yTile);
63 fPaint.setShader(s)->unref();
64
65 fName.printf("constXTile_");
66
67 static const char* gTileModeStr[SkShader::kTileModeCount] = { "C", "R", "M" };
68 fName.append(gTileModeStr[xTile]);
69 fName.append(gTileModeStr[yTile]);
70
71 if (doFilter) {
72 fName.append("_filter");
73 }
74
75 if (doTrans) {
76 fName.append("_trans");
77 }
robertphillips@google.com1e305232013-01-22 20:29:16 +000078
79 if (doScale) {
80 fName.append("_scale");
81 }
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000082 }
83
84protected:
85 virtual const char* onGetName() {
86 return fName.c_str();
87 }
88
89 virtual void onDraw(SkCanvas* canvas) {
90 SkPaint paint(fPaint);
91 this->setupPaint(&paint);
92 paint.setFilterBitmap(fDoFilter);
93 if (fDoTrans) {
94 paint.setColor(SkColorSetARGBMacro(0x80, 0xFF, 0xFF, 0xFF));
95 }
96
97 SkRect r;
98
robertphillips@google.com1e305232013-01-22 20:29:16 +000099 if (fDoScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000100 r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480));
101 canvas->scale(SK_ScalarHalf, SK_ScalarHalf);
102 } else {
103 r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480));
104 }
105
106 SkPaint bgPaint;
107 bgPaint.setColor(SK_ColorWHITE);
108
109 for (int i = 0; i < N; i++) {
110 if (fDoTrans) {
111 canvas->drawRect(r, bgPaint);
112 }
113
114 canvas->drawRect(r, paint);
115 }
116 }
117
118private:
119 typedef SkBenchmark INHERITED;
120};
121
robertphillips@google.com1e305232013-01-22 20:29:16 +0000122DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, false, true))
123DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false))
124DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, false, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000125
robertphillips@google.com1e305232013-01-22 20:29:16 +0000126DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false))
127DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true))
128DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, false, false))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000129
robertphillips@google.com1e305232013-01-22 20:29:16 +0000130DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true))
131DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false))
132DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, true, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000133
robertphillips@google.com1e305232013-01-22 20:29:16 +0000134DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false))
135DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true))
136DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false))