blob: 204ada6a4df534383ddccf6ccd331df3223801c5 [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;
29 int samples;
30#if SK_SUPPORT_GPU
31 GrContextFactory::GLContextType ctxType;
kkinnunen5219fd92015-12-10 06:28:13 -080032 GrContextFactory::GLContextOptions ctxOptions;
tomhudsond968a6f2015-03-26 11:28:06 -070033 bool useDFText;
34#else
35 int bogusInt;
kkinnunen5219fd92015-12-10 06:28:13 -080036 int bogusIntOption;
tomhudsond968a6f2015-03-26 11:28:06 -070037 bool bogusBool;
38#endif
39};
40
41struct Target {
42 explicit Target(const Config& c) : config(c) { }
43 virtual ~Target() { }
44
45 const Config config;
46 SkAutoTDelete<SkSurface> surface;
47
48 /** Called once per target, immediately before any timing or drawing. */
49 virtual void setup() { }
50
51 /** Called *after* the clock timer is started, before the benchmark
tomhudson75a0ebb2015-03-27 12:11:44 -070052 is drawn. Most back ends just return the canvas passed in,
53 but some may replace it. */
tomhudsond968a6f2015-03-26 11:28:06 -070054 virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; }
55
56 /** Called *after* a benchmark is drawn, but before the clock timer
57 is stopped. */
58 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. */
63 virtual void fence() { }
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. */
cdaltond416a5b2015-06-23 13:23:44 -070068 virtual bool needsFrameTiming(int* frameLag) const { return false; }
tomhudsond968a6f2015-03-26 11:28:06 -070069
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 any config-specific data to the log. */
79 virtual void fillOptions(ResultsWriter*) { }
tomhudson75a0ebb2015-03-27 12:11:44 -070080
81 SkCanvas* getCanvas() const {
82 if (!surface.get()) {
halcanary96fcdcc2015-08-27 07:41:13 -070083 return nullptr;
tomhudson75a0ebb2015-03-27 12:11:44 -070084 }
85 return surface->getCanvas();
86 }
tomhudsond968a6f2015-03-26 11:28:06 -070087};
88
89#endif // nanobench_DEFINED