blob: c89e7ea82a4fccec7cdbe49e910d73e66be2b61f [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"
Mike Reeda6d1c0a2019-01-03 23:29:38 -050011#include "SkFont.h"
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000012#include "SkPaint.h"
13#include "SkRandom.h"
14#include "SkString.h"
Mike Reeda6d1c0a2019-01-03 23:29:38 -050015#include "SkTextBlob.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;
Mike Reedbfa23d72017-07-04 09:05:32 -040022 fName.printf("blendmode_%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;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000033 for (int i = 0; i < loops; ++i) {
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);
Mike Reeda6d1c0a2019-01-03 23:29:38 -050043 auto blob = SkTextBlob::MakeFromText(text, len, font, kUTF8_SkTextEncoding);
mtkleind0a10882015-05-13 11:54:00 -070044 for (int j = 0; j < 1000; ++j) {
Mike Reeda6d1c0a2019-01-03 23:29:38 -050045 canvas->drawTextBlob(blob, x, y, paint);
mtkleind0a10882015-05-13 11:54:00 -070046 }
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 }
reedf92ace92015-04-02 12:46:24 -070060 }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000061 }
62 }
63
64private:
reed374772b2016-10-05 17:33:02 -070065 SkBlendMode fBlendMode;
66 SkString fName;
67 bool fAA;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000068
tfarinaf168b862014-06-19 12:32:29 -070069 typedef Benchmark INHERITED;
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000070};
71
72//////////////////////////////////////////////////////////////////////////////
73
mtkleind0a10882015-05-13 11:54:00 -070074#define BENCH(...) \
75 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); ) \
76 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); )
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000077
reed374772b2016-10-05 17:33:02 -070078BENCH(SkBlendMode::kClear)
79BENCH(SkBlendMode::kSrc)
80BENCH(SkBlendMode::kDst)
81BENCH(SkBlendMode::kSrcOver)
82BENCH(SkBlendMode::kDstOver)
83BENCH(SkBlendMode::kSrcIn)
84BENCH(SkBlendMode::kDstIn)
85BENCH(SkBlendMode::kSrcOut)
86BENCH(SkBlendMode::kDstOut)
87BENCH(SkBlendMode::kSrcATop)
88BENCH(SkBlendMode::kDstATop)
89BENCH(SkBlendMode::kXor)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000090
reed374772b2016-10-05 17:33:02 -070091BENCH(SkBlendMode::kPlus)
92BENCH(SkBlendMode::kModulate)
93BENCH(SkBlendMode::kScreen)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000094
reed374772b2016-10-05 17:33:02 -070095BENCH(SkBlendMode::kOverlay)
96BENCH(SkBlendMode::kDarken)
97BENCH(SkBlendMode::kLighten)
98BENCH(SkBlendMode::kColorDodge)
99BENCH(SkBlendMode::kColorBurn)
100BENCH(SkBlendMode::kHardLight)
101BENCH(SkBlendMode::kSoftLight)
102BENCH(SkBlendMode::kDifference)
103BENCH(SkBlendMode::kExclusion)
104BENCH(SkBlendMode::kMultiply)
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000105
reed374772b2016-10-05 17:33:02 -0700106BENCH(SkBlendMode::kHue)
107BENCH(SkBlendMode::kSaturation)
108BENCH(SkBlendMode::kColor)
109BENCH(SkBlendMode::kLuminosity)