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 <functional>
20 #include <mutex>
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(bool isSafe);
52 
53     void CalcVote();
54     HgmErrCode GetVoteRes(PolicyConfigData::StrategyConfig& strategyRes) const;
55 
56     void RegisterStrategyChangeCallback(const StrategyChangeCallback& callback);
57     bool CheckPidValid(pid_t pid);
58 
59     std::string GetAppStrategyConfigName(const std::string& pkgName);
60     HgmErrCode GetFocusAppStrategyConfig(PolicyConfigData::StrategyConfig& strategyRes);
61     std::unordered_map<std::string, std::pair<pid_t, int32_t>> GetPidAppType();
62     std::unordered_map<pid_t, std::pair<int32_t, std::string>> GetForegroundPidApp();
63     HgmLRUCache<pid_t> GetBackgroundPid();
64     std::vector<std::string> GetPackages();
65     void CleanApp(pid_t pid);
66     void UpdateXmlConfigCache();
67     PolicyConfigData::ScreenSetting GetScreenSetting();
68     void SetScreenSetting(const PolicyConfigData::ScreenSetting& screenSetting);
69     PolicyConfigData::StrategyConfigMap GetStrategyConfigs();
70     void SetStrategyConfigs(const PolicyConfigData::StrategyConfigMap& strategyConfigs);
71     HgmErrCode GetStrategyConfig(const std::string& strategyName, PolicyConfigData::StrategyConfig& strategyRes);
72     HgmErrCode GetAppStrategyConfig(const std::string& pkgName, PolicyConfigData::StrategyConfig& strategyRes);
73 
74     static std::tuple<std::string, pid_t, int32_t> AnalyzePkgParam(const std::string& param);
75 
76     // use in temporary scheme with background alpha
77     void CheckPackageInConfigList(const std::vector<std::string>& pkgs);
78 private:
79     void UseStrategyNum();
80     void FollowFocus();
81     void UseMax();
82 
83     void OnLightFactor(PolicyConfigData::StrategyConfig& strategyRes) const;
84     void UpdateStrategyByTouch(
85         PolicyConfigData::StrategyConfig& strategy, const std::string& pkgName, bool forceUpdate = false);
86     void OnStrategyChange();
87 
88     std::mutex pkgsMutex_;
89     std::vector<std::string> pkgs_;
90     std::unordered_map<std::string, std::pair<pid_t, int32_t>> pidAppTypeMap_;
91     std::unordered_map<pid_t, std::pair<int32_t, std::string>> foregroundPidAppMap_;
92     HgmLRUCache<pid_t> backgroundPid_{ 100 }; // max nums of pkgs that can be stored is 100
93     std::mutex pidAppTypeMutex_;
94     std::pair<HgmErrCode, PolicyConfigData::StrategyConfig> voteRes_ = { HGM_ERROR, {
95         .min = OledRefreshRate::OLED_NULL_HZ,
96         .max = OledRefreshRate::OLED_120_HZ,
97         .dynamicMode = DynamicModeType::TOUCH_ENABLED,
98         .drawMin = OledRefreshRate::OLED_NULL_HZ,
99         .drawMax = OledRefreshRate::OLED_120_HZ,
100         .down = OledRefreshRate::OLED_120_HZ,
101     }};
102     std::mutex touchInfoMutex_;
103     TouchInfo touchInfo_ = { "", TouchState::IDLE_STATE, OLED_120_HZ }; // pkgName, touchState
104     std::unique_ptr<TouchInfo> uniqueTouchInfo_ = nullptr;
105     std::atomic<bool> lightFactorStatus_{false};
106     std::vector<StrategyChangeCallback> strategyChangeCallbacks_;
107 
108     std::mutex updateCacheMutex_;
109     PolicyConfigData::ScreenSetting& screenSettingCache_;
110     PolicyConfigData::StrategyConfigMap& strategyConfigMapCache_;
111     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
112 };
113 } // namespace Rosen
114 } // namespace OHOS
115 #endif // HGM_MULTI_APP_STRATEGY_H