• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "window_rate_manager.h"
17 
18 #include "vsync_station.h"
19 #include "window_frame_trace.h"
20 #include "window_manager_hilog.h"
21 #include "ui/rs_frame_rate_linker.h"
22 
23 using namespace FRAME_TRACE;
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowRateManager"};
29 }
WM_IMPLEMENT_SINGLE_INSTANCE(WindowRateManager)30 WM_IMPLEMENT_SINGLE_INSTANCE(WindowRateManager)
31 
32 void WindowRateManager::FlushFrameRate(int32_t windowId, uint32_t rate)
33 {
34     std::lock_guard<std::mutex> lock(mtx_);
35     if (auto iter = windowRateMap_.find(windowId); iter != windowRateMap_.end() && iter->second != rate) {
36         iter->second = rate;
37         expectedRate_ = GetExpectedRate();
38         VsyncStation::GetInstance().FlushFrameRate(expectedRate_, false);
39     }
40 }
41 
FlushFrameRateForRootWindow(uint32_t rate)42 void WindowRateManager::FlushFrameRateForRootWindow(uint32_t rate)
43 {
44     std::lock_guard<std::mutex> lock(mtx_);
45     if (rate != rootWindowRate_) {
46         rootWindowRate_ = rate;
47         expectedRate_ = GetExpectedRate();
48         VsyncStation::GetInstance().FlushFrameRate(expectedRate_, false);
49     }
50 }
51 
AddWindowRate(int32_t windowId,uint32_t rate)52 void WindowRateManager::AddWindowRate(int32_t windowId, uint32_t rate)
53 {
54     std::lock_guard<std::mutex> lock(mtx_);
55     if (windowRateMap_.count(windowId)) {
56         return;
57     }
58     windowRateMap_.emplace(windowId, rate);
59     WLOGD("WindowRateManager::AddWindowRate id: %{public}d", windowId);
60 }
61 
RemoveWindowRate(int32_t windowId)62 void WindowRateManager::RemoveWindowRate(int32_t windowId)
63 {
64     std::lock_guard<std::mutex> lock(mtx_);
65     if (auto iter = windowRateMap_.find(windowId); iter != windowRateMap_.end()) {
66         auto rate = iter->second;
67         windowRateMap_.erase(iter);
68         WLOGD("WindowRateManager::RemoveWindowRate id: %{public}d", windowId);
69         if (rate == expectedRate_) {
70             expectedRate_ = GetExpectedRate();
71             VsyncStation::GetInstance().FlushFrameRate(expectedRate_, true);
72         }
73     }
74 }
75 
GetExpectedRate()76 uint32_t WindowRateManager::GetExpectedRate()
77 {
78     uint32_t expectedRate = 0;
79     if (!windowRateMap_.empty()) {
80         auto iter = std::max_element(
81             windowRateMap_.begin(), windowRateMap_.end(), [](auto a, auto b) { return a.second < b.second; });
82         expectedRate = iter->second;
83     }
84     expectedRate = std::max(expectedRate, rootWindowRate_);
85     return expectedRate;
86 }
87 } // namespace Rosen
88 } // namespace OHOS
89