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_IDLE_DETECTOR_H
17 #define HGM_IDLE_DETECTOR_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 
22 namespace OHOS {
23 namespace Rosen {
24 
25 class HgmIdleDetector {
26 public:
27     static constexpr int32_t ANIMATOR_NOT_RUNNING = -1;
28 
29     HgmIdleDetector() = default;
30     ~HgmIdleDetector() = default;
31 
SetAppSupportedState(bool appSupported)32     void SetAppSupportedState(bool appSupported)
33     {
34         std::lock_guard<std::mutex> lock(appSupportedMutex_);
35         appSupported_ = appSupported;
36     }
37 
GetAppSupportedState()38     bool GetAppSupportedState()
39     {
40         std::lock_guard<std::mutex> lock(appSupportedMutex_);
41         return appSupported_;
42     }
43 
SetAceAnimatorIdleState(bool aceAnimatorIdleState)44     void SetAceAnimatorIdleState(bool aceAnimatorIdleState)
45     {
46         aceAnimatorIdleState_ = aceAnimatorIdleState;
47     }
48 
GetAceAnimatorIdleState()49     bool GetAceAnimatorIdleState() const
50     {
51         return aceAnimatorIdleState_;
52     }
53 
UpdateAceAnimatorExpectedFrameRate(int32_t aceAnimatorExpectedFrameRate)54     void UpdateAceAnimatorExpectedFrameRate(int32_t aceAnimatorExpectedFrameRate)
55     {
56         if (aceAnimatorExpectedFrameRate > aceAnimatorExpectedFrameRate_) {
57             aceAnimatorExpectedFrameRate_ = aceAnimatorExpectedFrameRate;
58         }
59     }
60 
GetAceAnimatorExpectedFrameRate()61     int32_t GetAceAnimatorExpectedFrameRate() const
62     {
63         return aceAnimatorExpectedFrameRate_;
64     }
65 
ResetAceAnimatorExpectedFrameRate()66     void ResetAceAnimatorExpectedFrameRate()
67     {
68         aceAnimatorExpectedFrameRate_ = ANIMATOR_NOT_RUNNING;
69     }
70 
71     void UpdateSurfaceTime(const std::string& surfaceName, uint64_t timestamp,  pid_t pid);
72     bool GetSurfaceIdleState(uint64_t timestamp);
73     int32_t GetTouchUpExpectedFPS();
74     bool ThirdFrameNeedHighRefresh();
ClearAppBufferList()75     void ClearAppBufferList()
76     {
77         std::lock_guard<std::mutex> lock(appBufferListMutex_);
78         appBufferList_.clear();
79     }
ClearAppBufferBlackList()80     void ClearAppBufferBlackList()
81     {
82         std::lock_guard<std::mutex> lock(appBufferBlackListMutex_);
83         appBufferBlackList_.clear();
84     }
UpdateAppBufferList(std::vector<std::pair<std::string,int32_t>> & appBufferList)85     void UpdateAppBufferList(std::vector<std::pair<std::string, int32_t>> &appBufferList)
86     {
87         std::lock_guard<std::mutex> lock(appBufferListMutex_);
88         appBufferList_ = appBufferList;
89     }
UpdateAppBufferBlackList(std::vector<std::string> & appBufferBlackList)90     void UpdateAppBufferBlackList(std::vector<std::string> &appBufferBlackList)
91     {
92         std::lock_guard<std::mutex> lock(appBufferBlackListMutex_);
93         appBufferBlackList_ = appBufferBlackList;
94     }
UpdateSupportAppBufferList(std::vector<std::string> & supportAppBufferList)95     void UpdateSupportAppBufferList(std::vector<std::string> &supportAppBufferList)
96     {
97         supportAppBufferList_ = supportAppBufferList;
98     }
99 private:
100     bool appSupported_ = false;
101     bool aceAnimatorIdleState_ = true;
102     int32_t aceAnimatorExpectedFrameRate_ = ANIMATOR_NOT_RUNNING;
103     std::mutex appSupportedMutex_;
104     std::mutex appBufferListMutex_;
105     std::mutex appBufferBlackListMutex_;
106     // FORMAT: <buffername>
107     std::vector<std::string> appBufferBlackList_;
108     std::vector<std::string> supportAppBufferList_;
109     // FORMAT: <buffername, time>
110     std::unordered_map<std::string, uint64_t> frameTimeMap_;
111     // FORMAT: <buffername, fps>
112     std::vector<std::pair<std::string, int32_t>> appBufferList_;
113 };
114 } // namespace Rosen
115 } // namespace OHOS
116 #endif // HGM_IDLE_DETECTOR_H
117