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 | |
| 9 | #include "SkBenchmark.h" |
| 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 |
| 17 | class XfermodeBench : public SkBenchmark { |
| 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)); |
| 21 | SkASSERT(NULL != fXfermode.get() || SkXfermode::kSrcOver_Mode == mode); |
| 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) { |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 26 | SkASSERT(NULL != xferMode); |
| 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: |
| 32 | virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); } |
| 33 | |
| 34 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 35 | SkISize size = canvas->getDeviceSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 36 | SkRandom random; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 37 | for (int i = 0; i < this->getLoops(); ++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 | ); |
| 49 | canvas->drawRect(rect, paint); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | enum { |
bsalomon@google.com | 292ce73 | 2013-05-03 16:28:33 +0000 | [diff] [blame] | 55 | kMinSize = 50, |
| 56 | kMaxSize = 100, |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 57 | }; |
| 58 | SkAutoTUnref<SkXfermode> fXfermode; |
| 59 | SkString fName; |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 60 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 61 | typedef SkBenchmark INHERITED; |
| 62 | }; |
| 63 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 64 | class XferCreateBench : public SkBenchmark { |
| 65 | public: |
| 66 | XferCreateBench() { |
| 67 | fIsRendering = false; |
| 68 | } |
skia.committer@gmail.com | d34f058 | 2013-10-05 07:01:34 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 70 | protected: |
| 71 | virtual const char* onGetName() SK_OVERRIDE { return "xfermode_create"; } |
| 72 | |
| 73 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 74 | for (int outer = 0; outer < this->getLoops() * 10; ++outer) { |
| 75 | for (int i = 0; i <= SkXfermode::kLastMode; ++i) { |
| 76 | SkXfermode* xfer = SkXfermode::Create(SkXfermode::Mode(i)); |
| 77 | SkSafeUnref(xfer); |
| 78 | } |
| 79 | } |
| 80 | } |
skia.committer@gmail.com | d34f058 | 2013-10-05 07:01:34 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 82 | private: |
| 83 | typedef SkBenchmark INHERITED; |
| 84 | }; |
| 85 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 86 | ////////////////////////////////////////////////////////////////////////////// |
| 87 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 88 | #define CONCAT_I(x, y) x ## y |
| 89 | #define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion |
| 90 | #define BENCH(...) \ |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 91 | DEF_BENCH( return new XfermodeBench(__VA_ARGS__); );\ |
| 92 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 93 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 94 | BENCH(SkXfermode::kClear_Mode) |
| 95 | BENCH(SkXfermode::kSrc_Mode) |
| 96 | BENCH(SkXfermode::kDst_Mode) |
| 97 | BENCH(SkXfermode::kSrcOver_Mode) |
| 98 | BENCH(SkXfermode::kDstOver_Mode) |
| 99 | BENCH(SkXfermode::kSrcIn_Mode) |
| 100 | BENCH(SkXfermode::kDstIn_Mode) |
| 101 | BENCH(SkXfermode::kSrcOut_Mode) |
| 102 | BENCH(SkXfermode::kDstOut_Mode) |
| 103 | BENCH(SkXfermode::kSrcATop_Mode) |
| 104 | BENCH(SkXfermode::kDstATop_Mode) |
| 105 | BENCH(SkXfermode::kXor_Mode) |
| 106 | |
| 107 | BENCH(SkXfermode::kPlus_Mode) |
| 108 | BENCH(SkXfermode::kModulate_Mode) |
| 109 | BENCH(SkXfermode::kScreen_Mode) |
| 110 | |
| 111 | BENCH(SkXfermode::kOverlay_Mode) |
| 112 | BENCH(SkXfermode::kDarken_Mode) |
| 113 | BENCH(SkXfermode::kLighten_Mode) |
| 114 | BENCH(SkXfermode::kColorDodge_Mode) |
| 115 | BENCH(SkXfermode::kColorBurn_Mode) |
| 116 | BENCH(SkXfermode::kHardLight_Mode) |
| 117 | BENCH(SkXfermode::kSoftLight_Mode) |
| 118 | BENCH(SkXfermode::kDifference_Mode) |
| 119 | BENCH(SkXfermode::kExclusion_Mode) |
| 120 | BENCH(SkXfermode::kMultiply_Mode) |
| 121 | |
| 122 | BENCH(SkXfermode::kHue_Mode) |
| 123 | BENCH(SkXfermode::kSaturation_Mode) |
| 124 | BENCH(SkXfermode::kColor_Mode) |
| 125 | BENCH(SkXfermode::kLuminosity_Mode) |
| 126 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 127 | DEF_BENCH(return new XferCreateBench;) |