blob: 3093016f72269b7757e8a72ba8ca01eba8a0fc02 [file] [log] [blame]
robertphillips@google.com8e679fe2013-01-17 19:51:41 +00001/*
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
robertphillips@google.com8e679fe2013-01-17 19:51:41 +00009#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkShader.h"
12#include "SkString.h"
13
14static 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 SkAutoLockPixels lock(*bm);
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
tfarinaf168b862014-06-19 12:32:29 -070030class ConstXTileBench : public Benchmark {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000031 SkPaint fPaint;
32 SkString fName;
33 bool fDoFilter;
34 bool fDoTrans;
robertphillips@google.com1e305232013-01-22 20:29:16 +000035 bool fDoScale;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000036 static const int kWidth = 1;
37 static const int kHeight = 300;
38
39public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000040 ConstXTileBench(SkShader::TileMode xTile,
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000041 SkShader::TileMode yTile,
42 bool doFilter,
robertphillips@google.com1e305232013-01-22 20:29:16 +000043 bool doTrans,
44 bool doScale)
mtklein@google.com410e6e82013-09-13 19:52:27 +000045 : fDoFilter(doFilter)
robertphillips@google.com1e305232013-01-22 20:29:16 +000046 , fDoTrans(doTrans)
47 , fDoScale(doScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000048 SkBitmap bm;
49
reed6c225732014-06-09 19:52:07 -070050 bm.allocN32Pixels(kWidth, kHeight, true);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000051 bm.eraseColor(SK_ColorWHITE);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000052
53 create_gradient(&bm);
54
reedc6f28f72016-03-14 12:22:10 -070055 fPaint.setShader(SkShader::MakeBitmapShader(bm, xTile, yTile));
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000056
57 fName.printf("constXTile_");
58
59 static const char* gTileModeStr[SkShader::kTileModeCount] = { "C", "R", "M" };
60 fName.append(gTileModeStr[xTile]);
61 fName.append(gTileModeStr[yTile]);
62
63 if (doFilter) {
64 fName.append("_filter");
65 }
66
67 if (doTrans) {
68 fName.append("_trans");
69 }
robertphillips@google.com1e305232013-01-22 20:29:16 +000070
71 if (doScale) {
72 fName.append("_scale");
73 }
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000074 }
75
76protected:
77 virtual const char* onGetName() {
78 return fName.c_str();
79 }
80
mtkleina1ebeb22015-10-01 09:43:39 -070081 virtual void onDraw(int loops, SkCanvas* canvas) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000082 SkPaint paint(fPaint);
83 this->setupPaint(&paint);
reed93a12152015-03-16 10:08:34 -070084 paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality
85 : kNone_SkFilterQuality);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000086 if (fDoTrans) {
87 paint.setColor(SkColorSetARGBMacro(0x80, 0xFF, 0xFF, 0xFF));
88 }
89
90 SkRect r;
91
robertphillips@google.com1e305232013-01-22 20:29:16 +000092 if (fDoScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000093 r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480));
94 canvas->scale(SK_ScalarHalf, SK_ScalarHalf);
95 } else {
96 r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480));
97 }
98
99 SkPaint bgPaint;
100 bgPaint.setColor(SK_ColorWHITE);
101
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000102 for (int i = 0; i < loops; i++) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000103 if (fDoTrans) {
104 canvas->drawRect(r, bgPaint);
105 }
106
107 canvas->drawRect(r, paint);
108 }
109 }
110
111private:
tfarinaf168b862014-06-19 12:32:29 -0700112 typedef Benchmark INHERITED;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000113};
114
mtklein474cef42014-06-24 15:19:25 -0700115// Scaled benches are trending towards free. Seems like caching.
116// TODO(mtklein, reed): fix and reenable
117
118//DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, false, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000119DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false))
mtklein474cef42014-06-24 15:19:25 -0700120//DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, false, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000121
mtklein@google.com410e6e82013-09-13 19:52:27 +0000122DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false))
mtklein474cef42014-06-24 15:19:25 -0700123//DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000124DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, false, false))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000125
mtklein474cef42014-06-24 15:19:25 -0700126//DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000127DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false))
mtklein474cef42014-06-24 15:19:25 -0700128//DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, true, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000129
mtklein@google.com410e6e82013-09-13 19:52:27 +0000130DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false))
mtklein474cef42014-06-24 15:19:25 -0700131//DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000132DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false))