blob: 58fa2c62683969defb25d31058c573345fd833ee [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPoint.h"
12#include "include/core/SkRefCnt.h"
13#include "include/core/SkString.h"
14#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
tfarinaf168b862014-06-19 12:32:29 -070034class Benchmark : public SkRefCnt {
reed@android.combd700c32009-01-05 03:34:50 +000035public:
tfarinaf168b862014-06-19 12:32:29 -070036 Benchmark();
reed@android.com4bc19832009-01-19 20:08:35 +000037
reed@android.combd700c32009-01-05 03:34:50 +000038 const char* getName();
mtklein96289052014-09-10 12:05:59 -070039 const char* getUniqueName();
reed@android.combd700c32009-01-05 03:34:50 +000040 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000041
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000042 enum Backend {
43 kNonRendering_Backend,
44 kRaster_Backend,
45 kGPU_Backend,
46 kPDF_Backend,
tomhudsond968a6f2015-03-26 11:28:06 -070047 kHWUI_Backend,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000048 };
49
50 // Call to determine whether the benchmark is intended for
51 // the rendering mode.
52 virtual bool isSuitableFor(Backend backend) {
53 return backend != kNonRendering_Backend;
54 }
55
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040056 // Allows a benchmark to override options used to construct the GrContext.
57 virtual void modifyGrContextOptions(GrContextOptions*) {}
58
cdaltonb4022962015-06-25 10:51:56 -070059 virtual int calculateLoops(int defaultLoops) const {
60 return defaultLoops;
61 }
62
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000063 // 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.
joshualitt8a6697a2015-09-30 12:11:07 -070066 void delayedSetup();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000067
robertphillips5b693772014-11-21 06:19:36 -080068 // Called once before and after a series of draw calls to a single canvas.
69 // The setup/break down in these calls is not timed.
70 void perCanvasPreDraw(SkCanvas*);
71 void perCanvasPostDraw(SkCanvas*);
72
joshualitt8a6697a2015-09-30 12:11:07 -070073 // Called just before and after each call to draw(). Not timed.
74 void preDraw(SkCanvas*);
75 void postDraw(SkCanvas*);
76
commit-bot@chromium.org33614712013-12-03 18:17:16 +000077 // Bench framework can tune loops to be large enough for stable timing.
mtkleina1ebeb22015-10-01 09:43:39 -070078 void draw(int loops, SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000079
joshualitte45c81c2015-12-02 09:05:37 -080080 virtual void getGpuStats(SkCanvas*, SkTArray<SkString>* keys, SkTArray<double>* values) {}
81
Mike Kleinfec9b902019-05-31 10:03:07 -050082 // Count of units (pixels, whatever) being exercised, to scale timing by.
83 int getUnits() const { return fUnits; }
84
reed@android.combd700c32009-01-05 03:34:50 +000085protected:
Mike Kleinfec9b902019-05-31 10:03:07 -050086 void setUnits(int units) { SkASSERT(units > 0); fUnits = units; }
87
reed@google.com0561a3c2012-11-15 19:52:20 +000088 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +000089
reed@android.combd700c32009-01-05 03:34:50 +000090 virtual const char* onGetName() = 0;
mtklein96289052014-09-10 12:05:59 -070091 virtual const char* onGetUniqueName() { return this->onGetName(); }
joshualitt8a6697a2015-09-30 12:11:07 -070092 virtual void onDelayedSetup() {}
robertphillips5b693772014-11-21 06:19:36 -080093 virtual void onPerCanvasPreDraw(SkCanvas*) {}
94 virtual void onPerCanvasPostDraw(SkCanvas*) {}
joshualitt8a6697a2015-09-30 12:11:07 -070095 virtual void onPreDraw(SkCanvas*) {}
96 virtual void onPostDraw(SkCanvas*) {}
commit-bot@chromium.org33614712013-12-03 18:17:16 +000097 // Each bench should do its main work in a loop like this:
98 // for (int i = 0; i < loops; i++) { <work here> }
mtkleina1ebeb22015-10-01 09:43:39 -070099 virtual void onDraw(int loops, SkCanvas*) = 0;
reed@android.comf523e252009-01-26 23:15:37 +0000100
101 virtual SkIPoint onGetSize();
102
reed@android.com4bc19832009-01-19 20:08:35 +0000103private:
Mike Kleinfec9b902019-05-31 10:03:07 -0500104 int fUnits = 1;
105
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000106 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000107};
108
Mike Reedab273fa2017-01-11 13:58:55 -0500109typedef sk_tools::Registry<Benchmark*(*)(void*)> BenchRegistry;
reed@android.comf523e252009-01-26 23:15:37 +0000110
reed@android.combd700c32009-01-05 03:34:50 +0000111#endif