1 /* 2 * Copyright (c) 2025 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_FRAME_VOTE_H 17 #define HGM_FRAME_VOTE_H 18 19 #include <unordered_map> 20 #include <unordered_set> 21 22 #include "hgm_multi_app_strategy.h" 23 #include "hgm_voter.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 class HgmFrameVoter final { 28 public: 29 using ChangeRangeCallbackFunc = std::function<void(const std::string&)>; 30 using UpdateVoteRuleFunc = std::function<void(std::vector<std::string>&)>; // voters 31 using CheckVoteFunc = std::function<bool(const std::string&, VoteInfo&)>; // vote, voteInfo 32 33 explicit HgmFrameVoter(HgmMultiAppStrategy& multiAppStrategy); 34 ~HgmFrameVoter() = default; 35 GetVoteRecord()36 const VoteRecord& GetVoteRecord() const { return voteRecord_; } GetVoters()37 const std::vector<std::string>& GetVoters() const { return voters_; } GetVoterGamesEffective()38 bool GetVoterGamesEffective() const { return voterGamesEffective_.load(); } 39 IsDragScene()40 bool IsDragScene() const { return isDragScene_; } SetDragScene(bool isDragScene)41 void SetDragScene(bool isDragScene) { isDragScene_ = isDragScene; } SetIsTouchUpLTPOFirstPeriod(bool isTouchUpLTPOFirstPeriod)42 void SetIsTouchUpLTPOFirstPeriod(bool isTouchUpLTPOFirstPeriod) 43 { 44 isTouchUpLTPOFirstPeriod_ = isTouchUpLTPOFirstPeriod; 45 } SetTouchUpLTPOFirstDynamicMode(DynamicModeType dynamicMode)46 void SetTouchUpLTPOFirstDynamicMode(DynamicModeType dynamicMode) 47 { 48 isTouchUpLTPOFirstDynamicMode_ = (dynamicMode == DynamicModeType::TOUCH_EXT_ENABLED_LTPO_FIRST); 49 } 50 51 void CleanVote(pid_t pid); 52 void DeliverVote(const VoteInfo& voteInfo, bool eventStatus); 53 std::pair<VoteInfo, VoteRange> ProcessVote(const std::string& curScreenStrategyId, 54 ScreenId curScreenId, int32_t curRefreshRateMode); 55 SetChangeRangeCallback(const ChangeRangeCallbackFunc & func)56 void SetChangeRangeCallback(const ChangeRangeCallbackFunc& func) { markVoteChange_ = func; } SetUpdateVoteRuleCallback(const UpdateVoteRuleFunc & func)57 void SetUpdateVoteRuleCallback(const UpdateVoteRuleFunc& func) { updateVoteRule_ = func; } SetCheckVoteCallback(const CheckVoteFunc & func)58 void SetCheckVoteCallback(const CheckVoteFunc& func) { checkVote_ = func; } 59 60 private: 61 void ProcessVoteLog(const VoteInfo& curVoteInfo, bool isSkip); 62 bool MergeLtpo2IdleVote( 63 std::vector<std::string>::iterator& voterIter, VoteInfo& resultVoteInfo, VoteRange& mergedVoteRange); 64 bool ProcessVoteIter(std::vector<std::string>::iterator& voterIter, 65 VoteInfo& resultVoteInfo, VoteRange& voteRange, bool& voterGamesEffective); 66 void MarkVoteChange(const std::string& voter = "") 67 { 68 if (markVoteChange_) { 69 markVoteChange_(voter); 70 } 71 } NeedSkipVoterTouch(bool existVoterLTPO)72 bool NeedSkipVoterTouch(bool existVoterLTPO) 73 { 74 if (existVoterLTPO && isTouchUpLTPOFirstPeriod_ && isTouchUpLTPOFirstDynamicMode_) { 75 return true; 76 } 77 return false; 78 } 79 80 bool isDragScene_ = false; 81 bool isTouchUpLTPOFirstPeriod_ = false; 82 bool isTouchUpLTPOFirstDynamicMode_ = false; 83 std::atomic<bool> voterGamesEffective_ = false; 84 std::unordered_set<pid_t> pidRecord_; 85 VoteRecord voteRecord_; 86 std::vector<std::string> voters_; 87 88 HgmMultiAppStrategy& multiAppStrategy_; 89 90 ChangeRangeCallbackFunc markVoteChange_{ nullptr }; 91 // param: voters 92 UpdateVoteRuleFunc updateVoteRule_{ nullptr }; 93 // return: false means vote is invalid, should be skip 94 CheckVoteFunc checkVote_{ nullptr }; 95 }; 96 } // namespace Rosen 97 } // namespace OHOS 98 #endif // HGM_FRAME_VOTE_H