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_SOFT_VSYNC_MANAGER_H 17 #define HGM_SOFT_VSYNC_MANAGER_H 18 19 #include <unordered_map> 20 #include <vector> 21 22 #include "common/rs_common_def.h" 23 #include "hgm_command.h" 24 #include "hgm_energy_consumption_policy.h" 25 #include "hgm_voter.h" 26 #include "hgm_vsync_generator_controller.h" 27 #include "pipeline/rs_render_frame_rate_linker.h" 28 #include "variable_frame_rate/rs_variable_frame_rate.h" 29 #include "vsync_distributor.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 using WindowId = uint64_t; 34 using VsyncName = std::string; 35 using FrameRateLinkerMap = std::unordered_map<FrameRateLinkerId, std::shared_ptr<RSRenderFrameRateLinker>>; 36 37 class HgmSoftVSyncManager { 38 public: 39 HgmSoftVSyncManager(); 40 ~HgmSoftVSyncManager() = default; 41 42 void SetWindowExpectedRefreshRate(pid_t pid, 43 const std::unordered_map<WindowId, EventInfo>& voters); 44 void SetWindowExpectedRefreshRate(pid_t pid, 45 const std::unordered_map<VsyncName, EventInfo>& voters); 46 bool CollectFrameRateChange(FrameRateRange finalRange, std::shared_ptr<RSRenderFrameRateLinker> rsFrameRateLinker, 47 const FrameRateLinkerMap& appFrameRateLinkers, const uint32_t currRefreshRate); 48 std::vector<std::pair<FrameRateLinkerId, uint32_t>> GetSoftAppChangeData(); 49 void UniProcessDataForLtpo(const std::map<uint64_t, int>& vRatesMap, 50 const FrameRateLinkerMap& appFrameRateLinkers); 51 void InitController(std::weak_ptr<HgmVSyncGeneratorController> controller, 52 sptr<VSyncDistributor> appDistributor); ChangePerformanceFirst(bool isPerformanceFirst)53 void ChangePerformanceFirst(bool isPerformanceFirst) 54 { 55 isPerformanceFirst_.store(isPerformanceFirst); 56 } GetControllerRate()57 uint32_t GetControllerRate() const 58 { 59 return controllerRate_; 60 } 61 // called by hgmFrameRateManager 62 void SetVsyncRateDiscountLTPO(const std::vector<uint64_t>& linkerIds, uint32_t rateDiscount); 63 void SetQosVSyncRate(const uint32_t currRefreshRate, const FrameRateLinkerMap& appFrameRateLinkers); 64 // Vrate 65 void GetVRateMiniFPS(const std::shared_ptr<PolicyConfigData>& configData); 66 void EraseGameRateDiscountMap(pid_t pid); 67 private: 68 void Reset(); 69 void HandleLinkers(); 70 void DeliverSoftVote(FrameRateLinkerId linkerId, const VoteInfo& voteInfo, bool eventStatus); 71 // vrate voting to hgm linkerId means that frameLinkerid, appFrameRate means that vrate 72 bool CollectVRateChange(uint64_t linkerId, FrameRateRange& appFrameRate); 73 bool CollectGameRateDiscountChange(uint64_t linkerId, FrameRateRange& expectedRange, 74 const uint32_t currRefreshRate); 75 static uint32_t GetDrawingFrameRate(const uint32_t refreshRate, const FrameRateRange& range); 76 void CalcAppFrameRate( 77 const std::pair<FrameRateLinkerId, std::shared_ptr<RSRenderFrameRateLinker>>& linker, 78 FrameRateRange& expectedRange, 79 bool& frameRateChanged, 80 bool controllerRateChanged, 81 const uint32_t currRefreshRate); 82 83 // FORMAT: "frameRateLinkerId, <voterName, <<pid, <min, max>>, effective>>" 84 std::unordered_map<uint64_t, std::shared_ptr<HgmVoter>> linkerVoteMap_; 85 std::unordered_map<WindowId, FrameRateLinkerId> winLinkerMap_; 86 std::unordered_map<VsyncName, std::vector<FrameRateLinkerId>> vsyncLinkerMap_; 87 std::unordered_map<FrameRateLinkerId, uint32_t> appVoteData_; 88 std::unordered_map<FrameRateLinkerId, uint32_t> appChangeData_; 89 std::weak_ptr<HgmVSyncGeneratorController> controller_; 90 // linkerid is key, vrate is value 91 std::map<uint64_t, int> vRatesMap_; 92 // Vrate 93 // defalut value is 1, visiable lower than 10%. 94 int32_t vrateControlMinifpsValue_ = 1; 95 sptr<VSyncDistributor> appDistributor_ = nullptr; 96 FrameRateLinkerMap appFrameRateLinkers_; 97 // FORMAT: <linkerid, rateDiscount> 98 std::unordered_map<uint64_t, uint32_t> gameRateDiscountMap_; 99 100 uint32_t controllerRate_ = 0; 101 102 std::atomic<bool> isPerformanceFirst_; 103 }; 104 } // namespace Rosen 105 } // namespace OHOS 106 #endif // HGM_SOFT_VSYNC_MANAGER_H