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" |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 15 | #include "SkLumaXfermode.h" |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 16 | |
| 17 | // Benchmark that draws non-AA rects with an SkXfermode::Mode |
| 18 | class XfermodeBench : public SkBenchmark { |
| 19 | public: |
| 20 | XfermodeBench(void* param, SkXfermode::Mode mode) : SkBenchmark(param) { |
| 21 | fXfermode.reset(SkXfermode::Create(mode)); |
| 22 | SkASSERT(NULL != fXfermode.get() || SkXfermode::kSrcOver_Mode == mode); |
| 23 | fName.printf("Xfermode_%s", SkXfermode::ModeName(mode)); |
| 24 | } |
| 25 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 26 | XfermodeBench(void* param, SkXfermode* xferMode, const char* name) |
| 27 | : SkBenchmark(param) { |
| 28 | SkASSERT(NULL != xferMode); |
| 29 | fXfermode.reset(xferMode); |
| 30 | fName.printf("Xfermode_%s", name); |
| 31 | } |
| 32 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 33 | protected: |
| 34 | virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); } |
| 35 | |
| 36 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 37 | SkISize size = canvas->getDeviceSize(); |
| 38 | SkMWCRandom random; |
| 39 | for (int i = 0; i < kNumRects; ++i) { |
| 40 | SkPaint paint; |
| 41 | paint.setXfermode(fXfermode.get()); |
| 42 | paint.setColor(random.nextU()); |
| 43 | SkScalar w = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize)); |
| 44 | SkScalar h = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize)); |
| 45 | SkRect rect = SkRect::MakeXYWH( |
| 46 | random.nextUScalar1() * (size.fWidth - w), |
| 47 | random.nextUScalar1() * (size.fHeight - h), |
| 48 | w, |
| 49 | h |
| 50 | ); |
| 51 | canvas->drawRect(rect, paint); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | enum { |
bsalomon@google.com | 292ce73 | 2013-05-03 16:28:33 +0000 | [diff] [blame] | 57 | kNumRects = SkBENCHLOOP(75), |
| 58 | kMinSize = 50, |
| 59 | kMaxSize = 100, |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 60 | }; |
| 61 | SkAutoTUnref<SkXfermode> fXfermode; |
| 62 | SkString fName; |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 63 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 64 | typedef SkBenchmark INHERITED; |
| 65 | }; |
| 66 | |
| 67 | ////////////////////////////////////////////////////////////////////////////// |
| 68 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 69 | #define CONCAT_I(x, y) x ## y |
| 70 | #define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion |
| 71 | #define BENCH(...) \ |
| 72 | static SkBenchmark* CONCAT(Fact, __LINE__)(void *p) { return new XfermodeBench(p, __VA_ARGS__); };\ |
| 73 | static BenchRegistry CONCAT(gReg, __LINE__)(CONCAT(Fact, __LINE__)); |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 74 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 75 | BENCH(SkXfermode::kClear_Mode) |
| 76 | BENCH(SkXfermode::kSrc_Mode) |
| 77 | BENCH(SkXfermode::kDst_Mode) |
| 78 | BENCH(SkXfermode::kSrcOver_Mode) |
| 79 | BENCH(SkXfermode::kDstOver_Mode) |
| 80 | BENCH(SkXfermode::kSrcIn_Mode) |
| 81 | BENCH(SkXfermode::kDstIn_Mode) |
| 82 | BENCH(SkXfermode::kSrcOut_Mode) |
| 83 | BENCH(SkXfermode::kDstOut_Mode) |
| 84 | BENCH(SkXfermode::kSrcATop_Mode) |
| 85 | BENCH(SkXfermode::kDstATop_Mode) |
| 86 | BENCH(SkXfermode::kXor_Mode) |
| 87 | |
| 88 | BENCH(SkXfermode::kPlus_Mode) |
| 89 | BENCH(SkXfermode::kModulate_Mode) |
| 90 | BENCH(SkXfermode::kScreen_Mode) |
| 91 | |
| 92 | BENCH(SkXfermode::kOverlay_Mode) |
| 93 | BENCH(SkXfermode::kDarken_Mode) |
| 94 | BENCH(SkXfermode::kLighten_Mode) |
| 95 | BENCH(SkXfermode::kColorDodge_Mode) |
| 96 | BENCH(SkXfermode::kColorBurn_Mode) |
| 97 | BENCH(SkXfermode::kHardLight_Mode) |
| 98 | BENCH(SkXfermode::kSoftLight_Mode) |
| 99 | BENCH(SkXfermode::kDifference_Mode) |
| 100 | BENCH(SkXfermode::kExclusion_Mode) |
| 101 | BENCH(SkXfermode::kMultiply_Mode) |
| 102 | |
| 103 | BENCH(SkXfermode::kHue_Mode) |
| 104 | BENCH(SkXfermode::kSaturation_Mode) |
| 105 | BENCH(SkXfermode::kColor_Mode) |
| 106 | BENCH(SkXfermode::kLuminosity_Mode) |
| 107 | |
| 108 | BENCH(SkLumaMaskXfermode::Create(SkXfermode::kSrcIn_Mode), "SrcInLuma") |
| 109 | BENCH(SkLumaMaskXfermode::Create(SkXfermode::kDstIn_Mode), "DstInLuma") |