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