File: display_performance_mode_controller.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,630 bytes parent folder | download | duplicates (6)
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
// 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_