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_BENCH3(code, N) \
17    static BenchRegistry gBench##N([](void*) -> Benchmark* { code; });
18#define DEF_BENCH2(code, N) DEF_BENCH3(code, N)
19#define DEF_BENCH(code) DEF_BENCH2(code, __COUNTER__)
20
21/*
22 *  With the above macros, you can register benches as follows (at the bottom
23 *  of your .cpp)
24 *
25 *  DEF_BENCH(return new MyBenchmark(...))
26 *  DEF_BENCH(return new MyBenchmark(...))
27 *  DEF_BENCH(return new MyBenchmark(...))
28 */
29
30
31class SkCanvas;
32class SkPaint;
33
34class SkTriState {
35public:
36    enum State {
37        kDefault,
38        kTrue,
39        kFalse
40    };
41    static const char* Name[];
42};
43
44class Benchmark : public SkRefCnt {
45public:
46    Benchmark();
47
48    const char* getName();
49    const char* getUniqueName();
50    SkIPoint getSize();
51
52    enum Backend {
53        kNonRendering_Backend,
54        kRaster_Backend,
55        kGPU_Backend,
56        kPDF_Backend,
57        kHWUI_Backend,
58    };
59
60    // Call to determine whether the benchmark is intended for
61    // the rendering mode.
62    virtual bool isSuitableFor(Backend backend) {
63        return backend != kNonRendering_Backend;
64    }
65
66    virtual int calculateLoops(int defaultLoops) const {
67        return defaultLoops;
68    }
69
70    // Call before draw, allows the benchmark to do setup work outside of the
71    // timer. When a benchmark is repeatedly drawn, this should be called once
72    // before the initial draw.
73    void delayedSetup();
74
75    // Called once before and after a series of draw calls to a single canvas.
76    // The setup/break down in these calls is not timed.
77    void perCanvasPreDraw(SkCanvas*);
78    void perCanvasPostDraw(SkCanvas*);
79
80    // Called just before and after each call to draw().  Not timed.
81    void preDraw(SkCanvas*);
82    void postDraw(SkCanvas*);
83
84    // Bench framework can tune loops to be large enough for stable timing.
85    void draw(int loops, SkCanvas*);
86
87    void setForceAlpha(int alpha) {
88        fForceAlpha = alpha;
89    }
90
91    void setDither(SkTriState::State state) {
92        fDither = state;
93    }
94
95    /** Assign masks for paint-flags. These will be applied when setupPaint()
96     *  is called.
97     *
98     *  Performs the following on the paint:
99     *      uint32_t flags = paint.getFlags();
100     *      flags &= ~clearMask;
101     *      flags |= orMask;
102     *      paint.setFlags(flags);
103     */
104    void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
105        fOrMask = orMask;
106        fClearMask = clearMask;
107    }
108
109    /*
110     * Benches which support running in a visual mode can advertise this functionality
111     */
112    virtual bool isVisual() { return false; }
113
114    /*
115     * VisualBench frequently resets the canvas.  As a result we need to bulk call all of the hooks
116     */
117    void preTimingHooks(SkCanvas* canvas) {
118        this->perCanvasPreDraw(canvas);
119        this->preDraw(canvas);
120    }
121
122    void postTimingHooks(SkCanvas* canvas)  {
123        this->postDraw(canvas);
124        this->perCanvasPostDraw(canvas);
125    }
126
127    virtual void getGpuStats(SkCanvas*, SkTArray<SkString>* keys, SkTArray<double>* values) {}
128
129protected:
130    virtual void setupPaint(SkPaint* paint);
131
132    virtual const char* onGetName() = 0;
133    virtual const char* onGetUniqueName() { return this->onGetName(); }
134    virtual void onDelayedSetup() {}
135    virtual void onPerCanvasPreDraw(SkCanvas*) {}
136    virtual void onPerCanvasPostDraw(SkCanvas*) {}
137    virtual void onPreDraw(SkCanvas*) {}
138    virtual void onPostDraw(SkCanvas*) {}
139    // Each bench should do its main work in a loop like this:
140    //   for (int i = 0; i < loops; i++) { <work here> }
141    virtual void onDraw(int loops, SkCanvas*) = 0;
142
143    virtual SkIPoint onGetSize();
144
145private:
146    int     fForceAlpha;
147    SkTriState::State  fDither;
148    uint32_t    fOrMask, fClearMask;
149
150    typedef SkRefCnt INHERITED;
151};
152
153typedef SkTRegistry<Benchmark*(*)(void*)> BenchRegistry;
154
155#endif
156