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