Overte C++ Documentation
RefreshRateManager.h
1 //
2 // RefreshRateManager.h
3 // interface/src/
4 //
5 // Created by Dante Ruiz on 2019-04-15.
6 // Copyright 2019 High Fidelity, Inc.
7 // Copyright 2022-2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
14 #ifndef hifi_RefreshRateManager_h
15 #define hifi_RefreshRateManager_h
16 
17 #include <map>
18 #include <string>
19 #include <functional>
20 
21 #include <QTimer>
22 
23 #include <SettingHandle.h>
24 #include <ScriptEngineCast.h>
25 #include <ScriptManager.h>
26 #include <shared/ReadWriteLockable.h>
27 
28 class RefreshRateManager : QObject {
29  Q_OBJECT
30 public:
31  enum RefreshRateProfile {
32  ECO = 0,
33  INTERACTIVE,
34  REALTIME,
35  CUSTOM,
36  PROFILE_NUM
37  };
38  Q_ENUM(RefreshRateProfile)
39  static bool isValidRefreshRateProfile(RefreshRateProfile value) { return (value >= 0 && value < RefreshRateProfile::PROFILE_NUM); }
40 
41  /*@jsdoc
42  * <p>Interface states that affect the refresh rate.</p>
43  * <table>
44  * <thead>
45  * <tr><th>Value</th><th>Name</th><th>Description</th>
46  * </thead>
47  * <tbody>
48  * <tr><td><code>0</code></td><td>FOCUS_ACTIVE</td><td>Interface has focus and the user is active or is in VR.</td></tr>
49  * <tr><td><code>1</code></td><td>FOCUS_INACTIVE</td><td>Interface has focus and the user is inactive.</td></tr>
50  * <tr><td><code>2</code></td><td>UNFOCUS</td><td>Interface doesn't have focus.</td></tr>
51  * <tr><td><code>3</code></td><td>MINIMIZED</td><td>Interface is minimized.</td></tr>
52  * <tr><td><code>4</code></td><td>STARTUP</td><td>Interface is starting up.</td></tr>
53  * <tr><td><code>5</code></td><td>SHUTDOWN</td><td>Interface is shutting down.</td></tr>
54  * </tbody>
55  * </table>
56  * @typedef {number} RefreshRateRegime
57  */
58  enum RefreshRateRegime {
59  FOCUS_ACTIVE = 0,
60  FOCUS_INACTIVE,
61  UNFOCUS,
62  MINIMIZED,
63  STARTUP,
64  SHUTDOWN,
65  REGIME_NUM
66  };
67  Q_ENUM(RefreshRateRegime)
68  static bool isValidRefreshRateRegime(RefreshRateRegime value) { return (value >= RefreshRateRegime::FOCUS_ACTIVE && value <= RefreshRateRegime::SHUTDOWN); }
69 
70  /*@jsdoc
71  * <p>User experience (UX) modes.</p>
72  * <table>
73  * <thead>
74  * <tr><th>Value</th><th>Name</th><th>Description</th>
75  * </thead>
76  * <tbody>
77  * <tr><td><code>0</code></td><td>DESKTOP</td><td>Desktop user experience.</td></tr>
78  * <tr><td><code>1</code></td><td>VR</td><td>VR use experience.</td></tr>
79  * </tbody>
80  * </table>
81  * @typedef {number} UXMode
82  */
83  enum UXMode {
84  DESKTOP = 0,
85  VR,
86  UX_NUM
87  };
88  Q_ENUM(UXMode)
89  static bool isValidUXMode(UXMode value) { return (value >= UXMode::DESKTOP && value <= UXMode::VR); }
90 
91  RefreshRateManager();
92  ~RefreshRateManager() = default;
93 
94  void setRefreshRateProfile(RefreshRateProfile refreshRateProfile);
95  RefreshRateProfile getRefreshRateProfile() const;
96 
97  void setRefreshRateRegime(RefreshRateRegime refreshRateRegime);
98  RefreshRateRegime getRefreshRateRegime() const;
99 
100  void setUXMode(UXMode uxMode);
101  UXMode getUXMode() const { return _uxMode; }
102 
103  void setRefreshRateOperator(std::function<void(int)> refreshRateOperator) { _refreshRateOperator = refreshRateOperator; }
104  int getActiveRefreshRate() const { return _activeRefreshRate; }
105  void updateRefreshRateController() const;
106 
107  // query the refresh rate target at the specified combination
108  int queryRefreshRateTarget(RefreshRateProfile profile, RefreshRateRegime regime, UXMode uxMode) const;
109 
110  int getCustomRefreshRate(RefreshRateRegime regime);
111  void setCustomRefreshRate(RefreshRateRegime regime, int value);
112 
113  void resetInactiveTimer();
114  void toggleInactive();
115 
116  static std::string refreshRateProfileToString(RefreshRateProfile refreshRateProfile);
117  static RefreshRateProfile refreshRateProfileFromString(std::string refreshRateProfile);
118  static std::string uxModeToString(UXMode uxMode);
119  static std::string refreshRateRegimeToString(RefreshRateRegime refreshRateRegime);
120 
121 private:
122  mutable int _activeRefreshRate { 20 };
123  RefreshRateProfile _refreshRateProfile { RefreshRateProfile::INTERACTIVE};
124  RefreshRateRegime _refreshRateRegime { RefreshRateRegime::STARTUP };
125  UXMode _uxMode { UXMode::DESKTOP };
126 
127  mutable ReadWriteLockable _refreshRateProfileSettingLock;
128  Setting::Handle<int> _refreshRateProfileSetting{ "refreshRateProfile", RefreshRateProfile::INTERACTIVE };
129  std::array<Setting::Handle<int>, REGIME_NUM> _customRefreshRateSettings { {
130  { "customRefreshRateFocusActive", 60 },
131  { "customRefreshRateFocusInactive", 60 },
132  { "customRefreshRateUnfocus", 60 },
133  { "customRefreshRateMinimized", 2 },
134  { "customRefreshRateStartup", 30 },
135  { "customRefreshRateShutdown", 30 }
136  } };
137 
138  std::function<void(int)> _refreshRateOperator { nullptr };
139 
140  std::shared_ptr<QTimer> _inactiveTimer { std::make_shared<QTimer>() };
141 };
142 
143 #endif