blob: 61002fc9e9355d412fc9a0e9b61036a8947583aa [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
joshualitt892b0c32015-08-18 12:13:34 -070014#include "ResultsWriter.h"
joshualittda7b8432015-05-27 09:19:03 -070015#include "SkPicture.h"
bsalomon45171e22015-06-16 13:52:18 -070016#include "SkString.h"
joshualittda7b8432015-05-27 09:19:03 -070017#include "SkSurface.h"
joshualitt7fe8ee42015-06-01 10:03:54 -070018#include "Timer.h"
joshualitt962cc982015-06-30 07:43:14 -070019#include "VisualBenchmarkStream.h"
joshualittda7b8432015-05-27 09:19:03 -070020#include "gl/SkGLContext.h"
21
22class GrContext;
23struct GrGLInterface;
24class GrRenderTarget;
25class SkCanvas;
26
27/*
28 * A Visual benchmarking tool for gpu benchmarking
29 */
30class VisualBench : public SkOSWindow {
31public:
32 VisualBench(void* hwnd, int argc, char** argv);
33 ~VisualBench() override;
34
35protected:
36 SkSurface* createSurface() override;
37
38 void draw(SkCanvas* canvas) override;
39
40 void onSizeChange() override;
41
42private:
joshualitta2a6fe82015-07-17 09:09:23 -070043 /*
44 * The heart of visual bench is an event driven timing loop.
45 * kPreWarmLoopsPerCanvasPreDraw_State: Before we begin timing, Benchmarks have a hook to
46 * access the canvas. Then we prewarm before the autotune
47 * loops step.
48 * kPreWarmLoops_State: We prewarm the gpu before auto tuning to enter a steady
49 * work state
50 * kTuneLoops_State: Then we tune the loops of the benchmark to ensure we
51 * are doing a measurable amount of work
52 * kPreWarmTimingPerCanvasPreDraw_State: Because reset the context after tuning loops to ensure
53 * coherent state, we need to give the benchmark
54 * another hook
55 * kPreWarmTiming_State: We prewarm the gpu again to enter a steady state
56 * kTiming_State: Finally we time the benchmark. When finished timing
57 * if we have enough samples then we'll start the next
58 * benchmark in the kPreWarmLoopsPerCanvasPreDraw_State.
59 * otherwise, we enter the
60 * kPreWarmTimingPerCanvasPreDraw_State for another sample
61 * In either case we reset the context.
62 */
63 enum State {
64 kPreWarmLoopsPerCanvasPreDraw_State,
65 kPreWarmLoops_State,
66 kTuneLoops_State,
67 kPreWarmTimingPerCanvasPreDraw_State,
68 kPreWarmTiming_State,
69 kTiming_State,
70 };
joshualittda7b8432015-05-27 09:19:03 -070071 void setTitle();
72 bool setupBackend();
joshualitt7fe8ee42015-06-01 10:03:54 -070073 void resetContext();
joshualittda7b8432015-05-27 09:19:03 -070074 void setupRenderTarget();
75 bool onHandleChar(SkUnichar unichar) override;
joshualitt7fe8ee42015-06-01 10:03:54 -070076 void printStats();
joshualitt962cc982015-06-30 07:43:14 -070077 bool advanceRecordIfNecessary(SkCanvas*);
joshualitt7fe8ee42015-06-01 10:03:54 -070078 inline void renderFrame(SkCanvas*);
joshualitta2a6fe82015-07-17 09:09:23 -070079 inline void nextState(State);
80 void perCanvasPreDraw(SkCanvas*, State);
81 void preWarm(State nextState);
82 void scaleLoops(double elapsedMs);
83 inline void tuneLoops();
84 inline void timing(SkCanvas*);
85 inline double elapsed();
86 void resetTimingState();
87 void postDraw(SkCanvas*);
88 void recordMeasurement();
joshualittda7b8432015-05-27 09:19:03 -070089
bsalomon45171e22015-06-16 13:52:18 -070090 struct Record {
joshualitt7fe8ee42015-06-01 10:03:54 -070091 SkTArray<double> fMeasurements;
92 };
93
joshualitt7fe8ee42015-06-01 10:03:54 -070094 int fCurrentSample;
joshualitt6205af02015-06-23 07:10:59 -070095 int fCurrentFrame;
joshualitt6205af02015-06-23 07:10:59 -070096 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;
joshualitt892b0c32015-08-18 12:13:34 -0700109 SkAutoTDelete<ResultsWriter> fResults;
joshualittda7b8432015-05-27 09:19:03 -0700110
111 typedef SkOSWindow INHERITED;
112};
113
114#endif