blob: 7d00f411bd05ce904fbf97a2229a7b62f5b2d869 [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"
senorblanco7adf9c12016-04-08 14:29:47 -07009#include "SkArithmeticMode.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"
14#include "SkXfermode.h"
15
mtkleind0a10882015-05-13 11:54:00 -070016// Benchmark that draws non-AA rects or AA text with an SkXfermode::Mode.
tfarinaf168b862014-06-19 12:32:29 -070017class XfermodeBench : public Benchmark {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000018public:
mtkleind0a10882015-05-13 11:54:00 -070019 XfermodeBench(SkXfermode::Mode mode, bool aa) {
reedcfb6bdf2016-03-29 11:32:50 -070020 fXfermode = SkXfermode::Make(mode);
mtkleind0a10882015-05-13 11:54:00 -070021 fAA = aa;
bsalomon49f085d2014-09-05 13:34:00 -070022 SkASSERT(fXfermode.get() || SkXfermode::kSrcOver_Mode == mode);
mtkleind0a10882015-05-13 11:54:00 -070023 fName.printf("Xfermode_%s%s", SkXfermode::ModeName(mode), aa ? "_aa" : "");
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000024 }
25
senorblanco7adf9c12016-04-08 14:29:47 -070026 XfermodeBench(sk_sp<SkXfermode> xferMode, const char* name, bool aa) {
27 fXfermode = xferMode;
mtkleind0a10882015-05-13 11:54:00 -070028 fAA = aa;
29 fName.printf("Xfermode_%s%s", name, aa ? "_aa" : "");
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000030 }
31
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000032protected:
mtklein36352bf2015-03-25 18:17:31 -070033 const char* onGetName() override { return fName.c_str(); }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000034
mtkleina1ebeb22015-10-01 09:43:39 -070035 void onDraw(int loops, SkCanvas* canvas) override {
mtkleind0a10882015-05-13 11:54:00 -070036 const char* text = "Hamburgefons";
37 size_t len = strlen(text);
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000038 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000039 SkRandom random;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000040 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000041 SkPaint paint;
reedcfb6bdf2016-03-29 11:32:50 -070042 paint.setXfermode(fXfermode);
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000043 paint.setColor(random.nextU());
mtkleind0a10882015-05-13 11:54:00 -070044 if (fAA) {
45 // Draw text to exercise AA code paths.
46 paint.setAntiAlias(true);
47 paint.setTextSize(random.nextRangeScalar(12, 96));
48 SkScalar x = random.nextRangeScalar(0, (SkScalar)size.fWidth),
49 y = random.nextRangeScalar(0, (SkScalar)size.fHeight);
50 for (int j = 0; j < 1000; ++j) {
51 canvas->drawText(text, len, x, y, paint);
52 }
53 } else {
54 // Draw rects to exercise non-AA code paths.
55 SkScalar w = random.nextRangeScalar(50, 100);
56 SkScalar h = random.nextRangeScalar(50, 100);
57 SkRect rect = SkRect::MakeXYWH(
58 random.nextUScalar1() * (size.fWidth - w),
59 random.nextUScalar1() * (size.fHeight - h),
60 w,
61 h
62 );
63 for (int j = 0; j < 1000; ++j) {
64 canvas->drawRect(rect, paint);
65 }
reedf92ace92015-04-02 12:46:24 -070066 }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000067 }
68 }
69
70private:
reedcfb6bdf2016-03-29 11:32:50 -070071 sk_sp<SkXfermode> fXfermode;
72 SkString fName;
73 bool fAA;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000074
tfarinaf168b862014-06-19 12:32:29 -070075 typedef Benchmark INHERITED;
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000076};
77
tfarinaf168b862014-06-19 12:32:29 -070078class XferCreateBench : public Benchmark {
commit-bot@chromium.org86490572013-10-04 16:52:55 +000079public:
mtklein36352bf2015-03-25 18:17:31 -070080 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000081 return backend == kNonRendering_Backend;
commit-bot@chromium.org86490572013-10-04 16:52:55 +000082 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000083
commit-bot@chromium.org86490572013-10-04 16:52:55 +000084protected:
mtklein36352bf2015-03-25 18:17:31 -070085 const char* onGetName() override { return "xfermode_create"; }
commit-bot@chromium.org86490572013-10-04 16:52:55 +000086
mtkleina1ebeb22015-10-01 09:43:39 -070087 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000088 for (int outer = 0; outer < loops * 10; ++outer) {
commit-bot@chromium.org86490572013-10-04 16:52:55 +000089 for (int i = 0; i <= SkXfermode::kLastMode; ++i) {
reedcfb6bdf2016-03-29 11:32:50 -070090 (void)SkXfermode::Make(SkXfermode::Mode(i));
commit-bot@chromium.org86490572013-10-04 16:52:55 +000091 }
92 }
93 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000094
commit-bot@chromium.org86490572013-10-04 16:52:55 +000095private:
tfarinaf168b862014-06-19 12:32:29 -070096 typedef Benchmark INHERITED;
commit-bot@chromium.org86490572013-10-04 16:52:55 +000097};
98
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000099//////////////////////////////////////////////////////////////////////////////
100
mtkleind0a10882015-05-13 11:54:00 -0700101#define BENCH(...) \
102 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); ) \
103 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); )
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +0000104
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000105BENCH(SkXfermode::kClear_Mode)
106BENCH(SkXfermode::kSrc_Mode)
107BENCH(SkXfermode::kDst_Mode)
108BENCH(SkXfermode::kSrcOver_Mode)
109BENCH(SkXfermode::kDstOver_Mode)
110BENCH(SkXfermode::kSrcIn_Mode)
111BENCH(SkXfermode::kDstIn_Mode)
112BENCH(SkXfermode::kSrcOut_Mode)
113BENCH(SkXfermode::kDstOut_Mode)
114BENCH(SkXfermode::kSrcATop_Mode)
115BENCH(SkXfermode::kDstATop_Mode)
116BENCH(SkXfermode::kXor_Mode)
117
118BENCH(SkXfermode::kPlus_Mode)
119BENCH(SkXfermode::kModulate_Mode)
120BENCH(SkXfermode::kScreen_Mode)
121
122BENCH(SkXfermode::kOverlay_Mode)
123BENCH(SkXfermode::kDarken_Mode)
124BENCH(SkXfermode::kLighten_Mode)
125BENCH(SkXfermode::kColorDodge_Mode)
126BENCH(SkXfermode::kColorBurn_Mode)
127BENCH(SkXfermode::kHardLight_Mode)
128BENCH(SkXfermode::kSoftLight_Mode)
129BENCH(SkXfermode::kDifference_Mode)
130BENCH(SkXfermode::kExclusion_Mode)
131BENCH(SkXfermode::kMultiply_Mode)
132
133BENCH(SkXfermode::kHue_Mode)
134BENCH(SkXfermode::kSaturation_Mode)
135BENCH(SkXfermode::kColor_Mode)
136BENCH(SkXfermode::kLuminosity_Mode)
137
senorblanco7adf9c12016-04-08 14:29:47 -0700138DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, false), \
139 "arithmetic", false); )
140DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, true), \
141 "arithmetic_enforce_pm", false); )
142DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, false), \
143 "arithmetic", true); )
144DEF_BENCH( return new XfermodeBench(SkArithmeticMode::Make(0.2f, -0.3f, 1.5f, -0.7f, true), \
145 "arithmetic_enforce_pm", true); )
146
commit-bot@chromium.org86490572013-10-04 16:52:55 +0000147DEF_BENCH(return new XferCreateBench;)