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 class HgmCore final { 39 public: 40 static HgmCore& Instance(); 41 GetScreenIds()42 std::vector<ScreenId> GetScreenIds() const 43 { 44 return screenIds_; 45 } 46 IsEnabled()47 bool IsEnabled() const 48 { 49 return isEnabled_; 50 } 51 IsInit()52 bool IsInit() const 53 { 54 return isInit_; 55 } 56 GetScreenListSize()57 int32_t GetScreenListSize() const 58 { 59 return screenList_.size(); 60 } 61 GetActiveScreenId()62 ScreenId GetActiveScreenId() const 63 { 64 return activeScreenId_; 65 } 66 GetPolicyConfigData()67 std::shared_ptr<PolicyConfigData> GetPolicyConfigData() const 68 { 69 return mPolicyConfigData_; 70 } 71 SetPendingScreenRefreshRate(uint32_t rate)72 void SetPendingScreenRefreshRate(uint32_t rate) 73 { 74 pendingScreenRefreshRate_ = rate; 75 } 76 GetPendingScreenRefreshRate()77 uint32_t GetPendingScreenRefreshRate() const 78 { 79 return pendingScreenRefreshRate_; 80 } 81 SetTimestamp(uint64_t timestamp)82 void SetTimestamp(uint64_t timestamp) 83 { 84 timestamp_ = timestamp; 85 } 86 GetCurrentTimestamp()87 uint64_t GetCurrentTimestamp() const 88 { 89 return timestamp_; 90 } 91 GetLtpoEnabled()92 bool GetLtpoEnabled() const 93 { 94 return ltpoEnabled_ && (customFrameRateMode_ == HGM_REFRESHRATE_MODE_AUTO) && 95 (maxTE_ == VSYNC_MAX_REFRESHRATE); 96 } 97 IsLTPOSwitchOn()98 bool IsLTPOSwitchOn() const 99 { 100 return ltpoEnabled_; 101 } 102 SetLtpoEnabled(bool ltpoEnabled)103 void SetLtpoEnabled(bool ltpoEnabled) 104 { 105 ltpoEnabled_ = ltpoEnabled; 106 } 107 GetAlignRate()108 uint32_t GetAlignRate() const 109 { 110 return alignRate_; 111 } 112 GetPipelineOffset()113 int64_t GetPipelineOffset() const 114 { 115 auto pulse = CreateVSyncGenerator()->GetVSyncPulse(); 116 return pipelineOffsetPulseNum_ * pulse; 117 } 118 GetSupportedMaxTE()119 uint32_t GetSupportedMaxTE() const 120 { 121 return maxTE_; 122 } 123 SetSupportedMaxTE(uint32_t maxTE)124 void SetSupportedMaxTE(uint32_t maxTE) 125 { 126 maxTE_ = maxTE; 127 } 128 129 // set refresh rates 130 int32_t SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate); 131 static int32_t SetRateAndResolution(ScreenId id, int32_t sceneId, int32_t rate, int32_t width, int32_t height); 132 int32_t SetRefreshRateMode(RefreshRateMode refreshRateMode); 133 134 void NotifyScreenPowerStatus(ScreenId id, ScreenPowerStatus status); 135 136 // screen interface 137 int32_t AddScreen(ScreenId id, int32_t defaultMode, ScreenSize& screenSize); 138 int32_t RemoveScreen(ScreenId id); 139 int32_t AddScreenInfo(ScreenId id, int32_t width, int32_t height, uint32_t rate, int32_t mode); 140 int32_t RefreshBundleName(const std::string& name); 141 uint32_t GetScreenCurrentRefreshRate(ScreenId id) const; 142 int32_t GetCurrentRefreshRateMode() const; 143 sptr<HgmScreen> GetScreen(ScreenId id) const; 144 sptr<HgmScreen> GetActiveScreen() const; 145 std::vector<uint32_t> GetScreenSupportedRefreshRates(ScreenId id); 146 std::vector<int32_t> GetScreenComponentRefreshRates(ScreenId id); 147 std::unique_ptr<std::unordered_map<ScreenId, int32_t>> GetModesToApply(); 148 void SetActiveScreenId(ScreenId id); GetFrameRateMgr()149 std::shared_ptr<HgmFrameRateManager> GetFrameRateMgr() { return hgmFrameRateMgr_; }; 150 151 // for LTPO 152 void SetLtpoConfig(); 153 int64_t GetIdealPeriod(uint32_t rate); 154 void RegisterRefreshRateModeChangeCallback(const RefreshRateModeChangeCallback& callback); GetRefreshRateModeChangeCallback()155 RefreshRateModeChangeCallback GetRefreshRateModeChangeCallback() const 156 { 157 return refreshRateModeChangeCallback_; 158 } 159 private: 160 HgmCore(); 161 ~HgmCore() = default; 162 HgmCore(const HgmCore&) = delete; 163 HgmCore(const HgmCore&&) = delete; 164 HgmCore& operator=(const HgmCore&) = delete; 165 HgmCore& operator=(const HgmCore&&) = delete; 166 167 bool Init(); 168 int32_t InitXmlConfig(); 169 int32_t SetCustomRateMode(RefreshRateMode mode); 170 171 bool isEnabled_ = true; 172 bool isInit_ = false; 173 static constexpr char configFileProduct[] = "/sys_prod/etc/graphic/hgm_policy_config.xml"; 174 std::unique_ptr<XMLParser> mParser_; 175 std::shared_ptr<PolicyConfigData> mPolicyConfigData_ = nullptr; 176 177 RefreshRateMode customFrameRateMode_ = HGM_REFRESHRATE_MODE_AUTO; 178 std::vector<ScreenId> screenIds_; 179 std::vector<sptr<HgmScreen>> screenList_; 180 mutable std::mutex listMutex_; 181 182 mutable std::mutex modeListMutex_; 183 std::unique_ptr<std::unordered_map<ScreenId, int32_t>> modeListToApply_ = nullptr; 184 185 std::string currentBundleName_; 186 ScreenId activeScreenId_ = INVALID_SCREEN_ID; 187 std::unordered_set<SceneType> screenSceneSet_; 188 std::shared_ptr<HgmFrameRateManager> hgmFrameRateMgr_ = nullptr; 189 190 // for LTPO 191 uint32_t pendingScreenRefreshRate_ = 0; 192 uint64_t timestamp_ = 0; 193 bool ltpoEnabled_ = false; 194 uint32_t maxTE_ = 0; 195 uint32_t alignRate_ = 0; 196 int32_t pipelineOffsetPulseNum_ = 8; 197 RefreshRateModeChangeCallback refreshRateModeChangeCallback_ = nullptr; 198 }; 199 } // namespace OHOS::Rosen 200 #endif // HGM_CORE_H 201