blob: 6a5f4fdece090ff4b4446ccdc05176a5fdc0ff93 [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;
Chris Dalton5dfb3f42021-04-30 17:16:12 -060031class GrRecordingContext;
reed@android.combd700c32009-01-05 03:34:50 +000032class SkCanvas;
reed@android.com4bc19832009-01-19 20:08:35 +000033class SkPaint;
reed@android.combd700c32009-01-05 03:34:50 +000034
tfarinaf168b862014-06-19 12:32:29 -070035class Benchmark : public SkRefCnt {
reed@android.combd700c32009-01-05 03:34:50 +000036public:
tfarinaf168b862014-06-19 12:32:29 -070037 Benchmark();
reed@android.com4bc19832009-01-19 20:08:35 +000038
reed@android.combd700c32009-01-05 03:34:50 +000039 const char* getName();
mtklein96289052014-09-10 12:05:59 -070040 const char* getUniqueName();
reed@android.combd700c32009-01-05 03:34:50 +000041 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000042
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000043 enum Backend {
44 kNonRendering_Backend,
45 kRaster_Backend,
46 kGPU_Backend,
47 kPDF_Backend,
tomhudsond968a6f2015-03-26 11:28:06 -070048 kHWUI_Backend,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000049 };
50
51 // Call to determine whether the benchmark is intended for
52 // the rendering mode.
53 virtual bool isSuitableFor(Backend backend) {
54 return backend != kNonRendering_Backend;
55 }
56
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040057 // Allows a benchmark to override options used to construct the GrContext.
58 virtual void modifyGrContextOptions(GrContextOptions*) {}
59
cdaltonb4022962015-06-25 10:51:56 -070060 virtual int calculateLoops(int defaultLoops) const {
61 return defaultLoops;
62 }
63
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000064 // Call before draw, allows the benchmark to do setup work outside of the
65 // timer. When a benchmark is repeatedly drawn, this should be called once
66 // before the initial draw.
joshualitt8a6697a2015-09-30 12:11:07 -070067 void delayedSetup();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000068
robertphillips5b693772014-11-21 06:19:36 -080069 // Called once before and after a series of draw calls to a single canvas.
70 // The setup/break down in these calls is not timed.
71 void perCanvasPreDraw(SkCanvas*);
72 void perCanvasPostDraw(SkCanvas*);
73
joshualitt8a6697a2015-09-30 12:11:07 -070074 // Called just before and after each call to draw(). Not timed.
75 void preDraw(SkCanvas*);
76 void postDraw(SkCanvas*);
77
commit-bot@chromium.org33614712013-12-03 18:17:16 +000078 // Bench framework can tune loops to be large enough for stable timing.
mtkleina1ebeb22015-10-01 09:43:39 -070079 void draw(int loops, SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000080
joshualitte45c81c2015-12-02 09:05:37 -080081 virtual void getGpuStats(SkCanvas*, SkTArray<SkString>* keys, SkTArray<double>* values) {}
82
Chris Dalton5dfb3f42021-04-30 17:16:12 -060083 // Replaces the GrRecordingContext's dmsaaStats() with a single frame of this benchmark.
84 virtual bool getDMSAAStats(GrRecordingContext*) { return false; }
85
Mike Kleinfec9b902019-05-31 10:03:07 -050086 // Count of units (pixels, whatever) being exercised, to scale timing by.
87 int getUnits() const { return fUnits; }
88
reed@android.combd700c32009-01-05 03:34:50 +000089protected:
Mike Kleinfec9b902019-05-31 10:03:07 -050090 void setUnits(int units) { SkASSERT(units > 0); fUnits = units; }
91
reed@google.com0561a3c2012-11-15 19:52:20 +000092 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +000093
reed@android.combd700c32009-01-05 03:34:50 +000094 virtual const char* onGetName() = 0;
mtklein96289052014-09-10 12:05:59 -070095 virtual const char* onGetUniqueName() { return this->onGetName(); }
joshualitt8a6697a2015-09-30 12:11:07 -070096 virtual void onDelayedSetup() {}
robertphillips5b693772014-11-21 06:19:36 -080097 virtual void onPerCanvasPreDraw(SkCanvas*) {}
98 virtual void onPerCanvasPostDraw(SkCanvas*) {}
joshualitt8a6697a2015-09-30 12:11:07 -070099 virtual void onPreDraw(SkCanvas*) {}
100 virtual void onPostDraw(SkCanvas*) {}
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000101 // Each bench should do its main work in a loop like this:
102 // for (int i = 0; i < loops; i++) { <work here> }
mtkleina1ebeb22015-10-01 09:43:39 -0700103 virtual void onDraw(int loops, SkCanvas*) = 0;
reed@android.comf523e252009-01-26 23:15:37 +0000104
105 virtual SkIPoint onGetSize();
106
reed@android.com4bc19832009-01-19 20:08:35 +0000107private:
Mike Kleinfec9b902019-05-31 10:03:07 -0500108 int fUnits = 1;
109
John Stiles7571f9e2020-09-02 22:42:33 -0400110 using INHERITED = SkRefCnt;
reed@android.combd700c32009-01-05 03:34:50 +0000111};
112
Mike Reedab273fa2017-01-11 13:58:55 -0500113typedef sk_tools::Registry<Benchmark*(*)(void*)> BenchRegistry;
reed@android.comf523e252009-01-26 23:15:37 +0000114
reed@android.combd700c32009-01-05 03:34:50 +0000115#endif