blob: 9843693777099b433dd371a3a1c2affaf6fdd727 [file] [log] [blame]
joshualittda7b8432015-05-27 09:19:03 -07001/*
2 * Copyright 2015 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
9#ifndef VisualBench_DEFINED
10#define VisualBench_DEFINED
11
12#include "SkWindow.h"
13
14#include "SkPicture.h"
bsalomon45171e22015-06-16 13:52:18 -070015#include "SkString.h"
joshualittda7b8432015-05-27 09:19:03 -070016#include "SkSurface.h"
joshualitt7fe8ee42015-06-01 10:03:54 -070017#include "Timer.h"
joshualitt962cc982015-06-30 07:43:14 -070018#include "VisualBenchmarkStream.h"
joshualittda7b8432015-05-27 09:19:03 -070019#include "gl/SkGLContext.h"
20
21class GrContext;
22struct GrGLInterface;
23class GrRenderTarget;
24class SkCanvas;
25
26/*
27 * A Visual benchmarking tool for gpu benchmarking
28 */
29class VisualBench : public SkOSWindow {
30public:
31 VisualBench(void* hwnd, int argc, char** argv);
32 ~VisualBench() override;
33
34protected:
35 SkSurface* createSurface() override;
36
37 void draw(SkCanvas* canvas) override;
38
39 void onSizeChange() override;
40
41private:
joshualitta2a6fe82015-07-17 09:09:23 -070042 /*
43 * The heart of visual bench is an event driven timing loop.
44 * kPreWarmLoopsPerCanvasPreDraw_State: Before we begin timing, Benchmarks have a hook to
45 * access the canvas. Then we prewarm before the autotune
46 * loops step.
47 * kPreWarmLoops_State: We prewarm the gpu before auto tuning to enter a steady
48 * work state
49 * kTuneLoops_State: Then we tune the loops of the benchmark to ensure we
50 * are doing a measurable amount of work
51 * kPreWarmTimingPerCanvasPreDraw_State: Because reset the context after tuning loops to ensure
52 * coherent state, we need to give the benchmark
53 * another hook
54 * kPreWarmTiming_State: We prewarm the gpu again to enter a steady state
55 * kTiming_State: Finally we time the benchmark. When finished timing
56 * if we have enough samples then we'll start the next
57 * benchmark in the kPreWarmLoopsPerCanvasPreDraw_State.
58 * otherwise, we enter the
59 * kPreWarmTimingPerCanvasPreDraw_State for another sample
60 * In either case we reset the context.
61 */
62 enum State {
63 kPreWarmLoopsPerCanvasPreDraw_State,
64 kPreWarmLoops_State,
65 kTuneLoops_State,
66 kPreWarmTimingPerCanvasPreDraw_State,
67 kPreWarmTiming_State,
68 kTiming_State,
69 };
joshualittda7b8432015-05-27 09:19:03 -070070 void setTitle();
71 bool setupBackend();
joshualitt7fe8ee42015-06-01 10:03:54 -070072 void resetContext();
joshualittda7b8432015-05-27 09:19:03 -070073 void setupRenderTarget();
74 bool onHandleChar(SkUnichar unichar) override;
joshualitt7fe8ee42015-06-01 10:03:54 -070075 void printStats();
joshualitt962cc982015-06-30 07:43:14 -070076 bool advanceRecordIfNecessary(SkCanvas*);
joshualitt7fe8ee42015-06-01 10:03:54 -070077 inline void renderFrame(SkCanvas*);
joshualitta2a6fe82015-07-17 09:09:23 -070078 inline void nextState(State);
79 void perCanvasPreDraw(SkCanvas*, State);
80 void preWarm(State nextState);
81 void scaleLoops(double elapsedMs);
82 inline void tuneLoops();
83 inline void timing(SkCanvas*);
84 inline double elapsed();
85 void resetTimingState();
86 void postDraw(SkCanvas*);
87 void recordMeasurement();
joshualittda7b8432015-05-27 09:19:03 -070088
bsalomon45171e22015-06-16 13:52:18 -070089 struct Record {
joshualitt7fe8ee42015-06-01 10:03:54 -070090 SkTArray<double> fMeasurements;
91 };
92
joshualitt7fe8ee42015-06-01 10:03:54 -070093 int fCurrentSample;
joshualitt6205af02015-06-23 07:10:59 -070094 int fCurrentFrame;
95 int fFlushes;
96 int fLoops;
bsalomon45171e22015-06-16 13:52:18 -070097 SkTArray<Record> fRecords;
joshualitt7fe8ee42015-06-01 10:03:54 -070098 WallTimer fTimer;
99 State fState;
joshualitt962cc982015-06-30 07:43:14 -0700100 SkAutoTDelete<VisualBenchmarkStream> fBenchmarkStream;
joshualitt47d280d2015-07-16 14:23:22 -0700101 SkAutoTUnref<Benchmark> fBenchmark;
joshualittda7b8432015-05-27 09:19:03 -0700102
103 // support framework
104 SkAutoTUnref<SkSurface> fSurface;
105 SkAutoTUnref<GrContext> fContext;
106 SkAutoTUnref<GrRenderTarget> fRenderTarget;
107 AttachmentInfo fAttachmentInfo;
108 SkAutoTUnref<const GrGLInterface> fInterface;
109
110 typedef SkOSWindow INHERITED;
111};
112
113#endif