skia2/bench/XfermodeBench.cpp
reed f92ace90d8 experimental speedup some xfermodes with Sk4f
Old:
   7M	1	11.1ms	11.3ms	11.3ms	11.6ms	1%	▅▄▂▂▁▁▄▄█▇	8888	Xfermode_Screen
   7M	1	10.7ms	10.9ms	10.9ms	11.1ms	1%	▄▄▄▇▃▁█▄▂▅	8888	Xfermode_Modulate
   7M	1	7.86ms	8.03ms	8ms	8.18ms	1%	█▇▅▁▃▃▂▃▆▅	8888	Xfermode_Plus
   7M	1	14.6ms	14.8ms	14.8ms	15.1ms	1%	▄█▆▅▄▁▁▆▄▆	8888	Xfermode_Xor
   7M	1	13ms	13.5ms	13.4ms	13.8ms	2%	▅▃▇▁█▂▃▅▃▅	8888	Xfermode_DstATop
   7M	1	13.1ms	13.4ms	13.3ms	13.6ms	1%	▄▁▁▆▅▄▇▆█▂	8888	Xfermode_SrcATop

New:
   7M	1	6.99ms	7.19ms	7.4ms	8.98ms	8%	▁▂▁▃▂█▁▂▂▂	8888	Xfermode_Screen
   7M	1	5.27ms	5.46ms	5.46ms	5.89ms	3%	▁▁▅▁▂█▄▃▄▃	8888	Xfermode_Modulate
   7M	1	6.8ms	7.04ms	7.27ms	8.53ms	8%	▂▁█▁▁▂▂▂▂▇	8888	Xfermode_Plus
   7M	1	9ms	9.2ms	9.33ms	10.5ms	5%	▁█▃▁▂▁▁▁▅▂	8888	Xfermode_Xor
   7M	1	8.34ms	8.57ms	8.73ms	10.6ms	8%	▁▁▁▂▂▂▂▂▂█	8888	Xfermode_DstATop
   7M	1	8.38ms	8.62ms	8.91ms	10.3ms	8%	▁▃▁▂▇▂▁▂▁█	8888	Xfermode_SrcATop

Need to define SK_SUPPORT_LEGACY_SCALAR_XFERMODES in chrome to suppress change (see https://codereview.chromium.org/1054083002/)

Review URL: https://codereview.chromium.org/1043413002
2015-04-02 12:46:24 -07:00

130 lines
3.7 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Benchmark.h"
#include "SkCanvas.h"
#include "SkPaint.h"
#include "SkRandom.h"
#include "SkString.h"
#include "SkXfermode.h"
// Benchmark that draws non-AA rects with an SkXfermode::Mode
class XfermodeBench : public Benchmark {
public:
XfermodeBench(SkXfermode::Mode mode) {
fXfermode.reset(SkXfermode::Create(mode));
SkASSERT(fXfermode.get() || SkXfermode::kSrcOver_Mode == mode);
fName.printf("Xfermode_%s", SkXfermode::ModeName(mode));
}
XfermodeBench(SkXfermode* xferMode, const char* name) {
SkASSERT(xferMode);
fXfermode.reset(xferMode);
fName.printf("Xfermode_%s", name);
}
protected:
const char* onGetName() override { return fName.c_str(); }
void onDraw(const int loops, SkCanvas* canvas) override {
SkISize size = canvas->getDeviceSize();
SkRandom random;
for (int i = 0; i < loops; ++i) {
SkPaint paint;
paint.setXfermode(fXfermode.get());
paint.setColor(random.nextU());
SkScalar w = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
SkScalar h = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize));
SkRect rect = SkRect::MakeXYWH(
random.nextUScalar1() * (size.fWidth - w),
random.nextUScalar1() * (size.fHeight - h),
w,
h
);
for (int j = 0; j < 1000; ++j) {
canvas->drawRect(rect, paint);
}
}
}
private:
enum {
kMinSize = 50,
kMaxSize = 100,
};
SkAutoTUnref<SkXfermode> fXfermode;
SkString fName;
typedef Benchmark INHERITED;
};
class XferCreateBench : public Benchmark {
public:
bool isSuitableFor(Backend backend) override {
return backend == kNonRendering_Backend;
}
protected:
const char* onGetName() override { return "xfermode_create"; }
void onDraw(const int loops, SkCanvas* canvas) override {
for (int outer = 0; outer < loops * 10; ++outer) {
for (int i = 0; i <= SkXfermode::kLastMode; ++i) {
SkXfermode* xfer = SkXfermode::Create(SkXfermode::Mode(i));
SkSafeUnref(xfer);
}
}
}
private:
typedef Benchmark INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
#define CONCAT_I(x, y) x ## y
#define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion
#define BENCH(...) \
DEF_BENCH( return new XfermodeBench(__VA_ARGS__); );\
BENCH(SkXfermode::kClear_Mode)
BENCH(SkXfermode::kSrc_Mode)
BENCH(SkXfermode::kDst_Mode)
BENCH(SkXfermode::kSrcOver_Mode)
BENCH(SkXfermode::kDstOver_Mode)
BENCH(SkXfermode::kSrcIn_Mode)
BENCH(SkXfermode::kDstIn_Mode)
BENCH(SkXfermode::kSrcOut_Mode)
BENCH(SkXfermode::kDstOut_Mode)
BENCH(SkXfermode::kSrcATop_Mode)
BENCH(SkXfermode::kDstATop_Mode)
BENCH(SkXfermode::kXor_Mode)
BENCH(SkXfermode::kPlus_Mode)
BENCH(SkXfermode::kModulate_Mode)
BENCH(SkXfermode::kScreen_Mode)
BENCH(SkXfermode::kOverlay_Mode)
BENCH(SkXfermode::kDarken_Mode)
BENCH(SkXfermode::kLighten_Mode)
BENCH(SkXfermode::kColorDodge_Mode)
BENCH(SkXfermode::kColorBurn_Mode)
BENCH(SkXfermode::kHardLight_Mode)
BENCH(SkXfermode::kSoftLight_Mode)
BENCH(SkXfermode::kDifference_Mode)
BENCH(SkXfermode::kExclusion_Mode)
BENCH(SkXfermode::kMultiply_Mode)
BENCH(SkXfermode::kHue_Mode)
BENCH(SkXfermode::kSaturation_Mode)
BENCH(SkXfermode::kColor_Mode)
BENCH(SkXfermode::kLuminosity_Mode)
DEF_BENCH(return new XferCreateBench;)