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