blob: 6b91ada0c3c141a4f56f318eff281b8a9437121b [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
tfarinaf168b862014-06-19 12:32:29 -07009#include "Benchmark.h"
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000010#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
tfarinaf168b862014-06-19 12:32:29 -070031class ConstXTileBench : public Benchmark {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000032 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 static const int kWidth = 1;
38 static const int kHeight = 300;
39
40public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000041 ConstXTileBench(SkShader::TileMode xTile,
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000042 SkShader::TileMode yTile,
43 bool doFilter,
robertphillips@google.com1e305232013-01-22 20:29:16 +000044 bool doTrans,
45 bool doScale)
mtklein@google.com410e6e82013-09-13 19:52:27 +000046 : fDoFilter(doFilter)
robertphillips@google.com1e305232013-01-22 20:29:16 +000047 , fDoTrans(doTrans)
48 , fDoScale(doScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000049 SkBitmap bm;
50
reed6c225732014-06-09 19:52:07 -070051 bm.allocN32Pixels(kWidth, kHeight, true);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000052 bm.eraseColor(SK_ColorWHITE);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000053
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 }
robertphillips@google.com1e305232013-01-22 20:29:16 +000072
73 if (doScale) {
74 fName.append("_scale");
75 }
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000076 }
77
78protected:
79 virtual const char* onGetName() {
80 return fName.c_str();
81 }
82
mtkleina1ebeb22015-10-01 09:43:39 -070083 virtual void onDraw(int loops, SkCanvas* canvas) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000084 SkPaint paint(fPaint);
85 this->setupPaint(&paint);
reed93a12152015-03-16 10:08:34 -070086 paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality
87 : kNone_SkFilterQuality);
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000088 if (fDoTrans) {
89 paint.setColor(SkColorSetARGBMacro(0x80, 0xFF, 0xFF, 0xFF));
90 }
91
92 SkRect r;
93
robertphillips@google.com1e305232013-01-22 20:29:16 +000094 if (fDoScale) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +000095 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
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000104 for (int i = 0; i < loops; i++) {
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000105 if (fDoTrans) {
106 canvas->drawRect(r, bgPaint);
107 }
108
109 canvas->drawRect(r, paint);
110 }
111 }
112
113private:
tfarinaf168b862014-06-19 12:32:29 -0700114 typedef Benchmark INHERITED;
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000115};
116
mtklein474cef42014-06-24 15:19:25 -0700117// Scaled benches are trending towards free. Seems like caching.
118// TODO(mtklein, reed): fix and reenable
119
120//DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, false, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000121DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false))
mtklein474cef42014-06-24 15:19:25 -0700122//DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, false, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000123
mtklein@google.com410e6e82013-09-13 19:52:27 +0000124DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false))
mtklein474cef42014-06-24 15:19:25 -0700125//DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000126DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, false, false))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000127
mtklein474cef42014-06-24 15:19:25 -0700128//DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000129DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false))
mtklein474cef42014-06-24 15:19:25 -0700130//DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, true, true))
robertphillips@google.com8e679fe2013-01-17 19:51:41 +0000131
mtklein@google.com410e6e82013-09-13 19:52:27 +0000132DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false))
mtklein474cef42014-06-24 15:19:25 -0700133//DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true))
mtklein@google.com410e6e82013-09-13 19:52:27 +0000134DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false))