1 /*
2  * Copyright (c) 2023 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 #include "frame_rate_manager.h"
16 
17 namespace OHOS::Ace::NG {
IsRateChanged()18 bool FrameRateManager::IsRateChanged()
19 {
20     return isRateChanged_;
21 }
22 
SetIsRateChanged(bool isChanged)23 void FrameRateManager::SetIsRateChanged(bool isChanged)
24 {
25     isRateChanged_ = isChanged;
26 }
27 
AddNodeRate(int32_t nodeId,int32_t rate)28 void FrameRateManager::AddNodeRate(int32_t nodeId, int32_t rate)
29 {
30     auto [iter, success] = nodeRateMap_.try_emplace(nodeId, rate);
31     if (success) {
32         isRateChanged_ = true;
33     }
34 }
35 
RemoveNodeRate(int32_t nodeId)36 void FrameRateManager::RemoveNodeRate(int32_t nodeId)
37 {
38     if (auto iter = nodeRateMap_.find(nodeId); iter != nodeRateMap_.end()) {
39         nodeRateMap_.erase(iter);
40         isRateChanged_ = true;
41     }
42 }
43 
UpdateNodeRate(int32_t nodeId,int32_t rate)44 void FrameRateManager::UpdateNodeRate(int32_t nodeId, int32_t rate)
45 {
46     if (auto iter = nodeRateMap_.find(nodeId); iter != nodeRateMap_.end() && iter->second != rate) {
47         iter->second = rate;
48         isRateChanged_ = true;
49     }
50 }
51 
SetAnimateRate(int32_t rate)52 void FrameRateManager::SetAnimateRate(int32_t rate)
53 {
54     if (animateRate_ != rate) {
55         animateRate_ = rate;
56         isRateChanged_ = true;
57     }
58 }
59 
SetDisplaySyncRate(int32_t displaySyncRate)60 void FrameRateManager::SetDisplaySyncRate(int32_t displaySyncRate)
61 {
62     if (displaySyncRate_ != displaySyncRate) {
63         displaySyncRate_ = displaySyncRate;
64         isRateChanged_ = true;
65     }
66 }
67 
GetDisplaySyncRate() const68 int32_t FrameRateManager::GetDisplaySyncRate() const
69 {
70     return displaySyncRate_;
71 }
72 
GetExpectedRate()73 std::pair<int32_t, int32_t> FrameRateManager::GetExpectedRate()
74 {
75     int32_t expectedRate = 0;
76     int32_t rateType = 0;
77     if (!nodeRateMap_.empty()) {
78         auto maxIter = std::max_element(
79             nodeRateMap_.begin(), nodeRateMap_.end(), [](auto a, auto b) { return a.second < b.second; });
80         expectedRate = maxIter->second;
81         rateType = ACE_COMPONENT_FRAME_RATE_TYPE;
82     }
83     if (displaySyncRate_ > expectedRate) {
84         expectedRate = displaySyncRate_;
85         rateType = DISPLAY_SYNC_FRAME_RATE_TYPE;
86     }
87     if (animateRate_ > expectedRate) {
88         expectedRate = animateRate_;
89         rateType = UI_ANIMATION_FRAME_RATE_TYPE;
90     }
91     return {expectedRate, rateType};
92 }
93 } // namespace OHOS::Ace::NG