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 "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" 16 17 class SkBitmap; 18 class SkCanvas; 19 class NanoJSONResultsWriter; 20 21 struct Config { 22 SkString name; 23 Benchmark::Backend backend; 24 SkColorType color; 25 SkAlphaType alpha; 26 sk_sp<SkColorSpace> colorSpace; 27 int samples; 28 sk_gpu_test::GrContextFactory::ContextType ctxType; 29 sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides; 30 bool useDFText; 31 }; 32 33 struct Target { TargetTarget34 explicit Target(const Config& c) : config(c) { } ~TargetTarget35 virtual ~Target() { } 36 37 const Config config; 38 sk_sp<SkSurface> surface; 39 40 /** Called once per target, immediately before any timing or drawing. */ setupTarget41 virtual void setup() { } 42 43 /** Called *after* the clock timer is started, before the benchmark 44 is drawn. Most back ends just return the canvas passed in, 45 but some may replace it. */ beginTimingTarget46 virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; } 47 48 /** Called *after* a benchmark is drawn, but before the clock timer 49 is stopped. */ endTimingTarget50 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. */ fenceTarget55 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. */ needsFrameTimingTarget60 virtual bool needsFrameTiming(int* frameLag) const { return false; } 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. */ fillOptionsTarget71 virtual void fillOptions(NanoJSONResultsWriter& log) { } 72 73 /** Writes gathered stats using SkDebugf. */ dumpStatsTarget74 virtual void dumpStats() {} 75 getCanvasTarget76 SkCanvas* getCanvas() const { 77 if (!surface.get()) { 78 return nullptr; 79 } 80 return surface->getCanvas(); 81 } 82 }; 83 84 #endif // nanobench_DEFINED 85