blob: ddc93870cd20295d7b195955679f4bdfb4ffef07 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.combd700c32009-01-05 03:34:50 +00007
tfarinaf168b862014-06-19 12:32:29 -07008#ifndef Benchmark_DEFINED
9#define Benchmark_DEFINED
10
reed@android.combd700c32009-01-05 03:34:50 +000011#include "SkPoint.h"
tfarinaf168b862014-06-19 12:32:29 -070012#include "SkRefCnt.h"
scroggo@google.com111fd112013-09-25 21:42:12 +000013#include "SkString.h"
Mike Reedab273fa2017-01-11 13:58:55 -050014#include "../tools/Registry.h"
reed@android.combd700c32009-01-05 03:34:50 +000015
mtkleind0a10882015-05-13 11:54:00 -070016#define DEF_BENCH3(code, N) \
17 static BenchRegistry gBench##N([](void*) -> Benchmark* { code; });
18#define DEF_BENCH2(code, N) DEF_BENCH3(code, N)
19#define DEF_BENCH(code) DEF_BENCH2(code, __COUNTER__)
reed@google.comb8b92ea2012-10-16 15:57:13 +000020
21/*
22 * With the above macros, you can register benches as follows (at the bottom
23 * of your .cpp)
24 *
mtklein@google.com410e6e82013-09-13 19:52:27 +000025 * DEF_BENCH(return new MyBenchmark(...))
26 * DEF_BENCH(return new MyBenchmark(...))
27 * DEF_BENCH(return new MyBenchmark(...))
reed@google.comb8b92ea2012-10-16 15:57:13 +000028 */
29
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040030struct GrContextOptions;
reed@android.combd700c32009-01-05 03:34:50 +000031class SkCanvas;
reed@android.com4bc19832009-01-19 20:08:35 +000032class SkPaint;
reed@android.combd700c32009-01-05 03:34:50 +000033
reed@android.com4e635f92009-10-19 17:39:46 +000034class SkTriState {
35public:
36 enum State {
37 kDefault,
38 kTrue,
39 kFalse
40 };
mtklein@google.comc2897432013-09-10 19:23:38 +000041 static const char* Name[];
reed@android.com4e635f92009-10-19 17:39:46 +000042};
43
tfarinaf168b862014-06-19 12:32:29 -070044class Benchmark : public SkRefCnt {
reed@android.combd700c32009-01-05 03:34:50 +000045public:
tfarinaf168b862014-06-19 12:32:29 -070046 Benchmark();
reed@android.com4bc19832009-01-19 20:08:35 +000047
reed@android.combd700c32009-01-05 03:34:50 +000048 const char* getName();
mtklein96289052014-09-10 12:05:59 -070049 const char* getUniqueName();
reed@android.combd700c32009-01-05 03:34:50 +000050 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,
tomhudsond968a6f2015-03-26 11:28:06 -070057 kHWUI_Backend,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000058 };
59
60 // Call to determine whether the benchmark is intended for
61 // the rendering mode.
62 virtual bool isSuitableFor(Backend backend) {
63 return backend != kNonRendering_Backend;
64 }
65
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040066 // Allows a benchmark to override options used to construct the GrContext.
67 virtual void modifyGrContextOptions(GrContextOptions*) {}
68
cdaltonb4022962015-06-25 10:51:56 -070069 virtual int calculateLoops(int defaultLoops) const {
70 return defaultLoops;
71 }
72
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000073 // Call before draw, allows the benchmark to do setup work outside of the
74 // timer. When a benchmark is repeatedly drawn, this should be called once
75 // before the initial draw.
joshualitt8a6697a2015-09-30 12:11:07 -070076 void delayedSetup();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000077
robertphillips5b693772014-11-21 06:19:36 -080078 // Called once before and after a series of draw calls to a single canvas.
79 // The setup/break down in these calls is not timed.
80 void perCanvasPreDraw(SkCanvas*);
81 void perCanvasPostDraw(SkCanvas*);
82
joshualitt8a6697a2015-09-30 12:11:07 -070083 // Called just before and after each call to draw(). Not timed.
84 void preDraw(SkCanvas*);
85 void postDraw(SkCanvas*);
86
commit-bot@chromium.org33614712013-12-03 18:17:16 +000087 // Bench framework can tune loops to be large enough for stable timing.
mtkleina1ebeb22015-10-01 09:43:39 -070088 void draw(int loops, SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000089
reed@android.com4bc19832009-01-19 20:08:35 +000090 void setForceAlpha(int alpha) {
91 fForceAlpha = alpha;
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
joshualitt962cc982015-06-30 07:43:14 -0700112 /*
113 * Benches which support running in a visual mode can advertise this functionality
114 */
115 virtual bool isVisual() { return false; }
116
joshualittcb54e8e2015-10-05 13:58:26 -0700117 /*
118 * VisualBench frequently resets the canvas. As a result we need to bulk call all of the hooks
119 */
120 void preTimingHooks(SkCanvas* canvas) {
121 this->perCanvasPreDraw(canvas);
122 this->preDraw(canvas);
123 }
124
125 void postTimingHooks(SkCanvas* canvas) {
126 this->postDraw(canvas);
127 this->perCanvasPostDraw(canvas);
128 }
129
joshualitte45c81c2015-12-02 09:05:37 -0800130 virtual void getGpuStats(SkCanvas*, SkTArray<SkString>* keys, SkTArray<double>* values) {}
131
reed@android.combd700c32009-01-05 03:34:50 +0000132protected:
reed@google.com0561a3c2012-11-15 19:52:20 +0000133 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +0000134
reed@android.combd700c32009-01-05 03:34:50 +0000135 virtual const char* onGetName() = 0;
mtklein96289052014-09-10 12:05:59 -0700136 virtual const char* onGetUniqueName() { return this->onGetName(); }
joshualitt8a6697a2015-09-30 12:11:07 -0700137 virtual void onDelayedSetup() {}
robertphillips5b693772014-11-21 06:19:36 -0800138 virtual void onPerCanvasPreDraw(SkCanvas*) {}
139 virtual void onPerCanvasPostDraw(SkCanvas*) {}
joshualitt8a6697a2015-09-30 12:11:07 -0700140 virtual void onPreDraw(SkCanvas*) {}
141 virtual void onPostDraw(SkCanvas*) {}
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000142 // Each bench should do its main work in a loop like this:
143 // for (int i = 0; i < loops; i++) { <work here> }
mtkleina1ebeb22015-10-01 09:43:39 -0700144 virtual void onDraw(int loops, SkCanvas*) = 0;
reed@android.comf523e252009-01-26 23:15:37 +0000145
146 virtual SkIPoint onGetSize();
147
reed@android.com4bc19832009-01-19 20:08:35 +0000148private:
149 int fForceAlpha;
reed@android.com4e635f92009-10-19 17:39:46 +0000150 SkTriState::State fDither;
reed@google.comef77ec22013-05-29 15:39:54 +0000151 uint32_t fOrMask, fClearMask;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000152
153 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000154};
155
Mike Reedab273fa2017-01-11 13:58:55 -0500156typedef sk_tools::Registry<Benchmark*(*)(void*)> BenchRegistry;
reed@android.comf523e252009-01-26 23:15:37 +0000157
reed@android.combd700c32009-01-05 03:34:50 +0000158#endif