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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 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; |
Chris Dalton | 180b4a1 | 2021-03-16 20:49:15 -0600 | [diff] [blame] | 30 | uint32_t surfaceFlags; |
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 | |
Brian Salomon | 0b4d8aa | 2017-10-11 15:34:27 -0400 | [diff] [blame] | 70 | /** Writes gathered stats using SkDebugf. */ |
| 71 | virtual void dumpStats() {} |
| 72 | |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 73 | SkCanvas* getCanvas() const { |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 74 | if (!surface) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | return nullptr; |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 76 | } |
| 77 | return surface->getCanvas(); |
| 78 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | #endif // nanobench_DEFINED |