tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 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 "Benchmark.h" |
Brian Osman | c7ad40f | 2018-05-31 14:27:17 -0400 | [diff] [blame] | 12 | #include "GrContextFactory.h" |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
| 14 | #include "SkSurface.h" |
| 15 | #include "SkTypes.h" |
| 16 | |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 17 | class SkBitmap; |
| 18 | class SkCanvas; |
Brian Osman | 8c0a1ca | 2019-01-28 14:24:29 -0500 | [diff] [blame] | 19 | class NanoJSONResultsWriter; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 20 | |
| 21 | struct Config { |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 22 | SkString name; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 23 | Benchmark::Backend backend; |
| 24 | SkColorType color; |
| 25 | SkAlphaType alpha; |
brianosman | b109b8c | 2016-06-16 13:03:24 -0700 | [diff] [blame] | 26 | sk_sp<SkColorSpace> colorSpace; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 27 | int samples; |
bsalomon | 85b4b53 | 2016-04-05 11:06:27 -0700 | [diff] [blame] | 28 | sk_gpu_test::GrContextFactory::ContextType ctxType; |
csmartdalton | e812d49 | 2017-02-21 12:36:05 -0700 | [diff] [blame] | 29 | sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 30 | bool useDFText; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct Target { |
| 34 | explicit Target(const Config& c) : config(c) { } |
| 35 | virtual ~Target() { } |
| 36 | |
| 37 | const Config config; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 38 | sk_sp<SkSurface> surface; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 39 | |
| 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 |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 44 | is drawn. Most back ends just return the canvas passed in, |
| 45 | but some may replace it. */ |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 46 | 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. */ |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 60 | virtual bool needsFrameTiming(int* frameLag) const { return false; } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 61 | |
| 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 Osman | 8c0a1ca | 2019-01-28 14:24:29 -0500 | [diff] [blame] | 71 | virtual void fillOptions(NanoJSONResultsWriter& log) { } |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 72 | |
Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame] | 73 | /** Writes gathered stats using SkDebugf. */ |
| 74 | virtual void dumpStats() {} |
| 75 | |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 76 | SkCanvas* getCanvas() const { |
| 77 | if (!surface.get()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | return nullptr; |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 79 | } |
| 80 | return surface->getCanvas(); |
| 81 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | #endif // nanobench_DEFINED |