blob: f1e317da60c71bb5286301e99d0b85827ed79548 [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"
scroggo@google.com111fd112013-09-25 21:42:12 +000013#include "SkString.h"
reed@android.comf523e252009-01-26 23:15:37 +000014#include "SkTRegistry.h"
reed@android.combd700c32009-01-05 03:34:50 +000015
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000016#define DEF_BENCH(code) \
17namespace { \
18class SK_MACRO_APPEND_LINE(F_CLASS) : public SkBenchmarkFactory { \
19 virtual SkBenchmark* operator()() const SK_OVERRIDE { code; } \
20} SK_MACRO_APPEND_LINE(g_F_); \
21BenchRegistry SK_MACRO_APPEND_LINE(g_R_)(&SK_MACRO_APPEND_LINE(g_F_)); \
22}
reed@google.comb8b92ea2012-10-16 15:57:13 +000023
24/*
25 * With the above macros, you can register benches as follows (at the bottom
26 * of your .cpp)
27 *
mtklein@google.com410e6e82013-09-13 19:52:27 +000028 * DEF_BENCH(return new MyBenchmark(...))
29 * DEF_BENCH(return new MyBenchmark(...))
30 * DEF_BENCH(return new MyBenchmark(...))
reed@google.comb8b92ea2012-10-16 15:57:13 +000031 */
32
33
reed@android.combd700c32009-01-05 03:34:50 +000034class SkCanvas;
reed@android.com4bc19832009-01-19 20:08:35 +000035class SkPaint;
reed@android.combd700c32009-01-05 03:34:50 +000036
reed@android.com4e635f92009-10-19 17:39:46 +000037class SkTriState {
38public:
39 enum State {
40 kDefault,
41 kTrue,
42 kFalse
43 };
mtklein@google.comc2897432013-09-10 19:23:38 +000044 static const char* Name[];
reed@android.com4e635f92009-10-19 17:39:46 +000045};
46
reed@android.combd700c32009-01-05 03:34:50 +000047class SkBenchmark : public SkRefCnt {
48public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000049 SK_DECLARE_INST_COUNT(SkBenchmark)
50
mtklein@google.com410e6e82013-09-13 19:52:27 +000051 SkBenchmark();
reed@android.com4bc19832009-01-19 20:08:35 +000052
reed@android.combd700c32009-01-05 03:34:50 +000053 const char* getName();
54 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000055
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000056 enum Backend {
57 kNonRendering_Backend,
58 kRaster_Backend,
59 kGPU_Backend,
60 kPDF_Backend,
61 };
62
63 // Call to determine whether the benchmark is intended for
64 // the rendering mode.
65 virtual bool isSuitableFor(Backend backend) {
66 return backend != kNonRendering_Backend;
67 }
68
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000069 // Call before draw, allows the benchmark to do setup work outside of the
70 // timer. When a benchmark is repeatedly drawn, this should be called once
71 // before the initial draw.
72 void preDraw();
73
commit-bot@chromium.org33614712013-12-03 18:17:16 +000074 // Bench framework can tune loops to be large enough for stable timing.
75 void draw(const int loops, SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000076
77 // Call after draw, allows the benchmark to do cleanup work outside of the
78 // timer. When a benchmark is repeatedly drawn, this is only called once
79 // after the last draw.
80 void postDraw();
81
reed@android.com4bc19832009-01-19 20:08:35 +000082 void setForceAlpha(int alpha) {
83 fForceAlpha = alpha;
84 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085
reed@android.com4bc19832009-01-19 20:08:35 +000086 void setForceAA(bool aa) {
87 fForceAA = aa;
88 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000089
reed@android.com29348cb2009-08-04 18:17:15 +000090 void setForceFilter(bool filter) {
91 fForceFilter = filter;
92 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093
reed@android.com4e635f92009-10-19 17:39:46 +000094 void setDither(SkTriState::State state) {
95 fDither = state;
96 }
reed@android.come9d00602009-09-02 21:12:42 +000097
reed@google.comef77ec22013-05-29 15:39:54 +000098 /** Assign masks for paint-flags. These will be applied when setupPaint()
99 * is called.
100 *
101 * Performs the following on the paint:
102 * uint32_t flags = paint.getFlags();
103 * flags &= ~clearMask;
104 * flags |= orMask;
105 * paint.setFlags(flags);
106 */
107 void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
108 fOrMask = orMask;
109 fClearMask = clearMask;
110 }
111
scroggo@google.com111fd112013-09-25 21:42:12 +0000112 static void SetResourcePath(const char* resPath) { gResourcePath.set(resPath); }
113
114 static SkString& GetResourcePath() { return gResourcePath; }
115
reed@android.combd700c32009-01-05 03:34:50 +0000116protected:
reed@google.com0561a3c2012-11-15 19:52:20 +0000117 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +0000118
reed@android.combd700c32009-01-05 03:34:50 +0000119 virtual const char* onGetName() = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000120 virtual void onPreDraw() {}
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000121 // Each bench should do its main work in a loop like this:
122 // for (int i = 0; i < loops; i++) { <work here> }
123 virtual void onDraw(const int loops, SkCanvas*) = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000124 virtual void onPostDraw() {}
reed@android.comf523e252009-01-26 23:15:37 +0000125
126 virtual SkIPoint onGetSize();
127
reed@android.com4bc19832009-01-19 20:08:35 +0000128private:
129 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;
reed@google.comef77ec22013-05-29 15:39:54 +0000133 uint32_t fOrMask, fClearMask;
scroggo@google.com111fd112013-09-25 21:42:12 +0000134 static SkString gResourcePath;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000135
136 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000137};
138
commit-bot@chromium.org6adce672014-02-03 14:48:17 +0000139class SkBenchmarkFactory : public SkRefCnt {
140public:
141 // Creates a new SkBenchmark that is owned by the caller on each call.
142 virtual SkBenchmark* operator()() const = 0;
143 virtual ~SkBenchmarkFactory() {}
144};
145
146typedef SkTRegistry<SkBenchmarkFactory*> BenchRegistry;
reed@android.comf523e252009-01-26 23:15:37 +0000147
reed@android.combd700c32009-01-05 03:34:50 +0000148#endif