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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkShader.h" |
| 12 | #include "include/core/SkString.h" |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 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; |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 32 | bool fDoTrans; |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 33 | bool fDoScale; |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 34 | static const int kWidth = 1; |
| 35 | static const int kHeight = 300; |
| 36 | |
| 37 | public: |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 38 | ConstXTileBench(SkTileMode xTile, |
| 39 | SkTileMode yTile, |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 40 | SkFilterMode fm, |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 41 | bool doTrans, |
| 42 | bool doScale) |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 43 | : fDoTrans(doTrans) |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 44 | , fDoScale(doScale) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 45 | SkBitmap bm; |
| 46 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 47 | bm.allocN32Pixels(kWidth, kHeight, true); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 48 | bm.eraseColor(SK_ColorWHITE); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 49 | |
| 50 | create_gradient(&bm); |
| 51 | |
Mike Reed | 5ec2238 | 2021-01-14 21:59:01 -0500 | [diff] [blame^] | 52 | fPaint.setShader(bm.makeShader(xTile, yTile, SkSamplingOptions(fm))); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 53 | |
| 54 | fName.printf("constXTile_"); |
| 55 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 56 | static const char* gTileModeStr[kSkTileModeCount] = { "C", "R", "M", "D" }; |
| 57 | fName.append(gTileModeStr[(unsigned)xTile]); |
| 58 | fName.append(gTileModeStr[(unsigned)yTile]); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 59 | |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 60 | if (fm != SkFilterMode::kNearest) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 61 | fName.append("_filter"); |
| 62 | } |
| 63 | |
| 64 | if (doTrans) { |
| 65 | fName.append("_trans"); |
| 66 | } |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 67 | |
| 68 | if (doScale) { |
| 69 | fName.append("_scale"); |
| 70 | } |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | protected: |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 74 | const char* onGetName() override { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 75 | return fName.c_str(); |
| 76 | } |
| 77 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 78 | void onDraw(int loops, SkCanvas* canvas) override { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 79 | SkPaint paint(fPaint); |
| 80 | this->setupPaint(&paint); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 81 | if (fDoTrans) { |
Cary Clark | 6cdb7d3 | 2018-04-24 14:47:16 -0400 | [diff] [blame] | 82 | paint.setColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF)); |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | SkRect r; |
| 86 | |
robertphillips@google.com | 1e30523 | 2013-01-22 20:29:16 +0000 | [diff] [blame] | 87 | if (fDoScale) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 88 | r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480)); |
| 89 | canvas->scale(SK_ScalarHalf, SK_ScalarHalf); |
| 90 | } else { |
| 91 | r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480)); |
| 92 | } |
| 93 | |
| 94 | SkPaint bgPaint; |
| 95 | bgPaint.setColor(SK_ColorWHITE); |
| 96 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 97 | for (int i = 0; i < loops; i++) { |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 98 | if (fDoTrans) { |
| 99 | canvas->drawRect(r, bgPaint); |
| 100 | } |
| 101 | |
| 102 | canvas->drawRect(r, paint); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 107 | using INHERITED = Benchmark; |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
mtklein | 474cef4 | 2014-06-24 15:19:25 -0700 | [diff] [blame] | 110 | // Scaled benches are trending towards free. Seems like caching. |
| 111 | // TODO(mtklein, reed): fix and reenable |
| 112 | |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 113 | constexpr SkFilterMode gNN = SkFilterMode::kNearest; |
| 114 | constexpr SkFilterMode gLI = SkFilterMode::kLinear; |
| 115 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 116 | //DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, false, false, true)) |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 117 | DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, gNN, false, false)) |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 118 | //DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, false, false, true)) |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 119 | |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 120 | DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, gLI, false, false)) |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 121 | //DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, true, false, true)) |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 122 | DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, gLI, false, false)) |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 123 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 124 | //DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, false, true, true)) |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 125 | DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, gNN, true, false)) |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 126 | //DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, false, true, true)) |
robertphillips@google.com | 8e679fe | 2013-01-17 19:51:41 +0000 | [diff] [blame] | 127 | |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 128 | DEF_BENCH(return new ConstXTileBench(SkTileMode::kRepeat, SkTileMode::kRepeat, gLI, true, false)) |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 129 | //DEF_BENCH(return new ConstXTileBench(SkTileMode::kClamp, SkTileMode::kClamp, true, true, true)) |
Mike Reed | 057fcbe | 2020-12-12 14:31:25 -0500 | [diff] [blame] | 130 | DEF_BENCH(return new ConstXTileBench(SkTileMode::kMirror, SkTileMode::kMirror, gLI, true, false)) |