blob: 1910b75b210a4ca7238bf448df566fdd6802f609 [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
9#include "SkBenchmark.h"
10#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkString.h"
14#include "SkXfermode.h"
15
16// Benchmark that draws non-AA rects with an SkXfermode::Mode
17class XfermodeBench : public SkBenchmark {
18public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000019 XfermodeBench(SkXfermode::Mode mode) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000020 fXfermode.reset(SkXfermode::Create(mode));
21 SkASSERT(NULL != fXfermode.get() || SkXfermode::kSrcOver_Mode == mode);
22 fName.printf("Xfermode_%s", SkXfermode::ModeName(mode));
23 }
24
mtklein@google.com410e6e82013-09-13 19:52:27 +000025 XfermodeBench(SkXfermode* xferMode, const char* name) {
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000026 SkASSERT(NULL != xferMode);
27 fXfermode.reset(xferMode);
28 fName.printf("Xfermode_%s", name);
29 }
30
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000031protected:
32 virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); }
33
commit-bot@chromium.org33614712013-12-03 18:17:16 +000034 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000035 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000036 SkRandom random;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000037 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000038 SkPaint paint;
39 paint.setXfermode(fXfermode.get());
40 paint.setColor(random.nextU());
41 SkScalar w = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
42 SkScalar h = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
43 SkRect rect = SkRect::MakeXYWH(
44 random.nextUScalar1() * (size.fWidth - w),
45 random.nextUScalar1() * (size.fHeight - h),
46 w,
47 h
48 );
49 canvas->drawRect(rect, paint);
50 }
51 }
52
53private:
54 enum {
bsalomon@google.com292ce732013-05-03 16:28:33 +000055 kMinSize = 50,
56 kMaxSize = 100,
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000057 };
58 SkAutoTUnref<SkXfermode> fXfermode;
59 SkString fName;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000060
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000061 typedef SkBenchmark INHERITED;
62};
63
commit-bot@chromium.org86490572013-10-04 16:52:55 +000064class XferCreateBench : public SkBenchmark {
65public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000066 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
67 return backend == kNonRendering_Backend;
commit-bot@chromium.org86490572013-10-04 16:52:55 +000068 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000069
commit-bot@chromium.org86490572013-10-04 16:52:55 +000070protected:
71 virtual const char* onGetName() SK_OVERRIDE { return "xfermode_create"; }
72
commit-bot@chromium.org33614712013-12-03 18:17:16 +000073 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
74 for (int outer = 0; outer < loops * 10; ++outer) {
commit-bot@chromium.org86490572013-10-04 16:52:55 +000075 for (int i = 0; i <= SkXfermode::kLastMode; ++i) {
76 SkXfermode* xfer = SkXfermode::Create(SkXfermode::Mode(i));
77 SkSafeUnref(xfer);
78 }
79 }
80 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000081
commit-bot@chromium.org86490572013-10-04 16:52:55 +000082private:
83 typedef SkBenchmark INHERITED;
84};
85
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000086//////////////////////////////////////////////////////////////////////////////
87
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000088#define CONCAT_I(x, y) x ## y
89#define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion
90#define BENCH(...) \
mtklein@google.com410e6e82013-09-13 19:52:27 +000091 DEF_BENCH( return new XfermodeBench(__VA_ARGS__); );\
92
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000093
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000094BENCH(SkXfermode::kClear_Mode)
95BENCH(SkXfermode::kSrc_Mode)
96BENCH(SkXfermode::kDst_Mode)
97BENCH(SkXfermode::kSrcOver_Mode)
98BENCH(SkXfermode::kDstOver_Mode)
99BENCH(SkXfermode::kSrcIn_Mode)
100BENCH(SkXfermode::kDstIn_Mode)
101BENCH(SkXfermode::kSrcOut_Mode)
102BENCH(SkXfermode::kDstOut_Mode)
103BENCH(SkXfermode::kSrcATop_Mode)
104BENCH(SkXfermode::kDstATop_Mode)
105BENCH(SkXfermode::kXor_Mode)
106
107BENCH(SkXfermode::kPlus_Mode)
108BENCH(SkXfermode::kModulate_Mode)
109BENCH(SkXfermode::kScreen_Mode)
110
111BENCH(SkXfermode::kOverlay_Mode)
112BENCH(SkXfermode::kDarken_Mode)
113BENCH(SkXfermode::kLighten_Mode)
114BENCH(SkXfermode::kColorDodge_Mode)
115BENCH(SkXfermode::kColorBurn_Mode)
116BENCH(SkXfermode::kHardLight_Mode)
117BENCH(SkXfermode::kSoftLight_Mode)
118BENCH(SkXfermode::kDifference_Mode)
119BENCH(SkXfermode::kExclusion_Mode)
120BENCH(SkXfermode::kMultiply_Mode)
121
122BENCH(SkXfermode::kHue_Mode)
123BENCH(SkXfermode::kSaturation_Mode)
124BENCH(SkXfermode::kColor_Mode)
125BENCH(SkXfermode::kLuminosity_Mode)
126
commit-bot@chromium.org86490572013-10-04 16:52:55 +0000127DEF_BENCH(return new XferCreateBench;)