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 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 11 | #include "SkPoint.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame^] | 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 | |
| 30 | |
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 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 34 | class SkTriState { |
| 35 | public: |
| 36 | enum State { |
| 37 | kDefault, |
| 38 | kTrue, |
| 39 | kFalse |
| 40 | }; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 41 | static const char* Name[]; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 44 | class Benchmark : public SkRefCnt { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 45 | public: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 46 | Benchmark(); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 47 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 48 | const char* getName(); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 49 | const char* getUniqueName(); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 50 | SkIPoint getSize(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 52 | enum Backend { |
| 53 | kNonRendering_Backend, |
| 54 | kRaster_Backend, |
| 55 | kGPU_Backend, |
| 56 | kPDF_Backend, |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 57 | kHWUI_Backend, |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // Call to determine whether the benchmark is intended for |
| 61 | // the rendering mode. |
| 62 | virtual bool isSuitableFor(Backend backend) { |
| 63 | return backend != kNonRendering_Backend; |
| 64 | } |
| 65 | |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 66 | virtual int calculateLoops(int defaultLoops) const { |
| 67 | return defaultLoops; |
| 68 | } |
| 69 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 70 | // Call before draw, allows the benchmark to do setup work outside of the |
| 71 | // timer. When a benchmark is repeatedly drawn, this should be called once |
| 72 | // before the initial draw. |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 73 | void delayedSetup(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 74 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 75 | // Called once before and after a series of draw calls to a single canvas. |
| 76 | // The setup/break down in these calls is not timed. |
| 77 | void perCanvasPreDraw(SkCanvas*); |
| 78 | void perCanvasPostDraw(SkCanvas*); |
| 79 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 80 | // Called just before and after each call to draw(). Not timed. |
| 81 | void preDraw(SkCanvas*); |
| 82 | void postDraw(SkCanvas*); |
| 83 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 84 | // Bench framework can tune loops to be large enough for stable timing. |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 85 | void draw(int loops, SkCanvas*); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 86 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 87 | void setForceAlpha(int alpha) { |
| 88 | fForceAlpha = alpha; |
| 89 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 90 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 91 | void setDither(SkTriState::State state) { |
| 92 | fDither = state; |
| 93 | } |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 94 | |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 95 | /** Assign masks for paint-flags. These will be applied when setupPaint() |
| 96 | * is called. |
| 97 | * |
| 98 | * Performs the following on the paint: |
| 99 | * uint32_t flags = paint.getFlags(); |
| 100 | * flags &= ~clearMask; |
| 101 | * flags |= orMask; |
| 102 | * paint.setFlags(flags); |
| 103 | */ |
| 104 | void setPaintMasks(uint32_t orMask, uint32_t clearMask) { |
| 105 | fOrMask = orMask; |
| 106 | fClearMask = clearMask; |
| 107 | } |
| 108 | |
joshualitt | 962cc98 | 2015-06-30 07:43:14 -0700 | [diff] [blame] | 109 | /* |
| 110 | * Benches which support running in a visual mode can advertise this functionality |
| 111 | */ |
| 112 | virtual bool isVisual() { return false; } |
| 113 | |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 114 | /* |
| 115 | * VisualBench frequently resets the canvas. As a result we need to bulk call all of the hooks |
| 116 | */ |
| 117 | void preTimingHooks(SkCanvas* canvas) { |
| 118 | this->perCanvasPreDraw(canvas); |
| 119 | this->preDraw(canvas); |
| 120 | } |
| 121 | |
| 122 | void postTimingHooks(SkCanvas* canvas) { |
| 123 | this->postDraw(canvas); |
| 124 | this->perCanvasPostDraw(canvas); |
| 125 | } |
| 126 | |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 127 | virtual void getGpuStats(SkCanvas*, SkTArray<SkString>* keys, SkTArray<double>* values) {} |
| 128 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 129 | protected: |
reed@google.com | 0561a3c | 2012-11-15 19:52:20 +0000 | [diff] [blame] | 130 | virtual void setupPaint(SkPaint* paint); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 131 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 132 | virtual const char* onGetName() = 0; |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 133 | virtual const char* onGetUniqueName() { return this->onGetName(); } |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 134 | virtual void onDelayedSetup() {} |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 135 | virtual void onPerCanvasPreDraw(SkCanvas*) {} |
| 136 | virtual void onPerCanvasPostDraw(SkCanvas*) {} |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 137 | virtual void onPreDraw(SkCanvas*) {} |
| 138 | virtual void onPostDraw(SkCanvas*) {} |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 139 | // Each bench should do its main work in a loop like this: |
| 140 | // for (int i = 0; i < loops; i++) { <work here> } |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 141 | virtual void onDraw(int loops, SkCanvas*) = 0; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 142 | |
| 143 | virtual SkIPoint onGetSize(); |
| 144 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 145 | private: |
| 146 | int fForceAlpha; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 147 | SkTriState::State fDither; |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 148 | uint32_t fOrMask, fClearMask; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 149 | |
| 150 | typedef SkRefCnt INHERITED; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame^] | 153 | typedef sk_tools::Registry<Benchmark*(*)(void*)> BenchRegistry; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 154 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 155 | #endif |