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 RS_FRAME_RATE_VOTE_H
17 #define RS_FRAME_RATE_VOTE_H
18 
19 #include <iconsumer_surface.h>
20 
21 #include "rs_video_frame_rate_vote.h"
22 #include "ffrt.h"
23 #include "hgm_frame_rate_manager.h"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class RSFrameRateVote {
29     DECLARE_DELAYED_SINGLETON(RSFrameRateVote);
30 
31 public:
32     /**
33      * @brief vote frame rate by video rate
34      *
35      * @param surfaceNodeId the surfaceNode unique id
36      * @param sourceType the buffer queue type of the surfaceNode
37      * @param buffer the buffer queue
38      */
39     void VideoFrameRateVote(uint64_t surfaceNodeId, OHSurfaceSource sourceType, sptr<SurfaceBuffer>& buffer);
40 
41 private:
42     void ReleaseSurfaceMap(uint64_t surfaceNodeId);
43     void SurfaceVideoVote(uint64_t surfaceNodeId, uint32_t rate);
44     void VoteRate(pid_t pid, std::string eventName, uint32_t rate);
45     void CancelVoteRate(pid_t pid, std::string eventName);
46     void NotifyRefreshRateEvent(pid_t pid, EventInfo eventInfo);
47 
48 private:
49     bool isSwitchOn_ {false};
50     pid_t lastVotedPid_ {DEFAULT_PID};
51     uint32_t lastVotedRate_ {OLED_NULL_HZ};
52     bool isVoted_ {false};
53     std::shared_ptr<HgmFrameRateManager> frameRateMgr_ {nullptr};
54     std::unordered_map<uint64_t, std::shared_ptr<RSVideoFrameRateVote>> surfaceVideoFrameRateVote_ {};
55     std::unordered_map<uint64_t, uint32_t> surfaceVideoRate_ {};
56     std::mutex mutex_;
57     std::shared_ptr<ffrt::queue> ffrtQueue_ {nullptr};
58     ffrt::mutex ffrtMutex_;
59     ffrt::task_handle taskHandler_ {nullptr};
60 };
61 } // namespace Rosen
62 } // namespace OHOS
63 #endif // RS_FRAME_RATE_VOTE_H