robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "SkString.h" |
| 13 | |
| 14 | static 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.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 21 | 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 |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 29 | class ConstXTileBench : public Benchmark { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 30 | SkPaint fPaint; |
| 31 | SkString fName; |
| 32 | bool fDoFilter; |
| 33 | bool fDoTrans; |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 34 | bool fDoScale; |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 35 | static const int kWidth = 1; |
| 36 | static const int kHeight = 300; |
| 37 | |
| 38 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 39 | ConstXTileBench(SkShader::TileMode xTile, |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 40 | SkShader::TileMode yTile, |
| 41 | bool doFilter, |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 42 | bool doTrans, |
| 43 | bool doScale) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 44 | : fDoFilter(doFilter) |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 45 | , fDoTrans(doTrans) |
| 46 | , fDoScale(doScale) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 47 | SkBitmap bm; |
| 48 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 49 | bm.allocN32Pixels(kWidth, kHeight, true); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 50 | bm.eraseColor(SK_ColorWHITE); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 51 | |
| 52 | create_gradient(&bm); |
| 53 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 54 | fPaint.setShader(SkShader::MakeBitmapShader(bm, xTile, yTile)); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 55 | |
| 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.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 69 | |
| 70 | if (doScale) { |
| 71 | fName.append("_scale"); |
| 72 | } |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | protected: |
| 76 | virtual const char* onGetName() { |
| 77 | return fName.c_str(); |
| 78 | } |
| 79 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 80 | virtual void onDraw(int loops, SkCanvas* canvas) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 81 | SkPaint paint(fPaint); |
| 82 | this->setupPaint(&paint); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 83 | paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality |
| 84 | : kNone_SkFilterQuality); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 85 | if (fDoTrans) { |
Cary Clark | 6cdb7d3 | 2018-04-24 14:47:16 -0400 | [diff] [blame] | 86 | paint.setColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF)); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | SkRect r; |
| 90 | |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 91 | if (fDoScale) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 92 | 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.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 101 | for (int i = 0; i < loops; i++) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 102 | if (fDoTrans) { |
| 103 | canvas->drawRect(r, bgPaint); |
| 104 | } |
| 105 | |
| 106 | canvas->drawRect(r, paint); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 111 | typedef Benchmark INHERITED; |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
mtklein | 474cef4 | 2014-06-24 15:19:25 -0700 | [diff] [blame] | 114 | // 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.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 118 | DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false)) |
mtklein | 474cef4 | 2014-06-24 15:19:25 -0700 | [diff] [blame] | 119 | //DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, false, true)) |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 120 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 121 | DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false)) |
mtklein | 474cef4 | 2014-06-24 15:19:25 -0700 | [diff] [blame] | 122 | //DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true)) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 123 | DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, false, false)) |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 124 | |
mtklein | 474cef4 | 2014-06-24 15:19:25 -0700 | [diff] [blame] | 125 | //DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true)) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 126 | DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false)) |
mtklein | 474cef4 | 2014-06-24 15:19:25 -0700 | [diff] [blame] | 127 | //DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, true, true)) |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 128 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 129 | DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false)) |
mtklein | 474cef4 | 2014-06-24 15:19:25 -0700 | [diff] [blame] | 130 | //DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true)) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 131 | DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false)) |