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 VLOGD("dataCount:%{public}d, cb == nullptr:%{public}d", dataCount, (cb == nullptr));
64 // 1, 2: index of array data.
65 ScopedBytrace func("ReceiveVsync dataCount:" + std::to_string(dataCount) + "bytes now:" + std::to_string(now) +
66 " expectedEnd:" + std::to_string(data[1]) + " vsyncId:" + std::to_string(data[2]));
67 if (dataCount > 0 && cb != nullptr) {
68 cb(now, userData_);
69 }
70 }
71
VSyncReceiver(const sptr<IVSyncConnection> & conn,const sptr<IRemoteObject> & token,const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & looper,const std::string & name)72 VSyncReceiver::VSyncReceiver(const sptr<IVSyncConnection>& conn,
73 const sptr<IRemoteObject>& token,
74 const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper,
75 const std::string& name)
76 : connection_(conn), token_(token), looper_(looper),
77 listener_(std::make_shared<VSyncCallBackListener>()),
78 init_(false),
79 fd_(INVALID_FD),
80 name_(name)
81 {
82 };
83
Init()84 VsyncError VSyncReceiver::Init()
85 {
86 std::lock_guard<std::mutex> locker(initMutex_);
87 if (init_) {
88 return VSYNC_ERROR_OK;
89 }
90 if (connection_ == nullptr) {
91 return VSYNC_ERROR_NULLPTR;
92 }
93
94 VsyncError ret = connection_->GetReceiveFd(fd_);
95 if (ret != VSYNC_ERROR_OK) {
96 return ret;
97 }
98
99 int32_t retVal = fcntl(fd_, F_SETFL, O_NONBLOCK); // set fd to NonBlock mode
100 if (retVal != 0) {
101 VLOGW("%{public}s fcntl set fd_ NonBlock failed", __func__);
102 }
103
104 if (looper_ == nullptr) {
105 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
106 looper_ = std::make_shared<AppExecFwk::EventHandler>(runner);
107 runner->Run();
108 }
109
110 looper_->AddFileDescriptorListener(fd_, OHOS::AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener_);
111 init_ = true;
112 return VSYNC_ERROR_OK;
113 }
114
~VSyncReceiver()115 VSyncReceiver::~VSyncReceiver()
116 {
117 if (fd_ != INVALID_FD) {
118 looper_->RemoveFileDescriptorListener(fd_);
119 close(fd_);
120 fd_ = INVALID_FD;
121 }
122 }
123
RequestNextVSync(FrameCallback callback)124 VsyncError VSyncReceiver::RequestNextVSync(FrameCallback callback)
125 {
126 std::lock_guard<std::mutex> locker(initMutex_);
127 if (!init_) {
128 return VSYNC_ERROR_API_FAILED;
129 }
130 listener_->SetCallback(callback);
131 ScopedBytrace func("VSyncReceiver::_name:" + name_);
132 return connection_->RequestNextVSync();
133 }
134
SetVSyncRate(FrameCallback callback,int32_t rate)135 VsyncError VSyncReceiver::SetVSyncRate(FrameCallback callback, int32_t rate)
136 {
137 std::lock_guard<std::mutex> locker(initMutex_);
138 if (!init_) {
139 return VSYNC_ERROR_API_FAILED;
140 }
141 listener_->SetCallback(callback);
142 return connection_->SetVSyncRate(rate);
143 }
144
GetVSyncPeriod(int64_t & period)145 VsyncError VSyncReceiver::GetVSyncPeriod(int64_t &period)
146 {
147 std::lock_guard<std::mutex> locker(initMutex_);
148 if (!init_) {
149 return VSYNC_ERROR_API_FAILED;
150 }
151 VsyncError ret = connection_->GetVSyncPeriod(period);
152 if (ret != VSYNC_ERROR_OK) {
153 VLOGE("%{public}s get vsync period failed", __func__);
154 }
155 return ret;
156 }
157 } // namespace Rosen
158 } // namespace OHOS
159