blob: 71cf0e69b5dc0d3f0b8062992dee72ea400687ba [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:
20 XfermodeBench(void* param, SkXfermode::Mode mode) : SkBenchmark(param) {
21 fXfermode.reset(SkXfermode::Create(mode));
22 SkASSERT(NULL != fXfermode.get() || SkXfermode::kSrcOver_Mode == mode);
23 fName.printf("Xfermode_%s", SkXfermode::ModeName(mode));
24 }
25
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000026 XfermodeBench(void* param, SkXfermode* xferMode, const char* name)
27 : SkBenchmark(param) {
28 SkASSERT(NULL != xferMode);
29 fXfermode.reset(xferMode);
30 fName.printf("Xfermode_%s", name);
31 }
32
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000033protected:
34 virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); }
35
36 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
37 SkISize size = canvas->getDeviceSize();
38 SkMWCRandom random;
39 for (int i = 0; i < kNumRects; ++i) {
40 SkPaint paint;
41 paint.setXfermode(fXfermode.get());
42 paint.setColor(random.nextU());
43 SkScalar w = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
44 SkScalar h = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
45 SkRect rect = SkRect::MakeXYWH(
46 random.nextUScalar1() * (size.fWidth - w),
47 random.nextUScalar1() * (size.fHeight - h),
48 w,
49 h
50 );
51 canvas->drawRect(rect, paint);
52 }
53 }
54
55private:
56 enum {
bsalomon@google.com292ce732013-05-03 16:28:33 +000057 kNumRects = SkBENCHLOOP(75),
58 kMinSize = 50,
59 kMaxSize = 100,
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000060 };
61 SkAutoTUnref<SkXfermode> fXfermode;
62 SkString fName;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000063
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000064 typedef SkBenchmark INHERITED;
65};
66
67//////////////////////////////////////////////////////////////////////////////
68
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000069#define CONCAT_I(x, y) x ## y
70#define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion
71#define BENCH(...) \
72 static SkBenchmark* CONCAT(Fact, __LINE__)(void *p) { return new XfermodeBench(p, __VA_ARGS__); };\
73 static BenchRegistry CONCAT(gReg, __LINE__)(CONCAT(Fact, __LINE__));
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +000074
commit-bot@chromium.orgeaa77972013-08-23 13:13:12 +000075BENCH(SkXfermode::kClear_Mode)
76BENCH(SkXfermode::kSrc_Mode)
77BENCH(SkXfermode::kDst_Mode)
78BENCH(SkXfermode::kSrcOver_Mode)
79BENCH(SkXfermode::kDstOver_Mode)
80BENCH(SkXfermode::kSrcIn_Mode)
81BENCH(SkXfermode::kDstIn_Mode)
82BENCH(SkXfermode::kSrcOut_Mode)
83BENCH(SkXfermode::kDstOut_Mode)
84BENCH(SkXfermode::kSrcATop_Mode)
85BENCH(SkXfermode::kDstATop_Mode)
86BENCH(SkXfermode::kXor_Mode)
87
88BENCH(SkXfermode::kPlus_Mode)
89BENCH(SkXfermode::kModulate_Mode)
90BENCH(SkXfermode::kScreen_Mode)
91
92BENCH(SkXfermode::kOverlay_Mode)
93BENCH(SkXfermode::kDarken_Mode)
94BENCH(SkXfermode::kLighten_Mode)
95BENCH(SkXfermode::kColorDodge_Mode)
96BENCH(SkXfermode::kColorBurn_Mode)
97BENCH(SkXfermode::kHardLight_Mode)
98BENCH(SkXfermode::kSoftLight_Mode)
99BENCH(SkXfermode::kDifference_Mode)
100BENCH(SkXfermode::kExclusion_Mode)
101BENCH(SkXfermode::kMultiply_Mode)
102
103BENCH(SkXfermode::kHue_Mode)
104BENCH(SkXfermode::kSaturation_Mode)
105BENCH(SkXfermode::kColor_Mode)
106BENCH(SkXfermode::kLuminosity_Mode)
107
108BENCH(SkLumaMaskXfermode::Create(SkXfermode::kSrcIn_Mode), "SrcInLuma")
109BENCH(SkLumaMaskXfermode::Create(SkXfermode::kDstIn_Mode), "DstInLuma")