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 #if defined(SK_GRAPHITE) 17 #include "tools/graphite/ContextFactory.h" 18 #endif 19 20 class SkBitmap; 21 class SkCanvas; 22 class NanoJSONResultsWriter; 23 24 struct Config { 25 SkString name; 26 Benchmark::Backend backend; 27 SkColorType color; 28 SkAlphaType alpha; 29 sk_sp<SkColorSpace> colorSpace; 30 int samples; 31 sk_gpu_test::GrContextFactory::ContextType ctxType; 32 sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides; 33 uint32_t surfaceFlags; 34 }; 35 36 struct Target { TargetTarget37 explicit Target(const Config& c) : config(c) { } ~TargetTarget38 virtual ~Target() { } 39 40 const Config config; 41 sk_sp<SkSurface> surface; 42 43 /** Called once per target, immediately before any timing or drawing. */ setupTarget44 void setup() { 45 this->onSetup(); 46 // Make sure we're done with setup work before we start timing. 47 this->submitWorkAndSyncCPU(); 48 } onSetupTarget49 virtual void onSetup() { } 50 51 /** Called *after* the clock timer is started, before the benchmark 52 is drawn. Most back ends just return the canvas passed in, 53 but some may replace it. */ beginTimingTarget54 virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; } 55 56 /** Called *after* a frame of a benchmark is drawn and the frame's work should be submitted to 57 be executed. This is currently used for the GPU targets to submit their recorded work to the 58 GPU. */ submitFrameTarget59 virtual void submitFrame() { } 60 61 /** Called between benchmarks (or between calibration and measured 62 runs) to make sure all pending work in drivers / threads is 63 complete. */ submitWorkAndSyncCPUTarget64 virtual void submitWorkAndSyncCPU() { } 65 66 /** CPU-like targets can just be timed, but GPU-like 67 targets need to pay attention to frame boundaries 68 or other similar details. */ needsFrameTimingTarget69 virtual bool needsFrameTiming(int* frameLag) const { return false; } 70 71 /** Called once per target, during program initialization. 72 Returns false if initialization fails. */ 73 virtual bool init(SkImageInfo info, Benchmark* bench); 74 75 /** Stores any pixels drawn to the screen in the bitmap. 76 Returns false on error. */ 77 virtual bool capturePixels(SkBitmap* bmp); 78 79 /** Writes gathered stats using SkDebugf. */ dumpStatsTarget80 virtual void dumpStats() {} 81 getCanvasTarget82 SkCanvas* getCanvas() const { 83 if (!surface) { 84 return nullptr; 85 } 86 return surface->getCanvas(); 87 } 88 }; 89 90 #endif // nanobench_DEFINED 91