blob: 77e2357dd63a33915b828722459ac94d3f8b106b [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
reed@google.comb8b92ea2012-10-16 15:57:13 +000016#define DEF_BENCH(code) \
mtklein@google.com410e6e82013-09-13 19:52:27 +000017static SkBenchmark* SK_MACRO_APPEND_LINE(F_)() { code; } \
reed@google.comb8b92ea2012-10-16 15:57:13 +000018static 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 *
mtklein@google.com410e6e82013-09-13 19:52:27 +000024 * DEF_BENCH(return new MyBenchmark(...))
25 * DEF_BENCH(return new MyBenchmark(...))
26 * DEF_BENCH(return new MyBenchmark(...))
reed@google.comb8b92ea2012-10-16 15:57:13 +000027 */
28
29
reed@android.combd700c32009-01-05 03:34:50 +000030class SkCanvas;
reed@android.com4bc19832009-01-19 20:08:35 +000031class SkPaint;
reed@android.combd700c32009-01-05 03:34:50 +000032
reed@android.com4e635f92009-10-19 17:39:46 +000033class SkTriState {
34public:
35 enum State {
36 kDefault,
37 kTrue,
38 kFalse
39 };
mtklein@google.comc2897432013-09-10 19:23:38 +000040 static const char* Name[];
reed@android.com4e635f92009-10-19 17:39:46 +000041};
42
reed@android.combd700c32009-01-05 03:34:50 +000043class SkBenchmark : public SkRefCnt {
44public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000045 SK_DECLARE_INST_COUNT(SkBenchmark)
46
mtklein@google.com410e6e82013-09-13 19:52:27 +000047 SkBenchmark();
reed@android.com4bc19832009-01-19 20:08:35 +000048
reed@android.combd700c32009-01-05 03:34:50 +000049 const char* getName();
50 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000051
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000052 enum Backend {
53 kNonRendering_Backend,
54 kRaster_Backend,
55 kGPU_Backend,
56 kPDF_Backend,
57 };
58
59 // Call to determine whether the benchmark is intended for
60 // the rendering mode.
61 virtual bool isSuitableFor(Backend backend) {
62 return backend != kNonRendering_Backend;
63 }
64
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000065 // Call before draw, allows the benchmark to do setup work outside of the
66 // timer. When a benchmark is repeatedly drawn, this should be called once
67 // before the initial draw.
68 void preDraw();
69
reed@android.combd700c32009-01-05 03:34:50 +000070 void draw(SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000071
72 // Call after draw, allows the benchmark to do cleanup work outside of the
73 // timer. When a benchmark is repeatedly drawn, this is only called once
74 // after the last draw.
75 void postDraw();
76
reed@android.com4bc19832009-01-19 20:08:35 +000077 void setForceAlpha(int alpha) {
78 fForceAlpha = alpha;
79 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
reed@android.com4bc19832009-01-19 20:08:35 +000081 void setForceAA(bool aa) {
82 fForceAA = aa;
83 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000084
reed@android.com29348cb2009-08-04 18:17:15 +000085 void setForceFilter(bool filter) {
86 fForceFilter = filter;
87 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
reed@android.com4e635f92009-10-19 17:39:46 +000089 void setDither(SkTriState::State state) {
90 fDither = state;
91 }
reed@android.come9d00602009-09-02 21:12:42 +000092
reed@google.comef77ec22013-05-29 15:39:54 +000093 /** Assign masks for paint-flags. These will be applied when setupPaint()
94 * is called.
95 *
96 * Performs the following on the paint:
97 * uint32_t flags = paint.getFlags();
98 * flags &= ~clearMask;
99 * flags |= orMask;
100 * paint.setFlags(flags);
101 */
102 void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
103 fOrMask = orMask;
104 fClearMask = clearMask;
105 }
106
mtklein@google.comc2897432013-09-10 19:23:38 +0000107 // The bench framework calls this to control the runtime of a bench.
108 void setLoops(int loops) {
109 fLoops = loops;
110 }
111
112 // Each bench should do its main work in a loop like this:
113 // for (int i = 0; i < this->getLoops(); i++) { <work here> }
114 int getLoops() const { return fLoops; }
reed@google.comef77ec22013-05-29 15:39:54 +0000115
scroggo@google.com111fd112013-09-25 21:42:12 +0000116 static void SetResourcePath(const char* resPath) { gResourcePath.set(resPath); }
117
118 static SkString& GetResourcePath() { return gResourcePath; }
119
reed@android.combd700c32009-01-05 03:34:50 +0000120protected:
reed@google.com0561a3c2012-11-15 19:52:20 +0000121 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +0000122
reed@android.combd700c32009-01-05 03:34:50 +0000123 virtual const char* onGetName() = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000124 virtual void onPreDraw() {}
reed@android.combd700c32009-01-05 03:34:50 +0000125 virtual void onDraw(SkCanvas*) = 0;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000126 virtual void onPostDraw() {}
reed@android.comf523e252009-01-26 23:15:37 +0000127
128 virtual SkIPoint onGetSize();
129
reed@android.com4bc19832009-01-19 20:08:35 +0000130private:
131 int fForceAlpha;
132 bool fForceAA;
reed@android.com29348cb2009-08-04 18:17:15 +0000133 bool fForceFilter;
reed@android.com4e635f92009-10-19 17:39:46 +0000134 SkTriState::State fDither;
reed@google.comef77ec22013-05-29 15:39:54 +0000135 uint32_t fOrMask, fClearMask;
mtklein@google.comc2897432013-09-10 19:23:38 +0000136 int fLoops;
scroggo@google.com111fd112013-09-25 21:42:12 +0000137 static SkString gResourcePath;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000138
139 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000140};
141
mtklein@google.com410e6e82013-09-13 19:52:27 +0000142typedef SkTRegistry<SkBenchmark*(*)()> BenchRegistry;
reed@android.comf523e252009-01-26 23:15:37 +0000143
reed@android.combd700c32009-01-05 03:34:50 +0000144#endif