blob: 7e61826c79d0a20c2594b4a1e48503257ab784bd [file] [log] [blame]
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
tfarinaf168b862014-06-19 12:32:29 -07009#include "Benchmark.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) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000020 fXfermode.reset(SkXfermode::Create(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
mtkleind0a10882015-05-13 11:54:00 -070026 XfermodeBench(SkXfermode* xferMode, const char* name, bool aa) {
bsalomon49f085d2014-09-05 13:34:00 -070027 SkASSERT(xferMode);
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000028 fXfermode.reset(xferMode);
mtkleind0a10882015-05-13 11:54:00 -070029 fAA = aa;
30 fName.printf("Xfermode_%s%s", name, aa ? "_aa" : "");
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000031 }
32
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000033protected:
mtklein36352bf2015-03-25 18:17:31 -070034 const char* onGetName() override { return fName.c_str(); }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000035
mtklein36352bf2015-03-25 18:17:31 -070036 void onDraw(const int loops, SkCanvas* canvas) override {
mtkleind0a10882015-05-13 11:54:00 -070037 const char* text = "Hamburgefons";
38 size_t len = strlen(text);
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000039 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000040 SkRandom random;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000041 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000042 SkPaint paint;
43 paint.setXfermode(fXfermode.get());
44 paint.setColor(random.nextU());
mtkleind0a10882015-05-13 11:54:00 -070045 if (fAA) {
46 // Draw text to exercise AA code paths.
47 paint.setAntiAlias(true);
48 paint.setTextSize(random.nextRangeScalar(12, 96));
49 SkScalar x = random.nextRangeScalar(0, (SkScalar)size.fWidth),
50 y = random.nextRangeScalar(0, (SkScalar)size.fHeight);
51 for (int j = 0; j < 1000; ++j) {
52 canvas->drawText(text, len, x, y, paint);
53 }
54 } else {
55 // Draw rects to exercise non-AA code paths.
56 SkScalar w = random.nextRangeScalar(50, 100);
57 SkScalar h = random.nextRangeScalar(50, 100);
58 SkRect rect = SkRect::MakeXYWH(
59 random.nextUScalar1() * (size.fWidth - w),
60 random.nextUScalar1() * (size.fHeight - h),
61 w,
62 h
63 );
64 for (int j = 0; j < 1000; ++j) {
65 canvas->drawRect(rect, paint);
66 }
reedf92ace92015-04-02 12:46:24 -070067 }
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000068 }
69 }
70
71private:
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000072 SkAutoTUnref<SkXfermode> fXfermode;
73 SkString fName;
mtkleind0a10882015-05-13 11:54:00 -070074 bool fAA;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000075
tfarinaf168b862014-06-19 12:32:29 -070076 typedef Benchmark INHERITED;
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000077};
78
tfarinaf168b862014-06-19 12:32:29 -070079class XferCreateBench : public Benchmark {
commit-bot@chromium.org86490572013-10-04 16:52:55 +000080public:
mtklein36352bf2015-03-25 18:17:31 -070081 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000082 return backend == kNonRendering_Backend;
commit-bot@chromium.org86490572013-10-04 16:52:55 +000083 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000084
commit-bot@chromium.org86490572013-10-04 16:52:55 +000085protected:
mtklein36352bf2015-03-25 18:17:31 -070086 const char* onGetName() override { return "xfermode_create"; }
commit-bot@chromium.org86490572013-10-04 16:52:55 +000087
mtklein36352bf2015-03-25 18:17:31 -070088 void onDraw(const int loops, SkCanvas* canvas) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000089 for (int outer = 0; outer < loops * 10; ++outer) {
commit-bot@chromium.org86490572013-10-04 16:52:55 +000090 for (int i = 0; i <= SkXfermode::kLastMode; ++i) {
91 SkXfermode* xfer = SkXfermode::Create(SkXfermode::Mode(i));
92 SkSafeUnref(xfer);
93 }
94 }
95 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000096
commit-bot@chromium.org86490572013-10-04 16:52:55 +000097private:
tfarinaf168b862014-06-19 12:32:29 -070098 typedef Benchmark INHERITED;
commit-bot@chromium.org86490572013-10-04 16:52:55 +000099};
100
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +0000101//////////////////////////////////////////////////////////////////////////////
102
mtkleind0a10882015-05-13 11:54:00 -0700103#define BENCH(...) \
104 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); ) \
105 DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); )
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +0000106
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +0000107BENCH(SkXfermode::kClear_Mode)
108BENCH(SkXfermode::kSrc_Mode)
109BENCH(SkXfermode::kDst_Mode)
110BENCH(SkXfermode::kSrcOver_Mode)
111BENCH(SkXfermode::kDstOver_Mode)
112BENCH(SkXfermode::kSrcIn_Mode)
113BENCH(SkXfermode::kDstIn_Mode)
114BENCH(SkXfermode::kSrcOut_Mode)
115BENCH(SkXfermode::kDstOut_Mode)
116BENCH(SkXfermode::kSrcATop_Mode)
117BENCH(SkXfermode::kDstATop_Mode)
118BENCH(SkXfermode::kXor_Mode)
119
120BENCH(SkXfermode::kPlus_Mode)
121BENCH(SkXfermode::kModulate_Mode)
122BENCH(SkXfermode::kScreen_Mode)
123
124BENCH(SkXfermode::kOverlay_Mode)
125BENCH(SkXfermode::kDarken_Mode)
126BENCH(SkXfermode::kLighten_Mode)
127BENCH(SkXfermode::kColorDodge_Mode)
128BENCH(SkXfermode::kColorBurn_Mode)
129BENCH(SkXfermode::kHardLight_Mode)
130BENCH(SkXfermode::kSoftLight_Mode)
131BENCH(SkXfermode::kDifference_Mode)
132BENCH(SkXfermode::kExclusion_Mode)
133BENCH(SkXfermode::kMultiply_Mode)
134
135BENCH(SkXfermode::kHue_Mode)
136BENCH(SkXfermode::kSaturation_Mode)
137BENCH(SkXfermode::kColor_Mode)
138BENCH(SkXfermode::kLuminosity_Mode)
139
commit-bot@chromium.org86490572013-10-04 16:52:55 +0000140DEF_BENCH(return new XferCreateBench;)