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 int32_t FrameRateManager::GetExpectedRate()
74 {
75 int32_t expectedRate = 0;
76 if (!nodeRateMap_.empty()) {
77 auto maxIter = std::max_element(
78 nodeRateMap_.begin(), nodeRateMap_.end(), [](auto a, auto b) { return a.second < b.second; });
79 expectedRate = maxIter->second;
80 }
81 expectedRate = std::max(expectedRate, displaySyncRate_);
82 expectedRate = std::max(expectedRate, animateRate_);
83 return expectedRate;
84 }
85 } // namespace OHOS::Ace::NG