robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 9 | #include "SkBenchmark.h" |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkShader.h" |
| 13 | #include "SkString.h" |
| 14 | |
| 15 | static 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 |
| 31 | class ConstXTileBench : public SkBenchmark { |
| 32 | SkPaint fPaint; |
| 33 | SkString fName; |
| 34 | bool fDoFilter; |
| 35 | bool fDoTrans; |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 36 | bool fDoScale; |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 37 | static const int kWidth = 1; |
| 38 | static const int kHeight = 300; |
| 39 | |
| 40 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 41 | ConstXTileBench(SkShader::TileMode xTile, |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 42 | SkShader::TileMode yTile, |
| 43 | bool doFilter, |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 44 | bool doTrans, |
| 45 | bool doScale) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 46 | : fDoFilter(doFilter) |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 47 | , fDoTrans(doTrans) |
| 48 | , fDoScale(doScale) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 49 | SkBitmap bm; |
| 50 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 51 | bm.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight, 0, |
| 52 | kOpaque_SkAlphaType); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 53 | bm.allocPixels(); |
| 54 | bm.eraseColor(SK_ColorWHITE); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 55 | |
| 56 | create_gradient(&bm); |
| 57 | |
| 58 | SkShader* s = SkShader::CreateBitmapShader(bm, xTile, yTile); |
| 59 | fPaint.setShader(s)->unref(); |
| 60 | |
| 61 | fName.printf("constXTile_"); |
| 62 | |
| 63 | static const char* gTileModeStr[SkShader::kTileModeCount] = { "C", "R", "M" }; |
| 64 | fName.append(gTileModeStr[xTile]); |
| 65 | fName.append(gTileModeStr[yTile]); |
| 66 | |
| 67 | if (doFilter) { |
| 68 | fName.append("_filter"); |
| 69 | } |
| 70 | |
| 71 | if (doTrans) { |
| 72 | fName.append("_trans"); |
| 73 | } |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 74 | |
| 75 | if (doScale) { |
| 76 | fName.append("_scale"); |
| 77 | } |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | protected: |
| 81 | virtual const char* onGetName() { |
| 82 | return fName.c_str(); |
| 83 | } |
| 84 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 85 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 86 | SkPaint paint(fPaint); |
| 87 | this->setupPaint(&paint); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 88 | paint.setFilterLevel(fDoFilter ? SkPaint::kLow_FilterLevel |
| 89 | : SkPaint::kNone_FilterLevel); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 90 | if (fDoTrans) { |
| 91 | paint.setColor(SkColorSetARGBMacro(0x80, 0xFF, 0xFF, 0xFF)); |
| 92 | } |
| 93 | |
| 94 | SkRect r; |
| 95 | |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 96 | if (fDoScale) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 97 | r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480)); |
| 98 | canvas->scale(SK_ScalarHalf, SK_ScalarHalf); |
| 99 | } else { |
| 100 | r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480)); |
| 101 | } |
| 102 | |
| 103 | SkPaint bgPaint; |
| 104 | bgPaint.setColor(SK_ColorWHITE); |
| 105 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 106 | for (int i = 0; i < loops; i++) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 107 | if (fDoTrans) { |
| 108 | canvas->drawRect(r, bgPaint); |
| 109 | } |
| 110 | |
| 111 | canvas->drawRect(r, paint); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | typedef SkBenchmark INHERITED; |
| 117 | }; |
| 118 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 119 | DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, false, true)) |
| 120 | DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false)) |
| 121 | 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] | 122 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 123 | DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false)) |
| 124 | DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true)) |
| 125 | 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] | 126 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 127 | DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true)) |
| 128 | DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false)) |
| 129 | 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] | 130 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 131 | DEF_BENCH(return new ConstXTileBench(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false)) |
| 132 | DEF_BENCH(return new ConstXTileBench(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true)) |
| 133 | DEF_BENCH(return new ConstXTileBench(SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false)) |