• 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 "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 {
25     SkString name;
26     Benchmark::Backend backend;
27     SkColorType color;
28     SkAlphaType alpha;
29     int samples;
30 #if SK_SUPPORT_GPU
31     GrContextFactory::GLContextType ctxType;
32     GrContextFactory::GLContextOptions ctxOptions;
33     bool useDFText;
34 #else
35     int bogusInt;
36     int bogusIntOption;
37     bool bogusBool;
38 #endif
39 };
40 
41 struct Target {
TargetTarget42     explicit Target(const Config& c) : config(c) { }
~TargetTarget43     virtual ~Target() { }
44 
45     const Config config;
46     SkAutoTDelete<SkSurface> surface;
47 
48     /** Called once per target, immediately before any timing or drawing. */
setupTarget49     virtual void setup() { }
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 benchmark is drawn, but before the clock timer
57         is stopped.  */
endTimingTarget58     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. */
fenceTarget63     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. */
needsFrameTimingTarget68     virtual bool needsFrameTiming(int* frameLag) const { return false; }
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. */
fillOptionsTarget79     virtual void fillOptions(ResultsWriter*) { }
80 
getCanvasTarget81     SkCanvas* getCanvas() const {
82         if (!surface.get()) {
83             return nullptr;
84         }
85         return surface->getCanvas();
86     }
87 };
88 
89 #endif  // nanobench_DEFINED
90