aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/VisualBench/VisualBench.h
blob: 9843693777099b433dd371a3a1c2affaf6fdd727 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 */

#ifndef VisualBench_DEFINED
#define VisualBench_DEFINED

#include "SkWindow.h"

#include "SkPicture.h"
#include "SkString.h"
#include "SkSurface.h"
#include "Timer.h"
#include "VisualBenchmarkStream.h"
#include "gl/SkGLContext.h"

class GrContext;
struct GrGLInterface;
class GrRenderTarget;
class SkCanvas;

/*
 * A Visual benchmarking tool for gpu benchmarking
 */
class VisualBench : public SkOSWindow {
public:
    VisualBench(void* hwnd, int argc, char** argv);
    ~VisualBench() override;

protected:
    SkSurface* createSurface() override;

    void draw(SkCanvas* canvas) override;

    void onSizeChange() override;

private:
    /*
     * The heart of visual bench is an event driven timing loop.
     * kPreWarmLoopsPerCanvasPreDraw_State:  Before we begin timing, Benchmarks have a hook to
     *                                       access the canvas.  Then we prewarm before the autotune
     *                                       loops step.
     * kPreWarmLoops_State:                  We prewarm the gpu before auto tuning to enter a steady
     *                                       work state
     * kTuneLoops_State:                     Then we tune the loops of the benchmark to ensure we
     *                                       are doing a measurable amount of work
     * kPreWarmTimingPerCanvasPreDraw_State: Because reset the context after tuning loops to ensure
     *                                       coherent state, we need to give the benchmark
     *                                       another hook
     * kPreWarmTiming_State:                 We prewarm the gpu again to enter a steady state
     * kTiming_State:                        Finally we time the benchmark.  When finished timing
     *                                       if we have enough samples then we'll start the next
     *                                       benchmark in the kPreWarmLoopsPerCanvasPreDraw_State.
     *                                       otherwise, we enter the
     *                                       kPreWarmTimingPerCanvasPreDraw_State for another sample
     *                                       In either case we reset the context.
     */
    enum State {
        kPreWarmLoopsPerCanvasPreDraw_State,
        kPreWarmLoops_State,
        kTuneLoops_State,
        kPreWarmTimingPerCanvasPreDraw_State,
        kPreWarmTiming_State,
        kTiming_State,
    };
    void setTitle();
    bool setupBackend();
    void resetContext();
    void setupRenderTarget();
    bool onHandleChar(SkUnichar unichar) override;
    void printStats();
    bool advanceRecordIfNecessary(SkCanvas*);
    inline void renderFrame(SkCanvas*);
    inline void nextState(State);
    void perCanvasPreDraw(SkCanvas*, State);
    void preWarm(State nextState);
    void scaleLoops(double elapsedMs);
    inline void tuneLoops();
    inline void timing(SkCanvas*);
    inline double elapsed();
    void resetTimingState();
    void postDraw(SkCanvas*);
    void recordMeasurement();

    struct Record {
        SkTArray<double> fMeasurements;
    };

    int fCurrentSample;
    int fCurrentFrame;
    int fFlushes;
    int fLoops;
    SkTArray<Record> fRecords;
    WallTimer fTimer;
    State fState;
    SkAutoTDelete<VisualBenchmarkStream> fBenchmarkStream;
    SkAutoTUnref<Benchmark> fBenchmark;

    // support framework
    SkAutoTUnref<SkSurface> fSurface;
    SkAutoTUnref<GrContext> fContext;
    SkAutoTUnref<GrRenderTarget> fRenderTarget;
    AttachmentInfo fAttachmentInfo;
    SkAutoTUnref<const GrGLInterface> fInterface;

    typedef SkOSWindow INHERITED;
};

#endif