1 /*
2 * Copyright (c) 2021 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_receiver.h"
17 #include <memory>
18 #include <unistd.h>
19 #include <scoped_bytrace.h>
20 #include <fcntl.h>
21 #include "event_handler.h"
22 #include "graphic_common.h"
23 #include "vsync_log.h"
24 #include "sandbox_utils.h"
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr int32_t INVALID_FD = -1;
30 }
OnReadable(int32_t fileDescriptor)31 void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
32 {
33 if (fileDescriptor < 0) {
34 return;
35 }
36 // 3 is array size.
37 int64_t data[3];
38 int64_t now = 0;
39 ssize_t ret = 0;
40 ssize_t dataCount = 0;
41 do {
42 // only take the latest timestamp
43 ret = read(fileDescriptor, data, sizeof(data));
44 if (ret == 0) {
45 return;
46 }
47 if (ret == -1) {
48 if (errno == EINTR) {
49 ret = 0;
50 continue;
51 }
52 } else {
53 dataCount += ret;
54 }
55 } while (ret != -1);
56
57 VSyncCallback cb = nullptr;
58 {
59 std::lock_guard<std::mutex> locker(mtx_);
60 cb = vsyncCallbacks_;
61 }
62 now = data[0];
63 period_ = data[1] - data[0];
64 periodShared_ = data[1] - data[0];
65 timeStamp_ = data[0];
66 timeStampShared_ = data[0];
67
68 VLOGD("dataCount:%{public}d, cb == nullptr:%{public}d", dataCount, (cb == nullptr));
69 // 1, 2: index of array data.
70 ScopedBytrace func("ReceiveVsync dataCount:" + std::to_string(dataCount) + "bytes now:" + std::to_string(now) +
71 " expectedEnd:" + std::to_string(data[1]) + " vsyncId:" + std::to_string(data[2]));
72 if (dataCount > 0 && cb != nullptr) {
73 cb(now, userData_);
74 }
75 }
76
VSyncReceiver(const sptr<IVSyncConnection> & conn,const sptr<IRemoteObject> & token,const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & looper,const std::string & name)77 VSyncReceiver::VSyncReceiver(const sptr<IVSyncConnection>& conn,
78 const sptr<IRemoteObject>& token,
79 const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper,
80 const std::string& name)
81 : connection_(conn), token_(token), looper_(looper),
82 listener_(std::make_shared<VSyncCallBackListener>()),
83 init_(false),
84 fd_(INVALID_FD),
85 name_(name)
86 {
87 };
88
Init()89 VsyncError VSyncReceiver::Init()
90 {
91 std::lock_guard<std::mutex> locker(initMutex_);
92 if (init_) {
93 return VSYNC_ERROR_OK;
94 }
95 if (connection_ == nullptr) {
96 return VSYNC_ERROR_NULLPTR;
97 }
98
99 VsyncError ret = connection_->GetReceiveFd(fd_);
100 if (ret != VSYNC_ERROR_OK) {
101 return ret;
102 }
103
104 int32_t retVal = fcntl(fd_, F_SETFL, O_NONBLOCK); // set fd to NonBlock mode
105 if (retVal != 0) {
106 VLOGW("%{public}s fcntl set fd_ NonBlock failed", __func__);
107 }
108
109 if (looper_ == nullptr) {
110 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
111 looper_ = std::make_shared<AppExecFwk::EventHandler>(runner);
112 runner->Run();
113 }
114
115 looper_->AddFileDescriptorListener(fd_, OHOS::AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener_, "vSyncTask");
116 init_ = true;
117 return VSYNC_ERROR_OK;
118 }
119
~VSyncReceiver()120 VSyncReceiver::~VSyncReceiver()
121 {
122 if (fd_ != INVALID_FD) {
123 looper_->RemoveFileDescriptorListener(fd_);
124 close(fd_);
125 fd_ = INVALID_FD;
126 Destroy();
127 }
128 }
129
RequestNextVSync(FrameCallback callback)130 VsyncError VSyncReceiver::RequestNextVSync(FrameCallback callback)
131 {
132 std::lock_guard<std::mutex> locker(initMutex_);
133 if (!init_) {
134 return VSYNC_ERROR_API_FAILED;
135 }
136 listener_->SetCallback(callback);
137 ScopedDebugTrace func("VSyncReceiver::RequestNextVSync:" + name_);
138 return connection_->RequestNextVSync();
139 }
140
SetVSyncRate(FrameCallback callback,int32_t rate)141 VsyncError VSyncReceiver::SetVSyncRate(FrameCallback callback, int32_t rate)
142 {
143 std::lock_guard<std::mutex> locker(initMutex_);
144 if (!init_) {
145 return VSYNC_ERROR_API_FAILED;
146 }
147 listener_->SetCallback(callback);
148 return connection_->SetVSyncRate(rate);
149 }
150
GetVSyncPeriod(int64_t & period)151 VsyncError VSyncReceiver::GetVSyncPeriod(int64_t &period)
152 {
153 int64_t timeStamp;
154 return GetVSyncPeriodAndLastTimeStamp(period, timeStamp);
155 }
156
GetVSyncPeriodAndLastTimeStamp(int64_t & period,int64_t & timeStamp,bool isThreadShared)157 VsyncError VSyncReceiver::GetVSyncPeriodAndLastTimeStamp(int64_t &period, int64_t &timeStamp, bool isThreadShared)
158 {
159 std::lock_guard<std::mutex> locker(initMutex_);
160 if (!init_) {
161 return VSYNC_ERROR_API_FAILED;
162 }
163 if (isThreadShared == false) {
164 if (listener_->period_ == 0 || listener_->timeStamp_ == 0) {
165 VLOGE("%{public}s Hardware vsync is not available. please try again later!", __func__);
166 return VSYNC_ERROR_API_FAILED;
167 }
168 period = listener_->period_;
169 timeStamp = listener_->timeStamp_;
170 } else {
171 if (listener_->periodShared_ == 0 || listener_->timeStampShared_ == 0) {
172 return VSYNC_ERROR_API_FAILED;
173 }
174 period = listener_->periodShared_;
175 timeStamp = listener_->timeStampShared_;
176 }
177 ScopedBytrace func("VSyncReceiver:period:" + std::to_string(period) + " timeStamp:" + std::to_string(timeStamp) +
178 " isThreadShared:" + std::to_string(isThreadShared));
179 return VSYNC_ERROR_OK;
180 }
181
CloseVsyncReceiverFd()182 void VSyncReceiver::CloseVsyncReceiverFd()
183 {
184 if (looper_ != nullptr) {
185 looper_->RemoveFileDescriptorListener(fd_);
186 VLOGI("%{public}s looper remove fd listener, fd=%{public}d", __func__, fd_);
187 }
188
189 if (fd_ > 0) {
190 close(fd_);
191 fd_ = INVALID_FD;
192 }
193 }
194
Destroy()195 VsyncError VSyncReceiver::Destroy()
196 {
197 std::lock_guard<std::mutex> locker(initMutex_);
198 if (!init_) {
199 return VSYNC_ERROR_API_FAILED;
200 }
201 return connection_->Destroy();
202 }
203 } // namespace Rosen
204 } // namespace OHOS
205