1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef Benchmark_DEFINED
9#define Benchmark_DEFINED
10
11#include "SkPoint.h"
12#include "SkRefCnt.h"
13#include "SkString.h"
14#include "SkTRegistry.h"
15
16#define DEF_BENCH(code)                                                 \
17namespace {                                                             \
18static Benchmark* SK_MACRO_APPEND_LINE(factory)(void*) { code; }      \
19BenchRegistry SK_MACRO_APPEND_LINE(g_R_)(SK_MACRO_APPEND_LINE(factory)); \
20}
21
22/*
23 *  With the above macros, you can register benches as follows (at the bottom
24 *  of your .cpp)
25 *
26 *  DEF_BENCH(return new MyBenchmark(...))
27 *  DEF_BENCH(return new MyBenchmark(...))
28 *  DEF_BENCH(return new MyBenchmark(...))
29 */
30
31
32class SkCanvas;
33class SkPaint;
34
35class SkTriState {
36public:
37    enum State {
38        kDefault,
39        kTrue,
40        kFalse
41    };
42    static const char* Name[];
43};
44
45class Benchmark : public SkRefCnt {
46public:
47    SK_DECLARE_INST_COUNT(Benchmark)
48
49    Benchmark();
50
51    const char* getName();
52    SkIPoint getSize();
53
54    enum Backend {
55        kNonRendering_Backend,
56        kRaster_Backend,
57        kGPU_Backend,
58        kPDF_Backend,
59    };
60
61    // Call to determine whether the benchmark is intended for
62    // the rendering mode.
63    virtual bool isSuitableFor(Backend backend) {
64        return backend != kNonRendering_Backend;
65    }
66
67    // Call before draw, allows the benchmark to do setup work outside of the
68    // timer. When a benchmark is repeatedly drawn, this should be called once
69    // before the initial draw.
70    void preDraw();
71
72    // Bench framework can tune loops to be large enough for stable timing.
73    void draw(const int loops, SkCanvas*);
74
75    void setForceAlpha(int alpha) {
76        fForceAlpha = alpha;
77    }
78
79    void setForceAA(bool aa) {
80        fForceAA = aa;
81    }
82
83    void setForceFilter(bool filter) {
84        fForceFilter = filter;
85    }
86
87    void setDither(SkTriState::State state) {
88        fDither = state;
89    }
90
91    /** Assign masks for paint-flags. These will be applied when setupPaint()
92     *  is called.
93     *
94     *  Performs the following on the paint:
95     *      uint32_t flags = paint.getFlags();
96     *      flags &= ~clearMask;
97     *      flags |= orMask;
98     *      paint.setFlags(flags);
99     */
100    void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
101        fOrMask = orMask;
102        fClearMask = clearMask;
103    }
104
105protected:
106    virtual void setupPaint(SkPaint* paint);
107
108    virtual const char* onGetName() = 0;
109    virtual void onPreDraw() {}
110    // Each bench should do its main work in a loop like this:
111    //   for (int i = 0; i < loops; i++) { <work here> }
112    virtual void onDraw(const int loops, SkCanvas*) = 0;
113
114    virtual SkIPoint onGetSize();
115
116private:
117    int     fForceAlpha;
118    bool    fForceAA;
119    bool    fForceFilter;
120    SkTriState::State  fDither;
121    uint32_t    fOrMask, fClearMask;
122
123    typedef SkRefCnt INHERITED;
124};
125
126typedef SkTRegistry<Benchmark*(*)(void*)> BenchRegistry;
127
128#endif
129