1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HGM_CORE_H 17 #define HGM_CORE_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <cinttypes> 22 #include <memory> 23 #include <thread> 24 #include <unordered_map> 25 #include <vector> 26 #include <unordered_set> 27 28 #include <event_handler.h> 29 30 #include "hgm_screen.h" 31 #include "vsync_type.h" 32 #include "vsync_generator.h" 33 #include "xml_parser.h" 34 35 namespace OHOS::Rosen { 36 class HgmFrameRateManager; 37 using RefreshRateModeChangeCallback = std::function<void(int32_t)>; 38 using RefreshRateUpdateCallback = std::function<void(int32_t)>; 39 class HgmCore final { 40 public: 41 static HgmCore& Instance(); 42 GetScreenIds()43 std::vector<ScreenId> GetScreenIds() const 44 { 45 return screenIds_; 46 } 47 IsEnabled()48 bool IsEnabled() const 49 { 50 return isEnabled_; 51 } 52 IsInit()53 bool IsInit() const 54 { 55 return isInit_.load(); 56 } 57 GetScreenListSize()58 int32_t GetScreenListSize() const 59 { 60 return screenList_.size(); 61 } 62 GetActiveScreenId()63 ScreenId GetActiveScreenId() const 64 { 65 return activeScreenId_; 66 } 67 GetPolicyConfigData()68 const std::shared_ptr<PolicyConfigData>& GetPolicyConfigData() const 69 { 70 return mPolicyConfigData_; 71 } 72 SetPendingScreenRefreshRate(uint32_t rate)73 void SetPendingScreenRefreshRate(uint32_t rate) 74 { 75 pendingScreenRefreshRate_ = rate; 76 } 77 GetPendingScreenRefreshRate()78 uint32_t GetPendingScreenRefreshRate() const 79 { 80 return pendingScreenRefreshRate_; 81 } 82 83 // called by HgmThread 84 // the rate takes effect at the latest hardware timing SetScreenRefreshRateImme(uint32_t rate)85 void SetScreenRefreshRateImme(uint32_t rate) 86 { 87 screenRefreshRateImme_.store(rate); 88 } 89 90 // called by HardwareThread GetScreenRefreshRateImme()91 uint32_t GetScreenRefreshRateImme() 92 { 93 // 0 means disenable 94 return screenRefreshRateImme_.exchange(0); 95 } 96 SetPendingConstraintRelativeTime(uint64_t relativeTime)97 void SetPendingConstraintRelativeTime(uint64_t relativeTime) 98 { 99 pendingConstraintRelativeTime_ = relativeTime; 100 } 101 GetPendingConstraintRelativeTime()102 uint64_t GetPendingConstraintRelativeTime() const 103 { 104 return pendingConstraintRelativeTime_; 105 } 106 SetTimestamp(uint64_t timestamp)107 void SetTimestamp(uint64_t timestamp) 108 { 109 timestamp_ = timestamp; 110 } 111 GetCurrentTimestamp()112 uint64_t GetCurrentTimestamp() const 113 { 114 return timestamp_; 115 } 116 GetLtpoEnabled()117 bool GetLtpoEnabled() const 118 { 119 return ltpoEnabled_ && (customFrameRateMode_ == HGM_REFRESHRATE_MODE_AUTO) && 120 (maxTE_ == VSYNC_MAX_REFRESHRATE); 121 } 122 IsVBlankIdleCorrectEnabled()123 bool IsVBlankIdleCorrectEnabled() const 124 { 125 return vBlankIdleCorrectSwitch_; 126 } 127 IsLowRateToHighQuickEnabled()128 bool IsLowRateToHighQuickEnabled() const 129 { 130 return lowRateToHighQuickSwitch_; 131 } 132 IsLTPOSwitchOn()133 bool IsLTPOSwitchOn() const 134 { 135 return ltpoEnabled_; 136 } 137 SetLtpoEnabled(bool ltpoEnabled)138 void SetLtpoEnabled(bool ltpoEnabled) 139 { 140 ltpoEnabled_ = ltpoEnabled; 141 } 142 GetAlignRate()143 uint32_t GetAlignRate() const 144 { 145 return alignRate_; 146 } 147 GetPipelineOffset()148 int64_t GetPipelineOffset() const 149 { 150 auto pulse = CreateVSyncGenerator()->GetVSyncPulse(); 151 return pipelineOffsetPulseNum_ * pulse; 152 } 153 GetSupportedMaxTE()154 uint32_t GetSupportedMaxTE() const 155 { 156 return maxTE_; 157 } 158 SetSupportedMaxTE(uint32_t maxTE)159 void SetSupportedMaxTE(uint32_t maxTE) 160 { 161 maxTE_ = maxTE; 162 } 163 GetDirectCompositionFlag()164 bool GetDirectCompositionFlag() const 165 { 166 return doDirectComposition_; 167 } 168 SetDirectCompositionFlag(bool doDirectComposition)169 void SetDirectCompositionFlag(bool doDirectComposition) 170 { 171 doDirectComposition_ = doDirectComposition; 172 } 173 174 // set refresh rates 175 int32_t SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate); 176 static int32_t SetRateAndResolution(ScreenId id, int32_t sceneId, int32_t rate, int32_t width, int32_t height); 177 int32_t SetRefreshRateMode(int32_t refreshRateMode); 178 179 void NotifyScreenPowerStatus(ScreenId id, ScreenPowerStatus status); 180 181 // screen interface 182 int32_t AddScreen(ScreenId id, int32_t defaultMode, ScreenSize& screenSize); 183 int32_t RemoveScreen(ScreenId id); 184 int32_t AddScreenInfo(ScreenId id, int32_t width, int32_t height, uint32_t rate, int32_t mode); 185 int32_t RefreshBundleName(const std::string& name); 186 uint32_t GetScreenCurrentRefreshRate(ScreenId id) const; 187 int32_t GetCurrentRefreshRateMode() const; 188 int32_t GetCurrentRefreshRateModeName() const; 189 sptr<HgmScreen> GetScreen(ScreenId id) const; 190 sptr<HgmScreen> GetActiveScreen() const; 191 std::vector<uint32_t> GetScreenSupportedRefreshRates(ScreenId id); 192 std::vector<int32_t> GetScreenComponentRefreshRates(ScreenId id); 193 std::unique_ptr<std::unordered_map<ScreenId, int32_t>> GetModesToApply(); 194 void SetActiveScreenId(ScreenId id); GetFrameRateMgr()195 std::shared_ptr<HgmFrameRateManager> GetFrameRateMgr() { return hgmFrameRateMgr_; }; 196 197 // for LTPO 198 void SetLtpoConfig(); 199 void SetScreenConstraintConfig(); 200 int64_t GetIdealPeriod(uint32_t rate); 201 void RegisterRefreshRateModeChangeCallback(const RefreshRateModeChangeCallback& callback); 202 void RegisterRefreshRateUpdateCallback(const RefreshRateUpdateCallback& callback); GetRefreshRateModeChangeCallback()203 RefreshRateModeChangeCallback GetRefreshRateModeChangeCallback() const 204 { 205 return refreshRateModeChangeCallback_; 206 } 207 GetRefreshRateUpdate()208 RefreshRateUpdateCallback GetRefreshRateUpdate() const 209 { 210 return refreshRateUpdateCallback_; 211 } 212 SetEnableDynamicMode(bool enableDynamicMode)213 void SetEnableDynamicMode(bool enableDynamicMode) 214 { 215 enableDynamicMode_ = enableDynamicMode; 216 } 217 GetEnableDynamicMode()218 bool GetEnableDynamicMode() const 219 { 220 return enableDynamicMode_; 221 } 222 private: 223 HgmCore(); 224 ~HgmCore() = default; 225 HgmCore(const HgmCore&) = delete; 226 HgmCore(const HgmCore&&) = delete; 227 HgmCore& operator=(const HgmCore&) = delete; 228 HgmCore& operator=(const HgmCore&&) = delete; 229 230 bool Init(); 231 void CheckCustomFrameRateModeValid(); 232 int32_t InitXmlConfig(); 233 int32_t SetCustomRateMode(int32_t mode); 234 235 bool isEnabled_ = true; 236 std::atomic<bool> isInit_{false}; 237 static constexpr char configFileProduct[] = "/sys_prod/etc/graphic/hgm_policy_config.xml"; 238 std::unique_ptr<XMLParser> mParser_ = nullptr; 239 std::shared_ptr<PolicyConfigData> mPolicyConfigData_ = nullptr; 240 241 int32_t customFrameRateMode_ = HGM_REFRESHRATE_MODE_AUTO; 242 std::vector<ScreenId> screenIds_; 243 std::vector<sptr<HgmScreen>> screenList_; 244 mutable std::mutex listMutex_; 245 246 mutable std::mutex modeListMutex_; 247 std::unique_ptr<std::unordered_map<ScreenId, int32_t>> modeListToApply_ = nullptr; 248 249 std::string currentBundleName_; 250 ScreenId activeScreenId_ = INVALID_SCREEN_ID; 251 std::unordered_set<SceneType> screenSceneSet_; 252 std::shared_ptr<HgmFrameRateManager> hgmFrameRateMgr_ = nullptr; 253 254 // for LTPO 255 uint32_t pendingScreenRefreshRate_ = 0; 256 std::atomic<uint32_t> screenRefreshRateImme_{ 0 }; 257 uint64_t pendingConstraintRelativeTime_ = 0; 258 uint64_t timestamp_ = 0; 259 bool ltpoEnabled_ = false; 260 uint32_t maxTE_ = 0; 261 uint32_t alignRate_ = 0; 262 int32_t pipelineOffsetPulseNum_ = 8; 263 bool vBlankIdleCorrectSwitch_ = false; 264 bool lowRateToHighQuickSwitch_ = false; 265 RefreshRateModeChangeCallback refreshRateModeChangeCallback_ = nullptr; 266 RefreshRateUpdateCallback refreshRateUpdateCallback_ = nullptr; 267 bool doDirectComposition_ = false; 268 bool enableDynamicMode_ = true; 269 }; 270 } // namespace OHOS::Rosen 271 #endif // HGM_CORE_H 272