blob: 811a1db2c8366316dcf8f4872fbb6922faa72303 [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.comf523e252009-01-26 23:15:37 +000013#include "SkTRegistry.h"
reed@android.combd700c32009-01-05 03:34:50 +000014
reed@google.comb8b92ea2012-10-16 15:57:13 +000015#define DEF_BENCH(code) \
mtklein@google.com410e6e82013-09-13 19:52:27 +000016static SkBenchmark* SK_MACRO_APPEND_LINE(F_)() { code; } \
reed@google.comb8b92ea2012-10-16 15:57:13 +000017static BenchRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
18
19/*
20 * With the above macros, you can register benches as follows (at the bottom
21 * of your .cpp)
22 *
mtklein@google.com410e6e82013-09-13 19:52:27 +000023 * DEF_BENCH(return new MyBenchmark(...))
24 * DEF_BENCH(return new MyBenchmark(...))
25 * DEF_BENCH(return new MyBenchmark(...))
reed@google.comb8b92ea2012-10-16 15:57:13 +000026 */
27
28
reed@android.combd700c32009-01-05 03:34:50 +000029class SkCanvas;
reed@android.com4bc19832009-01-19 20:08:35 +000030class SkPaint;
reed@android.combd700c32009-01-05 03:34:50 +000031
reed@android.com4e635f92009-10-19 17:39:46 +000032class SkTriState {
33public:
34 enum State {
35 kDefault,
36 kTrue,
37 kFalse
38 };
mtklein@google.comc2897432013-09-10 19:23:38 +000039 static const char* Name[];
reed@android.com4e635f92009-10-19 17:39:46 +000040};
41
reed@android.combd700c32009-01-05 03:34:50 +000042class SkBenchmark : public SkRefCnt {
43public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000044 SK_DECLARE_INST_COUNT(SkBenchmark)
45
mtklein@google.com410e6e82013-09-13 19:52:27 +000046 SkBenchmark();
reed@android.com4bc19832009-01-19 20:08:35 +000047
reed@android.combd700c32009-01-05 03:34:50 +000048 const char* getName();
49 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000050
51 // Call before draw, allows the benchmark to do setup work outside of the
52 // timer. When a benchmark is repeatedly drawn, this should be called once
53 // before the initial draw.
54 void preDraw();
55
reed@android.combd700c32009-01-05 03:34:50 +000056 void draw(SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000057
58 // Call after draw, allows the benchmark to do cleanup work outside of the
59 // timer. When a benchmark is repeatedly drawn, this is only called once
60 // after the last draw.
61 void postDraw();
62
reed@android.com4bc19832009-01-19 20:08:35 +000063 void setForceAlpha(int alpha) {
64 fForceAlpha = alpha;
65 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066
reed@android.com4bc19832009-01-19 20:08:35 +000067 void setForceAA(bool aa) {
68 fForceAA = aa;
69 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070
reed@android.com29348cb2009-08-04 18:17:15 +000071 void setForceFilter(bool filter) {
72 fForceFilter = filter;
73 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000074
reed@android.com4e635f92009-10-19 17:39:46 +000075 void setDither(SkTriState::State state) {
76 fDither = state;
77 }
reed@android.come9d00602009-09-02 21:12:42 +000078
tomhudson@google.com9dc27132012-09-13 15:50:24 +000079 /** If true; the benchmark does rendering; if false, the benchmark
80 doesn't, and so need not be re-run in every different rendering
81 mode. */
82 bool isRendering() {
83 return fIsRendering;
84 }
85
reed@google.comef77ec22013-05-29 15:39:54 +000086 /** Assign masks for paint-flags. These will be applied when setupPaint()
87 * is called.
88 *
89 * Performs the following on the paint:
90 * uint32_t flags = paint.getFlags();
91 * flags &= ~clearMask;
92 * flags |= orMask;
93 * paint.setFlags(flags);
94 */
95 void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
96 fOrMask = orMask;
97 fClearMask = clearMask;
98 }
99
mtklein@google.comc2897432013-09-10 19:23:38 +0000100 // 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; }
reed@google.comef77ec22013-05-29 15:39:54 +0000108
reed@android.combd700c32009-01-05 03:34:50 +0000109protected:
reed@google.com0561a3c2012-11-15 19:52:20 +0000110 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +0000111
reed@android.combd700c32009-01-05 03:34:50 +0000112 virtual const char* onGetName() = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000113 virtual void onPreDraw() {}
reed@android.combd700c32009-01-05 03:34:50 +0000114 virtual void onDraw(SkCanvas*) = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000115 virtual void onPostDraw() {}
reed@android.comf523e252009-01-26 23:15:37 +0000116
117 virtual SkIPoint onGetSize();
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000118 /// Defaults to true.
119 bool fIsRendering;
reed@android.comf523e252009-01-26 23:15:37 +0000120
reed@android.com4bc19832009-01-19 20:08:35 +0000121private:
122 int fForceAlpha;
123 bool fForceAA;
reed@android.com29348cb2009-08-04 18:17:15 +0000124 bool fForceFilter;
reed@android.com4e635f92009-10-19 17:39:46 +0000125 SkTriState::State fDither;
reed@google.comef77ec22013-05-29 15:39:54 +0000126 uint32_t fOrMask, fClearMask;
mtklein@google.comc2897432013-09-10 19:23:38 +0000127 int fLoops;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000128
129 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000130};
131
mtklein@google.com410e6e82013-09-13 19:52:27 +0000132typedef SkTRegistry<SkBenchmark*(*)()> BenchRegistry;
reed@android.comf523e252009-01-26 23:15:37 +0000133
reed@android.combd700c32009-01-05 03:34:50 +0000134#endif