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" |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 14 | #include "SkTRegistry.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | 6adce67 | 2014-02-03 14:48:17 +0000 | [diff] [blame] | 16 | #define DEF_BENCH(code) \ |
| 17 | namespace { \ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | static Benchmark* SK_MACRO_APPEND_LINE(factory)(void*) { code; } \ |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 19 | BenchRegistry SK_MACRO_APPEND_LINE(g_R_)(SK_MACRO_APPEND_LINE(factory)); \ |
commit-bot@chromium.org | 6adce67 | 2014-02-03 14:48:17 +0000 | [diff] [blame] | 20 | } |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * With the above macros, you can register benches as follows (at the bottom |
| 24 | * of your .cpp) |
| 25 | * |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 26 | * DEF_BENCH(return new MyBenchmark(...)) |
| 27 | * DEF_BENCH(return new MyBenchmark(...)) |
| 28 | * DEF_BENCH(return new MyBenchmark(...)) |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | |
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 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 35 | class SkTriState { |
| 36 | public: |
| 37 | enum State { |
| 38 | kDefault, |
| 39 | kTrue, |
| 40 | kFalse |
| 41 | }; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 42 | static const char* Name[]; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 45 | class Benchmark : public SkRefCnt { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 46 | public: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 47 | SK_DECLARE_INST_COUNT(Benchmark) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 48 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 49 | Benchmark(); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 50 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 51 | const char* getName(); |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 52 | const char* getUniqueName(); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 53 | SkIPoint getSize(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 54 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 55 | enum Backend { |
| 56 | kNonRendering_Backend, |
| 57 | kRaster_Backend, |
| 58 | kGPU_Backend, |
| 59 | kPDF_Backend, |
| 60 | }; |
| 61 | |
| 62 | // Call to determine whether the benchmark is intended for |
| 63 | // the rendering mode. |
| 64 | virtual bool isSuitableFor(Backend backend) { |
| 65 | return backend != kNonRendering_Backend; |
| 66 | } |
| 67 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 68 | // Call before draw, allows the benchmark to do setup work outside of the |
| 69 | // timer. When a benchmark is repeatedly drawn, this should be called once |
| 70 | // before the initial draw. |
| 71 | void preDraw(); |
| 72 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 73 | // Bench framework can tune loops to be large enough for stable timing. |
| 74 | void draw(const int loops, SkCanvas*); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 75 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 76 | void setForceAlpha(int alpha) { |
| 77 | fForceAlpha = alpha; |
| 78 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 79 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 80 | void setDither(SkTriState::State state) { |
| 81 | fDither = state; |
| 82 | } |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 83 | |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 84 | /** Assign masks for paint-flags. These will be applied when setupPaint() |
| 85 | * is called. |
| 86 | * |
| 87 | * Performs the following on the paint: |
| 88 | * uint32_t flags = paint.getFlags(); |
| 89 | * flags &= ~clearMask; |
| 90 | * flags |= orMask; |
| 91 | * paint.setFlags(flags); |
| 92 | */ |
| 93 | void setPaintMasks(uint32_t orMask, uint32_t clearMask) { |
| 94 | fOrMask = orMask; |
| 95 | fClearMask = clearMask; |
| 96 | } |
| 97 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 98 | protected: |
reed@google.com | 0561a3c | 2012-11-15 19:52:20 +0000 | [diff] [blame] | 99 | virtual void setupPaint(SkPaint* paint); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 100 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 101 | virtual const char* onGetName() = 0; |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 102 | virtual const char* onGetUniqueName() { return this->onGetName(); } |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 103 | virtual void onPreDraw() {} |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 104 | // Each bench should do its main work in a loop like this: |
| 105 | // for (int i = 0; i < loops; i++) { <work here> } |
| 106 | virtual void onDraw(const int loops, SkCanvas*) = 0; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 107 | |
| 108 | virtual SkIPoint onGetSize(); |
| 109 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 110 | private: |
| 111 | int fForceAlpha; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 112 | SkTriState::State fDither; |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 113 | uint32_t fOrMask, fClearMask; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 114 | |
| 115 | typedef SkRefCnt INHERITED; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 118 | typedef SkTRegistry<Benchmark*(*)(void*)> BenchRegistry; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 119 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 120 | #endif |