total_animation_throughput_reporter.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "ui/compositor/total_animation_throughput_reporter.h"
  5. #include "base/logging.h"
  6. #include "base/observer_list.h"
  7. #include "ui/compositor/compositor.h"
  8. namespace ui {
  9. TotalAnimationThroughputReporter::TotalAnimationThroughputReporter(
  10. ui::Compositor* compositor,
  11. ReportOnceCallback once_callback,
  12. bool should_delete)
  13. : TotalAnimationThroughputReporter(compositor,
  14. ReportRepeatingCallback(),
  15. std::move(once_callback),
  16. should_delete) {}
  17. TotalAnimationThroughputReporter::TotalAnimationThroughputReporter(
  18. ui::Compositor* compositor,
  19. ReportRepeatingCallback repeating_callback)
  20. : TotalAnimationThroughputReporter(compositor,
  21. repeating_callback,
  22. ReportOnceCallback(),
  23. /*should_delete=*/false) {}
  24. TotalAnimationThroughputReporter::~TotalAnimationThroughputReporter() {
  25. if (throughput_tracker_)
  26. throughput_tracker_->Cancel();
  27. if (compositor_)
  28. compositor_->RemoveObserver(this);
  29. }
  30. void TotalAnimationThroughputReporter::OnFirstAnimationStarted(
  31. ui::Compositor* compositor) {
  32. throughput_tracker_ = compositor->RequestNewThroughputTracker();
  33. throughput_tracker_->Start(base::BindRepeating(
  34. &TotalAnimationThroughputReporter::Report, ptr_factory_.GetWeakPtr()));
  35. }
  36. void TotalAnimationThroughputReporter::OnLastAnimationEnded(
  37. ui::Compositor* compositor) {
  38. throughput_tracker_->Stop();
  39. throughput_tracker_.reset();
  40. // Stop observing if no need to report multiple times.
  41. if (report_repeating_callback_.is_null())
  42. compositor_->RemoveObserver(this);
  43. }
  44. void TotalAnimationThroughputReporter::OnCompositingShuttingDown(
  45. ui::Compositor* compositor) {
  46. if (throughput_tracker_) {
  47. throughput_tracker_->Cancel();
  48. throughput_tracker_.reset();
  49. }
  50. compositor->RemoveObserver(this);
  51. compositor_ = nullptr;
  52. if (should_delete_)
  53. delete this;
  54. }
  55. TotalAnimationThroughputReporter::TotalAnimationThroughputReporter(
  56. ui::Compositor* compositor,
  57. ReportRepeatingCallback repeating_callback,
  58. ReportOnceCallback once_callback,
  59. bool should_delete)
  60. : compositor_(compositor),
  61. report_repeating_callback_(repeating_callback),
  62. report_once_callback_(std::move(once_callback)),
  63. should_delete_(should_delete) {
  64. DCHECK_NE(report_repeating_callback_.is_null(),
  65. report_once_callback_.is_null());
  66. compositor_->AddObserver(this);
  67. if (!compositor->animation_observer_list_.empty())
  68. OnFirstAnimationStarted(compositor_);
  69. }
  70. void TotalAnimationThroughputReporter::Report(
  71. const cc::FrameSequenceMetrics::CustomReportData& data) {
  72. if (!report_once_callback_.is_null()) {
  73. compositor_->RemoveObserver(this);
  74. std::move(report_once_callback_).Run(data);
  75. if (should_delete_)
  76. delete this;
  77. return;
  78. }
  79. if (!report_repeating_callback_.is_null())
  80. report_repeating_callback_.Run(data);
  81. }
  82. } // namespace ui