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_COMMAND_H 17 #define HGM_COMMAND_H 18 19 #include <cinttypes> 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "hgm_update_callback.h" 26 #include "screen_manager/screen_types.h" 27 #include "animation/rs_frame_rate_range.h" 28 29 namespace OHOS::Rosen { 30 31 constexpr int UNI_APP_PID = -1; 32 constexpr int32_t HGM_REFRESHRATE_MODE_AUTO = -1; 33 constexpr pid_t DEFAULT_PID = 0; 34 constexpr int ADAPTIVE_SYNC_ENABLED = 1; 35 constexpr int32_t SWITCH_SCREEN_SCENE = 1; 36 constexpr int32_t STRING_BUFFER_MAX_SIZE = 256; 37 constexpr int64_t IDEAL_PULSE = 2777778; // 2.777778ms 38 const std::string HGM_CONFIG_TYPE_THERMAL_SUFFIX = "_THERMAL"; 39 const std::string HGM_CONFIG_TYPE_DRAGSLIDE_SUFFIX = "_DRAGSLIDE"; 40 const std::string HGM_CONFIG_TYPE_THROWSLIDE_SUFFIX = "_THROWSLIDE"; 41 // {Suffix, {Priority, State}} 42 const std::unordered_map<std::string, std::pair<int32_t, bool>> HGM_CONFIG_SCREENEXT_STRATEGY_MAP = { 43 {HGM_CONFIG_TYPE_THERMAL_SUFFIX, {1, false}}, 44 {HGM_CONFIG_TYPE_DRAGSLIDE_SUFFIX, {2, false}}, 45 {HGM_CONFIG_TYPE_THROWSLIDE_SUFFIX, {3, false}}, 46 }; 47 48 enum OledRefreshRate { 49 OLED_NULL_HZ = 0, 50 OLED_MIN_HZ = 1, 51 OLED_1_HZ = 1, 52 OLED_10_HZ = 10, 53 OLED_20_HZ = 20, 54 OLED_30_HZ = 30, 55 OLED_40_HZ = 40, 56 OLED_60_HZ = 60, 57 OLED_90_HZ = 90, 58 OLED_120_HZ = 120, 59 OLED_144_HZ = 144, 60 OLED_MAX_HZ = 1000, 61 }; 62 63 enum HgmErrCode { 64 HGM_ERROR = -1, 65 66 EXEC_SUCCESS = 0, 67 68 HGM_NO_SCREEN = 100, 69 HGM_BASE_REMOVE_FAILED, 70 71 XML_PARSER_CREATE_FAIL = 200, 72 XML_FILE_LOAD_FAIL, 73 XML_GET_ROOT_FAIL, 74 XML_PARSE_INTERNAL_FAIL, 75 76 HGM_SCREEN_MODE_EXIST, 77 HGM_SCREEN_PARAM_ERROR, 78 FINAL_RANGE_NOT_VALID, 79 }; 80 81 enum class HgmXmlNode { 82 HGM_XML_UNDEFINED = 0, 83 HGM_XML_PARAM, 84 HGM_XML_PARAMS, 85 }; 86 87 enum class SceneType { 88 SCREEN_RECORD, 89 }; 90 91 enum class PointerModeType : int32_t { 92 POINTER_DISENABLED = 0, 93 POINTER_ENABLED = 1, 94 }; 95 96 enum class DynamicModeType : int32_t { 97 TOUCH_DISENABLED = 0, 98 TOUCH_ENABLED = 1, 99 TOUCH_EXT_ENABLED = 2, // touch extend program 100 TOUCH_EXT_ENABLED_LTPO_FIRST = 4, // diff with 2:touch up 100ms period if has VOTER_LTPO then skip VOTER_TOUCH 101 }; 102 103 enum class MultiAppStrategyType { 104 USE_MAX, 105 FOLLOW_FOCUS, 106 USE_STRATEGY_NUM, 107 }; 108 109 class PolicyConfigData { 110 public: 111 PolicyConfigData() = default; 112 ~PolicyConfigData() = default; 113 114 struct StrategyConfig { 115 int32_t min; 116 int32_t max; 117 DynamicModeType dynamicMode; 118 PointerModeType pointerMode; 119 int32_t idleFps; 120 bool isFactor; 121 int32_t drawMin; 122 int32_t drawMax; 123 int32_t down; 124 // Does this game app require Adaptive Sync? Yes/No/Skip Required 125 int32_t supportAS; 126 // milliseconds 127 int32_t upTimeOut; 128 // <bufferName, fps> 129 std::unordered_map<std::string, int32_t> bufferFpsMap; 130 }; 131 // <"1", StrategyConfig> 132 using StrategyConfigMap = std::unordered_map<std::string, StrategyConfig>; 133 134 struct SceneConfig { 135 std::string strategy; 136 std::string priority; 137 bool doNotAutoClear; 138 bool disableSafeVote; 139 }; 140 // <"SCENE_APP_START_ANIMATION", SceneConfig> 141 using SceneConfigMap = std::unordered_map<std::string, SceneConfig>; 142 143 // <"LowBright", <30, 60, 120>> 144 using SupportedModeConfig = std::unordered_map<std::string, std::vector<uint32_t>>; 145 // <"LTPO-DEFAULT", SupportedModeConfig> 146 using SupportedModeMap = std::unordered_map<std::string, SupportedModeConfig>; 147 148 struct DynamicConfig { 149 int32_t min; 150 int32_t max; 151 int32_t preferredFps; 152 }; 153 // <"1", DynamicConfig> 154 using DynamicSetting = std::unordered_map<std::string, DynamicConfig>; 155 // <"translate", DynamicSetting> 156 using DynamicSettingMap = std::unordered_map<std::string, DynamicSetting>; 157 158 using PageUrlConfig = std::unordered_map<std::string, std::string>; 159 using PageUrlConfigMap = std::unordered_map<std::string, PageUrlConfig>; 160 std::vector<std::string> pageNameList_; 161 162 struct ScreenSetting { 163 std::string strategy; 164 // <"switch", "1"> 165 std::unordered_map<std::string, std::string> ltpoConfig; 166 // <"pkgName", "4"> 167 std::unordered_map<std::string, std::string> appList; 168 MultiAppStrategyType multiAppStrategyType; 169 std::string multiAppStrategyName; 170 // <appType, strategyName> 171 std::unordered_map<int32_t, std::string> appTypes; 172 SceneConfigMap sceneList; 173 // <SCENE_APP_START_ANIMATION, placeholder> 174 std::unordered_map<std::string, std::string> gameSceneList; 175 DynamicSettingMap animationDynamicSettings; 176 DynamicSettingMap aceSceneDynamicSettings; 177 int32_t smallSizeArea = -1; 178 int32_t smallSizeLength = -1; 179 DynamicSettingMap smallSizeAnimationDynamicSettings; 180 // <CONFIG_NAME, VALUE> 181 std::unordered_map<std::string, std::string> animationPowerConfig; 182 // <rateTypeName, idleFps> 183 std::unordered_map<std::string, std::string> uiPowerConfig; 184 // <SCENE_APP_START_ANIMATION, placeholder> 185 SceneConfigMap ancoSceneList; 186 // <componentCode, idleFps> 187 std::unordered_map<std::string, int32_t> componentPowerConfig; 188 // <"pkgName", "UnityPlayerSurface"> 189 std::unordered_map<std::string, std::string> gameAppNodeList; 190 // <"pageName", min, max> 191 PageUrlConfigMap pageUrlConfig; 192 std::unordered_map<std::string, std::string> performanceConfig; 193 }; 194 // <"-1", ScreenSetting> 195 using ScreenConfig = std::unordered_map<std::string, ScreenSetting>; 196 // <"LTPO-DEFAULT", ScreenConfig> 197 using ScreenConfigMap = std::unordered_map<std::string, ScreenConfig>; 198 199 std::string defaultRefreshRateMode_ = "-1"; 200 // <"120", "1"> 201 std::vector<std::pair<int32_t, int32_t>> refreshRateForSettings_; 202 std::unordered_map<std::string, std::vector<std::pair<int32_t, int32_t>>> refreshRateForSettingsMap_; 203 std::vector<std::string> appBufferList_; 204 bool xmlCompatibleMode_ = false; 205 bool safeVoteEnabled = true; 206 // <"VIRTUAL_AXX", "4"> 207 std::unordered_map<std::string, std::string> virtualDisplayConfigs_; 208 bool virtualDisplaySwitch_; 209 // <"screen0_LTPO", "LTPO-DEFAULT"> 210 std::unordered_map<std::string, std::string> screenStrategyConfigs_; 211 std::unordered_map<std::string, std::string> sourceTuningConfig_; 212 std::unordered_map<std::string, std::string> solidLayerConfig_; 213 std::unordered_map<std::string, std::string> hwcSourceTuningConfig_; 214 std::unordered_map<std::string, std::string> hwcSolidLayerConfig_; 215 // <"up_timeout_ms", 3000> 216 std::unordered_map<std::string, std::string> timeoutStrategyConfig_; 217 std::unordered_map<std::string, std::string> videoCallLayerConfig_; 218 StrategyConfigMap strategyConfigs_; 219 ScreenConfigMap screenConfigs_; 220 SupportedModeMap supportedModeConfigs_; 221 bool videoFrameRateVoteSwitch_ = false; 222 // <"pkgName", "1"> 223 std::unordered_map<std::string, std::string> videoFrameRateList_; 224 // vrate <"minifps", "1"> 225 std::unordered_map<std::string, std::string> vRateControlList_; 226 GetAceSceneDynamicSettingMap(const std::string & screenType,const std::string & settingMode)227 DynamicSettingMap GetAceSceneDynamicSettingMap(const std::string& screenType, const std::string& settingMode) 228 { 229 if (screenConfigs_.count(screenType) && screenConfigs_[screenType].count(settingMode)) { 230 return screenConfigs_[screenType][settingMode].aceSceneDynamicSettings; 231 } else { 232 return {}; 233 } 234 } 235 SettingModeId2XmlModeId(int32_t settingModeId)236 int32_t SettingModeId2XmlModeId(int32_t settingModeId) const 237 { 238 // return 0 when input is invalid 239 bool hasAutoConfig = refreshRateForSettings_.size() > 0 && 240 refreshRateForSettings_.at(0).first == HGM_REFRESHRATE_MODE_AUTO; 241 if (settingModeId == HGM_REFRESHRATE_MODE_AUTO) { 242 if (!hasAutoConfig) { 243 return 0; 244 } 245 return refreshRateForSettings_.at(0).second; 246 } 247 // The settingModeId must be -1 and 1, 2, 3, 4..., no 0. When there is no -1 in refreshRateForSettings_, 248 // 1 corresponds to refreshRateForSettings_[0], and 2 corresponds to refreshRateForSettings_[1] and so on 249 if (!hasAutoConfig) { 250 settingModeId--; 251 } 252 if (settingModeId < 0 || settingModeId >= static_cast<int32_t>(refreshRateForSettings_.size())) { 253 return 0; 254 } 255 return refreshRateForSettings_[settingModeId].second; 256 } 257 XmlModeId2SettingModeId(const std::string & xmlModeId)258 int32_t XmlModeId2SettingModeId(const std::string& xmlModeId) const 259 { 260 auto iter = std::find_if(refreshRateForSettings_.begin(), refreshRateForSettings_.end(), 261 [=] (auto nameModeId) { return std::to_string(nameModeId.second) == xmlModeId; }); 262 if (iter == refreshRateForSettings_.end()) { 263 return 0; 264 } 265 auto ret = static_cast<int32_t>(iter - refreshRateForSettings_.begin()); 266 // at(0).first == -1 means HGM_REFRESHRATE_MODE_AUTO is configured 267 if (refreshRateForSettings_.at(0).first != HGM_REFRESHRATE_MODE_AUTO) { 268 return ret + 1; 269 } 270 // HGM_REFRESHRATE_MODE_AUTO must be at(0), Only -1 is usable among negative numbers. 271 if (ret == 0) { 272 return HGM_REFRESHRATE_MODE_AUTO; 273 } else { 274 return ret; 275 } 276 } 277 GetRefreshRateModeName(int32_t refreshRateModeId)278 int32_t GetRefreshRateModeName(int32_t refreshRateModeId) 279 { 280 auto iter = std::find_if(refreshRateForSettings_.begin(), refreshRateForSettings_.end(), 281 [&](auto nameModeId) { return nameModeId.second == refreshRateModeId; }); 282 if (iter != refreshRateForSettings_.end()) { 283 return iter->first; 284 } 285 return 0; 286 } 287 UpdateRefreshRateForSettings(const std::string mode)288 void UpdateRefreshRateForSettings(const std::string mode) 289 { 290 auto it = refreshRateForSettingsMap_.find(mode); 291 if (it != refreshRateForSettingsMap_.end()) { 292 refreshRateForSettings_ = it->second; 293 } 294 } 295 }; 296 297 class PolicyConfigVisitor : public HgmUpdateCallback { 298 public: 299 PolicyConfigVisitor() = default; 300 ~PolicyConfigVisitor() override = default; 301 302 virtual const PolicyConfigData& GetXmlData() const = 0; 303 virtual void SetSettingModeId(int32_t settingModeId) = 0; 304 virtual void SetXmlModeId(const std::string& xmlModeId) = 0; 305 virtual void ChangeScreen(const std::string& screenConfigType) = 0; 306 307 virtual HgmErrCode GetStrategyConfig(const std::string& strategyName, 308 PolicyConfigData::StrategyConfig& strategyRes) const = 0; 309 310 virtual const PolicyConfigData::ScreenSetting& GetScreenSetting() const = 0; 311 virtual const PolicyConfigData::DynamicSettingMap& GetAceSceneDynamicSettingMap() const = 0; 312 313 virtual HgmErrCode GetAppStrategyConfig(const std::string& pkgName, 314 int32_t appType, 315 PolicyConfigData::StrategyConfig& strategyRes) const = 0; 316 317 virtual HgmErrCode GetDynamicAppStrategyConfig(const std::string& pkgName, 318 PolicyConfigData::StrategyConfig& strategyRes) const = 0; 319 320 virtual std::string GetGameNodeName(const std::string& pkgName) const = 0; 321 }; 322 323 class PolicyConfigVisitorImpl : public PolicyConfigVisitor { 324 public: 325 explicit PolicyConfigVisitorImpl(const PolicyConfigData& configData); 326 ~PolicyConfigVisitorImpl() override = default; 327 328 const PolicyConfigData& GetXmlData() const override; 329 void SetSettingModeId(int32_t settingModeId) override; 330 void SetXmlModeId(const std::string& xmlModeId) override; 331 void ChangeScreen(const std::string& screenConfigType) override; 332 333 HgmErrCode GetStrategyConfig(const std::string& strategyName, 334 PolicyConfigData::StrategyConfig& strategyRes) const override; 335 336 const PolicyConfigData::ScreenSetting& GetScreenSetting() const override; 337 const PolicyConfigData::DynamicSettingMap& GetAceSceneDynamicSettingMap() const override; 338 339 HgmErrCode GetAppStrategyConfig(const std::string& pkgName, 340 int32_t appType, 341 PolicyConfigData::StrategyConfig& strategyRes) const override; 342 343 HgmErrCode GetDynamicAppStrategyConfig(const std::string& pkgName, 344 PolicyConfigData::StrategyConfig& strategyRes) const override; 345 346 std::string GetGameNodeName(const std::string& pkgName) const override; 347 348 protected: 349 std::optional<std::string> SettingModeId2XmlModeId(int32_t settingModeId) const; 350 std::optional<int32_t> XmlModeId2SettingModeId(const std::string& xmlModeId) const; 351 int32_t GetRefreshRateModeName(int32_t refreshRateModeId) const; 352 std::string GetAppStrategyConfigName(const std::string& pkgName, int32_t appType) const; 353 354 const PolicyConfigData& configData_; 355 int32_t settingModeId_{ 0 }; 356 std::string xmlModeId_{ "-1" }; // auto mode 357 std::string screenConfigType_{ "LTPO-DEFAULT" }; 358 }; 359 } // namespace OHOS 360 #endif // HGM_COMMAND_H