blob: b72162f44c03c166c6395c0a3153e6aca148e2df [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
17#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL(X, Y)
18#define SK_MACRO_CONCAT_IMPL(X, Y) X ## Y
19
20#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
21
22#define DEF_BENCH(code) \
23static SkBenchmark* SK_MACRO_APPEND_LINE(F_)(void* p) { code; } \
24static BenchRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
25
26/*
27 * With the above macros, you can register benches as follows (at the bottom
28 * of your .cpp)
29 *
30 * DEF_BENCH(new MyBenchmark(p, ...))
31 * DEF_BENCH(new MyBenchmark(p, ...))
32 * DEF_BENCH(new MyBenchmark(p, ...))
33 */
34
35
tomhudson@google.comca529d32011-10-28 15:34:49 +000036#ifdef SK_DEBUG
37 #define SkBENCHLOOP(n) 1
38#else
39 #define SkBENCHLOOP(n) n
40#endif
41
reed@android.combd700c32009-01-05 03:34:50 +000042class SkCanvas;
reed@android.com4bc19832009-01-19 20:08:35 +000043class SkPaint;
reed@android.combd700c32009-01-05 03:34:50 +000044
reed@android.com4e635f92009-10-19 17:39:46 +000045class SkTriState {
46public:
47 enum State {
48 kDefault,
49 kTrue,
50 kFalse
51 };
52};
53
reed@android.combd700c32009-01-05 03:34:50 +000054class SkBenchmark : public SkRefCnt {
55public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000056 SK_DECLARE_INST_COUNT(SkBenchmark)
57
reed@android.come9d00602009-09-02 21:12:42 +000058 SkBenchmark(void* defineDict);
reed@android.com4bc19832009-01-19 20:08:35 +000059
reed@android.combd700c32009-01-05 03:34:50 +000060 const char* getName();
61 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000062
63 // Call before draw, allows the benchmark to do setup work outside of the
64 // timer. When a benchmark is repeatedly drawn, this should be called once
65 // before the initial draw.
66 void preDraw();
67
reed@android.combd700c32009-01-05 03:34:50 +000068 void draw(SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000069
70 // Call after draw, allows the benchmark to do cleanup work outside of the
71 // timer. When a benchmark is repeatedly drawn, this is only called once
72 // after the last draw.
73 void postDraw();
74
reed@android.com4bc19832009-01-19 20:08:35 +000075 void setForceAlpha(int alpha) {
76 fForceAlpha = alpha;
77 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000078
reed@android.com4bc19832009-01-19 20:08:35 +000079 void setForceAA(bool aa) {
80 fForceAA = aa;
81 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
reed@android.com29348cb2009-08-04 18:17:15 +000083 void setForceFilter(bool filter) {
84 fForceFilter = filter;
85 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
reed@android.com4e635f92009-10-19 17:39:46 +000087 void setDither(SkTriState::State state) {
88 fDither = state;
89 }
reed@android.come9d00602009-09-02 21:12:42 +000090
agl@chromium.org652807b2010-04-27 15:47:34 +000091 void setStrokeWidth(SkScalar width) {
92 strokeWidth = width;
93 fHasStrokeWidth = true;
94 }
95
96 SkScalar getStrokeWidth() {
97 return strokeWidth;
98 }
99
100 bool hasStrokeWidth() {
101 return fHasStrokeWidth;
102 }
103
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000104 /** If true; the benchmark does rendering; if false, the benchmark
105 doesn't, and so need not be re-run in every different rendering
106 mode. */
107 bool isRendering() {
108 return fIsRendering;
109 }
110
reed@android.come9d00602009-09-02 21:12:42 +0000111 const char* findDefine(const char* key) const;
reed@android.com0c9da392010-02-22 19:50:13 +0000112 bool findDefine32(const char* key, int32_t* value) const;
113 bool findDefineScalar(const char* key, SkScalar* value) const;
reed@android.come9d00602009-09-02 21:12:42 +0000114
reed@android.combd700c32009-01-05 03:34:50 +0000115protected:
reed@android.com4bc19832009-01-19 20:08:35 +0000116 void setupPaint(SkPaint* paint);
117
reed@android.combd700c32009-01-05 03:34:50 +0000118 virtual const char* onGetName() = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000119 virtual void onPreDraw() {}
reed@android.combd700c32009-01-05 03:34:50 +0000120 virtual void onDraw(SkCanvas*) = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000121 virtual void onPostDraw() {}
reed@android.comf523e252009-01-26 23:15:37 +0000122
123 virtual SkIPoint onGetSize();
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000124 /// Defaults to true.
125 bool fIsRendering;
reed@android.comf523e252009-01-26 23:15:37 +0000126
reed@android.com4bc19832009-01-19 20:08:35 +0000127private:
reed@android.come9d00602009-09-02 21:12:42 +0000128 const SkTDict<const char*>* fDict;
reed@android.com4bc19832009-01-19 20:08:35 +0000129 int fForceAlpha;
130 bool fForceAA;
reed@android.com29348cb2009-08-04 18:17:15 +0000131 bool fForceFilter;
reed@android.com4e635f92009-10-19 17:39:46 +0000132 SkTriState::State fDither;
agl@chromium.org652807b2010-04-27 15:47:34 +0000133 bool fHasStrokeWidth;
134 SkScalar strokeWidth;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000135
136 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000137};
138
reed@android.comf523e252009-01-26 23:15:37 +0000139typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry;
140
reed@android.combd700c32009-01-05 03:34:50 +0000141#endif