nanobench.h revision 9d66696f6b1651b52adad1b0202aca013ef7da58
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 20class ResultsWriter; 21class SkBitmap; 22class SkCanvas; 23 24struct Config { 25 const char* name; 26 Benchmark::Backend backend; 27 SkColorType color; 28 SkAlphaType alpha; 29 int samples; 30#if SK_SUPPORT_GPU 31 GrContextFactory::GLContextType ctxType; 32 bool useDFText; 33#else 34 int bogusInt; 35 bool bogusBool; 36#endif 37}; 38 39struct Target { 40 explicit Target(const Config& c) : config(c) { } 41 virtual ~Target() { } 42 43 const Config config; 44 SkAutoTDelete<SkSurface> surface; 45 46 /** Called once per target, immediately before any timing or drawing. */ 47 virtual void setup() { } 48 49 /** Called *after* the clock timer is started, before the benchmark 50 is drawn. Most back ends just return the canvas passed in, 51 but some may replace it. */ 52 virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; } 53 54 /** Called *after* a benchmark is drawn, but before the clock timer 55 is stopped. */ 56 virtual void endTiming() { } 57 58 /** Called between benchmarks (or between calibration and measured 59 runs) to make sure all pending work in drivers / threads is 60 complete. */ 61 virtual void fence() { } 62 63 /** CPU-like targets can just be timed, but GPU-like 64 targets need to pay attention to frame boundaries 65 or other similar details. */ 66 virtual bool needsFrameTiming(int* frameLag) const { return false; } 67 68 /** Called once per target, during program initialization. 69 Returns false if initialization fails. */ 70 virtual bool init(SkImageInfo info, Benchmark* bench); 71 72 /** Stores any pixels drawn to the screen in the bitmap. 73 Returns false on error. */ 74 virtual bool capturePixels(SkBitmap* bmp); 75 76 /** Writes any config-specific data to the log. */ 77 virtual void fillOptions(ResultsWriter*) { } 78 79 SkCanvas* getCanvas() const { 80 if (!surface.get()) { 81 return nullptr; 82 } 83 return surface->getCanvas(); 84 } 85}; 86 87#endif // nanobench_DEFINED 88