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 #include "transaction/rs_interfaces.h"
18 #include "window_manager_hilog.h"
19
20 namespace OHOS {
21 namespace Rosen {
22 namespace {
23 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "VsyncStation"};
24 const std::string VSYNC_TIME_OUT_TASK = "vsync_time_out_task";
25 constexpr int64_t VSYNC_TIME_OUT_MILLISECONDS = 600;
26 }
WM_IMPLEMENT_SINGLE_INSTANCE(VsyncStation)27 WM_IMPLEMENT_SINGLE_INSTANCE(VsyncStation)
28
29 void VsyncStation::RequestVsync(CallbackType type, const std::shared_ptr<VsyncCallback>& vsyncCallback)
30 {
31 {
32 std::lock_guard<std::mutex> lock(mtx_);
33 auto iter = vsyncCallbacks_.find(type);
34 if (iter == vsyncCallbacks_.end()) {
35 WLOGFE("wrong callback type.");
36 return;
37 }
38 iter->second.insert(vsyncCallback);
39
40 if (vsyncHandler_ == nullptr) {
41 auto mainEventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
42 if (mainEventRunner != nullptr && isMainHandlerAvailable_) {
43 vsyncHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainEventRunner);
44 } else {
45 WLOGFE("MainEventRunner is not available, create a new EventRunner for vsyncHandler_.");
46 vsyncHandler_ = std::make_shared<AppExecFwk::EventHandler>(
47 AppExecFwk::EventRunner::Create(VSYNC_THREAD_ID));
48 }
49 auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
50 while (receiver_ == nullptr) {
51 receiver_ = rsClient.CreateVSyncReceiver("WM_" + std::to_string(::getpid()), vsyncHandler_);
52 }
53 receiver_->Init();
54 }
55 if (hasRequestedVsync_) {
56 return;
57 }
58 hasRequestedVsync_ = true;
59 vsyncCount_++;
60 if (vsyncCount_ & 0x01) { // write log every 2 vsync
61 WLOGFI("Request next vsync.");
62 }
63 vsyncHandler_->RemoveTask(VSYNC_TIME_OUT_TASK);
64 vsyncHandler_->PostTask(vsyncTimeoutCallback_, VSYNC_TIME_OUT_TASK, VSYNC_TIME_OUT_MILLISECONDS);
65 }
66 receiver_->RequestNextVSync(frameCallback_);
67 }
68
RemoveCallback(CallbackType type,const std::shared_ptr<VsyncCallback> & vsyncCallback)69 void VsyncStation::RemoveCallback(CallbackType type, const std::shared_ptr<VsyncCallback>& vsyncCallback)
70 {
71 WLOGFI("Remove callback, type: %{public}u", type);
72 std::lock_guard<std::mutex> lock(mtx_);
73 auto iter = vsyncCallbacks_.find(type);
74 if (iter == vsyncCallbacks_.end()) {
75 WLOGFE("wrong callback type.");
76 return;
77 }
78 iter->second.erase(vsyncCallback);
79 }
80
VsyncCallbackInner(int64_t timestamp)81 void VsyncStation::VsyncCallbackInner(int64_t timestamp)
82 {
83 std::map<CallbackType, std::unordered_set<std::shared_ptr<VsyncCallback>>> vsyncCallbacks;
84 {
85 std::lock_guard<std::mutex> lock(mtx_);
86 hasRequestedVsync_ = false;
87 vsyncCallbacks = vsyncCallbacks_;
88 for (auto& vsyncCallbacksSet: vsyncCallbacks_) {
89 vsyncCallbacksSet.second.clear();
90 }
91 vsyncHandler_->RemoveTask(VSYNC_TIME_OUT_TASK);
92 if (vsyncCount_ & 0x01) { // write log every 2 vsync
93 WLOGFI("On vsync callback.");
94 }
95 }
96 for (auto& vsyncCallbacksSet: vsyncCallbacks) {
97 for (const auto& callback: vsyncCallbacksSet.second) {
98 callback->onCallback(timestamp);
99 }
100 }
101 }
102
OnVsync(int64_t timestamp,void * client)103 void VsyncStation::OnVsync(int64_t timestamp, void* client)
104 {
105 auto vsyncClient = static_cast<VsyncStation*>(client);
106 if (vsyncClient) {
107 vsyncClient->VsyncCallbackInner(timestamp);
108 } else {
109 WLOGFE("VsyncClient is null");
110 }
111 }
112
OnVsyncTimeOut()113 void VsyncStation::OnVsyncTimeOut()
114 {
115 WLOGFE("Vsync time out");
116 std::lock_guard<std::mutex> lock(mtx_);
117 hasRequestedVsync_ = false;
118 }
119 }
120 }