aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/XfermodeBench.cpp
blob: 0d7fe6f200a5d4296837ec14afa5469d070a0fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

/*
 * 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 "SkBenchmark.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 SkBenchmark {
public:
    XfermodeBench(void* param, SkXfermode::Mode mode) : SkBenchmark(param) {
        fXfermode.reset(SkXfermode::Create(mode));
        SkASSERT(NULL != fXfermode.get() || SkXfermode::kSrcOver_Mode == mode);
        fName.printf("Xfermode_%s", SkXfermode::ModeName(mode));
    }

protected:
    virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); }

    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
        SkISize size = canvas->getDeviceSize();
        SkMWCRandom random;
        for (int i = 0; i < kNumRects; ++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
            );
            canvas->drawRect(rect, paint);
        }
    }

private:
    enum {
        kNumRects = SkBENCHLOOP(1000),
        kMinSize = 10,
        kMaxSize = 400,
    };
    SkAutoTUnref<SkXfermode> fXfermode;
    SkString fName;

    typedef SkBenchmark INHERITED;
};

//////////////////////////////////////////////////////////////////////////////


// These modes were chosen because they are expected to be successively harder for the GPU.
// kSrc can disable blending, kSrcOver cannot, kDarken requires reading the dst pixel in the shader.
static SkBenchmark* Fact0(void* p) { return new XfermodeBench(p, SkXfermode::kSrc_Mode); }
static SkBenchmark* Fact1(void* p) { return new XfermodeBench(p, SkXfermode::kSrcOver_Mode); }
static SkBenchmark* Fact2(void* p) { return new XfermodeBench(p, SkXfermode::kDarken_Mode); }

static BenchRegistry gReg0(Fact0);
static BenchRegistry gReg1(Fact1);
static BenchRegistry gReg2(Fact2);