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 
16 #include "rs_realtime_refresh_rate_manager.h"
17 
18 #include <chrono>
19 #include <condition_variable>
20 #include <mutex>
21 
22 #include "hgm_core.h"
23 #include "hgm_frame_rate_manager.h"
24 #include "rs_trace.h"
25 #include "rs_main_thread.h"
26 
27 namespace OHOS::Rosen {
Instance()28 RSRealtimeRefreshRateManager& RSRealtimeRefreshRateManager::Instance()
29 {
30     static RSRealtimeRefreshRateManager instance;
31     return instance;
32 }
33 
SetShowRefreshRateEnabled(bool enable)34 void RSRealtimeRefreshRateManager::SetShowRefreshRateEnabled(bool enable)
35 {
36     std::unique_lock<std::mutex> threadLock(threadMutex_);
37     if (enableState_ == enable) {
38         return;
39     }
40     RS_LOGI("RSRealtimeRefreshRateManager state change: %{public}d -> %{public}d",
41         static_cast<bool>(enableState_), enable);
42     enableState_ = enable;
43 
44     if (enableState_) {
45         updateFpsThread_ = std::thread([&]() {
46             auto &hgmCore = HgmCore::Instance();
47             uint32_t lastRefreshRate = 0;
48             uint32_t lastRealtimeRefreshRate = 0;
49             currRealtimeRefreshRate_ = 1;
50             std::mutex condMutex;
51             while (enableState_) {
52                 std::unique_lock<std::mutex> lock(condMutex);
53                 auto st = std::chrono::steady_clock::now();
54                 realtimeFrameCount_ = 0;
55                 threadCondVar_.wait_for(lock, NS_FPS_SHOW_INTERVAL_);
56                 uint32_t realtimeFrameCount = realtimeFrameCount_;
57                 auto et = std::chrono::steady_clock::now();
58 
59                 RS_TRACE_BEGIN("RSRealtimeRefreshRateManager:Cal draw fps");
60 
61                 auto diff = std::chrono::duration_cast<std::chrono::nanoseconds>(et - st);
62                 uint32_t fps = std::round(realtimeFrameCount * static_cast<float>(NS_PER_S_) / diff.count());
63                 fps = std::max(1u, fps);
64                 if (fps <= IDLE_FPS_THRESHOLD_) {
65                     fps = 1;
66                 }
67 
68                 currRealtimeRefreshRate_ = fps;
69 
70                 auto screenId = hgmCore.GetFrameRateMgr()->GetCurScreenId();
71                 auto refreshRate = hgmCore.GetScreenCurrentRefreshRate(screenId);
72                 // draw
73                 if (lastRealtimeRefreshRate != currRealtimeRefreshRate_ || lastRefreshRate != refreshRate) {
74                     lastRefreshRate = refreshRate;
75                     lastRealtimeRefreshRate = currRealtimeRefreshRate_;
76                     RSMainThread::Instance()->SetDirtyFlag();
77                     RSMainThread::Instance()->RequestNextVSync();
78                     RS_LOGD("RSRealtimeRefreshRateManager: fps update: %{public}ud", currRealtimeRefreshRate_);
79                 }
80 
81                 RS_TRACE_END();
82             }
83         });
84         RS_LOGD("RSRealtimeRefreshRateManager: enable");
85     } else {
86         threadCondVar_.notify_all();
87         if (updateFpsThread_.joinable()) {
88             updateFpsThread_.join();
89         }
90         RS_LOGD("RSRealtimeRefreshRateManager: disable");
91     }
92 }
93 }