• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tools/graphite/ContextFactory.h"
17 
18 class SkBitmap;
19 class SkCanvas;
20 class NanoJSONResultsWriter;
21 
22 struct Config {
23     SkString name;
24     Benchmark::Backend backend;
25     SkColorType color;
26     SkAlphaType alpha;
27     sk_sp<SkColorSpace> colorSpace;
28     int samples;
29     sk_gpu_test::GrContextFactory::ContextType ctxType;
30     sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides;
31     uint32_t surfaceFlags;
32 };
33 
34 struct Target {
TargetTarget35     explicit Target(const Config& c) : config(c) { }
~TargetTarget36     virtual ~Target() { }
37 
38     const Config config;
39     sk_sp<SkSurface> surface;
40 
41     /** Called once per target, immediately before any timing or drawing. */
setupTarget42     virtual void setup() { }
43 
44     /** Called *after* the clock timer is started, before the benchmark
45         is drawn. Most back ends just return the canvas passed in,
46         but some may replace it. */
beginTimingTarget47     virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; }
48 
49     /** Called *after* a benchmark is drawn, but before the clock timer
50         is stopped.  */
endTimingTarget51     virtual void endTiming() { }
52 
53     /** Called between benchmarks (or between calibration and measured
54         runs) to make sure all pending work in drivers / threads is
55         complete. */
syncCPUTarget56     virtual void syncCPU() { }
57 
58     /** CPU-like targets can just be timed, but GPU-like
59         targets need to pay attention to frame boundaries
60         or other similar details. */
needsFrameTimingTarget61     virtual bool needsFrameTiming(int* frameLag) const { return false; }
62 
63     /** Called once per target, during program initialization.
64         Returns false if initialization fails. */
65     virtual bool init(SkImageInfo info, Benchmark* bench);
66 
67     /** Stores any pixels drawn to the screen in the bitmap.
68         Returns false on error. */
69     virtual bool capturePixels(SkBitmap* bmp);
70 
71     /** Writes gathered stats using SkDebugf. */
dumpStatsTarget72     virtual void dumpStats() {}
73 
getCanvasTarget74     SkCanvas* getCanvas() const {
75         if (!surface) {
76             return nullptr;
77         }
78         return surface->getCanvas();
79     }
80 };
81 
82 #endif  // nanobench_DEFINED
83