blob: fe63e04c12117deadcc2adb0cc39e8e6c6ee04fd [file] [log] [blame]
tomhudsond968a6f2015-03-26 11:28:06 -07001/*
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 "Benchmark.h"
Brian Osmanc7ad40f2018-05-31 14:27:17 -040012#include "GrContextFactory.h"
tomhudsond968a6f2015-03-26 11:28:06 -070013#include "SkImageInfo.h"
14#include "SkSurface.h"
15#include "SkTypes.h"
16
tomhudsond968a6f2015-03-26 11:28:06 -070017class SkBitmap;
18class SkCanvas;
Brian Osman8c0a1ca2019-01-28 14:24:29 -050019class NanoJSONResultsWriter;
tomhudsond968a6f2015-03-26 11:28:06 -070020
21struct Config {
svaisanenc47635e2016-01-28 06:05:43 -080022 SkString name;
tomhudsond968a6f2015-03-26 11:28:06 -070023 Benchmark::Backend backend;
24 SkColorType color;
25 SkAlphaType alpha;
brianosmanb109b8c2016-06-16 13:03:24 -070026 sk_sp<SkColorSpace> colorSpace;
tomhudsond968a6f2015-03-26 11:28:06 -070027 int samples;
bsalomon85b4b532016-04-05 11:06:27 -070028 sk_gpu_test::GrContextFactory::ContextType ctxType;
csmartdaltone812d492017-02-21 12:36:05 -070029 sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides;
tomhudsond968a6f2015-03-26 11:28:06 -070030 bool useDFText;
tomhudsond968a6f2015-03-26 11:28:06 -070031};
32
33struct Target {
34 explicit Target(const Config& c) : config(c) { }
35 virtual ~Target() { }
36
37 const Config config;
reede8f30622016-03-23 18:59:25 -070038 sk_sp<SkSurface> surface;
tomhudsond968a6f2015-03-26 11:28:06 -070039
40 /** Called once per target, immediately before any timing or drawing. */
41 virtual void setup() { }
42
43 /** Called *after* the clock timer is started, before the benchmark
tomhudson75a0ebb2015-03-27 12:11:44 -070044 is drawn. Most back ends just return the canvas passed in,
45 but some may replace it. */
tomhudsond968a6f2015-03-26 11:28:06 -070046 virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; }
47
48 /** Called *after* a benchmark is drawn, but before the clock timer
49 is stopped. */
50 virtual void endTiming() { }
51
52 /** Called between benchmarks (or between calibration and measured
53 runs) to make sure all pending work in drivers / threads is
54 complete. */
55 virtual void fence() { }
56
57 /** CPU-like targets can just be timed, but GPU-like
58 targets need to pay attention to frame boundaries
59 or other similar details. */
cdaltond416a5b2015-06-23 13:23:44 -070060 virtual bool needsFrameTiming(int* frameLag) const { return false; }
tomhudsond968a6f2015-03-26 11:28:06 -070061
62 /** Called once per target, during program initialization.
63 Returns false if initialization fails. */
64 virtual bool init(SkImageInfo info, Benchmark* bench);
65
66 /** Stores any pixels drawn to the screen in the bitmap.
67 Returns false on error. */
68 virtual bool capturePixels(SkBitmap* bmp);
69
70 /** Writes any config-specific data to the log. */
Brian Osman8c0a1ca2019-01-28 14:24:29 -050071 virtual void fillOptions(NanoJSONResultsWriter& log) { }
tomhudson75a0ebb2015-03-27 12:11:44 -070072
Brian Salomon0b4d8aa2017-10-11 15:34:27 -040073 /** Writes gathered stats using SkDebugf. */
74 virtual void dumpStats() {}
75
tomhudson75a0ebb2015-03-27 12:11:44 -070076 SkCanvas* getCanvas() const {
77 if (!surface.get()) {
halcanary96fcdcc2015-08-27 07:41:13 -070078 return nullptr;
tomhudson75a0ebb2015-03-27 12:11:44 -070079 }
80 return surface->getCanvas();
81 }
tomhudsond968a6f2015-03-26 11:28:06 -070082};
83
84#endif // nanobench_DEFINED