• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     Occlusion::Region visibleRegionBehindWindow;
40     RectI maxVisRect;
41     int appWindowArea = 0;
42 };
43 
44 class RSVsyncRateReduceManager {
45 public:
RSVsyncRateReduceManager()46     RSVsyncRateReduceManager() {};
47     ~RSVsyncRateReduceManager() = default;
48 
49     void SetFocusedNodeId(NodeId focusedNodeId);
50     void PushWindowNodeId(NodeId nodeId);
51     void ClearLastVisMapForVsyncRate();
GetVRateReduceEnabled()52     bool GetVRateReduceEnabled() const
53     {
54         return vRateReduceEnabled_;
55     }
56 
57     void FrameDurationBegin();
58     void FrameDurationEnd();
59 
GetIsReduceBySystemAnimatedScenes()60     bool GetIsReduceBySystemAnimatedScenes() const
61     {
62         return isReduceBySystemAnimatedScenes_;
63     }
64     void SetIsReduceBySystemAnimatedScenes(bool isReduceBySystemAnimatedScenes);
65     void Init(const sptr<VSyncDistributor>& appVSyncDistributor);
66     void ResetFrameValues(uint32_t refreshRate);
67     void CollectSurfaceVsyncInfo(const ScreenInfo& screenInfo, RSSurfaceRenderNode& node);
68     void SetUniVsync();
69 
70     void SetVSyncRateByVisibleLevel(std::map<NodeId, RSVisibleLevel>& pidVisMap,
71         std::vector<RSBaseRenderNode::SharedPtr>& curAllSurfaces);
72 
GetVrateMap()73     std::map<uint64_t, int> GetVrateMap()
74     {
75         return linkersRateMap_;
76     }
77 
SetVSyncRatesChangeStatus(bool newState)78     bool SetVSyncRatesChangeStatus(bool newState)
79     {
80         return needPostTask_.exchange(newState);
81     }
82 
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     static constexpr int RS_REFRESH_RATE_BEHIND_WINDOW = 30;
123     std::mutex mutexFrameDuration_;
124     std::deque<float> frameDurations_;
125 
126     bool isSystemAnimatedScenes_ = false;
127     bool isReduceBySystemAnimatedScenes_ = false;
128 
129     bool isDeviceSupprotVRate_ = false;
130     std::map<NodeId, SurfaceVRateInfo> surfaceVRateMap_;
131     sptr<VSyncDistributor> appVSyncDistributor_ = nullptr;
132     std::map<NodeId, uint64_t> windowLinkerMap_;
133     std::map<uint64_t, int> linkersRateMap_;
134     std::atomic<bool> needPostTask_{ false };
135 };
136 
137 } // namespace Rosen
138 } // namespace OHOS
139 
140 #endif // RENDER_SERVICE_VSYNC_RATE_REDUCE_H
141