blob: 8eabefd53cc0c9a464cd27aba8052e675d60c332 [file] [log] [blame]
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkFont.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkString.h"
13#include "include/core/SkTextBlob.h"
14#include "include/utils/SkRandom.h"
15#include "src/core/SkBlendModePriv.h"
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000016
mtkleind0a10882015-05-13 11:54:00 -070017// Benchmark that draws non-AA rects or AA text with an SkXfermode::Mode.
tfarinaf168b862014-06-19 12:32:29 -070018class XfermodeBench : public Benchmark {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000019public:
reed374772b2016-10-05 17:33:02 -070020 XfermodeBench(SkBlendMode mode, bool aa) : fBlendMode(mode) {
mtkleind0a10882015-05-13 11:54:00 -070021 fAA = aa;
John Stiles06b6f472021-03-24 10:06:41 -040022 fName.printf("blendmicro_%s_%s", aa ? "mask" : "rect", SkBlendMode_Name(mode));
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000023 }
24
25protected:
mtklein36352bf2015-03-25 18:17:31 -070026 const char* onGetName() override { return fName.c_str(); }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000027
mtkleina1ebeb22015-10-01 09:43:39 -070028 void onDraw(int loops, SkCanvas* canvas) override {
mtkleind0a10882015-05-13 11:54:00 -070029 const char* text = "Hamburgefons";
30 size_t len = strlen(text);
Mike Reed3661bc92017-02-22 13:21:42 -050031 SkISize size = canvas->getBaseLayerSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000032 SkRandom random;
John Stiles06b6f472021-03-24 10:06:41 -040033 while (loops > 0) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000034 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070035 paint.setBlendMode(fBlendMode);
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000036 paint.setColor(random.nextU());
mtkleind0a10882015-05-13 11:54:00 -070037 if (fAA) {
38 // Draw text to exercise AA code paths.
Mike Reeda6d1c0a2019-01-03 23:29:38 -050039 SkFont font;
40 font.setSize(random.nextRangeScalar(12, 96));
mtkleind0a10882015-05-13 11:54:00 -070041 SkScalar x = random.nextRangeScalar(0, (SkScalar)size.fWidth),
42 y = random.nextRangeScalar(0, (SkScalar)size.fHeight);
Ben Wagner51e15a62019-05-07 15:38:46 -040043 auto blob = SkTextBlob::MakeFromText(text, len, font, SkTextEncoding::kUTF8);
John Stiles06b6f472021-03-24 10:06:41 -040044 int iterations = std::min(1000, loops);
45 for (int j = 0; j < iterations; ++j) {
Mike Reeda6d1c0a2019-01-03 23:29:38 -050046 canvas->drawTextBlob(blob, x, y, paint);
mtkleind0a10882015-05-13 11:54:00 -070047 }
John Stiles06b6f472021-03-24 10:06:41 -040048 loops -= iterations;
mtkleind0a10882015-05-13 11:54:00 -070049 } else {
50 // Draw rects to exercise non-AA code paths.
51 SkScalar w = random.nextRangeScalar(50, 100);
52 SkScalar h = random.nextRangeScalar(50, 100);
53 SkRect rect = SkRect::MakeXYWH(
54 random.nextUScalar1() * (size.fWidth - w),
55 random.nextUScalar1() * (size.fHeight - h),
56 w,
57 h
58 );
John Stiles06b6f472021-03-24 10:06:41 -040059 int iterations = std::min(1000, loops);
60 for (int j = 0; j < iterations; ++j) {
mtkleind0a10882015-05-13 11:54:00 -070061 canvas->drawRect(rect, paint);
62 }
John Stiles06b6f472021-03-24 10:06:41 -040063 loops -= iterations;
reedf92ace92015-04-02 12:46:24 -070064 }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000065 }
66 }
67
68private:
reed374772b2016-10-05 17:33:02 -070069 SkBlendMode fBlendMode;
70 SkString fName;
71 bool fAA;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000072
John Stiles7571f9e2020-09-02 22:42:33 -040073 using INHERITED = Benchmark;
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000074};
75
76//////////////////////////////////////////////////////////////////////////////
77
mtkleind0a10882015-05-13 11:54:00 -070078#define BENCH(...) \
79 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); ) \
80 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); )
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000081
reed374772b2016-10-05 17:33:02 -070082BENCH(SkBlendMode::kClear)
83BENCH(SkBlendMode::kSrc)
84BENCH(SkBlendMode::kDst)
85BENCH(SkBlendMode::kSrcOver)
86BENCH(SkBlendMode::kDstOver)
87BENCH(SkBlendMode::kSrcIn)
88BENCH(SkBlendMode::kDstIn)
89BENCH(SkBlendMode::kSrcOut)
90BENCH(SkBlendMode::kDstOut)
91BENCH(SkBlendMode::kSrcATop)
92BENCH(SkBlendMode::kDstATop)
93BENCH(SkBlendMode::kXor)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000094
reed374772b2016-10-05 17:33:02 -070095BENCH(SkBlendMode::kPlus)
96BENCH(SkBlendMode::kModulate)
97BENCH(SkBlendMode::kScreen)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000098
reed374772b2016-10-05 17:33:02 -070099BENCH(SkBlendMode::kOverlay)
100BENCH(SkBlendMode::kDarken)
101BENCH(SkBlendMode::kLighten)
102BENCH(SkBlendMode::kColorDodge)
103BENCH(SkBlendMode::kColorBurn)
104BENCH(SkBlendMode::kHardLight)
105BENCH(SkBlendMode::kSoftLight)
106BENCH(SkBlendMode::kDifference)
107BENCH(SkBlendMode::kExclusion)
108BENCH(SkBlendMode::kMultiply)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000109
reed374772b2016-10-05 17:33:02 -0700110BENCH(SkBlendMode::kHue)
111BENCH(SkBlendMode::kSaturation)
112BENCH(SkBlendMode::kColor)
113BENCH(SkBlendMode::kLuminosity)