• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_) {
40             auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
41             while (receiver_ == nullptr) {
42                 receiver_ = rsClient.CreateVSyncReceiver("WM_" + std::to_string(getpid()));
43             }
44             receiver_->Init();
45             hasInitVsyncReceiver_ = true;
46         }
47         if (hasRequestedVsync_) {
48             return;
49         }
50         hasRequestedVsync_ = true;
51     }
52     receiver_->RequestNextVSync(frameCallback_);
53 }
54 
GetVSyncPeriod()55 int64_t VsyncStation::GetVSyncPeriod()
56 {
57     return 0;
58 }
59 
Init()60 void VsyncStation::Init()
61 {
62 }
63 
RemoveCallback()64 void VsyncStation::RemoveCallback()
65 {
66     WLOGI("Remove Vsync callback");
67     std::lock_guard<std::mutex> lock(mtx_);
68     vsyncCallbacks_.clear();
69 }
70 
VsyncCallbackInner(int64_t timestamp)71 void VsyncStation::VsyncCallbackInner(int64_t timestamp)
72 {
73     std::unordered_set<std::shared_ptr<VsyncCallback>> vsyncCallbacks;
74     {
75         std::lock_guard<std::mutex> lock(mtx_);
76         hasRequestedVsync_ = false;
77         vsyncCallbacks = vsyncCallbacks_;
78         vsyncCallbacks_.clear();
79     }
80     for (const auto& callback: vsyncCallbacks) {
81         callback->onCallback(timestamp);
82     }
83 }
84 
OnVsync(int64_t timestamp,void * client)85 void VsyncStation::OnVsync(int64_t timestamp, void* client)
86 {
87     auto vsyncClient = static_cast<VsyncStation*>(client);
88     if (vsyncClient) {
89         vsyncClient->VsyncCallbackInner(timestamp);
90     } else {
91         WLOGFE("VsyncClient is null");
92     }
93 }
94 
OnVsyncTimeOut()95 void VsyncStation::OnVsyncTimeOut()
96 {
97     WLOGW("Vsync time out");
98     std::lock_guard<std::mutex> lock(mtx_);
99     hasRequestedVsync_ = false;
100 }
101 }
102 }
103