xref: /aosp_15_r20/external/skia/bench/nanobench.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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 nanobench_DEFINED
9 #define nanobench_DEFINED
10 
11 #include "bench/Benchmark.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkTypes.h"
15 #include "tools/gpu/GrContextFactory.h"
16 #if defined(SK_GRAPHITE)
17 #include "tools/graphite/ContextFactory.h"
18 #endif
19 
20 class SkBitmap;
21 class SkCanvas;
22 class NanoJSONResultsWriter;
23 
24 struct Config {
25     SkString name;
26     Benchmark::Backend backend;
27     SkColorType color;
28     SkAlphaType alpha;
29     sk_sp<SkColorSpace> colorSpace;
30     int samples;
31     sk_gpu_test::GrContextFactory::ContextType ctxType;
32     sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides;
33     uint32_t surfaceFlags;
34 };
35 
36 struct Target {
TargetTarget37     explicit Target(const Config& c) : config(c) { }
~TargetTarget38     virtual ~Target() { }
39 
40     const Config config;
41     sk_sp<SkSurface> surface;
42 
43     /** Called once per target, immediately before any timing or drawing. */
setupTarget44     void setup() {
45         this->onSetup();
46         // Make sure we're done with setup work before we start timing.
47         this->submitWorkAndSyncCPU();
48     }
onSetupTarget49     virtual void onSetup() { }
50 
51     /** Called *after* the clock timer is started, before the benchmark
52         is drawn. Most back ends just return the canvas passed in,
53         but some may replace it. */
beginTimingTarget54     virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; }
55 
56     /** Called *after* a benchmark is drawn, but before the clock timer
57         is stopped.  */
endTimingTarget58     virtual void endTiming() { }
59 
60     /** Called between benchmarks (or between calibration and measured
61         runs) to make sure all pending work in drivers / threads is
62         complete. */
submitWorkAndSyncCPUTarget63     virtual void submitWorkAndSyncCPU() { }
64 
65     /** CPU-like targets can just be timed, but GPU-like
66         targets need to pay attention to frame boundaries
67         or other similar details. */
needsFrameTimingTarget68     virtual bool needsFrameTiming(int* frameLag) const { return false; }
69 
70     /** Called once per target, during program initialization.
71         Returns false if initialization fails. */
72     virtual bool init(SkImageInfo info, Benchmark* bench);
73 
74     /** Stores any pixels drawn to the screen in the bitmap.
75         Returns false on error. */
76     virtual bool capturePixels(SkBitmap* bmp);
77 
78     /** Writes gathered stats using SkDebugf. */
dumpStatsTarget79     virtual void dumpStats() {}
80 
getCanvasTarget81     SkCanvas* getCanvas() const {
82         if (!surface) {
83             return nullptr;
84         }
85         return surface->getCanvas();
86     }
87 };
88 
89 #endif  // nanobench_DEFINED
90