blob: 4a69ccbb2f797ea4cbc97151aa373d80d6a429da [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
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000021 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
tfarinaf168b862014-06-19 12:32:29 -070029class ConstXTileBench : public Benchmark {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000030 SkPaint fPaint;
31 SkString fName;
32 bool fDoFilter;
33 bool fDoTrans;
robertphillips@google.com1e305232013-01-22 20:29:16 +000034 bool fDoScale;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000035 static const int kWidth = 1;
36 static const int kHeight = 300;
37
38public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000039 ConstXTileBench(SkShader::TileMode xTile,
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000040 SkShader::TileMode yTile,
41 bool doFilter,
robertphillips@google.com1e305232013-01-22 20:29:16 +000042 bool doTrans,
43 bool doScale)
mtklein@google.com410e6e82013-09-13 19:52:27 +000044 : fDoFilter(doFilter)
robertphillips@google.com1e305232013-01-22 20:29:16 +000045 , fDoTrans(doTrans)
46 , fDoScale(doScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000047 SkBitmap bm;
48
reed6c225732014-06-09 19:52:07 -070049 bm.allocN32Pixels(kWidth, kHeight, true);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000050 bm.eraseColor(SK_ColorWHITE);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000051
52 create_gradient(&bm);
53
reedc6f28f72016-03-14 12:22:10 -070054 fPaint.setShader(SkShader::MakeBitmapShader(bm, xTile, yTile));
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000055
56 fName.printf("constXTile_");
57
58 static const char* gTileModeStr[SkShader::kTileModeCount] = { "C", "R", "M" };
59 fName.append(gTileModeStr[xTile]);
60 fName.append(gTileModeStr[yTile]);
61
62 if (doFilter) {
63 fName.append("_filter");
64 }
65
66 if (doTrans) {
67 fName.append("_trans");
68 }
robertphillips@google.com1e305232013-01-22 20:29:16 +000069
70 if (doScale) {
71 fName.append("_scale");
72 }
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000073 }
74
75protected:
76 virtual const char* onGetName() {
77 return fName.c_str();
78 }
79
mtkleina1ebeb22015-10-01 09:43:39 -070080 virtual void onDraw(int loops, SkCanvas* canvas) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000081 SkPaint paint(fPaint);
82 this->setupPaint(&paint);
reed93a12152015-03-16 10:08:34 -070083 paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality
84 : kNone_SkFilterQuality);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000085 if (fDoTrans) {
Cary Clark6cdb7d32018-04-24 14:47:16 -040086 paint.setColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000087 }
88
89 SkRect r;
90
robertphillips@google.com1e305232013-01-22 20:29:16 +000091 if (fDoScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000092 r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480));
93 canvas->scale(SK_ScalarHalf, SK_ScalarHalf);
94 } else {
95 r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480));
96 }
97
98 SkPaint bgPaint;
99 bgPaint.setColor(SK_ColorWHITE);
100
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000101 for (int i = 0; i < loops; i++) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000102 if (fDoTrans) {
103 canvas->drawRect(r, bgPaint);
104 }
105
106 canvas->drawRect(r, paint);
107 }
108 }
109
110private:
tfarinaf168b862014-06-19 12:32:29 -0700111 typedef Benchmark INHERITED;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000112};
113
mtklein474cef42014-06-24 15:19:25 -0700114// Scaled benches are trending towards free. Seems like caching.
115// TODO(mtklein, reed): fix and reenable
116
117//DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, false, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000118DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false))
mtklein474cef42014-06-24 15:19:25 -0700119//DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, false, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000120
mtklein@google.com410e6e82013-09-13 19:52:27 +0000121DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false))
mtklein474cef42014-06-24 15:19:25 -0700122//DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000123DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, false, false))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000124
mtklein474cef42014-06-24 15:19:25 -0700125//DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000126DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false))
mtklein474cef42014-06-24 15:19:25 -0700127//DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, true, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000128
mtklein@google.com410e6e82013-09-13 19:52:27 +0000129DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false))
mtklein474cef42014-06-24 15:19:25 -0700130//DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000131DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false))