commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
senorblanco | 7adf9c1 | 2016-04-08 14:29:47 -0700 | [diff] [blame] | 9 | #include "SkArithmeticMode.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 | |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 16 | // Benchmark that draws non-AA rects or AA text 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 | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 19 | XfermodeBench(SkXfermode::Mode mode, bool aa) { |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 20 | fXfermode = SkXfermode::Make(mode); |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 21 | fAA = aa; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 22 | SkASSERT(fXfermode.get() || SkXfermode::kSrcOver_Mode == mode); |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 23 | fName.printf("Xfermode_%s%s", SkXfermode::ModeName(mode), aa ? "_aa" : ""); |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 24 | } |
| 25 | |
senorblanco | 7adf9c1 | 2016-04-08 14:29:47 -0700 | [diff] [blame] | 26 | XfermodeBench(sk_sp<SkXfermode> xferMode, const char* name, bool aa) { |
| 27 | fXfermode = xferMode; |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 28 | fAA = aa; |
| 29 | fName.printf("Xfermode_%s%s", name, aa ? "_aa" : ""); |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 30 | } |
| 31 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 32 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | const char* onGetName() override { return fName.c_str(); } |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 34 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 35 | void onDraw(int loops, SkCanvas* canvas) override { |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 36 | const char* text = "Hamburgefons"; |
| 37 | size_t len = strlen(text); |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 38 | SkISize size = canvas->getDeviceSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 39 | SkRandom random; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 40 | for (int i = 0; i < loops; ++i) { |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 41 | SkPaint paint; |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 42 | paint.setXfermode(fXfermode); |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 43 | paint.setColor(random.nextU()); |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 44 | if (fAA) { |
| 45 | // Draw text to exercise AA code paths. |
| 46 | paint.setAntiAlias(true); |
| 47 | paint.setTextSize(random.nextRangeScalar(12, 96)); |
| 48 | SkScalar x = random.nextRangeScalar(0, (SkScalar)size.fWidth), |
| 49 | y = random.nextRangeScalar(0, (SkScalar)size.fHeight); |
| 50 | for (int j = 0; j < 1000; ++j) { |
| 51 | canvas->drawText(text, len, x, y, paint); |
| 52 | } |
| 53 | } else { |
| 54 | // Draw rects to exercise non-AA code paths. |
| 55 | SkScalar w = random.nextRangeScalar(50, 100); |
| 56 | SkScalar h = random.nextRangeScalar(50, 100); |
| 57 | SkRect rect = SkRect::MakeXYWH( |
| 58 | random.nextUScalar1() * (size.fWidth - w), |
| 59 | random.nextUScalar1() * (size.fHeight - h), |
| 60 | w, |
| 61 | h |
| 62 | ); |
| 63 | for (int j = 0; j < 1000; ++j) { |
| 64 | canvas->drawRect(rect, paint); |
| 65 | } |
reed | f92ace9 | 2015-04-02 12:46:24 -0700 | [diff] [blame] | 66 | } |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | private: |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 71 | sk_sp<SkXfermode> fXfermode; |
| 72 | SkString fName; |
| 73 | bool fAA; |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 74 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 75 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 78 | class XferCreateBench : public Benchmark { |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 79 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 81 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 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 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | const char* onGetName() override { return "xfermode_create"; } |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 86 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 87 | void onDraw(int loops, SkCanvas* canvas) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 88 | for (int outer = 0; outer < loops * 10; ++outer) { |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 89 | for (int i = 0; i <= SkXfermode::kLastMode; ++i) { |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 90 | (void)SkXfermode::Make(SkXfermode::Mode(i)); |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
skia.committer@gmail.com | d34f058 | 2013-10-05 07:01:34 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 95 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 96 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 99 | ////////////////////////////////////////////////////////////////////////////// |
| 100 | |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 101 | #define BENCH(...) \ |
| 102 | DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); ) \ |
| 103 | DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); ) |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 105 | BENCH(SkXfermode::kClear_Mode) |
| 106 | BENCH(SkXfermode::kSrc_Mode) |
| 107 | BENCH(SkXfermode::kDst_Mode) |
| 108 | BENCH(SkXfermode::kSrcOver_Mode) |
| 109 | BENCH(SkXfermode::kDstOver_Mode) |
| 110 | BENCH(SkXfermode::kSrcIn_Mode) |
| 111 | BENCH(SkXfermode::kDstIn_Mode) |
| 112 | BENCH(SkXfermode::kSrcOut_Mode) |
| 113 | BENCH(SkXfermode::kDstOut_Mode) |
| 114 | BENCH(SkXfermode::kSrcATop_Mode) |
| 115 | BENCH(SkXfermode::kDstATop_Mode) |
| 116 | BENCH(SkXfermode::kXor_Mode) |
| 117 | |
| 118 | BENCH(SkXfermode::kPlus_Mode) |
| 119 | BENCH(SkXfermode::kModulate_Mode) |
| 120 | BENCH(SkXfermode::kScreen_Mode) |
| 121 | |
| 122 | BENCH(SkXfermode::kOverlay_Mode) |
| 123 | BENCH(SkXfermode::kDarken_Mode) |
| 124 | BENCH(SkXfermode::kLighten_Mode) |
| 125 | BENCH(SkXfermode::kColorDodge_Mode) |
| 126 | BENCH(SkXfermode::kColorBurn_Mode) |
| 127 | BENCH(SkXfermode::kHardLight_Mode) |
| 128 | BENCH(SkXfermode::kSoftLight_Mode) |
| 129 | BENCH(SkXfermode::kDifference_Mode) |
| 130 | BENCH(SkXfermode::kExclusion_Mode) |
| 131 | BENCH(SkXfermode::kMultiply_Mode) |
| 132 | |
| 133 | BENCH(SkXfermode::kHue_Mode) |
| 134 | BENCH(SkXfermode::kSaturation_Mode) |
| 135 | BENCH(SkXfermode::kColor_Mode) |
| 136 | BENCH(SkXfermode::kLuminosity_Mode) |
| 137 | |
senorblanco | 7adf9c1 | 2016-04-08 14:29:47 -0700 | [diff] [blame] | 138 | DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, false), \ |
| 139 | "arithmetic", false); ) |
| 140 | DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, true), \ |
| 141 | "arithmetic_enforce_pm", false); ) |
| 142 | DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, false), \ |
| 143 | "arithmetic", true); ) |
| 144 | DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, true), \ |
| 145 | "arithmetic_enforce_pm", true); ) |
| 146 | |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 147 | DEF_BENCH(return new XferCreateBench;) |