1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124

ash / display / refresh_rate_controller.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_DISPLAY_REFRESH_RATE_CONTROLLER_H_
#define ASH_DISPLAY_REFRESH_RATE_CONTROLLER_H_

#include <vector>

#include "ash/ash_export.h"
#include "ash/display/display_performance_mode_controller.h"
#include "ash/system/power/power_status.h"
#include "base/scoped_observation.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tree_host_observer.h"
#include "ui/display/display_observer.h"
#include "ui/display/manager/display_configurator.h"

namespace ash {


// RefreshRateController manages features related to display refresh rate, such
// as the VRR enabled/disabled state and refresh rate throttling. It is responsible
// for communicating desired state to the configurator.
class ASH_EXPORT RefreshRateController
    : public PowerStatus::Observer,
      public aura::WindowObserver,
      public display::DisplayObserver,
      public display::DisplayConfigurator::Observer,
      public DisplayPerformanceModeController::Observer,
      public aura::WindowTreeHostObserver {
 public:
  RefreshRateController(
      display::DisplayConfigurator* display_configurator,
      PowerStatus* power_status,
      DisplayPerformanceModeController* display_performance_mode_controller);

  RefreshRateController(const RefreshRateController&) = delete;
  RefreshRateController& operator=(const RefreshRateController&) = delete;

  ~RefreshRateController() override;

  // PowerStatus::Observer implementation.
  void OnPowerStatusChanged() override;

  // Set Game mode on the window.
  void SetGameMode(aura::Window* window, bool game_mode_on);

  // WindowObserver implementation.
  void OnWindowAddedToRootWindow(aura::Window* window) override;
  void OnWindowDestroying(aura::Window* window) override;

  // DisplayObserver implementation.
  void OnDisplayMetricsChanged(const display::Display& display,
                               uint32_t changed_metrics) override;

  // DisplayConfigurator::Observer implementation.
  void OnDisplayConfigurationChanged(
      const display::DisplayConfigurator::DisplayStateList& displays) override;

  // DisplayPerformanceModeController::Observer implementation.
  void OnDisplayPerformanceModeChanged(
      DisplayPerformanceModeController::ModeState new_state) override;

  // WindowTreeHostObserver implementation.
  void OnSetPreferredRefreshRate(aura::WindowTreeHost* host,
                                 float preferred_refresh_rate) override;

  void StopObservingPowerStatusForTest();

  void OnWindowTreeHostCreated(aura::WindowTreeHost* host);

 private:
  // The requested state for refresh rate throttling.
  enum class ThrottleState {
    kEnabled,
    kDisabled,
  };

  void UpdateSeamlessRefreshRates(int64_t display_id);
  void OnSeamlessRefreshRatesReceived(
      int64_t display_id,
      const std::optional<std::vector<float>>& refresh_rates);

  void UpdateStates();
  void RefreshOverrideState();
  void RefreshVrrState();
  ThrottleState GetDesiredThrottleState();
  ThrottleState GetDynamicThrottleState();
  // Returns a refresh rates override map populated according to the desired
  // throttle state.
  display::DisplayConfigurator::RefreshRateOverrideMap GetThrottleOverrides();

  // Not owned.
  raw_ptr<display::DisplayConfigurator> display_configurator_;
  raw_ptr<PowerStatus> power_status_;

  raw_ptr<DisplayPerformanceModeController>
      display_performance_mode_controller_;
  DisplayPerformanceModeController::DisplayPerformanceModeController::ModeState
      current_performance_mode_ = DisplayPerformanceModeController::
          DisplayPerformanceModeController::ModeState::kDefault;

  bool force_throttle_ = false;

  std::unordered_map<int64_t, std::vector<float>> display_refresh_rates_;
  std::unordered_map<int64_t, float> refresh_rate_preferences_;

  base::ScopedObservation<ash::PowerStatus, ash::PowerStatus::Observer>
      power_status_observer_{this};
  base::ScopedObservation<aura::Window, aura::WindowObserver>
      game_window_observer_{this};
  display::ScopedDisplayObserver display_observer_{this};
  base::ScopedObservation<display::DisplayConfigurator,
                          display::DisplayConfigurator::Observer>
      display_configurator_observer_{this};

  base::WeakPtrFactory<RefreshRateController> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // ASH_DISPLAY_REFRESH_RATE_CONTROLLER_H_