refresh_rate_throttle_controller.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "ash/display/refresh_rate_throttle_controller.h"
  5. #include "base/logging.h"
  6. #include "ui/display/display.h"
  7. #include "ui/display/types/display_constants.h"
  8. #include "ui/display/types/display_mode.h"
  9. #include "ui/display/types/display_snapshot.h"
  10. #include "ui/display/util/display_util.h"
  11. namespace ash {
  12. namespace {
  13. const float kLowBatteryThreshold = 5.0f;
  14. display::RefreshRateThrottleState GetDesiredThrottleState(
  15. const PowerStatus* status) {
  16. if (status->GetBatteryPercent() < kLowBatteryThreshold)
  17. return display::kRefreshRateThrottleEnabled;
  18. if (!status->IsMainsChargerConnected())
  19. return display::kRefreshRateThrottleEnabled;
  20. return display::kRefreshRateThrottleDisabled;
  21. }
  22. const display::DisplaySnapshot* GetInternalDisplay(
  23. display::DisplayConfigurator* configurator) {
  24. for (const display::DisplaySnapshot* snapshot :
  25. configurator->cached_displays()) {
  26. if (snapshot->type() == display::DISPLAY_CONNECTION_TYPE_INTERNAL)
  27. return snapshot;
  28. }
  29. return nullptr;
  30. }
  31. } // namespace
  32. RefreshRateThrottleController::RefreshRateThrottleController(
  33. display::DisplayConfigurator* display_configurator,
  34. PowerStatus* power_status)
  35. : power_status_observer_(this),
  36. display_configurator_(display_configurator),
  37. power_status_(power_status) {
  38. power_status_observer_.Observe(power_status);
  39. }
  40. RefreshRateThrottleController::~RefreshRateThrottleController() = default;
  41. void RefreshRateThrottleController::OnPowerStatusChanged() {
  42. const display::DisplaySnapshot* internal_display =
  43. GetInternalDisplay(display_configurator_);
  44. if (internal_display == nullptr) {
  45. VLOG(4) << "No internal display present.";
  46. return;
  47. }
  48. VLOG(4) << "Battery percent: " << power_status_->GetBatteryPercent()
  49. << ", High Power Charger: "
  50. << (power_status_->IsMainsChargerConnected() ? "yes" : "no");
  51. display::RefreshRateThrottleState state =
  52. GetDesiredThrottleState(power_status_);
  53. display_configurator_->MaybeSetRefreshRateThrottleState(
  54. internal_display->display_id(), state);
  55. }
  56. } // namespace ash