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 "vsync_station.h"
17
18 #include <unistd.h>
19 #include "transaction/rs_interfaces.h"
20 #include "window_manager_hilog.h"
21
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "VsyncStation"};
27 }
WM_IMPLEMENT_SINGLE_INSTANCE(VsyncStation)28 WM_IMPLEMENT_SINGLE_INSTANCE(VsyncStation)
29
30 void VsyncStation::RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback)
31 {
32 {
33 std::lock_guard<std::mutex> lock(mtx_);
34 if (destroyed_) {
35 return;
36 }
37 vsyncCallbacks_.insert(vsyncCallback);
38
39 if (!hasInitVsyncReceiver_ || !vsyncHandler_) {
40 if (!vsyncHandler_) {
41 vsyncHandler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>();
42 }
43
44 auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
45 while (receiver_ == nullptr) {
46 receiver_ = rsClient.CreateVSyncReceiver("WM_" + std::to_string(getpid()), vsyncHandler_);
47 }
48 receiver_->Init();
49 hasInitVsyncReceiver_ = true;
50 }
51 if (hasRequestedVsync_) {
52 return;
53 }
54 hasRequestedVsync_ = true;
55 }
56 receiver_->RequestNextVSync(frameCallback_);
57 }
58
GetVSyncPeriod()59 int64_t VsyncStation::GetVSyncPeriod()
60 {
61 return 0;
62 }
63
Init()64 void VsyncStation::Init()
65 {
66 }
67
RemoveCallback()68 void VsyncStation::RemoveCallback()
69 {
70 WLOGI("Remove Vsync callback");
71 std::lock_guard<std::mutex> lock(mtx_);
72 vsyncCallbacks_.clear();
73 }
74
VsyncCallbackInner(int64_t timestamp)75 void VsyncStation::VsyncCallbackInner(int64_t timestamp)
76 {
77 std::unordered_set<std::shared_ptr<VsyncCallback>> vsyncCallbacks;
78 {
79 std::lock_guard<std::mutex> lock(mtx_);
80 hasRequestedVsync_ = false;
81 vsyncCallbacks = vsyncCallbacks_;
82 vsyncCallbacks_.clear();
83 }
84 for (const auto& callback: vsyncCallbacks) {
85 callback->onCallback(timestamp);
86 }
87 }
88
OnVsync(int64_t timestamp,void * client)89 void VsyncStation::OnVsync(int64_t timestamp, void* client)
90 {
91 auto vsyncClient = static_cast<VsyncStation*>(client);
92 if (vsyncClient) {
93 vsyncClient->VsyncCallbackInner(timestamp);
94 } else {
95 WLOGFE("VsyncClient is null");
96 }
97 }
98
OnVsyncTimeOut()99 void VsyncStation::OnVsyncTimeOut()
100 {
101 WLOGW("Vsync time out");
102 std::lock_guard<std::mutex> lock(mtx_);
103 hasRequestedVsync_ = false;
104 }
105 }
106 }
107