blob: 0c79cb3fa777ec0092908827c49d115dbd12fb1f [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
34 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
35 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000036 SkRandom random;
mtklein@google.comc2897432013-09-10 19:23:38 +000037 for (int i = 0; i < this->getLoops(); ++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:
66 XferCreateBench() {
67 fIsRendering = false;
68 }
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
73 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
74 for (int outer = 0; outer < this->getLoops() * 10; ++outer) {
75 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;)