1 /* 2 * Copyright (c) 2024 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_MULTI_APP_STRATEGY_H 17 #define HGM_MULTI_APP_STRATEGY_H 18 19 #include <algorithm> 20 #include <functional> 21 #include <tuple> 22 #include <unordered_map> 23 #include <unordered_set> 24 #include <vector> 25 26 #include "common/rs_common_def.h" 27 #include "hgm_command.h" 28 #include "hgm_lru_cache.h" 29 #include "hgm_touch_manager.h" 30 #include "pipeline/rs_render_frame_rate_linker.h" 31 32 namespace OHOS { 33 namespace Rosen { 34 constexpr int32_t DEFAULT_APP_TYPE = -1; 35 36 class HgmMultiAppStrategy final { 37 public: 38 using StrategyChangeCallback = std::function<void(const PolicyConfigData::StrategyConfig&)>; 39 40 struct TouchInfo { 41 std::string pkgName; 42 TouchState touchState; 43 int32_t upExpectFps; 44 }; 45 46 HgmMultiAppStrategy(); 47 ~HgmMultiAppStrategy() = default; 48 49 HgmErrCode HandlePkgsEvent(const std::vector<std::string>& pkgs); 50 void HandleTouchInfo(const TouchInfo& touchInfo); 51 void HandleLightFactorStatus(int32_t state); 52 void HandleLowAmbientStatus(bool isEffect); 53 void SetScreenType(bool isLtpo); 54 55 void CalcVote(); 56 HgmErrCode GetVoteRes(PolicyConfigData::StrategyConfig& strategyRes) const; 57 RegisterStrategyChangeCallback(const StrategyChangeCallback & callback)58 void RegisterStrategyChangeCallback(const StrategyChangeCallback& callback) 59 { 60 strategyChangeCallbacks_.emplace_back(callback); 61 } 62 bool CheckPidValid(pid_t pid, bool onlyCheckForegroundApp = false); 63 64 std::string GetGameNodeName(const std::string& pkgName); 65 std::string GetAppStrategyConfigName(const std::string& pkgName); 66 HgmErrCode GetFocusAppStrategyConfig(PolicyConfigData::StrategyConfig& strategyRes); GetPidAppType()67 std::unordered_map<std::string, std::pair<pid_t, int32_t>> GetPidAppType() const { return pidAppTypeMap_; } GetForegroundPidApp()68 std::unordered_map<pid_t, std::pair<int32_t, std::string>> GetForegroundPidApp() const 69 { 70 return foregroundPidAppMap_; 71 } GetBackgroundPid()72 HgmLRUCache<pid_t> GetBackgroundPid() const { return backgroundPid_; } GetPackages()73 std::vector<std::string> GetPackages() const { return pkgs_; } 74 void CleanApp(pid_t pid); 75 void UpdateXmlConfigCache(); GetScreenSetting()76 PolicyConfigData::ScreenSetting GetScreenSetting() const { return screenSettingCache_; } SetScreenSetting(const PolicyConfigData::ScreenSetting & screenSetting)77 void SetScreenSetting(const PolicyConfigData::ScreenSetting& screenSetting) { screenSettingCache_ = screenSetting; } GetStrategyConfigs()78 PolicyConfigData::StrategyConfigMap GetStrategyConfigs() const { return strategyConfigMapCache_; } SetStrategyConfigs(const PolicyConfigData::StrategyConfigMap & strategyConfigs)79 void SetStrategyConfigs(const PolicyConfigData::StrategyConfigMap& strategyConfigs) 80 { 81 strategyConfigMapCache_ = strategyConfigs; 82 } 83 HgmErrCode GetStrategyConfig(const std::string& strategyName, PolicyConfigData::StrategyConfig& strategyRes); 84 HgmErrCode GetAppStrategyConfig(const std::string& pkgName, PolicyConfigData::StrategyConfig& strategyRes); 85 86 static std::tuple<std::string, pid_t, int32_t> AnalyzePkgParam(const std::string& param); 87 88 // use in temporary scheme with background alpha 89 void CheckPackageInConfigList(const std::vector<std::string>& pkgs); SetDisableSafeVoteValue(bool disableSafeVote)90 void SetDisableSafeVoteValue(bool disableSafeVote) { disableSafeVote_ = disableSafeVote; } 91 private: 92 void UseStrategyNum(); 93 void FollowFocus(); 94 void UseMax(); 95 96 void OnLightFactor(PolicyConfigData::StrategyConfig& strategyRes) const; 97 void UpdateStrategyByTouch( 98 PolicyConfigData::StrategyConfig& strategy, const std::string& pkgName, bool forceUpdate = false); 99 void OnStrategyChange(); 100 101 std::vector<std::string> pkgs_; 102 std::unordered_map<std::string, std::pair<pid_t, int32_t>> pidAppTypeMap_; 103 std::unordered_map<pid_t, std::pair<int32_t, std::string>> foregroundPidAppMap_; 104 HgmLRUCache<pid_t> backgroundPid_{ 100 }; // max nums of pkgs that can be stored is 100 105 std::pair<HgmErrCode, PolicyConfigData::StrategyConfig> voteRes_ = { HGM_ERROR, { 106 .min = OledRefreshRate::OLED_NULL_HZ, 107 .max = OledRefreshRate::OLED_120_HZ, 108 .dynamicMode = DynamicModeType::TOUCH_ENABLED, 109 .idleFps = OledRefreshRate::OLED_NULL_HZ, 110 .drawMin = OledRefreshRate::OLED_NULL_HZ, 111 .drawMax = OledRefreshRate::OLED_120_HZ, 112 .down = OledRefreshRate::OLED_120_HZ, 113 }}; 114 TouchInfo touchInfo_ = { "", TouchState::IDLE_STATE, OLED_120_HZ }; // pkgName, touchState 115 std::unique_ptr<TouchInfo> uniqueTouchInfo_ = nullptr; 116 std::atomic<int32_t> lightFactorStatus_{ 0 }; 117 bool lowAmbientStatus_ = false; 118 bool isLtpo_ = true; 119 std::vector<StrategyChangeCallback> strategyChangeCallbacks_; 120 121 PolicyConfigData::ScreenSetting& screenSettingCache_; 122 PolicyConfigData::StrategyConfigMap& strategyConfigMapCache_; 123 bool disableSafeVote_ = false; 124 }; 125 } // namespace Rosen 126 } // namespace OHOS 127 #endif // HGM_MULTI_APP_STRATEGY_H