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