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
37 int64_t now = 0;
38 ssize_t ret = 0;
39 ssize_t dataCount = 0;
40 do {
41 // only take the latest timestamp
42 ret = read(fileDescriptor, &now, sizeof(int64_t));
43 if (ret == 0) {
44 return;
45 }
46 if (ret == -1) {
47 if (errno == EINTR) {
48 ret = 0;
49 continue;
50 }
51 } else {
52 dataCount += ret;
53 }
54 } while (ret != -1);
55
56 VSyncCallback cb = nullptr;
57 {
58 std::lock_guard<std::mutex> locker(mtx_);
59 cb = vsyncCallbacks_;
60 }
61 VLOGD("dataCount:%{public}d, cb == nullptr:%{public}d", dataCount, (cb == nullptr));
62 ScopedBytrace func("ReceiveVsync, dataCount:" + std::to_string(dataCount) + "bytes, now:" + std::to_string(now));
63 if (dataCount > 0 && cb != nullptr) {
64 cb(now, userData_);
65 }
66 }
67
VSyncReceiver(const sptr<IVSyncConnection> & conn,const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & looper,const std::string & name)68 VSyncReceiver::VSyncReceiver(const sptr<IVSyncConnection>& conn,
69 const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper,
70 const std::string& name)
71 : connection_(conn), looper_(looper),
72 listener_(std::make_shared<VSyncCallBackListener>()),
73 init_(false),
74 fd_(INVALID_FD),
75 name_(name)
76 {
77 };
78
Init()79 VsyncError VSyncReceiver::Init()
80 {
81 std::lock_guard<std::mutex> locker(initMutex_);
82 if (init_) {
83 return VSYNC_ERROR_OK;
84 }
85 if (connection_ == nullptr) {
86 return VSYNC_ERROR_NULLPTR;
87 }
88
89 VsyncError ret = connection_->GetReceiveFd(fd_);
90 if (ret != VSYNC_ERROR_OK) {
91 return ret;
92 }
93
94 int32_t retVal = fcntl(fd_, F_SETFL, O_NONBLOCK); // set fd to NonBlock mode
95 if (retVal != 0) {
96 VLOGW("%{public}s fcntl set fd_ NonBlock failed", __func__);
97 }
98
99 if (looper_ == nullptr) {
100 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
101 looper_ = std::make_shared<AppExecFwk::EventHandler>(runner);
102 runner->Run();
103 }
104
105 looper_->AddFileDescriptorListener(fd_, OHOS::AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener_);
106 init_ = true;
107 return VSYNC_ERROR_OK;
108 }
109
~VSyncReceiver()110 VSyncReceiver::~VSyncReceiver()
111 {
112 if (fd_ != INVALID_FD) {
113 looper_->RemoveFileDescriptorListener(fd_);
114 close(fd_);
115 fd_ = INVALID_FD;
116 }
117 }
118
RequestNextVSync(FrameCallback callback)119 VsyncError VSyncReceiver::RequestNextVSync(FrameCallback callback)
120 {
121 std::lock_guard<std::mutex> locker(initMutex_);
122 if (!init_) {
123 return VSYNC_ERROR_API_FAILED;
124 }
125 listener_->SetCallback(callback);
126 ScopedBytrace func("VSyncReceiver::RequestNextVSync_pid:" + std::to_string(GetRealPid()) + "_name:" + name_);
127 return connection_->RequestNextVSync();
128 }
129
SetVSyncRate(FrameCallback callback,int32_t rate)130 VsyncError VSyncReceiver::SetVSyncRate(FrameCallback callback, int32_t rate)
131 {
132 std::lock_guard<std::mutex> locker(initMutex_);
133 if (!init_) {
134 return VSYNC_ERROR_API_FAILED;
135 }
136 listener_->SetCallback(callback);
137 return connection_->SetVSyncRate(rate);
138 }
139 } // namespace Rosen
140 } // namespace OHOS
141