total_animation_throughput_reporter.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_COMPOSITOR_TOTAL_ANIMATION_THROUGHPUT_REPORTER_H_
  5. #define UI_COMPOSITOR_TOTAL_ANIMATION_THROUGHPUT_REPORTER_H_
  6. #include "base/callback_forward.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "cc/metrics/frame_sequence_metrics.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "ui/compositor/compositor_export.h"
  12. #include "ui/compositor/compositor_observer.h"
  13. #include "ui/compositor/throughput_tracker.h"
  14. namespace ui {
  15. // Reports cc::FrameSequenceMetrics::ThroughputData between the first animation
  16. // start and the last animation ends on a compositor.
  17. //
  18. // Please see AnimationThroughputReporter for the definition of the throughput
  19. // and jack metrics.
  20. //
  21. // See also docs/speed/graphics_metrics_definitions.md.
  22. //
  23. // cc::FrameSequenceMetrics::CustomReportData contains the numbers of produced
  24. // frames, expected frames and jank count.
  25. //
  26. // The tracking starts when the first animation observer is added to the
  27. // compositor, then stopped when the last animation observer is removed. The
  28. // report callback is invoked on the next begin frame if there is enough data.
  29. // Since this observes multiple animations, aborting one of animations will
  30. // not cancel the tracking, and the data will be reported as normal.
  31. class COMPOSITOR_EXPORT TotalAnimationThroughputReporter
  32. : public CompositorObserver {
  33. public:
  34. using ReportOnceCallback = base::OnceCallback<void(
  35. const cc::FrameSequenceMetrics::CustomReportData& data)>;
  36. using ReportRepeatingCallback = base::RepeatingCallback<void(
  37. const cc::FrameSequenceMetrics::CustomReportData& data)>;
  38. // Create a TotalAnimationThroughputReporter that observes
  39. // the total animation throughput just once. If |should_delete|
  40. // is true, then the object will be deleted after callback is
  41. // invoked.
  42. TotalAnimationThroughputReporter(Compositor* compositor,
  43. ReportOnceCallback callback,
  44. bool should_delete);
  45. // Create a persistent TotalAnimationThroughputReporter, which
  46. // will call the callback every time the last animation is finished.
  47. TotalAnimationThroughputReporter(Compositor* compositor,
  48. ReportRepeatingCallback callback);
  49. TotalAnimationThroughputReporter(const TotalAnimationThroughputReporter&) =
  50. delete;
  51. TotalAnimationThroughputReporter& operator=(
  52. const TotalAnimationThroughputReporter&) = delete;
  53. ~TotalAnimationThroughputReporter() override;
  54. // CompositorObserver:
  55. void OnFirstAnimationStarted(Compositor* compositor) override;
  56. void OnLastAnimationEnded(Compositor* compositor) override;
  57. void OnCompositingShuttingDown(Compositor* compositor) override;
  58. bool IsMeasuringForTesting() const { return bool{throughput_tracker_}; }
  59. private:
  60. TotalAnimationThroughputReporter(Compositor* compositor,
  61. ReportRepeatingCallback repeating_callback,
  62. ReportOnceCallback once_callback,
  63. bool should_delete);
  64. void Report(const cc::FrameSequenceMetrics::CustomReportData& data);
  65. raw_ptr<Compositor> compositor_;
  66. ReportRepeatingCallback report_repeating_callback_;
  67. ReportOnceCallback report_once_callback_;
  68. bool should_delete_ = false;
  69. absl::optional<ThroughputTracker> throughput_tracker_;
  70. base::WeakPtrFactory<TotalAnimationThroughputReporter> ptr_factory_{this};
  71. };
  72. } // namespace ui
  73. #endif // UI_COMPOSITOR_TOTAL_ANIMATION_THROUGHPUT_REPORTER_H_