vsync_timing_manager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019 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_VSYNC_TIMING_MANAGER_H_
  5. #define COMPONENTS_EXO_VSYNC_TIMING_MANAGER_H_
  6. #include <vector>
  7. #include "ash/frame_throttler/frame_throttling_observer.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/time/time.h"
  10. #include "mojo/public/cpp/bindings/pending_remote.h"
  11. #include "mojo/public/cpp/bindings/receiver.h"
  12. #include "services/viz/privileged/mojom/compositing/vsync_parameter_observer.mojom.h"
  13. namespace exo {
  14. // Multiplexes vsync parameter updates from the display compositor to exo
  15. // clients using the zcr_vsync_feedback_v1 protocol. Will maintain an IPC
  16. // connection to the display compositor only when necessary.
  17. class VSyncTimingManager : public viz::mojom::VSyncParameterObserver,
  18. public ash::FrameThrottlingObserver {
  19. public:
  20. // Will be notified about changes in vsync parameters.
  21. class Observer {
  22. public:
  23. virtual ~Observer() = default;
  24. virtual void OnUpdateVSyncParameters(base::TimeTicks timebase,
  25. base::TimeDelta interval) = 0;
  26. };
  27. // Used to setup IPC connection to display compositor.
  28. class Delegate {
  29. public:
  30. virtual ~Delegate() = default;
  31. virtual void AddVSyncParameterObserver(
  32. mojo::PendingRemote<viz::mojom::VSyncParameterObserver> observer) = 0;
  33. };
  34. explicit VSyncTimingManager(Delegate* delegate);
  35. VSyncTimingManager(const VSyncTimingManager&) = delete;
  36. VSyncTimingManager& operator=(const VSyncTimingManager&) = delete;
  37. ~VSyncTimingManager() override;
  38. void AddObserver(Observer* obs);
  39. void RemoveObserver(Observer* obs);
  40. base::TimeDelta throttled_interval() const { return throttled_interval_; }
  41. private:
  42. // Overridden from viz::mojom::VSyncParameterObserver:
  43. void OnUpdateVSyncParameters(base::TimeTicks timebase,
  44. base::TimeDelta interval) override;
  45. // Overridden from ash::FrameThrottlingObserver
  46. void OnThrottlingStarted(const std::vector<aura::Window*>& windows,
  47. uint8_t fps) override;
  48. void OnThrottlingEnded() override;
  49. void InitializeConnection();
  50. void MaybeInitializeConnection();
  51. void OnConnectionError();
  52. base::TimeDelta throttled_interval_;
  53. base::TimeDelta last_interval_;
  54. base::TimeTicks last_timebase_;
  55. Delegate* const delegate_;
  56. std::vector<Observer*> observers_;
  57. mojo::Receiver<viz::mojom::VSyncParameterObserver> receiver_{this};
  58. base::WeakPtrFactory<VSyncTimingManager> weak_ptr_factory_{this};
  59. };
  60. } // namespace exo
  61. #endif // COMPONENTS_EXO_VSYNC_TIMING_MANAGER_H_