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 "vsync_station.h"
17
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 if (destroyed_) {
38 return;
39 }
40 vsyncCallbacks_.insert(vsyncCallback);
41
42 Init();
43 if (hasRequestedVsync_) {
44 return;
45 }
46 hasRequestedVsync_ = true;
47 if (vsyncHandler_) {
48 vsyncHandler_->RemoveTask(VSYNC_TIME_OUT_TASK);
49 vsyncHandler_->PostTask(vsyncTimeoutCallback_, VSYNC_TIME_OUT_TASK, VSYNC_TIME_OUT_MILLISECONDS);
50 }
51 }
52 WindowFrameTraceImpl::GetInstance()->VsyncStartFrameTrace();
53 receiver_->RequestNextVSync(frameCallback_);
54 }
55
GetVSyncPeriod()56 int64_t VsyncStation::GetVSyncPeriod()
57 {
58 {
59 std::lock_guard<std::mutex> lock(mtx_);
60 Init();
61 }
62 int64_t period = 0;
63 if (receiver_ != nullptr) {
64 receiver_->GetVSyncPeriod(period);
65 }
66 return period;
67 }
68
Init()69 void VsyncStation::Init()
70 {
71 if (!hasInitVsyncReceiver_ || !vsyncHandler_) {
72 auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
73 if (mainEventRunner != nullptr && isMainHandlerAvailable_) {
74 WLOGI("MainEventRunner is available");
75 vsyncHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
76 } else {
77 WLOGI("MainEventRunner is not available");
78 if (!vsyncHandler_) {
79 vsyncHandler_ = std::make_shared<AppExecFwk::EventHandler>(
80 AppExecFwk::EventRunner::Create(VSYNC_THREAD_ID));
81 }
82 }
83 auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
84 while (receiver_ == nullptr) {
85 receiver_ = rsClient.CreateVSyncReceiver("WM_" + std::to_string(::getpid()), vsyncHandler_);
86 }
87 receiver_->Init();
88 hasInitVsyncReceiver_ = true;
89 }
90 }
91
RemoveCallback()92 void VsyncStation::RemoveCallback()
93 {
94 WLOGI("Remove Vsync callback");
95 std::lock_guard<std::mutex> lock(mtx_);
96 vsyncCallbacks_.clear();
97 }
98
VsyncCallbackInner(int64_t timestamp)99 void VsyncStation::VsyncCallbackInner(int64_t timestamp)
100 {
101 std::unordered_set<std::shared_ptr<VsyncCallback>> vsyncCallbacks;
102 {
103 std::lock_guard<std::mutex> lock(mtx_);
104 hasRequestedVsync_ = false;
105 vsyncCallbacks = vsyncCallbacks_;
106 vsyncCallbacks_.clear();
107 vsyncHandler_->RemoveTask(VSYNC_TIME_OUT_TASK);
108 }
109 for (const auto& callback: vsyncCallbacks) {
110 callback->onCallback(timestamp);
111 }
112 }
113
OnVsync(int64_t timestamp,void * client)114 void VsyncStation::OnVsync(int64_t timestamp, void* client)
115 {
116 auto vsyncClient = static_cast<VsyncStation*>(client);
117 if (vsyncClient) {
118 vsyncClient->VsyncCallbackInner(timestamp);
119 WindowFrameTraceImpl::GetInstance()->VsyncStopFrameTrace();
120 } else {
121 WLOGFE("VsyncClient is null");
122 }
123 }
124
OnVsyncTimeOut()125 void VsyncStation::OnVsyncTimeOut()
126 {
127 WLOGW("Vsync time out");
128 std::lock_guard<std::mutex> lock(mtx_);
129 hasRequestedVsync_ = false;
130 }
131 }
132 }
133