epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 8 | #ifndef SkBenchmark_DEFINED |
| 9 | #define SkBenchmark_DEFINED |
| 10 | |
| 11 | #include "SkRefCnt.h" |
| 12 | #include "SkPoint.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 { \ |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 18 | static SkBenchmark* SK_MACRO_APPEND_LINE(factory)(void*) { code; } \ |
| 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 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 45 | class SkBenchmark : public SkRefCnt { |
| 46 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 47 | SK_DECLARE_INST_COUNT(SkBenchmark) |
| 48 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 49 | SkBenchmark(); |
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(); |
| 52 | SkIPoint getSize(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 53 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 54 | enum Backend { |
| 55 | kNonRendering_Backend, |
| 56 | kRaster_Backend, |
| 57 | kGPU_Backend, |
| 58 | kPDF_Backend, |
| 59 | }; |
| 60 | |
| 61 | // Call to determine whether the benchmark is intended for |
| 62 | // the rendering mode. |
| 63 | virtual bool isSuitableFor(Backend backend) { |
| 64 | return backend != kNonRendering_Backend; |
| 65 | } |
| 66 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 67 | // Call before draw, allows the benchmark to do setup work outside of the |
| 68 | // timer. When a benchmark is repeatedly drawn, this should be called once |
| 69 | // before the initial draw. |
| 70 | void preDraw(); |
| 71 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 72 | // Bench framework can tune loops to be large enough for stable timing. |
| 73 | void draw(const int loops, SkCanvas*); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 74 | |
| 75 | // Call after draw, allows the benchmark to do cleanup work outside of the |
| 76 | // timer. When a benchmark is repeatedly drawn, this is only called once |
| 77 | // after the last draw. |
| 78 | void postDraw(); |
| 79 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 80 | void setForceAlpha(int alpha) { |
| 81 | fForceAlpha = alpha; |
| 82 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 83 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 84 | void setForceAA(bool aa) { |
| 85 | fForceAA = aa; |
| 86 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 87 | |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 88 | void setForceFilter(bool filter) { |
| 89 | fForceFilter = filter; |
| 90 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 91 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 92 | void setDither(SkTriState::State state) { |
| 93 | fDither = state; |
| 94 | } |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 95 | |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 96 | /** Assign masks for paint-flags. These will be applied when setupPaint() |
| 97 | * is called. |
| 98 | * |
| 99 | * Performs the following on the paint: |
| 100 | * uint32_t flags = paint.getFlags(); |
| 101 | * flags &= ~clearMask; |
| 102 | * flags |= orMask; |
| 103 | * paint.setFlags(flags); |
| 104 | */ |
| 105 | void setPaintMasks(uint32_t orMask, uint32_t clearMask) { |
| 106 | fOrMask = orMask; |
| 107 | fClearMask = clearMask; |
| 108 | } |
| 109 | |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 110 | static void SetResourcePath(const char* resPath) { gResourcePath.set(resPath); } |
| 111 | |
| 112 | static SkString& GetResourcePath() { return gResourcePath; } |
| 113 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 114 | protected: |
reed@google.com | 0561a3c | 2012-11-15 19:52:20 +0000 | [diff] [blame] | 115 | virtual void setupPaint(SkPaint* paint); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 116 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 117 | virtual const char* onGetName() = 0; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 118 | virtual void onPreDraw() {} |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 119 | // Each bench should do its main work in a loop like this: |
| 120 | // for (int i = 0; i < loops; i++) { <work here> } |
| 121 | virtual void onDraw(const int loops, SkCanvas*) = 0; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 122 | virtual void onPostDraw() {} |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 123 | |
| 124 | virtual SkIPoint onGetSize(); |
| 125 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 126 | private: |
| 127 | int fForceAlpha; |
| 128 | bool fForceAA; |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 129 | bool fForceFilter; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 130 | SkTriState::State fDither; |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 131 | uint32_t fOrMask, fClearMask; |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 132 | static SkString gResourcePath; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 133 | |
| 134 | typedef SkRefCnt INHERITED; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 137 | typedef SkTRegistry<SkBenchmark*(*)(void*)> BenchRegistry; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 138 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 139 | #endif |