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 HgmMultiAppStrategy(); 41 ~HgmMultiAppStrategy() = default; 42 43 struct TouchInfo { 44 std::string pkgName; 45 TouchState touchState; 46 int32_t upExpectFps; 47 }; 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 void SetAppStrategyConfig( 92 const std::string& pkgName, const std::vector<std::pair<std::string, std::string>>& newConfig); 93 void UpdateAppStrategyConfigCache(); 94 private: 95 void UseStrategyNum(); 96 void FollowFocus(); 97 void UseMax(); 98 99 void OnLightFactor(PolicyConfigData::StrategyConfig& strategyRes) const; 100 void UpdateStrategyByTouch( 101 PolicyConfigData::StrategyConfig& strategy, const std::string& pkgName, bool forceUpdate = false); 102 void OnStrategyChange(); 103 void HandleAppBufferStrategy(const std::string& configName, const std::string& configValue, 104 PolicyConfigData::StrategyConfig& appStrategyConfig); 105 106 std::vector<std::string> pkgs_; 107 std::unordered_map<std::string, std::pair<pid_t, int32_t>> pidAppTypeMap_; 108 std::unordered_map<pid_t, std::pair<int32_t, std::string>> foregroundPidAppMap_; 109 HgmLRUCache<pid_t> backgroundPid_{ 100 }; // max nums of pkgs that can be stored is 100 110 std::pair<HgmErrCode, PolicyConfigData::StrategyConfig> voteRes_ = { HGM_ERROR, { 111 .min = OledRefreshRate::OLED_NULL_HZ, 112 .max = OledRefreshRate::OLED_120_HZ, 113 .dynamicMode = DynamicModeType::TOUCH_ENABLED, 114 .idleFps = OledRefreshRate::OLED_NULL_HZ, 115 .drawMin = OledRefreshRate::OLED_NULL_HZ, 116 .drawMax = OledRefreshRate::OLED_120_HZ, 117 .down = OledRefreshRate::OLED_120_HZ, 118 }}; 119 TouchInfo touchInfo_ = { "", TouchState::IDLE_STATE, OLED_120_HZ }; // pkgName, touchState 120 std::unique_ptr<TouchInfo> uniqueTouchInfo_ = nullptr; 121 std::atomic<int32_t> lightFactorStatus_{ 0 }; 122 bool lowAmbientStatus_ = false; 123 bool isLtpo_ = true; 124 std::vector<StrategyChangeCallback> strategyChangeCallbacks_; 125 126 PolicyConfigData::ScreenSetting& screenSettingCache_; 127 PolicyConfigData::StrategyConfigMap& strategyConfigMapCache_; 128 bool disableSafeVote_ = false; 129 130 PolicyConfigData::StrategyConfigMap& appStrategyConfigMapCache_; 131 PolicyConfigData::StrategyConfigMap& appStrategyConfigMapPreCache_; 132 bool appStrategyConfigMapChanged_ = false; 133 std::vector<std::string>& appBufferListCache_; 134 }; 135 } // namespace Rosen 136 } // namespace OHOS 137 #endif // HGM_MULTI_APP_STRATEGY_H