blob: 19309f49f6ada3f58566043ce7d28484049095a5 [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"
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000015#include "SkLumaXfermode.h"
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000016
17// Benchmark that draws non-AA rects with an SkXfermode::Mode
18class XfermodeBench : public SkBenchmark {
19public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 XfermodeBench(SkXfermode::Mode mode) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000021 fXfermode.reset(SkXfermode::Create(mode));
22 SkASSERT(NULL != fXfermode.get() || SkXfermode::kSrcOver_Mode == mode);
23 fName.printf("Xfermode_%s", SkXfermode::ModeName(mode));
24 }
25
mtklein@google.com410e6e82013-09-13 19:52:27 +000026 XfermodeBench(SkXfermode* xferMode, const char* name) {
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000027 SkASSERT(NULL != xferMode);
28 fXfermode.reset(xferMode);
29 fName.printf("Xfermode_%s", name);
30 }
31
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000032protected:
33 virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); }
34
35 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
36 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000037 SkRandom random;
mtklein@google.comc2897432013-09-10 19:23:38 +000038 for (int i = 0; i < this->getLoops(); ++i) {
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000039 SkPaint paint;
40 paint.setXfermode(fXfermode.get());
41 paint.setColor(random.nextU());
42 SkScalar w = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
43 SkScalar h = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
44 SkRect rect = SkRect::MakeXYWH(
45 random.nextUScalar1() * (size.fWidth - w),
46 random.nextUScalar1() * (size.fHeight - h),
47 w,
48 h
49 );
50 canvas->drawRect(rect, paint);
51 }
52 }
53
54private:
55 enum {
bsalomon@google.com292ce732013-05-03 16:28:33 +000056 kMinSize = 50,
57 kMaxSize = 100,
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000058 };
59 SkAutoTUnref<SkXfermode> fXfermode;
60 SkString fName;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000061
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000062 typedef SkBenchmark INHERITED;
63};
64
commit-bot@chromium.org86490572013-10-04 16:52:55 +000065class XferCreateBench : public SkBenchmark {
66public:
67 XferCreateBench() {
68 fIsRendering = false;
69 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000070
commit-bot@chromium.org86490572013-10-04 16:52:55 +000071protected:
72 virtual const char* onGetName() SK_OVERRIDE { return "xfermode_create"; }
73
74 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
75 for (int outer = 0; outer < this->getLoops() * 10; ++outer) {
76 for (int i = 0; i <= SkXfermode::kLastMode; ++i) {
77 SkXfermode* xfer = SkXfermode::Create(SkXfermode::Mode(i));
78 SkSafeUnref(xfer);
79 }
80 }
81 }
skia.committer@gmail.comd34f0582013-10-05 07:01:34 +000082
commit-bot@chromium.org86490572013-10-04 16:52:55 +000083private:
84 typedef SkBenchmark INHERITED;
85};
86
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000087//////////////////////////////////////////////////////////////////////////////
88
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000089#define CONCAT_I(x, y) x ## y
90#define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion
91#define BENCH(...) \
mtklein@google.com410e6e82013-09-13 19:52:27 +000092 DEF_BENCH( return new XfermodeBench(__VA_ARGS__); );\
93
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000094
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000095BENCH(SkXfermode::kClear_Mode)
96BENCH(SkXfermode::kSrc_Mode)
97BENCH(SkXfermode::kDst_Mode)
98BENCH(SkXfermode::kSrcOver_Mode)
99BENCH(SkXfermode::kDstOver_Mode)
100BENCH(SkXfermode::kSrcIn_Mode)
101BENCH(SkXfermode::kDstIn_Mode)
102BENCH(SkXfermode::kSrcOut_Mode)
103BENCH(SkXfermode::kDstOut_Mode)
104BENCH(SkXfermode::kSrcATop_Mode)
105BENCH(SkXfermode::kDstATop_Mode)
106BENCH(SkXfermode::kXor_Mode)
107
108BENCH(SkXfermode::kPlus_Mode)
109BENCH(SkXfermode::kModulate_Mode)
110BENCH(SkXfermode::kScreen_Mode)
111
112BENCH(SkXfermode::kOverlay_Mode)
113BENCH(SkXfermode::kDarken_Mode)
114BENCH(SkXfermode::kLighten_Mode)
115BENCH(SkXfermode::kColorDodge_Mode)
116BENCH(SkXfermode::kColorBurn_Mode)
117BENCH(SkXfermode::kHardLight_Mode)
118BENCH(SkXfermode::kSoftLight_Mode)
119BENCH(SkXfermode::kDifference_Mode)
120BENCH(SkXfermode::kExclusion_Mode)
121BENCH(SkXfermode::kMultiply_Mode)
122
123BENCH(SkXfermode::kHue_Mode)
124BENCH(SkXfermode::kSaturation_Mode)
125BENCH(SkXfermode::kColor_Mode)
126BENCH(SkXfermode::kLuminosity_Mode)
127
128BENCH(SkLumaMaskXfermode::Create(SkXfermode::kSrcIn_Mode), "SrcInLuma")
129BENCH(SkLumaMaskXfermode::Create(SkXfermode::kDstIn_Mode), "DstInLuma")
fmalita@google.comef45a642013-09-17 13:09:16 +0000130BENCH(SkLumaMaskXfermode::Create(SkXfermode::kSrcOver_Mode), "SrcOverLuma")
commit-bot@chromium.org86490572013-10-04 16:52:55 +0000131
132DEF_BENCH(return new XferCreateBench;)