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" |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 9 | #include "SkBlendModePriv.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" |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 14 | |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 15 | // Benchmark that draws non-AA rects or AA text with an SkXfermode::Mode. |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 16 | class XfermodeBench : public Benchmark { |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 17 | public: |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 18 | XfermodeBench(SkBlendMode mode, bool aa) : fBlendMode(mode) { |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 19 | fAA = aa; |
Mike Reed | bfa23d7 | 2017-07-04 09:05:32 -0400 | [diff] [blame] | 20 | fName.printf("blendmode_%s_%s", aa ? "mask" : "rect", SkBlendMode_Name(mode)); |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | const char* onGetName() override { return fName.c_str(); } |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 25 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 26 | void onDraw(int loops, SkCanvas* canvas) override { |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 27 | const char* text = "Hamburgefons"; |
| 28 | size_t len = strlen(text); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 29 | SkISize size = canvas->getBaseLayerSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 30 | SkRandom random; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 31 | for (int i = 0; i < loops; ++i) { |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 32 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 33 | paint.setBlendMode(fBlendMode); |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 34 | paint.setColor(random.nextU()); |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 35 | if (fAA) { |
| 36 | // Draw text to exercise AA code paths. |
| 37 | paint.setAntiAlias(true); |
| 38 | paint.setTextSize(random.nextRangeScalar(12, 96)); |
| 39 | SkScalar x = random.nextRangeScalar(0, (SkScalar)size.fWidth), |
| 40 | y = random.nextRangeScalar(0, (SkScalar)size.fHeight); |
| 41 | for (int j = 0; j < 1000; ++j) { |
| 42 | canvas->drawText(text, len, x, y, paint); |
| 43 | } |
| 44 | } else { |
| 45 | // Draw rects to exercise non-AA code paths. |
| 46 | SkScalar w = random.nextRangeScalar(50, 100); |
| 47 | SkScalar h = random.nextRangeScalar(50, 100); |
| 48 | SkRect rect = SkRect::MakeXYWH( |
| 49 | random.nextUScalar1() * (size.fWidth - w), |
| 50 | random.nextUScalar1() * (size.fHeight - h), |
| 51 | w, |
| 52 | h |
| 53 | ); |
| 54 | for (int j = 0; j < 1000; ++j) { |
| 55 | canvas->drawRect(rect, paint); |
| 56 | } |
reed | f92ace9 | 2015-04-02 12:46:24 -0700 | [diff] [blame] | 57 | } |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | private: |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 62 | SkBlendMode fBlendMode; |
| 63 | SkString fName; |
| 64 | bool fAA; |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 65 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 66 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | ////////////////////////////////////////////////////////////////////////////// |
| 70 | |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 71 | #define BENCH(...) \ |
| 72 | DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); ) \ |
| 73 | DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); ) |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 74 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 75 | BENCH(SkBlendMode::kClear) |
| 76 | BENCH(SkBlendMode::kSrc) |
| 77 | BENCH(SkBlendMode::kDst) |
| 78 | BENCH(SkBlendMode::kSrcOver) |
| 79 | BENCH(SkBlendMode::kDstOver) |
| 80 | BENCH(SkBlendMode::kSrcIn) |
| 81 | BENCH(SkBlendMode::kDstIn) |
| 82 | BENCH(SkBlendMode::kSrcOut) |
| 83 | BENCH(SkBlendMode::kDstOut) |
| 84 | BENCH(SkBlendMode::kSrcATop) |
| 85 | BENCH(SkBlendMode::kDstATop) |
| 86 | BENCH(SkBlendMode::kXor) |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 87 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 88 | BENCH(SkBlendMode::kPlus) |
| 89 | BENCH(SkBlendMode::kModulate) |
| 90 | BENCH(SkBlendMode::kScreen) |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 91 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 92 | BENCH(SkBlendMode::kOverlay) |
| 93 | BENCH(SkBlendMode::kDarken) |
| 94 | BENCH(SkBlendMode::kLighten) |
| 95 | BENCH(SkBlendMode::kColorDodge) |
| 96 | BENCH(SkBlendMode::kColorBurn) |
| 97 | BENCH(SkBlendMode::kHardLight) |
| 98 | BENCH(SkBlendMode::kSoftLight) |
| 99 | BENCH(SkBlendMode::kDifference) |
| 100 | BENCH(SkBlendMode::kExclusion) |
| 101 | BENCH(SkBlendMode::kMultiply) |
commit-bot@chromium.org | eaa7797 | 2013-08-23 13:13:12 +0000 | [diff] [blame] | 102 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 103 | BENCH(SkBlendMode::kHue) |
| 104 | BENCH(SkBlendMode::kSaturation) |
| 105 | BENCH(SkBlendMode::kColor) |
| 106 | BENCH(SkBlendMode::kLuminosity) |