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 | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 13 | #include "SkTDict.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 | |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 16 | #define DEF_BENCH(code) \ |
| 17 | static SkBenchmark* SK_MACRO_APPEND_LINE(F_)(void* p) { code; } \ |
| 18 | static BenchRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); |
| 19 | |
| 20 | /* |
| 21 | * With the above macros, you can register benches as follows (at the bottom |
| 22 | * of your .cpp) |
| 23 | * |
| 24 | * DEF_BENCH(new MyBenchmark(p, ...)) |
| 25 | * DEF_BENCH(new MyBenchmark(p, ...)) |
| 26 | * DEF_BENCH(new MyBenchmark(p, ...)) |
| 27 | */ |
| 28 | |
| 29 | |
tomhudson@google.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame] | 30 | #ifdef SK_DEBUG |
| 31 | #define SkBENCHLOOP(n) 1 |
| 32 | #else |
| 33 | #define SkBENCHLOOP(n) n |
| 34 | #endif |
| 35 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 36 | class SkCanvas; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 37 | class SkPaint; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 38 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 39 | class SkTriState { |
| 40 | public: |
| 41 | enum State { |
| 42 | kDefault, |
| 43 | kTrue, |
| 44 | kFalse |
| 45 | }; |
| 46 | }; |
| 47 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 48 | class SkBenchmark : public SkRefCnt { |
| 49 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 50 | SK_DECLARE_INST_COUNT(SkBenchmark) |
| 51 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 52 | SkBenchmark(void* defineDict); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 53 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 54 | const char* getName(); |
| 55 | SkIPoint getSize(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 56 | |
| 57 | // Call before draw, allows the benchmark to do setup work outside of the |
| 58 | // timer. When a benchmark is repeatedly drawn, this should be called once |
| 59 | // before the initial draw. |
| 60 | void preDraw(); |
| 61 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 62 | void draw(SkCanvas*); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 63 | |
| 64 | // Call after draw, allows the benchmark to do cleanup work outside of the |
| 65 | // timer. When a benchmark is repeatedly drawn, this is only called once |
| 66 | // after the last draw. |
| 67 | void postDraw(); |
| 68 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 69 | void setForceAlpha(int alpha) { |
| 70 | fForceAlpha = alpha; |
| 71 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 72 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 73 | void setForceAA(bool aa) { |
| 74 | fForceAA = aa; |
| 75 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 76 | |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 77 | void setForceFilter(bool filter) { |
| 78 | fForceFilter = filter; |
| 79 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 80 | |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 81 | void setDither(SkTriState::State state) { |
| 82 | fDither = state; |
| 83 | } |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 84 | |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 85 | void setStrokeWidth(SkScalar width) { |
| 86 | strokeWidth = width; |
| 87 | fHasStrokeWidth = true; |
| 88 | } |
| 89 | |
| 90 | SkScalar getStrokeWidth() { |
| 91 | return strokeWidth; |
| 92 | } |
| 93 | |
| 94 | bool hasStrokeWidth() { |
| 95 | return fHasStrokeWidth; |
| 96 | } |
| 97 | |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 98 | /** If true; the benchmark does rendering; if false, the benchmark |
| 99 | doesn't, and so need not be re-run in every different rendering |
| 100 | mode. */ |
| 101 | bool isRendering() { |
| 102 | return fIsRendering; |
| 103 | } |
| 104 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 105 | const char* findDefine(const char* key) const; |
reed@android.com | 0c9da39 | 2010-02-22 19:50:13 +0000 | [diff] [blame] | 106 | bool findDefine32(const char* key, int32_t* value) const; |
| 107 | bool findDefineScalar(const char* key, SkScalar* value) const; |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +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: |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 122 | const SkTDict<const char*>* fDict; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 123 | int fForceAlpha; |
| 124 | bool fForceAA; |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 125 | bool fForceFilter; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 126 | SkTriState::State fDither; |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 127 | bool fHasStrokeWidth; |
| 128 | SkScalar strokeWidth; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 129 | |
| 130 | typedef SkRefCnt INHERITED; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 133 | typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry; |
| 134 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 135 | #endif |