shell_surface_presentation_time_recorder.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2022 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 COMPONENTS_EXO_SHELL_SURFACE_PRESENTATION_TIME_RECORDER_H_
  5. #define COMPONENTS_EXO_SHELL_SURFACE_PRESENTATION_TIME_RECORDER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "ash/public/cpp/presentation_time_recorder.h"
  9. #include "base/containers/circular_deque.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/scoped_observation.h"
  12. #include "base/time/time.h"
  13. #include "components/exo/shell_surface.h"
  14. #include "components/exo/shell_surface_observer.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace gfx {
  17. struct PresentationFeedback;
  18. }
  19. namespace exo {
  20. // ShellSurfacePresentationTimeRecorder records the time of
  21. // configure-> ack -> present
  22. // of the underlying ShellSurface after a request is made. Requests
  23. // made for the same configure will be ignored.
  24. class ShellSurfacePresentationTimeRecorder
  25. : public ash::PresentationTimeRecorder,
  26. public ShellSurfaceObserver {
  27. public:
  28. class Reporter {
  29. public:
  30. virtual ~Reporter() = default;
  31. // Invoked to report the time delta.
  32. virtual void ReportTime(base::TimeDelta delta) = 0;
  33. };
  34. // Factory to create histogram reporter.
  35. static std::unique_ptr<Reporter> CreateHistogramReporter(
  36. const char* latency_histogram_name,
  37. absl::optional<const char*> max_latency_histogram_name = absl::nullopt);
  38. ShellSurfacePresentationTimeRecorder(ShellSurface* shell_surface,
  39. std::unique_ptr<Reporter> reporter);
  40. ShellSurfacePresentationTimeRecorder(
  41. const ShellSurfacePresentationTimeRecorder&) = delete;
  42. ShellSurfacePresentationTimeRecorder& operator=(
  43. const ShellSurfacePresentationTimeRecorder&) = delete;
  44. ~ShellSurfacePresentationTimeRecorder() override;
  45. // ash::PresentationTimeRecorder:
  46. void PrepareToRecord() override;
  47. bool RequestNext() override;
  48. // ShellSurfaceObserver:
  49. void OnConfigure(uint32_t serial) override;
  50. void OnAcknowledgeConfigure(uint32_t serial) override;
  51. void OnShellSurfaceDestroyed() override;
  52. protected:
  53. struct Request {
  54. uint64_t request_id = 0u;
  55. // Time when RequestNext is called.
  56. base::TimeTicks request_time;
  57. // Serial of the first Configure after RequestNext.
  58. absl::optional<uint32_t> serial = absl::nullopt;
  59. };
  60. // Invoked to notify a frame is presented to calculate time delta between
  61. // `request_time` and the frame's presentation feedback.
  62. // Virtual for testing.
  63. virtual void OnFramePresented(const Request& request,
  64. const gfx::PresentationFeedback& feedback);
  65. private:
  66. ShellSurface* shell_surface_ = nullptr;
  67. std::unique_ptr<Reporter> reporter_;
  68. uint64_t next_request_id_ = 0u;
  69. // Request waiting for configure. There would be only one such request.
  70. absl::optional<Request> pending_request_;
  71. // Requests that have received "configure" and wait for "ack".
  72. base::circular_deque<Request> requests_;
  73. base::ScopedObservation<ShellSurface, ShellSurfaceObserver>
  74. scoped_observation_{this};
  75. base::WeakPtrFactory<ShellSurfacePresentationTimeRecorder> weak_ptr_factory_{
  76. this};
  77. };
  78. } // namespace exo
  79. #endif // COMPONENTS_EXO_SHELL_SURFACE_PRESENTATION_TIME_RECORDER_H_