1 /* 2 * Copyright (c) 2024-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 RENDER_SERVICE_VSYNC_RATE_REDUCE_MANAGER_H 17 #define RENDER_SERVICE_VSYNC_RATE_REDUCE_MANAGER_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <cstdio> 22 #include <chrono> 23 #include <screen_manager/rs_screen_info.h> 24 #include "common/rs_common_def.h" 25 #include "common/rs_occlusion_region.h" 26 #include "pipeline/rs_render_node.h" 27 #include "pipeline/rs_surface_render_node.h" 28 #include "system/rs_system_parameters.h" 29 #include "visitor/rs_node_visitor.h" 30 #include "vsync_distributor.h" 31 32 namespace OHOS { 33 namespace Rosen { 34 35 struct SurfaceVRateInfo { 36 NodeId nodeId = 0; 37 std::string name; 38 Occlusion::Region visibleRegion; 39 RectI maxVisRect; 40 int appWindowArea = 0; 41 }; 42 43 class RSVsyncRateReduceManager { 44 public: RSVsyncRateReduceManager()45 RSVsyncRateReduceManager() {}; 46 ~RSVsyncRateReduceManager() = default; 47 48 void SetFocusedNodeId(NodeId focusedNodeId); 49 void PushWindowNodeId(NodeId nodeId); 50 void ClearLastVisMapForVsyncRate(); GetVRateReduceEnabled()51 bool GetVRateReduceEnabled() const 52 { 53 return vRateReduceEnabled_; 54 } 55 56 void FrameDurationBegin(); 57 void FrameDurationEnd(); 58 GetIsReduceBySystemAnimatedScenes()59 bool GetIsReduceBySystemAnimatedScenes() const 60 { 61 return isReduceBySystemAnimatedScenes_; 62 } 63 void SetIsReduceBySystemAnimatedScenes(bool isReduceBySystemAnimatedScenes); 64 void Init(const sptr<VSyncDistributor>& appVSyncDistributor); 65 void ResetFrameValues(uint32_t refreshRate); 66 void CollectSurfaceVsyncInfo(const ScreenInfo& screenInfo, RSSurfaceRenderNode& node); 67 void SetUniVsync(); 68 69 void SetVSyncRateByVisibleLevel(std::map<NodeId, RSVisibleLevel>& pidVisMap, 70 std::vector<RSBaseRenderNode::SharedPtr>& curAllSurfaces); 71 GetVrateMap()72 std::map<uint64_t, int> GetVrateMap() 73 { 74 return linkersRateMap_; 75 } 76 SetVSyncRatesChangeStatus(bool newState)77 bool SetVSyncRatesChangeStatus(bool newState) 78 { 79 return needPostTask_.exchange(newState); 80 } 81 82 bool GetVRateIsSupport(); GetVRateDeviceSupport()83 bool GetVRateDeviceSupport() const 84 { 85 return isDeviceSupprotVRate_; 86 } 87 private: 88 void NotifyVRates(); 89 int UpdateRatesLevel(); 90 void CalcRates(); 91 bool CheckNeedNotify(); 92 int GetRateByBalanceLevel(double vVal); 93 void EnqueueFrameDuration(float duration); 94 void NotifyVSyncRates(const std::map<NodeId, RSVisibleLevel>& vSyncRates); 95 static inline Occlusion::Rect GetMaxVerticalRect(const Occlusion::Region& region); 96 static Occlusion::Rect CalcMaxVisibleRect(const Occlusion::Region& region, int appWindowArea); 97 static float CalcVValByAreas(int windowArea, int maxVisRectArea, int visTotalArea); 98 99 uint64_t Now(); SetVSyncRatesChanged(bool vSyncRatesChanged)100 void SetVSyncRatesChanged(bool vSyncRatesChanged) 101 { 102 vSyncRatesChanged_ = vSyncRatesChanged_ || vSyncRatesChanged; 103 } 104 private: 105 bool vRateReduceEnabled_ = false; 106 bool vRateConditionQualified_ = false; 107 bool vSyncRatesChanged_ = false; 108 std::map<NodeId, int> vSyncRateMap_; 109 std::map<NodeId, int> lastVSyncRateMap_; 110 std::vector<NodeId> curAllMainAndLeashWindowNodesIds_; 111 std::vector<NodeId> lastAllMainAndLeashWindowNodesIds_; 112 std::map<NodeId, RSVisibleLevel> visMapForVSyncVisLevel_; 113 std::map<NodeId, RSVisibleLevel> lastVisMapForVSyncVisLevel_; 114 115 NodeId focusedNodeId_ = 0; 116 NodeId lastFocusedNodeId_ = 0; 117 118 uint64_t curTime_ = 0; 119 int curRatesLevel_ = 0; 120 int64_t oneFramePeriod_ = 0; 121 uint32_t rsRefreshRate_ = 0; 122 std::mutex mutexFrameDuration_; 123 std::deque<float> frameDurations_; 124 125 bool isSystemAnimatedScenes_ = false; 126 bool isReduceBySystemAnimatedScenes_ = false; 127 128 bool isDeviceSupprotVRate_ = false; 129 std::map<NodeId, SurfaceVRateInfo> surfaceVRateMap_; 130 sptr<VSyncDistributor> appVSyncDistributor_ = nullptr; 131 std::map<NodeId, uint64_t> windowLinkerMap_; 132 std::map<uint64_t, int> linkersRateMap_; 133 std::atomic<bool> needPostTask_{ false }; 134 }; 135 136 } // namespace Rosen 137 } // namespace OHOS 138 139 #endif // RENDER_SERVICE_VSYNC_RATE_REDUCE_H 140