joshualitt | 189aef7 | 2015-09-08 07:08:11 -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 VisualLightweightBenchModule_DEFINED |
| 10 | #define VisualLightweightBenchModule_DEFINED |
| 11 | |
| 12 | #include "VisualModule.h" |
| 13 | |
| 14 | #include "ResultsWriter.h" |
| 15 | #include "SkPicture.h" |
| 16 | #include "Timer.h" |
| 17 | #include "VisualBench.h" |
| 18 | #include "VisualBenchmarkStream.h" |
| 19 | |
| 20 | class SkCanvas; |
| 21 | |
| 22 | /* |
| 23 | * This module is designed to be a minimal overhead timing module for VisualBench |
| 24 | */ |
| 25 | class VisualLightweightBenchModule : public VisualModule { |
| 26 | public: |
| 27 | // TODO get rid of backpointer |
| 28 | VisualLightweightBenchModule(VisualBench* owner); |
| 29 | |
| 30 | void draw(SkCanvas* canvas) override; |
| 31 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 32 | bool onHandleChar(SkUnichar c) override; |
| 33 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 34 | private: |
| 35 | /* |
| 36 | * The heart of visual bench is an event driven timing loop. |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 37 | * kWarmup_State: We run a dummy bench to let things settle on startup |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 38 | * kPreWarmLoopsPerCanvasPreDraw_State: Before we begin timing, Benchmarks have a hook to |
| 39 | * access the canvas. Then we prewarm before the autotune |
| 40 | * loops step. |
| 41 | * kPreWarmLoops_State: We prewarm the gpu before auto tuning to enter a steady |
| 42 | * work state |
| 43 | * kTuneLoops_State: Then we tune the loops of the benchmark to ensure we |
| 44 | * are doing a measurable amount of work |
| 45 | * kPreWarmTimingPerCanvasPreDraw_State: Because reset the context after tuning loops to ensure |
| 46 | * coherent state, we need to give the benchmark |
| 47 | * another hook |
| 48 | * kPreWarmTiming_State: We prewarm the gpu again to enter a steady state |
| 49 | * kTiming_State: Finally we time the benchmark. When finished timing |
| 50 | * if we have enough samples then we'll start the next |
| 51 | * benchmark in the kPreWarmLoopsPerCanvasPreDraw_State. |
| 52 | * otherwise, we enter the |
| 53 | * kPreWarmTimingPerCanvasPreDraw_State for another sample |
| 54 | * In either case we reset the context. |
| 55 | */ |
| 56 | enum State { |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 57 | kWarmup_State, |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 58 | kPreWarmLoopsPerCanvasPreDraw_State, |
| 59 | kPreWarmLoops_State, |
| 60 | kTuneLoops_State, |
| 61 | kPreWarmTimingPerCanvasPreDraw_State, |
| 62 | kPreWarmTiming_State, |
| 63 | kTiming_State, |
| 64 | }; |
| 65 | void setTitle(); |
| 66 | bool setupBackend(); |
| 67 | void setupRenderTarget(); |
| 68 | void printStats(); |
| 69 | bool advanceRecordIfNecessary(SkCanvas*); |
| 70 | inline void renderFrame(SkCanvas*); |
| 71 | inline void nextState(State); |
| 72 | void perCanvasPreDraw(SkCanvas*, State); |
| 73 | void preWarm(State nextState); |
| 74 | void scaleLoops(double elapsedMs); |
| 75 | inline void tuneLoops(); |
| 76 | inline void timing(SkCanvas*); |
| 77 | inline double elapsed(); |
| 78 | void resetTimingState(); |
| 79 | void postDraw(SkCanvas*); |
| 80 | void recordMeasurement(); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame^] | 81 | void warmup(SkCanvas* canvas); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 82 | |
| 83 | struct Record { |
| 84 | SkTArray<double> fMeasurements; |
| 85 | }; |
| 86 | |
| 87 | int fCurrentSample; |
| 88 | int fCurrentFrame; |
| 89 | int fLoops; |
| 90 | SkTArray<Record> fRecords; |
| 91 | WallTimer fTimer; |
| 92 | State fState; |
| 93 | SkAutoTDelete<VisualBenchmarkStream> fBenchmarkStream; |
| 94 | SkAutoTUnref<Benchmark> fBenchmark; |
| 95 | |
| 96 | // support framework |
| 97 | SkAutoTUnref<VisualBench> fOwner; |
| 98 | SkAutoTDelete<ResultsWriter> fResults; |
| 99 | |
| 100 | typedef VisualModule INHERITED; |
| 101 | }; |
| 102 | |
| 103 | #endif |