blob: ffd6893c4a9cbbc4776c2248c76f30f6acf7336b [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"
12#include "SkImageInfo.h"
13#include "SkSurface.h"
14#include "SkTypes.h"
15
16#if SK_SUPPORT_GPU
17#include "GrContextFactory.h"
18#endif
19
20class ResultsWriter;
21class SkBitmap;
22class SkCanvas;
23
24struct Config {
svaisanenc47635e2016-01-28 06:05:43 -080025 SkString name;
tomhudsond968a6f2015-03-26 11:28:06 -070026 Benchmark::Backend backend;
27 SkColorType color;
28 SkAlphaType alpha;
brianosmanb109b8c2016-06-16 13:03:24 -070029 sk_sp<SkColorSpace> colorSpace;
tomhudsond968a6f2015-03-26 11:28:06 -070030 int samples;
31#if SK_SUPPORT_GPU
bsalomon85b4b532016-04-05 11:06:27 -070032 sk_gpu_test::GrContextFactory::ContextType ctxType;
csmartdaltone812d492017-02-21 12:36:05 -070033 sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides;
tomhudsond968a6f2015-03-26 11:28:06 -070034 bool useDFText;
35#else
36 int bogusInt;
kkinnunen5219fd92015-12-10 06:28:13 -080037 int bogusIntOption;
tomhudsond968a6f2015-03-26 11:28:06 -070038 bool bogusBool;
39#endif
40};
41
42struct Target {
43 explicit Target(const Config& c) : config(c) { }
44 virtual ~Target() { }
45
46 const Config config;
reede8f30622016-03-23 18:59:25 -070047 sk_sp<SkSurface> surface;
tomhudsond968a6f2015-03-26 11:28:06 -070048
49 /** Called once per target, immediately before any timing or drawing. */
50 virtual void setup() { }
51
52 /** Called *after* the clock timer is started, before the benchmark
tomhudson75a0ebb2015-03-27 12:11:44 -070053 is drawn. Most back ends just return the canvas passed in,
54 but some may replace it. */
tomhudsond968a6f2015-03-26 11:28:06 -070055 virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; }
56
57 /** Called *after* a benchmark is drawn, but before the clock timer
58 is stopped. */
59 virtual void endTiming() { }
60
61 /** Called between benchmarks (or between calibration and measured
62 runs) to make sure all pending work in drivers / threads is
63 complete. */
64 virtual void fence() { }
65
66 /** CPU-like targets can just be timed, but GPU-like
67 targets need to pay attention to frame boundaries
68 or other similar details. */
cdaltond416a5b2015-06-23 13:23:44 -070069 virtual bool needsFrameTiming(int* frameLag) const { return false; }
tomhudsond968a6f2015-03-26 11:28:06 -070070
71 /** Called once per target, during program initialization.
72 Returns false if initialization fails. */
73 virtual bool init(SkImageInfo info, Benchmark* bench);
74
75 /** Stores any pixels drawn to the screen in the bitmap.
76 Returns false on error. */
77 virtual bool capturePixels(SkBitmap* bmp);
78
79 /** Writes any config-specific data to the log. */
80 virtual void fillOptions(ResultsWriter*) { }
tomhudson75a0ebb2015-03-27 12:11:44 -070081
82 SkCanvas* getCanvas() const {
83 if (!surface.get()) {
halcanary96fcdcc2015-08-27 07:41:13 -070084 return nullptr;
tomhudson75a0ebb2015-03-27 12:11:44 -070085 }
86 return surface->getCanvas();
87 }
tomhudsond968a6f2015-03-26 11:28:06 -070088};
89
90#endif // nanobench_DEFINED