Benchmark.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2011 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 Benchmark_DEFINED
  8. #define Benchmark_DEFINED
  9. #include "include/core/SkPoint.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "include/core/SkString.h"
  12. #include "tools/Registry.h"
  13. #define DEF_BENCH3(code, N) \
  14. static BenchRegistry gBench##N([](void*) -> Benchmark* { code; });
  15. #define DEF_BENCH2(code, N) DEF_BENCH3(code, N)
  16. #define DEF_BENCH(code) DEF_BENCH2(code, __COUNTER__)
  17. /*
  18. * With the above macros, you can register benches as follows (at the bottom
  19. * of your .cpp)
  20. *
  21. * DEF_BENCH(return new MyBenchmark(...))
  22. * DEF_BENCH(return new MyBenchmark(...))
  23. * DEF_BENCH(return new MyBenchmark(...))
  24. */
  25. struct GrContextOptions;
  26. class SkCanvas;
  27. class SkPaint;
  28. class Benchmark : public SkRefCnt {
  29. public:
  30. Benchmark();
  31. const char* getName();
  32. const char* getUniqueName();
  33. SkIPoint getSize();
  34. enum Backend {
  35. kNonRendering_Backend,
  36. kRaster_Backend,
  37. kGPU_Backend,
  38. kPDF_Backend,
  39. kHWUI_Backend,
  40. };
  41. // Call to determine whether the benchmark is intended for
  42. // the rendering mode.
  43. virtual bool isSuitableFor(Backend backend) {
  44. return backend != kNonRendering_Backend;
  45. }
  46. // Allows a benchmark to override options used to construct the GrContext.
  47. virtual void modifyGrContextOptions(GrContextOptions*) {}
  48. virtual int calculateLoops(int defaultLoops) const {
  49. return defaultLoops;
  50. }
  51. // Call before draw, allows the benchmark to do setup work outside of the
  52. // timer. When a benchmark is repeatedly drawn, this should be called once
  53. // before the initial draw.
  54. void delayedSetup();
  55. // Called once before and after a series of draw calls to a single canvas.
  56. // The setup/break down in these calls is not timed.
  57. void perCanvasPreDraw(SkCanvas*);
  58. void perCanvasPostDraw(SkCanvas*);
  59. // Called just before and after each call to draw(). Not timed.
  60. void preDraw(SkCanvas*);
  61. void postDraw(SkCanvas*);
  62. // Bench framework can tune loops to be large enough for stable timing.
  63. void draw(int loops, SkCanvas*);
  64. virtual void getGpuStats(SkCanvas*, SkTArray<SkString>* keys, SkTArray<double>* values) {}
  65. // Count of units (pixels, whatever) being exercised, to scale timing by.
  66. int getUnits() const { return fUnits; }
  67. protected:
  68. void setUnits(int units) { SkASSERT(units > 0); fUnits = units; }
  69. virtual void setupPaint(SkPaint* paint);
  70. virtual const char* onGetName() = 0;
  71. virtual const char* onGetUniqueName() { return this->onGetName(); }
  72. virtual void onDelayedSetup() {}
  73. virtual void onPerCanvasPreDraw(SkCanvas*) {}
  74. virtual void onPerCanvasPostDraw(SkCanvas*) {}
  75. virtual void onPreDraw(SkCanvas*) {}
  76. virtual void onPostDraw(SkCanvas*) {}
  77. // Each bench should do its main work in a loop like this:
  78. // for (int i = 0; i < loops; i++) { <work here> }
  79. virtual void onDraw(int loops, SkCanvas*) = 0;
  80. virtual SkIPoint onGetSize();
  81. private:
  82. int fUnits = 1;
  83. typedef SkRefCnt INHERITED;
  84. };
  85. typedef sk_tools::Registry<Benchmark*(*)(void*)> BenchRegistry;
  86. #endif