joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
joshualitt | 892b0c3 | 2015-08-18 12:13:34 -0700 | [diff] [blame] | 14 | #include "ResultsWriter.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 15 | #include "SkPicture.h" |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 16 | #include "SkString.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 17 | #include "SkSurface.h" |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 18 | #include "Timer.h" |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 19 | #include "VisualBenchmarkStream.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 20 | #include "gl/SkGLContext.h" |
| 21 | |
| 22 | class GrContext; |
| 23 | struct GrGLInterface; |
| 24 | class GrRenderTarget; |
| 25 | class SkCanvas; |
| 26 | |
| 27 | /* |
| 28 | * A Visual benchmarking tool for gpu benchmarking |
| 29 | */ |
| 30 | class VisualBench : public SkOSWindow { |
| 31 | public: |
| 32 | VisualBench(void* hwnd, int argc, char** argv); |
| 33 | ~VisualBench() override; |
| 34 | |
| 35 | protected: |
| 36 | SkSurface* createSurface() override; |
| 37 | |
| 38 | void draw(SkCanvas* canvas) override; |
| 39 | |
| 40 | void onSizeChange() override; |
| 41 | |
| 42 | private: |
joshualitt | a2a6fe8 | 2015-07-17 09:09:23 -0700 | [diff] [blame] | 43 | /* |
| 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 | }; |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 71 | void setTitle(); |
| 72 | bool setupBackend(); |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 73 | void resetContext(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 74 | void setupRenderTarget(); |
| 75 | bool onHandleChar(SkUnichar unichar) override; |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 76 | void printStats(); |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 77 | bool advanceRecordIfNecessary(SkCanvas*); |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 78 | inline void renderFrame(SkCanvas*); |
joshualitt | a2a6fe8 | 2015-07-17 09:09:23 -0700 | [diff] [blame] | 79 | 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(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 89 | |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 90 | struct Record { |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 91 | SkTArray<double> fMeasurements; |
| 92 | }; |
| 93 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 94 | int fCurrentSample; |
joshualitt | 6205af0 | 2015-06-23 07:10:59 -0700 | [diff] [blame] | 95 | int fCurrentFrame; |
joshualitt | 6205af0 | 2015-06-23 07:10:59 -0700 | [diff] [blame] | 96 | int fLoops; |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 97 | SkTArray<Record> fRecords; |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 98 | WallTimer fTimer; |
| 99 | State fState; |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 100 | SkAutoTDelete<VisualBenchmarkStream> fBenchmarkStream; |
joshualitt | 47d280d | 2015-07-16 14:23:22 -0700 | [diff] [blame] | 101 | SkAutoTUnref<Benchmark> fBenchmark; |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 102 | |
| 103 | // support framework |
| 104 | SkAutoTUnref<SkSurface> fSurface; |
| 105 | SkAutoTUnref<GrContext> fContext; |
| 106 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 107 | AttachmentInfo fAttachmentInfo; |
| 108 | SkAutoTUnref<const GrGLInterface> fInterface; |
joshualitt | 892b0c3 | 2015-08-18 12:13:34 -0700 | [diff] [blame] | 109 | SkAutoTDelete<ResultsWriter> fResults; |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 110 | |
| 111 | typedef SkOSWindow INHERITED; |
| 112 | }; |
| 113 | |
| 114 | #endif |