commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 9 | #include "Benchmark.h" |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkRandom.h" |
| 13 | #include "SkString.h" |
| 14 | #include "SkXfermode.h" |
| 15 | |
| 16 | // Benchmark that draws non-AA rects with an SkXfermode::Mode |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 17 | class XfermodeBench : public Benchmark { |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 18 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 19 | XfermodeBench(SkXfermode::Mode mode) { |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 20 | fXfermode.reset(SkXfermode::Create(mode)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 21 | SkASSERT(fXfermode.get() || SkXfermode::kSrcOver_Mode == mode); |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 22 | fName.printf("Xfermode_%s", SkXfermode::ModeName(mode)); |
| 23 | } |
| 24 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 25 | XfermodeBench(SkXfermode* xferMode, const char* name) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 26 | SkASSERT(xferMode); |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 27 | fXfermode.reset(xferMode); |
| 28 | fName.printf("Xfermode_%s", name); |
| 29 | } |
| 30 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 31 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | const char* onGetName() override { return fName.c_str(); } |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void onDraw(const int loops, SkCanvas* canvas) override { |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 35 | SkISize size = canvas->getDeviceSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 36 | SkRandom random; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 37 | for (int i = 0; i < loops; ++i) { |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 38 | SkPaint paint; |
| 39 | paint.setXfermode(fXfermode.get()); |
| 40 | paint.setColor(random.nextU()); |
| 41 | SkScalar w = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize)); |
| 42 | SkScalar h = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize)); |
| 43 | SkRect rect = SkRect::MakeXYWH( |
| 44 | random.nextUScalar1() * (size.fWidth - w), |
| 45 | random.nextUScalar1() * (size.fHeight - h), |
| 46 | w, |
| 47 | h |
| 48 | ); |
reed | f92ace9 | 2015-04-02 12:46:24 -0700 | [diff] [blame] | 49 | for (int j = 0; j < 1000; ++j) { |
| 50 | canvas->drawRect(rect, paint); |
| 51 | } |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | enum { |
bsalomon@google.com | 292ce73 | 2013-05-03 16:28:33 +0000 | [diff] [blame] | 57 | kMinSize = 50, |
| 58 | kMaxSize = 100, |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 59 | }; |
| 60 | SkAutoTUnref<SkXfermode> fXfermode; |
| 61 | SkString fName; |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 62 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 63 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 66 | class XferCreateBench : public Benchmark { |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 67 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 69 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 70 | } |
skia.committer@gmail.com | d34f058 | 2013-10-05 07:01:34 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 72 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | const char* onGetName() override { return "xfermode_create"; } |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | void onDraw(const int loops, SkCanvas* canvas) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 76 | for (int outer = 0; outer < loops * 10; ++outer) { |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 77 | for (int i = 0; i <= SkXfermode::kLastMode; ++i) { |
| 78 | SkXfermode* xfer = SkXfermode::Create(SkXfermode::Mode(i)); |
| 79 | SkSafeUnref(xfer); |
| 80 | } |
| 81 | } |
| 82 | } |
skia.committer@gmail.com | d34f058 | 2013-10-05 07:01:34 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 84 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 85 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 90 | #define CONCAT_I(x, y) x ## y |
| 91 | #define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion |
| 92 | #define BENCH(...) \ |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 93 | DEF_BENCH( return new XfermodeBench(__VA_ARGS__); );\ |
| 94 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 95 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 96 | BENCH(SkXfermode::kClear_Mode) |
| 97 | BENCH(SkXfermode::kSrc_Mode) |
| 98 | BENCH(SkXfermode::kDst_Mode) |
| 99 | BENCH(SkXfermode::kSrcOver_Mode) |
| 100 | BENCH(SkXfermode::kDstOver_Mode) |
| 101 | BENCH(SkXfermode::kSrcIn_Mode) |
| 102 | BENCH(SkXfermode::kDstIn_Mode) |
| 103 | BENCH(SkXfermode::kSrcOut_Mode) |
| 104 | BENCH(SkXfermode::kDstOut_Mode) |
| 105 | BENCH(SkXfermode::kSrcATop_Mode) |
| 106 | BENCH(SkXfermode::kDstATop_Mode) |
| 107 | BENCH(SkXfermode::kXor_Mode) |
| 108 | |
| 109 | BENCH(SkXfermode::kPlus_Mode) |
| 110 | BENCH(SkXfermode::kModulate_Mode) |
| 111 | BENCH(SkXfermode::kScreen_Mode) |
| 112 | |
| 113 | BENCH(SkXfermode::kOverlay_Mode) |
| 114 | BENCH(SkXfermode::kDarken_Mode) |
| 115 | BENCH(SkXfermode::kLighten_Mode) |
| 116 | BENCH(SkXfermode::kColorDodge_Mode) |
| 117 | BENCH(SkXfermode::kColorBurn_Mode) |
| 118 | BENCH(SkXfermode::kHardLight_Mode) |
| 119 | BENCH(SkXfermode::kSoftLight_Mode) |
| 120 | BENCH(SkXfermode::kDifference_Mode) |
| 121 | BENCH(SkXfermode::kExclusion_Mode) |
| 122 | BENCH(SkXfermode::kMultiply_Mode) |
| 123 | |
| 124 | BENCH(SkXfermode::kHue_Mode) |
| 125 | BENCH(SkXfermode::kSaturation_Mode) |
| 126 | BENCH(SkXfermode::kColor_Mode) |
| 127 | BENCH(SkXfermode::kLuminosity_Mode) |
| 128 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 129 | DEF_BENCH(return new XferCreateBench;) |