OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #ifndef SkBenchmark_DEFINED | 8 #ifndef SkBenchmark_DEFINED |
9 #define SkBenchmark_DEFINED | 9 #define SkBenchmark_DEFINED |
10 | 10 |
11 #include "SkRefCnt.h" | 11 #include "SkRefCnt.h" |
12 #include "SkPoint.h" | 12 #include "SkPoint.h" |
13 #include "SkTDict.h" | |
14 #include "SkTRegistry.h" | 13 #include "SkTRegistry.h" |
15 | 14 |
16 #define DEF_BENCH(code) \ | 15 #define DEF_BENCH(code) \ |
17 static SkBenchmark* SK_MACRO_APPEND_LINE(F_)(void* p) { code; } \ | 16 static SkBenchmark* SK_MACRO_APPEND_LINE(F_)(void* p) { code; } \ |
18 static BenchRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); | 17 static BenchRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); |
19 | 18 |
20 /* | 19 /* |
21 * With the above macros, you can register benches as follows (at the bottom | 20 * With the above macros, you can register benches as follows (at the bottom |
22 * of your .cpp) | 21 * of your .cpp) |
23 * | 22 * |
24 * DEF_BENCH(new MyBenchmark(p, ...)) | 23 * DEF_BENCH(new MyBenchmark(p, ...)) |
25 * DEF_BENCH(new MyBenchmark(p, ...)) | 24 * DEF_BENCH(new MyBenchmark(p, ...)) |
26 * DEF_BENCH(new MyBenchmark(p, ...)) | 25 * DEF_BENCH(new MyBenchmark(p, ...)) |
27 */ | 26 */ |
28 | 27 |
29 | 28 |
30 #ifdef SK_DEBUG | |
31 #define SkBENCHLOOP(n) 1 | |
32 #else | |
33 #define SkBENCHLOOP(n) n | |
34 #endif | |
35 | |
36 class SkCanvas; | 29 class SkCanvas; |
37 class SkPaint; | 30 class SkPaint; |
38 | 31 |
39 class SkTriState { | 32 class SkTriState { |
40 public: | 33 public: |
41 enum State { | 34 enum State { |
42 kDefault, | 35 kDefault, |
43 kTrue, | 36 kTrue, |
44 kFalse | 37 kFalse |
45 }; | 38 }; |
| 39 static const char* Name[]; |
46 }; | 40 }; |
47 | 41 |
48 class SkBenchmark : public SkRefCnt { | 42 class SkBenchmark : public SkRefCnt { |
49 public: | 43 public: |
50 SK_DECLARE_INST_COUNT(SkBenchmark) | 44 SK_DECLARE_INST_COUNT(SkBenchmark) |
51 | 45 |
52 SkBenchmark(void* defineDict); | 46 SkBenchmark(void* defineDict); |
53 | 47 |
54 const char* getName(); | 48 const char* getName(); |
55 SkIPoint getSize(); | 49 SkIPoint getSize(); |
(...skipping 19 matching lines...) Expand all Loading... |
75 } | 69 } |
76 | 70 |
77 void setForceFilter(bool filter) { | 71 void setForceFilter(bool filter) { |
78 fForceFilter = filter; | 72 fForceFilter = filter; |
79 } | 73 } |
80 | 74 |
81 void setDither(SkTriState::State state) { | 75 void setDither(SkTriState::State state) { |
82 fDither = state; | 76 fDither = state; |
83 } | 77 } |
84 | 78 |
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 | |
98 /** If true; the benchmark does rendering; if false, the benchmark | 79 /** If true; the benchmark does rendering; if false, the benchmark |
99 doesn't, and so need not be re-run in every different rendering | 80 doesn't, and so need not be re-run in every different rendering |
100 mode. */ | 81 mode. */ |
101 bool isRendering() { | 82 bool isRendering() { |
102 return fIsRendering; | 83 return fIsRendering; |
103 } | 84 } |
104 | 85 |
105 const char* findDefine(const char* key) const; | |
106 bool findDefine32(const char* key, int32_t* value) const; | |
107 bool findDefineScalar(const char* key, SkScalar* value) const; | |
108 | |
109 /** Assign masks for paint-flags. These will be applied when setupPaint() | 86 /** Assign masks for paint-flags. These will be applied when setupPaint() |
110 * is called. | 87 * is called. |
111 * | 88 * |
112 * Performs the following on the paint: | 89 * Performs the following on the paint: |
113 * uint32_t flags = paint.getFlags(); | 90 * uint32_t flags = paint.getFlags(); |
114 * flags &= ~clearMask; | 91 * flags &= ~clearMask; |
115 * flags |= orMask; | 92 * flags |= orMask; |
116 * paint.setFlags(flags); | 93 * paint.setFlags(flags); |
117 */ | 94 */ |
118 void setPaintMasks(uint32_t orMask, uint32_t clearMask) { | 95 void setPaintMasks(uint32_t orMask, uint32_t clearMask) { |
119 fOrMask = orMask; | 96 fOrMask = orMask; |
120 fClearMask = clearMask; | 97 fClearMask = clearMask; |
121 } | 98 } |
122 | 99 |
123 float getDurationScale() { return this->onGetDurationScale(); } | 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; } |
124 | 108 |
125 protected: | 109 protected: |
126 virtual void setupPaint(SkPaint* paint); | 110 virtual void setupPaint(SkPaint* paint); |
127 | 111 |
128 virtual const char* onGetName() = 0; | 112 virtual const char* onGetName() = 0; |
129 virtual void onPreDraw() {} | 113 virtual void onPreDraw() {} |
130 virtual void onDraw(SkCanvas*) = 0; | 114 virtual void onDraw(SkCanvas*) = 0; |
131 virtual void onPostDraw() {} | 115 virtual void onPostDraw() {} |
132 // the caller will scale the computed duration by this value. It allows a | |
133 // slow bench to run fewer inner loops, but return the corresponding scale | |
134 // so that its reported duration can be compared against other benches. | |
135 // e.g. | |
136 // if I run 10x slower, I can run 1/10 the number of inner-loops, but | |
137 // return 10.0 for my durationScale, so I "report" the honest duration. | |
138 virtual float onGetDurationScale() { return 1; } | |
139 | 116 |
140 virtual SkIPoint onGetSize(); | 117 virtual SkIPoint onGetSize(); |
141 /// Defaults to true. | 118 /// Defaults to true. |
142 bool fIsRendering; | 119 bool fIsRendering; |
143 | 120 |
144 private: | 121 private: |
145 const SkTDict<const char*>* fDict; | |
146 int fForceAlpha; | 122 int fForceAlpha; |
147 bool fForceAA; | 123 bool fForceAA; |
148 bool fForceFilter; | 124 bool fForceFilter; |
149 SkTriState::State fDither; | 125 SkTriState::State fDither; |
150 bool fHasStrokeWidth; | |
151 SkScalar strokeWidth; | |
152 uint32_t fOrMask, fClearMask; | 126 uint32_t fOrMask, fClearMask; |
| 127 int fLoops; |
153 | 128 |
154 typedef SkRefCnt INHERITED; | 129 typedef SkRefCnt INHERITED; |
155 }; | 130 }; |
156 | 131 |
157 typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry; | 132 typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry; |
158 | 133 |
159 #endif | 134 #endif |
OLD | NEW |