blob: 404d4c3e7e6ebd5e65a0c4119f624b4653dade5e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.combd700c32009-01-05 03:34:50 +00008#ifndef SkBenchmark_DEFINED
9#define SkBenchmark_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkPoint.h"
reed@android.come9d00602009-09-02 21:12:42 +000013#include "SkTDict.h"
reed@android.comf523e252009-01-26 23:15:37 +000014#include "SkTRegistry.h"
reed@android.combd700c32009-01-05 03:34:50 +000015
reed@google.comb8b92ea2012-10-16 15:57:13 +000016#define DEF_BENCH(code) \
17static SkBenchmark* SK_MACRO_APPEND_LINE(F_)(void* p) { code; } \
18static 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.comca529d32011-10-28 15:34:49 +000030#ifdef SK_DEBUG
31 #define SkBENCHLOOP(n) 1
32#else
33 #define SkBENCHLOOP(n) n
34#endif
35
reed@android.combd700c32009-01-05 03:34:50 +000036class SkCanvas;
reed@android.com4bc19832009-01-19 20:08:35 +000037class SkPaint;
reed@android.combd700c32009-01-05 03:34:50 +000038
reed@android.com4e635f92009-10-19 17:39:46 +000039class SkTriState {
40public:
41 enum State {
42 kDefault,
43 kTrue,
44 kFalse
45 };
46};
47
reed@android.combd700c32009-01-05 03:34:50 +000048class SkBenchmark : public SkRefCnt {
49public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000050 SK_DECLARE_INST_COUNT(SkBenchmark)
51
reed@android.come9d00602009-09-02 21:12:42 +000052 SkBenchmark(void* defineDict);
reed@android.com4bc19832009-01-19 20:08:35 +000053
reed@android.combd700c32009-01-05 03:34:50 +000054 const char* getName();
55 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000056
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.combd700c32009-01-05 03:34:50 +000062 void draw(SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000063
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.com4bc19832009-01-19 20:08:35 +000069 void setForceAlpha(int alpha) {
70 fForceAlpha = alpha;
71 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
reed@android.com4bc19832009-01-19 20:08:35 +000073 void setForceAA(bool aa) {
74 fForceAA = aa;
75 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000076
reed@android.com29348cb2009-08-04 18:17:15 +000077 void setForceFilter(bool filter) {
78 fForceFilter = filter;
79 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
reed@android.com4e635f92009-10-19 17:39:46 +000081 void setDither(SkTriState::State state) {
82 fDither = state;
83 }
reed@android.come9d00602009-09-02 21:12:42 +000084
agl@chromium.org652807b2010-04-27 15:47:34 +000085 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.com9dc27132012-09-13 15:50:24 +000098 /** 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.come9d00602009-09-02 21:12:42 +0000105 const char* findDefine(const char* key) const;
reed@android.com0c9da392010-02-22 19:50:13 +0000106 bool findDefine32(const char* key, int32_t* value) const;
107 bool findDefineScalar(const char* key, SkScalar* value) const;
reed@android.come9d00602009-09-02 21:12:42 +0000108
reed@google.comef77ec22013-05-29 15:39:54 +0000109 /** Assign masks for paint-flags. These will be applied when setupPaint()
110 * is called.
111 *
112 * Performs the following on the paint:
113 * uint32_t flags = paint.getFlags();
114 * flags &= ~clearMask;
115 * flags |= orMask;
116 * paint.setFlags(flags);
117 */
118 void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
119 fOrMask = orMask;
120 fClearMask = clearMask;
121 }
122
123 float getDurationScale() { return this->onGetDurationScale(); }
124
reed@android.combd700c32009-01-05 03:34:50 +0000125protected:
reed@google.com0561a3c2012-11-15 19:52:20 +0000126 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +0000127
reed@android.combd700c32009-01-05 03:34:50 +0000128 virtual const char* onGetName() = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000129 virtual void onPreDraw() {}
reed@android.combd700c32009-01-05 03:34:50 +0000130 virtual void onDraw(SkCanvas*) = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000131 virtual void onPostDraw() {}
reed@google.comef77ec22013-05-29 15:39:54 +0000132 // 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; }
reed@android.comf523e252009-01-26 23:15:37 +0000139
140 virtual SkIPoint onGetSize();
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000141 /// Defaults to true.
142 bool fIsRendering;
reed@android.comf523e252009-01-26 23:15:37 +0000143
reed@android.com4bc19832009-01-19 20:08:35 +0000144private:
reed@android.come9d00602009-09-02 21:12:42 +0000145 const SkTDict<const char*>* fDict;
reed@android.com4bc19832009-01-19 20:08:35 +0000146 int fForceAlpha;
147 bool fForceAA;
reed@android.com29348cb2009-08-04 18:17:15 +0000148 bool fForceFilter;
reed@android.com4e635f92009-10-19 17:39:46 +0000149 SkTriState::State fDither;
agl@chromium.org652807b2010-04-27 15:47:34 +0000150 bool fHasStrokeWidth;
151 SkScalar strokeWidth;
reed@google.comef77ec22013-05-29 15:39:54 +0000152 uint32_t fOrMask, fClearMask;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000153
154 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000155};
156
reed@android.comf523e252009-01-26 23:15:37 +0000157typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry;
158
reed@android.combd700c32009-01-05 03:34:50 +0000159#endif