epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 7 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #ifndef Benchmark_DEFINED |
| 9 | #define Benchmark_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPoint.h" |
| 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/core/SkString.h" |
| 14 | #include "tools/Registry.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 15 | |
mtklein | d0a1088 | 2015-05-13 11:54:00 -0700 | [diff] [blame] | 16 | #define DEF_BENCH3(code, N) \ |
| 17 | static BenchRegistry gBench##N([](void*) -> Benchmark* { code; }); |
| 18 | #define DEF_BENCH2(code, N) DEF_BENCH3(code, N) |
| 19 | #define DEF_BENCH(code) DEF_BENCH2(code, __COUNTER__) |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * With the above macros, you can register benches as follows (at the bottom |
| 23 | * of your .cpp) |
| 24 | * |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 25 | * DEF_BENCH(return new MyBenchmark(...)) |
| 26 | * DEF_BENCH(return new MyBenchmark(...)) |
| 27 | * DEF_BENCH(return new MyBenchmark(...)) |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame] | 30 | struct GrContextOptions; |
Chris Dalton | 5dfb3f4 | 2021-04-30 17:16:12 -0600 | [diff] [blame] | 31 | class GrRecordingContext; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 32 | class SkCanvas; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 33 | class SkPaint; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 34 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 35 | class Benchmark : public SkRefCnt { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 36 | public: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 37 | Benchmark(); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 38 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 39 | const char* getName(); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 40 | const char* getUniqueName(); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 41 | SkIPoint getSize(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 42 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 43 | enum Backend { |
| 44 | kNonRendering_Backend, |
| 45 | kRaster_Backend, |
| 46 | kGPU_Backend, |
| 47 | kPDF_Backend, |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 48 | kHWUI_Backend, |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // Call to determine whether the benchmark is intended for |
| 52 | // the rendering mode. |
| 53 | virtual bool isSuitableFor(Backend backend) { |
| 54 | return backend != kNonRendering_Backend; |
| 55 | } |
| 56 | |
Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame] | 57 | // Allows a benchmark to override options used to construct the GrContext. |
| 58 | virtual void modifyGrContextOptions(GrContextOptions*) {} |
| 59 | |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 60 | virtual int calculateLoops(int defaultLoops) const { |
| 61 | return defaultLoops; |
| 62 | } |
| 63 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 64 | // Call before draw, allows the benchmark to do setup work outside of the |
| 65 | // timer. When a benchmark is repeatedly drawn, this should be called once |
| 66 | // before the initial draw. |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 67 | void delayedSetup(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 68 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 69 | // Called once before and after a series of draw calls to a single canvas. |
| 70 | // The setup/break down in these calls is not timed. |
| 71 | void perCanvasPreDraw(SkCanvas*); |
| 72 | void perCanvasPostDraw(SkCanvas*); |
| 73 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 74 | // Called just before and after each call to draw(). Not timed. |
| 75 | void preDraw(SkCanvas*); |
| 76 | void postDraw(SkCanvas*); |
| 77 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 78 | // Bench framework can tune loops to be large enough for stable timing. |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 79 | void draw(int loops, SkCanvas*); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 80 | |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 81 | virtual void getGpuStats(SkCanvas*, SkTArray<SkString>* keys, SkTArray<double>* values) {} |
| 82 | |
Chris Dalton | 5dfb3f4 | 2021-04-30 17:16:12 -0600 | [diff] [blame] | 83 | // Replaces the GrRecordingContext's dmsaaStats() with a single frame of this benchmark. |
| 84 | virtual bool getDMSAAStats(GrRecordingContext*) { return false; } |
| 85 | |
Mike Klein | fec9b90 | 2019-05-31 10:03:07 -0500 | [diff] [blame] | 86 | // Count of units (pixels, whatever) being exercised, to scale timing by. |
| 87 | int getUnits() const { return fUnits; } |
| 88 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 89 | protected: |
Mike Klein | fec9b90 | 2019-05-31 10:03:07 -0500 | [diff] [blame] | 90 | void setUnits(int units) { SkASSERT(units > 0); fUnits = units; } |
| 91 | |
reed@google.com | 0561a3c | 2012-11-15 19:52:20 +0000 | [diff] [blame] | 92 | virtual void setupPaint(SkPaint* paint); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 93 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 94 | virtual const char* onGetName() = 0; |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 95 | virtual const char* onGetUniqueName() { return this->onGetName(); } |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 96 | virtual void onDelayedSetup() {} |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 97 | virtual void onPerCanvasPreDraw(SkCanvas*) {} |
| 98 | virtual void onPerCanvasPostDraw(SkCanvas*) {} |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 99 | virtual void onPreDraw(SkCanvas*) {} |
| 100 | virtual void onPostDraw(SkCanvas*) {} |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 101 | // Each bench should do its main work in a loop like this: |
| 102 | // for (int i = 0; i < loops; i++) { <work here> } |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 103 | virtual void onDraw(int loops, SkCanvas*) = 0; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 104 | |
| 105 | virtual SkIPoint onGetSize(); |
| 106 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 107 | private: |
Mike Klein | fec9b90 | 2019-05-31 10:03:07 -0500 | [diff] [blame] | 108 | int fUnits = 1; |
| 109 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 110 | using INHERITED = SkRefCnt; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 113 | typedef sk_tools::Registry<Benchmark*(*)(void*)> BenchRegistry; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 114 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 115 | #endif |