Home | History | Annotate | Download | only in bench
      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     sk_sp<SkColorSpace> colorSpace;
     30     int samples;
     31 #if SK_SUPPORT_GPU
     32     sk_gpu_test::GrContextFactory::ContextType ctxType;
     33     sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides;
     34     bool useDFText;
     35 #else
     36     int bogusInt;
     37     int bogusIntOption;
     38     bool bogusBool;
     39 #endif
     40 };
     41 
     42 struct Target {
     43     explicit Target(const Config& c) : config(c) { }
     44     virtual ~Target() { }
     45 
     46     const Config config;
     47     sk_sp<SkSurface> surface;
     48 
     49     /** Called once per target, immediately before any timing or drawing. */
     50     virtual void setup() { }
     51 
     52     /** Called *after* the clock timer is started, before the benchmark
     53         is drawn. Most back ends just return the canvas passed in,
     54         but some may replace it. */
     55     virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; }
     56 
     57     /** Called *after* a benchmark is drawn, but before the clock timer
     58         is stopped.  */
     59     virtual void endTiming() { }
     60 
     61     /** Called between benchmarks (or between calibration and measured
     62         runs) to make sure all pending work in drivers / threads is
     63         complete. */
     64     virtual void fence() { }
     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. */
     69     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 any config-specific data to the log. */
     80     virtual void fillOptions(ResultsWriter*) { }
     81 
     82     /** Writes gathered stats using SkDebugf. */
     83     virtual void dumpStats() {}
     84 
     85     SkCanvas* getCanvas() const {
     86         if (!surface.get()) {
     87             return nullptr;
     88         }
     89         return surface->getCanvas();
     90     }
     91 };
     92 
     93 #endif  // nanobench_DEFINED
     94