host_frame_rate_throttler.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2021 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/aura/host_frame_rate_throttler.h"
  5. #include "base/containers/contains.h"
  6. #include "build/build_config.h"
  7. #include "components/viz/common/surfaces/frame_sink_id.h"
  8. #include "components/viz/host/host_frame_sink_manager.h"
  9. #include "ui/aura/env.h"
  10. #include "ui/aura/window_tree_host.h"
  11. #include "ui/compositor/compositor.h"
  12. namespace aura {
  13. #if BUILDFLAG(IS_WIN)
  14. constexpr uint8_t kDefaultThrottleFps = 1;
  15. #else
  16. constexpr uint8_t kDefaultThrottleFps = 20;
  17. #endif
  18. HostFrameRateThrottler& HostFrameRateThrottler::GetInstance() {
  19. static base::NoDestructor<HostFrameRateThrottler> instance;
  20. return *instance;
  21. }
  22. HostFrameRateThrottler::HostFrameRateThrottler() = default;
  23. HostFrameRateThrottler::~HostFrameRateThrottler() = default;
  24. void HostFrameRateThrottler::AddHost(WindowTreeHost* host) {
  25. if (base::Contains(hosts_, host))
  26. return;
  27. hosts_.insert(host);
  28. UpdateHostFrameSinkManager();
  29. }
  30. void HostFrameRateThrottler::RemoveHost(WindowTreeHost* host) {
  31. if (!base::Contains(hosts_, host))
  32. return;
  33. hosts_.erase(host);
  34. UpdateHostFrameSinkManager();
  35. }
  36. void HostFrameRateThrottler::UpdateHostFrameSinkManager() {
  37. std::vector<viz::FrameSinkId> ids;
  38. ids.reserve(hosts_.size());
  39. for (WindowTreeHost* host : hosts_)
  40. ids.push_back(host->compositor()->frame_sink_id());
  41. Env::GetInstance()->context_factory()->GetHostFrameSinkManager()->Throttle(
  42. ids, base::Hertz(kDefaultThrottleFps));
  43. }
  44. } // namespace aura