Flutter Engine
The Flutter Engine
Benchmark.h
Go to the documentation of this file.
1/*
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 */
7
8#ifndef Benchmark_DEFINED
9#define Benchmark_DEFINED
10
12#include "include/core/SkSize.h"
15#include "tools/Registry.h"
16
17#define DEF_BENCH3(code, N) \
18 static BenchRegistry gBench##N([](void*) -> Benchmark* { code; });
19#define DEF_BENCH2(code, N) DEF_BENCH3(code, N)
20#define DEF_BENCH(code) DEF_BENCH2(code, __COUNTER__)
21
22/*
23 * With the above macros, you can register benches as follows (at the bottom
24 * of your .cpp)
25 *
26 * DEF_BENCH(return new MyBenchmark(...))
27 * DEF_BENCH(return new MyBenchmark(...))
28 * DEF_BENCH(return new MyBenchmark(...))
29 */
30
31struct GrContextOptions;
33class SkCanvas;
34class SkPaint;
35
36class Benchmark : public SkRefCnt {
37public:
38 Benchmark();
39
40 const char* getName();
41 const char* getUniqueName();
43
44 enum class Backend {
45 kNonRendering,
46 kRaster,
47 kGanesh,
48 kGraphite,
49 kPDF,
50 kHWUI,
51 };
52
53 // Call to determine whether the benchmark is intended for
54 // the rendering mode.
57 }
58
59 // Allows a benchmark to override options used to construct the GrContext.
61
62 // Whether or not this benchmark requires multiple samples to get a meaningful result.
63 virtual bool shouldLoop() const {
64 return true;
65 }
66
67 // Call before draw, allows the benchmark to do setup work outside of the
68 // timer. When a benchmark is repeatedly drawn, this should be called once
69 // before the initial draw.
70 void delayedSetup();
71
72 // Called once before and after a series of draw calls to a single canvas.
73 // The setup/break down in these calls is not timed.
76
77 // Called just before and after each call to draw(). Not timed.
78 void preDraw(SkCanvas*);
79 void postDraw(SkCanvas*);
80
81 // Bench framework can tune loops to be large enough for stable timing.
82 void draw(int loops, SkCanvas*);
83
84 virtual void getGpuStats(SkCanvas*,
87
88 // Replaces the GrRecordingContext's dmsaaStats() with a single frame of this benchmark.
89 virtual bool getDMSAAStats(GrRecordingContext*) { return false; }
90
91 // Count of units (pixels, whatever) being exercised, to scale timing by.
92 int getUnits() const { return fUnits; }
93
94protected:
95 void setUnits(int units) { SkASSERT(units > 0); fUnits = units; }
96
97 virtual void setupPaint(SkPaint* paint);
98
99 virtual const char* onGetName() = 0;
100 virtual const char* onGetUniqueName() { return this->onGetName(); }
101 virtual void onDelayedSetup() {}
102 virtual void onPerCanvasPreDraw(SkCanvas*) {}
104 virtual void onPreDraw(SkCanvas*) {}
105 virtual void onPostDraw(SkCanvas*) {}
106 // Each bench should do its main work in a loop like this:
107 // for (int i = 0; i < loops; i++) { <work here> }
108 virtual void onDraw(int loops, SkCanvas*) = 0;
109
110 virtual SkISize onGetSize();
111
112private:
113 int fUnits = 1;
114
115 using INHERITED = SkRefCnt;
116};
117
119
120#endif
sk_tools::Registry< Benchmark *(*)(void *)> BenchRegistry
Definition: Benchmark.h:118
const char * backend
#define SkASSERT(cond)
Definition: SkAssert.h:116
virtual void getGpuStats(SkCanvas *, skia_private::TArray< SkString > *keys, skia_private::TArray< double > *values)
Definition: Benchmark.h:84
virtual bool shouldLoop() const
Definition: Benchmark.h:63
virtual void onPreDraw(SkCanvas *)
Definition: Benchmark.h:104
void delayedSetup()
Definition: Benchmark.cpp:30
virtual void onPerCanvasPostDraw(SkCanvas *)
Definition: Benchmark.h:103
virtual SkISize onGetSize()
Definition: Benchmark.cpp:59
void draw(int loops, SkCanvas *)
Definition: Benchmark.cpp:50
void postDraw(SkCanvas *)
Definition: Benchmark.cpp:42
virtual bool getDMSAAStats(GrRecordingContext *)
Definition: Benchmark.h:89
void setUnits(int units)
Definition: Benchmark.h:95
virtual void onDraw(int loops, SkCanvas *)=0
SkISize getSize()
Definition: Benchmark.cpp:26
void perCanvasPreDraw(SkCanvas *)
Definition: Benchmark.cpp:34
const char * getName()
Definition: Benchmark.cpp:18
virtual bool isSuitableFor(Backend backend)
Definition: Benchmark.h:55
virtual void onDelayedSetup()
Definition: Benchmark.h:101
virtual void onPerCanvasPreDraw(SkCanvas *)
Definition: Benchmark.h:102
virtual void onPostDraw(SkCanvas *)
Definition: Benchmark.h:105
void perCanvasPostDraw(SkCanvas *)
Definition: Benchmark.cpp:46
virtual void setupPaint(SkPaint *paint)
Definition: Benchmark.cpp:55
virtual const char * onGetUniqueName()
Definition: Benchmark.h:100
int getUnits() const
Definition: Benchmark.h:92
virtual const char * onGetName()=0
virtual void modifyGrContextOptions(GrContextOptions *)
Definition: Benchmark.h:60
void preDraw(SkCanvas *)
Definition: Benchmark.cpp:38
const char * getUniqueName()
Definition: Benchmark.cpp:22
const Paint & paint
Definition: color_source.cc:38
@ kRaster
Suitable for thread which raster data.
Definition: embedder.h:266
Definition: SkSize.h:16