blob: d38807a2084c2cbd99b8096308ef5cf9a91119cd [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
Mike Reedd4706732016-11-15 16:44:34 -05009#include "SkBlendModePriv.h"
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkString.h"
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000014
mtkleind0a10882015-05-13 11:54:00 -070015// Benchmark that draws non-AA rects or AA text with an SkXfermode::Mode.
tfarinaf168b862014-06-19 12:32:29 -070016class XfermodeBench : public Benchmark {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000017public:
reed374772b2016-10-05 17:33:02 -070018 XfermodeBench(SkBlendMode mode, bool aa) : fBlendMode(mode) {
mtkleind0a10882015-05-13 11:54:00 -070019 fAA = aa;
Mike Reedbfa23d72017-07-04 09:05:32 -040020 fName.printf("blendmode_%s_%s", aa ? "mask" : "rect", SkBlendMode_Name(mode));
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000021 }
22
23protected:
mtklein36352bf2015-03-25 18:17:31 -070024 const char* onGetName() override { return fName.c_str(); }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000025
mtkleina1ebeb22015-10-01 09:43:39 -070026 void onDraw(int loops, SkCanvas* canvas) override {
mtkleind0a10882015-05-13 11:54:00 -070027 const char* text = "Hamburgefons";
28 size_t len = strlen(text);
Mike Reed3661bc92017-02-22 13:21:42 -050029 SkISize size = canvas->getBaseLayerSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000030 SkRandom random;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000031 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000032 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070033 paint.setBlendMode(fBlendMode);
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000034 paint.setColor(random.nextU());
mtkleind0a10882015-05-13 11:54:00 -070035 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 }
reedf92ace92015-04-02 12:46:24 -070057 }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000058 }
59 }
60
61private:
reed374772b2016-10-05 17:33:02 -070062 SkBlendMode fBlendMode;
63 SkString fName;
64 bool fAA;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000065
tfarinaf168b862014-06-19 12:32:29 -070066 typedef Benchmark INHERITED;
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000067};
68
69//////////////////////////////////////////////////////////////////////////////
70
mtkleind0a10882015-05-13 11:54:00 -070071#define BENCH(...) \
72 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); ) \
73 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); )
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000074
reed374772b2016-10-05 17:33:02 -070075BENCH(SkBlendMode::kClear)
76BENCH(SkBlendMode::kSrc)
77BENCH(SkBlendMode::kDst)
78BENCH(SkBlendMode::kSrcOver)
79BENCH(SkBlendMode::kDstOver)
80BENCH(SkBlendMode::kSrcIn)
81BENCH(SkBlendMode::kDstIn)
82BENCH(SkBlendMode::kSrcOut)
83BENCH(SkBlendMode::kDstOut)
84BENCH(SkBlendMode::kSrcATop)
85BENCH(SkBlendMode::kDstATop)
86BENCH(SkBlendMode::kXor)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000087
reed374772b2016-10-05 17:33:02 -070088BENCH(SkBlendMode::kPlus)
89BENCH(SkBlendMode::kModulate)
90BENCH(SkBlendMode::kScreen)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000091
reed374772b2016-10-05 17:33:02 -070092BENCH(SkBlendMode::kOverlay)
93BENCH(SkBlendMode::kDarken)
94BENCH(SkBlendMode::kLighten)
95BENCH(SkBlendMode::kColorDodge)
96BENCH(SkBlendMode::kColorBurn)
97BENCH(SkBlendMode::kHardLight)
98BENCH(SkBlendMode::kSoftLight)
99BENCH(SkBlendMode::kDifference)
100BENCH(SkBlendMode::kExclusion)
101BENCH(SkBlendMode::kMultiply)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000102
reed374772b2016-10-05 17:33:02 -0700103BENCH(SkBlendMode::kHue)
104BENCH(SkBlendMode::kSaturation)
105BENCH(SkBlendMode::kColor)
106BENCH(SkBlendMode::kLuminosity)