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

ash / display / display_performance_mode_controller.h [blame]

// Copyright 2024 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_DISPLAY_PERFORMANCE_MODE_CONTROLLER_H_
#define ASH_DISPLAY_DISPLAY_PERFORMANCE_MODE_CONTROLLER_H_

#include "ash/ash_export.h"
#include "ash/system/power/power_status.h"
#include "base/scoped_observation.h"

namespace ash {

// DisplayPerformanceModeController listens to the power status change and the
// Display Performance Mode change in the UI to dictate what state the display
// features should be at. Display features that want to depend on the power and
// user preference would listen to this controller and update their state
// accordingly.
class ASH_EXPORT DisplayPerformanceModeController
    : public PowerStatus::Observer {
 public:
  // DisplayPerformanceModeController exposes 3 different modes that a client
  // observing a mode change gets:

  // kHighPerformance: This mode is enabled by the user that wants the best and
  // smoothest display experience. This mode does not necessarily indicate that
  // a power source is connected. However, in this mode, the display features
  // will prioritize performance over power efficiency.

  // kIntelligent: This mode represents an intelligent state for the display
  // features. Users can expect that the display features will dynamically
  // adjust their behavior based on the power status and user preferences. The
  // display features will strive to balance performance and power efficiency,
  // optimizing the user experience. This mode is the default mode.

  // kPowerSaver: This mode represents a power-saving state for the display
  // features. Users can expect that the display features will prioritize
  // power efficiency over performance. This mode is triggered by the system
  // Power Saver mode and is currently not user-configurable.
  enum class ModeState {
    kHighPerformance,
    kIntelligent,
    kPowerSaver,
    kDefault = kIntelligent
  };

  class Observer : public base::CheckedObserver {
   public:
    virtual void OnDisplayPerformanceModeChanged(ModeState new_state) = 0;

   protected:
    ~Observer() override = default;
  };

  explicit DisplayPerformanceModeController();
  DisplayPerformanceModeController(const DisplayPerformanceModeController&) =
      delete;
  DisplayPerformanceModeController& operator=(
      const DisplayPerformanceModeController&) = delete;
  ~DisplayPerformanceModeController() override;

  // When an observer is added, the current state should be sent to be up to
  // date.
  ModeState AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  void SetHighPerformanceModeByUser(bool is_high_performance_enabled);

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

  // For testing. Clients should observe the mode change instead of calling
  // this.
  ModeState GetCurrentStateForTesting() const { return current_state_; }

 private:
  void UpdateCurrentStateAndNotifyIfChanged();
  void NotifyObservers();

  ModeState current_state_ = ModeState::kIntelligent;
  bool is_high_performance_enabled_ = false;

  base::ObserverList<Observer> observers_;

  // Not owned.
  // TODO(b/327054689): This pointer is needed because some power tests delete
  // PowerStatus without the observers knowing about it, so we have to check for
  // its validity before using it.
  base::WeakPtr<PowerStatus> power_status_;

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

}  // namespace ash

#endif  // ASH_DISPLAY_DISPLAY_PERFORMANCE_MODE_CONTROLLER_H_