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" |
| 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 | |
| 20 | class ResultsWriter; |
| 21 | class SkBitmap; |
| 22 | class SkCanvas; |
| 23 | |
| 24 | struct Config { |
svaisanen | c47635e | 2016-01-28 06:05:43 -0800 | [diff] [blame] | 25 | SkString name; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 26 | Benchmark::Backend backend; |
| 27 | SkColorType color; |
| 28 | SkAlphaType alpha; |
| 29 | int samples; |
| 30 | #if SK_SUPPORT_GPU |
| 31 | GrContextFactory::GLContextType ctxType; |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 32 | GrContextFactory::GLContextOptions ctxOptions; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 33 | bool useDFText; |
| 34 | #else |
| 35 | int bogusInt; |
kkinnunen | 5219fd9 | 2015-12-10 06:28:13 -0800 | [diff] [blame] | 36 | int bogusIntOption; |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 37 | bool bogusBool; |
| 38 | #endif |
| 39 | }; |
| 40 | |
| 41 | struct 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 |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 52 | is drawn. Most back ends just return the canvas passed in, |
| 53 | but some may replace it. */ |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 54 | 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. */ |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 68 | virtual bool needsFrameTiming(int* frameLag) const { return false; } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 69 | |
| 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*) { } |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 80 | |
| 81 | SkCanvas* getCanvas() const { |
| 82 | if (!surface.get()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 83 | return nullptr; |
tomhudson | 75a0ebb | 2015-03-27 12:11:44 -0700 | [diff] [blame] | 84 | } |
| 85 | return surface->getCanvas(); |
| 86 | } |
tomhudson | d968a6f | 2015-03-26 11:28:06 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | #endif // nanobench_DEFINED |