• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <unistd.h>
17 #include "vsync_station.h"
18 #include "window_frame_trace.h"
19 #include "transaction/rs_interfaces.h"
20 #include "window_manager_hilog.h"
21 
22 using namespace FRAME_TRACE;
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "VsyncStation"};
28     const std::string VSYNC_TIME_OUT_TASK = "vsync_time_out_task";
29     constexpr int64_t VSYNC_TIME_OUT_MILLISECONDS = 600;
30 }
WM_IMPLEMENT_SINGLE_INSTANCE(VsyncStation)31 WM_IMPLEMENT_SINGLE_INSTANCE(VsyncStation)
32 
33 void VsyncStation::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
34 {
35     {
36         std::lock_guard<std::mutex> lock(mtx_);
37         vsyncCallbacks_.insert(vsyncCallback);
38 
39         if (!hasInitVsyncReceiver_ || !vsyncHandler_) {
40             auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
41             if (mainEventRunner != nullptr && isMainHandlerAvailable_) {
42                 WLOGFI("MainEventRunner is available");
43                 vsyncHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
44             } else {
45                 WLOGFI("MainEventRunner is not available");
46                 if (!vsyncHandler_) {
47                     vsyncHandler_ = std::make_shared<AppExecFwk::EventHandler>(
48                         AppExecFwk::EventRunner::Create(VSYNC_THREAD_ID));
49                 }
50             }
51             auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
52             while (receiver_ == nullptr) {
53                 receiver_ = rsClient.CreateVSyncReceiver("WM_" + std::to_string(::getpid()), vsyncHandler_);
54             }
55             receiver_->Init();
56             hasInitVsyncReceiver_ = true;
57         }
58         if (hasRequestedVsync_) {
59             return;
60         }
61         hasRequestedVsync_ = true;
62         if (vsyncHandler_) {
63             vsyncHandler_->RemoveTask(VSYNC_TIME_OUT_TASK);
64             vsyncHandler_->PostTask(vsyncTimeoutCallback_, VSYNC_TIME_OUT_TASK, VSYNC_TIME_OUT_MILLISECONDS);
65         }
66     }
67     WindowFrameTraceImpl::GetInstance()->VsyncStartFrameTrace();
68     receiver_->RequestNextVSync(frameCallback_);
69 }
70 
RemoveCallback()71 void VsyncStation::RemoveCallback()
72 {
73     std::lock_guard<std::mutex> lock(mtx_);
74     WLOGFI("[WM] Remove Vsync callback");
75     vsyncCallbacks_.clear();
76 }
77 
VsyncCallbackInner(int64_t timestamp)78 void VsyncStation::VsyncCallbackInner(int64_t timestamp)
79 {
80     std::unordered_set<std::shared_ptr<VsyncCallback>> vsyncCallbacks;
81     {
82         std::lock_guard<std::mutex> lock(mtx_);
83         hasRequestedVsync_ = false;
84         vsyncCallbacks = vsyncCallbacks_;
85         vsyncCallbacks_.clear();
86         vsyncHandler_->RemoveTask(VSYNC_TIME_OUT_TASK);
87     }
88     for (const auto& callback: vsyncCallbacks) {
89         callback->onCallback(timestamp);
90     }
91 }
92 
OnVsync(int64_t timestamp,void * client)93 void VsyncStation::OnVsync(int64_t timestamp, void* client)
94 {
95     auto vsyncClient = static_cast<VsyncStation*>(client);
96     if (vsyncClient) {
97         vsyncClient->VsyncCallbackInner(timestamp);
98         WindowFrameTraceImpl::GetInstance()->VsyncStopFrameTrace();
99     } else {
100         WLOGFE("VsyncClient is null");
101     }
102 }
103 
OnVsyncTimeOut()104 void VsyncStation::OnVsyncTimeOut()
105 {
106     std::lock_guard<std::mutex> lock(mtx_);
107     WLOGFI("[WM] Vsync time out");
108     hasRequestedVsync_ = false;
109 }
110 }
111 }
112