blob: 5ba6e9110893b442f6952dfd9711c7468d03cf24 [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"
reed@android.comf523e252009-01-26 23:15:37 +000014#include "SkTRegistry.h"
reed@android.combd700c32009-01-05 03:34:50 +000015
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000016#define DEF_BENCH(code) \
17namespace { \
tfarinaf168b862014-06-19 12:32:29 -070018static Benchmark* SK_MACRO_APPEND_LINE(factory)(void*) { code; } \
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000019BenchRegistry SK_MACRO_APPEND_LINE(g_R_)(SK_MACRO_APPEND_LINE(factory)); \
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000020}
reed@google.comb8b92ea2012-10-16 15:57:13 +000021
22/*
23 * With the above macros, you can register benches as follows (at the bottom
24 * of your .cpp)
25 *
mtklein@google.com410e6e82013-09-13 19:52:27 +000026 * DEF_BENCH(return new MyBenchmark(...))
27 * DEF_BENCH(return new MyBenchmark(...))
28 * DEF_BENCH(return new MyBenchmark(...))
reed@google.comb8b92ea2012-10-16 15:57:13 +000029 */
30
31
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
reed@android.com4e635f92009-10-19 17:39:46 +000035class SkTriState {
36public:
37 enum State {
38 kDefault,
39 kTrue,
40 kFalse
41 };
mtklein@google.comc2897432013-09-10 19:23:38 +000042 static const char* Name[];
reed@android.com4e635f92009-10-19 17:39:46 +000043};
44
tfarinaf168b862014-06-19 12:32:29 -070045class Benchmark : public SkRefCnt {
reed@android.combd700c32009-01-05 03:34:50 +000046public:
tfarinaf168b862014-06-19 12:32:29 -070047 SK_DECLARE_INST_COUNT(Benchmark)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000048
tfarinaf168b862014-06-19 12:32:29 -070049 Benchmark();
reed@android.com4bc19832009-01-19 20:08:35 +000050
reed@android.combd700c32009-01-05 03:34:50 +000051 const char* getName();
mtklein96289052014-09-10 12:05:59 -070052 const char* getUniqueName();
reed@android.combd700c32009-01-05 03:34:50 +000053 SkIPoint getSize();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000054
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000055 enum Backend {
56 kNonRendering_Backend,
57 kRaster_Backend,
58 kGPU_Backend,
59 kPDF_Backend,
tomhudsond968a6f2015-03-26 11:28:06 -070060 kHWUI_Backend,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000061 };
62
63 // Call to determine whether the benchmark is intended for
64 // the rendering mode.
65 virtual bool isSuitableFor(Backend backend) {
66 return backend != kNonRendering_Backend;
67 }
68
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000069 // Call before draw, allows the benchmark to do setup work outside of the
70 // timer. When a benchmark is repeatedly drawn, this should be called once
71 // before the initial draw.
72 void preDraw();
73
robertphillips5b693772014-11-21 06:19:36 -080074 // Called once before and after a series of draw calls to a single canvas.
75 // The setup/break down in these calls is not timed.
76 void perCanvasPreDraw(SkCanvas*);
77 void perCanvasPostDraw(SkCanvas*);
78
commit-bot@chromium.org33614712013-12-03 18:17:16 +000079 // Bench framework can tune loops to be large enough for stable timing.
80 void draw(const int loops, SkCanvas*);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000081
reed@android.com4bc19832009-01-19 20:08:35 +000082 void setForceAlpha(int alpha) {
83 fForceAlpha = alpha;
84 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085
reed@android.com4e635f92009-10-19 17:39:46 +000086 void setDither(SkTriState::State state) {
87 fDither = state;
88 }
reed@android.come9d00602009-09-02 21:12:42 +000089
reed@google.comef77ec22013-05-29 15:39:54 +000090 /** Assign masks for paint-flags. These will be applied when setupPaint()
91 * is called.
92 *
93 * Performs the following on the paint:
94 * uint32_t flags = paint.getFlags();
95 * flags &= ~clearMask;
96 * flags |= orMask;
97 * paint.setFlags(flags);
98 */
99 void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
100 fOrMask = orMask;
101 fClearMask = clearMask;
102 }
103
reed@android.combd700c32009-01-05 03:34:50 +0000104protected:
reed@google.com0561a3c2012-11-15 19:52:20 +0000105 virtual void setupPaint(SkPaint* paint);
reed@android.com4bc19832009-01-19 20:08:35 +0000106
reed@android.combd700c32009-01-05 03:34:50 +0000107 virtual const char* onGetName() = 0;
mtklein96289052014-09-10 12:05:59 -0700108 virtual const char* onGetUniqueName() { return this->onGetName(); }
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000109 virtual void onPreDraw() {}
robertphillips5b693772014-11-21 06:19:36 -0800110 virtual void onPerCanvasPreDraw(SkCanvas*) {}
111 virtual void onPerCanvasPostDraw(SkCanvas*) {}
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000112 // Each bench should do its main work in a loop like this:
113 // for (int i = 0; i < loops; i++) { <work here> }
114 virtual void onDraw(const int loops, SkCanvas*) = 0;
reed@android.comf523e252009-01-26 23:15:37 +0000115
116 virtual SkIPoint onGetSize();
117
reed@android.com4bc19832009-01-19 20:08:35 +0000118private:
119 int fForceAlpha;
reed@android.com4e635f92009-10-19 17:39:46 +0000120 SkTriState::State fDither;
reed@google.comef77ec22013-05-29 15:39:54 +0000121 uint32_t fOrMask, fClearMask;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000122
123 typedef SkRefCnt INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +0000124};
125
tfarinaf168b862014-06-19 12:32:29 -0700126typedef SkTRegistry<Benchmark*(*)(void*)> BenchRegistry;
reed@android.comf523e252009-01-26 23:15:37 +0000127
reed@android.combd700c32009-01-05 03:34:50 +0000128#endif