nanobench.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef nanobench_DEFINED
  8. #define nanobench_DEFINED
  9. #include "bench/Benchmark.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkSurface.h"
  12. #include "include/core/SkTypes.h"
  13. #include "tools/gpu/GrContextFactory.h"
  14. class SkBitmap;
  15. class SkCanvas;
  16. class NanoJSONResultsWriter;
  17. struct Config {
  18. SkString name;
  19. Benchmark::Backend backend;
  20. SkColorType color;
  21. SkAlphaType alpha;
  22. sk_sp<SkColorSpace> colorSpace;
  23. int samples;
  24. sk_gpu_test::GrContextFactory::ContextType ctxType;
  25. sk_gpu_test::GrContextFactory::ContextOverrides ctxOverrides;
  26. bool useDFText;
  27. };
  28. struct Target {
  29. explicit Target(const Config& c) : config(c) { }
  30. virtual ~Target() { }
  31. const Config config;
  32. sk_sp<SkSurface> surface;
  33. /** Called once per target, immediately before any timing or drawing. */
  34. virtual void setup() { }
  35. /** Called *after* the clock timer is started, before the benchmark
  36. is drawn. Most back ends just return the canvas passed in,
  37. but some may replace it. */
  38. virtual SkCanvas* beginTiming(SkCanvas* canvas) { return canvas; }
  39. /** Called *after* a benchmark is drawn, but before the clock timer
  40. is stopped. */
  41. virtual void endTiming() { }
  42. /** Called between benchmarks (or between calibration and measured
  43. runs) to make sure all pending work in drivers / threads is
  44. complete. */
  45. virtual void fence() { }
  46. /** CPU-like targets can just be timed, but GPU-like
  47. targets need to pay attention to frame boundaries
  48. or other similar details. */
  49. virtual bool needsFrameTiming(int* frameLag) const { return false; }
  50. /** Called once per target, during program initialization.
  51. Returns false if initialization fails. */
  52. virtual bool init(SkImageInfo info, Benchmark* bench);
  53. /** Stores any pixels drawn to the screen in the bitmap.
  54. Returns false on error. */
  55. virtual bool capturePixels(SkBitmap* bmp);
  56. /** Writes any config-specific data to the log. */
  57. virtual void fillOptions(NanoJSONResultsWriter& log) { }
  58. /** Writes gathered stats using SkDebugf. */
  59. virtual void dumpStats() {}
  60. SkCanvas* getCanvas() const {
  61. if (!surface.get()) {
  62. return nullptr;
  63. }
  64. return surface->getCanvas();
  65. }
  66. };
  67. #endif // nanobench_DEFINED