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" |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 13 | #include "SkTRegistry.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 14 | |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 15 | #define DEF_BENCH(code) \ |
| 16 | static SkBenchmark* SK_MACRO_APPEND_LINE(F_)(void* p) { code; } \ |
| 17 | static BenchRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); |
| 18 | |
| 19 | /* |
| 20 | * With the above macros, you can register benches as follows (at the bottom |
| 21 | * of your .cpp) |
| 22 | * |
| 23 | * DEF_BENCH(new MyBenchmark(p, ...)) |
| 24 | * DEF_BENCH(new MyBenchmark(p, ...)) |
| 25 | * DEF_BENCH(new MyBenchmark(p, ...)) |
| 26 | */ |
| 27 | |
| 28 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 29 | class SkCanvas; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 30 | class SkPaint; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 31 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 32 | class SkTriState { |
| 33 | public: |
| 34 | enum State { |
| 35 | kDefault, |
| 36 | kTrue, |
| 37 | kFalse |
| 38 | }; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 39 | static const char* Name[]; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 42 | class SkBenchmark : public SkRefCnt { |
| 43 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 44 | SK_DECLARE_INST_COUNT(SkBenchmark) |
| 45 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 46 | SkBenchmark(void* defineDict); |
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(); |
| 49 | SkIPoint getSize(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 50 | |
| 51 | // Call before draw, allows the benchmark to do setup work outside of the |
| 52 | // timer. When a benchmark is repeatedly drawn, this should be called once |
| 53 | // before the initial draw. |
| 54 | void preDraw(); |
| 55 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 56 | void draw(SkCanvas*); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 57 | |
| 58 | // Call after draw, allows the benchmark to do cleanup work outside of the |
| 59 | // timer. When a benchmark is repeatedly drawn, this is only called once |
| 60 | // after the last draw. |
| 61 | void postDraw(); |
| 62 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 63 | void setForceAlpha(int alpha) { |
| 64 | fForceAlpha = alpha; |
| 65 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 66 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 67 | void setForceAA(bool aa) { |
| 68 | fForceAA = aa; |
| 69 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 70 | |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 71 | void setForceFilter(bool filter) { |
| 72 | fForceFilter = filter; |
| 73 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 74 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 75 | void setDither(SkTriState::State state) { |
| 76 | fDither = state; |
| 77 | } |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 78 | |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 79 | /** If true; the benchmark does rendering; if false, the benchmark |
| 80 | doesn't, and so need not be re-run in every different rendering |
| 81 | mode. */ |
| 82 | bool isRendering() { |
| 83 | return fIsRendering; |
| 84 | } |
| 85 | |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 86 | /** Assign masks for paint-flags. These will be applied when setupPaint() |
| 87 | * is called. |
| 88 | * |
| 89 | * Performs the following on the paint: |
| 90 | * uint32_t flags = paint.getFlags(); |
| 91 | * flags &= ~clearMask; |
| 92 | * flags |= orMask; |
| 93 | * paint.setFlags(flags); |
| 94 | */ |
| 95 | void setPaintMasks(uint32_t orMask, uint32_t clearMask) { |
| 96 | fOrMask = orMask; |
| 97 | fClearMask = clearMask; |
| 98 | } |
| 99 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 100 | // The bench framework calls this to control the runtime of a bench. |
| 101 | void setLoops(int loops) { |
| 102 | fLoops = loops; |
| 103 | } |
| 104 | |
| 105 | // Each bench should do its main work in a loop like this: |
| 106 | // for (int i = 0; i < this->getLoops(); i++) { <work here> } |
| 107 | int getLoops() const { return fLoops; } |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 108 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 109 | protected: |
reed@google.com | 0561a3c | 2012-11-15 19:52:20 +0000 | [diff] [blame] | 110 | virtual void setupPaint(SkPaint* paint); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 111 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 112 | virtual const char* onGetName() = 0; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 113 | virtual void onPreDraw() {} |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 114 | virtual void onDraw(SkCanvas*) = 0; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 115 | virtual void onPostDraw() {} |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 116 | |
| 117 | virtual SkIPoint onGetSize(); |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 118 | /// Defaults to true. |
| 119 | bool fIsRendering; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 120 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 121 | private: |
| 122 | int fForceAlpha; |
| 123 | bool fForceAA; |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 124 | bool fForceFilter; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 125 | SkTriState::State fDither; |
reed@google.com | ef77ec2 | 2013-05-29 15:39:54 +0000 | [diff] [blame] | 126 | uint32_t fOrMask, fClearMask; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 127 | int fLoops; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 128 | |
| 129 | typedef SkRefCnt INHERITED; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 132 | typedef SkTRegistry<SkBenchmark*(*)(void*)> BenchRegistry; |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 133 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 134 | #endif |